Skip to content

Commit

Permalink
Merge pull request #6 from datachainlab/deneb
Browse files Browse the repository at this point in the history
Deneb support

Signed-off-by: Jun Kimura <[email protected]>
  • Loading branch information
bluele authored Jan 15, 2024
2 parents d0caaa0 + da939e8 commit 186c5b1
Show file tree
Hide file tree
Showing 12 changed files with 739 additions and 106 deletions.
29 changes: 28 additions & 1 deletion crates/consensus/src/beacon.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
bls::{PublicKey, Signature},
internal_prelude::*,
types::{serde_hex, Bytes32, H256, U64},
types::{serde_hex, Address, Bytes32, H256, U64},
};
use ssz_rs::{Bitlist, Deserialize, List, Sized, Vector};
use ssz_rs_derive::SimpleSerialize;
Expand Down Expand Up @@ -194,3 +194,30 @@ pub struct VoluntaryExit {
pub epoch: Epoch,
pub validator_index: ValidatorIndex,
}

#[derive(
Clone, Debug, PartialEq, Eq, Default, SimpleSerialize, serde::Serialize, serde::Deserialize,
)]
pub struct Withdrawal {
pub index: U64,
pub validator_index: ValidatorIndex,
pub address: Address,
pub amount: Gwei,
}

#[derive(
Clone, Debug, PartialEq, Eq, Default, SimpleSerialize, serde::Serialize, serde::Deserialize,
)]
pub struct BlsToExecutionChange {
pub validator_index: ValidatorIndex,
pub from_bls_public_key: PublicKey,
pub to_execution_address: Address,
}

