Skip to content

Commit 07d7047

Browse files
authored
feat: add electra types (#1799)
1 parent afa57c1 commit 07d7047

28 files changed

+690
-91
lines changed

Makefile

+4-3
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,16 @@ build-release-tarballs: ## Create a series of `.tar.gz` files in the BIN_DIR dir
101101

102102
EF_TESTS_TARGET = mainnet.tar.gz
103103
EF_TESTS_DIR = ./testing/ef-tests/mainnet
104-
LATEST_RELEASE_URL = https://api.github.com/repos/ethereum/consensus-spec-tests/releases/latest
104+
ALL_RELEASES_URL = https://api.github.com/repos/ethereum/consensus-spec-tests/releases
105105

106106
download_test_data:
107107
@if [ -d $(EF_TESTS_DIR) ]; then \
108108
echo "$(EF_TESTS_DIR) already downloaded. Skipping download."; \
109109
else \
110-
echo "Fetching the latest release URL for $(EF_TESTS_TARGET)..."; \
111-
curl -s $(LATEST_RELEASE_URL) \
110+
echo "Fetching the latest release (including pre-releases) for $(EF_TESTS_TARGET)..."; \
111+
curl -s $(ALL_RELEASES_URL) \
112112
| grep "browser_download_url.*$(EF_TESTS_TARGET)" \
113+
| head -n 1 \
113114
| cut -d : -f 2,3 \
114115
| tr -d \" \
115116
| wget -qi -; \

bin/trin-execution/src/era/beacon.rs

+47-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use alloy::{
1010
use ethportal_api::consensus::{
1111
beacon_block::{
1212
SignedBeaconBlock, SignedBeaconBlockBellatrix, SignedBeaconBlockCapella,
13-
SignedBeaconBlockDeneb,
13+
SignedBeaconBlockDeneb, SignedBeaconBlockElectra,
1414
},
1515
body::Transactions,
1616
};
@@ -32,6 +32,7 @@ impl ProcessBeaconBlock for SignedBeaconBlock {
3232
SignedBeaconBlock::Bellatrix(block) => block.process_beacon_block(),
3333
SignedBeaconBlock::Capella(block) => block.process_beacon_block(),
3434
SignedBeaconBlock::Deneb(block) => block.process_beacon_block(),
35+
SignedBeaconBlock::Electra(block) => block.process_beacon_block(),
3536
}
3637
}
3738
}
@@ -167,6 +168,51 @@ impl ProcessBeaconBlock for SignedBeaconBlockDeneb {
167168
}
168169
}
169170

