Skip to content

Commit

Permalink
Update with VersionedExecutionPayloadHeader
Browse files Browse the repository at this point in the history
  • Loading branch information
yrong committed Nov 22, 2023
1 parent 48cc44f commit fe1e191
Show file tree
Hide file tree
Showing 15 changed files with 4,402 additions and 21 deletions.
2 changes: 2 additions & 0 deletions go.work.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1064,6 +1064,7 @@ github.com/spf13/afero v1.3.3/go.mod h1:5KUK8ByomD5Ti5Artl0RtHeI5pTF7MIDuXL3yY52
github.com/spf13/afero v1.6.0/go.mod h1:Ai8FlHk4v/PARR026UzYexafAt9roJ7LcLMAmO6Z93I=
github.com/spf13/cobra v1.0.0/go.mod h1:/6GTrnGXV9HjY+aR4k0oJ5tcvakLuG6EuKReYlHNrgE=
github.com/spf13/cobra v1.5.0/go.mod h1:dWXEIy2H428czQCjInthrTRUg7yKbok+2Qi/yBIJoUM=
github.com/spf13/jwalterweatherman v1.0.0 h1:XHEdyB+EcvlqZamSM4ZOMGlc93t6AcsBEu9Gc1vn7yk=
github.com/spf13/viper v1.4.0/go.mod h1:PTJ7Z/lr49W6bUbkmS1V3by4uWynFiR9p7+dSq/yZzE=
github.com/steakknife/bloomfilter v0.0.0-20180922174646-6819c0d2a570 h1:gIlAHnH1vJb5vwEjIp5kBj/eu99p/bl0Ay2goiPe5xE=
github.com/steakknife/hamming v0.0.0-20180906055917-c99c65617cd3 h1:njlZPzLwU639dk2kqnCPPv+wNjq7Xb6EfUxe/oX0/NM=
Expand Down Expand Up @@ -1145,6 +1146,7 @@ go.opentelemetry.io/proto/otlp v0.19.0/go.mod h1:H7XAot3MsfNsj7EXtrA2q5xSNQ10UqI
go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4=
go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{CheckpointUpdate, ExecutionHeaderUpdate, Update};
use hex_literal::hex;
use primitives::{
updates::AncestryProof, BeaconHeader, ExecutionPayloadHeader, NextSyncCommitteeUpdate,
SyncAggregate, SyncCommittee,
SyncAggregate, SyncCommittee, VersionedExecutionPayloadHeader,
};
use sp_core::U256;
use sp_std::{boxed::Box, vec};
Expand Down Expand Up @@ -1188,7 +1188,7 @@ pub fn make_execution_header_update() -> Box<ExecutionHeaderUpdate> {
],
finalized_block_root: hex!("f6e721e4e65d9565091a557705285ec6db0a3a3072317317719ec8ad563859a3").into(),
}),
execution_header: ExecutionPayloadHeader {
execution_header: VersionedExecutionPayloadHeader::Capella(ExecutionPayloadHeader {
parent_hash: hex!("6d51d7c94763813ffefa234097a51c6fd7009424d2991695f7bd6203157c86f9").into(),
fee_recipient: hex!("000095e79eac4d76aab57cb2c1f091d553b36ca0").into(),
state_root: hex!("fe9f753520a7b5c0263bbf4fdba728f69e9cf861ce1883aa13de5da30ff75d74").into(),
Expand All @@ -1204,7 +1204,7 @@ pub fn make_execution_header_update() -> Box<ExecutionHeaderUpdate> {
block_hash: hex!("e4a67cdb1512f29ad9b331e7a37cf8e376222eafa58e72cee7771ad582cc0610").into(),
transactions_root: hex!("bd7eaeb676c14c37bbf0b6f3db2ce021a04a41dbf002f6c7df3bb61639ac7287").into(),
withdrawals_root: hex!("8647d3ecaaf62e1d087c5ab54a23f1d64f477b7ddd16fff458847181d89fc432").into(),
},
}),
execution_branch: vec![
hex!("795608ac1294bcc663127b8428513ba4a5ffe952ff72f8322dca23628f13d716").into(),
hex!("336488033fe5f3ef4ccc12af07b9370b92e553e35ecb4a337a1b1c0e4afe1e0e").into(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ mod benchmarks {
let checkpoint_update = make_checkpoint();
let finalized_header_update = make_finalized_header_update();
let execution_header_update = make_execution_header_update();
let execution_header_hash = execution_header_update.execution_header.block_hash;
let execution_header_hash = execution_header_update.execution_header.block_hash();
EthereumBeaconClient::<T>::process_checkpoint_update(&checkpoint_update)?;
EthereumBeaconClient::<T>::process_update(&finalized_header_update)?;

Expand Down
8 changes: 5 additions & 3 deletions parachain/pallets/ethereum-beacon-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -549,10 +549,12 @@ pub mod pallet {
);

// Checks that we don't skip execution headers, they need to be imported sequentially.
let compact_execution_header: CompactExecutionHeader =
update.execution_header.clone().into();
let latest_execution_state: ExecutionHeaderState = Self::latest_execution_state();
ensure!(
latest_execution_state.block_number == 0 ||
update.execution_header.block_number ==
compact_execution_header.block_number ==
latest_execution_state.block_number + 1,
Error::<T>::ExecutionHeaderSkippedBlock
);
Expand Down Expand Up @@ -603,8 +605,8 @@ pub mod pallet {
}

Self::store_execution_header(
update.execution_header.block_hash,
update.execution_header.clone().into(),
update.execution_header.block_hash(),
compact_execution_header,
update.header.slot,
block_root,
);
Expand Down
1 change: 1 addition & 0 deletions parachain/primitives/beacon/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub use types::{
BeaconHeader, CompactBeaconState, CompactExecutionHeader, ExecutionHeaderState,
ExecutionPayloadHeader, FinalizedHeaderState, Fork, ForkData, ForkVersion, ForkVersions, Mode,
PublicKey, Signature, SigningData, SyncAggregate, SyncCommittee, SyncCommitteePrepared,
VersionedExecutionPayloadHeader,
};
pub use updates::{CheckpointUpdate, ExecutionHeaderUpdate, NextSyncCommitteeUpdate, Update};

Expand Down
42 changes: 41 additions & 1 deletion parachain/primitives/beacon/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ impl<const COMMITTEE_SIZE: usize, const COMMITTEE_BITS_SIZE: usize>
)]
#[cfg_attr(
feature = "std",
derive(Deserialize),
derive(Serialize, Deserialize),
serde(deny_unknown_fields, bound(serialize = ""), bound(deserialize = ""))
)]
#[codec(mel_bound())]
Expand Down Expand Up @@ -386,6 +386,46 @@ pub struct CompactBeaconState {
pub block_roots_root: H256,
}

