Merge branch 'electra-epoch-proc' of https://github.com/sigp/lighthouse into electra-engine-api

This commit is contained in:
realbigsean
2024-06-13 14:47:37 -04:00
78 changed files with 1329 additions and 4860 deletions

View File

@@ -1,7 +1,4 @@
#![allow(deprecated)]
use criterion::Criterion;
use criterion::{black_box, criterion_group, criterion_main, Benchmark};
use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion};
use swap_or_not_shuffle::{compute_shuffled_index, shuffle_list as fast_shuffle};
const SHUFFLE_ROUND_COUNT: u8 = 90;
@@ -25,70 +22,32 @@ fn shuffles(c: &mut Criterion) {
b.iter(|| black_box(shuffle_list(&seed, 8)))
});
c.bench(
"whole list shuffle",
Benchmark::new("8 elements", move |b| {
let seed = vec![42; 32];
b.iter(|| black_box(shuffle_list(&seed, 8)))
}),
);
for size in [8, 16, 512, 16_384] {
c.bench_with_input(
BenchmarkId::new("whole list shuffle", format!("{size} elements")),
&size,
move |b, &n| {
let seed = vec![42; 32];
b.iter(|| black_box(shuffle_list(&seed, n)))
},
);
}
c.bench(
"whole list shuffle",
Benchmark::new("16 elements", move |b| {
let seed = vec![42; 32];
b.iter(|| black_box(shuffle_list(&seed, 16)))
}),
);
c.bench(
"whole list shuffle",
Benchmark::new("512 elements", move |b| {
let seed = vec![42; 32];
b.iter(|| black_box(shuffle_list(&seed, 512)))
})
.sample_size(10),
);
c.bench(
"_fast_ whole list shuffle",
Benchmark::new("512 elements", move |b| {
let seed = vec![42; 32];
let list: Vec<usize> = (0..512).collect();
b.iter(|| black_box(fast_shuffle(list.clone(), SHUFFLE_ROUND_COUNT, &seed, true)))
})
.sample_size(10),
);
c.bench(
"whole list shuffle",
Benchmark::new("16384 elements", move |b| {
let seed = vec![42; 32];
b.iter(|| black_box(shuffle_list(&seed, 16_384)))
})
.sample_size(10),
);
c.bench(
"_fast_ whole list shuffle",
Benchmark::new("16384 elements", move |b| {
let seed = vec![42; 32];
let list: Vec<usize> = (0..16384).collect();
b.iter(|| black_box(fast_shuffle(list.clone(), SHUFFLE_ROUND_COUNT, &seed, true)))
})
.sample_size(10),
);
c.bench(
"_fast_ whole list shuffle",
Benchmark::new("4m elements", move |b| {
let seed = vec![42; 32];
let list: Vec<usize> = (0..4_000_000).collect();
b.iter(|| black_box(fast_shuffle(list.clone(), SHUFFLE_ROUND_COUNT, &seed, true)))
})
.sample_size(10),
);
let mut group = c.benchmark_group("fast");
group.sample_size(10);
for size in [512, 16_384, 4_000_000] {
group.bench_with_input(
BenchmarkId::new("whole list shuffle", format!("{size} elements")),
&size,
move |b, &n| {
let seed = vec![42; 32];
let list: Vec<usize> = (0..n).collect();
b.iter(|| black_box(fast_shuffle(list.clone(), SHUFFLE_ROUND_COUNT, &seed, true)))
},
);
}
group.finish();
}
criterion_group!(benches, shuffles,);
criterion_group!(benches, shuffles);
criterion_main!(benches);

View File

