Skip to content

Commit

Permalink
feat: use alloy genesis now (#6135)
Browse files Browse the repository at this point in the history
Co-authored-by: Matthias Seitz <[email protected]>
  • Loading branch information
supernovahs and mattsse authored Jan 20, 2024
1 parent 51b40a0 commit bf3264b
Show file tree
Hide file tree
Showing 16 changed files with 257 additions and 1,461 deletions.
30 changes: 27 additions & 3 deletions Cargo.lock

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

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -169,14 +169,17 @@ alloy-primitives = "0.6"
alloy-dyn-abi = "0.6"
alloy-sol-types = "0.6"
alloy-rlp = "0.3"
alloy-trie = "0.2"
alloy-rpc-types = { git = "https://github.com/alloy-rs/alloy" }
alloy-rpc-trace-types = { git = "https://github.com/alloy-rs/alloy" }
alloy-rpc-engine-types = { git = "https://github.com/alloy-rs/alloy" }
alloy-trie = "0.2"
alloy-genesis = { git = "https://github.com/alloy-rs/alloy"}
alloy-node-bindings = {git = "https://github.com/alloy-rs/alloy"}
ethers-core = { version = "2.0", default-features = false }
ethers-providers = { version = "2.0", default-features = false }
ethers-signers = { version = "2.0", default-features = false }
ethers-middleware = { version = "2.0", default-features = false }

discv5 = { git = "https://github.com/sigp/discv5", rev = "f289bbd4c57d499bb1bdb393af3c249600a1c662" }
igd = { git = "https://github.com/stevefan1999-personal/rust-igd", rev = "c2d1f83eb1612a462962453cb0703bc93258b173" }

Expand Down
7 changes: 5 additions & 2 deletions bin/reth/src/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,11 @@ pub fn insert_genesis_hashes<DB: Database>(
genesis: &reth_primitives::Genesis,
) -> ProviderResult<()> {
// insert and hash accounts to hashing table
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)?;

let alloc_storage = genesis.alloc.clone().into_iter().filter_map(|(addr, account)| {
Expand Down
1 change: 1 addition & 0 deletions crates/consensus/beacon/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
5 changes: 4 additions & 1 deletion crates/consensus/beacon/src/engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
1 change: 1 addition & 0 deletions crates/net/network/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
10 changes: 6 additions & 4 deletions crates/net/network/src/transactions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
4 changes: 2 additions & 2 deletions crates/net/network/tests/it/clique/clique_middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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))
Expand Down
16 changes: 8 additions & 8 deletions crates/net/network/tests/it/clique/geth.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -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 {
///
Expand All @@ -34,15 +34,15 @@ 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>>,
}

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
Expand All @@ -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) {
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion crates/net/network/tests/it/connect.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
13 changes: 7 additions & 6 deletions crates/net/network/tests/it/geth.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
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::{
test_utils::{unused_tcp_and_udp_port, unused_tcp_udp, NetworkEventStream},
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() {
Expand Down Expand Up @@ -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());

Expand Down
2 changes: 1 addition & 1 deletion crates/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }

Expand Down
14 changes: 12 additions & 2 deletions crates/primitives/src/account.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down Expand Up @@ -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 {
Expand Down
Loading

0 comments on commit bf3264b

Please sign in to comment.