Skip to content
This repository has been archived by the owner on Jan 15, 2024. It is now read-only.

Commit

Permalink
Use EIP1559 transactions (#1228)
Browse files Browse the repository at this point in the history
* WIP: use EIP1559 transactions

* Fix tests in cap-rust-sandbox

- Specify manual gas limit where estimated value leads to transaction
  running out of gas.
- Add some `.ensure_mined()` calls.
- Replace some unwraps with `?`.

* Fix cap-rust-sandbox integration tests

- Specify manual gas limit where transactions run out of gas.

* WIP: define constant for gas limit

* Extract constant for gas limit override
  • Loading branch information
sveitser authored Oct 28, 2022
1 parent 87bceac commit 3ce0a78
Show file tree
Hide file tree
Showing 11 changed files with 83 additions and 73 deletions.
6 changes: 3 additions & 3 deletions contracts/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions contracts/rust/src/cape/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
};
Expand Down Expand Up @@ -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?;
Expand Down
20 changes: 12 additions & 8 deletions contracts/rust/src/cape/submit_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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},
};
Expand Down Expand Up @@ -227,14 +224,17 @@ mod tests {
.add_root(root.generic_into::<MerkleRootSol>().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!(
Expand Down Expand Up @@ -353,6 +353,7 @@ mod tests {

contract
.submit_cape_block(cape_block.into())
.gas(GAS_LIMIT_OVERRIDE)
.send()
.await?
.await?
Expand All @@ -377,13 +378,16 @@ mod tests {
.add_root(root.generic_into::<MerkleRootSol>().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()
Expand Down
56 changes: 26 additions & 30 deletions contracts/rust/src/cape_e2e_test_mint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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 */
Expand All @@ -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());
Expand All @@ -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();
Expand All @@ -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());
Expand Down Expand Up @@ -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
Expand Down
42 changes: 20 additions & 22 deletions contracts/rust/src/cape_e2e_test_transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*,
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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!(
Expand Down Expand Up @@ -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
Expand All @@ -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;

Expand Down
6 changes: 5 additions & 1 deletion contracts/rust/src/ethereum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -212,7 +216,7 @@ pub async fn deploy<M: 'static + Middleware, T: Send + Tokenize>(
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)
}

Expand Down
2 changes: 0 additions & 2 deletions contracts/rust/src/records_merkle_tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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?
Expand Down
Loading

0 comments on commit 3ce0a78

Please sign in to comment.