@@ -1,7 +1,4 @@
#![allow(deprecated)]
use criterion::Criterion;
use criterion::{black_box, criterion_group, criterion_main, Benchmark};
use criterion::{black_box, criterion_group, criterion_main, BatchSize, BenchmarkId, Criterion};
use milhouse::List;
use rayon::prelude::*;
use ssz::Encode;
@@ -53,75 +50,82 @@ fn all_benches(c: &mut Criterion) {
let validator_count = 16_384;
let spec = Arc::new(MainnetEthSpec::default_spec());
let mut g = c.benchmark_group("types");
g.sample_size(10);
let mut state = get_state::<MainnetEthSpec>(validator_count);
state.build_caches(&spec).expect("should build caches");
let state_bytes = state.as_ssz_bytes();
let inner_state = state.clone();
c.bench(
&format!("{}_validators", validator_count),
Benchmark::new("encode/beacon_state", move |b| {
g.bench_with_input(
BenchmarkId::new("encode/beacon_state", validator_count),
&inner_state,
|b, state| {
b.iter_batched_ref(
|| inner_state.clone(),
|| state.clone(),
|state| black_box(state.as_ssz_bytes()),
criterion::BatchSize::SmallInput,
BatchSize::SmallInput,
)
})
.sample_size(10),
},
);
c.bench(
&format!("{}_validators", validator_count),
Benchmark::new("decode/beacon_state", move |b| {
g.bench_with_input(
BenchmarkId::new("decode/beacon_state", validator_count),
&(state_bytes.clone(), spec.clone()),
|b, (bytes, spec)| {
b.iter_batched_ref(
|| (state_bytes.clone(), spec.clone()),
|| (bytes.clone(), spec.clone()),
|(bytes, spec)| {
let state: BeaconState<MainnetEthSpec> =
BeaconState::from_ssz_bytes(&bytes, &spec).expect("should decode");
black_box(state)
},
criterion::BatchSize::SmallInput,
BatchSize::SmallInput,
)
})
.sample_size(10),
},
);
let inner_state = state.clone();
c.bench(
&format!("{}_validators", validator_count),
Benchmark::new("clone/beacon_state", move |b| {
g.bench_with_input(
BenchmarkId::new("clone/beacon_state", validator_count),
&inner_state,
|b, state| {
b.iter_batched_ref(
|| inner_state.clone(),
|| state.clone(),
|state| black_box(state.clone()),
criterion::BatchSize::SmallInput,
BatchSize::SmallInput,
)
})
.sample_size(10),
},
);
let inner_state = state.clone();
c.bench(
&format!("{}_validators", validator_count),
Benchmark::new(
g.bench_with_input(
BenchmarkId::new(
"initialized_cached_tree_hash_without_changes/beacon_state",
move |b| {
b.iter_batched_ref(
|| inner_state.clone(),
|state| black_box(state.update_tree_hash_cache()),
criterion::BatchSize::SmallInput,
)
},
)
.sample_size(10),
validator_count,
),
&inner_state,
|b, state| {
b.iter_batched_ref(
|| state.clone(),
|state| black_box(state.update_tree_hash_cache()),
BatchSize::SmallInput,
)
},
);
let mut inner_state = state.clone();
inner_state.drop_all_caches().unwrap();
c.bench(
&format!("{}_validators", validator_count),
Benchmark::new("non_initialized_cached_tree_hash/beacon_state", move |b| {
g.bench_with_input(
BenchmarkId::new(
"non_initialized_cached_tree_hash/beacon_state",
validator_count,
),
&inner_state,
|b, state| {
b.iter_batched_ref(
|| inner_state.clone(),
|| state.clone(),
|state| {
black_box(
state
@@ -129,41 +133,40 @@ fn all_benches(c: &mut Criterion) {
.expect("should update tree hash"),
)
},
criterion::BatchSize::SmallInput,
BatchSize::SmallInput,
)
})
.sample_size(10),
},
);
let inner_state = state.clone();
c.bench(
&format!("{}_validators", validator_count),
Benchmark::new(
g.bench_with_input(
BenchmarkId::new(
"initialized_cached_tree_hash_with_new_validators/beacon_state",
move |b| {
b.iter_batched_ref(
|| {
let mut state = inner_state.clone();
for _ in 0..16 {
state
.validators_mut()
.push(Validator::default())
.expect("should push validatorj");
state
.balances_mut()
.push(32_000_000_000)
.expect("should push balance");
}
validator_count,
),
&inner_state,
|b, state| {
b.iter_batched_ref(
|| {
let mut state = state.clone();
for _ in 0..16 {
state
},
|state| black_box(state.update_tree_hash_cache()),
criterion::BatchSize::SmallInput,
)
},
)
.sample_size(10),
.validators_mut()
.push(Validator::default())
.expect("should push validator");
state
.balances_mut()
.push(32_000_000_000)
.expect("should push balance");
}
state
},
|state| black_box(state.update_tree_hash_cache()),
BatchSize::SmallInput,
)
},
);
}
criterion_group!(benches, all_benches,);
criterion_group!(benches, all_benches);
criterion_main!(benches);

View File

@@ -2,6 +2,7 @@ use crate::test_utils::TestRandom;
use crate::*;
use derivative::Derivative;
use merkle_proof::{MerkleTree, MerkleTreeError};
use metastruct::metastruct;
use serde::{Deserialize, Serialize};
use ssz_derive::{Decode, Encode};
use std::marker::PhantomData;
@@ -50,6 +51,14 @@ pub const BLOB_KZG_COMMITMENTS_INDEX: usize = 11;
),
arbitrary(bound = "E: EthSpec, Payload: AbstractExecPayload<E>"),
),
specific_variant_attributes(
Base(metastruct(mappings(beacon_block_body_base_fields(groups(fields))))),
Altair(metastruct(mappings(beacon_block_body_altair_fields(groups(fields))))),
Bellatrix(metastruct(mappings(beacon_block_body_bellatrix_fields(groups(fields))))),
Capella(metastruct(mappings(beacon_block_body_capella_fields(groups(fields))))),
Deneb(metastruct(mappings(beacon_block_body_deneb_fields(groups(fields))))),
Electra(metastruct(mappings(beacon_block_body_electra_fields(groups(fields))))),
),
cast_error(ty = "Error", expr = "Error::IncorrectStateVariant"),
partial_getter_error(ty = "Error", expr = "Error::IncorrectStateVariant")
)]
@@ -108,6 +117,7 @@ pub struct BeaconBlockBody<E: EthSpec, Payload: AbstractExecPayload<E> = FullPay
#[superstruct(only(Electra))]
pub consolidations: VariableList<SignedConsolidation, E::MaxConsolidations>,
#[superstruct(only(Base, Altair))]
#[metastruct(exclude_from(fields))]
#[ssz(skip_serializing, skip_deserializing)]
#[tree_hash(skip_hashing)]
#[serde(skip)]
@@ -132,139 +142,88 @@ impl<'a, E: EthSpec, Payload: AbstractExecPayload<E>> BeaconBlockBodyRef<'a, E,
}
}
fn body_merkle_leaves(&self) -> Vec<Hash256> {
let mut leaves = vec![];
match self {
Self::Base(body) => {
beacon_block_body_base_fields!(body, |_, field| leaves
.push(field.tree_hash_root()));
}
Self::Altair(body) => {
beacon_block_body_altair_fields!(body, |_, field| leaves
.push(field.tree_hash_root()));
}
Self::Bellatrix(body) => {
beacon_block_body_bellatrix_fields!(body, |_, field| leaves
.push(field.tree_hash_root()));
}
Self::Capella(body) => {
beacon_block_body_capella_fields!(body, |_, field| leaves
.push(field.tree_hash_root()));
}
Self::Deneb(body) => {
beacon_block_body_deneb_fields!(body, |_, field| leaves
.push(field.tree_hash_root()));
}
Self::Electra(body) => {
beacon_block_body_electra_fields!(body, |_, field| leaves
.push(field.tree_hash_root()));
}
}
leaves
}
/// Produces the proof of inclusion for a `KzgCommitment` in `self.blob_kzg_commitments`
/// at `index`.
pub fn kzg_commitment_merkle_proof(
&self,
index: usize,
) -> Result<FixedVector<Hash256, E::KzgCommitmentInclusionProofDepth>, Error> {
match self {
Self::Base(_) | Self::Altair(_) | Self::Bellatrix(_) | Self::Capella(_) => {
Err(Error::IncorrectStateVariant)
}
Self::Deneb(body) => {
// We compute the branches by generating 2 merkle trees:
// 1. Merkle tree for the `blob_kzg_commitments` List object
// 2. Merkle tree for the `BeaconBlockBody` container
// We then merge the branches for both the trees all the way up to the root.
// We compute the branches by generating 2 merkle trees:
// 1. Merkle tree for the `blob_kzg_commitments` List object
// 2. Merkle tree for the `BeaconBlockBody` container
// We then merge the branches for both the trees all the way up to the root.
// Part1 (Branches for the subtree rooted at `blob_kzg_commitments`)
//
// Branches for `blob_kzg_commitments` without length mix-in
let depth = E::max_blob_commitments_per_block()
.next_power_of_two()
.ilog2();
let leaves: Vec<_> = body
.blob_kzg_commitments
.iter()
.map(|commitment| commitment.tree_hash_root())
.collect();
let tree = MerkleTree::create(&leaves, depth as usize);
let (_, mut proof) = tree
.generate_proof(index, depth as usize)
.map_err(Error::MerkleTreeError)?;
// Part1 (Branches for the subtree rooted at `blob_kzg_commitments`)
//
// Branches for `blob_kzg_commitments` without length mix-in
let blob_leaves = self
.blob_kzg_commitments()?
.iter()
.map(|commitment| commitment.tree_hash_root())
.collect::<Vec<_>>();
let depth = E::max_blob_commitments_per_block()
.next_power_of_two()
.ilog2();
let tree = MerkleTree::create(&blob_leaves, depth as usize);
let (_, mut proof) = tree
.generate_proof(index, depth as usize)
.map_err(Error::MerkleTreeError)?;
// Add the branch corresponding to the length mix-in.
let length = body.blob_kzg_commitments.len();
let usize_len = std::mem::size_of::<usize>();
let mut length_bytes = [0; BYTES_PER_CHUNK];
length_bytes
.get_mut(0..usize_len)
.ok_or(Error::MerkleTreeError(MerkleTreeError::PleaseNotifyTheDevs))?
.copy_from_slice(&length.to_le_bytes());
let length_root = Hash256::from_slice(length_bytes.as_slice());
proof.push(length_root);
// Add the branch corresponding to the length mix-in.
let length = blob_leaves.len();
let usize_len = std::mem::size_of::<usize>();
let mut length_bytes = [0; BYTES_PER_CHUNK];
length_bytes
.get_mut(0..usize_len)
.ok_or(Error::MerkleTreeError(MerkleTreeError::PleaseNotifyTheDevs))?
.copy_from_slice(&length.to_le_bytes());
let length_root = Hash256::from_slice(length_bytes.as_slice());
proof.push(length_root);
// Part 2
// Branches for `BeaconBlockBody` container
let leaves = [
body.randao_reveal.tree_hash_root(),
body.eth1_data.tree_hash_root(),
body.graffiti.tree_hash_root(),
body.proposer_slashings.tree_hash_root(),
body.attester_slashings.tree_hash_root(),
body.attestations.tree_hash_root(),
body.deposits.tree_hash_root(),
body.voluntary_exits.tree_hash_root(),
body.sync_aggregate.tree_hash_root(),
body.execution_payload.tree_hash_root(),
body.bls_to_execution_changes.tree_hash_root(),
body.blob_kzg_commitments.tree_hash_root(),
];
let beacon_block_body_depth = leaves.len().next_power_of_two().ilog2() as usize;
let tree = MerkleTree::create(&leaves, beacon_block_body_depth);
let (_, mut proof_body) = tree
.generate_proof(BLOB_KZG_COMMITMENTS_INDEX, beacon_block_body_depth)
.map_err(Error::MerkleTreeError)?;
// Join the proofs for the subtree and the main tree
proof.append(&mut proof_body);
// Part 2
// Branches for `BeaconBlockBody` container
let body_leaves = self.body_merkle_leaves();
let beacon_block_body_depth = body_leaves.len().next_power_of_two().ilog2() as usize;
let tree = MerkleTree::create(&body_leaves, beacon_block_body_depth);
let (_, mut proof_body) = tree
.generate_proof(BLOB_KZG_COMMITMENTS_INDEX, beacon_block_body_depth)
.map_err(Error::MerkleTreeError)?;
// Join the proofs for the subtree and the main tree
proof.append(&mut proof_body);
debug_assert_eq!(proof.len(), E::kzg_proof_inclusion_proof_depth());
debug_assert_eq!(proof.len(), E::kzg_proof_inclusion_proof_depth());
Ok(proof.into())
}
// TODO(electra): De-duplicate proof computation.
Self::Electra(body) => {
// We compute the branches by generating 2 merkle trees:
// 1. Merkle tree for the `blob_kzg_commitments` List object
// 2. Merkle tree for the `BeaconBlockBody` container
// We then merge the branches for both the trees all the way up to the root.
// Part1 (Branches for the subtree rooted at `blob_kzg_commitments`)
//
// Branches for `blob_kzg_commitments` without length mix-in
let depth = E::max_blob_commitments_per_block()
.next_power_of_two()
.ilog2();
let leaves: Vec<_> = body
.blob_kzg_commitments
.iter()
.map(|commitment| commitment.tree_hash_root())
.collect();
let tree = MerkleTree::create(&leaves, depth as usize);
let (_, mut proof) = tree
.generate_proof(index, depth as usize)
.map_err(Error::MerkleTreeError)?;
// Add the branch corresponding to the length mix-in.
let length = body.blob_kzg_commitments.len();
let usize_len = std::mem::size_of::<usize>();
let mut length_bytes = [0; BYTES_PER_CHUNK];
length_bytes
.get_mut(0..usize_len)
.ok_or(Error::MerkleTreeError(MerkleTreeError::PleaseNotifyTheDevs))?
.copy_from_slice(&length.to_le_bytes());
let length_root = Hash256::from_slice(length_bytes.as_slice());
proof.push(length_root);
// Part 2
// Branches for `BeaconBlockBody` container
let leaves = [
body.randao_reveal.tree_hash_root(),
body.eth1_data.tree_hash_root(),
body.graffiti.tree_hash_root(),
body.proposer_slashings.tree_hash_root(),
body.attester_slashings.tree_hash_root(),
body.attestations.tree_hash_root(),
body.deposits.tree_hash_root(),
body.voluntary_exits.tree_hash_root(),
body.sync_aggregate.tree_hash_root(),
body.execution_payload.tree_hash_root(),
body.bls_to_execution_changes.tree_hash_root(),
body.blob_kzg_commitments.tree_hash_root(),
body.consolidations.tree_hash_root(),
];
let beacon_block_body_depth = leaves.len().next_power_of_two().ilog2() as usize;
let tree = MerkleTree::create(&leaves, beacon_block_body_depth);
let (_, mut proof_body) = tree
.generate_proof(BLOB_KZG_COMMITMENTS_INDEX, beacon_block_body_depth)
.map_err(Error::MerkleTreeError)?;
// Join the proofs for the subtree and the main tree
proof.append(&mut proof_body);
debug_assert_eq!(proof.len(), E::kzg_proof_inclusion_proof_depth());
Ok(proof.into())
}
}
Ok(proof.into())
}
/// Return `true` if this block body has a non-zero number of blobs.