Skip to content

Commit

Permalink
create utilities for common test patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
itegulov committed Dec 5, 2024
1 parent 9867c8f commit a258c7f
Show file tree
Hide file tree
Showing 8 changed files with 574 additions and 288 deletions.
2 changes: 2 additions & 0 deletions e2e-tests-rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion e2e-tests-rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ publish = false

[dependencies]
alloy-zksync = { git = "https://github.com/itegulov/alloy-zksync.git", rev = "c43bba1a6c5e744afb975b261cba6e964d6a58c6" }
alloy = { version = "0.6", features = ["full", "rlp", "serde", "sol-types", "provider-anvil-api"] }
alloy = { version = "0.6", features = ["full", "rlp", "serde", "sol-types", "getrandom", "provider-anvil-api"] }
anyhow = "1.0"
fs2 = "0.4.3"
tokio = { version = "1", features = ["time", "rt", "process"] }
futures = "0.3.31"
itertools = "0.13.0"
async-trait = "0.1.83"

[dev-dependencies]

Expand Down
73 changes: 73 additions & 0 deletions e2e-tests-rust/src/ext.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
//! Module containing extensions of existing alloy abstractions.
use alloy::network::ReceiptResponse;
use alloy::primitives::{Address, BlockHash};
use alloy::providers::WalletProvider;
use alloy::signers::local::PrivateKeySigner;
use alloy_zksync::network::Zksync;
use alloy_zksync::wallet::ZksyncWallet;

pub trait ReceiptExt: ReceiptResponse {
fn block_number_ext(&self) -> anyhow::Result<u64> {
self.block_number().ok_or_else(|| {
anyhow::anyhow!(
"receipt (hash={}) does not have block number",
self.transaction_hash()
)
})
}

fn block_hash_ext(&self) -> anyhow::Result<BlockHash> {
self.block_hash().ok_or_else(|| {
anyhow::anyhow!(
"receipt (hash={}) does not have block hash",
self.transaction_hash()
)
})
}

/// Asserts that receipts belong to a block and that block is the same for both of them.
fn assert_same_block(&self, other: &Self) -> anyhow::Result<()> {
let lhs_number = self.block_number_ext()?;
let rhs_number = other.block_number_ext()?;
let lhs_hash = self.block_hash_ext()?;
let rhs_hash = other.block_hash_ext()?;

if lhs_number == rhs_number && lhs_hash == rhs_hash {
Ok(())
} else {
anyhow::bail!(
"receipt (hash={}, block={}) is not from the same block as receipt (hash={}, block={})",
self.transaction_hash(),
lhs_number,
other.transaction_hash(),
rhs_number
)
}
}
/// Asserts that receipt is successful.
fn assert_successful(&self) -> anyhow::Result<()> {
if !self.status() {
anyhow::bail!(
"receipt (hash={}, block={:?}) is not successful",
self.transaction_hash(),
self.block_number(),
);
}
Ok(())
}
}

impl<T: ReceiptResponse> ReceiptExt for T {}

pub trait ZksyncWalletProviderExt: WalletProvider<Zksync, Wallet = ZksyncWallet> {
/// Creates and registers a random signer. Returns new signer's address.
fn register_random_signer(&mut self) -> Address {
let signer = PrivateKeySigner::random();
let address = signer.address();
self.wallet_mut().register_signer(signer);
address
}
}

impl<T: WalletProvider<Zksync, Wallet = ZksyncWallet>> ZksyncWalletProviderExt for T {}
39 changes: 6 additions & 33 deletions e2e-tests-rust/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,8 @@
use alloy::network::Network;
use alloy::providers::{Provider, ProviderCall};
use alloy::rpc::client::NoParams;
use alloy::serde::WithOtherFields;
use alloy::transports::Transport;
use alloy_zksync::network::Zksync;
#![allow(async_fn_in_trait)]

pub mod utils;
mod ext;
mod provider;
mod utils;

pub trait AnvilZKsyncApiProvider<T>: Provider<T, Zksync>
where
T: Transport + Clone,
{
/// Custom version of [`alloy::providers::ext::AnvilApi::anvil_mine_detailed`] that returns
/// block representation with transactions that contain extra custom fields.
fn mine_detailed(
&self,
) -> ProviderCall<
T,
NoParams,
alloy::rpc::types::Block<
WithOtherFields<<Zksync as Network>::TransactionResponse>,
<Zksync as Network>::HeaderResponse,
>,
> {
self.client().request_noparams("anvil_mine_detailed").into()
}
}

impl<P, T> AnvilZKsyncApiProvider<T> for P
where
T: Transport + Clone,
P: Provider<T, Zksync>,
{
}
pub use ext::{ReceiptExt, ZksyncWalletProviderExt};
pub use provider::{init_testing_provider, AnvilZKsyncApi, TestingProvider};
34 changes: 34 additions & 0 deletions e2e-tests-rust/src/provider/anvil_zksync.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use alloy::network::Network;
use alloy::providers::{Provider, ProviderCall};
use alloy::rpc::client::NoParams;
use alloy::serde::WithOtherFields;
use alloy::transports::Transport;
use alloy_zksync::network::Zksync;

/// RPC interface that gives access to methods specific to anvil-zksync.
pub trait AnvilZKsyncApi<T>: Provider<T, Zksync>
where
T: Transport + Clone,
{
/// Custom version of [`alloy::providers::ext::AnvilApi::anvil_mine_detailed`] that returns
/// block representation with transactions that contain extra custom fields.
fn anvil_zksync_mine_detailed(
&self,
) -> ProviderCall<
T,
NoParams,
alloy::rpc::types::Block<
WithOtherFields<<Zksync as Network>::TransactionResponse>,
<Zksync as Network>::HeaderResponse,
>,
> {
self.client().request_noparams("anvil_mine_detailed").into()
}
}

impl<P, T> AnvilZKsyncApi<T> for P
where
T: Transport + Clone,
P: Provider<T, Zksync>,
{
}
5 changes: 5 additions & 0 deletions e2e-tests-rust/src/provider/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mod anvil_zksync;
mod testing;

pub use anvil_zksync::AnvilZKsyncApi;
pub use testing::{init_testing_provider, TestingProvider};
Loading

0 comments on commit a258c7f

Please sign in to comment.