Skip to content

Commit

Permalink
Fix TxManager, InstrumentedClient, Signer tests (#150)
Browse files Browse the repository at this point in the history
As foundry is no longer required, this PR updates some tests to run
using an Anvil container.

Also, changed Anvil version on tests to `latest`. NOTE: this version
works fine for AMD architectures but is not listed for ARM, so to run
tests locally the image needs to be build manually, due to [this foundry
issue](foundry-rs/foundry#8039)
  • Loading branch information
TomasArrachea authored Oct 23, 2024
1 parent 40a1680 commit f3a9f32
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 68 deletions.
6 changes: 1 addition & 5 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion crates/chainio/clients/eth/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ thiserror.workspace = true
url.workspace = true

[dev-dependencies]
alloy-node-bindings.workspace = true
eigen-signer.workspace = true
eigen-testing-utils.workspace = true
eigen-utils.workspace = true
Expand Down
26 changes: 10 additions & 16 deletions crates/chainio/clients/eth/src/instrumented_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -834,12 +834,12 @@ mod tests {
use alloy::rpc::types::eth::{
pubsub::SubscriptionResult, BlockId, BlockNumberOrTag, BlockTransactionsKind,
};
use alloy_node_bindings::Anvil;
use alloy_primitives::address;
use eigen_signer::signer::Config;
use eigen_testing_utils::anvil::start_anvil_container;
use eigen_utils::get_provider;
use std::{thread, time};
use tokio;
use tokio::time::sleep;
use tokio::{self, time};

#[tokio::test]
async fn test_suggest_gas_tip_cap() {
Expand Down Expand Up @@ -1006,29 +1006,23 @@ mod tests {
/// * `transaction_in_block`
#[tokio::test]
async fn test_transaction_methods() {
let (_container, _http_endpoint, _ws_endpoint) = start_anvil_container().await;

// create a new anvil instance because otherwise it fails with "nonce too low" error.
let anvil = Anvil::new().try_spawn().unwrap();
let rpc_url: String = anvil.endpoint().parse().unwrap();

let (_container, rpc_url, _ws_endpoint) = start_anvil_container().await;
let instrumented_client = InstrumentedClient::new(&rpc_url).await.unwrap();

let addresses = anvil.addresses().to_vec();
let private_key = anvil.keys().first().unwrap();
let to = addresses.first().unwrap();
// build the transaction
let to = address!("a0Ee7A142d267C1f36714E4a8F75612F20a79720");
let mut tx = TxLegacy {
to: Call(*to),
to: Call(to),
value: U256::from(0),
gas_limit: 2_000_000,
nonce: 0,
nonce: 0x69, // nonce queried from the sender account
gas_price: 21_000_000_000,
input: bytes!(),
chain_id: Some(31337),
};

let private_key_hex = hex::encode(private_key.to_bytes());
let private_key_hex =
"ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80".to_string();
let config = Config::PrivateKey(private_key_hex);
let signer = Config::signer_from_config(config).unwrap();
let signature = signer.sign_transaction_sync(&mut tx).unwrap();
Expand All @@ -1045,7 +1039,7 @@ mod tests {
assert!(tx_by_hash.is_ok());

// this sleep is needed because we have to wait since the transaction is not ready yet
thread::sleep(time::Duration::from_secs(1));
sleep(time::Duration::from_secs(1)).await;

// test transaction_receipt
let receipt = instrumented_client.transaction_receipt(tx_hash).await;
Expand Down
4 changes: 1 addition & 3 deletions crates/chainio/txmanager/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ reqwest.workspace = true
thiserror.workspace = true

[dev-dependencies]
alloy-primitives.workspace = true
alloy-consensus.workspace = true
alloy-node-bindings.workspace = true
eigen-testing-utils.workspace = true
once_cell.workspace = true
tokio.workspace = true
53 changes: 20 additions & 33 deletions crates/chainio/txmanager/src/simple_tx_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,41 +293,34 @@ mod tests {
use super::SimpleTxManager;
use alloy::consensus::TxLegacy;
use alloy::network::TransactionBuilder;
use alloy::primitives::{address, bytes, TxKind::Call, U256};
use alloy::rpc::types::eth::TransactionRequest;
use alloy_node_bindings::Anvil;
use alloy_primitives::{bytes, TxKind::Call, U256};
use eigen_logging::get_test_logger;
use tokio;
use eigen_testing_utils::anvil::start_anvil_container;

#[tokio::test]
async fn test_send_transaction_from_legacy() {
let anvil = Anvil::new().try_spawn().unwrap();
let rpc_url: String = anvil.endpoint().parse().unwrap();
let (_container, rpc_url, _ws_endpoint) = start_anvil_container().await;
let logger = get_test_logger();

let private_key = anvil.keys().first().unwrap();
let simple_tx_manager = SimpleTxManager::new(
logger,
1.0,
private_key.as_scalar_primitive().to_string().as_str(),
rpc_url.as_str(),
)
.unwrap();

let addresses = anvil.addresses().to_vec();
let to = addresses.first().cloned().unwrap();
let private_key =
"ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80".to_string();
let simple_tx_manager =
SimpleTxManager::new(logger, 1.0, private_key.as_str(), rpc_url.as_str()).unwrap();
let to = address!("a0Ee7A142d267C1f36714E4a8F75612F20a79720");

let account_nonce = 0x69; // nonce queried from the sender account
let tx = TxLegacy {
to: Call(to),
value: U256::from(1_000_000_000),
gas_limit: 2_000_000,
nonce: 0,
nonce: account_nonce,
gas_price: 21_000_000_000,
input: bytes!(),
chain_id: Some(31337),
};

let mut tx_request: TransactionRequest = tx.clone().into();
let mut tx_request: TransactionRequest = tx.into();
// send transaction and wait for receipt
let receipt = simple_tx_manager.send_tx(&mut tx_request).await.unwrap();
let block_number = receipt.block_number.unwrap();
Expand All @@ -338,32 +331,26 @@ mod tests {

#[tokio::test]
async fn test_send_transaction_from_eip1559() {
let anvil = Anvil::new().try_spawn().unwrap();
let rpc_url: String = anvil.endpoint().parse().unwrap();
let (_container, rpc_url, _ws_endpoint) = start_anvil_container().await;
let logger = get_test_logger();

let private_key = anvil.keys().first().unwrap();
let simple_tx_manager = SimpleTxManager::new(
logger,
1.0,
private_key.as_scalar_primitive().to_string().as_str(),
rpc_url.as_str(),
)
.unwrap();

let addresses = anvil.addresses().to_vec();
let to = addresses.first().cloned().unwrap();
let private_key =
"ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80".to_string();
let simple_tx_manager =
SimpleTxManager::new(logger, 1.0, private_key.as_str(), rpc_url.as_str()).unwrap();
let to = address!("a0Ee7A142d267C1f36714E4a8F75612F20a79720");

let account_nonce = 0x69; // nonce queried from the sender account
let mut tx = TransactionRequest::default()
.with_to(to)
.with_nonce(0)
.with_nonce(account_nonce)
.with_chain_id(31337)
.with_value(U256::from(100))
.with_gas_limit(21_000)
.with_max_priority_fee_per_gas(1_000_000_000)
.with_max_fee_per_gas(20_000_000_000);

tx.set_gas_price(21_000_000_000);

// send transaction and wait for receipt
let receipt = simple_tx_manager.send_tx(&mut tx).await.unwrap();
let block_number = receipt.block_number.unwrap();
Expand Down
1 change: 0 additions & 1 deletion crates/signer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ thiserror.workspace = true
url.workspace = true

[dev-dependencies]
alloy-node-bindings.workspace = true
aws-config.workspace = true
eigen-testing-utils.workspace = true
testcontainers.workspace = true
Expand Down
15 changes: 7 additions & 8 deletions crates/signer/src/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,14 @@ mod test {
use alloy::network::{TxSigner, TxSignerSync};
use alloy::signers::local::PrivateKeySigner;
use alloy::signers::Signature;
use alloy_node_bindings::Anvil;
use alloy_primitives::{address, bytes, hex_literal::hex, keccak256, Address, U256};
use aws_config::{BehaviorVersion, Region, SdkConfig};
use aws_sdk_kms::{
self,
config::{Credentials, SharedCredentialsProvider},
types::KeyMetadata,
};
use eigen_testing_utils::anvil::start_anvil_container;
use eigen_testing_utils::test_data::TestData;
use std::str::FromStr;
use testcontainers::{
Expand Down Expand Up @@ -202,25 +202,24 @@ mod test {

#[tokio::test]
async fn test_sign_legacy_transaction_with_web3_signer() {
let anvil = Anvil::default().spawn();
let (_container, endpoint, _ws_endpoint) = start_anvil_container().await;

let endpoint = anvil.endpoint();
let address = anvil.addresses()[0];
let address = address!("f39Fd6e51aad88F6F4ce6aB8827279cffFb92266");
let signer = Config::web3_signer(endpoint, address).unwrap();
let mut tx = TxLegacy {
to: anvil.addresses()[1].into(),
to: address!("a0Ee7A142d267C1f36714E4a8F75612F20a79720").into(),
value: U256::from(1_000_000_000),
gas_limit: 0x76c0,
gas_price: 21_000_000_000,
nonce: 0,
input: bytes!(),
chain_id: Some(anvil.chain_id()),
chain_id: Some(31337),
};

let signature = signer.sign_transaction(&mut tx).await.unwrap();

let private_key = anvil.keys()[0].clone();
let expected_signer = PrivateKeySigner::from_field_bytes(&private_key.to_bytes()).unwrap();
let private_key_hex = "ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80";
let expected_signer = PrivateKeySigner::from_str(private_key_hex).unwrap();
let expected_signature = expected_signer.sign_transaction_sync(&mut tx).unwrap();

assert_eq!(signature, expected_signature);
Expand Down
2 changes: 1 addition & 1 deletion testing/testing-utils/src/anvil.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use testcontainers::{
};

const ANVIL_IMAGE: &str = "ghcr.io/foundry-rs/foundry";
const ANVIL_TAG: &str = "nightly-5b7e4cb3c882b28f3c32ba580de27ce7381f415a";
const ANVIL_TAG: &str = "latest";
const ANVIL_STATE_PATH: &str = "./crates/contracts/anvil/contracts_deployed_anvil_state.json"; // relative path from the project root

fn workspace_dir() -> PathBuf {
Expand Down

0 comments on commit f3a9f32

Please sign in to comment.