Merge remote-tracking branch 'origin/unstable' into tree-states

This commit is contained in:
Michael Sproul
2024-02-22 15:43:04 +11:00
190 changed files with 21898 additions and 2040 deletions

View File

@@ -176,6 +176,12 @@ impl<'a, T: EthSpec, Payload: AbstractExecPayload<T>> BeaconBlockBodyRef<'a, T,
}
}
}
/// Return `true` if this block body has a non-zero number of blobs.
pub fn has_blobs(self) -> bool {
self.blob_kzg_commitments()
.map_or(false, |blobs| !blobs.is_empty())
}
}
impl<'a, T: EthSpec, Payload: AbstractExecPayload<T>> BeaconBlockBodyRef<'a, T, Payload> {

View File

@@ -765,7 +765,7 @@ impl<T: EthSpec> BeaconState<T> {
if self.slot() <= decision_slot {
Ok(block_root)
} else {
self.get_block_root(decision_slot).map(|root| *root)
self.get_block_root(decision_slot).copied()
}
}
@@ -781,7 +781,7 @@ impl<T: EthSpec> BeaconState<T> {
if self.slot() == decision_slot {
Ok(block_root)
} else {
self.get_block_root(decision_slot).map(|root| *root)
self.get_block_root(decision_slot).copied()
}
}
@@ -807,7 +807,7 @@ impl<T: EthSpec> BeaconState<T> {
if self.slot() == decision_slot {
Ok(block_root)
} else {
self.get_block_root(decision_slot).map(|root| *root)
self.get_block_root(decision_slot).copied()
}
}

View File

@@ -1,4 +1,5 @@
use crate::application_domain::{ApplicationDomain, APPLICATION_DOMAIN_BUILDER};
use crate::blob_sidecar::BlobIdentifier;
use crate::*;
use int_to_bytes::int_to_bytes4;
use safe_arith::{ArithError, SafeArith};
@@ -189,6 +190,7 @@ pub struct ChainSpec {
pub attestation_subnet_count: u64,
pub attestation_subnet_extra_bits: u8,
pub attestation_subnet_prefix_bits: u8,
pub attestation_subnet_shuffling_prefix_bits: u8,
/*
* Networking Deneb
@@ -701,7 +703,7 @@ impl ChainSpec {
* Deneb hard fork params
*/
deneb_fork_version: [0x04, 0x00, 0x00, 0x00],
deneb_fork_epoch: None,
deneb_fork_epoch: Some(Epoch::new(269568)),
/*
* Network specific
@@ -723,6 +725,8 @@ impl ChainSpec {
message_domain_valid_snappy: default_message_domain_valid_snappy(),
attestation_subnet_extra_bits: default_attestation_subnet_extra_bits(),
attestation_subnet_prefix_bits: default_attestation_subnet_prefix_bits(),
attestation_subnet_shuffling_prefix_bits:
default_attestation_subnet_shuffling_prefix_bits(),
max_request_blocks: default_max_request_blocks(),
/*
@@ -827,7 +831,7 @@ impl ChainSpec {
max_committees_per_slot: 64,
target_committee_size: 128,
min_per_epoch_churn_limit: 4,
max_per_epoch_activation_churn_limit: 8,
max_per_epoch_activation_churn_limit: 2,
churn_limit_quotient: 4_096,
shuffle_round_count: 90,
min_genesis_active_validator_count: 4_096,
@@ -962,7 +966,7 @@ impl ChainSpec {
* Deneb hard fork params
*/
deneb_fork_version: [0x04, 0x00, 0x00, 0x64],
deneb_fork_epoch: None,
deneb_fork_epoch: Some(Epoch::new(889856)),
/*
* Network specific
@@ -976,7 +980,7 @@ impl ChainSpec {
target_aggregators_per_committee: 16,
epochs_per_subnet_subscription: default_epochs_per_subnet_subscription(),
gossip_max_size: default_gossip_max_size(),
min_epochs_for_block_requests: default_min_epochs_for_block_requests(),
min_epochs_for_block_requests: 33024,
max_chunk_size: default_max_chunk_size(),
ttfb_timeout: default_ttfb_timeout(),
resp_timeout: default_resp_timeout(),
@@ -984,6 +988,8 @@ impl ChainSpec {
message_domain_valid_snappy: default_message_domain_valid_snappy(),
attestation_subnet_extra_bits: default_attestation_subnet_extra_bits(),
attestation_subnet_prefix_bits: default_attestation_subnet_prefix_bits(),
attestation_subnet_shuffling_prefix_bits:
default_attestation_subnet_shuffling_prefix_bits(),
max_request_blocks: default_max_request_blocks(),
/*
@@ -991,7 +997,7 @@ impl ChainSpec {
*/
max_request_blocks_deneb: default_max_request_blocks_deneb(),
max_request_blob_sidecars: default_max_request_blob_sidecars(),
min_epochs_for_blob_sidecars_requests: default_min_epochs_for_blob_sidecars_requests(),
min_epochs_for_blob_sidecars_requests: 16384,
blob_sidecar_subnet_count: default_blob_sidecar_subnet_count(),
/*
@@ -1161,6 +1167,9 @@ pub struct Config {
#[serde(default = "default_attestation_subnet_prefix_bits")]
#[serde(with = "serde_utils::quoted_u8")]
attestation_subnet_prefix_bits: u8,
#[serde(default = "default_attestation_subnet_shuffling_prefix_bits")]
#[serde(with = "serde_utils::quoted_u8")]
attestation_subnet_shuffling_prefix_bits: u8,
#[serde(default = "default_max_request_blocks_deneb")]
#[serde(with = "serde_utils::quoted_u64")]
max_request_blocks_deneb: u64,
@@ -1258,6 +1267,10 @@ const fn default_attestation_subnet_prefix_bits() -> u8 {
6
}
const fn default_attestation_subnet_shuffling_prefix_bits() -> u8 {
3
}
const fn default_max_request_blocks() -> u64 {
1024
}
@@ -1302,8 +1315,13 @@ fn max_blocks_by_root_request_common(max_request_blocks: u64) -> usize {
fn max_blobs_by_root_request_common(max_request_blob_sidecars: u64) -> usize {
let max_request_blob_sidecars = max_request_blob_sidecars as usize;
RuntimeVariableList::<Hash256>::from_vec(
vec![Hash256::zero(); max_request_blob_sidecars],
let empty_blob_identifier = BlobIdentifier {
block_root: Hash256::zero(),
index: 0,
};
RuntimeVariableList::<BlobIdentifier>::from_vec(
vec![empty_blob_identifier; max_request_blob_sidecars],
max_request_blob_sidecars,
)
.as_ssz_bytes()
@@ -1436,6 +1454,7 @@ impl Config {
message_domain_valid_snappy: spec.message_domain_valid_snappy,
attestation_subnet_extra_bits: spec.attestation_subnet_extra_bits,
attestation_subnet_prefix_bits: spec.attestation_subnet_prefix_bits,
attestation_subnet_shuffling_prefix_bits: spec.attestation_subnet_shuffling_prefix_bits,
max_request_blocks_deneb: spec.max_request_blocks_deneb,
max_request_blob_sidecars: spec.max_request_blob_sidecars,
min_epochs_for_blob_sidecars_requests: spec.min_epochs_for_blob_sidecars_requests,
@@ -1496,6 +1515,7 @@ impl Config {
message_domain_valid_snappy,
attestation_subnet_extra_bits,
attestation_subnet_prefix_bits,
attestation_subnet_shuffling_prefix_bits,
max_request_blocks,
epochs_per_subnet_subscription,
attestation_propagation_slot_range,
@@ -1553,6 +1573,7 @@ impl Config {
message_domain_valid_snappy,
attestation_subnet_extra_bits,
attestation_subnet_prefix_bits,
attestation_subnet_shuffling_prefix_bits,
max_request_blocks,
epochs_per_subnet_subscription,
attestation_propagation_slot_range,
@@ -1839,6 +1860,7 @@ mod yaml_tests {
check_default!(message_domain_valid_snappy);
check_default!(attestation_subnet_extra_bits);
check_default!(attestation_subnet_prefix_bits);
check_default!(attestation_subnet_shuffling_prefix_bits);
assert_eq!(chain_spec.bellatrix_fork_epoch, None);
}

View File

@@ -81,7 +81,7 @@ impl<'a, N: Unsigned> CachedTreeHash<TreeHashCache> for HistoricalSummaryCache<'
pub fn leaf_iter(
values: &[HistoricalSummary],
) -> impl Iterator<Item = [u8; BYTES_PER_CHUNK]> + ExactSizeIterator + '_ {
) -> impl ExactSizeIterator<Item = [u8; BYTES_PER_CHUNK]> + '_ {
values
.iter()
.map(|value| value.tree_hash_root())

View File

@@ -10,7 +10,7 @@ use ssz_types::FixedVector;
use std::sync::Arc;
use test_random_derive::TestRandom;
/// A LightClientBootstrap is the initializer we send over to lightclient nodes
/// A LightClientBootstrap is the initializer we send over to light_client nodes
/// that are trying to generate their basic storage when booting up.
#[derive(
Debug,

View File

@@ -1,6 +1,4 @@
use super::{
EthSpec, FixedVector, Hash256, SignedBeaconBlock, SignedBlindedBeaconBlock, Slot, SyncAggregate,
};
use super::{EthSpec, Hash256, SignedBeaconBlock, SignedBlindedBeaconBlock, Slot, SyncAggregate};
use crate::{
light_client_update::*, test_utils::TestRandom, BeaconState, ChainSpec, ForkName,
ForkVersionDeserialize, LightClientHeader,
@@ -8,10 +6,11 @@ use crate::{
use serde::{Deserialize, Deserializer, Serialize};
use serde_json::Value;
use ssz_derive::{Decode, Encode};
use ssz_types::FixedVector;
use test_random_derive::TestRandom;
use tree_hash::TreeHash;
/// A LightClientFinalityUpdate is the update lightclient request or received by a gossip that
/// A LightClientFinalityUpdate is the update light_client request or received by a gossip that
/// signal a new finalized beacon block header for the light client sync protocol.
#[derive(
Debug,

View File

@@ -43,7 +43,7 @@ pub fn leaf_count(len: usize) -> usize {
pub fn leaf_iter(
values: &[ParticipationFlags],
) -> impl Iterator<Item = [u8; BYTES_PER_CHUNK]> + ExactSizeIterator + '_ {
) -> impl ExactSizeIterator<Item = [u8; BYTES_PER_CHUNK]> + '_ {
values.chunks(BYTES_PER_CHUNK).map(|xs| {
// Zero-pad chunks on the right.
let mut chunk = [0u8; BYTES_PER_CHUNK];

View File

@@ -72,36 +72,43 @@ impl SubnetId {
.into())
}
#[allow(clippy::arithmetic_side_effects)]
/// Computes the set of subnets the node should be subscribed to during the current epoch,
/// along with the first epoch in which these subscriptions are no longer valid.
#[allow(clippy::arithmetic_side_effects)]
pub fn compute_subnets_for_epoch<T: EthSpec>(
node_id: ethereum_types::U256,
epoch: Epoch,
spec: &ChainSpec,
) -> Result<(impl Iterator<Item = SubnetId>, Epoch), &'static str> {
// Simplify the variable name
// simplify variable naming
let subscription_duration = spec.epochs_per_subnet_subscription;
let prefix_bits = spec.attestation_subnet_prefix_bits as u64;
let shuffling_prefix_bits = spec.attestation_subnet_shuffling_prefix_bits as u64;
let node_id_prefix =
(node_id >> (256 - spec.attestation_subnet_prefix_bits as usize)).as_usize();
// calculate the prefixes used to compute the subnet and shuffling
let node_id_prefix = (node_id >> (256 - prefix_bits)).as_u64();
let shuffling_prefix = (node_id >> (256 - (prefix_bits + shuffling_prefix_bits))).as_u64();
// NOTE: The as_u64() panics if the number is larger than u64::max_value(). This cannot be
// true as spec.epochs_per_subnet_subscription is a u64.
let node_offset = (node_id % ethereum_types::U256::from(subscription_duration)).as_u64();
// number of groups the shuffling creates
let shuffling_groups = 1 << shuffling_prefix_bits;
// shuffling group for this node
let shuffling_bits = shuffling_prefix % shuffling_groups;
let epoch_transition = (node_id_prefix
+ (shuffling_bits * (subscription_duration >> shuffling_prefix_bits)))
% subscription_duration;
// Calculate at which epoch this node needs to re-evaluate
let valid_until_epoch = epoch.as_u64()
+ subscription_duration
.saturating_sub((epoch.as_u64() + node_offset) % subscription_duration);
.saturating_sub((epoch.as_u64() + epoch_transition) % subscription_duration);
let subscription_event_idx = (epoch.as_u64() + node_offset) / subscription_duration;
let subscription_event_idx = (epoch.as_u64() + epoch_transition) / subscription_duration;
let permutation_seed =
ethereum_hashing::hash(&int_to_bytes::int_to_bytes8(subscription_event_idx));
let num_subnets = 1 << spec.attestation_subnet_prefix_bits;
let permutated_prefix = compute_shuffled_index(
node_id_prefix,
node_id_prefix as usize,
num_subnets,
&permutation_seed,
spec.shuffle_round_count,
@@ -180,38 +187,33 @@ mod tests {
"60930578857433095740782970114409273483106482059893286066493409689627770333527",
"103822458477361691467064888613019442068586830412598673713899771287914656699997",
]
.into_iter()
.map(|v| ethereum_types::U256::from_dec_str(v).unwrap())
.collect::<Vec<_>>();
.map(|v| ethereum_types::U256::from_dec_str(v).unwrap());
let epochs = [
54321u64, 1017090249, 1827566880, 846255942, 766597383, 1204990115, 1616209495,
1774367616, 1484598751, 3525502229,
]
.into_iter()
.map(Epoch::from)
.collect::<Vec<_>>();
.map(Epoch::from);
// Test mainnet
let spec = ChainSpec::mainnet();
// Calculated by hand
let expected_valid_time: Vec<u64> = [
54528, 1017090371, 1827567108, 846256076, 766597570, 1204990135, 1616209582,
1774367723, 1484598953, 3525502371,
]
.into();
let expected_valid_time = [
54528u64, 1017090255, 1827567030, 846256049, 766597387, 1204990287, 1616209536,
1774367857, 1484598847, 3525502311,
];
// Calculated from pyspec
let expected_subnets = vec![
let expected_subnets = [
vec![4u64, 5u64],
vec![61, 62],
vec![23, 24],
vec![31, 32],
vec![39, 40],
vec![38, 39],
vec![53, 54],
vec![39, 40],
vec![57, 58],
vec![48, 49],
vec![39, 40],
vec![1, 2],
vec![34, 35],
vec![37, 38],
];
@@ -228,11 +230,11 @@ mod tests {
>(node_ids[x], epochs[x], &spec)
.unwrap();
assert_eq!(Epoch::from(expected_valid_time[x]), valid_time);
assert_eq!(
expected_subnets[x],
computed_subnets.map(SubnetId::into).collect::<Vec<u64>>()
);
assert_eq!(Epoch::from(expected_valid_time[x]), valid_time);
}
}
}