Skip to content
Open
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
59 changes: 45 additions & 14 deletions crates/blockchain/state_transition/src/beacon/block_production.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,17 @@ 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,
};
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 @@ -377,12 +378,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 @@ -399,15 +406,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 @@ -508,10 +519,17 @@ 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 = BeaconBlockBody {
Expand Down Expand Up @@ -585,13 +603,17 @@ pub fn assemble_block(
BeaconState::Lean(_) => lean_state_unreachable("assemble_block"),
};
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,
)?;
// Fold the block's writes into their trees first, so the root is computed
Expand Down Expand Up @@ -679,6 +701,7 @@ mod tests {
&state,
inputs,
&Config::mainnet(),
&PubkeyIndexCache::default(),
BlockProcessingOptions::default(),
)
.unwrap();
Expand All @@ -700,8 +723,7 @@ mod tests {
&signed,
&Config::mainnet(),
&ExecutionEngine::valid(),
&CommitteeCache::default(),
&ActiveBalanceCache::default(),
OwnedBlockCaches::default().caches(),
BlockProcessingOptions::default(),
)
.unwrap();
Expand Down Expand Up @@ -736,6 +758,7 @@ mod tests {
&state,
inputs_with_infinity_reveal(&state),
&config,
&PubkeyIndexCache::default(),
BlockProcessingOptions::default(),
)
.is_err()
Expand All @@ -745,6 +768,7 @@ mod tests {
&state,
inputs_with_infinity_reveal(&state),
&config,
&PubkeyIndexCache::default(),
skip_randao(),
)
.unwrap();
Expand All @@ -764,8 +788,7 @@ mod tests {
&signed,
&config,
&ExecutionEngine::valid(),
&CommitteeCache::default(),
&ActiveBalanceCache::default(),
OwnedBlockCaches::default().caches(),
skip_randao(),
)
.unwrap();
Expand All @@ -792,6 +815,7 @@ mod tests {
&state,
inputs,
&Config::mainnet(),
&PubkeyIndexCache::default(),
BlockProcessingOptions::default(),
)
.is_err()
Expand Down Expand Up @@ -1161,8 +1185,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 @@ -1236,6 +1266,7 @@ mod tests {
&state,
inputs,
&Config::mainnet(),
&PubkeyIndexCache::default(),
BlockProcessingOptions::default(),
)
.unwrap();
Expand Down
47 changes: 24 additions & 23 deletions crates/blockchain/state_transition/src/beacon/fork_choice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,7 @@ use crate::beacon::containers::{bellatrix, deneb, electra, fulu, gloas, phase0};
use crate::beacon::error::{Error, Result, verify};
use crate::beacon::fork::ForkName;
use crate::beacon::helpers::accessors::{
ActiveBalanceCache, CommitteeCache, CommitteeCacheExt, get_active_validator_indices,
get_current_epoch,
CommitteeCache, CommitteeCacheExt, get_active_validator_indices, get_current_epoch,
};
use crate::beacon::helpers::attestation as phase0_attestation;
use crate::beacon::helpers::electra as electra_helpers;
Expand Down Expand Up @@ -1060,8 +1059,7 @@ fn transition_block(
validate_result: bool,
config: &Config,
engine: &stf::ExecutionEngine,
committees: &CommitteeCache,
active_balances: &ActiveBalanceCache,
caches: stf::BlockCaches<'_>,
) -> (BeaconState, Result<()>) {
let block_slot = signed_block.slot();
let epoch = compute_epoch_at_slot(block_slot);
Expand All @@ -1086,8 +1084,7 @@ fn transition_block(
validate_result,
config,
engine,
committees,
active_balances,
caches,
);
return (state, outcome);
}
Expand All @@ -1103,8 +1100,7 @@ fn transition_block(
validate_result,
config,
engine,
committees,
active_balances,
caches,
)
});
return (state, outcome);
Expand All @@ -1128,8 +1124,7 @@ fn transition_block(
validate_result,
config,
engine,
committees,
active_balances,
caches,
)
});
(state, outcome)
Expand Down Expand Up @@ -4958,9 +4953,17 @@ pub fn on_block(
// applies, after `process_slots`, so no second parent state exists).
// Either way `state` is an independent clone, so a failing block cannot
// corrupt a cached entry.
// The store's own cache, so every block of this chain shares one entry per
// epoch; see `ActiveBalanceCacheExt`.
// The store's own tables: every block of this chain shares one total
// active balance per epoch (see `ActiveBalanceCacheExt`), and one pubkey
// table, shared across forks since every hit is verified against the state
// in hand (see `PubkeyIndexCacheExt`).
let active_balances = store.active_balance_cache();
let pubkey_indices = store.pubkey_index_cache();
let caches = stf::BlockCaches {
committees,
active_balances: &active_balances,
pubkey_indices: &pubkey_indices,
};
let (mut state, transition) = transition_block(
store,
&parent_state,
Expand All @@ -4969,8 +4972,7 @@ pub fn on_block(
true,
config,
&engine,
committees,
&active_balances,
caches,
);

// `optimistic-sync.md`: a block deemed `INVALIDATED` MUST NOT be included
Expand Down Expand Up @@ -5646,7 +5648,7 @@ mod tests {
use super::*;
use crate::beacon::bls;
use crate::beacon::containers::BeaconBlockHeader;
use crate::beacon::helpers::accessors::{self, ActiveBalanceCache};
use crate::beacon::helpers::accessors;
use crate::beacon::helpers::misc::compute_signing_root;
use crate::beacon::helpers::test_state;
use crate::beacon::primitives::BlsSignature;
Expand Down Expand Up @@ -6316,8 +6318,7 @@ mod tests {
false,
&config,
&stf::ExecutionEngine::valid(),
&CommitteeCache::default(),
&ActiveBalanceCache::default(),
stf::OwnedBlockCaches::default().caches(),
);
outcome.expect("applies");
post.apply_pending_mutations();
Expand Down Expand Up @@ -7189,15 +7190,13 @@ mod tests {
// the transition once, unvalidated, the way a proposer decides
// everything but the signature and the state root before signing.
let mut trial_state = anchor_state.clone();
let trial_committees = CommitteeCache::default();
stf::state_transition(
&mut trial_state,
&draft,
false,
&config,
&stf::ExecutionEngine::valid(),
&trial_committees,
&ActiveBalanceCache::default(),
stf::OwnedBlockCaches::default().caches(),
)
.expect("the drafted block must transition cleanly");
let state_root = trial_state.hash_tree_root();
Expand Down Expand Up @@ -10391,7 +10390,9 @@ mod tests {
BlockInputs, advance_to_slot, assemble_block, empty_sync_aggregate, payload_inputs,
};
use crate::beacon::containers::electra;
use crate::beacon::helpers::accessors::{get_beacon_proposer_index, get_domain};
use crate::beacon::helpers::accessors::{
PubkeyIndexCache, get_beacon_proposer_index, get_domain,
};
use crate::beacon::helpers::test_state::{sign_for, with_signing_validators_at};
use crate::beacon::primitives::Bytes32;
use ethlambda_types::beacon::containers::deneb::ExecutionPayload;
Expand Down Expand Up @@ -10448,6 +10449,7 @@ mod tests {
&advanced,
inputs,
&config,
&PubkeyIndexCache::default(),
crate::beacon::stf::block::BlockProcessingOptions::default(),
)
.expect("assemble");
Expand All @@ -10473,8 +10475,7 @@ mod tests {
false,
&Config::mainnet(),
&stf::ExecutionEngine::valid(),
&CommitteeCache::default(),
&ActiveBalanceCache::default(),
stf::OwnedBlockCaches::default().caches(),
);
outcome.expect("the block applies");
state.apply_pending_mutations();
Expand Down
Loading
Loading