Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions bin/ethlambda/src/era/replay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
use std::path::Path;
use std::time::Instant;

use ethlambda_state_transition::beacon::helpers::accessors::{ActiveBalanceCache, CommitteeCache};
use ethlambda_state_transition::beacon::stf::{self, ExecutionEngine};
use ethlambda_state_transition::beacon::stf::{self, ExecutionEngine, OwnedBlockCaches};
use ethlambda_types::beacon::config::Config;
use ethlambda_types::beacon::containers::{BeaconState, SignedBeaconBlock};
use ethlambda_types::beacon::preset;
Expand Down Expand Up @@ -52,8 +51,7 @@ pub(super) struct Replay<'a> {
config: &'a Config,
engine: ExecutionEngine,
/// One for the whole run, as the node holds one across its imports.
committees: CommitteeCache,
active_balances: ActiveBalanceCache,
caches: OwnedBlockCaches,
max_blocks: Option<u64>,
carried: Option<Carried>,
}
Expand All @@ -64,8 +62,7 @@ impl<'a> Replay<'a> {
network,
config,
engine: ExecutionEngine::valid(),
committees: CommitteeCache::default(),
active_balances: ActiveBalanceCache::default(),
caches: OwnedBlockCaches::default(),
max_blocks,
carried: None,
}
Expand Down Expand Up @@ -167,8 +164,7 @@ impl<'a> Replay<'a> {
true,
self.config,
&self.engine,
&self.committees,
&self.active_balances,
self.caches.caches(),
)
.map_err(|err| {
eyre!(
Expand Down
81 changes: 61 additions & 20 deletions crates/blockchain/state_transition/src/beacon/block_production.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ use super::bls;
use super::config::Config;
use super::error::{Error, Result, verify};
use super::helpers::accessors::{
ActiveBalanceCache, CommitteeCache, get_beacon_proposer_index, get_block_root,
get_block_root_at_slot, get_current_epoch, get_domain, get_previous_epoch, get_randao_mix,
ActiveBalanceCache, CommitteeCache, PubkeyIndexCache, get_beacon_proposer_index,
get_block_root, get_block_root_at_slot, get_current_epoch, get_domain, get_previous_epoch,
get_randao_mix,
};
use super::helpers::electra::{
get_attesting_indices, get_indexed_attestation, is_valid_indexed_attestation,
Expand All @@ -50,7 +51,7 @@ use super::helpers::fulu::fulu_state;
use super::helpers::misc::compute_signing_root;
use super::helpers::predicates::is_slashable_validator;
use super::lean_boundary::lean_state_unreachable;
use super::stf::{self, ExecutionEngine};
use super::stf::{self, BlockCaches, ExecutionEngine, OwnedBlockCaches};
use ethlambda_storage::pools::single_committee;

/// `state` advanced through empty slots to `slot`, as a block for `slot` is
Expand Down Expand Up @@ -380,12 +381,18 @@ impl Operations {
/// maximum.
pub fn pack_operations(state: &BeaconState, candidates: Operations, config: &Config) -> Operations {
let mut scratch = state.clone();
// One table for the whole scratch pass, slashings included (a slashing
// starts an exit too): the scratch state is at the block's own slot, so
// its total active balance cannot change between candidates.
let active_balances = ActiveBalanceCache::default();
let proposer_slashings = pack_each(
&mut scratch,
candidates.proposer_slashings,
preset::MAX_PROPOSER_SLASHINGS,
config,
stf::operations::process_proposer_slashing,
|state, slashing, config| {
stf::operations::process_proposer_slashing(state, slashing, config, &active_balances)
},
);

let epoch = get_current_epoch(&scratch);
Expand All @@ -402,15 +409,19 @@ pub fn pack_operations(state: &BeaconState, candidates: Operations, config: &Con
ranked.into_iter().map(|(_, slashing)| slashing).collect(),
preset::MAX_ATTESTER_SLASHINGS_ELECTRA,
config,
stf::electra::process_attester_slashing,
|state, slashing, config| {
stf::electra::process_attester_slashing(state, slashing, config, &active_balances)
},
);

let voluntary_exits = pack_each(
&mut scratch,
candidates.voluntary_exits,
preset::MAX_VOLUNTARY_EXITS,
config,
stf::electra::process_voluntary_exit,
|state, exit, config| {
stf::electra::process_voluntary_exit(state, exit, config, &active_balances)
},
);
let bls_to_execution_changes = pack_each(
&mut scratch,
Expand Down Expand Up @@ -511,15 +522,22 @@ pub struct BlockInputs {
/// the network would, before anything is signed. `options` is what that run may
/// leave unchecked: only a caller that set `skip_randao_verification` passes
/// anything but the default.
///
/// `pubkey_indices` is the pubkey table that run resolves execution requests
/// and the sync committee through. A node passes its `Store`'s shared one: a
/// fresh table would first ingest the whole registry, which at mainnet's size
/// costs more than the registry scans the table replaces. The run's committee
/// and total-active-balance tables are fresh, as they always were here.
pub fn assemble_block(
state: &BeaconState,
inputs: BlockInputs,
config: &Config,
pubkey_indices: &PubkeyIndexCache,
options: stf::block::BlockProcessingOptions,
) -> Result<BeaconBlock> {
let body = full_body(state, inputs)?;
let mut block = unsigned_block(state, body)?;
let mut post = post_state(state, &block, config, options)?;
let mut post = post_state(state, &block, config, pubkey_indices, options)?;
// Fold the block's writes into their trees first, so the root is computed
// on (and cached in) the state's own nodes rather than a throwaway copy,
// as `state_transition` does before it checks a block's state root.
Expand Down Expand Up @@ -591,11 +609,12 @@ fn unsigned_block(state: &BeaconState, body: BeaconBlockBody) -> Result<BeaconBl
/// A copy of `state` with `block` applied by `process_block`, under an
/// execution engine that accepts its payload. Pending mutations are left
/// unapplied, so the caller can still edit the state before computing its
/// root.
/// root. `pubkey_indices` is as in [`assemble_block`].
fn post_state(
state: &BeaconState,
block: &BeaconBlock,
config: &Config,
pubkey_indices: &PubkeyIndexCache,
options: stf::block::BlockProcessingOptions,
) -> Result<BeaconState> {
let signed = electra::SignedBeaconBlock {
Expand All @@ -618,13 +637,17 @@ fn post_state(
BeaconState::Lean(_) => lean_state_unreachable("post_state"),
};
let mut post = state.clone();
let scratch = OwnedBlockCaches::default();
let caches = BlockCaches {
pubkey_indices,
..scratch.caches()
};
stf::block::process_block(
&mut post,
&wrapped,
config,
&ExecutionEngine::valid(),
&CommitteeCache::default(),
&ActiveBalanceCache::default(),
caches,
options,
)?;
Ok(post)
Expand Down Expand Up @@ -679,7 +702,7 @@ pub struct BlindedBlockInputs {
/// (at least the response's fork version, the signature, the value, the gas
/// limit and `blob_gas_used`) before building on it.
///
/// `options` is as in [`assemble_block`].
/// `pubkey_indices` and `options` are as in [`assemble_block`].
///
/// Fulu only. The table describes fulu's `process_block`, and fulu is the last
/// fork with blinded blocks (gloas has none), so it cannot change under this
Expand All @@ -688,6 +711,7 @@ pub fn assemble_blinded_block(
state: &BeaconState,
inputs: BlindedBlockInputs,
config: &Config,
pubkey_indices: &PubkeyIndexCache,
options: stf::block::BlockProcessingOptions,
) -> Result<BlindedBeaconBlock> {
if !matches!(state, BeaconState::Fulu(_)) {
Expand Down Expand Up @@ -727,7 +751,7 @@ pub fn assemble_blinded_block(
},
)?;
let block = unsigned_block(state, body)?;
let mut post = post_state(state, &block, config, options)?;
let mut post = post_state(state, &block, config, pubkey_indices, options)?;

let mut blinded = BlindedBeaconBlock::from_parts(block, header);
let fields = fulu_state(&mut post, "assemble_blinded_block")?;
Expand Down Expand Up @@ -817,6 +841,7 @@ mod tests {
&state,
inputs,
&Config::mainnet(),
&PubkeyIndexCache::default(),
BlockProcessingOptions::default(),
)
.unwrap();
Expand All @@ -838,8 +863,7 @@ mod tests {
&signed,
&Config::mainnet(),
&ExecutionEngine::valid(),
&CommitteeCache::default(),
&ActiveBalanceCache::default(),
OwnedBlockCaches::default().caches(),
BlockProcessingOptions::default(),
)
.unwrap();
Expand Down Expand Up @@ -874,6 +898,7 @@ mod tests {
&state,
inputs_with_infinity_reveal(&state),
&config,
&PubkeyIndexCache::default(),
BlockProcessingOptions::default(),
)
.is_err()
Expand All @@ -883,6 +908,7 @@ mod tests {
&state,
inputs_with_infinity_reveal(&state),
&config,
&PubkeyIndexCache::default(),
skip_randao(),
)
.unwrap();
Expand All @@ -902,8 +928,7 @@ mod tests {
&signed,
&config,
&ExecutionEngine::valid(),
&CommitteeCache::default(),
&ActiveBalanceCache::default(),
OwnedBlockCaches::default().caches(),
skip_randao(),
)
.unwrap();
Expand All @@ -930,6 +955,7 @@ mod tests {
&state,
inputs,
&Config::mainnet(),
&PubkeyIndexCache::default(),
BlockProcessingOptions::default(),
)
.is_err()
Expand Down Expand Up @@ -1299,8 +1325,14 @@ mod tests {
execution_requests: ExecutionRequests::default(),
sync_aggregate: empty_sync_aggregate(),
};
let block =
assemble_block(&state, inputs, &config, BlockProcessingOptions::default()).unwrap();
let block = assemble_block(
&state,
inputs,
&config,
&PubkeyIndexCache::default(),
BlockProcessingOptions::default(),
)
.unwrap();
assert_eq!(block.body.voluntary_exits.len(), 1);
assert_eq!(block.body.voluntary_exits[0].message.validator_index, 4);
}
Expand Down Expand Up @@ -1374,6 +1406,7 @@ mod tests {
&state,
inputs,
&Config::mainnet(),
&PubkeyIndexCache::default(),
BlockProcessingOptions::default(),
)
.unwrap();
Expand Down Expand Up @@ -1498,12 +1531,19 @@ mod tests {
let blinded_inputs = blinded(&inputs);
let config = Config::mainnet();

let full =
assemble_block(&state, inputs, &config, BlockProcessingOptions::default()).unwrap();
let full = assemble_block(
&state,
inputs,
&config,
&PubkeyIndexCache::default(),
BlockProcessingOptions::default(),
)
.unwrap();
let blinded = assemble_blinded_block(
&state,
blinded_inputs,
&config,
&PubkeyIndexCache::default(),
BlockProcessingOptions::default(),
)
.unwrap();
Expand All @@ -1519,6 +1559,7 @@ mod tests {
state,
inputs,
&Config::mainnet(),
&PubkeyIndexCache::default(),
BlockProcessingOptions::default(),
) {
Err(Error::SpecAssert(what)) => what,
Expand Down
Loading
Loading