Skip to content

Commit 568374c

Browse files
committed
add light versions of some entities
impl From for beacon BlockHeader
1 parent 261dacc commit 568374c

File tree

4 files changed

+129
-1
lines changed

4 files changed

+129
-1
lines changed

ethereum-common/src/beacon/block_header.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,15 @@ pub struct BlockHeader {
1010
pub state_root: Hash256,
1111
pub body_root: Hash256,
1212
}
13+
14+
impl From<Block> for BlockHeader {
15+
fn from(value: Block) -> Self {
16+
Self {
17+
slot: value.slot,
18+
proposer_index: value.proposer_index,
19+
parent_root: value.parent_root,
20+
state_root: value.state_root,
21+
body_root: value.body.tree_hash_root(),
22+
}
23+
}
24+
}

ethereum-common/src/beacon/light.rs

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
// Module contains lightened versions of the entities.
2+
3+
use super::*;
4+
5+
#[derive(Debug, Clone, Decode, Encode, Deserialize, tree_hash_derive::TreeHash)]
6+
pub struct ExecutionPayload {
7+
pub parent_hash: Bytes32,
8+
pub fee_recipient: Address,
9+
pub state_root: Bytes32,
10+
pub receipts_root: Bytes32,
11+
pub logs_bloom: H256,
12+
pub prev_randao: Bytes32,
13+
#[serde(deserialize_with = "utils::deserialize_u64")]
14+
pub block_number: u64,
15+
#[serde(deserialize_with = "utils::deserialize_u64")]
16+
pub gas_limit: u64,
17+
#[serde(deserialize_with = "utils::deserialize_u64")]
18+
pub gas_used: u64,
19+
#[serde(deserialize_with = "utils::deserialize_u64")]
20+
pub timestamp: u64,
21+
pub extra_data: base_types::ByteList<32>,
22+
#[serde(deserialize_with = "utils::deserialize_u256")]
23+
pub base_fee_per_gas: U256,
24+
pub block_hash: Bytes32,
25+
pub transactions: H256,
26+
pub withdrawals: H256,
27+
#[serde(deserialize_with = "utils::deserialize_u64")]
28+
pub blob_gas_used: u64,
29+
#[serde(deserialize_with = "utils::deserialize_u64")]
30+
pub excess_blob_gas: u64,
31+
}
32+
33+
impl From<super::ExecutionPayload> for ExecutionPayload {
34+
fn from(value: super::ExecutionPayload) -> Self {
35+
Self {
36+
parent_hash: value.parent_hash,
37+
fee_recipient: value.fee_recipient,
38+
state_root: value.state_root,
39+
receipts_root: value.receipts_root,
40+
logs_bloom: value.logs_bloom.tree_hash_root(),
41+
prev_randao: value.prev_randao,
42+
block_number: value.block_number,
43+
gas_limit: value.gas_limit,
44+
gas_used: value.gas_used,
45+
timestamp: value.timestamp,
46+
extra_data: value.extra_data,
47+
base_fee_per_gas: value.base_fee_per_gas,
48+
block_hash: value.block_hash,
49+
transactions: value.transactions.tree_hash_root(),
50+
withdrawals: value.withdrawals.tree_hash_root(),
51+
blob_gas_used: value.blob_gas_used,
52+
excess_blob_gas: value.excess_blob_gas,
53+
}
54+
}
55+
}
56+
57+
#[derive(Debug, Clone, Decode, Encode, tree_hash_derive::TreeHash, Deserialize)]
58+
pub struct BlockBody {
59+
pub randao_reveal: H256,
60+
pub eth1_data: H256,
61+
pub graffiti: Bytes32,
62+
pub proposer_slashings: H256,
63+
pub attester_slashings: H256,
64+
pub attestations: H256,
65+
pub deposits: H256,
66+
pub voluntary_exits: H256,
67+
pub sync_aggregate: H256,
68+
pub execution_payload: ExecutionPayload,
69+
pub bls_to_execution_changes: H256,
70+
pub blob_kzg_commitments: H256,
71+
}
72+
73+
impl From<super::BlockBody> for BlockBody {
74+
fn from(value: super::BlockBody) -> Self {
75+
Self {
76+
randao_reveal: value.randao_reveal.tree_hash_root(),
77+
eth1_data: value.eth1_data.tree_hash_root(),
78+
graffiti: value.graffiti,
79+
proposer_slashings: value.proposer_slashings.tree_hash_root(),
80+
attester_slashings: value.attester_slashings.tree_hash_root(),
81+
attestations: value.attestations.tree_hash_root(),
82+
deposits: value.deposits.tree_hash_root(),
83+
voluntary_exits: value.voluntary_exits.tree_hash_root(),
84+
sync_aggregate: value.sync_aggregate.tree_hash_root(),
85+
execution_payload: value.execution_payload.into(),
86+
bls_to_execution_changes: value.bls_to_execution_changes.tree_hash_root(),
87+
blob_kzg_commitments: value.blob_kzg_commitments.tree_hash_root(),
88+
}
89+
}
90+
}
91+
92+
#[derive(Debug, Clone, tree_hash_derive::TreeHash, Decode, Encode, Deserialize)]
93+
pub struct Block {
94+
#[serde(deserialize_with = "utils::deserialize_u64")]
95+
pub slot: u64,
96+
#[serde(deserialize_with = "utils::deserialize_u64")]
97+
pub proposer_index: u64,
98+
pub parent_root: Hash256,
99+
pub state_root: Hash256,
100+
pub body: BlockBody,
101+
}
102+
103+
impl From<super::Block> for Block {
104+
fn from(value: super::Block) -> Self {
105+
Self {
106+
slot: value.slot,
107+
proposer_index: value.proposer_index,
108+
parent_root: value.parent_root,
109+
state_root: value.state_root,
110+
body: value.body.into(),
111+
}
112+
}
113+
}

ethereum-common/src/beacon/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ mod block_body;
55
mod block_header;
66
mod common;
77
mod execution_payload;
8+
pub mod light;
89

910
pub use block::Block;
1011
pub use block_body::BlockBody;

gear-programs/checkpoint-light-client/io/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ pub use ark_bls12_381::{G1Projective as G1, G2Projective as G2};
1616
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
1717
pub use ethereum_common::{
1818
self,
19-
beacon::{BLSPubKey, BlockHeader as BeaconBlockHeader, Bytes32, SyncAggregate},
19+
beacon::{
20+
BLSPubKey, Block as BeaconBlock, BlockHeader as BeaconBlockHeader, Bytes32, SyncAggregate,
21+
},
2022
network::Network,
2123
tree_hash, SYNC_COMMITTEE_SIZE,
2224
};

0 commit comments

Comments
 (0)