171+
impl ProcessBeaconBlock for SignedBeaconBlockElectra {
172+
fn process_beacon_block(&self) -> anyhow::Result<ProcessedBlock> {
173+
let payload = &self.message.body.execution_payload;
174+
175+
let transactions = decode_transactions(&payload.transactions)?;
176+
let transactions_root = calculate_transaction_root(&transactions);
177+
let transactions = process_transactions(transactions)?;
178+
179+
let withdrawals: Vec<Withdrawal> =
180+
payload.withdrawals.iter().map(Withdrawal::from).collect();
181+
let withdrawals_root = calculate_withdrawals_root(&withdrawals);
182+
183+
let header = Header {
184+
parent_hash: payload.parent_hash,
185+
ommers_hash: EMPTY_UNCLE_ROOT_HASH,
186+
beneficiary: payload.fee_recipient,
187+
state_root: payload.state_root,
188+
transactions_root,
189+
receipts_root: payload.receipts_root,
190+
logs_bloom: Bloom::from_slice(payload.logs_bloom.to_vec().as_slice()),
191+
difficulty: U256::ZERO,
192+
number: payload.block_number,
193+
gas_limit: payload.gas_limit,
194+
gas_used: payload.gas_used,
195+
timestamp: payload.timestamp,
196+
extra_data: payload.extra_data.to_vec().into(),
197+
mix_hash: payload.prev_randao,
198+
nonce: B64::ZERO,
199+
base_fee_per_gas: Some(payload.base_fee_per_gas.to()),
200+
withdrawals_root: Some(withdrawals_root),
201+
blob_gas_used: Some(payload.blob_gas_used),
202+
excess_blob_gas: Some(payload.excess_blob_gas),
203+
parent_beacon_block_root: Some(self.message.parent_root),
204+
requests_hash: Some(self.message.body.execution_requests.requests_hash()),
205+
};
206+
207+
Ok(ProcessedBlock {
208+
header: header.clone(),
209+
uncles: None,
210+
withdrawals: Some(withdrawals),
211+
transactions,
212+
})
213+
}
214+
}
215+
170216
pub fn decode_transactions(transactions: &Transactions) -> anyhow::Result<Vec<TxEnvelope>> {
171217
transactions
172218
.into_par_iter()

crates/ethportal-api/src/types/consensus/beacon_block.rs

+16-3
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,18 @@ use tree_hash::TreeHash;
99
use tree_hash_derive::TreeHash;
1010

1111
use crate::consensus::{
12-
body::{BeaconBlockBodyBellatrix, BeaconBlockBodyCapella, BeaconBlockBodyDeneb},
12+
body::{
13+
BeaconBlockBodyBellatrix, BeaconBlockBodyCapella, BeaconBlockBodyDeneb,
14+
BeaconBlockBodyElectra,
15+
},
1316
fork::ForkName,
1417
proof::build_merkle_proof_for_index,
1518
signature::BlsSignature,
1619
};
1720

1821
/// A block of the `BeaconChain`.
1922
#[superstruct(
20-
variants(Bellatrix, Capella, Deneb),
23+
variants(Bellatrix, Capella, Deneb, Electra),
2124
variant_attributes(
2225
derive(
2326
Debug,
@@ -57,6 +60,8 @@ pub struct BeaconBlock {
5760
pub body: BeaconBlockBodyCapella,
5861
#[superstruct(only(Deneb), partial_getter(rename = "body_deneb"))]
5962
pub body: BeaconBlockBodyDeneb,
63+
#[superstruct(only(Electra), partial_getter(rename = "body_electra"))]
64+
pub body: BeaconBlockBodyElectra,
6065
}
6166

6267
impl BeaconBlock {
@@ -65,6 +70,7 @@ impl BeaconBlock {
6570
ForkName::Bellatrix => BeaconBlockBellatrix::from_ssz_bytes(bytes).map(Self::Bellatrix),
6671
ForkName::Capella => BeaconBlockCapella::from_ssz_bytes(bytes).map(Self::Capella),
6772
ForkName::Deneb => BeaconBlockDeneb::from_ssz_bytes(bytes).map(Self::Deneb),
73+
ForkName::Electra => BeaconBlockElectra::from_ssz_bytes(bytes).map(Self::Electra),
6874
}
6975
}
7076
}
@@ -140,7 +146,7 @@ impl BeaconBlockDeneb {
140146

141147
/// A `BeaconBlock` and a signature from its proposer.
142148
#[superstruct(
143-
variants(Bellatrix, Capella, Deneb),
149+
variants(Bellatrix, Capella, Deneb, Electra),
144150
variant_attributes(derive(
145151
Debug,
146152
Clone,
@@ -163,6 +169,8 @@ pub struct SignedBeaconBlock {
163169
pub message: BeaconBlockCapella,
164170
#[superstruct(only(Deneb), partial_getter(rename = "message_deneb"))]
165171
pub message: BeaconBlockDeneb,
172+
#[superstruct(only(Electra), partial_getter(rename = "message_electra"))]
173+
pub message: BeaconBlockElectra,
166174
pub signature: BlsSignature,
167175
}
168176

@@ -205,6 +213,9 @@ impl SignedBeaconBlock {
205213
BeaconBlock::Deneb(message) => {
206214
SignedBeaconBlock::Deneb(SignedBeaconBlockDeneb { message, signature })
207215
}
216+
BeaconBlock::Electra(message) => {
217+
SignedBeaconBlock::Electra(SignedBeaconBlockElectra { message, signature })
218+
}
208219
}
209220
}
210221

@@ -214,6 +225,7 @@ impl SignedBeaconBlock {
214225
SignedBeaconBlock::Bellatrix(block) => block.message.slot,
215226
SignedBeaconBlock::Capella(block) => block.message.slot,
216227
SignedBeaconBlock::Deneb(block) => block.message.slot,
228+
SignedBeaconBlock::Electra(block) => block.message.slot,
217229
}
218230
}
219231

@@ -225,6 +237,7 @@ impl SignedBeaconBlock {
225237
}
226238
SignedBeaconBlock::Capella(block) => block.message.body.execution_payload.block_number,
227239
SignedBeaconBlock::Deneb(block) => block.message.body.execution_payload.block_number,
240+
SignedBeaconBlock::Electra(block) => block.message.body.execution_payload.block_number,
228241
}
229242
}
230243
}

