Skip to content

Commit

Permalink
move time preset values to config
Browse files Browse the repository at this point in the history
  • Loading branch information
dapplion committed Aug 19, 2023
1 parent d6f6ddc commit 5122df4
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 93 deletions.
7 changes: 3 additions & 4 deletions beacon_node/beacon_chain/src/beacon_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4006,7 +4006,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
let whisk_shuffling_decision_root = cached_head
.snapshot
.beacon_state
.whisk_proposer_shuffling_decision_root(self.genesis_block_root)?;
.whisk_proposer_shuffling_decision_root(self.genesis_block_root, &self.spec)?;

// After whisk the beacon node cannot compute the beacon proposer
// A validator voluntarily declares in advance that it is the proposer of a slot
Expand Down Expand Up @@ -4625,10 +4625,9 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
// with `whisk_post_shuffle_trackers`, `whisk_shuffle_proof`, `pre_shuffle_trackers`
// is derived from that state at this slot.
// - shuffle requires forgetable randomness, so it can be generated by the beacon node
let (whisk_post_shuffle_trackers, whisk_shuffle_proof) = if should_shuffle_trackers::<
T::EthSpec,
>(
let (whisk_post_shuffle_trackers, whisk_shuffle_proof) = if should_shuffle_trackers(
produce_at_slot.epoch(T::EthSpec::slots_per_epoch()),
&self.spec,
) {
let shuffle_indices_timer =
metrics::start_timer(&metrics::BLOCK_PRODUCTION_SHUFFLE_INDICES_TIMES);
Expand Down
2 changes: 1 addition & 1 deletion beacon_node/beacon_chain/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -899,7 +899,7 @@ where

// TODO WHISK: is it necessary to register whisk proposer here?
let whisk_shuffling_decision_root = state
.whisk_proposer_shuffling_decision_root(self.chain.head_beacon_block_root())
.whisk_proposer_shuffling_decision_root(self.chain.head_beacon_block_root(), &self.spec)
.unwrap();
self.chain
.register_whisk_proposer(&WhiskProposerPreparationData {
Expand Down
10 changes: 7 additions & 3 deletions beacon_node/http_api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1159,8 +1159,9 @@ pub fn serve<T: BeaconChainTypes>(
)
};

let requested_shuffle_round_start_slot =
T::EthSpec::whisk_shuffle_round_start_slot(requested_slot);
let requested_shuffle_round_start_slot = chain
.spec
.whisk_shuffle_round_start_slot::<T::EthSpec>(requested_slot);
let retrieve_state_at_slot = std::cmp::max(
head_slot,
std::cmp::max(whisk_fork_slot, requested_shuffle_round_start_slot),
Expand All @@ -1181,7 +1182,10 @@ pub fn serve<T: BeaconChainTypes>(
))
})?;
let dependent_root = state
.whisk_proposer_shuffling_decision_root(chain.genesis_validators_root)
.whisk_proposer_shuffling_decision_root(
chain.genesis_validators_root,
&chain.spec,
)
.map_err(|e| {
warp_utils::reject::custom_server_error(format!(
"cannot compute shuffling root: {e:?}"
Expand Down
2 changes: 1 addition & 1 deletion consensus/state_processing/src/per_block_processing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ pub fn per_block_processing<T: EthSpec, Payload: AbstractExecPayload<T>>(
update_progressive_balances_metrics(state.progressive_balances_cache())?;
}

process_shuffled_trackers(state, block.body())?;
process_shuffled_trackers(state, block.body(), spec)?;
process_whisk_registration(state, block)?;

Ok(())
Expand Down
57 changes: 6 additions & 51 deletions consensus/state_processing/src/per_block_processing/whisk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use safe_arith::SafeArith;
use ssz::Encode;
use types::{
AbstractExecPayload, BeaconBlockBodyRef, BeaconBlockRef, BeaconState, BeaconStateCapella,
BeaconStateError, Epoch, EthSpec, WhiskShuffleProof, WhiskTrackerProof,
BeaconStateError, ChainSpec, Epoch, EthSpec, WhiskShuffleProof, WhiskTrackerProof,
};

use crate::{upgrade::capella::compute_initial_whisk_k, BlockProcessingError};
Expand Down Expand Up @@ -155,6 +155,7 @@ pub fn get_shuffle_indices<T: EthSpec>(randao_reveal: &Signature) -> Vec<usize>
pub fn process_shuffled_trackers<T: EthSpec, Payload: AbstractExecPayload<T>>(
state: &mut BeaconState<T>,
body: BeaconBlockBodyRef<T, Payload>,
spec: &ChainSpec,
) -> Result<(), BlockProcessingError> {
let current_epoch = state.current_epoch();

Expand All @@ -173,7 +174,7 @@ pub fn process_shuffled_trackers<T: EthSpec, Payload: AbstractExecPayload<T>>(
let post_shuffle_trackers: Vec<WhiskTracker> =
whisk_post_shuffle_trackers.iter().cloned().collect();

if !should_shuffle_trackers::<T>(current_epoch) {
if !should_shuffle_trackers(current_epoch, spec) {
// Require zero-ed trackers during cooldown
let zero_tracker = WhiskTracker::default();
block_verify!(
Expand Down Expand Up @@ -221,10 +222,10 @@ pub fn process_shuffled_trackers<T: EthSpec, Payload: AbstractExecPayload<T>>(

/// Return true if at `epoch` validator should shuffle candidate trackers
#[allow(clippy::arithmetic_side_effects)]
pub fn should_shuffle_trackers<T: EthSpec>(epoch: Epoch) -> bool {
pub fn should_shuffle_trackers(epoch: Epoch, spec: &ChainSpec) -> bool {
// (clippy::arithmetic_side_effects) Will never divide by zero
epoch % T::whisk_epochs_per_shuffling_phase() + T::whisk_proposer_selection_gap() + 1
< T::whisk_epochs_per_shuffling_phase()
epoch % spec.whisk_epochs_per_shuffling_phase + spec.whisk_proposer_selection_gap + 1
< spec.whisk_epochs_per_shuffling_phase
}

pub fn verify_whisk_opening_proof<T: EthSpec, Payload: AbstractExecPayload<T>>(
Expand Down Expand Up @@ -277,49 +278,3 @@ pub fn verify_whisk_opening_proof<T: EthSpec, Payload: AbstractExecPayload<T>>(

Ok(())
}

#[cfg(test)]
mod tests {
use super::*;
use types::{MainnetEthSpec, MinimalEthSpec};

#[test]
fn test_should_shuffle_trackers_minimal_spec() {
test_should_shuffle_trackers_on_spec::<MinimalEthSpec>("minimal");
}

#[test]
fn test_should_shuffle_trackers_mainnet_spec() {
test_should_shuffle_trackers_on_spec::<MainnetEthSpec>("mainnet");
}

fn test_should_shuffle_trackers_on_spec<T: EthSpec>(spec_name: &str) {
dbg!((
T::whisk_proposer_selection_gap(),
T::whisk_epochs_per_shuffling_phase()
));
for (i, (epoch, expected_result)) in [
(0, true),
(1, true),
(T::whisk_epochs_per_shuffling_phase(), true),
(T::whisk_epochs_per_shuffling_phase() - 1, false),
(
T::whisk_epochs_per_shuffling_phase() - T::whisk_proposer_selection_gap() - 1,
false,
),
(
T::whisk_epochs_per_shuffling_phase() - T::whisk_proposer_selection_gap() - 2,
true,
),
]
.iter()
.enumerate()
{
assert_eq!(
should_shuffle_trackers::<T>(Epoch::new(*epoch)),
*expected_result,
"{spec_name} case {i} epoch {epoch}"
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ fn process_whisk_updates<T: EthSpec>(
spec: &ChainSpec,
) -> Result<(), EpochProcessingError> {
let next_epoch = state.next_epoch()?;
if next_epoch.safe_rem(T::whisk_epochs_per_shuffling_phase())? == 0 {
if next_epoch.safe_rem(spec.whisk_epochs_per_shuffling_phase)? == 0 {
select_whisk_trackers(state, next_epoch, spec)?;
}
Ok(())
Expand Down
7 changes: 5 additions & 2 deletions consensus/types/src/beacon_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -661,8 +661,11 @@ impl<T: EthSpec> BeaconState<T> {
pub fn whisk_proposer_shuffling_decision_root(
&self,
block_root: Hash256,
spec: &ChainSpec,
) -> Result<WhiskProposerShufflingRoot, Error> {
let decision_slot = T::whisk_shuffle_round_start_slot(self.slot()).saturating_sub(1_u64);
let decision_slot = spec
.whisk_shuffle_round_start_slot::<T>(self.slot())
.saturating_sub(1_u64);

if self.slot() == decision_slot {
Ok(WhiskProposerShufflingRoot(block_root))
Expand Down Expand Up @@ -856,7 +859,7 @@ impl<T: EthSpec> BeaconState<T> {
spec: &ChainSpec,
) -> Result<Vec<usize>, Error> {
let proposer_seed = self.get_seed(
epoch.saturating_sub(T::whisk_proposer_selection_gap()),
epoch.saturating_sub(spec.whisk_proposer_selection_gap),
Domain::WhiskProposerSelection,
spec,
)?;
Expand Down
22 changes: 22 additions & 0 deletions consensus/types/src/chain_spec.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::application_domain::{ApplicationDomain, APPLICATION_DOMAIN_BUILDER};
use crate::*;
use int_to_bytes::int_to_bytes4;
use safe_arith::SafeArith;
use serde::{Deserializer, Serialize, Serializer};
use serde_derive::Deserialize;
use serde_utils::quoted_u64::MaybeQuoted;
Expand Down Expand Up @@ -162,6 +163,8 @@ pub struct ChainSpec {
/// The Capella fork epoch is optional, with `None` representing "Capella never happens".
pub capella_fork_epoch: Option<Epoch>,
pub max_validators_per_withdrawals_sweep: u64,
pub whisk_epochs_per_shuffling_phase: Epoch,
pub whisk_proposer_selection_gap: Epoch,

/*
* Whisk
Expand Down Expand Up @@ -332,6 +335,16 @@ impl ChainSpec {
}
}

pub fn whisk_shuffle_round_start_slot<T: EthSpec>(&self, slot: Slot) -> Slot {
let epochs_per_round = self.whisk_epochs_per_shuffling_phase;
slot.epoch(T::slots_per_epoch())
.safe_div(epochs_per_round)
.unwrap_or(Epoch::new(0))
.safe_mul(epochs_per_round)
.expect("not overflow")
.start_slot(T::slots_per_epoch())
}

/// Returns a full `Fork` struct for a given epoch.
pub fn fork_at_epoch(&self, epoch: Epoch) -> Fork {
let current_fork_name = self.fork_name_at_epoch(epoch);
Expand Down Expand Up @@ -633,6 +646,11 @@ impl ChainSpec {
capella_fork_version: [0x03, 00, 00, 00],
capella_fork_epoch: Some(Epoch::new(194048)),
max_validators_per_withdrawals_sweep: 16384,
// TODO WHISK: Reduced values for faster devnet testing
// Should equal CandidateTrackerCount / SlotsPerEpoch.
whisk_epochs_per_shuffling_phase: Epoch::new(8),
// TODO WHISK: Reduced values for faster devnet testing
whisk_proposer_selection_gap: Epoch::new(1),

/*
* Whisk / TODO: Review values
Expand Down Expand Up @@ -712,6 +730,8 @@ impl ChainSpec {
// Capella
capella_fork_version: [0x03, 0x00, 0x00, 0x01],
capella_fork_epoch: None,
whisk_epochs_per_shuffling_phase: Epoch::new(4),
whisk_proposer_selection_gap: Epoch::new(1),
max_validators_per_withdrawals_sweep: 16,
// Other
network_id: 2, // lighthouse testnet network id
Expand Down Expand Up @@ -876,6 +896,8 @@ impl ChainSpec {
/*
* Whisk / TODO: Review values
*/
whisk_epochs_per_shuffling_phase: Epoch::new(256),
whisk_proposer_selection_gap: Epoch::new(2),
domain_whisk_candidate_selection: 0x0107,
domain_whisk_shuffle: 0x0207,
domain_whisk_proposer_selection: 0x0307,
Expand Down
31 changes: 1 addition & 30 deletions consensus/types/src/eth_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use safe_arith::SafeArith;
use serde_derive::{Deserialize, Serialize};
use ssz_types::typenum::U124;
use ssz_types::typenum::{
bit::B0, op, UInt, Unsigned, U0, U1, U1024, U1048576, U1073741824, U1099511627776, U128, U16,
bit::B0, op, UInt, Unsigned, U0, U1024, U1048576, U1073741824, U1099511627776, U128, U16,
U16384, U16777216, U2, U2048, U256, U32, U4, U4096, U48, U480, U512, U625, U64, U65536, U8,
U8192,
};
Expand Down Expand Up @@ -128,9 +128,7 @@ pub trait EthSpec:
*/
type WhiskCandidateTrackersCount: Unsigned + Clone + Sync + Send + Debug + PartialEq;
type WhiskProposerTrackersCount: Unsigned + Clone + Sync + Send + Debug + PartialEq;
type WhiskEpochsPerShufflingPhase: Unsigned + Clone + Sync + Send + Debug + PartialEq;
type WhiskValidatorsPerShuffle: Unsigned + Clone + Sync + Send + Debug + PartialEq;
type WhiskProposerSelectionGap: Unsigned + Clone + Sync + Send + Debug + PartialEq;
type WhiskMaxShuffleProofSize: Unsigned + Clone + Sync + Send + Debug + PartialEq;
type WhiskMaxOpeningProofSize: Unsigned + Clone + Sync + Send + Debug + PartialEq;
// TODO Temp
Expand Down Expand Up @@ -264,27 +262,9 @@ pub trait EthSpec:
Self::WhiskProposerTrackersCount::to_usize()
}

fn whisk_proposer_selection_gap() -> u64 {
Self::WhiskProposerSelectionGap::to_u64()
}

fn whisk_epochs_per_shuffling_phase() -> u64 {
Self::WhiskEpochsPerShufflingPhase::to_u64()
}

fn whisk_validators_per_shuffle() -> usize {
Self::WhiskValidatorsPerShuffle::to_usize()
}

fn whisk_shuffle_round_start_slot(slot: Slot) -> Slot {
let epochs_per_round = Self::whisk_epochs_per_shuffling_phase();
slot.epoch(Self::slots_per_epoch())
.safe_div(epochs_per_round)
.expect("epochs per shuffling phase is not zero")
.safe_mul(epochs_per_round)
.expect("not overflow")
.start_slot(Self::slots_per_epoch())
}
}

/// Macro to inherit some type values from another EthSpec.
Expand Down Expand Up @@ -333,13 +313,8 @@ impl EthSpec for MainnetEthSpec {
type WhiskCandidateTrackersCount = U256;
// TODO WHISK: Reduced value for devnet testing. Should equal CandidateCount / 2
type WhiskProposerTrackersCount = U128;
// TODO WHISK: Reduced value for faster devnet testing.
// Should equal CandidateTrackerCount / SlotsPerEpoch.
type WhiskEpochsPerShufflingPhase = U8;
// Actual count of shuffled trackers is `ELL = N - N_BLINDERS`
type WhiskValidatorsPerShuffle = U124;
// TODO WHISK: Reduced value faster devnet testing.
type WhiskProposerSelectionGap = U1;
type WhiskMaxShuffleProofSize = U4576;
type WhiskMaxOpeningProofSize = U128;
type BytesPerG1Point = U48;
Expand Down Expand Up @@ -370,9 +345,7 @@ impl EthSpec for MinimalEthSpec {
type MaxWithdrawalsPerPayload = U4;
type WhiskCandidateTrackersCount = U256;
type WhiskProposerTrackersCount = U128;
type WhiskEpochsPerShufflingPhase = U4;
type WhiskValidatorsPerShuffle = U124;
type WhiskProposerSelectionGap = U1;
type WhiskMaxShuffleProofSize = U4576;
type WhiskMaxOpeningProofSize = U128;
type BytesPerG1Point = U48;
Expand Down Expand Up @@ -444,9 +417,7 @@ impl EthSpec for GnosisEthSpec {
type MaxWithdrawalsPerPayload = U8;
type WhiskCandidateTrackersCount = U16384;
type WhiskProposerTrackersCount = U8192;
type WhiskEpochsPerShufflingPhase = U256;
type WhiskValidatorsPerShuffle = U124;
type WhiskProposerSelectionGap = U2;
type WhiskMaxShuffleProofSize = U4576;
type WhiskMaxOpeningProofSize = U128;
type BytesPerG1Point = U48;
Expand Down

0 comments on commit 5122df4

Please sign in to comment.