Merge branch 'unstable' into deneb-free-blobs

# Conflicts:
#	.github/workflows/docker.yml
#	.github/workflows/local-testnet.yml
#	.github/workflows/test-suite.yml
#	Cargo.lock
#	Cargo.toml
#	beacon_node/beacon_chain/src/beacon_chain.rs
#	beacon_node/beacon_chain/src/builder.rs
#	beacon_node/beacon_chain/src/test_utils.rs
#	beacon_node/execution_layer/src/engine_api/json_structures.rs
#	beacon_node/network/src/beacon_processor/mod.rs
#	beacon_node/network/src/beacon_processor/worker/gossip_methods.rs
#	beacon_node/network/src/sync/backfill_sync/mod.rs
#	beacon_node/store/src/config.rs
#	beacon_node/store/src/hot_cold_store.rs
#	common/eth2_network_config/Cargo.toml
#	consensus/ssz/src/decode/impls.rs
#	consensus/ssz_derive/src/lib.rs
#	consensus/ssz_derive/tests/tests.rs
#	consensus/ssz_types/src/serde_utils/mod.rs
#	consensus/tree_hash/src/impls.rs
#	consensus/tree_hash/src/lib.rs
#	consensus/types/Cargo.toml
#	consensus/types/src/beacon_state.rs
#	consensus/types/src/chain_spec.rs
#	consensus/types/src/eth_spec.rs
#	consensus/types/src/fork_name.rs
#	lcli/Cargo.toml
#	lcli/src/main.rs
#	lcli/src/new_testnet.rs
#	scripts/local_testnet/el_bootnode.sh
#	scripts/local_testnet/genesis.json
#	scripts/local_testnet/geth.sh
#	scripts/local_testnet/setup.sh
#	scripts/local_testnet/start_local_testnet.sh
#	scripts/local_testnet/vars.env
#	scripts/tests/doppelganger_protection.sh
#	scripts/tests/genesis.json
#	scripts/tests/vars.env
#	testing/ef_tests/Cargo.toml
#	validator_client/src/block_service.rs
This commit is contained in:
Jimmy Chen
2023-05-30 11:26:33 +10:00
333 changed files with 5930 additions and 13386 deletions

View File

@@ -25,11 +25,11 @@ serde_json = "1.0.58"
serde_repr = "0.1.6"
serde_yaml = "0.8.13"
eth2_network_config = { path = "../../common/eth2_network_config" }
eth2_serde_utils = { path = "../../consensus/serde_utils" }
eth2_ssz = "0.4.1"
eth2_ssz_derive = "0.3.1"
tree_hash = "0.4.1"
tree_hash_derive = "0.4.0"
ethereum_serde_utils = "0.5.0"
ethereum_ssz = "0.5.0"
ethereum_ssz_derive = "0.5.0"
tree_hash = "0.5.0"
tree_hash_derive = "0.5.0"
cached_tree_hash = { path = "../../consensus/cached_tree_hash" }
state_processing = { path = "../../consensus/state_processing" }
swap_or_not_shuffle = { path = "../../consensus/swap_or_not_shuffle" }

View File

