feat: use alloy genesis now (#6135)

Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
This commit is contained in:
Supernovahs.eth
2024-01-20 15:38:32 +05:30
committed by GitHub
parent 51b40a029d
commit bf3264b257
16 changed files with 287 additions and 1491 deletions

View File

@@ -45,6 +45,7 @@ cfg-if = "1.0.0"
[dev-dependencies]
# reth
reth-payload-builder = { workspace = true, features = ["test-utils"] }
reth-primitives = { workspace = true, features = ["test-utils"] }
reth-interfaces = { workspace = true, features = ["test-utils"] }
reth-stages = { workspace = true, features = ["test-utils"] }
reth-blockchain-tree = { workspace = true, features = ["test-utils"] }

View File

@@ -2346,7 +2346,10 @@ mod tests {
mod new_payload {
use super::*;
use reth_interfaces::test_utils::{generators, generators::random_block};
use reth_primitives::{Genesis, GenesisAllocator, Hardfork, U256};
use reth_primitives::{
genesis::{Genesis, GenesisAllocator},
Hardfork, U256,
};
use reth_provider::test_utils::blocks::BlockChainTestData;
#[tokio::test]

View File

@@ -84,6 +84,7 @@ reth-provider = { workspace = true, features = ["test-utils"] }
reth-tracing.workspace = true
reth-transaction-pool = { workspace = true, features = ["test-utils"] }
alloy-node-bindings.workspace = true
ethers-core = { workspace = true, default-features = false }
ethers-providers = { workspace = true, default-features = false, features = ["ws"] }
ethers-signers = { workspace = true, default-features = false }

View File

@@ -1258,23 +1258,25 @@ mod tests {
use super::*;
use crate::{test_utils::Testnet, NetworkConfigBuilder, NetworkManager};
use alloy_rlp::Decodable;
use fetcher::MAX_ALTERNATIVE_PEERS_PER_TX;
use futures::FutureExt;
use reth_interfaces::sync::{NetworkSyncUpdater, SyncState};
use reth_network_api::NetworkInfo;
use reth_primitives::hex;
use reth_provider::test_utils::NoopProvider;
use reth_transaction_pool::test_utils::{testing_pool, MockTransaction};
use secp256k1::SecretKey;
use std::{future::poll_fn, hash};
use fetcher::MAX_ALTERNATIVE_PEERS_PER_TX;
async fn new_tx_manager() -> TransactionsManager<impl TransactionPool> {
let secret_key = SecretKey::new(&mut rand::thread_rng());
let client = NoopProvider::default();
let config = NetworkConfigBuilder::new(secret_key).disable_discovery().build(client);
let config = NetworkConfigBuilder::new(secret_key)
// let OS choose port
.listener_port(0)
.disable_discovery()
.build(client);
let pool = testing_pool();

View File

@@ -91,7 +91,7 @@ pub trait CliqueMiddleware: Send + Sync + Middleware {
Ok(())
}
/// Returns the genesis block of the [`Geth`](ethers_core::utils::Geth) instance by calling
/// Returns the genesis block of the [`Geth`](alloy_node_bindings::Geth) instance by calling
/// geth's `eth_getBlock`.
async fn remote_genesis_block(&self) -> Result<Block<H256>, CliqueMiddlewareError<Self>> {
self.get_block(BlockNumber::Earliest).await?.ok_or(CliqueError::NoGenesis)
@@ -109,7 +109,7 @@ pub trait CliqueMiddleware: Send + Sync + Middleware {
Ok(())
}
/// Returns the [`Geth`](ethers_core::utils::Geth) instance [`PeerId`](reth_primitives::PeerId)
/// Returns the [`Geth`](alloy_node_bindings::Geth) instance [`PeerId`](reth_primitives::PeerId)
/// by calling geth's `admin_nodeInfo`.
async fn peer_id(&self) -> Result<PeerId, CliqueMiddlewareError<Self>> {
Ok(enr_to_peer_id(self.node_info().await?.enr))

View File

@@ -1,8 +1,8 @@
#![allow(unreachable_pub)]
//! Helper struct for working with a clique geth instance.
use alloy_node_bindings::{Geth, GethInstance};
use enr::k256::ecdsa::SigningKey;
use ethers_core::utils::{Geth, GethInstance};
use ethers_middleware::SignerMiddleware;
use ethers_providers::{Provider, Ws};
use ethers_signers::{LocalWallet, Wallet};
@@ -11,15 +11,15 @@ use std::{
net::SocketAddr,
};
/// A [`Geth`](ethers_core::utils::Geth) instance configured with Clique and a custom
/// [`Genesis`](ethers_core::utils::Genesis).
/// A [`Geth`](alloy_node_bindings::Geth) instance configured with Clique and a custom
/// [`Genesis`](reth_primitives::Genesis).
///
/// This holds a [`SignerMiddleware`](ethers_middleware::SignerMiddleware) for
/// enabling block production and creating transactions.
///
/// # Example
/// ```no_run
/// # use ethers_core::utils::Geth;
/// # use alloy_node_bindings::Geth;
/// # use reth_staged_sync::test_utils::CliqueGethInstance;
/// # let clique = async {
///
@@ -34,7 +34,7 @@ use std::{
/// # };
/// ```
pub struct CliqueGethInstance {
/// The spawned [`GethInstance`](ethers_core::utils::GethInstance).
/// The spawned [`GethInstance`](alloy_node_bindings::GethInstance).
pub instance: GethInstance,
/// The provider who can talk to this instance
pub provider: SignerMiddleware<Provider<Ws>, Wallet<SigningKey>>,
@@ -42,7 +42,7 @@ pub struct CliqueGethInstance {
impl CliqueGethInstance {
/// Sets up a new [`SignerMiddleware`](ethers_middleware::SignerMiddleware)
/// for the [`Geth`](ethers_core::utils::Geth) instance and returns the
/// for the [`Geth`](alloy_node_bindings::Geth) instance and returns the
/// [`CliqueGethInstance`](crate::test_utils::CliqueGethInstance).
///
/// The signer is assumed to be the clique signer and the signer for any transactions sent for
@@ -69,7 +69,7 @@ impl CliqueGethInstance {
Self { instance, provider }
}
/// Prints the logs of the [`Geth`](ethers_core::utils::Geth) instance in a new
/// Prints the logs of the [`Geth`](alloy_node_bindings::Geth) instance in a new
/// [`task`](tokio::task).
#[allow(dead_code)]
pub async fn print_logs(&mut self) {
@@ -96,7 +96,7 @@ impl CliqueGethInstance {
});
}
/// Prevents the [`Geth`](ethers_core::utils::Geth) instance from blocking due to the `stderr`
/// Prevents the [`Geth`](alloy_node_bindings::Geth) instance from blocking due to the `stderr`
/// filling up.
pub async fn prevent_blocking(&mut self) {
// take the stderr of the geth instance and print it

View File

@@ -1,6 +1,6 @@
//! Connection tests
use ethers_core::utils::Geth;
use alloy_node_bindings::Geth;
use ethers_providers::{Http, Middleware, Provider};
use futures::StreamExt;
use reth_discv4::Discv4Config;

View File

@@ -1,7 +1,7 @@
use crate::clique::{CliqueGethInstance, CliqueMiddleware};
use ethers_core::{
types::{transaction::eip2718::TypedTransaction, Address, Eip1559TransactionRequest},
utils::Geth,
use alloy_node_bindings::Geth;
use ethers_core::types::{
transaction::eip2718::TypedTransaction, Address, Eip1559TransactionRequest,
};
use ethers_providers::Middleware;
use reth_network::{
@@ -9,10 +9,11 @@ use reth_network::{
NetworkConfig, NetworkEvents, NetworkManager,
};
use reth_network_api::Peers;
use reth_primitives::{ChainSpec, Genesis, PeerId, SealedHeader};
use reth_primitives::{ChainSpec, PeerId, SealedHeader};
use reth_provider::test_utils::NoopProvider;
use secp256k1::SecretKey;
use std::{net::SocketAddr, sync::Arc};
#[tokio::test(flavor = "multi_thread")]
#[cfg_attr(not(feature = "geth-tests"), ignore)]
async fn can_peer_with_geth() {
@@ -97,9 +98,9 @@ async fn init_geth() -> (CliqueGethInstance, Arc<ChainSpec>) {
// === check that we have the same genesis hash ===
// get the chainspec from the genesis we configured for geth
let chainspec = ChainSpec::from(Genesis::from(
let chainspec = ChainSpec::from(
clique.instance.genesis().clone().expect("clique should be configured with a genesis"),
));
);
let remote_genesis = SealedHeader::from(&clique.provider.remote_genesis_block().await.unwrap());

View File

@@ -26,7 +26,7 @@ alloy-rlp = { workspace = true, features = ["arrayvec"] }
alloy-trie = { workspace = true, features = ["serde"] }
ethers-core = { workspace = true, default-features = false, optional = true }
nybbles = { version = "0.1.2", features = ["serde", "rlp"] }
alloy-genesis.workspace = true
# crypto
secp256k1 = { workspace = true, features = ["global-context", "recovery"] }

View File

@@ -1,13 +1,13 @@
use crate::{
keccak256,
revm_primitives::{Bytecode as RevmBytecode, BytecodeState, Bytes, JumpMap},
B256, KECCAK_EMPTY, U256,
GenesisAccount, B256, KECCAK_EMPTY, U256,
};
use byteorder::{BigEndian, ReadBytesExt};
use bytes::Buf;
use reth_codecs::{main_codec, Compact};
use serde::{Deserialize, Serialize};
use std::ops::Deref;
/// An Ethereum account.
#[main_codec]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
@@ -37,6 +37,16 @@ impl Account {
self.nonce == 0 && self.balance.is_zero() && is_bytecode_empty
}
/// Converts [GenesisAccount] to [Account] type
pub fn from_genesis_account(value: GenesisAccount) -> Self {
Account {
// nonce must exist, so we default to zero when converting a genesis account
nonce: value.nonce.unwrap_or_default(),
balance: value.balance,
bytecode_hash: value.code.map(keccak256),
}
}
/// Returns an account bytecode's hash.
/// In case of no bytecode, returns [`KECCAK_EMPTY`].
pub fn get_bytecode_hash(&self) -> B256 {

File diff suppressed because it is too large Load Diff

View File

@@ -23,7 +23,7 @@ pub mod constants;
pub mod eip4844;
mod error;
pub mod fs;
mod genesis;
pub mod genesis;
mod header;
mod integer_list;
mod log;
@@ -59,7 +59,7 @@ pub use constants::{
KECCAK_EMPTY, MAINNET_GENESIS_HASH, SEPOLIA_GENESIS_HASH,
};
pub use error::{GotExpected, GotExpectedBoxed};
pub use genesis::{ChainConfig, Genesis, GenesisAccount, GenesisAllocator};
pub use genesis::{ChainConfig, Genesis, GenesisAccount};
pub use header::{Header, HeadersDirection, SealedHeader};
pub use integer_list::IntegerList;
pub use log::{logs_bloom, Log};

View File

@@ -206,8 +206,11 @@ mod tests {
// Hash accounts and insert them into hashing table.
let genesis = chain_spec.genesis();
let alloc_accounts =
genesis.alloc.clone().into_iter().map(|(addr, account)| (addr, Some(account.into())));
let alloc_accounts = genesis
.alloc
.clone()
.into_iter()
.map(|(addr, account)| (addr, Some(Account::from_genesis_account(account))));
provider.insert_account_for_hashing(alloc_accounts).unwrap();
let alloc_storage = genesis.alloc.clone().into_iter().filter_map(|(addr, account)| {