Skip to content

Commit

Permalink
run cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
dutterbutter committed Dec 19, 2024
1 parent 9c0e73d commit 71b15bc
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 50 deletions.
9 changes: 6 additions & 3 deletions e2e-tests-rust/src/ext.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Module containing extensions of existing alloy abstractions.
use alloy::network::{ReceiptResponse,TxSigner};
use alloy::primitives::{Address,BlockHash,PrimitiveSignature};
use alloy::network::{ReceiptResponse, TxSigner};
use alloy::primitives::{Address, BlockHash, PrimitiveSignature};
use alloy::providers::WalletProvider;
use alloy::signers::local::PrivateKeySigner;
use alloy_zksync::network::Zksync;
Expand Down Expand Up @@ -80,7 +80,10 @@ pub trait ZksyncWalletProviderExt: WalletProvider<Zksync, Wallet = ZksyncWallet>
}

/// Registers provider signer.
fn register_signer<T: TxSigner<PrimitiveSignature> + Send + Sync + 'static>(&mut self, signer: T) {
fn register_signer<T: TxSigner<PrimitiveSignature> + Send + Sync + 'static>(
&mut self,
signer: T,
) {
self.wallet_mut().register_signer(signer);
}
}
Expand Down
5 changes: 4 additions & 1 deletion e2e-tests-rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@ mod provider;
mod utils;

pub use ext::{ReceiptExt, ZksyncWalletProviderExt};
pub use provider::{init_testing_provider, init_testing_provider_with_client, AnvilZKsyncApi, TestingProvider, DEFAULT_TX_VALUE};
pub use provider::{
init_testing_provider, init_testing_provider_with_client, AnvilZKsyncApi, TestingProvider,
DEFAULT_TX_VALUE,
};
pub use utils::get_node_binary_path;
2 changes: 1 addition & 1 deletion e2e-tests-rust/src/provider/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use alloy_zksync::network::receipt_response::ReceiptResponse;
use alloy_zksync::network::transaction_response::TransactionResponse;
use alloy_zksync::network::Zksync;
use alloy_zksync::node_bindings::{AnvilZKsync, AnvilZKsyncError::NoKeysAvailable};
use alloy_zksync::provider::{zksync_provider, layers::anvil_zksync::AnvilZKsyncLayer};
use alloy_zksync::provider::{layers::anvil_zksync::AnvilZKsyncLayer, zksync_provider};
use alloy_zksync::wallet::ZksyncWallet;
use anyhow::Context as _;
use async_trait::async_trait;
Expand Down
122 changes: 77 additions & 45 deletions e2e-tests-rust/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,23 @@ use alloy::network::ReceiptResponse;
use alloy::providers::ext::AnvilApi;
use alloy::providers::Provider;
use alloy::{
network::primitives::BlockTransactionsKind,
primitives::U256,
signers::local::PrivateKeySigner,
network::primitives::BlockTransactionsKind, primitives::U256, signers::local::PrivateKeySigner,
};
use anyhow::Context;
use anvil_zksync_core::node::VersionedState;
use anvil_zksync_core::utils::write_json_file;
use anvil_zksync_e2e_tests::{
init_testing_provider, init_testing_provider_with_client, AnvilZKsyncApi, ReceiptExt,
ZksyncWalletProviderExt, DEFAULT_TX_VALUE, get_node_binary_path
get_node_binary_path, init_testing_provider, init_testing_provider_with_client, AnvilZKsyncApi,
ReceiptExt, ZksyncWalletProviderExt, DEFAULT_TX_VALUE,
};
use anvil_zksync_core::utils::write_json_file;
use anyhow::Context;
use flate2::read::GzDecoder;
use http::header::{
HeaderMap, HeaderValue, ACCESS_CONTROL_ALLOW_HEADERS, ACCESS_CONTROL_ALLOW_METHODS,
ACCESS_CONTROL_ALLOW_ORIGIN, ORIGIN,
};
use anvil_zksync_core::node::VersionedState;
use std::{ fs, convert::identity, thread::sleep, time::Duration };
use tempdir::TempDir;
use flate2::read::GzDecoder;
use std::io::Read;
use std::{convert::identity, fs, thread::sleep, time::Duration};
use tempdir::TempDir;

