diff --git a/FUNDING.json b/FUNDING.json new file mode 100644 index 0000000000..5001999927 --- /dev/null +++ b/FUNDING.json @@ -0,0 +1,7 @@ +{ + "drips": { + "ethereum": { + "ownedBy": "0x25c4a76E7d118705e7Ea2e9b7d8C59930d8aCD3b" + } + } +} \ No newline at end of file diff --git a/beacon_node/beacon_chain/src/blob_verification.rs b/beacon_node/beacon_chain/src/blob_verification.rs index fdf8ee2b97..2b62a83194 100644 --- a/beacon_node/beacon_chain/src/blob_verification.rs +++ b/beacon_node/beacon_chain/src/blob_verification.rs @@ -10,7 +10,6 @@ use crate::block_verification::{ use crate::kzg_utils::{validate_blob, validate_blobs}; use crate::{metrics, BeaconChainError}; use kzg::{Error as KzgError, Kzg, KzgCommitment}; -use merkle_proof::MerkleTreeError; use slog::debug; use ssz_derive::{Decode, Encode}; use ssz_types::VariableList; @@ -128,13 +127,6 @@ pub enum GossipBlobError { /// The blob sidecar is invalid and the peer is faulty. KzgError(kzg::Error), - /// The kzg commitment inclusion proof failed. - /// - /// ## Peer scoring - /// - /// The blob sidecar is invalid - InclusionProof(MerkleTreeError), - /// The pubkey cache timed out. /// /// ## Peer scoring @@ -459,10 +451,7 @@ pub fn validate_blob_sidecar_for_gossip( // Verify the inclusion proof in the sidecar let _timer = metrics::start_timer(&metrics::BLOB_SIDECAR_INCLUSION_PROOF_VERIFICATION); - if !blob_sidecar - .verify_blob_sidecar_inclusion_proof() - .map_err(GossipBlobError::InclusionProof)? - { + if !blob_sidecar.verify_blob_sidecar_inclusion_proof() { return Err(GossipBlobError::InvalidInclusionProof); } drop(_timer); diff --git a/beacon_node/client/src/lib.rs b/beacon_node/client/src/lib.rs index fd92c28255..e6042103e1 100644 --- a/beacon_node/client/src/lib.rs +++ b/beacon_node/client/src/lib.rs @@ -1,5 +1,3 @@ -extern crate slog; - mod compute_light_client_updates; pub mod config; mod metrics; diff --git a/beacon_node/eth1/src/lib.rs b/beacon_node/eth1/src/lib.rs index 3b288de490..9d6cb7c847 100644 --- a/beacon_node/eth1/src/lib.rs +++ b/beacon_node/eth1/src/lib.rs @@ -1,6 +1,3 @@ -#[macro_use] -extern crate lazy_static; - mod block_cache; mod deposit_cache; mod inner; diff --git a/beacon_node/eth1/src/metrics.rs b/beacon_node/eth1/src/metrics.rs index 5441b40d7e..ad94d42ecb 100644 --- a/beacon_node/eth1/src/metrics.rs +++ b/beacon_node/eth1/src/metrics.rs @@ -1,5 +1,7 @@ pub use lighthouse_metrics::*; +use lazy_static::lazy_static; + lazy_static! { /* * Eth1 blocks diff --git a/beacon_node/execution_layer/src/lib.rs b/beacon_node/execution_layer/src/lib.rs index 126aba15f5..f78803a909 100644 --- a/beacon_node/execution_layer/src/lib.rs +++ b/beacon_node/execution_layer/src/lib.rs @@ -1985,39 +1985,10 @@ impl ExecutionLayer { excess_blob_gas: deneb_block.excess_blob_gas, }) } - ExecutionBlockWithTransactions::Electra(electra_block) => { - let withdrawals = VariableList::new( - electra_block - .withdrawals - .into_iter() - .map(Into::into) - .collect(), - ) - .map_err(ApiError::DeserializeWithdrawals)?; - ExecutionPayload::Electra(ExecutionPayloadElectra { - parent_hash: electra_block.parent_hash, - fee_recipient: electra_block.fee_recipient, - state_root: electra_block.state_root, - receipts_root: electra_block.receipts_root, - logs_bloom: electra_block.logs_bloom, - prev_randao: electra_block.prev_randao, - block_number: electra_block.block_number, - gas_limit: electra_block.gas_limit, - gas_used: electra_block.gas_used, - timestamp: electra_block.timestamp, - extra_data: electra_block.extra_data, - base_fee_per_gas: electra_block.base_fee_per_gas, - block_hash: electra_block.block_hash, - transactions: convert_transactions(electra_block.transactions)?, - withdrawals, - blob_gas_used: electra_block.blob_gas_used, - excess_blob_gas: electra_block.excess_blob_gas, - // TODO(electra) - // deposit_receipts: electra_block.deposit_receipts, - // withdrawal_requests: electra_block.withdrawal_requests, - deposit_receipts: <_>::default(), - withdrawal_requests: <_>::default(), - }) + ExecutionBlockWithTransactions::Electra(_) => { + return Err(ApiError::UnsupportedForkVariant(format!( + "legacy payload construction for {fork} is not implemented" + ))); } }; diff --git a/beacon_node/execution_layer/src/versioned_hashes.rs b/beacon_node/execution_layer/src/versioned_hashes.rs index 37bd35646d..9bf87596b4 100644 --- a/beacon_node/execution_layer/src/versioned_hashes.rs +++ b/beacon_node/execution_layer/src/versioned_hashes.rs @@ -1,5 +1,3 @@ -extern crate alloy_consensus; -extern crate alloy_rlp; use alloy_consensus::TxEnvelope; use alloy_rlp::Decodable; use types::{EthSpec, ExecutionPayloadRef, Hash256, Unsigned, VersionedHash}; diff --git a/beacon_node/lighthouse_network/src/lib.rs b/beacon_node/lighthouse_network/src/lib.rs index 264795844a..0b827164fc 100644 --- a/beacon_node/lighthouse_network/src/lib.rs +++ b/beacon_node/lighthouse_network/src/lib.rs @@ -2,9 +2,6 @@ /// all required libp2p functionality. /// /// This crate builds and manages the libp2p services required by the beacon node. -#[macro_use] -extern crate lazy_static; - mod config; pub mod service; diff --git a/beacon_node/lighthouse_network/src/metrics.rs b/beacon_node/lighthouse_network/src/metrics.rs index fc441f2533..8efed44eb4 100644 --- a/beacon_node/lighthouse_network/src/metrics.rs +++ b/beacon_node/lighthouse_network/src/metrics.rs @@ -1,5 +1,7 @@ pub use lighthouse_metrics::*; +use lazy_static::lazy_static; + lazy_static! { pub static ref NAT_OPEN: Result = try_create_int_gauge_vec( "nat_open", diff --git a/beacon_node/lighthouse_network/src/peer_manager/peerdb/score.rs b/beacon_node/lighthouse_network/src/peer_manager/peerdb/score.rs index ba9bd31472..8187dc4ba4 100644 --- a/beacon_node/lighthouse_network/src/peer_manager/peerdb/score.rs +++ b/beacon_node/lighthouse_network/src/peer_manager/peerdb/score.rs @@ -6,6 +6,7 @@ //! //! The scoring algorithms are currently experimental. use crate::service::gossipsub_scoring_parameters::GREYLIST_THRESHOLD as GOSSIPSUB_GREYLIST_THRESHOLD; +use lazy_static::lazy_static; use serde::Serialize; use std::time::Instant; use strum::AsRefStr; diff --git a/beacon_node/lighthouse_network/src/rpc/protocol.rs b/beacon_node/lighthouse_network/src/rpc/protocol.rs index 12a7f09338..bfaaef9b3b 100644 --- a/beacon_node/lighthouse_network/src/rpc/protocol.rs +++ b/beacon_node/lighthouse_network/src/rpc/protocol.rs @@ -3,6 +3,7 @@ use crate::rpc::codec::{base::BaseInboundCodec, ssz_snappy::SSZSnappyInboundCode use futures::future::BoxFuture; use futures::prelude::{AsyncRead, AsyncWrite}; use futures::{FutureExt, StreamExt}; +use lazy_static::lazy_static; use libp2p::core::{InboundUpgrade, UpgradeInfo}; use ssz::Encode; use ssz_types::VariableList; diff --git a/beacon_node/network/src/lib.rs b/beacon_node/network/src/lib.rs index da64368b16..1149e6e6e3 100644 --- a/beacon_node/network/src/lib.rs +++ b/beacon_node/network/src/lib.rs @@ -1,6 +1,3 @@ -#[macro_use] -extern crate lazy_static; - /// This crate provides the network server for Lighthouse. pub mod error; #[allow(clippy::mutable_key_type)] // PeerId in hashmaps are no longer permitted by clippy diff --git a/beacon_node/network/src/metrics.rs b/beacon_node/network/src/metrics.rs index bf4cbd09ab..32e57da8ae 100644 --- a/beacon_node/network/src/metrics.rs +++ b/beacon_node/network/src/metrics.rs @@ -5,6 +5,7 @@ use beacon_chain::{ sync_committee_verification::Error as SyncCommitteeError, }; use fnv::FnvHashMap; +use lazy_static::lazy_static; pub use lighthouse_metrics::*; use lighthouse_network::{ peer_manager::peerdb::client::ClientKind, types::GossipKind, GossipTopic, Gossipsub, diff --git a/beacon_node/network/src/network_beacon_processor/gossip_methods.rs b/beacon_node/network/src/network_beacon_processor/gossip_methods.rs index d78091b313..34eb8b26b1 100644 --- a/beacon_node/network/src/network_beacon_processor/gossip_methods.rs +++ b/beacon_node/network/src/network_beacon_processor/gossip_methods.rs @@ -695,7 +695,6 @@ impl NetworkBeaconProcessor { | GossipBlobError::InvalidSubnet { .. } | GossipBlobError::InvalidInclusionProof | GossipBlobError::KzgError(_) - | GossipBlobError::InclusionProof(_) | GossipBlobError::NotFinalizedDescendant { .. } => { warn!( self.log, diff --git a/beacon_node/network/src/sync/network_context/requests.rs b/beacon_node/network/src/sync/network_context/requests.rs index cd73b4beba..6e4683701b 100644 --- a/beacon_node/network/src/sync/network_context/requests.rs +++ b/beacon_node/network/src/sync/network_context/requests.rs @@ -120,7 +120,7 @@ impl ActiveBlobsByRootRequest { if self.request.block_root != block_root { return Err(LookupVerifyError::UnrequestedBlockRoot(block_root)); } - if !blob.verify_blob_sidecar_inclusion_proof().unwrap_or(false) { + if !blob.verify_blob_sidecar_inclusion_proof() { return Err(LookupVerifyError::InvalidInclusionProof); } if !self.request.indices.contains(&blob.index) { diff --git a/beacon_node/src/lib.rs b/beacon_node/src/lib.rs index 4ca084c316..ab400d2e73 100644 --- a/beacon_node/src/lib.rs +++ b/beacon_node/src/lib.rs @@ -1,5 +1,3 @@ -extern crate clap; - mod cli; mod config; diff --git a/beacon_node/store/src/lib.rs b/beacon_node/store/src/lib.rs index 66032d89c5..0247bea554 100644 --- a/beacon_node/store/src/lib.rs +++ b/beacon_node/store/src/lib.rs @@ -7,9 +7,6 @@ //! //! Provides a simple API for storing/retrieving all types that sometimes needs type-hints. See //! tests for implementation examples. -#[macro_use] -extern crate lazy_static; - mod chunk_writer; pub mod chunked_iter; pub mod chunked_vector; diff --git a/beacon_node/store/src/metrics.rs b/beacon_node/store/src/metrics.rs index 2d901fdd93..1e614036ea 100644 --- a/beacon_node/store/src/metrics.rs +++ b/beacon_node/store/src/metrics.rs @@ -1,6 +1,7 @@ pub use lighthouse_metrics::{set_gauge, try_create_int_gauge, *}; use directory::size_of_dir; +use lazy_static::lazy_static; use std::path::Path; lazy_static! { diff --git a/beacon_node/store/src/partial_beacon_state.rs b/beacon_node/store/src/partial_beacon_state.rs index e56d0580ac..5e6054bc06 100644 --- a/beacon_node/store/src/partial_beacon_state.rs +++ b/beacon_node/store/src/partial_beacon_state.rs @@ -133,7 +133,6 @@ where #[superstruct(only(Electra))] pub earliest_consolidation_epoch: Epoch, - // TODO(electra) should these be optional? #[superstruct(only(Electra))] pub pending_balance_deposits: List, #[superstruct(only(Electra))] diff --git a/common/compare_fields_derive/src/lib.rs b/common/compare_fields_derive/src/lib.rs index 01c5a8f6ef..1a89ccf4fd 100644 --- a/common/compare_fields_derive/src/lib.rs +++ b/common/compare_fields_derive/src/lib.rs @@ -1,5 +1,3 @@ -extern crate proc_macro; - use proc_macro::TokenStream; use quote::quote; use syn::{parse_macro_input, DeriveInput}; diff --git a/common/eth2_interop_keypairs/src/lib.rs b/common/eth2_interop_keypairs/src/lib.rs index 3031e1c4dc..34c3d6f87c 100644 --- a/common/eth2_interop_keypairs/src/lib.rs +++ b/common/eth2_interop_keypairs/src/lib.rs @@ -16,11 +16,9 @@ //! //! This implementation passes the [reference implementation //! tests](https://github.com/ethereum/eth2.0-pm/blob/6e41fcf383ebeb5125938850d8e9b4e9888389b4/interop/mocked_start/keygen_test_vector.yaml). -#[macro_use] -extern crate lazy_static; - use bls::{Keypair, PublicKey, SecretKey}; use ethereum_hashing::hash; +use lazy_static::lazy_static; use num_bigint::BigUint; use serde::{Deserialize, Serialize}; use std::fs::File; diff --git a/common/lighthouse_metrics/src/lib.rs b/common/lighthouse_metrics/src/lib.rs index 5d25bb313f..4a76184b8a 100644 --- a/common/lighthouse_metrics/src/lib.rs +++ b/common/lighthouse_metrics/src/lib.rs @@ -20,8 +20,7 @@ //! ## Example //! //! ```rust -//! #[macro_use] -//! extern crate lazy_static; +//! use lazy_static::lazy_static; //! use lighthouse_metrics::*; //! //! // These metrics are "magically" linked to the global registry defined in `lighthouse_metrics`. diff --git a/common/logging/src/lib.rs b/common/logging/src/lib.rs index b0e1da00e9..50d04fc088 100644 --- a/common/logging/src/lib.rs +++ b/common/logging/src/lib.rs @@ -1,6 +1,4 @@ -#[macro_use] -extern crate lazy_static; - +use lazy_static::lazy_static; use lighthouse_metrics::{ inc_counter, try_create_int_counter, IntCounter, Result as MetricsResult, }; diff --git a/common/logging/src/tracing_metrics_layer.rs b/common/logging/src/tracing_metrics_layer.rs index 08c323ee89..b9dde584b4 100644 --- a/common/logging/src/tracing_metrics_layer.rs +++ b/common/logging/src/tracing_metrics_layer.rs @@ -1,5 +1,6 @@ //! Exposes [`MetricsLayer`]: A tracing layer that registers metrics of logging events. +use lazy_static::lazy_static; use lighthouse_metrics as metrics; use tracing_log::NormalizeEvent; diff --git a/common/slot_clock/src/lib.rs b/common/slot_clock/src/lib.rs index 4f54b2ee76..a742e29457 100644 --- a/common/slot_clock/src/lib.rs +++ b/common/slot_clock/src/lib.rs @@ -1,6 +1,3 @@ -#[macro_use] -extern crate lazy_static; - mod manual_slot_clock; mod metrics; mod system_time_slot_clock; diff --git a/common/slot_clock/src/metrics.rs b/common/slot_clock/src/metrics.rs index 23a793b203..ae3a9b599f 100644 --- a/common/slot_clock/src/metrics.rs +++ b/common/slot_clock/src/metrics.rs @@ -1,4 +1,5 @@ use crate::SlotClock; +use lazy_static::lazy_static; pub use lighthouse_metrics::*; use types::{EthSpec, Slot}; diff --git a/common/test_random_derive/src/lib.rs b/common/test_random_derive/src/lib.rs index 648c20121a..8c4b1ef7c3 100644 --- a/common/test_random_derive/src/lib.rs +++ b/common/test_random_derive/src/lib.rs @@ -1,6 +1,4 @@ -extern crate proc_macro; - -use crate::proc_macro::TokenStream; +use proc_macro::TokenStream; use quote::quote; use syn::{parse_macro_input, DeriveInput}; diff --git a/consensus/types/src/beacon_state/committee_cache/tests.rs b/consensus/types/src/beacon_state/committee_cache/tests.rs index a227476569..4dc06feab3 100644 --- a/consensus/types/src/beacon_state/committee_cache/tests.rs +++ b/consensus/types/src/beacon_state/committee_cache/tests.rs @@ -2,6 +2,7 @@ use crate::test_utils::*; use beacon_chain::test_utils::{BeaconChainHarness, EphemeralHarnessType}; use beacon_chain::types::*; +use lazy_static::lazy_static; use swap_or_not_shuffle::shuffle_list; pub const VALIDATOR_COUNT: usize = 16; diff --git a/consensus/types/src/beacon_state/tests.rs b/consensus/types/src/beacon_state/tests.rs index 38a76e44c5..16c7ff152f 100644 --- a/consensus/types/src/beacon_state/tests.rs +++ b/consensus/types/src/beacon_state/tests.rs @@ -6,6 +6,7 @@ use beacon_chain::types::{ ChainSpec, Domain, Epoch, EthSpec, Hash256, Keypair, MainnetEthSpec, MinimalEthSpec, RelativeEpoch, Slot, Vector, }; +use lazy_static::lazy_static; use ssz::Encode; use std::ops::Mul; use swap_or_not_shuffle::compute_shuffled_index; diff --git a/consensus/types/src/blob_sidecar.rs b/consensus/types/src/blob_sidecar.rs index e54bc2f4f9..50530543a5 100644 --- a/consensus/types/src/blob_sidecar.rs +++ b/consensus/types/src/blob_sidecar.rs @@ -12,7 +12,7 @@ use kzg::{ }; use merkle_proof::{merkle_root_from_branch, verify_merkle_proof, MerkleTreeError}; use rand::Rng; -use safe_arith::{ArithError, SafeArith}; +use safe_arith::ArithError; use serde::{Deserialize, Serialize}; use ssz::Encode; use ssz_derive::{Decode, Encode}; @@ -190,33 +190,30 @@ impl BlobSidecar { } /// Verifies the kzg commitment inclusion merkle proof. - pub fn verify_blob_sidecar_inclusion_proof(&self) -> Result { - // Depth of the subtree rooted at `blob_kzg_commitments` in the `BeaconBlockBody` - // is equal to depth of the ssz List max size + 1 for the length mixin - let kzg_commitments_tree_depth = (E::max_blob_commitments_per_block() - .next_power_of_two() - .ilog2() - .safe_add(1))? as usize; + pub fn verify_blob_sidecar_inclusion_proof(&self) -> bool { + let kzg_commitments_tree_depth = E::kzg_commitments_tree_depth(); + + // EthSpec asserts that kzg_commitments_tree_depth is less than KzgCommitmentInclusionProofDepth + let (kzg_commitment_subtree_proof, kzg_commitments_proof) = self + .kzg_commitment_inclusion_proof + .split_at(kzg_commitments_tree_depth); + // Compute the `tree_hash_root` of the `blob_kzg_commitments` subtree using the // inclusion proof branches let blob_kzg_commitments_root = merkle_root_from_branch( self.kzg_commitment.tree_hash_root(), - self.kzg_commitment_inclusion_proof - .get(0..kzg_commitments_tree_depth) - .ok_or(MerkleTreeError::PleaseNotifyTheDevs)?, + kzg_commitment_subtree_proof, kzg_commitments_tree_depth, self.index as usize, ); // The remaining inclusion proof branches are for the top level `BeaconBlockBody` tree - Ok(verify_merkle_proof( + verify_merkle_proof( blob_kzg_commitments_root, - self.kzg_commitment_inclusion_proof - .get(kzg_commitments_tree_depth..E::kzg_proof_inclusion_proof_depth()) - .ok_or(MerkleTreeError::PleaseNotifyTheDevs)?, - E::kzg_proof_inclusion_proof_depth().safe_sub(kzg_commitments_tree_depth)?, + kzg_commitments_proof, + E::block_body_tree_depth(), BLOB_KZG_COMMITMENTS_INDEX, self.signed_block_header.message.body_root, - )) + ) } pub fn random_valid(rng: &mut R, kzg: &Kzg) -> Result { diff --git a/consensus/types/src/eth_spec.rs b/consensus/types/src/eth_spec.rs index 14949e6753..cdbab2d540 100644 --- a/consensus/types/src/eth_spec.rs +++ b/consensus/types/src/eth_spec.rs @@ -294,6 +294,22 @@ pub trait EthSpec: Self::KzgCommitmentInclusionProofDepth::to_usize() } + fn kzg_commitments_tree_depth() -> usize { + // Depth of the subtree rooted at `blob_kzg_commitments` in the `BeaconBlockBody` + // is equal to depth of the ssz List max size + 1 for the length mixin + Self::max_blob_commitments_per_block() + .next_power_of_two() + .ilog2() + .safe_add(1) + .expect("The log of max_blob_commitments_per_block can not overflow") as usize + } + + fn block_body_tree_depth() -> usize { + Self::kzg_proof_inclusion_proof_depth() + .safe_sub(Self::kzg_commitments_tree_depth()) + .expect("Preset values are not configurable and never result in non-positive block body depth") + } + /// Returns the `PENDING_BALANCE_DEPOSITS_LIMIT` constant for this specification. fn pending_balance_deposits_limit() -> usize { Self::PendingBalanceDepositsLimit::to_usize() @@ -525,3 +541,26 @@ impl EthSpec for GnosisEthSpec { EthSpecId::Gnosis } } + +#[cfg(test)] +mod test { + use crate::{EthSpec, GnosisEthSpec, MainnetEthSpec, MinimalEthSpec}; + + fn assert_valid_spec() { + E::kzg_commitments_tree_depth(); + E::block_body_tree_depth(); + } + + #[test] + fn mainnet_spec() { + assert_valid_spec::(); + } + #[test] + fn minimal_spec() { + assert_valid_spec::(); + } + #[test] + fn gnosis_spec() { + assert_valid_spec::(); + } +} diff --git a/consensus/types/src/execution_payload_header.rs b/consensus/types/src/execution_payload_header.rs index 2d7bc95071..a3df69652c 100644 --- a/consensus/types/src/execution_payload_header.rs +++ b/consensus/types/src/execution_payload_header.rs @@ -117,7 +117,6 @@ impl ExecutionPayloadHeader { #[allow(clippy::arithmetic_side_effects)] pub fn ssz_max_var_len_for_fork(fork_name: ForkName) -> usize { // Matching here in case variable fields are added in future forks. - // TODO(electra): review electra changes match fork_name { ForkName::Base | ForkName::Altair diff --git a/consensus/types/src/lib.rs b/consensus/types/src/lib.rs index 716c831478..75456446bf 100644 --- a/consensus/types/src/lib.rs +++ b/consensus/types/src/lib.rs @@ -9,8 +9,6 @@ ) )] -#[macro_use] -extern crate lazy_static; #[macro_use] pub mod test_utils; diff --git a/consensus/types/src/subnet_id.rs b/consensus/types/src/subnet_id.rs index b5b05e710b..25018096ae 100644 --- a/consensus/types/src/subnet_id.rs +++ b/consensus/types/src/subnet_id.rs @@ -1,5 +1,6 @@ //! Identifies each shard by an integer identifier. use crate::{AttestationRef, ChainSpec, CommitteeIndex, Epoch, EthSpec, Slot}; +use lazy_static::lazy_static; use safe_arith::{ArithError, SafeArith}; use serde::{Deserialize, Serialize}; use std::ops::{Deref, DerefMut}; diff --git a/consensus/types/src/sync_subnet_id.rs b/consensus/types/src/sync_subnet_id.rs index dd0807f21c..00fb910bda 100644 --- a/consensus/types/src/sync_subnet_id.rs +++ b/consensus/types/src/sync_subnet_id.rs @@ -1,6 +1,7 @@ //! Identifies each sync committee subnet by an integer identifier. use crate::consts::altair::SYNC_COMMITTEE_SUBNET_COUNT; use crate::EthSpec; +use lazy_static::lazy_static; use safe_arith::{ArithError, SafeArith}; use serde::{Deserialize, Serialize}; use ssz_types::typenum::Unsigned; diff --git a/consensus/types/src/test_utils/generate_random_block_and_blobs.rs b/consensus/types/src/test_utils/generate_random_block_and_blobs.rs new file mode 100644 index 0000000000..ab7ded0409 --- /dev/null +++ b/consensus/types/src/test_utils/generate_random_block_and_blobs.rs @@ -0,0 +1,96 @@ +use rand::Rng; + +use kzg::{KzgCommitment, KzgProof}; + +use crate::beacon_block_body::KzgCommitments; +use crate::*; + +use super::*; + +type BlobsBundle = (KzgCommitments, KzgProofs, BlobsList); + +pub fn generate_rand_block_and_blobs( + fork_name: ForkName, + num_blobs: usize, + rng: &mut impl Rng, +) -> (SignedBeaconBlock>, Vec>) { + let inner = map_fork_name!(fork_name, BeaconBlock, <_>::random_for_test(rng)); + let mut block = SignedBeaconBlock::from_block(inner, Signature::random_for_test(rng)); + let mut blob_sidecars = vec![]; + + if block.fork_name_unchecked() < ForkName::Deneb { + return (block, blob_sidecars); + } + + let (commitments, proofs, blobs) = generate_blobs::(num_blobs).unwrap(); + *block + .message_mut() + .body_mut() + .blob_kzg_commitments_mut() + .expect("kzg commitment expected from Deneb") = commitments.clone(); + + for (index, ((blob, kzg_commitment), kzg_proof)) in blobs + .into_iter() + .zip(commitments.into_iter()) + .zip(proofs.into_iter()) + .enumerate() + { + blob_sidecars.push(BlobSidecar { + index: index as u64, + blob: blob.clone(), + kzg_commitment, + kzg_proof, + signed_block_header: block.signed_block_header(), + kzg_commitment_inclusion_proof: block + .message() + .body() + .kzg_commitment_merkle_proof(index) + .unwrap(), + }); + } + (block, blob_sidecars) +} + +pub fn generate_blobs(n_blobs: usize) -> Result, String> { + let (mut commitments, mut proofs, mut blobs) = BlobsBundle::::default(); + + for blob_index in 0..n_blobs { + blobs + .push(Blob::::default()) + .map_err(|_| format!("blobs are full, blob index: {:?}", blob_index))?; + commitments + .push(KzgCommitment::empty_for_testing()) + .map_err(|_| format!("blobs are full, blob index: {:?}", blob_index))?; + proofs + .push(KzgProof::empty()) + .map_err(|_| format!("blobs are full, blob index: {:?}", blob_index))?; + } + + Ok((commitments, proofs, blobs)) +} + +#[cfg(test)] +mod test { + use super::*; + use rand::thread_rng; + + #[test] + fn test_verify_blob_inclusion_proof() { + let (_block, blobs) = + generate_rand_block_and_blobs::(ForkName::Deneb, 6, &mut thread_rng()); + for blob in blobs { + assert!(blob.verify_blob_sidecar_inclusion_proof()); + } + } + + #[test] + fn test_verify_blob_inclusion_proof_invalid() { + let (_block, blobs) = + generate_rand_block_and_blobs::(ForkName::Deneb, 6, &mut thread_rng()); + + for mut blob in blobs { + blob.kzg_commitment_inclusion_proof = FixedVector::random_for_test(&mut thread_rng()); + assert!(!blob.verify_blob_sidecar_inclusion_proof()); + } + } +} diff --git a/consensus/types/src/test_utils/mod.rs b/consensus/types/src/test_utils/mod.rs index d172342ee6..9599bcd364 100644 --- a/consensus/types/src/test_utils/mod.rs +++ b/consensus/types/src/test_utils/mod.rs @@ -15,6 +15,8 @@ use tree_hash::TreeHash; #[macro_use] mod macros; mod generate_deterministic_keypairs; +#[cfg(test)] +mod generate_random_block_and_blobs; mod test_random; pub fn test_ssz_tree_hash_pair(v1: &T, v2: &U) diff --git a/lcli/src/block_root.rs b/lcli/src/block_root.rs index 0ee304c8a5..a90a4843d8 100644 --- a/lcli/src/block_root.rs +++ b/lcli/src/block_root.rs @@ -32,6 +32,7 @@ use clap_utils::{parse_optional, parse_required}; use environment::Environment; use eth2::{types::BlockId, BeaconNodeHttpClient, SensitiveUrl, Timeouts}; use eth2_network_config::Eth2NetworkConfig; +use log::info; use std::path::PathBuf; use std::time::{Duration, Instant}; use types::{EthSpec, FullPayload, SignedBeaconBlock}; diff --git a/lcli/src/main.rs b/lcli/src/main.rs index 911e9bdcac..85898b60ee 100644 --- a/lcli/src/main.rs +++ b/lcli/src/main.rs @@ -1,5 +1,3 @@ -#[macro_use] -extern crate log; mod block_root; mod check_deposit_data; mod generate_bootnode_enr; diff --git a/lcli/src/parse_ssz.rs b/lcli/src/parse_ssz.rs index 3aa77e5700..dd13f6847b 100644 --- a/lcli/src/parse_ssz.rs +++ b/lcli/src/parse_ssz.rs @@ -1,6 +1,7 @@ use clap::ArgMatches; use clap_utils::parse_required; use eth2_network_config::Eth2NetworkConfig; +use log::info; use serde::Serialize; use snap::raw::Decoder; use ssz::Decode; diff --git a/lcli/src/skip_slots.rs b/lcli/src/skip_slots.rs index a2173f10df..2ad79051ea 100644 --- a/lcli/src/skip_slots.rs +++ b/lcli/src/skip_slots.rs @@ -50,6 +50,7 @@ use clap_utils::{parse_optional, parse_required}; use environment::Environment; use eth2::{types::StateId, BeaconNodeHttpClient, SensitiveUrl, Timeouts}; use eth2_network_config::Eth2NetworkConfig; +use log::info; use ssz::Encode; use state_processing::state_advance::{complete_state_advance, partial_state_advance}; use state_processing::AllCaches; diff --git a/lcli/src/state_root.rs b/lcli/src/state_root.rs index 06293b79b3..17a947b2f0 100644 --- a/lcli/src/state_root.rs +++ b/lcli/src/state_root.rs @@ -4,6 +4,7 @@ use clap_utils::{parse_optional, parse_required}; use environment::Environment; use eth2::{types::StateId, BeaconNodeHttpClient, SensitiveUrl, Timeouts}; use eth2_network_config::Eth2NetworkConfig; +use log::info; use std::path::PathBuf; use std::time::{Duration, Instant}; use types::{BeaconState, EthSpec}; diff --git a/lcli/src/transition_blocks.rs b/lcli/src/transition_blocks.rs index 7097908677..62ae602187 100644 --- a/lcli/src/transition_blocks.rs +++ b/lcli/src/transition_blocks.rs @@ -72,6 +72,7 @@ use eth2::{ BeaconNodeHttpClient, SensitiveUrl, Timeouts, }; use eth2_network_config::Eth2NetworkConfig; +use log::{debug, info}; use ssz::Encode; use state_processing::state_advance::complete_state_advance; use state_processing::{ diff --git a/lighthouse/src/main.rs b/lighthouse/src/main.rs index 5743bedfd7..abee30737c 100644 --- a/lighthouse/src/main.rs +++ b/lighthouse/src/main.rs @@ -688,9 +688,15 @@ fn run( let executor = context.executor.clone(); let mut config = beacon_node::get_config::(matches, &context)?; config.logger_config = logger_config; - let shutdown_flag = matches.get_flag("immediate-shutdown"); // Dump configs if `dump-config` or `dump-chain-config` flags are set clap_utils::check_dump_configs::<_, E>(matches, &config, &context.eth2_config.spec)?; + + let shutdown_flag = matches.get_flag("immediate-shutdown"); + if shutdown_flag { + info!(log, "Beacon node immediate shutdown triggered."); + return Ok(()); + } + executor.clone().spawn( async move { if let Err(e) = ProductionBeaconNode::new(context.clone(), config).await { @@ -700,10 +706,6 @@ fn run( let _ = executor .shutdown_sender() .try_send(ShutdownReason::Failure("Failed to start beacon node")); - } else if shutdown_flag { - let _ = executor.shutdown_sender().try_send(ShutdownReason::Success( - "Beacon node immediate shutdown triggered.", - )); } }, "beacon_node", @@ -715,31 +717,31 @@ fn run( let executor = context.executor.clone(); let config = validator_client::Config::from_cli(matches, context.log()) .map_err(|e| format!("Unable to initialize validator config: {}", e))?; - let shutdown_flag = matches.get_flag("immediate-shutdown"); // Dump configs if `dump-config` or `dump-chain-config` flags are set clap_utils::check_dump_configs::<_, E>(matches, &config, &context.eth2_config.spec)?; - if !shutdown_flag { - executor.clone().spawn( - async move { - if let Err(e) = ProductionValidatorClient::new(context, config) - .and_then(|mut vc| async move { vc.start_service().await }) - .await - { - crit!(log, "Failed to start validator client"; "reason" => e); - // Ignore the error since it always occurs during normal operation when - // shutting down. - let _ = executor.shutdown_sender().try_send(ShutdownReason::Failure( - "Failed to start validator client", - )); - } - }, - "validator_client", - ); - } else { - let _ = executor.shutdown_sender().try_send(ShutdownReason::Success( - "Validator client immediate shutdown triggered.", - )); + + let shutdown_flag = matches.get_flag("immediate-shutdown"); + if shutdown_flag { + info!(log, "Validator client immediate shutdown triggered."); + return Ok(()); } + + executor.clone().spawn( + async move { + if let Err(e) = ProductionValidatorClient::new(context, config) + .and_then(|mut vc| async move { vc.start_service().await }) + .await + { + crit!(log, "Failed to start validator client"; "reason" => e); + // Ignore the error since it always occurs during normal operation when + // shutting down. + let _ = executor + .shutdown_sender() + .try_send(ShutdownReason::Failure("Failed to start validator client")); + } + }, + "validator_client", + ); } _ => { crit!(log, "No subcommand supplied. See --help ."); diff --git a/lighthouse/tests/beacon_node.rs b/lighthouse/tests/beacon_node.rs index 73badac913..caadf208e3 100644 --- a/lighthouse/tests/beacon_node.rs +++ b/lighthouse/tests/beacon_node.rs @@ -58,13 +58,22 @@ impl CommandLineTest { fn run_with_zero_port(&mut self) -> CompletedTest { // Required since Deneb was enabled on mainnet. - self.cmd.arg("--allow-insecure-genesis-sync"); - self.run_with_zero_port_and_no_genesis_sync() + self.set_allow_insecure_genesis_sync() + .run_with_zero_port_and_no_genesis_sync() } fn run_with_zero_port_and_no_genesis_sync(&mut self) -> CompletedTest { + self.set_zero_port().run() + } + + fn set_allow_insecure_genesis_sync(&mut self) -> &mut Self { + self.cmd.arg("--allow-insecure-genesis-sync"); + self + } + + fn set_zero_port(&mut self) -> &mut Self { self.cmd.arg("-z"); - self.run() + self } } @@ -101,9 +110,20 @@ fn staking_flag() { } #[test] -#[should_panic] fn allow_insecure_genesis_sync_default() { - CommandLineTest::new().run_with_zero_port_and_no_genesis_sync(); + CommandLineTest::new() + .run_with_zero_port_and_no_genesis_sync() + .with_config(|config| { + assert_eq!(config.allow_insecure_genesis_sync, false); + }); +} + +#[test] +#[should_panic] +fn insecure_genesis_sync_should_panic() { + CommandLineTest::new() + .set_zero_port() + .run_with_immediate_shutdown(false); } #[test] @@ -2252,7 +2272,9 @@ fn ensure_panic_on_failed_launch() { CommandLineTest::new() .flag("slasher", None) .flag("slasher-chunk-size", Some("10")) - .run_with_zero_port() + .set_allow_insecure_genesis_sync() + .set_zero_port() + .run_with_immediate_shutdown(false) .with_config(|config| { let slasher_config = config .slasher diff --git a/lighthouse/tests/exec.rs b/lighthouse/tests/exec.rs index 61e0677ca8..9d6453908c 100644 --- a/lighthouse/tests/exec.rs +++ b/lighthouse/tests/exec.rs @@ -21,12 +21,19 @@ pub trait CommandLineTestExec { self } + fn run(&mut self) -> CompletedTest { + self.run_with_immediate_shutdown(true) + } + /// Executes the `Command` returned by `Self::cmd_mut` with temporary data directory, dumps - /// the configuration and shuts down immediately. + /// the configuration and shuts down immediately if `immediate_shutdown` is set to true. /// /// Options `--datadir`, `--dump-config`, `--dump-chain-config` and `--immediate-shutdown` must /// not be set on the command. - fn run(&mut self) -> CompletedTest { + fn run_with_immediate_shutdown( + &mut self, + immediate_shutdown: bool, + ) -> CompletedTest { // Setup temp directory. let tmp_dir = TempDir::new().expect("Unable to create temporary directory"); let tmp_config_path: PathBuf = tmp_dir.path().join("config.json"); @@ -39,8 +46,11 @@ pub trait CommandLineTestExec { .arg(format!("--{}", "dump-config")) .arg(tmp_config_path.as_os_str()) .arg(format!("--{}", "dump-chain-config")) - .arg(tmp_chain_config_path.as_os_str()) - .arg("--immediate-shutdown"); + .arg(tmp_chain_config_path.as_os_str()); + + if immediate_shutdown { + cmd.arg("--immediate-shutdown"); + } // Run the command. let output = output_result(cmd); diff --git a/testing/simulator/src/main.rs b/testing/simulator/src/main.rs index 03ee902c77..a259ac1133 100644 --- a/testing/simulator/src/main.rs +++ b/testing/simulator/src/main.rs @@ -10,9 +10,6 @@ //! simulation uses `println` to communicate some info. It might be nice if the nodes logged to //! easy-to-find files and stdout only contained info from the simulation. //! - -extern crate clap; - mod basic_sim; mod checks; mod cli;