/// VersionedExecutionPayloadHeader
#[derive(Encode, Decode, CloneNoBound, PartialEqNoBound, RuntimeDebugNoBound, TypeInfo)]
#[cfg_attr(
feature = "std",
derive(Serialize, Deserialize),
serde(deny_unknown_fields, bound(serialize = ""), bound(deserialize = ""))
)]
#[codec(mel_bound())]
pub enum VersionedExecutionPayloadHeader {
Capella(ExecutionPayloadHeader),
}

/// Convert VersionedExecutionPayloadHeader to CompactExecutionHeader
impl From<VersionedExecutionPayloadHeader> for CompactExecutionHeader {
fn from(versioned_execution_header: VersionedExecutionPayloadHeader) -> Self {
match versioned_execution_header {
VersionedExecutionPayloadHeader::Capella(execution_payload_header) =>
execution_payload_header.into(),
}
}
}

impl VersionedExecutionPayloadHeader {
pub fn hash_tree_root(&self) -> Result<H256, SimpleSerializeError> {
match self {
VersionedExecutionPayloadHeader::Capella(execution_payload_header) =>
hash_tree_root::<SSZExecutionPayloadHeader>(
execution_payload_header.clone().try_into()?,
),
}
}

pub fn block_hash(&self) -> H256 {
match self {
VersionedExecutionPayloadHeader::Capella(execution_payload_header) =>
execution_payload_header.block_hash,
}
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
4 changes: 2 additions & 2 deletions parachain/primitives/beacon/src/updates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use scale_info::TypeInfo;
use sp_core::H256;
use sp_std::prelude::*;

use crate::types::{BeaconHeader, ExecutionPayloadHeader, SyncAggregate, SyncCommittee};
use crate::types::{BeaconHeader, SyncAggregate, SyncCommittee, VersionedExecutionPayloadHeader};

#[derive(Encode, Decode, CloneNoBound, PartialEqNoBound, RuntimeDebugNoBound, TypeInfo)]
#[cfg_attr(
Expand Down Expand Up @@ -91,7 +91,7 @@ pub struct ExecutionHeaderUpdate {
/// Proof that `header` is an ancestor of a finalized header
pub ancestry_proof: Option<AncestryProof>,
/// Execution header to be imported
pub execution_header: ExecutionPayloadHeader,
pub execution_header: VersionedExecutionPayloadHeader,
/// Merkle proof that execution payload is contained within `header`
pub execution_branch: Vec<H256>,
}
Expand Down
2 changes: 1 addition & 1 deletion relayer/magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func Build() {
}

func BuildMain() error {
err := sh.Run("sszgen", "--path", "relays/beacon/state/beacon.go", "--objs", "BeaconStateCapellaMinimal,BeaconStateCapellaMainnet,BlockRootsContainerMainnet,BlockRootsContainerMinimal,TransactionsRootContainer,BeaconBlockCapellaMinimal,BeaconBlockCapellaMainnet,WithdrawalsRootContainerMinimal,WithdrawalsRootContainerMainnet")
err := sh.Run("sszgen", "--path", "relays/beacon/state", "--objs", "BeaconStateCapellaMinimal,BeaconStateCapellaMainnet,BlockRootsContainerMainnet,BlockRootsContainerMinimal,TransactionsRootContainer,BeaconBlockCapellaMinimal,BeaconBlockCapellaMainnet,WithdrawalsRootContainerMinimal,WithdrawalsRootContainerMainnet,BeaconStateDenebMinimal,BeaconStateDenebMainnet,BeaconBlockDenebMinimal,BeaconBlockDenebMainnet")
if err != nil {
return err
}
Expand Down
18 changes: 14 additions & 4 deletions relayer/relays/beacon/header/syncer/json/beacon_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,10 @@ type BlockBody struct {
}

type HeaderUpdate struct {
Header BeaconHeader `json:"header"`
AncestryProof *AncestryProof `json:"ancestry_proof"`
ExecutionHeader ExecutionPayloadHeaderCapella `json:"execution_header"`
ExecutionBranch []string `json:"execution_branch"`
Header BeaconHeader `json:"header"`
AncestryProof *AncestryProof `json:"ancestry_proof"`
ExecutionHeader VersionedExecutionPayloadHeader `json:"execution_header"`
ExecutionBranch []string `json:"execution_branch"`
}

type AncestryProof struct {
Expand Down Expand Up @@ -312,3 +312,13 @@ func removeLeadingZeroHashForSlice(s []string) []string {
func removeLeadingZeroHash(s string) string {
return strings.Replace(s, "0x", "", 1)
}

type VersionedExecutionPayloadHeader struct {
Capella *ExecutionPayloadHeaderCapella `json:"Capella"`
}

func (v *VersionedExecutionPayloadHeader) RemoveLeadingZeroHashes() {
if v.Capella != nil {
v.Capella.RemoveLeadingZeroHashes()
}
}
15 changes: 14 additions & 1 deletion relayer/relays/beacon/header/syncer/scale/beacon_scale.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (o *OptionNextSyncCommitteeUpdatePayload) Decode(decoder scale.Decoder) err
type HeaderUpdatePayload struct {
Header BeaconHeader
AncestryProof OptionAncestryProof
ExecutionHeader ExecutionPayloadHeaderCapella
ExecutionHeader VersionedExecutionPayloadHeader
ExecutionBranch []types.H256
}

Expand Down Expand Up @@ -294,3 +294,16 @@ type BeaconState struct {
CompactBeaconState
BlockRoot types.H256
}

type VersionedExecutionPayloadHeader struct {
Capella *ExecutionPayloadHeaderCapella
}

func (v VersionedExecutionPayloadHeader) Encode(encoder scale.Encoder) error {
var err error
if v.Capella != nil {
encoder.PushByte(0)
err = encoder.Encode(v.Capella)
}
return err
}
5 changes: 5 additions & 0 deletions relayer/relays/beacon/header/syncer/scale/json_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,8 @@ func (s *SyncAggregate) ToJSON() json.SyncAggregate {
SyncCommitteeSignature: util.BytesToHexString(s.SyncCommitteeSignature[:]),
}
}

func (v *VersionedExecutionPayloadHeader) ToJSON() json.VersionedExecutionPayloadHeader {
data := v.Capella.ToJSON()
return json.VersionedExecutionPayloadHeader{Capella: &data}
}
10 changes: 6 additions & 4 deletions relayer/relays/beacon/header/syncer/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,15 +394,17 @@ func (s *Syncer) GetHeaderUpdate(blockRoot common.Hash, checkpoint *cache.Proof)
return scale.HeaderUpdatePayload{}, fmt.Errorf("beacon header to scale: %w", err)
}

executionPayloadScale, err := api.CapellaExecutionPayloadToScale(block.GetExecutionPayload(), s.activeSpec)
executionHeaderBranch, err := s.getExecutionHeaderBranch(block)
if err != nil {
return scale.HeaderUpdatePayload{}, err
}

executionHeaderBranch, err := s.getExecutionHeaderBranch(block)
// Todo: Upgrade to Deneb based on fork epoch
executionPayloadScale, err := api.CapellaExecutionPayloadToScale(block.GetExecutionPayload(), s.activeSpec)
if err != nil {
return scale.HeaderUpdatePayload{}, err
}
versionedExecutionPayloadHeader := scale.VersionedExecutionPayloadHeader{Capella: &executionPayloadScale}

// If checkpoint not provided or slot == finalizedSlot there won't be an ancestry proof because the header state in question is also the finalized header
if checkpoint == nil || block.GetBeaconSlot() == checkpoint.Slot {
Expand All @@ -411,7 +413,7 @@ func (s *Syncer) GetHeaderUpdate(blockRoot common.Hash, checkpoint *cache.Proof)
AncestryProof: scale.OptionAncestryProof{
HasValue: false,
},
ExecutionHeader: executionPayloadScale,
ExecutionHeader: versionedExecutionPayloadHeader,
ExecutionBranch: executionHeaderBranch,
}, nil
}
Expand All @@ -435,7 +437,7 @@ func (s *Syncer) GetHeaderUpdate(blockRoot common.Hash, checkpoint *cache.Proof)
FinalizedBlockRoot: types.NewH256(checkpoint.FinalizedBlockRoot.Bytes()),
},
},
ExecutionHeader: executionPayloadScale,
ExecutionHeader: versionedExecutionPayloadHeader,
ExecutionBranch: executionHeaderBranch,
}, nil
}
Expand Down
Loading

0 comments on commit fe1e191

Please sign in to comment.