Skip to content

Commit

Permalink
add non cached contract gas usage as a separate column
Browse files Browse the repository at this point in the history
  • Loading branch information
qalisander committed Sep 25, 2024
1 parent 93f33eb commit 886b26b
Show file tree
Hide file tree
Showing 8 changed files with 269 additions and 74 deletions.
38 changes: 29 additions & 9 deletions benches/src/access_control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ use alloy::{
};
use e2e::{receipt, Account};

use crate::report::Report;
use crate::{
report::{ContractReport, FunctionReport},
CacheOpt,
};

sol!(
#[sol(rpc)]
Expand All @@ -33,7 +36,22 @@ const ROLE: [u8; 32] =
const NEW_ADMIN_ROLE: [u8; 32] =
hex!("879ce0d4bfd332649ca3552efe772a38d64a315eb70ab69689fd309c735946b5");

pub async fn bench() -> eyre::Result<Report> {
pub async fn bench() -> eyre::Result<ContractReport> {
let receipts = run_with(CacheOpt::None).await?;
let report = receipts
.into_iter()
.try_fold(ContractReport::new("AccessControl"), ContractReport::add)?;

let cached_receipts = run_with(CacheOpt::Bid(0)).await?;
let report = cached_receipts
.into_iter()
.try_fold(report, ContractReport::add_cached)?;

Ok(report)
}
pub async fn run_with(
cache_opt: CacheOpt,
) -> eyre::Result<Vec<FunctionReport>> {
let alice = Account::new().await?;
let alice_addr = alice.address();
let alice_wallet = ProviderBuilder::new()
Expand All @@ -50,8 +68,7 @@ pub async fn bench() -> eyre::Result<Report> {
.wallet(EthereumWallet::from(bob.signer.clone()))
.on_http(bob.url().parse()?);

let contract_addr = deploy(&alice).await?;
crate::cache_contract(&alice, contract_addr)?;
let contract_addr = deploy(&alice, cache_opt).await?;

let contract = AccessControl::new(contract_addr, &alice_wallet);
let contract_bob = AccessControl::new(contract_addr, &bob_wallet);
Expand All @@ -68,14 +85,17 @@ pub async fn bench() -> eyre::Result<Report> {
(setRoleAdminCall::SIGNATURE, receipt!(contract.setRoleAdmin(ROLE.into(), NEW_ADMIN_ROLE.into()))?),
];

let report = receipts
receipts
.into_iter()
.try_fold(Report::new("AccessControl"), Report::add)?;
Ok(report)
.map(FunctionReport::new)
.collect::<eyre::Result<Vec<_>>>()
}

async fn deploy(account: &Account) -> eyre::Result<Address> {
async fn deploy(
account: &Account,
cache_opt: CacheOpt,
) -> eyre::Result<Address> {
let args = AccessControl::constructorCall {};
let args = alloy::hex::encode(args.abi_encode());
crate::deploy(account, "access-control", Some(args)).await
crate::deploy(account, "access-control", Some(args), cache_opt).await
}
40 changes: 31 additions & 9 deletions benches/src/erc20.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ use alloy::{
use alloy_primitives::U256;
use e2e::{receipt, Account};

use crate::report::Report;
use crate::{
report::{ContractReport, FunctionReport},
CacheOpt,
};

sol!(
#[sol(rpc)]
Expand Down Expand Up @@ -39,7 +42,23 @@ const TOKEN_NAME: &str = "Test Token";
const TOKEN_SYMBOL: &str = "TTK";
const CAP: U256 = uint!(1_000_000_U256);

pub async fn bench() -> eyre::Result<Report> {
pub async fn bench() -> eyre::Result<ContractReport> {
let reports = run_with(CacheOpt::None).await?;
let report = reports
.into_iter()
.try_fold(ContractReport::new("Erc20"), ContractReport::add)?;

let cached_reports = run_with(CacheOpt::Bid(0)).await?;
let report = cached_reports
.into_iter()
.try_fold(report, ContractReport::add_cached)?;

Ok(report)
}

pub async fn run_with(
cache_opt: CacheOpt,
) -> eyre::Result<Vec<FunctionReport>> {
let alice = Account::new().await?;
let alice_addr = alice.address();
let alice_wallet = ProviderBuilder::new()
Expand All @@ -56,8 +75,7 @@ pub async fn bench() -> eyre::Result<Report> {
.wallet(EthereumWallet::from(bob.signer.clone()))
.on_http(bob.url().parse()?);

let contract_addr = deploy(&alice).await?;
crate::cache_contract(&alice, contract_addr)?;
let contract_addr = deploy(&alice, cache_opt).await?;

let contract = Erc20::new(contract_addr, &alice_wallet);
let contract_bob = Erc20::new(contract_addr, &bob_wallet);
Expand All @@ -81,17 +99,21 @@ pub async fn bench() -> eyre::Result<Report> {
(transferFromCall::SIGNATURE, receipt!(contract_bob.transferFrom(alice_addr, bob_addr, uint!(4_U256)))?),
];

let report =
receipts.into_iter().try_fold(Report::new("Erc20"), Report::add)?;
Ok(report)
receipts
.into_iter()
.map(FunctionReport::new)
.collect::<eyre::Result<Vec<_>>>()
}

async fn deploy(account: &Account) -> eyre::Result<Address> {
async fn deploy(
account: &Account,
cache_opt: CacheOpt,
) -> eyre::Result<Address> {
let args = Erc20Example::constructorCall {
name_: TOKEN_NAME.to_owned(),
symbol_: TOKEN_SYMBOL.to_owned(),
cap_: CAP,
};
let args = alloy::hex::encode(args.abi_encode());
crate::deploy(account, "erc20", Some(args)).await
crate::deploy(account, "erc20", Some(args), cache_opt).await
}
40 changes: 31 additions & 9 deletions benches/src/erc721.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ use alloy::{
};
use e2e::{receipt, Account};

use crate::report::Report;
use crate::{
report::{ContractReport, FunctionReport},
CacheOpt,
};

sol!(
#[sol(rpc)]
Expand All @@ -29,7 +32,23 @@ sol!(

sol!("../examples/erc721/src/constructor.sol");

pub async fn bench() -> eyre::Result<Report> {
pub async fn bench() -> eyre::Result<ContractReport> {
let reports = run_with(CacheOpt::None).await?;
let report = reports
.into_iter()
.try_fold(ContractReport::new("Erc721"), ContractReport::add)?;

let cached_reports = run_with(CacheOpt::Bid(0)).await?;
let report = cached_reports
.into_iter()
.try_fold(report, ContractReport::add_cached)?;

Ok(report)
}

pub async fn run_with(
cache_opt: CacheOpt,
) -> eyre::Result<Vec<FunctionReport>> {
let alice = Account::new().await?;
let alice_addr = alice.address();
let alice_wallet = ProviderBuilder::new()
Expand All @@ -41,8 +60,7 @@ pub async fn bench() -> eyre::Result<Report> {
let bob = Account::new().await?;
let bob_addr = bob.address();

let contract_addr = deploy(&alice).await?;
crate::cache_contract(&alice, contract_addr)?;
let contract_addr = deploy(&alice, cache_opt).await?;

let contract = Erc721::new(contract_addr, &alice_wallet);

Expand Down Expand Up @@ -72,13 +90,17 @@ pub async fn bench() -> eyre::Result<Report> {
(burnCall::SIGNATURE, receipt!(contract.burn(token_1))?),
];

let report =
receipts.into_iter().try_fold(Report::new("Erc721"), Report::add)?;
Ok(report)
receipts
.into_iter()
.map(FunctionReport::new)
.collect::<eyre::Result<Vec<_>>>()
}

async fn deploy(account: &Account) -> eyre::Result<Address> {
async fn deploy(
account: &Account,
cache_opt: CacheOpt,
) -> eyre::Result<Address> {
let args = Erc721Example::constructorCall {};
let args = alloy::hex::encode(args.abi_encode());
crate::deploy(account, "erc721", Some(args)).await
crate::deploy(account, "erc721", Some(args), cache_opt).await
}
29 changes: 20 additions & 9 deletions benches/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,21 @@ struct ArbOtherFields {
l1_block_number: String,
}

/// Cache options for the contract.
/// `Bid(0)` will likely cache the contract on the nitro test node.
pub enum CacheOpt {
None,
Bid(u32),
}

type ArbTxReceipt =
WithOtherFields<TransactionReceipt<AnyReceiptEnvelope<Log>>>;

fn get_l2_gas_used(receipt: &ArbTxReceipt) -> eyre::Result<u128> {
let l2_gas = receipt.gas_used;
let arb_fields: ArbOtherFields = receipt.other.deserialize_as()?;
let l1_gas = arb_fields.gas_used_for_l1.to::<u128>();
Ok(l2_gas - l1_gas)
}

async fn deploy(
account: &Account,
contract_name: &str,
args: Option<String>,
cache_opt: CacheOpt,
) -> eyre::Result<Address> {
let manifest_dir =
std::env::current_dir().context("should get current dir from env")?;
Expand Down Expand Up @@ -78,7 +79,16 @@ async fn deploy(
quiet: true,
};

koba::deploy(&config).await.expect("should deploy contract").address()
let address = koba::deploy(&config)
.await
.expect("should deploy contract")
.address()?;

if let CacheOpt::Bid(bid) = cache_opt {
cache_contract(account, address, bid)?;
}

Ok(address)
}

/// Try to cache a contract on the stylus network.
Expand All @@ -88,6 +98,7 @@ async fn deploy(
fn cache_contract(
account: &Account,
contract_addr: Address,
bid: u32,
) -> eyre::Result<()> {
// We don't need a status code.
// Since it is not zero when the contract is already cached.
Expand All @@ -96,7 +107,7 @@ fn cache_contract(
.args(["-e", &env("RPC_URL")?])
.args(["--private-key", &format!("0x{}", account.pk())])
.arg(contract_addr.to_string())
.arg("0")
.arg(bid.to_string())
.status()
.context("failed to execute `cargo stylus cache bid` command")?;
Ok(())
Expand Down
9 changes: 6 additions & 3 deletions benches/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use benches::{access_control, erc20, erc721, merkle_proofs, report::Reports};
use benches::{
access_control, erc20, erc721, merkle_proofs, report::BenchmarkReport,
};

#[tokio::main]
async fn main() -> eyre::Result<()> {
Expand All @@ -10,8 +12,9 @@ async fn main() -> eyre::Result<()> {
);

let reports = [reports.0?, reports.1?, reports.2?, reports.3?];
let report =
reports.into_iter().fold(Reports::default(), Reports::merge_with);
let report = reports
.into_iter()
.fold(BenchmarkReport::default(), BenchmarkReport::merge_with);

println!();
println!("{report}");
Expand Down
39 changes: 30 additions & 9 deletions benches/src/merkle_proofs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ use alloy::{
};
use e2e::{receipt, Account};

use crate::report::Report;
use crate::{
report::{ContractReport, FunctionReport},
CacheOpt,
};

sol!(
#[sol(rpc)]
Expand Down Expand Up @@ -57,16 +60,31 @@ const PROOF: [[u8; 32]; 16] = bytes_array! {
"fd47b6c292f51911e8dfdc3e4f8bd127773b17f25b7a554beaa8741e99c41208",
};

pub async fn bench() -> eyre::Result<Report> {
pub async fn bench() -> eyre::Result<ContractReport> {
let reports = run_with(CacheOpt::None).await?;
let report = reports
.into_iter()
.try_fold(ContractReport::new("MerkleProofs"), ContractReport::add)?;

let cached_reports = run_with(CacheOpt::Bid(0)).await?;
let report = cached_reports
.into_iter()
.try_fold(report, ContractReport::add_cached)?;

Ok(report)
}

pub async fn run_with(
cache_opt: CacheOpt,
) -> eyre::Result<Vec<FunctionReport>> {
let alice = Account::new().await?;
let alice_wallet = ProviderBuilder::new()
.network::<AnyNetwork>()
.with_recommended_fillers()
.wallet(EthereumWallet::from(alice.signer.clone()))
.on_http(alice.url().parse()?);

let contract_addr = deploy(&alice).await?;
crate::cache_contract(&alice, contract_addr)?;
let contract_addr = deploy(&alice, cache_opt).await?;

let contract = Verifier::new(contract_addr, &alice_wallet);

Expand All @@ -77,12 +95,15 @@ pub async fn bench() -> eyre::Result<Report> {
receipt!(contract.verify(proof, ROOT.into(), LEAF.into()))?,
)];

let report = receipts
receipts
.into_iter()
.try_fold(Report::new("MerkleProofs"), Report::add)?;
Ok(report)
.map(FunctionReport::new)
.collect::<eyre::Result<Vec<_>>>()
}

async fn deploy(account: &Account) -> eyre::Result<Address> {
crate::deploy(account, "merkle-proofs", None).await
async fn deploy(
account: &Account,
cache_opt: CacheOpt,
) -> eyre::Result<Address> {
crate::deploy(account, "merkle-proofs", None, cache_opt).await
}
Loading

0 comments on commit 886b26b

Please sign in to comment.