Replace ganache-cli with anvil (#3555)

## Issue Addressed

N/A

## Proposed Changes

Replace ganache-cli with anvil https://github.com/foundry-rs/foundry/blob/master/anvil/README.md
We can lose all js dependencies in CI as a consequence.

## Additional info
Also changes the ethers-rs version used in the execution layer (for the transaction reconstruction) to a newer one. This was necessary to get use the ethers utils for anvil. The fixed execution engine integration tests should catch any potential issues with the payload reconstruction after #3592 


Co-authored-by: Michael Sproul <michael@sigmaprime.io>
This commit is contained in:
Pawan Dhananjay
2023-05-15 07:22:02 +00:00
parent 3c029d48bf
commit 8a3eb4df9c
24 changed files with 465 additions and 506 deletions

View File

@@ -6,8 +6,11 @@ edition = "2021"
[dependencies]
tokio = { version = "1.14.0", features = ["time"] }
web3 = { version = "0.18.0", default-features = false, features = ["http-tls", "signing", "ws-tls-tokio"] }
ethers-core = "1.0.2"
ethers-providers = "1.0.2"
ethers-contract = "1.0.2"
types = { path = "../../consensus/types"}
serde_json = "1.0.58"
deposit_contract = { path = "../../common/deposit_contract"}
unused_port = { path = "../../common/unused_port" }
hex = "0.4.2"

View File

@@ -0,0 +1,101 @@
use ethers_core::utils::{Anvil, AnvilInstance};
use ethers_providers::{Http, Middleware, Provider};
use serde_json::json;
use std::convert::TryFrom;
use unused_port::unused_tcp4_port;
/// Provides a dedicated `anvil` instance.
///
/// Requires that `anvil` is installed and available on `PATH`.
pub struct AnvilCliInstance {
pub port: u16,
pub anvil: AnvilInstance,
pub client: Provider<Http>,
chain_id: u64,
}
impl AnvilCliInstance {
fn new_from_child(anvil_instance: Anvil, chain_id: u64, port: u16) -> Result<Self, String> {
let client = Provider::<Http>::try_from(&endpoint(port))
.map_err(|e| format!("Failed to start HTTP transport connected to anvil: {:?}", e))?;
Ok(Self {
port,
anvil: anvil_instance.spawn(),
client,
chain_id,
})
}
pub fn new(chain_id: u64) -> Result<Self, String> {
let port = unused_tcp4_port()?;
let anvil = Anvil::new()
.port(port)
.mnemonic("vast thought differ pull jewel broom cook wrist tribe word before omit")
.arg("--balance")
.arg("1000000000")
.arg("--gas-limit")
.arg("1000000000")
.arg("--accounts")
.arg("10")
.arg("--chain-id")
.arg(format!("{}", chain_id));
Self::new_from_child(anvil, chain_id, port)
}
pub fn fork(&self) -> Result<Self, String> {
let port = unused_tcp4_port()?;
let anvil = Anvil::new()
.port(port)
.arg("--chain-id")
.arg(format!("{}", self.chain_id()))
.fork(self.endpoint());
Self::new_from_child(anvil, self.chain_id, port)
}
/// Returns the endpoint that this instance is listening on.
pub fn endpoint(&self) -> String {
endpoint(self.port)
}
/// Returns the chain id of the anvil instance
pub fn chain_id(&self) -> u64 {
self.chain_id
}
/// Increase the timestamp on future blocks by `increase_by` seconds.
pub async fn increase_time(&self, increase_by: u64) -> Result<(), String> {
self.client
.request("evm_increaseTime", vec![json!(increase_by)])
.await
.map(|_json_value: u64| ())
.map_err(|e| format!("Failed to increase time on EVM (is this anvil?): {:?}", e))
}
/// Returns the current block number, as u64
pub async fn block_number(&self) -> Result<u64, String> {
self.client
.get_block_number()
.await
.map(|v| v.as_u64())
.map_err(|e| format!("Failed to get block number: {:?}", e))
}
/// Mines a single block.
pub async fn evm_mine(&self) -> Result<(), String> {
self.client
.request("evm_mine", ())
.await
.map(|_: String| ())
.map_err(|_| {
"utils should mine new block with evm_mine (only works with anvil/ganache!)"
.to_string()
})
}
}
fn endpoint(port: u16) -> String {
format!("http://127.0.0.1:{}", port)
}

View File

@@ -1,193 +0,0 @@
use serde_json::json;
use std::io::prelude::*;
use std::io::BufReader;
use std::process::{Child, Command, Stdio};
use std::time::{Duration, Instant};
use unused_port::unused_tcp4_port;
use web3::{transports::Http, Transport, Web3};
/// How long we will wait for ganache to indicate that it is ready.
const GANACHE_STARTUP_TIMEOUT_MILLIS: u64 = 10_000;
/// Provides a dedicated `ganachi-cli` instance with a connected `Web3` instance.
///
/// Requires that `ganachi-cli` is installed and available on `PATH`.
pub struct GanacheInstance {
pub port: u16,
child: Child,
pub web3: Web3<Http>,
chain_id: u64,
}
impl GanacheInstance {
fn new_from_child(mut child: Child, port: u16, chain_id: u64) -> Result<Self, String> {
let stdout = child
.stdout
.ok_or("Unable to get stdout for ganache child process")?;
let start = Instant::now();
let mut reader = BufReader::new(stdout);
loop {
if start + Duration::from_millis(GANACHE_STARTUP_TIMEOUT_MILLIS) <= Instant::now() {
break Err(
"Timed out waiting for ganache to start. Is ganache installed?".to_string(),
);
}
let mut line = String::new();
if let Err(e) = reader.read_line(&mut line) {
break Err(format!("Failed to read line from ganache process: {:?}", e));
} else if line.starts_with("RPC Listening on") {
break Ok(());
} else {
continue;
}
}?;
let transport = Http::new(&endpoint(port)).map_err(|e| {
format!(
"Failed to start HTTP transport connected to ganache: {:?}",
e
)
})?;
let web3 = Web3::new(transport);
child.stdout = Some(reader.into_inner());
Ok(Self {
port,
child,
web3,
chain_id,
})
}
/// Start a new `ganache` process, waiting until it indicates that it is ready to accept
/// RPC connections.
pub fn new(chain_id: u64) -> Result<Self, String> {
let port = unused_tcp4_port()?;
let binary = match cfg!(windows) {
true => "ganache.cmd",
false => "ganache",
};
let child = Command::new(binary)
.stdout(Stdio::piped())
.arg("--defaultBalanceEther")
.arg("1000000000")
.arg("--gasLimit")
.arg("1000000000")
.arg("--accounts")
.arg("10")
.arg("--port")
.arg(format!("{}", port))
.arg("--mnemonic")
.arg("\"vast thought differ pull jewel broom cook wrist tribe word before omit\"")
.arg("--chain.chainId")
.arg(format!("{}", chain_id))
.spawn()
.map_err(|e| {
format!(
"Failed to start {}. \
Is it installed and available on $PATH? Error: {:?}",
binary, e
)
})?;
Self::new_from_child(child, port, chain_id)
}
pub fn fork(&self) -> Result<Self, String> {
let port = unused_tcp4_port()?;
let binary = match cfg!(windows) {
true => "ganache.cmd",
false => "ganache",
};
let child = Command::new(binary)
.stdout(Stdio::piped())
.arg("--fork")
.arg(self.endpoint())
.arg("--port")
.arg(format!("{}", port))
.arg("--chain.chainId")
.arg(format!("{}", self.chain_id))
.spawn()
.map_err(|e| {
format!(
"Failed to start {}. \
Is it installed and available on $PATH? Error: {:?}",
binary, e
)
})?;
Self::new_from_child(child, port, self.chain_id)
}
/// Returns the endpoint that this instance is listening on.
pub fn endpoint(&self) -> String {
endpoint(self.port)
}
/// Returns the chain id of the ganache instance
pub fn chain_id(&self) -> u64 {
self.chain_id
}
/// Increase the timestamp on future blocks by `increase_by` seconds.
pub async fn increase_time(&self, increase_by: u64) -> Result<(), String> {
self.web3
.transport()
.execute("evm_increaseTime", vec![json!(increase_by)])
.await
.map(|_json_value| ())
.map_err(|e| format!("Failed to increase time on EVM (is this ganache?): {:?}", e))
}
/// Returns the current block number, as u64
pub async fn block_number(&self) -> Result<u64, String> {
self.web3
.eth()
.block_number()
.await
.map(|v| v.as_u64())
.map_err(|e| format!("Failed to get block number: {:?}", e))
}
/// Mines a single block.
pub async fn evm_mine(&self) -> Result<(), String> {
self.web3
.transport()
.execute("evm_mine", vec![])
.await
.map(|_| ())
.map_err(|_| {
"utils should mine new block with evm_mine (only works with ganache!)".to_string()
})
}
}
fn endpoint(port: u16) -> String {
format!("http://127.0.0.1:{}", port)
}
impl Drop for GanacheInstance {
fn drop(&mut self) {
if cfg!(windows) {
// Calling child.kill() in Windows will only kill the process
// that spawned ganache, leaving the actual ganache process
// intact. You have to kill the whole process tree. What's more,
// if you don't spawn ganache with --keepAliveTimeout=0, Windows
// will STILL keep the server running even after you've ended
// the process tree and it's disappeared from the task manager.
// Unbelievable...
Command::new("taskkill")
.arg("/pid")
.arg(self.child.id().to_string())
.arg("/T")
.arg("/F")
.output()
.expect("failed to execute taskkill");
} else {
let _ = self.child.kill();
}
}
}

View File

@@ -1,77 +1,79 @@
//! Provides utilities for deploying and manipulating the eth2 deposit contract on the eth1 chain.
//!
//! Presently used with [`ganache`](https://github.com/trufflesuite/ganache) to simulate
//! Presently used with [`anvil`](https://github.com/foundry-rs/foundry/tree/master/anvil) to simulate
//! the deposit contract for testing beacon node eth1 integration.
//!
//! Not tested to work with actual clients (e.g., geth). It should work fine, however there may be
//! some initial issues.
mod ganache;
mod anvil;
use anvil::AnvilCliInstance;
use deposit_contract::{
encode_eth1_tx_data, testnet, ABI, BYTECODE, CONTRACT_DEPLOY_GAS, DEPOSIT_GAS,
};
use ganache::GanacheInstance;
use ethers_contract::Contract;
use ethers_core::{
abi::Abi,
types::{transaction::eip2718::TypedTransaction, Address, Bytes, TransactionRequest, U256},
};
pub use ethers_providers::{Http, Middleware, Provider};
use std::time::Duration;
use tokio::time::sleep;
use types::DepositData;
use types::{test_utils::generate_deterministic_keypair, EthSpec, Hash256, Keypair, Signature};
use web3::contract::{Contract, Options};
use web3::transports::Http;
use web3::types::{Address, TransactionRequest, U256};
use web3::Web3;
pub const DEPLOYER_ACCOUNTS_INDEX: usize = 0;
pub const DEPOSIT_ACCOUNTS_INDEX: usize = 0;
/// Provides a dedicated ganache instance with the deposit contract already deployed.
pub struct GanacheEth1Instance {
pub ganache: GanacheInstance,
/// Provides a dedicated anvil instance with the deposit contract already deployed.
pub struct AnvilEth1Instance {
pub anvil: AnvilCliInstance,
pub deposit_contract: DepositContract,
}
impl GanacheEth1Instance {
impl AnvilEth1Instance {
pub async fn new(chain_id: u64) -> Result<Self, String> {
let ganache = GanacheInstance::new(chain_id)?;
DepositContract::deploy(ganache.web3.clone(), 0, None)
let anvil = AnvilCliInstance::new(chain_id)?;
DepositContract::deploy(anvil.client.clone(), 0, None)
.await
.map(|deposit_contract| Self {
ganache,
anvil,
deposit_contract,
})
}
pub fn endpoint(&self) -> String {
self.ganache.endpoint()
self.anvil.endpoint()
}
pub fn web3(&self) -> Web3<Http> {
self.ganache.web3.clone()
pub fn json_rpc_client(&self) -> Provider<Http> {
self.anvil.client.clone()
}
}
/// Deploys and provides functions for the eth2 deposit contract, deployed on the eth1 chain.
#[derive(Clone, Debug)]
pub struct DepositContract {
web3: Web3<Http>,
contract: Contract<Http>,
client: Provider<Http>,
contract: Contract<Provider<Http>>,
}
impl DepositContract {
pub async fn deploy(
web3: Web3<Http>,
client: Provider<Http>,
confirmations: usize,
password: Option<String>,
) -> Result<Self, String> {
Self::deploy_bytecode(web3, confirmations, BYTECODE, ABI, password).await
Self::deploy_bytecode(client, confirmations, BYTECODE, ABI, password).await
}
pub async fn deploy_testnet(
web3: Web3<Http>,
client: Provider<Http>,
confirmations: usize,
password: Option<String>,
) -> Result<Self, String> {
Self::deploy_bytecode(
web3,
client,
confirmations,
testnet::BYTECODE,
testnet::ABI,
@@ -81,29 +83,25 @@ impl DepositContract {
}
async fn deploy_bytecode(
web3: Web3<Http>,
client: Provider<Http>,
confirmations: usize,
bytecode: &[u8],
abi: &[u8],
password: Option<String>,
) -> Result<Self, String> {
let address = deploy_deposit_contract(
web3.clone(),
confirmations,
bytecode.to_vec(),
abi.to_vec(),
password,
)
.await
.map_err(|e| {
format!(
"Failed to deploy contract: {}. Is scripts/ganache_tests_node.sh running?.",
e
)
})?;
Contract::from_json(web3.clone().eth(), address, ABI)
.map_err(|e| format!("Failed to init contract: {:?}", e))
.map(move |contract| Self { web3, contract })
let abi = Abi::load(abi).map_err(|e| format!("Invalid deposit contract abi: {:?}", e))?;
let address =
deploy_deposit_contract(client.clone(), confirmations, bytecode.to_vec(), password)
.await
.map_err(|e| {
format!(
"Failed to deploy contract: {}. Is scripts/anvil_tests_node.sh running?.",
e
)
})?;
let contract = Contract::new(address, abi, client.clone());
Ok(Self { client, contract })
}
/// The deposit contract's address in `0x00ab...` format.
@@ -178,9 +176,8 @@ impl DepositContract {
/// Performs a non-blocking deposit.
pub async fn deposit_async(&self, deposit_data: DepositData) -> Result<(), String> {
let from = self
.web3
.eth()
.accounts()
.client
.get_accounts()
.await
.map_err(|e| format!("Failed to get accounts: {:?}", e))
.and_then(|accounts| {
@@ -189,32 +186,33 @@ impl DepositContract {
.cloned()
.ok_or_else(|| "Insufficient accounts for deposit".to_string())
})?;
let tx_request = TransactionRequest {
from,
to: Some(self.contract.address()),
gas: Some(U256::from(DEPOSIT_GAS)),
gas_price: None,
max_fee_per_gas: None,
max_priority_fee_per_gas: None,
value: Some(from_gwei(deposit_data.amount)),
// Note: the reason we use this `TransactionRequest` instead of just using the
// function in `self.contract` is so that the `eth1_tx_data` function gets used
// during testing.
//
// It's important that `eth1_tx_data` stays correct and does not suffer from
// code-rot.
data: encode_eth1_tx_data(&deposit_data).map(Into::into).ok(),
nonce: None,
condition: None,
transaction_type: None,
access_list: None,
};
// Note: the reason we use this `TransactionRequest` instead of just using the
// function in `self.contract` is so that the `eth1_tx_data` function gets used
// during testing.
//
// It's important that `eth1_tx_data` stays correct and does not suffer from
// code-rot.
let tx_request = TransactionRequest::new()
.from(from)
.to(self.contract.address())
.gas(DEPOSIT_GAS)
.value(from_gwei(deposit_data.amount))
.data(Bytes::from(encode_eth1_tx_data(&deposit_data).map_err(
|e| format!("Failed to encode deposit data: {:?}", e),
)?));
self.web3
.eth()
.send_transaction(tx_request)
let pending_tx = self
.client
.send_transaction(tx_request, None)
.await
.map_err(|e| format!("Failed to call deposit fn: {:?}", e))?;
pending_tx
.interval(Duration::from_millis(10))
.confirmations(0)
.await
.map_err(|e| format!("Transaction failed to resolve: {:?}", e))?
.ok_or_else(|| "Transaction dropped from mempool".to_string())?;
Ok(())
}
@@ -245,17 +243,13 @@ fn from_gwei(gwei: u64) -> U256 {
/// Deploys the deposit contract to the given web3 instance using the account with index
/// `DEPLOYER_ACCOUNTS_INDEX`.
async fn deploy_deposit_contract(
web3: Web3<Http>,
client: Provider<Http>,
confirmations: usize,
bytecode: Vec<u8>,
abi: Vec<u8>,
password_opt: Option<String>,
) -> Result<Address, String> {
let bytecode = String::from_utf8(bytecode).expect("bytecode must be valid utf8");
let from_address = web3
.eth()
.accounts()
let from_address = client
.get_accounts()
.await
.map_err(|e| format!("Failed to get accounts: {:?}", e))
.and_then(|accounts| {
@@ -266,30 +260,42 @@ async fn deploy_deposit_contract(
})?;
let deploy_address = if let Some(password) = password_opt {
let result = web3
.personal()
.unlock_account(from_address, &password, None)
let result = client
.request(
"personal_unlockAccount",
vec![from_address.to_string(), password],
)
.await;
match result {
Ok(true) => return Ok(from_address),
Ok(true) => from_address,
Ok(false) => return Err("Eth1 node refused to unlock account".to_string()),
Err(e) => return Err(format!("Eth1 unlock request failed: {:?}", e)),
};
}
} else {
from_address
};
let pending_contract = Contract::deploy(web3.eth(), &abi)
.map_err(|e| format!("Unable to build contract deployer: {:?}", e))?
.confirmations(confirmations)
.options(Options {
gas: Some(U256::from(CONTRACT_DEPLOY_GAS)),
..Options::default()
})
.execute(bytecode, (), deploy_address);
let mut bytecode = String::from_utf8(bytecode).unwrap();
bytecode.retain(|c| c.is_ascii_hexdigit());
let bytecode = hex::decode(&bytecode[1..]).unwrap();
pending_contract
let deploy_tx: TypedTransaction = TransactionRequest::new()
.from(deploy_address)
.data(Bytes::from(bytecode))
.gas(CONTRACT_DEPLOY_GAS)
.into();
let pending_tx = client
.send_transaction(deploy_tx, None)
.await
.map(|contract| contract.address())
.map_err(|e| format!("Unable to resolve pending contract: {:?}", e))
.map_err(|e| format!("Failed to send tx: {:?}", e))?;
let tx = pending_tx
.interval(Duration::from_millis(500))
.confirmations(confirmations)
.await
.map_err(|e| format!("Failed to fetch tx receipt: {:?}", e))?;
tx.and_then(|tx| tx.contract_address)
.ok_or_else(|| "Deposit contract not deployed successfully".to_string())
}

View File

@@ -10,7 +10,7 @@ pub fn cli_app<'a, 'b>() -> App<'a, 'b> {
.about(
"Lighthouse Beacon Chain Simulator creates `n` beacon node and validator clients, \
each with `v` validators. A deposit contract is deployed at the start of the \
simulation using a local `ganache` instance (you must have `ganache` \
simulation using a local `anvil` instance (you must have `anvil` \
installed and avaliable on your path). All beacon nodes independently listen \
for genesis from the deposit contract, then start operating. \
\

View File

@@ -2,7 +2,7 @@ use crate::local_network::{EXECUTION_PORT, TERMINAL_BLOCK, TERMINAL_DIFFICULTY};
use crate::{checks, LocalNetwork, E};
use clap::ArgMatches;
use eth1::{Eth1Endpoint, DEFAULT_CHAIN_ID};
use eth1_test_rig::GanacheEth1Instance;
use eth1_test_rig::AnvilEth1Instance;
use execution_layer::http::deposit_methods::Eth1Id;
use futures::prelude::*;
@@ -110,12 +110,12 @@ pub fn run_eth1_sim(matches: &ArgMatches) -> Result<(), String> {
* Deploy the deposit contract, spawn tasks to keep creating new blocks and deposit
* validators.
*/
let ganache_eth1_instance = GanacheEth1Instance::new(DEFAULT_CHAIN_ID.into()).await?;
let deposit_contract = ganache_eth1_instance.deposit_contract;
let chain_id = ganache_eth1_instance.ganache.chain_id();
let ganache = ganache_eth1_instance.ganache;
let eth1_endpoint = SensitiveUrl::parse(ganache.endpoint().as_str())
.expect("Unable to parse ganache endpoint.");
let anvil_eth1_instance = AnvilEth1Instance::new(DEFAULT_CHAIN_ID.into()).await?;
let deposit_contract = anvil_eth1_instance.deposit_contract;
let chain_id = anvil_eth1_instance.anvil.chain_id();
let anvil = anvil_eth1_instance.anvil;
let eth1_endpoint = SensitiveUrl::parse(anvil.endpoint().as_str())
.expect("Unable to parse anvil endpoint.");
let deposit_contract_address = deposit_contract.address();
// Start a timer that produces eth1 blocks on an interval.
@@ -123,7 +123,7 @@ pub fn run_eth1_sim(matches: &ArgMatches) -> Result<(), String> {
let mut interval = tokio::time::interval(eth1_block_time);
loop {
interval.tick().await;
let _ = ganache.evm_mine().await;
let _ = anvil.evm_mine().await;
}
});

View File

@@ -1,6 +1,6 @@
//! This crate provides a simluation that creates `n` beacon node and validator clients, each with
//! `v` validators. A deposit contract is deployed at the start of the simulation using a local
//! `ganache` instance (you must have `ganache` installed and avaliable on your path). All
//! `anvil` instance (you must have `anvil` installed and avaliable on your path). All
//! beacon nodes independently listen for genesis from the deposit contract, then start operating.
//!
//! As the simulation runs, there are checks made to ensure that all components are running