Skip to content

Commit 89cbccf

Browse files
authored
refactor(light-client): Refactor light-client crate to use ethportal-api consensus types (#907)
* Refactor consensus `ethportal-api` types * Update `light-client` test files to latest Capella data * Update `light-client` sync test to recent Capella mainnet data * Refator `light-client` crate to use `ethportal-api` consensus types
1 parent d059fe4 commit 89cbccf

33 files changed

+341
-4336
lines changed

Cargo.lock

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ethportal-api/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ eth2_ssz = "0.4.0"
2121
eth2_ssz_derive = "0.3.0"
2222
eth2_ssz_types = "0.2.1"
2323
ethereum-types = "0.12.1"
24+
ethereum_serde_utils = "0.5.1"
2425
ethnum = "1.3.2"
2526
hex = "0.4.3"
2627
jsonrpsee = {version="0.20.0", features = ["async-client", "client", "macros", "server"]}

ethportal-api/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ pub use types::content_key::{
3232
state::StateContentKey,
3333
};
3434

35+
pub use types::consensus;
36+
pub use types::consensus::light_client;
3537
pub use types::content_value::{
3638
beacon::{BeaconContentValue, PossibleBeaconContentValue},
3739
error::ContentValueError,

ethportal-api/src/types/consensus/execution_payload.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
11
use super::serde::{de_hex_to_txs, de_number_to_u256, se_hex_to_number, se_txs_to_hex};
22
use crate::types::consensus::body::Transactions;
33
use crate::types::consensus::fork::ForkName;
4-
use crate::types::wrapped::bloom::Bloom;
5-
use crate::types::wrapped::bytes::Bytes;
64
use crate::types::wrapped::h160::H160;
5+
use crate::utils::serde::{hex_fixed_vec, hex_var_list};
76
use ethereum_types::{H256, U256};
87
use serde::{Deserialize, Serialize};
98
use serde_this_or_that::as_u64;
109
use ssz::Decode;
1110
use ssz_derive::{Decode, Encode};
11+
use ssz_types::{typenum, FixedVector, VariableList};
1212
use superstruct::superstruct;
13+
use tree_hash_derive::TreeHash;
14+
15+
pub type Bloom = FixedVector<u8, typenum::U256>;
16+
pub type ExtraData = VariableList<u8, typenum::U32>;
1317

1418
#[derive(Debug, PartialEq, Clone, Deserialize, Serialize, Encode, Decode)]
1519
pub struct ExecutionPayload {
1620
pub parent_hash: H256,
1721
pub fee_recipient: H160,
1822
pub state_root: H256,
1923
pub receipts_root: H256,
24+
#[serde(with = "hex_fixed_vec")]
2025
pub logs_bloom: Bloom,
2126
pub prev_randao: H256, // 'difficulty' in the yellow paper
2227
#[serde(deserialize_with = "as_u64")]
@@ -27,7 +32,8 @@ pub struct ExecutionPayload {
2732
pub gas_used: u64,
2833
#[serde(deserialize_with = "as_u64")]
2934
pub timestamp: u64,
30-
pub extra_data: Bytes,
35+
#[serde(with = "hex_var_list")]
36+
pub extra_data: ExtraData,
3137
#[serde(deserialize_with = "de_number_to_u256")]
3238
#[serde(serialize_with = "se_hex_to_number")]
3339
pub base_fee_per_gas: U256,
@@ -49,10 +55,12 @@ pub struct ExecutionPayload {
4955
Deserialize,
5056
Encode,
5157
Decode,
58+
TreeHash
5259
),)
5360
)]
54-
#[derive(Debug, Clone, Serialize, Deserialize, Encode)]
61+
#[derive(Debug, Clone, Serialize, Deserialize, Encode, TreeHash)]
5562
#[ssz(enum_behaviour = "transparent")]
63+
#[tree_hash(enum_behaviour = "transparent")]
5664
pub struct ExecutionPayloadHeader {
5765
#[superstruct(getter(copy))]
5866
pub parent_hash: H256,
@@ -61,6 +69,7 @@ pub struct ExecutionPayloadHeader {
6169
pub state_root: H256,
6270
#[superstruct(getter(copy))]
6371
pub receipts_root: H256,
72+
#[serde(with = "hex_fixed_vec")]
6473
pub logs_bloom: Bloom,
6574
#[superstruct(getter(copy))]
6675
pub prev_randao: H256,
@@ -76,7 +85,8 @@ pub struct ExecutionPayloadHeader {
7685
#[superstruct(getter(copy))]
7786
#[serde(deserialize_with = "as_u64")]
7887
pub timestamp: u64,
79-
pub extra_data: Bytes,
88+
#[serde(with = "hex_var_list")]
89+
pub extra_data: ExtraData,
8090
#[superstruct(getter(copy))]
8191
#[serde(deserialize_with = "de_number_to_u256")]
8292
#[serde(serialize_with = "se_hex_to_number")]

ethportal-api/src/types/consensus/header.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@ use ethereum_types::H256;
22
use serde::{Deserialize, Serialize};
33
use serde_this_or_that::as_u64;
44
use ssz_derive::{Decode, Encode};
5+
use tree_hash_derive::TreeHash;
56

67
/// Types based off specs @
78
/// https://github.com/ethereum/consensus-specs/blob/5970ae56a1cd50ea06049d8aad6bed74093d49d3/specs/phase0/beacon-chain.md
8-
#[derive(Debug, PartialEq, Eq, Clone, Default, Serialize, Deserialize, Encode, Decode)]
9+
#[derive(
10+
Debug, PartialEq, Eq, Clone, Default, Serialize, Deserialize, Encode, Decode, TreeHash,
11+
)]
912
pub struct BeaconBlockHeader {
1013
#[serde(deserialize_with = "as_u64")]
1114
pub slot: u64,

ethportal-api/src/types/consensus/light_client/bootstrap.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pub type CurrentSyncCommitteeProofLen = U5;
2323
)
2424
)]
2525
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Encode)]
26+
#[serde(untagged)]
2627
#[ssz(enum_behaviour = "transparent")]
2728
pub struct LightClientBootstrap {
2829
/// Header matching the requested beacon block root

ethportal-api/src/types/consensus/light_client/header.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,30 @@ use ssz_derive::{Decode, Encode};
88
use ssz_types::typenum::U4;
99
use ssz_types::FixedVector;
1010
use superstruct::superstruct;
11+
use tree_hash_derive::TreeHash;
1112

1213
pub type ExecutionBranchLen = U4;
1314

1415
#[superstruct(
1516
variants(Bellatrix, Capella),
1617
variant_attributes(
17-
derive(Debug, Clone, Serialize, PartialEq, Deserialize, Encode, Decode,),
18+
derive(
19+
Debug,
20+
Clone,
21+
Serialize,
22+
PartialEq,
23+
Deserialize,
24+
Encode,
25+
Decode,
26+
Default,
27+
TreeHash
28+
),
1829
serde(deny_unknown_fields),
1930
)
2031
)]
21-
#[derive(Debug, Clone, Serialize, Deserialize, Encode)]
32+
#[derive(Debug, Clone, Serialize, Deserialize, Encode, TreeHash)]
2233
#[ssz(enum_behaviour = "transparent")]
34+
#[tree_hash(enum_behaviour = "transparent")]
2335
pub struct LightClientHeader {
2436
pub beacon: BeaconBlockHeader,
2537
#[superstruct(only(Capella))]
@@ -28,6 +40,12 @@ pub struct LightClientHeader {
2840
pub execution_branch: FixedVector<H256, ExecutionBranchLen>,
2941
}
3042

43+
impl Default for LightClientHeader {
44+
fn default() -> Self {
45+
Self::Capella(LightClientHeaderCapella::default())
46+
}
47+
}
48+
3149
impl LightClientHeader {
3250
pub fn from_ssz_bytes(bytes: &[u8], fork_name: ForkName) -> Result<Self, ssz::DecodeError> {
3351
match fork_name {

ethportal-api/src/types/consensus/pubkey.rs

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,42 @@
11
use serde::{Deserialize, Deserializer, Serialize, Serializer};
22
use ssz::{Decode, Encode};
3+
use ssz_types::{typenum, FixedVector};
4+
use std::ops::Deref;
5+
use tree_hash_derive::TreeHash;
36

47
use crate::utils::bytes::{hex_decode, hex_encode};
58

69
/// Types based off specs @
710
/// https://github.com/ethereum/consensus-specs/blob/5970ae56a1/specs/phase0/beacon-chain.md
8-
#[derive(Debug, PartialEq, Clone)]
9-
pub struct PubKey([u8; 48]);
11+
#[derive(Debug, PartialEq, Clone, TreeHash)]
12+
pub struct PubKey {
13+
pub inner: FixedVector<u8, typenum::U48>,
14+
}
15+
16+
impl Default for PubKey {
17+
fn default() -> Self {
18+
Self {
19+
inner: FixedVector::from_elem(0),
20+
}
21+
}
22+
}
23+
24+
impl Deref for PubKey {
25+
type Target = FixedVector<u8, typenum::U48>;
26+
27+
fn deref(&self) -> &Self::Target {
28+
&self.inner
29+
}
30+
}
1031

1132
impl Decode for PubKey {
1233
fn is_ssz_fixed_len() -> bool {
1334
true
1435
}
1536

1637
fn from_ssz_bytes(bytes: &[u8]) -> Result<Self, ssz::DecodeError> {
17-
let mut key = [0u8; 48];
18-
key.copy_from_slice(bytes);
19-
Ok(Self(key))
38+
let key = FixedVector::from(Vec::from(bytes));
39+
Ok(Self { inner: key })
2040
}
2141
fn ssz_fixed_len() -> usize {
2242
48
@@ -29,10 +49,10 @@ impl Encode for PubKey {
2949
}
3050

3151
fn ssz_append(&self, buf: &mut Vec<u8>) {
32-
buf.extend_from_slice(&self.0);
52+
buf.extend_from_slice(&self.inner.as_ssz_bytes());
3353
}
3454
fn ssz_bytes_len(&self) -> usize {
35-
self.0.len()
55+
self.inner.len()
3656
}
3757
fn ssz_fixed_len() -> usize {
3858
48
@@ -46,9 +66,8 @@ impl<'de> Deserialize<'de> for PubKey {
4666
{
4767
let result: String = Deserialize::deserialize(deserializer)?;
4868
let result = hex_decode(&result).map_err(serde::de::Error::custom)?;
49-
let mut key = [0u8; 48];
50-
key.copy_from_slice(&result);
51-
Ok(Self(key))
69+
let key = FixedVector::from(result);
70+
Ok(Self { inner: key })
5271
}
5372
}
5473

@@ -57,7 +76,7 @@ impl Serialize for PubKey {
5776
where
5877
S: Serializer,
5978
{
60-
let val = hex_encode(self.0);
79+
let val = hex_encode(self.inner.as_ssz_bytes());
6180
serializer.serialize_str(&val)
6281
}
6382
}

ethportal-api/src/types/consensus/signature.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ pub struct BlsSignature {
1010
pub signature: [u8; 96],
1111
}
1212

13+
impl Default for BlsSignature {
14+
fn default() -> Self {
15+
Self {
16+
signature: [0u8; 96],
17+
}
18+
}
19+
}
20+
1321
impl Decode for BlsSignature {
1422
fn is_ssz_fixed_len() -> bool {
1523
true

ethportal-api/src/types/consensus/sync_committee.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,22 @@ use serde::{Deserialize, Serialize};
33
use ssz_derive::{Decode, Encode};
44
use ssz_types::typenum::U512;
55
use ssz_types::FixedVector;
6+
use tree_hash_derive::TreeHash;
67

78
type SyncCommitteeSize = U512;
89

910
/// https://github.com/ethereum/consensus-specs/blob/dev/specs/altair/beacon-chain.md#synccommittee
10-
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize, Encode, Decode)]
11+
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize, Encode, Decode, TreeHash)]
1112
pub struct SyncCommittee {
1213
pub pubkeys: FixedVector<PubKey, SyncCommitteeSize>,
1314
pub aggregate_pubkey: PubKey,
1415
}
16+
17+
impl Default for SyncCommittee {
18+
fn default() -> Self {
19+
Self {
20+
pubkeys: FixedVector::from_elem(PubKey::default()),
21+
aggregate_pubkey: PubKey::default(),
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)