Skip to content

Commit ca41e5a

Browse files
committed
chore(engine): alloy type port
1 parent 8861cf7 commit ca41e5a

File tree

6 files changed

+80
-63
lines changed

6 files changed

+80
-63
lines changed

bin/network.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
use std::str::FromStr;
2-
3-
use ethers::types::Address;
1+
use alloy_primitives::address;
42
use eyre::Result;
53

64
use magi::{
@@ -15,9 +13,7 @@ async fn main() -> Result<()> {
1513

1614
let addr = "0.0.0.0:9876".parse()?;
1715
let chain_id = 420;
18-
let (_, recv) = watch::channel(Address::from_str(
19-
"0x715b7219d986641df9efd9c7ef01218d528e19ec",
20-
)?);
16+
let (_, recv) = watch::channel(address!("715b7219d986641df9efd9c7ef01218d528e19ec"));
2117
let (block_handler, block_recv) = BlockHandler::new(chain_id, recv);
2218

2319
Service::new(addr, chain_id)

src/derive/stages/attributes.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ impl Attributes {
9393
let seq_number = Some(self.sequence_number);
9494
let prev_randao = l1_info.block_info.mix_hash;
9595
let epoch = Some(input.epoch);
96-
let transactions = Some(self.derive_transactions(input, l1_info));
96+
let transactions = Some(self.derive_transactions(&input, l1_info));
9797
let suggested_fee_recipient = SystemAccounts::default().fee_vault;
9898

9999
PayloadAttributes {
@@ -117,7 +117,7 @@ impl Attributes {
117117
/// Returns a [RawTransaction] vector containing all of the transactions for the L2 block.
118118
fn derive_transactions(
119119
&self,
120-
input: BlockInput<Epoch>,
120+
input: &BlockInput<Epoch>,
121121
l1_info: &L1Info,
122122
) -> Vec<RawTransaction> {
123123
let mut transactions = Vec::new();
@@ -144,7 +144,7 @@ impl Attributes {
144144
}
145145

146146
// Remaining transactions
147-
let mut rest = input.transactions;
147+
let mut rest = input.transactions.clone();
148148
transactions.append(&mut rest);
149149

150150
transactions

src/driver/engine_driver.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,8 +244,10 @@ fn should_skip(block: &Block<Transaction>, attributes: &PayloadAttributes) -> Re
244244

245245
let is_same = attributes_hashes == block_hashes
246246
&& attributes.timestamp == alloy_primitives::U64::from(block.timestamp.as_u64())
247-
&& attributes.prev_randao == alloy_primitives::B256::from_slice(block.mix_hash.unwrap().as_bytes())
248-
&& attributes.suggested_fee_recipient == alloy_primitives::Address::from_slice(block.author.unwrap().as_bytes())
247+
&& attributes.prev_randao
248+
== alloy_primitives::B256::from_slice(block.mix_hash.unwrap().as_bytes())
249+
&& attributes.suggested_fee_recipient
250+
== alloy_primitives::Address::from_slice(block.author.unwrap().as_bytes())
249251
&& attributes.gas_limit == alloy_primitives::U64::from(block.gas_limit.as_u64());
250252

251253
Ok(is_same)

src/driver/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use std::{
44
time::Duration,
55
};
66

7+
use alloy_primitives::Address;
78
use ethers::providers::{Http, Provider};
8-
use ethers::types::Address;
99
use eyre::Result;
1010
use reqwest::Url;
1111
use tokio::{
@@ -100,7 +100,7 @@ impl Driver<EngineApi> {
100100

101101
let _addr = rpc::run_server(config.clone()).await?;
102102

103-
let signer = Address::from_slice(config.chain.system_config.unsafe_block_signer.as_slice());
103+
let signer = config.chain.system_config.unsafe_block_signer;
104104
let (unsafe_block_signer_sender, unsafe_block_signer_recv) = watch::channel(signer);
105105

106106
let (block_handler, unsafe_block_recv) =
@@ -239,10 +239,10 @@ impl<E: Engine> Driver<E> {
239239
unsafe_block_num > synced_block_num && unsafe_block_num - synced_block_num < 1024
240240
});
241241

242-
let next_unsafe_payload = self.future_unsafe_blocks.iter().find(|p| {
243-
p.parent_hash
244-
== self.engine_driver.unsafe_head.hash
245-
});
242+
let next_unsafe_payload = self
243+
.future_unsafe_blocks
244+
.iter()
245+
.find(|p| p.parent_hash == self.engine_driver.unsafe_head.hash);
246246

247247
if let Some(payload) = next_unsafe_payload {
248248
_ = self.engine_driver.handle_unsafe_payload(payload).await;

src/engine/payload.rs

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use alloy_consensus::TxEnvelope;
22
use alloy_eips::eip2718::Encodable2718;
3+
use alloy_primitives::{Address, Bytes, B256, U64};
34
use alloy_rpc_types::Block;
45
use alloy_rpc_types::BlockTransactions;
5-
use alloy_primitives::{Bytes, Address, B256, U64};
66
use eyre::Result;
77
use serde::{Deserialize, Serialize};
88

@@ -80,19 +80,37 @@ impl TryFrom<Block> for ExecutionPayload {
8080
state_root: value.header.state_root,
8181
receipts_root: value.header.receipts_root,
8282
logs_bloom: value.header.logs_bloom.0.to_vec().into(),
83-
prev_randao: value.header.mix_hash.ok_or_else(|| eyre::eyre!("Missing mix hash"))?,
84-
block_number: value.header.number.ok_or_else(|| eyre::eyre!("Missing block number"))?.try_into()?,
83+
prev_randao: value
84+
.header
85+
.mix_hash
86+
.ok_or_else(|| eyre::eyre!("Missing mix hash"))?,
87+
block_number: value
88+
.header
89+
.number
90+
.ok_or_else(|| eyre::eyre!("Missing block number"))?
91+
.try_into()?,
8592
gas_limit: (value.header.gas_limit as u64).try_into()?,
8693
gas_used: (value.header.gas_used as u64).try_into()?,
8794
timestamp: value.header.timestamp.try_into()?,
8895
extra_data: Bytes::from(value.header.extra_data.0),
8996
base_fee_per_gas: (value.header.base_fee_per_gas.unwrap_or_else(|| 0u64.into()) as u64)
9097
.try_into()?,
91-
block_hash: value.header.hash.ok_or_else(|| eyre::eyre!("Missing block hash"))?,
98+
block_hash: value
99+
.header
100+
.hash
101+
.ok_or_else(|| eyre::eyre!("Missing block hash"))?,
92102
transactions: encoded_txs,
93103
withdrawals: Some(Vec::new()),
94-
blob_gas_used: value.header.blob_gas_used.map(|v| (v as u64).try_into()).transpose()?,
95-
excess_blob_gas: value.header.excess_blob_gas.map(|v| (v as u64).try_into()).transpose()?,
104+
blob_gas_used: value
105+
.header
106+
.blob_gas_used
107+
.map(|v| (v as u64).try_into())
108+
.transpose()?,
109+
excess_blob_gas: value
110+
.header
111+
.excess_blob_gas
112+
.map(|v| (v as u64).try_into())
113+
.transpose()?,
96114
})
97115
}
98116
}
@@ -175,17 +193,18 @@ pub enum Status {
175193

176194
#[cfg(test)]
177195
mod tests {
178-
use eyre::Result;
179-
use alloy_provider::{Provider, ProviderBuilder};
180-
use alloy_primitives::{b256, uint};
181196
use crate::engine::ExecutionPayload;
197+
use alloy_primitives::{b256, uint};
198+
use alloy_provider::{Provider, ProviderBuilder};
199+
use eyre::Result;
182200

183201
#[tokio::test]
184202
async fn test_from_block_hash_to_execution_paylaod() -> Result<()> {
185203
let Ok(l2_rpc_url) = std::env::var("L2_TEST_RPC_URL") else {
186204
return Ok(());
187205
};
188-
let checkpoint_hash = b256!("c2794a16acacd9f7670379ffd12b6968ff98e2a602f57d7d1f880220aa5a4973");
206+
let checkpoint_hash =
207+
b256!("c2794a16acacd9f7670379ffd12b6968ff98e2a602f57d7d1f880220aa5a4973");
189208
let url = reqwest::Url::parse(&l2_rpc_url)?;
190209
let l2_provider = ProviderBuilder::new().on_http(url);
191210

src/network/handlers/block_handler.rs

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
use std::sync::mpsc::{channel, Receiver, Sender};
22
use std::time::SystemTime;
33

4-
use ethers::types::{Address, Bytes, Signature, H256};
5-
use ethers::utils::keccak256;
4+
use alloy_primitives::{keccak256, Address, Bytes, Signature, B256, U64};
65
use eyre::Result;
76
use libp2p::gossipsub::{IdentTopic, Message, MessageAcceptance, TopicHash};
87
use ssz_rs::{prelude::*, List, Vector, U256};
@@ -32,7 +31,7 @@ struct ExecutionPayloadEnvelope {
3231
signature: Signature,
3332
hash: PayloadHash,
3433
#[allow(unused)]
35-
parent_beacon_block_root: Option<H256>,
34+
parent_beacon_block_root: Option<B256>,
3635
}
3736

3837
impl Handler for BlockHandler {
@@ -109,7 +108,10 @@ impl BlockHandler {
109108

110109
let msg = envelope.hash.signature_message(self.chain_id);
111110
let block_signer = *self.unsafe_signer_recv.borrow();
112-
let sig_valid = envelope.signature.verify(msg, block_signer).is_ok();
111+
let sig_valid = envelope
112+
.signature
113+
.recover_address_from_prehash(&msg)
114+
.map_or(false, |addr| addr == block_signer);
113115

114116
time_valid && sig_valid
115117
}
@@ -154,7 +156,7 @@ fn decode_post_ecotone_block_msg(data: Vec<u8>) -> Result<ExecutionPayloadEnvelo
154156

155157
let signature = Signature::try_from(sig_data)?;
156158

157-
let parent_beacon_block_root = Some(H256::from_slice(parent_beacon_block_root));
159+
let parent_beacon_block_root = Some(B256::from_slice(parent_beacon_block_root));
158160

159161
let payload: ExecutionPayloadV3SSZ = deserialize(block_data)?;
160162
let payload = ExecutionPayload::from(payload);
@@ -170,30 +172,30 @@ fn decode_post_ecotone_block_msg(data: Vec<u8>) -> Result<ExecutionPayloadEnvelo
170172
}
171173

172174
/// Represents the Keccak256 hash of the block
173-
struct PayloadHash(H256);
175+
struct PayloadHash(B256);
174176

175177
impl From<&[u8]> for PayloadHash {
176178
/// Returns the Keccak256 hash of a sequence of bytes
177179
fn from(value: &[u8]) -> Self {
178-
Self(keccak256(value).into())
180+
Self(keccak256(value))
179181
}
180182
}
181183

182184
impl PayloadHash {
183185
/// The expected message that should be signed by the unsafe block signer.
184-
fn signature_message(&self, chain_id: u64) -> H256 {
185-
let domain = H256::zero();
186-
let chain_id = H256::from_low_u64_be(chain_id);
186+
fn signature_message(&self, chain_id: u64) -> B256 {
187+
let domain = B256::ZERO;
188+
let chain_id = B256::from_slice(&chain_id.to_be_bytes());
187189
let payload_hash = self.0;
188190

189191
let data: Vec<u8> = [
190-
domain.as_bytes(),
191-
chain_id.as_bytes(),
192-
payload_hash.as_bytes(),
192+
domain.as_slice(),
193+
chain_id.as_slice(),
194+
payload_hash.as_slice(),
193195
]
194196
.concat();
195197

196-
keccak256(data).into()
198+
keccak256(data)
197199
}
198200
}
199201

@@ -246,10 +248,10 @@ impl From<ExecutionPayloadV1SSZ> for ExecutionPayload {
246248
receipts_root: convert_hash(value.receipts_root),
247249
logs_bloom: convert_byte_vector(value.logs_bloom),
248250
prev_randao: convert_hash(value.prev_randao),
249-
block_number: value.block_number.into(),
250-
gas_limit: value.gas_limit.into(),
251-
gas_used: value.gas_used.into(),
252-
timestamp: value.timestamp.into(),
251+
block_number: value.block_number.try_into().unwrap_or_default(),
252+
gas_limit: value.gas_limit.try_into().unwrap_or_default(),
253+
gas_used: value.gas_used.try_into().unwrap_or_default(),
254+
timestamp: value.timestamp.try_into().unwrap_or_default(),
253255
extra_data: convert_byte_list(value.extra_data),
254256
base_fee_per_gas: convert_uint(value.base_fee_per_gas),
255257
block_hash: convert_hash(value.block_hash),
@@ -320,10 +322,10 @@ impl From<ExecutionPayloadV2SSZ> for ExecutionPayload {
320322
receipts_root: convert_hash(value.receipts_root),
321323
logs_bloom: convert_byte_vector(value.logs_bloom),
322324
prev_randao: convert_hash(value.prev_randao),
323-
block_number: value.block_number.into(),
324-
gas_limit: value.gas_limit.into(),
325-
gas_used: value.gas_used.into(),
326-
timestamp: value.timestamp.into(),
325+
block_number: value.block_number.try_into().unwrap_or_default(),
326+
gas_limit: value.gas_limit.try_into().unwrap_or_default(),
327+
gas_used: value.gas_used.try_into().unwrap_or_default(),
328+
timestamp: value.timestamp.try_into().unwrap_or_default(),
327329
extra_data: convert_byte_list(value.extra_data),
328330
base_fee_per_gas: convert_uint(value.base_fee_per_gas),
329331
block_hash: convert_hash(value.block_hash),
@@ -365,24 +367,24 @@ impl From<ExecutionPayloadV3SSZ> for ExecutionPayload {
365367
receipts_root: convert_hash(value.receipts_root),
366368
logs_bloom: convert_byte_vector(value.logs_bloom),
367369
prev_randao: convert_hash(value.prev_randao),
368-
block_number: value.block_number.into(),
369-
gas_limit: value.gas_limit.into(),
370-
gas_used: value.gas_used.into(),
371-
timestamp: value.timestamp.into(),
370+
block_number: value.block_number.try_into().unwrap_or_default(),
371+
gas_limit: value.gas_limit.try_into().unwrap_or_default(),
372+
gas_used: value.gas_used.try_into().unwrap_or_default(),
373+
timestamp: value.timestamp.try_into().unwrap_or_default(),
372374
extra_data: convert_byte_list(value.extra_data),
373375
base_fee_per_gas: convert_uint(value.base_fee_per_gas),
374376
block_hash: convert_hash(value.block_hash),
375377
transactions: convert_tx_list(value.transactions),
376378
withdrawals: Some(Vec::new()),
377-
blob_gas_used: Some(value.blob_gas_used.into()),
378-
excess_blob_gas: Some(value.excess_blob_gas.into()),
379+
blob_gas_used: Some(value.blob_gas_used.try_into().unwrap_or_default()),
380+
excess_blob_gas: Some(value.excess_blob_gas.try_into().unwrap_or_default()),
379381
}
380382
}
381383
}
382384

383-
/// Converts [Bytes32] into [H256]
384-
fn convert_hash(bytes: Bytes32) -> H256 {
385-
H256::from_slice(bytes.as_slice())
385+
/// Converts [Bytes32] into [B256]
386+
fn convert_hash(bytes: Bytes32) -> B256 {
387+
B256::from_slice(bytes.as_slice())
386388
}
387389

388390
/// Converts [VecAddress] into [Address]
@@ -400,12 +402,10 @@ fn convert_byte_list<const N: usize>(list: List<u8, N>) -> Bytes {
400402
Bytes::from(list.to_vec())
401403
}
402404

403-
/// Converts a [U256] into [ethers::types::U64]
404-
fn convert_uint(value: U256) -> ethers::types::U64 {
405+
/// Converts a [U256] into [U64]
406+
fn convert_uint(value: U256) -> U64 {
405407
let bytes = value.to_bytes_le();
406-
ethers::types::U256::from_little_endian(&bytes)
407-
.as_u64()
408-
.into()
408+
U64::from_le_slice(&bytes)
409409
}
410410

411411
/// Converts [ssz_rs::List] of [Transaction] into a vector of [RawTransaction]

0 commit comments

Comments
 (0)