diff --git a/contracts/rust/Cargo.toml b/contracts/rust/Cargo.toml index 8986c28e..4e35bfce 100644 --- a/contracts/rust/Cargo.toml +++ b/contracts/rust/Cargo.toml @@ -42,9 +42,9 @@ commit = { git = "https://github.com/EspressoSystems/commit.git", tag = "0.1.0" espresso-macros = { git = "https://github.com/EspressoSystems/espresso-macros.git" } # We need the legacy feature in order to avoid gas estimation issues. See https://github.com/gakonst/ethers-rs/issues/825 -ethers = { features = ["legacy"], git = "https://github.com/gakonst/ethers-rs" } -ethers-contract = { features = ["legacy"], git = "https://github.com/gakonst/ethers-rs" } -ethers-core = { features = ["legacy"], git = "https://github.com/gakonst/ethers-rs" } +ethers = { git = "https://github.com/gakonst/ethers-rs" } +ethers-contract = { git = "https://github.com/gakonst/ethers-rs" } +ethers-core = { git = "https://github.com/gakonst/ethers-rs" } futures = "0.3.16" generic-array = { version = "0.14.4", features = ["serde"] } hex = "0.4.3" diff --git a/contracts/rust/src/cape/events.rs b/contracts/rust/src/cape/events.rs index eec01dbd..43b8a922 100644 --- a/contracts/rust/src/cape/events.rs +++ b/contracts/rust/src/cape/events.rs @@ -28,7 +28,7 @@ mod tests { submit_block::{fetch_cape_memos, submit_cape_block_with_memos}, BlockWithMemos, CapeBlock, }, - ethereum::EthConnection, + ethereum::{EthConnection, GAS_LIMIT_OVERRIDE}, ledger::CapeLedger, types::{GenericInto, MerkleRootSol}, }; @@ -96,7 +96,7 @@ mod tests { &connection.contract, block_with_memos.clone(), BlockNumber::Latest, - 10_000_000, // gas limit + GAS_LIMIT_OVERRIDE, // gas limit ) .await? .await?; diff --git a/contracts/rust/src/cape/submit_block.rs b/contracts/rust/src/cape/submit_block.rs index 66675988..82abd70a 100644 --- a/contracts/rust/src/cape/submit_block.rs +++ b/contracts/rust/src/cape/submit_block.rs @@ -87,10 +87,6 @@ pub async fn submit_cape_block_with_memos( .fill_transaction(&mut tx, Some(block_number.into())) .await?; - // Increase the gas price by 20% to increase the chances for the transaction - // to be mined. - tx.set_gas_price(tx.gas_price().unwrap() * 12 / 10); - // The estimated gas cost can be too low. For example, if a deposit is made // in an earlier transaction in the same block the estimate would not include // the cost for crediting the deposit. @@ -111,6 +107,7 @@ mod tests { assertion::{EnsureMined, Matcher}, cape::CapeBlock, deploy::deploy_test_cape, + ethereum::GAS_LIMIT_OVERRIDE, ledger::CapeLedger, types::{GenericInto, MerkleRootSol, NullifierSol}, }; @@ -227,14 +224,17 @@ mod tests { .add_root(root.generic_into::().0) .send() .await? - .await?; + .await? + .ensure_mined(); // Submit to the contract contract .submit_cape_block(cape_block.into()) + .gas(GAS_LIMIT_OVERRIDE) // runs out of gas with estimate .send() .await? - .await?; + .await? + .ensure_mined(); // Check that now the nullifier has been inserted assert!( @@ -353,6 +353,7 @@ mod tests { contract .submit_cape_block(cape_block.into()) + .gas(GAS_LIMIT_OVERRIDE) .send() .await? .await? @@ -377,13 +378,16 @@ mod tests { .add_root(root.generic_into::().0) .send() .await? - .await?; + .await? + .ensure_mined(); contract .submit_cape_block(cape_block.into()) + .gas(GAS_LIMIT_OVERRIDE) .send() .await? - .await?; + .await? + .ensure_mined(); let logs = contract .block_committed_filter() diff --git a/contracts/rust/src/cape_e2e_test_mint.rs b/contracts/rust/src/cape_e2e_test_mint.rs index d28a1e74..8de6c7fb 100644 --- a/contracts/rust/src/cape_e2e_test_mint.rs +++ b/contracts/rust/src/cape_e2e_test_mint.rs @@ -8,7 +8,9 @@ #![cfg(test)] #![deny(warnings)] +use crate::assertion::EnsureMined; use crate::deploy::deploy_test_cape; +use crate::ethereum::GAS_LIMIT_OVERRIDE; use crate::{ cape::*, ledger::CapeLedger, @@ -87,43 +89,38 @@ async fn test_mint_maybe_submit(should_submit: bool) -> Result<()> { let alice_rec_comm = RecordCommitment::from(&alice_rec1); let alice_rec_field_elem = alice_rec_comm.to_field_element(); t.push(alice_rec_field_elem); - let alice_rec_path = t.get_leaf(0).expect_ok().unwrap().1.path; + let alice_rec_path = t.get_leaf(0).expect_ok()?.1.path; assert_eq!( alice_rec_path.nodes.len(), CapeLedger::merkle_height() as usize ); if let Some(contract) = contract.as_ref() { - assert_eq!( - contract.get_root_value().call().await.unwrap(), - U256::from(0u64) - ); + assert_eq!(contract.get_root_value().call().await?, U256::from(0u64)); contract .set_initial_record_commitments(vec![field_to_u256(alice_rec_field_elem)]) + .gas(GAS_LIMIT_OVERRIDE) .send() - .await - .unwrap() - .await - .unwrap(); + .await? + .await? + .ensure_mined(); let first_root = t.commitment().root_value; - assert_eq!( - contract.get_num_leaves().call().await.unwrap(), - U256::from(1u64) - ); + assert_eq!(contract.get_num_leaves().call().await?, U256::from(1u64)); assert_eq!( - contract.get_root_value().call().await.unwrap(), + contract.get_root_value().call().await?, field_to_u256(first_root.to_scalar()) ); - assert!(contract - .contains_root(field_to_u256(first_root.to_scalar())) - .call() - .await - .unwrap()); + assert!( + contract + .contains_root(field_to_u256(first_root.to_scalar())) + .call() + .await? + ); } println!("Tree set up: {}s", now.elapsed().as_secs_f32()); @@ -162,9 +159,9 @@ async fn test_mint_maybe_submit(should_submit: bool) -> Result<()> { let description = "My Asset".as_bytes(); let code = AssetCode::new_domestic(seed, description); let policy = AssetPolicy::default(); - let new_coin = AssetDefinition::new(code, policy).unwrap(); + let new_coin = AssetDefinition::new(code, policy)?; - let (fee_info, _fee_ro) = TxnFeeInfo::new(&mut prng, fee_input, 1u64.into()).unwrap(); + let (fee_info, _fee_ro) = TxnFeeInfo::new(&mut prng, fee_input, 1u64.into())?; let mint_ro = RecordOpening::new( &mut prng, 1u64.into(), /* 1 less, for the transaction fee */ @@ -180,8 +177,7 @@ async fn test_mint_maybe_submit(should_submit: bool) -> Result<()> { description, fee_info, &prove_keys.mint, - ) - .unwrap() + )? }; println!("Mint generated: {}s", now.elapsed().as_secs_f32()); @@ -204,11 +200,9 @@ async fn test_mint_maybe_submit(should_submit: bool) -> Result<()> { let txn1_cape = CapeModelTxn::CAP(TransactionNote::Mint(Box::new(txn1))); - let (new_state, effects) = validator - .submit_operations(vec![CapeModelOperation::SubmitBlock(vec![ - txn1_cape.clone() - ])]) - .unwrap(); + let (new_state, effects) = validator.submit_operations(vec![ + CapeModelOperation::SubmitBlock(vec![txn1_cape.clone()]), + ])?; if let Some(contract) = contract.as_ref() { let miner = UserPubKey::default(); @@ -217,9 +211,11 @@ async fn test_mint_maybe_submit(should_submit: bool) -> Result<()> { // Submit to the contract contract .submit_cape_block(cape_block.into()) + .gas(GAS_LIMIT_OVERRIDE) .send() .await? - .await?; + .await? + .ensure_mined(); } println!("Mint validated & applied: {}s", now.elapsed().as_secs_f32()); @@ -250,7 +246,7 @@ async fn test_mint_maybe_submit(should_submit: bool) -> Result<()> { if let Some(contract) = contract.as_ref() { assert_eq!( - contract.get_root_value().call().await.unwrap(), + contract.get_root_value().call().await?, field_to_u256( new_state .ledger diff --git a/contracts/rust/src/cape_e2e_test_transfer.rs b/contracts/rust/src/cape_e2e_test_transfer.rs index 15c4ebac..5270bbd1 100644 --- a/contracts/rust/src/cape_e2e_test_transfer.rs +++ b/contracts/rust/src/cape_e2e_test_transfer.rs @@ -8,7 +8,9 @@ #![cfg(test)] #![deny(warnings)] +use crate::assertion::EnsureMined; use crate::deploy::deploy_test_cape; +use crate::ethereum::GAS_LIMIT_OVERRIDE; use crate::test_utils::keysets_for_test; use crate::{ cape::*, @@ -85,43 +87,37 @@ async fn test_2user_maybe_submit(should_submit: bool) -> Result<()> { let alice_rec_comm = RecordCommitment::from(&alice_rec1); let alice_rec_field_elem = alice_rec_comm.to_field_element(); t.push(alice_rec_field_elem); - let alice_rec_path = t.get_leaf(0).expect_ok().unwrap().1.path; + let alice_rec_path = t.get_leaf(0).expect_ok()?.1.path; assert_eq!( alice_rec_path.nodes.len(), CapeLedger::merkle_height() as usize ); if let Some(contract) = contract.as_ref() { - assert_eq!( - contract.get_root_value().call().await.unwrap(), - U256::from(0u64) - ); + assert_eq!(contract.get_root_value().call().await?, U256::from(0u64)); contract .set_initial_record_commitments(vec![field_to_u256(alice_rec_field_elem)]) .send() - .await - .unwrap() - .await - .unwrap(); + .await? + .await? + .ensure_mined(); let first_root = t.commitment().root_value; - assert_eq!( - contract.get_num_leaves().call().await.unwrap(), - U256::from(1u64) - ); + assert_eq!(contract.get_num_leaves().call().await?, U256::from(1u64)); assert_eq!( - contract.get_root_value().call().await.unwrap(), + contract.get_root_value().call().await?, field_to_u256(first_root.to_scalar()) ); - assert!(contract - .contains_root(field_to_u256(first_root.to_scalar())) - .call() - .await - .unwrap()); + assert!( + contract + .contains_root(field_to_u256(first_root.to_scalar())) + .call() + .await? + ); } println!("Tree set up: {}s", now.elapsed().as_secs_f32()); @@ -211,9 +207,11 @@ async fn test_2user_maybe_submit(should_submit: bool) -> Result<()> { // Submit to the contract contract .submit_cape_block(cape_block.into()) + .gas(GAS_LIMIT_OVERRIDE) .send() .await? - .await?; + .await? + .ensure_mined(); } println!( @@ -248,7 +246,7 @@ async fn test_2user_maybe_submit(should_submit: bool) -> Result<()> { if let Some(contract) = contract.as_ref() { assert_eq!( - contract.get_root_value().call().await.unwrap(), + contract.get_root_value().call().await?, field_to_u256( new_state .ledger @@ -269,7 +267,7 @@ async fn test_2user_maybe_submit(should_submit: bool) -> Result<()> { } let ro = bob_rec; - let merkle_path = wallet_merkle_tree.get_leaf(2).expect_ok().unwrap().1.path; + let merkle_path = wallet_merkle_tree.get_leaf(2).expect_ok()?.1.path; let merkle_root = new_state.ledger.record_merkle_commitment.root_value; let uid = 2; diff --git a/contracts/rust/src/ethereum.rs b/contracts/rust/src/ethereum.rs index b0cef5fe..93544053 100644 --- a/contracts/rust/src/ethereum.rs +++ b/contracts/rust/src/ethereum.rs @@ -25,6 +25,10 @@ use ethers::{ use std::{convert::TryFrom, env, fs, path::Path, sync::Arc, time::Duration}; +/// Supply this gas limit when the automatically filled estimated gas value is +/// too low and the transaction runs out of gas. +pub const GAS_LIMIT_OVERRIDE: u64 = 10_000_000; + /// Utility to interact with the CAPE contract on some Ethereum blockchain #[derive(Clone, Debug)] pub struct EthConnection { @@ -212,7 +216,7 @@ pub async fn deploy( link_unlinked_libraries(&mut bytecode, &client).await?; let factory = ContractFactory::new(abi.clone(), bytecode.into_bytes().unwrap(), client.clone()); - let contract = factory.deploy(constructor_args)?.legacy().send().await?; + let contract = factory.deploy(constructor_args)?.send().await?; Ok(contract) } diff --git a/contracts/rust/src/records_merkle_tree/mod.rs b/contracts/rust/src/records_merkle_tree/mod.rs index fcf23e83..dd53d74b 100644 --- a/contracts/rust/src/records_merkle_tree/mod.rs +++ b/contracts/rust/src/records_merkle_tree/mod.rs @@ -241,7 +241,6 @@ mod tests { let elems_u256 = insert_elements_into_jellyfish_mt(&mut mt, n_leaves_before); contract .update_records_merkle_tree(elems_u256) - .legacy() .send() .await .unwrap() @@ -254,7 +253,6 @@ mod tests { let elems_u256 = insert_elements_into_jellyfish_mt(&mut mt, n_leaves_after); contract .update_records_merkle_tree(elems_u256) - .legacy() .send() .await .unwrap() diff --git a/contracts/rust/tests/combined/smoke_tests_deployed_contract.rs b/contracts/rust/tests/combined/smoke_tests_deployed_contract.rs index f119d0a6..f75e4b28 100644 --- a/contracts/rust/tests/combined/smoke_tests_deployed_contract.rs +++ b/contracts/rust/tests/combined/smoke_tests_deployed_contract.rs @@ -9,7 +9,7 @@ use anyhow::Result; use cap_rust_sandbox::assertion::EnsureMined; use cap_rust_sandbox::cape::CapeBlock; use cap_rust_sandbox::deploy::{deploy_cape, deploy_erc20_token}; -use cap_rust_sandbox::ethereum::get_funded_client; +use cap_rust_sandbox::ethereum::{get_funded_client, GAS_LIMIT_OVERRIDE}; use cap_rust_sandbox::helpers::compute_faucet_key_pair_from_mnemonic; use cap_rust_sandbox::ledger::CapeLedger; use cap_rust_sandbox::model::{erc20_asset_description, Erc20Code, EthereumAddr}; @@ -197,6 +197,7 @@ async fn smoke_tests() -> Result<()> { cape_contract .submit_cape_block(cape_block.clone().into()) + .gas(GAS_LIMIT_OVERRIDE) // out of gas with estimate .send() .await? .await? diff --git a/contracts/rust/tests/combined/unwrapping.rs b/contracts/rust/tests/combined/unwrapping.rs index 1d28a814..a2cff9cc 100644 --- a/contracts/rust/tests/combined/unwrapping.rs +++ b/contracts/rust/tests/combined/unwrapping.rs @@ -9,7 +9,7 @@ use anyhow::Result; use cap_rust_sandbox::assertion::{EnsureMined, Matcher}; use cap_rust_sandbox::cape::CapeBlock; use cap_rust_sandbox::deploy::{deploy_cape, deploy_erc20_token}; -use cap_rust_sandbox::ethereum::get_funded_client; +use cap_rust_sandbox::ethereum::{get_funded_client, GAS_LIMIT_OVERRIDE}; use cap_rust_sandbox::ledger::CapeLedger; use cap_rust_sandbox::model::{erc20_asset_description, Erc20Code, EthereumAddr}; use cap_rust_sandbox::test_utils::{ @@ -83,7 +83,8 @@ async fn integration_test_unwrapping() -> Result<()> { .approve(cape_contract_address, amount_u256) .send() .await? - .await?; + .await? + .ensure_mined(); let wrapped_ro = RecordOpening::new( rng, @@ -105,7 +106,8 @@ async fn integration_test_unwrapping() -> Result<()> { ) .send() .await? - .await?; + .await? + .ensure_mined(); // Submit empty block to trigger the inclusion of the pending deposit record commitment into the merkle tree let miner = UserPubKey::default(); @@ -113,6 +115,7 @@ async fn integration_test_unwrapping() -> Result<()> { cape_contract .submit_cape_block(empty_block.clone().into()) + .gas(GAS_LIMIT_OVERRIDE) // out of gas with estimate .send() .await? .await? @@ -160,9 +163,11 @@ async fn integration_test_unwrapping() -> Result<()> { cape_contract .submit_cape_block(cape_block.clone().into()) + .gas(GAS_LIMIT_OVERRIDE) // out of gas with estimate .send() .await? .await? + .ensure_mined() .print_gas("Burn transaction"); // The recipient has received the ERC20 tokens diff --git a/contracts/rust/tests/combined/wrapped_assets_can_be_frozen.rs b/contracts/rust/tests/combined/wrapped_assets_can_be_frozen.rs index 09a2f7a7..7d06a496 100644 --- a/contracts/rust/tests/combined/wrapped_assets_can_be_frozen.rs +++ b/contracts/rust/tests/combined/wrapped_assets_can_be_frozen.rs @@ -9,6 +9,7 @@ use anyhow::Result; use cap_rust_sandbox::assertion::EnsureMined; use cap_rust_sandbox::cape::CapeBlock; use cap_rust_sandbox::deploy::{deploy_cape, deploy_erc20_token}; +use cap_rust_sandbox::ethereum::GAS_LIMIT_OVERRIDE; use cap_rust_sandbox::ledger::CapeLedger; use cap_rust_sandbox::model::{erc20_asset_description, Erc20Code, EthereumAddr}; use cap_rust_sandbox::test_utils::{ @@ -127,6 +128,7 @@ async fn integration_test_wrapped_assets_can_be_frozen() -> Result<()> { cape_contract .submit_cape_block(empty_block.clone().into()) + .gas(GAS_LIMIT_OVERRIDE) // out of gas with estimate .send() .await? .await? @@ -194,6 +196,7 @@ async fn integration_test_wrapped_assets_can_be_frozen() -> Result<()> { // Submit the block with the freeze note cape_contract .submit_cape_block(block_with_freeze_note.clone().into()) + .gas(GAS_LIMIT_OVERRIDE) // out of gas with estimate .send() .await? .await? diff --git a/contracts/rust/tests/combined/wrapping.rs b/contracts/rust/tests/combined/wrapping.rs index cf61642d..13694ebb 100644 --- a/contracts/rust/tests/combined/wrapping.rs +++ b/contracts/rust/tests/combined/wrapping.rs @@ -9,6 +9,7 @@ use anyhow::Result; use cap_rust_sandbox::assertion::EnsureMined; use cap_rust_sandbox::cape::CapeBlock; use cap_rust_sandbox::deploy::{deploy_erc20_token, deploy_test_cape}; +use cap_rust_sandbox::ethereum::GAS_LIMIT_OVERRIDE; use cap_rust_sandbox::helpers::compare_merkle_root_from_contract_and_jf_tree; use cap_rust_sandbox::ledger::CapeLedger; use cap_rust_sandbox::model::{erc20_asset_description, Erc20Code, EthereumAddr}; @@ -240,7 +241,7 @@ async fn integration_test_wrapping_erc20_tokens() -> Result<()> { // Submit to the contract cape_contract .submit_cape_block(cape_block.clone().into()) - .gas(U256::from(10_000_000)) + .gas(GAS_LIMIT_OVERRIDE) .send() .await? .await?