@@ -5,7 +5,7 @@ use crate::decode::{ssz_decode_file_with, ssz_decode_state, yaml_decode_file};
use serde_derive::Deserialize;
use state_processing::{
per_block_processing, per_slot_processing, BlockProcessingError, BlockSignatureStrategy,
ConsensusContext, VerifyBlockRoot,
ConsensusContext, StateProcessingStrategy, VerifyBlockRoot,
};
use types::{BeaconState, EthSpec, ForkName, RelativeEpoch, SignedBeaconBlock};
@@ -96,6 +96,7 @@ impl<E: EthSpec> Case for SanityBlocks<E> {
&mut indiv_state,
signed_block,
BlockSignatureStrategy::VerifyIndividual,
StateProcessingStrategy::Accurate,
VerifyBlockRoot::True,
&mut ctxt,
spec,
@@ -106,6 +107,7 @@ impl<E: EthSpec> Case for SanityBlocks<E> {
&mut bulk_state,
signed_block,
BlockSignatureStrategy::VerifyBulk,
StateProcessingStrategy::Accurate,
VerifyBlockRoot::True,
&mut ctxt,
spec,

View File

@@ -4,7 +4,7 @@ use crate::decode::{ssz_decode_file_with, ssz_decode_state, yaml_decode_file};
use serde_derive::Deserialize;
use state_processing::{
per_block_processing, state_advance::complete_state_advance, BlockSignatureStrategy,
ConsensusContext, VerifyBlockRoot,
ConsensusContext, StateProcessingStrategy, VerifyBlockRoot,
};
use std::str::FromStr;
use types::{BeaconState, Epoch, ForkName, SignedBeaconBlock};
@@ -107,6 +107,7 @@ impl<E: EthSpec> Case for TransitionTest<E> {
&mut state,
block,
BlockSignatureStrategy::VerifyBulk,
StateProcessingStrategy::Accurate,
VerifyBlockRoot::True,
&mut ctxt,
spec,

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. \
\
@@ -24,6 +24,12 @@ pub fn cli_app<'a, 'b>() -> App<'a, 'b> {
.takes_value(true)
.default_value("4")
.help("Number of beacon nodes"))
.arg(Arg::with_name("proposer-nodes")
.short("n")
.long("nodes")
.takes_value(true)
.default_value("2")
.help("Number of proposer-only beacon nodes"))
.arg(Arg::with_name("validators_per_node")
.short("v")
.long("validators_per_node")
@@ -57,6 +63,12 @@ pub fn cli_app<'a, 'b>() -> App<'a, 'b> {
.takes_value(true)
.default_value("4")
.help("Number of beacon nodes"))
.arg(Arg::with_name("proposer-nodes")
.short("n")
.long("nodes")
.takes_value(true)
.default_value("2")
.help("Number of proposer-only beacon nodes"))
.arg(Arg::with_name("validators_per_node")
.short("v")
.long("validators_per_node")

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::*;
@@ -27,6 +27,8 @@ const SUGGESTED_FEE_RECIPIENT: [u8; 20] =
pub fn run_eth1_sim(matches: &ArgMatches) -> Result<(), String> {
let node_count = value_t!(matches, "nodes", usize).expect("missing nodes default");
let proposer_nodes = value_t!(matches, "proposer-nodes", usize).unwrap_or(0);
println!("PROPOSER-NODES: {}", proposer_nodes);
let validators_per_node = value_t!(matches, "validators_per_node", usize)
.expect("missing validators_per_node default");
let speed_up_factor =
@@ -35,7 +37,8 @@ pub fn run_eth1_sim(matches: &ArgMatches) -> Result<(), String> {
let post_merge_sim = matches.is_present("post-merge");
println!("Beacon Chain Simulator:");
println!(" nodes:{}", node_count);
println!(" nodes:{}, proposer_nodes: {}", node_count, proposer_nodes);
println!(" validators_per_node:{}", validators_per_node);
println!(" post merge simulation:{}", post_merge_sim);
println!(" continue_after_checks:{}", continue_after_checks);
@@ -69,6 +72,7 @@ pub fn run_eth1_sim(matches: &ArgMatches) -> Result<(), String> {
max_log_number: 0,
compression: false,
is_restricted: true,
sse_logging: false,
})?
.multi_threaded_tokio_runtime()?
.build()?;
@@ -107,12 +111,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.
@@ -120,7 +124,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;
}
});
@@ -147,7 +151,7 @@ pub fn run_eth1_sim(matches: &ArgMatches) -> Result<(), String> {
beacon_config.sync_eth1_chain = true;
beacon_config.eth1.auto_update_interval_millis = eth1_block_time.as_millis() as u64;
beacon_config.eth1.chain_id = Eth1Id::from(chain_id);
beacon_config.network.target_peers = node_count - 1;
beacon_config.network.target_peers = node_count + proposer_nodes - 1;
beacon_config.network.enr_address = (Some(Ipv4Addr::LOCALHOST), None);
@@ -173,7 +177,17 @@ pub fn run_eth1_sim(matches: &ArgMatches) -> Result<(), String> {
* One by one, add beacon nodes to the network.
*/
for _ in 0..node_count - 1 {
network.add_beacon_node(beacon_config.clone()).await?;
network
.add_beacon_node(beacon_config.clone(), false)
.await?;
}
/*
* One by one, add proposer nodes to the network.
*/
for _ in 0..proposer_nodes - 1 {
println!("Adding a proposer node");
network.add_beacon_node(beacon_config.clone(), true).await?;
}
/*
@@ -310,7 +324,7 @@ pub fn run_eth1_sim(matches: &ArgMatches) -> Result<(), String> {
*/
println!(
"Simulation complete. Finished with {} beacon nodes and {} validator clients",
network.beacon_node_count(),
network.beacon_node_count() + network.proposer_node_count(),
network.validator_client_count()
);

View File

@@ -25,6 +25,7 @@ pub const TERMINAL_BLOCK: u64 = 64;
pub struct Inner<E: EthSpec> {
pub context: RuntimeContext<E>,
pub beacon_nodes: RwLock<Vec<LocalBeaconNode<E>>>,
pub proposer_nodes: RwLock<Vec<LocalBeaconNode<E>>>,
pub validator_clients: RwLock<Vec<LocalValidatorClient<E>>>,
pub execution_nodes: RwLock<Vec<LocalExecutionNode<E>>>,
}
@@ -97,6 +98,7 @@ impl<E: EthSpec> LocalNetwork<E> {
inner: Arc::new(Inner {
context,
beacon_nodes: RwLock::new(vec![beacon_node]),
proposer_nodes: RwLock::new(vec![]),
execution_nodes: RwLock::new(execution_node),
validator_clients: RwLock::new(vec![]),
}),
@@ -111,6 +113,14 @@ impl<E: EthSpec> LocalNetwork<E> {
self.beacon_nodes.read().len()
}
/// Returns the number of proposer nodes in the network.
///
/// Note: does not count nodes that are external to this `LocalNetwork` that may have connected
/// (e.g., another Lighthouse process on the same machine.)
pub fn proposer_node_count(&self) -> usize {
self.proposer_nodes.read().len()
}
/// Returns the number of validator clients in the network.
///
/// Note: does not count nodes that are external to this `LocalNetwork` that may have connected
@@ -120,7 +130,11 @@ impl<E: EthSpec> LocalNetwork<E> {
}
/// Adds a beacon node to the network, connecting to the 0'th beacon node via ENR.
pub async fn add_beacon_node(&self, mut beacon_config: ClientConfig) -> Result<(), String> {
pub async fn add_beacon_node(
&self,
mut beacon_config: ClientConfig,
is_proposer: bool,
) -> Result<(), String> {
let self_1 = self.clone();
let count = self.beacon_node_count() as u16;
println!("Adding beacon node..");
@@ -135,6 +149,7 @@ impl<E: EthSpec> LocalNetwork<E> {
.enr()
.expect("bootnode must have a network"),
);
let count = (self.beacon_node_count() + self.proposer_node_count()) as u16;
beacon_config.network.set_ipv4_listening_address(
std::net::Ipv4Addr::UNSPECIFIED,
BOOTNODE_PORT + count,
@@ -143,6 +158,7 @@ impl<E: EthSpec> LocalNetwork<E> {
beacon_config.network.enr_udp4_port = Some(BOOTNODE_PORT + count);
beacon_config.network.enr_tcp4_port = Some(BOOTNODE_PORT + count);
beacon_config.network.discv5_config.table_filter = |_| true;
beacon_config.network.proposer_only = is_proposer;
}
if let Some(el_config) = &mut beacon_config.execution_layer {
let config = MockExecutionConfig {
@@ -173,7 +189,11 @@ impl<E: EthSpec> LocalNetwork<E> {
beacon_config,
)
.await?;
self_1.beacon_nodes.write().push(beacon_node);
if is_proposer {
self_1.proposer_nodes.write().push(beacon_node);
} else {
self_1.beacon_nodes.write().push(beacon_node);
}
Ok(())
}
@@ -200,6 +220,16 @@ impl<E: EthSpec> LocalNetwork<E> {
.http_api_listen_addr()
.expect("Must have http started")
};
// If there is a proposer node for the same index, we will use that for proposing
let proposer_socket_addr = {
let read_lock = self.proposer_nodes.read();
read_lock.get(beacon_node).map(|proposer_node| {
proposer_node
.client
.http_api_listen_addr()
.expect("Must have http started")
})
};
let beacon_node = SensitiveUrl::parse(
format!("http://{}:{}", socket_addr.ip(), socket_addr.port()).as_str(),
@@ -210,6 +240,21 @@ impl<E: EthSpec> LocalNetwork<E> {
} else {
vec![beacon_node]
};
// If we have a proposer node established, use it.
if let Some(proposer_socket_addr) = proposer_socket_addr {
let url = SensitiveUrl::parse(
format!(
"http://{}:{}",
proposer_socket_addr.ip(),
proposer_socket_addr.port()
)
.as_str(),
)
.unwrap();
validator_config.proposer_nodes = vec![url];
}
let validator_client = LocalValidatorClient::production_with_insecure_keypairs(
context,
validator_config,
@@ -223,9 +268,11 @@ impl<E: EthSpec> LocalNetwork<E> {
/// For all beacon nodes in `Self`, return a HTTP client to access each nodes HTTP API.
pub fn remote_nodes(&self) -> Result<Vec<BeaconNodeHttpClient>, String> {
let beacon_nodes = self.beacon_nodes.read();
let proposer_nodes = self.proposer_nodes.read();
beacon_nodes
.iter()
.chain(proposer_nodes.iter())
.map(|beacon_node| beacon_node.remote_node())
.collect()
}

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

View File

@@ -54,6 +54,7 @@ pub fn run_no_eth1_sim(matches: &ArgMatches) -> Result<(), String> {
max_log_number: 0,
compression: false,
is_restricted: true,
sse_logging: false,
})?
.multi_threaded_tokio_runtime()?
.build()?;
@@ -100,7 +101,9 @@ pub fn run_no_eth1_sim(matches: &ArgMatches) -> Result<(), String> {
*/
for _ in 0..node_count - 1 {
network.add_beacon_node(beacon_config.clone()).await?;
network
.add_beacon_node(beacon_config.clone(), false)
.await?;
}
/*
@@ -151,7 +154,7 @@ pub fn run_no_eth1_sim(matches: &ArgMatches) -> Result<(), String> {
*/
println!(
"Simulation complete. Finished with {} beacon nodes and {} validator clients",
network.beacon_node_count(),
network.beacon_node_count() + network.proposer_node_count(),
network.validator_client_count()
);

View File

@@ -58,6 +58,7 @@ fn syncing_sim(
max_log_number: 0,
compression: false,
is_restricted: true,
sse_logging: false,
})?
.multi_threaded_tokio_runtime()?
.build()?;
@@ -228,7 +229,7 @@ pub async fn verify_one_node_sync<E: EthSpec>(
)
.await;
// Add a beacon node
network.add_beacon_node(beacon_config).await?;
network.add_beacon_node(beacon_config, false).await?;
// Check every `epoch_duration` if nodes are synced
// limited to at most `sync_timeout` epochs
let mut interval = tokio::time::interval(epoch_duration);
@@ -265,8 +266,10 @@ pub async fn verify_two_nodes_sync<E: EthSpec>(
)
.await;
// Add beacon nodes
network.add_beacon_node(beacon_config.clone()).await?;
network.add_beacon_node(beacon_config).await?;
network
.add_beacon_node(beacon_config.clone(), false)
.await?;
network.add_beacon_node(beacon_config, false).await?;
// Check every `epoch_duration` if nodes are synced
// limited to at most `sync_timeout` epochs
let mut interval = tokio::time::interval(epoch_duration);
@@ -305,8 +308,10 @@ pub async fn verify_in_between_sync<E: EthSpec>(
)
.await;
// Add two beacon nodes
network.add_beacon_node(beacon_config.clone()).await?;
network.add_beacon_node(beacon_config).await?;
network
.add_beacon_node(beacon_config.clone(), false)
.await?;
network.add_beacon_node(beacon_config, false).await?;
// Delay before adding additional syncing nodes.
epoch_delay(
Epoch::new(sync_timeout - 5),
@@ -315,7 +320,7 @@ pub async fn verify_in_between_sync<E: EthSpec>(
)
.await;
// Add a beacon node
network.add_beacon_node(config1.clone()).await?;
network.add_beacon_node(config1.clone(), false).await?;
// Check every `epoch_duration` if nodes are synced
// limited to at most `sync_timeout` epochs
let mut interval = tokio::time::interval(epoch_duration);

View File

@@ -9,7 +9,7 @@ edition = "2021"
[dependencies]
state_processing = { path = "../../consensus/state_processing" }
types = { path = "../../consensus/types" }
eth2_ssz = "0.4.1"
ethereum_ssz = "0.5.0"
beacon_chain = { path = "../../beacon_node/beacon_chain" }
lazy_static = "1.4.0"
tokio = { version = "1.14.0", features = ["rt-multi-thread"] }

View File

@@ -2,7 +2,7 @@ use super::*;
use beacon_chain::test_utils::{BeaconChainHarness, EphemeralHarnessType};
use state_processing::{
per_block_processing, per_block_processing::errors::ExitInvalid, BlockProcessingError,
BlockSignatureStrategy, ConsensusContext, VerifyBlockRoot,
BlockSignatureStrategy, ConsensusContext, StateProcessingStrategy, VerifyBlockRoot,
};
use types::{BeaconBlock, BeaconState, Epoch, EthSpec, SignedBeaconBlock};
@@ -69,6 +69,7 @@ impl ExitTest {
state,
block,
BlockSignatureStrategy::VerifyIndividual,
StateProcessingStrategy::Accurate,
VerifyBlockRoot::True,
&mut ctxt,
&E::default_spec(),