#[derive(
Clone, Debug, PartialEq, Eq, Default, SimpleSerialize, serde::Serialize, serde::Deserialize,
)]
pub struct SignedBlsToExecutionChange {
message: BlsToExecutionChange,
signature: Signature,
}
79 changes: 43 additions & 36 deletions crates/consensus/src/bellatrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::{
errors::Error,
execution::BlockNumber,
internal_prelude::*,
merkle::MerkleTree,
sync_protocol::{
SyncAggregate, SyncCommittee, CURRENT_SYNC_COMMITTEE_DEPTH, EXECUTION_PAYLOAD_DEPTH,
FINALIZED_ROOT_DEPTH, NEXT_SYNC_COMMITTEE_DEPTH,
Expand Down Expand Up @@ -291,24 +292,27 @@ pub fn gen_execution_payload_proof<
SYNC_COMMITTEE_SIZE,
>,
) -> Result<(Root, [H256; EXECUTION_PAYLOAD_DEPTH]), Error> {
let tree = rs_merkle::MerkleTree::<rs_merkle::algorithms::Sha256>::from_leaves(&[
hash_tree_root(body.randao_reveal.clone()).unwrap().0,
hash_tree_root(body.eth1_data.clone()).unwrap().0,
body.graffiti.0,
hash_tree_root(body.proposer_slashings.clone()).unwrap().0,
hash_tree_root(body.attester_slashings.clone()).unwrap().0,
hash_tree_root(body.attestations.clone()).unwrap().0,
hash_tree_root(body.deposits.clone()).unwrap().0,
hash_tree_root(body.voluntary_exits.clone()).unwrap().0,
hash_tree_root(body.sync_aggregate.clone()).unwrap().0,
hash_tree_root(body.execution_payload.clone()).unwrap().0,
Default::default(),
Default::default(),
Default::default(),
Default::default(),
Default::default(),
Default::default(),
]);
let tree = MerkleTree::from_leaves(
([
hash_tree_root(body.randao_reveal.clone()).unwrap().0,
hash_tree_root(body.eth1_data.clone()).unwrap().0,
body.graffiti.0,
hash_tree_root(body.proposer_slashings.clone()).unwrap().0,
hash_tree_root(body.attester_slashings.clone()).unwrap().0,
hash_tree_root(body.attestations.clone()).unwrap().0,
hash_tree_root(body.deposits.clone()).unwrap().0,
hash_tree_root(body.voluntary_exits.clone()).unwrap().0,
hash_tree_root(body.sync_aggregate.clone()).unwrap().0,
hash_tree_root(body.execution_payload.clone()).unwrap().0,
Default::default(),
Default::default(),
Default::default(),
Default::default(),
Default::default(),
Default::default(),
] as [_; 16])
.as_ref(),
);
let mut branch = [Default::default(); EXECUTION_PAYLOAD_DEPTH];
branch.copy_from_slice(
tree.proof(&[BLOCK_BODY_EXECUTION_PAYLOAD_LEAF_INDEX])
Expand All @@ -328,24 +332,27 @@ pub fn gen_execution_payload_field_proof<
payload: &ExecutionPayloadHeader<BYTES_PER_LOGS_BLOOM, MAX_EXTRA_DATA_BYTES>,
leaf_index: usize,
) -> Result<(Root, [H256; EXECUTION_PAYLOAD_TREE_DEPTH]), Error> {
let tree = rs_merkle::MerkleTree::<rs_merkle::algorithms::Sha256>::from_leaves(&[
payload.parent_hash.0,
hash_tree_root(payload.fee_recipient.clone()).unwrap().0,
payload.state_root.0,
payload.receipts_root.0,
hash_tree_root(payload.logs_bloom.clone()).unwrap().0,
payload.prev_randao.0,
hash_tree_root(payload.block_number).unwrap().0,
hash_tree_root(payload.gas_limit).unwrap().0,
hash_tree_root(payload.gas_used).unwrap().0,
hash_tree_root(payload.timestamp).unwrap().0,
hash_tree_root(payload.extra_data.clone()).unwrap().0,
hash_tree_root(payload.base_fee_per_gas.clone()).unwrap().0,
payload.block_hash.0,
payload.transactions_root.0,
Default::default(),
Default::default(),
]);
let tree = MerkleTree::from_leaves(
([
payload.parent_hash.0,
hash_tree_root(payload.fee_recipient.clone()).unwrap().0,
payload.state_root.0,
payload.receipts_root.0,
hash_tree_root(payload.logs_bloom.clone()).unwrap().0,
payload.prev_randao.0,
hash_tree_root(payload.block_number).unwrap().0,
hash_tree_root(payload.gas_limit).unwrap().0,
hash_tree_root(payload.gas_used).unwrap().0,
hash_tree_root(payload.timestamp).unwrap().0,
hash_tree_root(payload.extra_data.clone()).unwrap().0,
hash_tree_root(payload.base_fee_per_gas.clone()).unwrap().0,
payload.block_hash.0,
payload.transactions_root.0,
Default::default(),
Default::default(),
] as [_; 16])
.as_ref(),
);
let mut branch = [Default::default(); EXECUTION_PAYLOAD_TREE_DEPTH];
branch.copy_from_slice(
tree.proof(&[leaf_index])
Expand Down
116 changes: 48 additions & 68 deletions crates/consensus/src/capella.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
use crate::{
beacon::{
Attestation, AttesterSlashing, BeaconBlockHeader, Deposit, Eth1Data, Gwei,
ProposerSlashing, Root, SignedVoluntaryExit, Slot, ValidatorIndex,
Attestation, AttesterSlashing, BeaconBlockHeader, Deposit, Eth1Data, ProposerSlashing,
Root, SignedBlsToExecutionChange, SignedVoluntaryExit, Slot, ValidatorIndex, Withdrawal,
BLOCK_BODY_EXECUTION_PAYLOAD_LEAF_INDEX,
},
bellatrix::EXECUTION_PAYLOAD_TREE_DEPTH,
bls::{PublicKey, Signature},
bls::Signature,
compute::hash_tree_root,
errors::Error,
execution::BlockNumber,
internal_prelude::*,
merkle::MerkleTree,
sync_protocol::{
SyncAggregate, SyncCommittee, CURRENT_SYNC_COMMITTEE_DEPTH, EXECUTION_PAYLOAD_DEPTH,
FINALIZED_ROOT_DEPTH, NEXT_SYNC_COMMITTEE_DEPTH,
Expand Down Expand Up @@ -147,23 +148,6 @@ pub struct BeaconBlockBody<
pub bls_to_execution_changes: List<SignedBlsToExecutionChange, MAX_BLS_TO_EXECUTION_CHANGES>,
}

#[derive(
Clone, Debug, PartialEq, Eq, Default, SimpleSerialize, serde::Serialize, serde::Deserialize,
)]
pub struct BlsToExecutionChange {
pub validator_index: ValidatorIndex,
pub from_bls_public_key: PublicKey,
pub to_execution_address: Address,
}

#[derive(
Clone, Debug, PartialEq, Eq, Default, SimpleSerialize, serde::Serialize, serde::Deserialize,
)]
pub struct SignedBlsToExecutionChange {
message: BlsToExecutionChange,
signature: Signature,
}

// Execution

/// https://github.com/ethereum/consensus-specs/blob/dev/specs/bellatrix/beacon-chain.md#executionpayload
Expand Down Expand Up @@ -271,16 +255,6 @@ pub struct ExecutionPayloadHeader<
pub withdrawals_root: Root,
}

#[derive(
Clone, Debug, PartialEq, Eq, Default, SimpleSerialize, serde::Serialize, serde::Deserialize,
)]
pub struct Withdrawal {
pub index: U64,
pub validator_index: ValidatorIndex,
pub address: Address,
pub amount: Gwei,
}