const SOME_ORIGIN: HeaderValue = HeaderValue::from_static("http://some.origin");
const OTHER_ORIGIN: HeaderValue = HeaderValue::from_static("http://other.origin");
Expand Down Expand Up @@ -470,13 +468,31 @@ async fn cli_allow_origin() -> anyhow::Result<()> {
async fn pool_txs_order_fifo() -> anyhow::Result<()> {
let provider_fifo = init_testing_provider(|node| node.no_mine()).await?;

let pending_tx0 = provider_fifo.tx().with_rich_from(0).with_max_fee_per_gas(50_000_000).register().await?;
let pending_tx1 = provider_fifo.tx().with_rich_from(1).with_max_fee_per_gas(100_000_000).register().await?;
let pending_tx2 = provider_fifo.tx().with_rich_from(2).with_max_fee_per_gas(150_000_000).register().await?;
let pending_tx0 = provider_fifo
.tx()
.with_rich_from(0)
.with_max_fee_per_gas(50_000_000)
.register()
.await?;
let pending_tx1 = provider_fifo
.tx()
.with_rich_from(1)
.with_max_fee_per_gas(100_000_000)
.register()
.await?;
let pending_tx2 = provider_fifo
.tx()
.with_rich_from(2)
.with_max_fee_per_gas(150_000_000)
.register()
.await?;

provider_fifo.anvil_mine(Some(U256::from(1)), None).await?;

let block = provider_fifo.get_block(1.into(), BlockTransactionsKind::Hashes).await?.unwrap();
let block = provider_fifo
.get_block(1.into(), BlockTransactionsKind::Hashes)
.await?
.unwrap();
let tx_hashes = block.transactions.as_hashes().unwrap();
assert_eq!(&tx_hashes[0], pending_tx0.tx_hash());
assert_eq!(&tx_hashes[1], pending_tx1.tx_hash());
Expand All @@ -488,13 +504,31 @@ async fn pool_txs_order_fifo() -> anyhow::Result<()> {
async fn pool_txs_order_fees() -> anyhow::Result<()> {
let provider_fees = init_testing_provider(|node| node.no_mine().arg("--order=fees")).await?;

let pending_tx0 = provider_fees.tx().with_rich_from(0).with_max_fee_per_gas(50_000_000).register().await?;
let pending_tx1 = provider_fees.tx().with_rich_from(1).with_max_fee_per_gas(100_000_000).register().await?;
let pending_tx2 = provider_fees.tx().with_rich_from(2).with_max_fee_per_gas(150_000_000).register().await?;
let pending_tx0 = provider_fees
.tx()
.with_rich_from(0)
.with_max_fee_per_gas(50_000_000)
.register()
.await?;
let pending_tx1 = provider_fees
.tx()
.with_rich_from(1)
.with_max_fee_per_gas(100_000_000)
.register()
.await?;
let pending_tx2 = provider_fees
.tx()
.with_rich_from(2)
.with_max_fee_per_gas(150_000_000)
.register()
.await?;

provider_fees.anvil_mine(Some(U256::from(1)), None).await?;

let block = provider_fees.get_block(1.into(), BlockTransactionsKind::Hashes).await?.unwrap();
let block = provider_fees
.get_block(1.into(), BlockTransactionsKind::Hashes)
.await?
.unwrap();
let tx_hashes = block.transactions.as_hashes().unwrap();
assert_eq!(&tx_hashes[0], pending_tx2.tx_hash());
assert_eq!(&tx_hashes[1], pending_tx1.tx_hash());
Expand All @@ -519,14 +553,13 @@ async fn transactions_have_index() -> anyhow::Result<()> {
}

#[tokio::test]
async fn dump_state_on_run() -> anyhow::Result<()> {
async fn dump_state_on_run() -> anyhow::Result<()> {
let temp_dir = TempDir::new("state-test").expect("failed creating temporary dir");
let dump_path = temp_dir.path().join("state_dump.json");

let dump_path_clone = dump_path.clone();
let provider = init_testing_provider(move |node| {
node
.path(get_node_binary_path())
let provider = init_testing_provider(move |node| {
node.path(get_node_binary_path())
.arg("--state-interval")
.arg("1")
.arg("--dump-state")
Expand All @@ -538,18 +571,18 @@ async fn dump_state_on_run() -> anyhow::Result<()> {
let tx_hash = receipt.transaction_hash().to_string();

sleep(Duration::from_secs(2));

drop(provider);

assert!(
dump_path.exists(),
"State dump file should exist at {:?}",
dump_path
);

let dumped_data = fs::read_to_string(&dump_path)?;
let state: VersionedState = serde_json::from_str(&dumped_data)
.context("Failed to deserialize state")?;
let state: VersionedState =
serde_json::from_str(&dumped_data).context("Failed to deserialize state")?;

match state {
VersionedState::V1 { version: _, state } => {
Expand All @@ -562,16 +595,17 @@ async fn dump_state_on_run() -> anyhow::Result<()> {
"state_dump.json should contain at least one transaction"
);
let tx_exists = state.transactions.iter().any(|tx| {
let tx_hash_full = format!("0x{}", hex::encode(tx.receipt.transaction_hash.as_bytes()));
let tx_hash_full =
format!("0x{}", hex::encode(tx.receipt.transaction_hash.as_bytes()));
tx_hash_full == tx_hash
});

assert!(
tx_exists,
"The state dump should contain the transaction with hash: {:?}",
tx_hash
);
},
}
VersionedState::Unknown { version } => {
panic!("Encountered unknown state version: {}", version);
}
Expand All @@ -581,14 +615,13 @@ async fn dump_state_on_run() -> anyhow::Result<()> {
}

#[tokio::test]
async fn dump_state_on_fork() -> anyhow::Result<()> {
async fn dump_state_on_fork() -> anyhow::Result<()> {
let temp_dir = TempDir::new("state-fork-test").expect("failed creating temporary dir");
let dump_path = temp_dir.path().join("state_dump_fork.json");

let dump_path_clone = dump_path.clone();
let provider = init_testing_provider(move |node| {
node
.path(get_node_binary_path())
node.path(get_node_binary_path())
.arg("--state-interval")
.arg("1")
.arg("--dump-state")
Expand All @@ -601,18 +634,18 @@ async fn dump_state_on_fork() -> anyhow::Result<()> {
let tx_hash = receipt.transaction_hash().to_string();

sleep(Duration::from_secs(2));

drop(provider);

assert!(
dump_path.exists(),
"State dump file should exist at {:?}",
dump_path
);

let dumped_data = fs::read_to_string(&dump_path)?;
let state: VersionedState = serde_json::from_str(&dumped_data)
.context("Failed to deserialize state")?;
let state: VersionedState =
serde_json::from_str(&dumped_data).context("Failed to deserialize state")?;

match state {
VersionedState::V1 { version: _, state } => {
Expand All @@ -625,15 +658,16 @@ async fn dump_state_on_fork() -> anyhow::Result<()> {
"state_dump_fork.json should contain at least one transaction"
);
let tx_exists = state.transactions.iter().any(|tx| {
let tx_hash_full = format!("0x{}", hex::encode(tx.receipt.transaction_hash.as_bytes()));
let tx_hash_full =
format!("0x{}", hex::encode(tx.receipt.transaction_hash.as_bytes()));
tx_hash_full == tx_hash
});
assert!(
tx_exists,
"The state dump should contain the transaction with hash: {:?}",
tx_hash
);
},
}
VersionedState::Unknown { version } => {
panic!("Encountered unknown state version: {}", version);
}
Expand Down Expand Up @@ -663,8 +697,7 @@ async fn load_state_on_run() -> anyhow::Result<()> {

let dump_path_clone = dump_path.clone();
let new_provider = init_testing_provider(move |node| {
node
.path(get_node_binary_path())
node.path(get_node_binary_path())
.arg("--state-interval")
.arg("1")
.arg("--load-state")
Expand Down Expand Up @@ -704,7 +737,7 @@ async fn load_state_on_fork() -> anyhow::Result<()> {
let blocks = provider.get_blocks_by_receipts(&receipts).await?;
let state_bytes = provider.anvil_dump_state().await?;
drop(provider);

let mut decoder = GzDecoder::new(&state_bytes.0[..]);
let mut json_str = String::new();
decoder.read_to_string(&mut json_str).unwrap();
Expand All @@ -713,8 +746,7 @@ async fn load_state_on_fork() -> anyhow::Result<()> {

let dump_path_clone = dump_path.clone();
let new_provider = init_testing_provider(move |node| {
node
.path(get_node_binary_path())
node.path(get_node_binary_path())
.arg("--state-interval")
.arg("1")
.arg("--load-state")
Expand All @@ -741,4 +773,4 @@ async fn load_state_on_fork() -> anyhow::Result<()> {
);

Ok(())
}
}

0 comments on commit 71b15bc

Please sign in to comment.