diff --git a/bin/network.rs b/bin/network.rs index 4b6a38e5..eba1c286 100644 --- a/bin/network.rs +++ b/bin/network.rs @@ -1,6 +1,4 @@ -use std::str::FromStr; - -use ethers::types::Address; +use alloy_primitives::address; use eyre::Result; use magi::{ @@ -15,9 +13,7 @@ async fn main() -> Result<()> { let addr = "0.0.0.0:9876".parse()?; let chain_id = 420; - let (_, recv) = watch::channel(Address::from_str( - "0x715b7219d986641df9efd9c7ef01218d528e19ec", - )?); + let (_, recv) = watch::channel(address!("715b7219d986641df9efd9c7ef01218d528e19ec")); let (block_handler, block_recv) = BlockHandler::new(chain_id, recv); Service::new(addr, chain_id) diff --git a/src/common/mod.rs b/src/common/mod.rs index a4789eee..4c0c76d2 100644 --- a/src/common/mod.rs +++ b/src/common/mod.rs @@ -92,10 +92,10 @@ impl From<&ExecutionPayload> for BlockInfo { /// Converts an [ExecutionPayload] to [BlockInfo] fn from(value: &ExecutionPayload) -> Self { Self { - number: value.block_number.as_u64(), - hash: B256::from_slice(value.block_hash.as_bytes()), - parent_hash: B256::from_slice(value.parent_hash.as_bytes()), - timestamp: value.timestamp.as_u64(), + number: value.block_number.try_into().unwrap_or_default(), + hash: value.block_hash, + parent_hash: value.parent_hash, + timestamp: value.timestamp.try_into().unwrap_or_default(), } } } diff --git a/src/derive/stages/attributes.rs b/src/derive/stages/attributes.rs index bc99287f..617c6a6d 100644 --- a/src/derive/stages/attributes.rs +++ b/src/derive/stages/attributes.rs @@ -1,7 +1,7 @@ use std::sync::{Arc, RwLock}; use ethers::abi::{decode, encode, ParamType, Token}; -use ethers::types::{Address, Log, H256, U256, U64}; +use ethers::types::{Address, H256, U256}; use ethers::utils::{keccak256, rlp::Encodable, rlp::RlpStream}; use eyre::Result; @@ -89,21 +89,20 @@ impl Attributes { None }; - let timestamp = U64([input.timestamp]); let l1_inclusion_block = Some(input.l1_inclusion_block); let seq_number = Some(self.sequence_number); let prev_randao = l1_info.block_info.mix_hash; let epoch = Some(input.epoch); - let transactions = Some(self.derive_transactions(input, l1_info)); + let transactions = Some(self.derive_transactions(&input, l1_info)); let suggested_fee_recipient = SystemAccounts::default().fee_vault; PayloadAttributes { - timestamp, - prev_randao: H256::from_slice(prev_randao.as_slice()), - suggested_fee_recipient: Address::from_slice(suggested_fee_recipient.as_slice()), + timestamp: alloy_primitives::U64::from(input.timestamp), + prev_randao, + suggested_fee_recipient, transactions, no_tx_pool: true, - gas_limit: U64([l1_info.system_config.gas_limit.try_into().unwrap()]), + gas_limit: alloy_primitives::U64::from(l1_info.system_config.gas_limit), withdrawals, epoch, l1_inclusion_block, @@ -118,7 +117,7 @@ impl Attributes { /// Returns a [RawTransaction] vector containing all of the transactions for the L2 block. fn derive_transactions( &self, - input: BlockInput, + input: &BlockInput, l1_info: &L1Info, ) -> Vec { let mut transactions = Vec::new(); @@ -145,7 +144,7 @@ impl Attributes { } // Remaining transactions - let mut rest = input.transactions; + let mut rest = input.transactions.clone(); transactions.append(&mut rest); transactions @@ -428,28 +427,25 @@ impl UserDeposited { } } -impl TryFrom for UserDeposited { +impl TryFrom for UserDeposited { type Error = eyre::Report; /// Converts the emitted L1 deposit event log into [UserDeposited] - fn try_from(log: Log) -> Result { - let opaque_data = decode(&[ParamType::Bytes], &log.data)?[0] + fn try_from(log: alloy_rpc_types::Log) -> Result { + let opaque_data = decode(&[ParamType::Bytes], &log.data().data)?[0] .clone() .into_bytes() .unwrap(); - let from = Address::from(log.topics[1]); - let to = Address::from(log.topics[2]); + let from = Address::from_slice(log.topics()[1].as_slice()); + let to = Address::from_slice(log.topics()[2].as_slice()); let mint = U256::from_big_endian(&opaque_data[0..32]); let value = U256::from_big_endian(&opaque_data[32..64]); let gas = u64::from_be_bytes(opaque_data[64..72].try_into()?); let is_creation = opaque_data[72] != 0; let data = opaque_data[73..].to_vec(); - let l1_block_num = log - .block_number - .ok_or(eyre::eyre!("block num not found"))? - .as_u64(); + let l1_block_num = log.block_number.ok_or(eyre::eyre!("block num not found"))?; let l1_block_hash = log.block_hash.ok_or(eyre::eyre!("block hash not found"))?; let log_index = log.log_index.unwrap(); @@ -463,8 +459,8 @@ impl TryFrom for UserDeposited { is_creation, data, l1_block_num, - l1_block_hash, - log_index, + l1_block_hash: H256::from_slice(l1_block_hash.as_slice()), + log_index: log_index.into(), }) } } diff --git a/src/driver/engine_driver.rs b/src/driver/engine_driver.rs index 50c02303..97c72923 100644 --- a/src/driver/engine_driver.rs +++ b/src/driver/engine_driver.rs @@ -1,6 +1,5 @@ use std::sync::Arc; -use alloy_primitives::B256; use ethers::providers::{Http, Middleware, Provider}; use ethers::types::Transaction; use ethers::{ @@ -38,7 +37,8 @@ pub struct EngineDriver { impl EngineDriver { /// Initiates validation & production of a new L2 block from the given [PayloadAttributes] and updates the forkchoice pub async fn handle_attributes(&mut self, attributes: PayloadAttributes) -> Result<()> { - let block: Option> = self.block_at(attributes.timestamp.as_u64()).await; + let timestamp: u64 = attributes.timestamp.try_into()?; + let block: Option> = self.block_at(timestamp).await; if let Some(block) = block { if should_skip(&block, &attributes)? { @@ -100,10 +100,10 @@ impl EngineDriver { let payload = self.build_payload(attributes).await?; let new_head = BlockInfo { - number: payload.block_number.as_u64(), - hash: B256::from_slice(payload.block_hash.as_bytes()), - parent_hash: B256::from_slice(payload.parent_hash.as_bytes()), - timestamp: payload.timestamp.as_u64(), + number: payload.block_number.try_into().unwrap_or_default(), + hash: payload.block_hash, + parent_hash: payload.parent_hash, + timestamp: payload.timestamp.try_into().unwrap_or_default(), }; self.push_payload(payload).await?; @@ -199,9 +199,9 @@ impl EngineDriver { /// - `finalized_block` = `finalized_head` fn create_forkchoice_state(&self) -> ForkchoiceState { ForkchoiceState { - head_block_hash: H256::from_slice(self.unsafe_head.hash.as_slice()), - safe_block_hash: H256::from_slice(self.safe_head.hash.as_slice()), - finalized_block_hash: H256::from_slice(self.finalized_head.hash.as_slice()), + head_block_hash: self.unsafe_head.hash, + safe_block_hash: self.safe_head.hash, + finalized_block_hash: self.finalized_head.hash, } } @@ -243,10 +243,12 @@ fn should_skip(block: &Block, attributes: &PayloadAttributes) -> Re tracing::debug!("block hashes: {:?}", block_hashes); let is_same = attributes_hashes == block_hashes - && attributes.timestamp.as_u64() == block.timestamp.as_u64() - && attributes.prev_randao == block.mix_hash.unwrap() - && attributes.suggested_fee_recipient == block.author.unwrap() - && attributes.gas_limit.as_u64() == block.gas_limit.as_u64(); + && attributes.timestamp == alloy_primitives::U64::from(block.timestamp.as_u64()) + && attributes.prev_randao + == alloy_primitives::B256::from_slice(block.mix_hash.unwrap().as_bytes()) + && attributes.suggested_fee_recipient + == alloy_primitives::Address::from_slice(block.author.unwrap().as_bytes()) + && attributes.gas_limit == alloy_primitives::U64::from(block.gas_limit.as_u64()); Ok(is_same) } diff --git a/src/driver/mod.rs b/src/driver/mod.rs index 522fa720..2a02ebfd 100644 --- a/src/driver/mod.rs +++ b/src/driver/mod.rs @@ -4,8 +4,8 @@ use std::{ time::Duration, }; +use alloy_primitives::Address; use ethers::providers::{Http, Provider}; -use ethers::types::Address; use eyre::Result; use reqwest::Url; use tokio::{ @@ -100,7 +100,7 @@ impl Driver { let _addr = rpc::run_server(config.clone()).await?; - let signer = Address::from_slice(config.chain.system_config.unsafe_block_signer.as_slice()); + let signer = config.chain.system_config.unsafe_block_signer; let (unsafe_block_signer_sender, unsafe_block_signer_recv) = watch::channel(signer); let (block_handler, unsafe_block_recv) = @@ -230,16 +230,19 @@ impl Driver { } self.future_unsafe_blocks.retain(|payload| { - let unsafe_block_num = payload.block_number.as_u64(); + let unsafe_block_num: u64 = match payload.block_number.try_into() { + Ok(num) => num, + Err(_) => return false, + }; let synced_block_num = self.engine_driver.unsafe_head.number; unsafe_block_num > synced_block_num && unsafe_block_num - synced_block_num < 1024 }); - let next_unsafe_payload = self.future_unsafe_blocks.iter().find(|p| { - alloy_primitives::B256::from_slice(p.parent_hash.as_bytes()) - == self.engine_driver.unsafe_head.hash - }); + let next_unsafe_payload = self + .future_unsafe_blocks + .iter() + .find(|p| p.parent_hash == self.engine_driver.unsafe_head.hash); if let Some(payload) = next_unsafe_payload { _ = self.engine_driver.handle_unsafe_payload(payload).await; diff --git a/src/engine/fork.rs b/src/engine/fork.rs index c078a5b4..db5a6280 100644 --- a/src/engine/fork.rs +++ b/src/engine/fork.rs @@ -1,5 +1,4 @@ use alloy_primitives::B256; -use ethers::types::H256; use serde::{Deserialize, Serialize}; use super::{PayloadId, PayloadStatus}; @@ -23,23 +22,22 @@ pub struct ForkChoiceUpdate { #[serde(rename_all = "camelCase")] pub struct ForkchoiceState { /// 32 byte block hash of the head of the canonical chain - pub head_block_hash: H256, + pub head_block_hash: B256, /// 32 byte "safe" block hash of the canonical chain under certain synchrony and honesty assumptions /// This value MUST be either equal to or an ancestor of headBlockHash - pub safe_block_hash: H256, + pub safe_block_hash: B256, /// 32 byte block hash of the most recent finalized block - pub finalized_block_hash: H256, + pub finalized_block_hash: B256, } impl ForkchoiceState { /// Creates a new fork choice state with the given head block hash. /// The safe and finalized block hashes are set to the head block hash. pub fn from_single_head(head_block_hash: B256) -> Self { - let hash = H256::from_slice(head_block_hash.as_slice()); Self { - head_block_hash: hash, - safe_block_hash: hash, - finalized_block_hash: hash, + head_block_hash, + safe_block_hash: head_block_hash, + finalized_block_hash: head_block_hash, } } } diff --git a/src/engine/payload.rs b/src/engine/payload.rs index 533e453b..f14f74d5 100644 --- a/src/engine/payload.rs +++ b/src/engine/payload.rs @@ -1,8 +1,8 @@ use alloy_consensus::TxEnvelope; use alloy_eips::eip2718::Encodable2718; -use alloy_rpc_types::Block as AlloyBlock; +use alloy_primitives::{Address, Bytes, B256, U64}; +use alloy_rpc_types::Block; use alloy_rpc_types::BlockTransactions; -use ethers::types::{Block, Bytes, Transaction, H160, H256, U64}; use eyre::Result; use serde::{Deserialize, Serialize}; @@ -16,17 +16,17 @@ use crate::{ #[serde(rename_all = "camelCase")] pub struct ExecutionPayload { /// A 32 byte hash of the parent payload - pub parent_hash: H256, - /// A 20 byte hash (aka Address) for the feeRecipient field of the new payload - pub fee_recipient: H160, + pub parent_hash: B256, + /// The fee recipient of the new payload + pub fee_recipient: Address, /// A 32 byte state root hash - pub state_root: H256, + pub state_root: B256, /// A 32 byte receipt root hash - pub receipts_root: H256, + pub receipts_root: B256, /// A 32 byte logs bloom filter pub logs_bloom: Bytes, /// A 32 byte beacon chain randomness value - pub prev_randao: H256, + pub prev_randao: B256, /// A 64 bit number for the current block index pub block_number: U64, /// A 64 bit value for the gas limit @@ -40,7 +40,7 @@ pub struct ExecutionPayload { /// 256 bits for the base fee per gas pub base_fee_per_gas: U64, /// The 32 byte block hash - pub block_hash: H256, + pub block_hash: B256, /// An array of transaction objects where each object is a byte list pub transactions: Vec, /// An array of beaconchain withdrawals. Always empty as this exists only for L1 compatibility @@ -54,11 +54,11 @@ pub struct ExecutionPayload { pub excess_blob_gas: Option, } -impl TryFrom for ExecutionPayload { +impl TryFrom for ExecutionPayload { type Error = eyre::Report; /// Converts a [Block] to an [ExecutionPayload] - fn try_from(value: AlloyBlock) -> Result { + fn try_from(value: Block) -> Result { let txs = if let BlockTransactions::Full(txs) = value.transactions { txs } else { @@ -75,62 +75,42 @@ impl TryFrom for ExecutionPayload { .to_vec(); Ok(ExecutionPayload { - parent_hash: H256::from_slice(value.header.parent_hash.as_slice()), - fee_recipient: H160::from_slice(SystemAccounts::default().fee_vault.as_slice()), - state_root: H256::from_slice(value.header.state_root.as_slice()), - receipts_root: H256::from_slice(value.header.receipts_root.as_slice()), + parent_hash: value.header.parent_hash, + fee_recipient: SystemAccounts::default().fee_vault, + state_root: value.header.state_root, + receipts_root: value.header.receipts_root, logs_bloom: value.header.logs_bloom.0.to_vec().into(), - prev_randao: H256::from_slice(value.header.mix_hash.unwrap().as_slice()), - block_number: value.header.number.unwrap().into(), - gas_limit: (value.header.gas_limit as u64).into(), - gas_used: (value.header.gas_used as u64).into(), - timestamp: value.header.timestamp.into(), + prev_randao: value + .header + .mix_hash + .ok_or_else(|| eyre::eyre!("Missing mix hash"))?, + block_number: value + .header + .number + .ok_or_else(|| eyre::eyre!("Missing block number"))? + .try_into()?, + gas_limit: (value.header.gas_limit as u64).try_into()?, + gas_used: (value.header.gas_used as u64).try_into()?, + timestamp: value.header.timestamp.try_into()?, extra_data: Bytes::from(value.header.extra_data.0), base_fee_per_gas: (value.header.base_fee_per_gas.unwrap_or_else(|| 0u64.into()) as u64) - .into(), - block_hash: H256::from_slice(value.header.hash.unwrap().as_slice()), + .try_into()?, + block_hash: value + .header + .hash + .ok_or_else(|| eyre::eyre!("Missing block hash"))?, transactions: encoded_txs, withdrawals: Some(Vec::new()), - blob_gas_used: value.header.blob_gas_used.map(|v| (v as u64).into()), - excess_blob_gas: value.header.excess_blob_gas.map(|v| (v as u64).into()), - }) - } -} - -impl TryFrom> for ExecutionPayload { - type Error = eyre::Report; - - /// Converts a [Block] to an [ExecutionPayload] - fn try_from(value: Block) -> Result { - let encoded_txs = (*value - .transactions - .into_iter() - .map(|tx| RawTransaction(tx.rlp().to_vec())) - .collect::>()) - .to_vec(); - - Ok(ExecutionPayload { - parent_hash: value.parent_hash, - fee_recipient: H160::from_slice(SystemAccounts::default().fee_vault.as_slice()), - state_root: value.state_root, - receipts_root: value.receipts_root, - logs_bloom: value.logs_bloom.unwrap().as_bytes().to_vec().into(), - prev_randao: value.mix_hash.unwrap(), - block_number: value.number.unwrap(), - gas_limit: value.gas_limit.as_u64().into(), - gas_used: value.gas_used.as_u64().into(), - timestamp: value.timestamp.as_u64().into(), - extra_data: value.extra_data.clone(), - base_fee_per_gas: value - .base_fee_per_gas - .unwrap_or_else(|| 0u64.into()) - .as_u64() - .into(), - block_hash: value.hash.unwrap(), - transactions: encoded_txs, - withdrawals: Some(Vec::new()), - blob_gas_used: value.blob_gas_used.map(|v| v.as_u64().into()), - excess_blob_gas: value.excess_blob_gas.map(|v| v.as_u64().into()), + blob_gas_used: value + .header + .blob_gas_used + .map(|v| (v as u64).try_into()) + .transpose()?, + excess_blob_gas: value + .header + .excess_blob_gas + .map(|v| (v as u64).try_into()) + .transpose()?, }) } } @@ -145,9 +125,9 @@ pub struct PayloadAttributes { /// 64 bit value for the timestamp field of the new payload. pub timestamp: U64, /// 32 byte value for the prevRandao field of the new payload. - pub prev_randao: H256, + pub prev_randao: B256, /// 20 bytes suggested value for the feeRecipient field of the new payload. - pub suggested_fee_recipient: H160, + pub suggested_fee_recipient: Address, /// Array of transactions to be included in the new payload. pub transactions: Option>, /// Boolean value indicating whether or not the payload should be built without including transactions from the txpool. @@ -187,7 +167,7 @@ pub struct PayloadStatus { /// The status of the payload. pub status: Status, /// 32 Bytes - the hash of the most recent valid block in the branch defined by payload and its ancestors - pub latest_valid_hash: Option, + pub latest_valid_hash: Option, /// A message providing additional details on the validation error if the payload is classified as INVALID or INVALID_BLOCK_HASH. #[serde(default)] pub validation_error: Option, @@ -213,37 +193,31 @@ pub enum Status { #[cfg(test)] mod tests { - - use ethers::{ - providers::{Http, Middleware, Provider}, - types::H256, - }; - use eyre::Result; - use crate::engine::ExecutionPayload; + use alloy_primitives::{b256, uint}; + use alloy_provider::{Provider, ProviderBuilder}; + use eyre::Result; #[tokio::test] async fn test_from_block_hash_to_execution_paylaod() -> Result<()> { - if std::env::var("L2_TEST_RPC_URL").is_ok() { - let checkpoint_hash: H256 = - "0xc2794a16acacd9f7670379ffd12b6968ff98e2a602f57d7d1f880220aa5a4973".parse()?; + let Ok(l2_rpc_url) = std::env::var("L2_TEST_RPC_URL") else { + return Ok(()); + }; + let checkpoint_hash = + b256!("c2794a16acacd9f7670379ffd12b6968ff98e2a602f57d7d1f880220aa5a4973"); + let url = reqwest::Url::parse(&l2_rpc_url)?; + let l2_provider = ProviderBuilder::new().on_http(url); - let l2_rpc = std::env::var("L2_TEST_RPC_URL")?; - let checkpoint_sync_url = Provider::::try_from(l2_rpc)?; - let checkpoint_block = checkpoint_sync_url - .get_block_with_txs(checkpoint_hash) - .await? - .unwrap(); + let checkpoint_block = l2_provider + .get_block(checkpoint_hash.into(), true) + .await? + .unwrap(); - let payload = ExecutionPayload::try_from(checkpoint_block)?; + let payload = ExecutionPayload::try_from(checkpoint_block)?; - assert_eq!( - payload.block_hash, - "0xc2794a16acacd9f7670379ffd12b6968ff98e2a602f57d7d1f880220aa5a4973".parse()? - ); - assert_eq!(payload.block_number, 8453214u64.into()); - assert_eq!(payload.base_fee_per_gas, 50u64.into()); - } + assert_eq!(payload.block_hash, checkpoint_hash); + assert_eq!(payload.block_number, uint!(8453214U64)); + assert_eq!(payload.base_fee_per_gas, uint!(50U64)); Ok(()) } diff --git a/src/l1/chain_watcher.rs b/src/l1/chain_watcher.rs index 6f9ff2fa..90a7b364 100644 --- a/src/l1/chain_watcher.rs +++ b/src/l1/chain_watcher.rs @@ -4,7 +4,7 @@ use eyre::Result; use once_cell::sync::Lazy; use tokio::{spawn, sync::mpsc, task::JoinHandle, time::sleep}; -use alloy_primitives::{keccak256, Address, Bytes, B256, U256}; +use alloy_primitives::{keccak256, Address, Bytes, B256}; use alloy_provider::{Provider, ProviderBuilder, ReqwestProvider}; use alloy_rpc_types::{Block, BlockId, BlockNumberOrTag, BlockTransactions, Filter, Transaction}; @@ -330,8 +330,6 @@ impl InnerWatcher { config.unsafe_block_signer = addr; } } - - let update_block: u64 = update_block; self.system_config_update = (update_block, Some(config)); } else { self.system_config_update = (to_block, None); @@ -399,33 +397,26 @@ impl InnerWatcher { Some(deposits) => Ok(deposits), None => { let end_block = self.head_block.min(block_num + 1000); + let deposit_filter = Filter::new() + .address(self.config.chain.deposit_contract) + .event_signature(*TRANSACTION_DEPOSITED_TOPIC) + .from_block(block_num) + .to_block(end_block); + + let deposit_logs = self + .provider + .get_logs(&deposit_filter) + .await? + .into_iter() + .map(|log| UserDeposited::try_from(log).unwrap()) + .collect::>(); - // TODO: Optimize this to batch requests. - // Right now we need to do this since logs don't contain the block data. for num in block_num..=end_block { - let deposit_filter = Filter::new() - .address(self.config.chain.deposit_contract) - .event_signature(*TRANSACTION_DEPOSITED_TOPIC) - .select(num); - - let block = self - .provider - .get_block(BlockId::Number(BlockNumberOrTag::Number(block_num)), false) - .await? - .ok_or(eyre::eyre!("block not found"))?; - let block_hash = block - .header - .hash - .ok_or(eyre::eyre!("block hash not found"))?; - - let deposits = self - .provider - .get_logs(&deposit_filter) - .await? - .into_iter() - .enumerate() - .map(|(i, log)| UserDeposited::new(log, num, block_hash, U256::from(i))) - .collect::>>()?; + let deposits = deposit_logs + .iter() + .filter(|d| d.l1_block_num == num) + .cloned() + .collect(); self.deposits.insert(num, deposits); } @@ -480,8 +471,10 @@ impl InnerWatcher { return Ok(batcher_transactions_data); } - let timestamp: u64 = block.header.timestamp; - let slot = self.blob_fetcher.get_slot_from_time(timestamp).await?; + let slot = self + .blob_fetcher + .get_slot_from_time(block.header.timestamp) + .await?; // perf: fetch only the required indexes instead of all let blobs = self.blob_fetcher.fetch_blob_sidecars(slot).await?; diff --git a/src/network/handlers/block_handler.rs b/src/network/handlers/block_handler.rs index 54868c9d..b84f451b 100644 --- a/src/network/handlers/block_handler.rs +++ b/src/network/handlers/block_handler.rs @@ -1,8 +1,7 @@ use std::sync::mpsc::{channel, Receiver, Sender}; use std::time::SystemTime; -use ethers::types::{Address, Bytes, Signature, H256}; -use ethers::utils::keccak256; +use alloy_primitives::{keccak256, Address, Bytes, Signature, B256, U64}; use eyre::Result; use libp2p::gossipsub::{IdentTopic, Message, MessageAcceptance, TopicHash}; use ssz_rs::{prelude::*, List, Vector, U256}; @@ -32,7 +31,7 @@ struct ExecutionPayloadEnvelope { signature: Signature, hash: PayloadHash, #[allow(unused)] - parent_beacon_block_root: Option, + parent_beacon_block_root: Option, } impl Handler for BlockHandler { @@ -102,13 +101,17 @@ impl BlockHandler { .unwrap() .as_secs(); - let is_future = envelope.payload.timestamp.as_u64() > current_timestamp + 5; - let is_past = envelope.payload.timestamp.as_u64() < current_timestamp - 60; + let timestamp: u64 = envelope.payload.timestamp.try_into().unwrap_or_default(); + let is_future = timestamp > current_timestamp + 5; + let is_past = timestamp < current_timestamp - 60; let time_valid = !(is_future || is_past); let msg = envelope.hash.signature_message(self.chain_id); let block_signer = *self.unsafe_signer_recv.borrow(); - let sig_valid = envelope.signature.verify(msg, block_signer).is_ok(); + let sig_valid = envelope + .signature + .recover_address_from_prehash(&msg) + .map_or(false, |addr| addr == block_signer); time_valid && sig_valid } @@ -153,7 +156,7 @@ fn decode_post_ecotone_block_msg(data: Vec) -> Result) -> Result for PayloadHash { /// Returns the Keccak256 hash of a sequence of bytes fn from(value: &[u8]) -> Self { - Self(keccak256(value).into()) + Self(keccak256(value)) } } impl PayloadHash { /// The expected message that should be signed by the unsafe block signer. - fn signature_message(&self, chain_id: u64) -> H256 { - let domain = H256::zero(); - let chain_id = H256::from_low_u64_be(chain_id); + fn signature_message(&self, chain_id: u64) -> B256 { + let domain = B256::ZERO; + let chain_id = B256::from_slice(&chain_id.to_be_bytes()); let payload_hash = self.0; let data: Vec = [ - domain.as_bytes(), - chain_id.as_bytes(), - payload_hash.as_bytes(), + domain.as_slice(), + chain_id.as_slice(), + payload_hash.as_slice(), ] .concat(); - keccak256(data).into() + keccak256(data) } } @@ -245,10 +248,10 @@ impl From for ExecutionPayload { receipts_root: convert_hash(value.receipts_root), logs_bloom: convert_byte_vector(value.logs_bloom), prev_randao: convert_hash(value.prev_randao), - block_number: value.block_number.into(), - gas_limit: value.gas_limit.into(), - gas_used: value.gas_used.into(), - timestamp: value.timestamp.into(), + block_number: value.block_number.try_into().unwrap_or_default(), + gas_limit: value.gas_limit.try_into().unwrap_or_default(), + gas_used: value.gas_used.try_into().unwrap_or_default(), + timestamp: value.timestamp.try_into().unwrap_or_default(), extra_data: convert_byte_list(value.extra_data), base_fee_per_gas: convert_uint(value.base_fee_per_gas), block_hash: convert_hash(value.block_hash), @@ -319,10 +322,10 @@ impl From for ExecutionPayload { receipts_root: convert_hash(value.receipts_root), logs_bloom: convert_byte_vector(value.logs_bloom), prev_randao: convert_hash(value.prev_randao), - block_number: value.block_number.into(), - gas_limit: value.gas_limit.into(), - gas_used: value.gas_used.into(), - timestamp: value.timestamp.into(), + block_number: value.block_number.try_into().unwrap_or_default(), + gas_limit: value.gas_limit.try_into().unwrap_or_default(), + gas_used: value.gas_used.try_into().unwrap_or_default(), + timestamp: value.timestamp.try_into().unwrap_or_default(), extra_data: convert_byte_list(value.extra_data), base_fee_per_gas: convert_uint(value.base_fee_per_gas), block_hash: convert_hash(value.block_hash), @@ -364,24 +367,24 @@ impl From for ExecutionPayload { receipts_root: convert_hash(value.receipts_root), logs_bloom: convert_byte_vector(value.logs_bloom), prev_randao: convert_hash(value.prev_randao), - block_number: value.block_number.into(), - gas_limit: value.gas_limit.into(), - gas_used: value.gas_used.into(), - timestamp: value.timestamp.into(), + block_number: value.block_number.try_into().unwrap_or_default(), + gas_limit: value.gas_limit.try_into().unwrap_or_default(), + gas_used: value.gas_used.try_into().unwrap_or_default(), + timestamp: value.timestamp.try_into().unwrap_or_default(), extra_data: convert_byte_list(value.extra_data), base_fee_per_gas: convert_uint(value.base_fee_per_gas), block_hash: convert_hash(value.block_hash), transactions: convert_tx_list(value.transactions), withdrawals: Some(Vec::new()), - blob_gas_used: Some(value.blob_gas_used.into()), - excess_blob_gas: Some(value.excess_blob_gas.into()), + blob_gas_used: Some(value.blob_gas_used.try_into().unwrap_or_default()), + excess_blob_gas: Some(value.excess_blob_gas.try_into().unwrap_or_default()), } } } -/// Converts [Bytes32] into [H256] -fn convert_hash(bytes: Bytes32) -> H256 { - H256::from_slice(bytes.as_slice()) +/// Converts [Bytes32] into [B256] +fn convert_hash(bytes: Bytes32) -> B256 { + B256::from_slice(bytes.as_slice()) } /// Converts [VecAddress] into [Address] @@ -399,12 +402,10 @@ fn convert_byte_list(list: List) -> Bytes { Bytes::from(list.to_vec()) } -/// Converts a [U256] into [ethers::types::U64] -fn convert_uint(value: U256) -> ethers::types::U64 { +/// Converts a [U256] into [U64] +fn convert_uint(value: U256) -> U64 { let bytes = value.to_bytes_le(); - ethers::types::U256::from_little_endian(&bytes) - .as_u64() - .into() + U64::from_le_slice(&bytes) } /// Converts [ssz_rs::List] of [Transaction] into a vector of [RawTransaction]