/// https://github.com/ethereum/consensus-specs/blob/dev/specs/altair/light-client/sync-protocol.md#lightclientbootstrap
#[derive(Clone, Debug, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct LightClientBootstrap<
Expand Down Expand Up @@ -359,26 +333,29 @@ pub fn gen_execution_payload_proof<
SYNC_COMMITTEE_SIZE,
>,
) -> Result<(Root, [H256; EXECUTION_PAYLOAD_DEPTH]), Error> {
let tree = rs_merkle::MerkleTree::<rs_merkle::algorithms::Sha256>::from_leaves(&[
hash_tree_root(body.randao_reveal.clone()).unwrap().0,
hash_tree_root(body.eth1_data.clone()).unwrap().0,
body.graffiti.0,
hash_tree_root(body.proposer_slashings.clone()).unwrap().0,
hash_tree_root(body.attester_slashings.clone()).unwrap().0,
hash_tree_root(body.attestations.clone()).unwrap().0,
hash_tree_root(body.deposits.clone()).unwrap().0,
hash_tree_root(body.voluntary_exits.clone()).unwrap().0,
hash_tree_root(body.sync_aggregate.clone()).unwrap().0,
hash_tree_root(body.execution_payload.clone()).unwrap().0,
hash_tree_root(body.bls_to_execution_changes.clone())
.unwrap()
.0,
Default::default(),
Default::default(),
Default::default(),
Default::default(),
Default::default(),
]);
let tree = MerkleTree::from_leaves(
([
hash_tree_root(body.randao_reveal.clone()).unwrap().0,
hash_tree_root(body.eth1_data.clone()).unwrap().0,
body.graffiti.0,
hash_tree_root(body.proposer_slashings.clone()).unwrap().0,
hash_tree_root(body.attester_slashings.clone()).unwrap().0,
hash_tree_root(body.attestations.clone()).unwrap().0,
hash_tree_root(body.deposits.clone()).unwrap().0,
hash_tree_root(body.voluntary_exits.clone()).unwrap().0,
hash_tree_root(body.sync_aggregate.clone()).unwrap().0,
hash_tree_root(body.execution_payload.clone()).unwrap().0,
hash_tree_root(body.bls_to_execution_changes.clone())
.unwrap()
.0,
Default::default(),
Default::default(),
Default::default(),
Default::default(),
Default::default(),
] as [_; 16])
.as_ref(),
);
let mut branch = [Default::default(); EXECUTION_PAYLOAD_DEPTH];
branch.copy_from_slice(
tree.proof(&[BLOCK_BODY_EXECUTION_PAYLOAD_LEAF_INDEX])
Expand All @@ -398,24 +375,27 @@ pub fn gen_execution_payload_field_proof<
payload: &ExecutionPayloadHeader<BYTES_PER_LOGS_BLOOM, MAX_EXTRA_DATA_BYTES>,
leaf_index: usize,
) -> Result<(Root, [H256; EXECUTION_PAYLOAD_TREE_DEPTH]), Error> {
let tree = rs_merkle::MerkleTree::<rs_merkle::algorithms::Sha256>::from_leaves(&[
payload.parent_hash.0,
hash_tree_root(payload.fee_recipient.clone()).unwrap().0,
payload.state_root.0,
payload.receipts_root.0,
hash_tree_root(payload.logs_bloom.clone()).unwrap().0,
payload.prev_randao.0,
hash_tree_root(payload.block_number).unwrap().0,
hash_tree_root(payload.gas_limit).unwrap().0,
hash_tree_root(payload.gas_used).unwrap().0,
hash_tree_root(payload.timestamp).unwrap().0,
hash_tree_root(payload.extra_data.clone()).unwrap().0,
hash_tree_root(payload.base_fee_per_gas.clone()).unwrap().0,
payload.block_hash.0,
payload.transactions_root.0,
payload.withdrawals_root.0,
Default::default(),
]);
let tree = MerkleTree::from_leaves(
([
payload.parent_hash.0,
hash_tree_root(payload.fee_recipient.clone()).unwrap().0,
payload.state_root.0,
payload.receipts_root.0,
hash_tree_root(payload.logs_bloom.clone()).unwrap().0,
payload.prev_randao.0,
hash_tree_root(payload.block_number).unwrap().0,
hash_tree_root(payload.gas_limit).unwrap().0,
hash_tree_root(payload.gas_used).unwrap().0,
hash_tree_root(payload.timestamp).unwrap().0,
hash_tree_root(payload.extra_data.clone()).unwrap().0,
hash_tree_root(payload.base_fee_per_gas.clone()).unwrap().0,
payload.block_hash.0,
payload.transactions_root.0,
payload.withdrawals_root.0,
Default::default(),
] as [_; 16])
.as_ref(),
);
let mut branch = [Default::default(); EXECUTION_PAYLOAD_TREE_DEPTH];
branch.copy_from_slice(
tree.proof(&[leaf_index])
Expand Down
6 changes: 5 additions & 1 deletion crates/consensus/src/config/minimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@ pub fn get_config() -> Config {
fork_parameters: ForkParameters::new(
Version([0, 0, 0, 1]),
vec![
ForkParameter::new(Version([4, 0, 0, 1]), U64(u64::MAX)),
// deneb
ForkParameter::new(Version([4, 0, 0, 1]), U64(0)),
// capella
ForkParameter::new(Version([3, 0, 0, 1]), U64(0)),
// belatrix
ForkParameter::new(Version([2, 0, 0, 1]), U64(0)),
// altair
ForkParameter::new(Version([1, 0, 0, 1]), U64(0)),
],
),
Expand Down
Loading

0 comments on commit 186c5b1

Please sign in to comment.