Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: pectra support #395

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
31 changes: 19 additions & 12 deletions ethereum/consensus-core/src/consensus_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,19 @@
checkpoint: B256,
forks: &Forks,
) -> Result<()> {
if !is_valid_header::<S>(&bootstrap.header, forks) {
if !is_valid_header::<S>(&bootstrap.header(), forks) {

Check failure on line 34 in ethereum/consensus-core/src/consensus_core.rs

View workflow job for this annotation

GitHub Actions / clippy

this expression creates a reference which is immediately dereferenced by the compiler
return Err(ConsensusError::InvalidExecutionPayloadProof.into());
}

let committee_valid = is_current_committee_proof_valid(
&bootstrap.header.beacon(),
&bootstrap.current_sync_committee,
&bootstrap.current_sync_committee_branch,
bootstrap.header().beacon(),
bootstrap.current_sync_committee(),
bootstrap.current_sync_committee_branch(),
bootstrap.header().beacon().slot / S::slots_per_epoch(),
forks,
);

let header_hash = bootstrap.header.beacon().tree_hash_root();
let header_hash = bootstrap.header().beacon().tree_hash_root();
let header_valid = header_hash == checkpoint;

if !header_valid {
Expand Down Expand Up @@ -93,10 +95,10 @@
bootstrap: &Bootstrap<S>,
) {
*store = LightClientStore {
finalized_header: bootstrap.header.clone(),
current_sync_committee: bootstrap.current_sync_committee.clone(),
finalized_header: bootstrap.header().clone(),
current_sync_committee: bootstrap.current_sync_committee().clone(),
next_sync_committee: None,
optimistic_header: bootstrap.header.clone(),
optimistic_header: bootstrap.header().clone(),
previous_max_active_participants: 0,
current_max_active_participants: 0,
best_valid_update: None,
Expand Down Expand Up @@ -138,7 +140,7 @@

// update best valid update
if store.best_valid_update.is_none()
|| is_better_update(update, &store.best_valid_update.as_ref().unwrap())

Check failure on line 143 in ethereum/consensus-core/src/consensus_core.rs

View workflow job for this annotation

GitHub Actions / clippy

this expression creates a reference which is immediately dereferenced by the compiler
{
store.best_valid_update = Some(update.clone());
}
Expand Down Expand Up @@ -296,9 +298,11 @@
}

let is_valid = is_finality_proof_valid(
&update.attested_header.beacon(),
&finalized_header.beacon(),
update.attested_header.beacon(),
finalized_header.beacon(),
finality_branch,
update.attested_header.beacon().slot / S::slots_per_epoch(),
forks,
);

if !is_valid {
Expand All @@ -312,9 +316,11 @@
if let Some(next_sync_committee) = &update.next_sync_committee {
if let Some(next_sync_committee_branch) = &update.next_sync_committee_branch {
let is_valid = is_next_committee_proof_valid(
&update.attested_header.beacon(),
update.attested_header.beacon(),
next_sync_committee,
next_sync_committee_branch,
update.attested_header.beacon().slot / S::slots_per_epoch(),
forks,
);

if !is_valid {
Expand All @@ -337,7 +343,7 @@
let fork_data_root = compute_fork_data_root(fork_version, genesis_root);
let is_valid_sig = verify_sync_committee_signture(
&pks,
&update.attested_header.beacon(),

Check failure on line 346 in ethereum/consensus-core/src/consensus_core.rs

View workflow job for this annotation

GitHub Actions / clippy

this expression creates a reference which is immediately dereferenced by the compiler
&update.sync_aggregate.sync_committee_signature,
fork_data_root,
);
Expand Down Expand Up @@ -488,6 +494,7 @@
let execution_branch = header.execution_branch().unwrap();

let valid_execution_type = match execution {
ExecutionPayloadHeader::Electra(_) => epoch >= forks.electra.epoch,
ExecutionPayloadHeader::Deneb(_) => epoch >= forks.deneb.epoch,
ExecutionPayloadHeader::Capella(_) => {
epoch >= forks.capella.epoch && epoch < forks.deneb.epoch
Expand All @@ -498,7 +505,7 @@
};

let proof_valid =
is_execution_payload_proof_valid(&header.beacon(), execution, execution_branch);
is_execution_payload_proof_valid(header.beacon(), execution, execution_branch);

proof_valid && valid_execution_type
} else {
Expand Down
21 changes: 21 additions & 0 deletions ethereum/consensus-core/src/consensus_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ use typenum::Unsigned;
pub trait ConsensusSpec: 'static + Default + Sync + Send + Clone + Debug + PartialEq {
type MaxProposerSlashings: Unsigned + Default + Debug + Sync + Send + Clone + PartialEq;
type MaxAttesterSlashings: Unsigned + Default + Debug + Sync + Send + Clone + PartialEq;
type MaxAttesterSlashingsElectra: Unsigned + Default + Debug + Sync + Send + Clone + PartialEq;
type MaxAttestations: Unsigned + Default + Debug + Sync + Send + Clone + PartialEq;
type MaxAttestationsElectra: Unsigned + Default + Debug + Sync + Send + Clone + PartialEq;
type MaxValidatorsPerSlot: Unsigned + Default + Debug + Sync + Send + Clone + PartialEq;
type MaxCommitteesPerSlot: Unsigned + Default + Debug + Sync + Send + Clone + PartialEq;
type MaxDeposits: Unsigned + Default + Debug + Sync + Send + Clone + PartialEq;
type MaxVoluntaryExits: Unsigned + Default + Debug + Sync + Send + Clone + PartialEq;
type MaxBlsToExecutionChanged: Unsigned + Default + Debug + Sync + Send + Clone + PartialEq;
Expand All @@ -16,6 +20,9 @@ pub trait ConsensusSpec: 'static + Default + Sync + Send + Clone + Debug + Parti
type SlotsPerEpoch: Unsigned + Default + Debug + Sync + Send + Clone + PartialEq;
type EpochsPerSyncCommiteePeriod: Unsigned + Default + Debug + Sync + Send + Clone + PartialEq;
type SyncCommitteeSize: Unsigned + Default + Debug + Sync + Send + Clone + PartialEq;
type MaxWithdrawalRequests: Unsigned + Default + Debug + Sync + Send + Clone + PartialEq;
type MaxDepositRequests: Unsigned + Default + Debug + Sync + Send + Clone + PartialEq;
type MaxConsolidationRequests: Unsigned + Default + Debug + Sync + Send + Clone + PartialEq;

fn slots_per_epoch() -> u64 {
Self::SlotsPerEpoch::to_u64()
Expand All @@ -40,7 +47,11 @@ pub struct MainnetConsensusSpec;
impl ConsensusSpec for MainnetConsensusSpec {
type MaxProposerSlashings = typenum::U16;
type MaxAttesterSlashings = typenum::U2;
type MaxAttesterSlashingsElectra = typenum::U1;
type MaxAttestations = typenum::U128;
type MaxAttestationsElectra = typenum::U8;
type MaxCommitteesPerSlot = typenum::U64;
type MaxValidatorsPerSlot = typenum::U131072;
type MaxDeposits = typenum::U16;
type MaxVoluntaryExits = typenum::U16;
type MaxBlsToExecutionChanged = typenum::U16;
Expand All @@ -50,6 +61,9 @@ impl ConsensusSpec for MainnetConsensusSpec {
type SlotsPerEpoch = typenum::U32;
type EpochsPerSyncCommiteePeriod = typenum::U256;
type SyncCommitteeSize = typenum::U512;
type MaxDepositRequests = typenum::U8192;
type MaxWithdrawalRequests = typenum::U16;
type MaxConsolidationRequests = typenum::U1;
}

#[derive(Serialize, Deserialize, Default, Clone, Debug, PartialEq)]
Expand All @@ -58,7 +72,11 @@ pub struct MinimalConsensusSpec;
impl ConsensusSpec for MinimalConsensusSpec {
type MaxProposerSlashings = typenum::U16;
type MaxAttesterSlashings = typenum::U2;
type MaxAttesterSlashingsElectra = typenum::U1;
type MaxAttestations = typenum::U128;
type MaxAttestationsElectra = typenum::U8;
type MaxCommitteesPerSlot = typenum::U4;
type MaxValidatorsPerSlot = typenum::U8192;
type MaxDeposits = typenum::U16;
type MaxVoluntaryExits = typenum::U16;
type MaxBlsToExecutionChanged = typenum::U16;
Expand All @@ -68,4 +86,7 @@ impl ConsensusSpec for MinimalConsensusSpec {
type SlotsPerEpoch = typenum::U8;
type EpochsPerSyncCommiteePeriod = typenum::U8;
type SyncCommitteeSize = typenum::U32;
type MaxDepositRequests = typenum::U4;
type MaxWithdrawalRequests = typenum::U2;
type MaxConsolidationRequests = typenum::U1;
}
38 changes: 31 additions & 7 deletions ethereum/consensus-core/src/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,48 +4,72 @@ use tree_hash::TreeHash;

use crate::{
consensus_spec::ConsensusSpec,
types::{BeaconBlockHeader, ExecutionPayloadHeader, SyncCommittee},
types::{BeaconBlockHeader, ExecutionPayloadHeader, Forks, SyncCommittee},
};

pub fn is_finality_proof_valid(
attested_header: &BeaconBlockHeader,
finality_header: &BeaconBlockHeader,
finality_branch: &[B256],
current_epoch: u64,
forks: &Forks,
) -> bool {
let (index, depth) = if current_epoch >= forks.electra.epoch {
(41, 7)
} else {
(41, 6)
};

is_proof_valid(
attested_header.state_root,
finality_header,
finality_branch,
6,
41,
depth,
index,
)
}

pub fn is_next_committee_proof_valid<S: ConsensusSpec>(
attested_header: &BeaconBlockHeader,
next_committee: &SyncCommittee<S>,
next_committee_branch: &[B256],
current_epoch: u64,
forks: &Forks,
) -> bool {
let (index, depth) = if current_epoch >= forks.electra.epoch {
(23, 6)
} else {
(23, 5)
};

is_proof_valid(
attested_header.state_root,
next_committee,
next_committee_branch,
5,
23,
depth,
index,
)
}

pub fn is_current_committee_proof_valid<S: ConsensusSpec>(
attested_header: &BeaconBlockHeader,
current_committee: &SyncCommittee<S>,
current_committee_branch: &[B256],
current_epoch: u64,
forks: &Forks,
) -> bool {
let (index, depth) = if current_epoch >= forks.electra.epoch {
(22, 6)
} else {
(22, 5)
};

is_proof_valid(
attested_header.state_root,
current_committee,
current_committee_branch,
5,
22,
depth,
index,
)
}

Expand Down
Loading
Loading