crates/ethportal-api/src/types/consensus/beacon_state.rs

+49-11
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use serde_utils;
88
use ssz::{Decode, DecodeError, Encode};
99
use ssz_derive::{Decode, Encode};
1010
use ssz_types::{
11-
typenum::{U1099511627776, U16777216, U2048, U4, U65536, U8192},
11+
typenum::{U1099511627776, U134217728, U16777216, U2048, U262144, U4, U65536, U8192},
1212
BitVector, FixedVector, VariableList,
1313
};
1414
use superstruct::superstruct;
@@ -18,12 +18,16 @@ use tree_hash_derive::TreeHash;
1818
use crate::consensus::{
1919
body::{Checkpoint, Eth1Data},
2020
execution_payload::{
21-
ExecutionPayloadHeaderBellatrix, ExecutionPayloadHeaderCapella, ExecutionPayloadHeaderDeneb,
21+
ExecutionPayloadHeaderBellatrix, ExecutionPayloadHeaderCapella,
22+
ExecutionPayloadHeaderDeneb, ExecutionPayloadHeaderElectra,
2223
},
2324
fork::ForkName,
2425
header::BeaconBlockHeader,
2526
historical_summaries::HistoricalSummaries,
2627
participation_flags::ParticipationFlags,
28+
pending_balance_deposit::PendingDeposit,
29+
pending_consolidation::PendingConsolidation,
30+
pending_partial_withdrawal::PendingPartialWithdrawal,
2731
proof::build_merkle_proof_for_index,
2832
pubkey::PubKey,
2933
sync_committee::SyncCommittee,
@@ -39,10 +43,13 @@ type JustificationBitsLength = U4;
3943

4044
pub type RootsPerHistoricalRoot = FixedVector<B256, SlotsPerHistoricalRoot>;
4145
pub type HistoricalRoots = VariableList<B256, HistoricalRootsLimit>;
46+
pub type PendingDepositsLimit = U134217728;
47+
pub type PendingPartialWithdrawalsLimit = U134217728;
48+
pub type PendingConsolidationsLimit = U262144;
4249

4350
/// The state of the `BeaconChain` at some slot.
4451
#[superstruct(
45-
variants(Bellatrix, Capella, Deneb),
52+
variants(Bellatrix, Capella, Deneb, Electra),
4653
variant_attributes(
4754
derive(
4855
Clone,
@@ -101,9 +108,9 @@ pub struct BeaconState {
101108
pub slashings: FixedVector<u64, EpochsPerSlashingsVector>,
102109

103110
// Participation (Altair and later)
104-
#[superstruct(only(Bellatrix, Capella, Deneb))]
111+
#[superstruct(only(Bellatrix, Capella, Deneb, Electra))]
105112
pub previous_epoch_participation: VariableList<ParticipationFlags, ValidatorRegistryLimit>,
106-
#[superstruct(only(Bellatrix, Capella, Deneb))]
113+
#[superstruct(only(Bellatrix, Capella, Deneb, Electra))]
107114
pub current_epoch_participation: VariableList<ParticipationFlags, ValidatorRegistryLimit>,
108115

109116
// Finality
@@ -116,14 +123,14 @@ pub struct BeaconState {
116123
pub finalized_checkpoint: Checkpoint,
117124

118125
// Inactivity
119-
#[superstruct(only(Bellatrix, Capella, Deneb))]
126+
#[superstruct(only(Bellatrix, Capella, Deneb, Electra))]
120127
#[serde(deserialize_with = "ssz_types::serde_utils::quoted_u64_var_list::deserialize")]
121128
pub inactivity_scores: VariableList<u64, ValidatorRegistryLimit>,
122129

123130
// Light-client sync committees
124-
#[superstruct(only(Bellatrix, Capella, Deneb))]
131+
#[superstruct(only(Bellatrix, Capella, Deneb, Electra))]
125132
pub current_sync_committee: Arc<SyncCommittee>,
126-
#[superstruct(only(Bellatrix, Capella, Deneb))]
133+
#[superstruct(only(Bellatrix, Capella, Deneb, Electra))]
127134
pub next_sync_committee: Arc<SyncCommittee>,
128135

129136
// Execution
@@ -142,17 +149,47 @@ pub struct BeaconState {
142149
partial_getter(rename = "latest_execution_payload_header_deneb")
143150
)]
144151
pub latest_execution_payload_header: ExecutionPayloadHeaderDeneb,
152+
#[superstruct(
153+
only(Electra),
154+
partial_getter(rename = "latest_execution_payload_header_electra")
155+
)]
156+
pub latest_execution_payload_header: ExecutionPayloadHeaderElectra,
145157

146158
// Capella
147-
#[superstruct(only(Capella, Deneb), partial_getter(copy))]
159+
#[superstruct(only(Capella, Deneb, Electra), partial_getter(copy))]
148160
#[serde(deserialize_with = "as_u64")]
149161
pub next_withdrawal_index: u64,
150-
#[superstruct(only(Capella, Deneb), partial_getter(copy))]
162+
#[superstruct(only(Capella, Deneb, Electra), partial_getter(copy))]
151163
#[serde(deserialize_with = "as_u64")]
152164
pub next_withdrawal_validator_index: u64,
153165
// Deep history valid from Capella onwards.
154-
#[superstruct(only(Capella, Deneb))]
166+
#[superstruct(only(Capella, Deneb, Electra))]
155167
pub historical_summaries: HistoricalSummaries,
168+
169+
// Electra
170+
#[superstruct(only(Electra), partial_getter(copy))]
171+
#[serde(deserialize_with = "as_u64")]
172+
pub deposit_requests_start_index: u64,
173+
#[superstruct(only(Electra), partial_getter(copy))]
174+
#[serde(deserialize_with = "as_u64")]
175+
pub deposit_balance_to_consume: u64,
176+
#[superstruct(only(Electra), partial_getter(copy))]
177+
#[serde(deserialize_with = "as_u64")]
178+
pub exit_balance_to_consume: u64,
179+
#[superstruct(only(Electra), partial_getter(copy))]
180+
pub earliest_exit_epoch: Epoch,
181+
#[superstruct(only(Electra), partial_getter(copy))]
182+
#[serde(deserialize_with = "as_u64")]
183+
pub consolidation_balance_to_consume: u64,
184+
#[superstruct(only(Electra), partial_getter(copy))]
185+
pub earliest_consolidation_epoch: Epoch,
186+
#[superstruct(only(Electra))]
187+
pub pending_deposits: VariableList<PendingDeposit, PendingDepositsLimit>,
188+
#[superstruct(only(Electra))]
189+
pub pending_partial_withdrawals:
190+
VariableList<PendingPartialWithdrawal, PendingPartialWithdrawalsLimit>,
191+
#[superstruct(only(Electra))]
192+
pub pending_consolidations: VariableList<PendingConsolidation, PendingConsolidationsLimit>,
156193
}
157194

158195
impl BeaconState {
@@ -161,6 +198,7 @@ impl BeaconState {
161198
ForkName::Bellatrix => BeaconStateBellatrix::from_ssz_bytes(bytes).map(Self::Bellatrix),
162199
ForkName::Capella => BeaconStateCapella::from_ssz_bytes(bytes).map(Self::Capella),
163200
ForkName::Deneb => BeaconStateDeneb::from_ssz_bytes(bytes).map(Self::Deneb),
201+
ForkName::Electra => BeaconStateElectra::from_ssz_bytes(bytes).map(Self::Electra),
164202
}
165203
}
166204
}

0 commit comments

Comments
 (0)