Skip to content

Commit

Permalink
Migrate from ethereum-types to alloy-primitives (#6078)
Browse files Browse the repository at this point in the history
* Remove use of ethers_core::RlpStream

* Merge branch 'unstable' of https://github.com/sigp/lighthouse into remove_use_of_ethers_core

* Remove old code

* Simplify keccak call

* Remove unused package

* Merge branch 'unstable' of https://github.com/ethDreamer/lighthouse into remove_use_of_ethers_core

* Merge branch 'unstable' into remove_use_of_ethers_core

* Run clippy

* Merge branch 'remove_use_of_ethers_core' of https://github.com/dospore/lighthouse into remove_use_of_ethers_core

* Check all cargo fmt

* migrate to alloy primitives init

* fix deps

* integrate alloy-primitives

* resolve dep issues

* more changes based on dep changes

* add TODOs

* Merge branch 'unstable' of https://github.com/sigp/lighthouse into remove_use_of_ethers_core

* Revert lock

* Add BeaconBlocksByRange v3

* continue migration

* Revert "Add BeaconBlocksByRange v3"

This reverts commit e3ce7fc.

* impl hash256 extended trait

* revert some uneeded diffs

* merge conflict resolved

* fix subnet id rshift calc

* rename to FixedBytesExtended

* debugging

* Merge branch 'unstable' of https://github.com/sigp/lighthouse into migrate-to-alloy-primitives

* fix failed test

* fixing more tests

* Merge branch 'unstable' of https://github.com/sigp/lighthouse into remove_use_of_ethers_core

* introduce a shim to convert between the two u256 types

* move alloy to wrokspace

* align alloy versions

* update

* update web3signer test certs

* refactor

* resolve failing tests

* linting

* fix graffiti string test

* fmt

* fix ef test

* resolve merge conflicts

* remove udep and revert cert

* cargo patch

* cyclic dep

* fix build error

* Merge branch 'unstable' of https://github.com/sigp/lighthouse into migrate-to-alloy-primitives

* resolve conflicts, update deps

* merge unstable

* fmt

* fix deps

* Merge branch 'unstable' of https://github.com/sigp/lighthouse into migrate-to-alloy-primitives

* resolve merge conflicts

* resolve conflicts, make necessary changes

* Remove patch

* fmt

* remove file

* merge conflicts

* sneaking in a smol change

* bump versions

* Merge remote-tracking branch 'origin/unstable' into migrate-to-alloy-primitives

* Updates for peerDAS

* Update ethereum_hashing to prevent dupe

* updated alloy-consensus, removed TODOs

* cargo update

* endianess fix

* Merge branch 'unstable' of https://github.com/sigp/lighthouse into migrate-to-alloy-primitives

* fmt

* fix merge

* fix test

* fixed_bytes crate

* minor fixes

* convert u256 to i64

* panic free mixin to_low_u64_le

* from_str_radix

* computbe_subnet api and ensuring we use big-endian

* Merge branch 'unstable' of https://github.com/sigp/lighthouse into migrate-to-alloy-primitives

* fix test

* Simplify subnet_id test

* Simplify some more tests

* Add tests to fixed_bytes crate

* Merge branch 'unstable' into migrate-to-alloy-primitives
  • Loading branch information
eserilev authored Sep 2, 2024
1 parent 002ca2c commit 99e53b8
Show file tree
Hide file tree
Showing 152 changed files with 1,048 additions and 716 deletions.
674 changes: 384 additions & 290 deletions Cargo.lock

Large diffs are not rendered by default.

29 changes: 15 additions & 14 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ members = [
"database_manager",

"consensus/int_to_bytes",
"consensus/fixed_bytes",
"consensus/fork_choice",
"consensus/proto_array",
"consensus/safe_arith",
Expand Down Expand Up @@ -94,9 +95,9 @@ resolver = "2"
edition = "2021"

[workspace.dependencies]
alloy-primitives = "0.7.7"
alloy-primitives = "0.8"
alloy-rlp = "0.3.4"
alloy-consensus = "0.2.0"
alloy-consensus = "0.3.0"
anyhow = "1"
arbitrary = { version = "1", features = ["derive"] }
async-channel = "1.9.0"
Expand All @@ -118,11 +119,10 @@ rust_eth_kzg = "0.5.1"
discv5 = { version = "0.4.1", features = ["libp2p"] }
env_logger = "0.9"
error-chain = "0.12"
ethereum-types = "0.14"
ethereum_hashing = "0.6.0"
ethereum_serde_utils = "0.5.2"
ethereum_ssz = "0.5"
ethereum_ssz_derive = "0.5"
ethereum_hashing = "0.7.0"
ethereum_serde_utils = "0.7"
ethereum_ssz = "0.7"
ethereum_ssz_derive = "0.7"
ethers-core = "1"
ethers-providers = { version = "1", default-features = false }
exit-future = "0.2"
Expand All @@ -137,7 +137,7 @@ libsecp256k1 = "0.7"
log = "0.4"
lru = "0.12"
maplit = "1"
milhouse = "0.1"
milhouse = "0.3"
num_cpus = "1"
parking_lot = "0.12"
paste = "1"
Expand All @@ -163,7 +163,7 @@ slog-term = "2"
sloggers = { version = "2", features = ["json"] }
smallvec = { version = "1.11.2", features = ["arbitrary"] }
snap = "1"
ssz_types = "0.6"
ssz_types = "0.8"
strum = { version = "0.24", features = ["derive"] }
superstruct = "0.8"
syn = "1"
Expand All @@ -177,8 +177,8 @@ tracing-appender = "0.2"
tracing-core = "0.1"
tracing-log = "0.2"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
tree_hash = "0.6"
tree_hash_derive = "0.6"
tree_hash = "0.8"
tree_hash_derive = "0.8"
url = "2"
uuid = { version = "0.8", features = ["serde", "v4"] }
warp = { version = "0.3.7", default-features = false, features = ["tls"] }
Expand All @@ -205,6 +205,7 @@ eth2_keystore = { path = "crypto/eth2_keystore" }
eth2_network_config = { path = "common/eth2_network_config" }
eth2_wallet = { path = "crypto/eth2_wallet" }
execution_layer = { path = "beacon_node/execution_layer" }
fixed_bytes = { path = "consensus/fixed_bytes" }
filesystem = { path = "common/filesystem" }
fork_choice = { path = "consensus/fork_choice" }
genesis = { path = "beacon_node/genesis" }
Expand Down Expand Up @@ -240,11 +241,11 @@ validator_client = { path = "validator_client" }
validator_dir = { path = "common/validator_dir" }
warp_utils = { path = "common/warp_utils" }

[patch.crates-io]
quick-protobuf = { git = "https://github.com/sigp/quick-protobuf.git", rev = "681f413312404ab6e51f0b46f39b0075c6f4ebfd" }

[profile.maxperf]
inherits = "release"
lto = "fat"
codegen-units = 1
incremental = false

[patch.crates-io]
quick-protobuf = { git = "https://github.com/sigp/quick-protobuf.git", rev = "681f413312404ab6e51f0b46f39b0075c6f4ebfd" }
1 change: 1 addition & 0 deletions beacon_node/beacon_chain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ serde_json = { workspace = true }
criterion = { workspace = true }

[dependencies]
alloy-primitives = { workspace = true }
bitvec = { workspace = true }
bls = { workspace = true }
derivative = { workspace = true }
Expand Down
4 changes: 2 additions & 2 deletions beacon_node/beacon_chain/src/attester_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ use types::{
beacon_state::{
compute_committee_index_in_epoch, compute_committee_range_in_epoch, epoch_committee_count,
},
BeaconState, BeaconStateError, ChainSpec, Checkpoint, Epoch, EthSpec, Hash256, RelativeEpoch,
Slot,
BeaconState, BeaconStateError, ChainSpec, Checkpoint, Epoch, EthSpec, FixedBytesExtended,
Hash256, RelativeEpoch, Slot,
};

type JustifiedCheckpoint = Checkpoint;
Expand Down
4 changes: 3 additions & 1 deletion beacon_node/beacon_chain/src/beacon_block_streamer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,9 @@ mod tests {
use std::sync::LazyLock;
use std::time::Duration;
use tokio::sync::mpsc;
use types::{ChainSpec, Epoch, EthSpec, Hash256, Keypair, MinimalEthSpec, Slot};
use types::{
ChainSpec, Epoch, EthSpec, FixedBytesExtended, Hash256, Keypair, MinimalEthSpec, Slot,
};

const VALIDATOR_COUNT: usize = 48;

Expand Down
16 changes: 8 additions & 8 deletions beacon_node/beacon_chain/src/beacon_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,10 @@ pub type ForkChoiceError = fork_choice::Error<crate::ForkChoiceStoreError>;
type HashBlockTuple<E> = (Hash256, RpcBlock<E>);

// These keys are all zero because they get stored in different columns, see `DBColumn` type.
pub const BEACON_CHAIN_DB_KEY: Hash256 = Hash256::zero();
pub const OP_POOL_DB_KEY: Hash256 = Hash256::zero();
pub const ETH1_CACHE_DB_KEY: Hash256 = Hash256::zero();
pub const FORK_CHOICE_DB_KEY: Hash256 = Hash256::zero();
pub const BEACON_CHAIN_DB_KEY: Hash256 = Hash256::ZERO;
pub const OP_POOL_DB_KEY: Hash256 = Hash256::ZERO;
pub const ETH1_CACHE_DB_KEY: Hash256 = Hash256::ZERO;
pub const FORK_CHOICE_DB_KEY: Hash256 = Hash256::ZERO;

/// Defines how old a block can be before it's no longer a candidate for the early attester cache.
const EARLY_ATTESTER_CACHE_HISTORIC_SLOTS: u64 = 4;
Expand Down Expand Up @@ -528,7 +528,7 @@ impl<E: EthSpec> BeaconBlockResponseWrapper<E> {
}

pub fn consensus_block_value_wei(&self) -> Uint256 {
Uint256::from(self.consensus_block_value_gwei()) * 1_000_000_000
Uint256::from(self.consensus_block_value_gwei()) * Uint256::from(1_000_000_000)
}

pub fn is_blinded(&self) -> bool {
Expand Down Expand Up @@ -3446,7 +3446,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
{
let mut slashable_cache = self.observed_slashable.write();
for header in blobs
.into_iter()
.iter()
.filter_map(|b| b.as_ref().map(|b| b.signed_block_header.clone()))
.unique()
{
Expand Down Expand Up @@ -5425,7 +5425,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
},
}),
None,
Uint256::zero(),
Uint256::ZERO,
),
BeaconState::Altair(_) => (
BeaconBlock::Altair(BeaconBlockAltair {
Expand All @@ -5448,7 +5448,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
},
}),
None,
Uint256::zero(),
Uint256::ZERO,
),
BeaconState::Bellatrix(_) => {
let block_proposal_contents =
Expand Down
2 changes: 1 addition & 1 deletion beacon_node/beacon_chain/src/beacon_fork_choice_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use store::{Error as StoreError, HotColdDB, ItemStore};
use superstruct::superstruct;
use types::{
AbstractExecPayload, BeaconBlockRef, BeaconState, BeaconStateError, Checkpoint, Epoch, EthSpec,
Hash256, Slot,
FixedBytesExtended, Hash256, Slot,
};

#[derive(Debug)]
Expand Down
2 changes: 1 addition & 1 deletion beacon_node/beacon_chain/src/bellatrix_readiness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl MergeConfig {
/// Instantiate `self` from the values in a `ChainSpec`.
pub fn from_chainspec(spec: &ChainSpec) -> Self {
let mut params = MergeConfig::default();
if spec.terminal_total_difficulty != Uint256::max_value() {
if spec.terminal_total_difficulty != Uint256::MAX {
params.terminal_total_difficulty = Some(spec.terminal_total_difficulty);
}
if spec.terminal_block_hash != ExecutionBlockHash::zero() {
Expand Down
1 change: 1 addition & 0 deletions beacon_node/beacon_chain/src/block_times_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ impl BlockTimesCache {
#[cfg(test)]
mod test {
use super::*;
use types::FixedBytesExtended;

#[test]
fn observed_time_uses_minimum() {
Expand Down
6 changes: 3 additions & 3 deletions beacon_node/beacon_chain/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ use std::time::Duration;
use store::{Error as StoreError, HotColdDB, ItemStore, KeyValueStoreOp};
use task_executor::{ShutdownReason, TaskExecutor};
use types::{
BeaconBlock, BeaconState, BlobSidecarList, ChainSpec, Checkpoint, Epoch, EthSpec, Hash256,
Signature, SignedBeaconBlock, Slot,
BeaconBlock, BeaconState, BlobSidecarList, ChainSpec, Checkpoint, Epoch, EthSpec,
FixedBytesExtended, Hash256, Signature, SignedBeaconBlock, Slot,
};

/// An empty struct used to "witness" all the `BeaconChainTypes` traits. It has no user-facing
Expand Down Expand Up @@ -1292,7 +1292,7 @@ mod test {
}

for v in state.validators() {
let creds = v.withdrawal_credentials.as_bytes();
let creds = v.withdrawal_credentials.as_slice();
assert_eq!(
creds[0], spec.bls_withdrawal_prefix_byte,
"first byte of withdrawal creds should be bls prefix"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,6 @@ impl<E: EthSpec> PendingComponents<E> {
let num_blobs_expected = diet_executed_block.num_blobs_expected();
let Some(verified_blobs) = verified_blobs
.into_iter()
.cloned()
.map(|b| b.map(|b| b.to_blob()))
.take(num_blobs_expected)
.collect::<Option<Vec<_>>>()
Expand Down Expand Up @@ -1155,7 +1154,9 @@ mod pending_components_tests {
use rand::SeedableRng;
use state_processing::ConsensusContext;
use types::test_utils::TestRandom;
use types::{BeaconState, ForkName, MainnetEthSpec, SignedBeaconBlock, Slot};
use types::{
BeaconState, FixedBytesExtended, ForkName, MainnetEthSpec, SignedBeaconBlock, Slot,
};

type E = MainnetEthSpec;

Expand Down
2 changes: 1 addition & 1 deletion beacon_node/beacon_chain/src/eth1_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,7 @@ fn is_candidate_block(block: &Eth1Block, period_start: u64, spec: &ChainSpec) ->
#[cfg(test)]
mod test {
use super::*;
use types::{DepositData, MinimalEthSpec, Signature};
use types::{DepositData, FixedBytesExtended, MinimalEthSpec, Signature};

type E = MinimalEthSpec;

Expand Down
4 changes: 2 additions & 2 deletions beacon_node/beacon_chain/src/execution_payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ where
return Ok(BlockProposalContentsType::Full(
BlockProposalContents::Payload {
payload: FullPayload::default_at_fork(fork)?,
block_value: Uint256::zero(),
block_value: Uint256::ZERO,
},
));
}
Expand All @@ -523,7 +523,7 @@ where
return Ok(BlockProposalContentsType::Full(
BlockProposalContents::Payload {
payload: FullPayload::default_at_fork(fork)?,
block_value: Uint256::zero(),
block_value: Uint256::ZERO,
},
));
}
Expand Down
2 changes: 1 addition & 1 deletion beacon_node/beacon_chain/src/head_tracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ impl SszHeadTracker {
mod test {
use super::*;
use ssz::{Decode, Encode};
use types::{BeaconBlock, EthSpec, MainnetEthSpec};
use types::{BeaconBlock, EthSpec, FixedBytesExtended, MainnetEthSpec};

type E = MainnetEthSpec;

Expand Down
2 changes: 1 addition & 1 deletion beacon_node/beacon_chain/src/historical_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::iter;
use std::time::Duration;
use store::metadata::DataColumnInfo;
use store::{chunked_vector::BlockRoots, AnchorInfo, BlobInfo, ChunkWriter, KeyValueStore};
use types::{Hash256, Slot};
use types::{FixedBytesExtended, Hash256, Slot};

/// Use a longer timeout on the pubkey cache.
///
Expand Down
1 change: 1 addition & 0 deletions beacon_node/beacon_chain/src/metrics.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::observed_attesters::SlotSubcommitteeIndex;
use crate::types::consts::altair::SYNC_COMMITTEE_SUBNET_COUNT;
use crate::{BeaconChain, BeaconChainError, BeaconChainTypes};
use bls::FixedBytesExtended;
pub use lighthouse_metrics::*;
use slot_clock::SlotClock;
use std::sync::LazyLock;
Expand Down
4 changes: 2 additions & 2 deletions beacon_node/beacon_chain/src/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ use store::iter::RootsIterator;
use store::{Error, ItemStore, StoreItem, StoreOp};
pub use store::{HotColdDB, MemoryStore};
use types::{
BeaconState, BeaconStateError, BeaconStateHash, Checkpoint, Epoch, EthSpec, Hash256,
SignedBeaconBlockHash, Slot,
BeaconState, BeaconStateError, BeaconStateHash, Checkpoint, Epoch, EthSpec, FixedBytesExtended,
Hash256, SignedBeaconBlockHash, Slot,
};

/// Compact at least this frequently, finalization permitting (7 days).
Expand Down
5 changes: 3 additions & 2 deletions beacon_node/beacon_chain/src/naive_aggregation_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl TreeHash for AttestationKey {
// Combine the hash of the data with the hash of the index
let mut hasher = MerkleHasher::with_leaves(2);
hasher
.write(self.data_root.as_bytes())
.write(self.data_root.as_slice())
.expect("should write data hash");
hasher
.write(&index.to_le_bytes())
Expand Down Expand Up @@ -582,7 +582,8 @@ mod tests {
use tree_hash::TreeHash;
use types::{
test_utils::{generate_deterministic_keypair, test_random_instance},
Attestation, AttestationBase, AttestationElectra, Fork, Hash256, SyncCommitteeMessage,
Attestation, AttestationBase, AttestationElectra, FixedBytesExtended, Fork, Hash256,
SyncCommitteeMessage,
};

type E = types::MainnetEthSpec;
Expand Down
2 changes: 1 addition & 1 deletion beacon_node/beacon_chain/src/observed_aggregates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ where
#[cfg(not(debug_assertions))]
mod tests {
use super::*;
use types::{test_utils::test_random_instance, AttestationBase, Hash256};
use types::{test_utils::test_random_instance, AttestationBase, FixedBytesExtended, Hash256};

type E = types::MainnetEthSpec;

Expand Down
1 change: 1 addition & 0 deletions beacon_node/beacon_chain/src/observed_attesters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,7 @@ impl SlotSubcommitteeIndex {
#[cfg(test)]
mod tests {
use super::*;
use types::FixedBytesExtended;

type E = types::MainnetEthSpec;

Expand Down
2 changes: 1 addition & 1 deletion beacon_node/beacon_chain/src/otb_verification_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl OptimisticTransitionBlock {
store
.as_ref()
.hot_db
.key_delete(OTBColumn.into(), self.root.as_bytes())
.key_delete(OTBColumn.into(), self.root.as_slice())
}

fn is_canonical<T: BeaconChainTypes>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ pub fn downgrade_from_v21<T: BeaconChainTypes>(
message: format!("{e:?}"),
})?;

let db_key = get_key_for_col(DBColumn::PubkeyCache.into(), key.as_bytes());
let db_key = get_key_for_col(DBColumn::PubkeyCache.into(), key.as_slice());
ops.push(KeyValueStoreOp::PutKeyValue(
db_key,
pubkey_bytes.as_ssz_bytes(),
Expand Down
1 change: 1 addition & 0 deletions beacon_node/beacon_chain/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2627,6 +2627,7 @@ pub fn generate_rand_block_and_blobs<E: EthSpec>(
let inner = map_fork_name!(fork_name, BeaconBlock, <_>::random_for_test(rng));

let mut block = SignedBeaconBlock::from_block(inner, types::Signature::random_for_test(rng));

let mut blob_sidecars = vec![];

let bundle = match block {
Expand Down
2 changes: 1 addition & 1 deletion beacon_node/beacon_chain/src/validator_pubkey_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use ssz_derive::{Decode, Encode};
use std::collections::HashMap;
use std::marker::PhantomData;
use store::{DBColumn, Error as StoreError, StoreItem, StoreOp};
use types::{BeaconState, Hash256, PublicKey, PublicKeyBytes};
use types::{BeaconState, FixedBytesExtended, Hash256, PublicKey, PublicKeyBytes};

/// Provides a mapping of `validator_index -> validator_publickey`.
///
Expand Down
4 changes: 2 additions & 2 deletions beacon_node/beacon_chain/tests/attestation_verification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ use types::{
signed_aggregate_and_proof::SignedAggregateAndProofRefMut,
test_utils::generate_deterministic_keypair, Address, AggregateSignature, Attestation,
AttestationRef, AttestationRefMut, BeaconStateError, BitList, ChainSpec, Epoch, EthSpec,
ForkName, Hash256, Keypair, MainnetEthSpec, SecretKey, SelectionProof, SignedAggregateAndProof,
Slot, SubnetId, Unsigned,
FixedBytesExtended, ForkName, Hash256, Keypair, MainnetEthSpec, SecretKey, SelectionProof,
SignedAggregateAndProof, Slot, SubnetId, Unsigned,
};

pub type E = MainnetEthSpec;
Expand Down
4 changes: 2 additions & 2 deletions beacon_node/beacon_chain/tests/payload_invalidation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1280,7 +1280,7 @@ struct OptimisticTransitionSetup {
impl OptimisticTransitionSetup {
async fn new(num_blocks: usize, ttd: u64) -> Self {
let mut spec = E::default_spec();
spec.terminal_total_difficulty = ttd.into();
spec.terminal_total_difficulty = Uint256::from(ttd);
let mut rig = InvalidPayloadRig::new_with_spec(spec).enable_attestations();
rig.move_to_terminal_block();

Expand Down Expand Up @@ -1323,7 +1323,7 @@ async fn build_optimistic_chain(
// Build a brand-new testing harness. We will apply the blocks from the previous harness to
// this one.
let mut spec = E::default_spec();
spec.terminal_total_difficulty = rig_ttd.into();
spec.terminal_total_difficulty = Uint256::from(rig_ttd);
let rig = InvalidPayloadRig::new_with_spec(spec);

let spec = &rig.harness.chain.spec;
Expand Down
Loading

0 comments on commit 99e53b8

Please sign in to comment.