Skip to content

Commit

Permalink
chore(runner): port types and providers to alloy
Browse files Browse the repository at this point in the history
  • Loading branch information
refcell committed Apr 30, 2024
1 parent 9d4c40d commit ebc0428
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 57 deletions.
72 changes: 57 additions & 15 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@ again = "0.1"

# Alloy types
alloy-primitives = { version = "0.7.1", default-features = false, features = ["serde"] }
alloy-provider = { git = "https://github.com/alloy-rs/alloy", rev = "e3f2f07" }
alloy-rpc-types = { git = "https://github.com/alloy-rs/alloy", rev = "e3f2f07" }
alloy-provider = { git = "https://github.com/alloy-rs/alloy" }
alloy-rpc-types = { git = "https://github.com/alloy-rs/alloy" }
alloy-consensus = { git = "https://github.com/alloy-rs/alloy" }
alloy-rlp = { git = "https://github.com/alloy-rs/alloy" }
alloy-eips = { git = "https://github.com/alloy-rs/alloy" }

# Logging and Metrics
chrono = "0.4.22"
Expand Down
10 changes: 6 additions & 4 deletions src/engine/fork.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use alloy_primitives::B256;
use ethers::types::H256;
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -33,11 +34,12 @@ pub struct ForkchoiceState {
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: H256) -> Self {
pub fn from_single_head(head_block_hash: B256) -> Self {
let hash = H256::from_slice(head_block_hash.as_slice());
Self {
head_block_hash,
safe_block_hash: head_block_hash,
finalized_block_hash: head_block_hash,
head_block_hash: hash,
safe_block_hash: hash,
finalized_block_hash: hash,
}
}
}
48 changes: 48 additions & 0 deletions src/engine/payload.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
use alloy_consensus::TxEnvelope;
use alloy_eips::eip2718::Encodable2718;
use alloy_rlp::Encodable;
use alloy_rpc_types::Block as AlloyBlock;
use alloy_rpc_types::BlockTransactions;
use ethers::types::{Block, Bytes, Transaction, H160, H256, U64};
use eyre::Result;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -50,6 +55,49 @@ pub struct ExecutionPayload {
pub excess_blob_gas: Option<U64>,
}

impl TryFrom<AlloyBlock> for ExecutionPayload {
type Error = eyre::Report;

/// Converts a [Block] to an [ExecutionPayload]
fn try_from(value: AlloyBlock) -> Result<Self> {
let txs = if let BlockTransactions::Full(txs) = value.transactions {
txs
} else {
return Err(eyre::eyre!("Expected full transactions"));
};
let encoded_txs = (txs
.into_iter()
.map(|tx| {
let envelope = TxEnvelope::from(tx);
let encoded = envelope.encoded_2718();
RawTransaction(encoded.to_vec())
})
.collect::<Vec<_>>())
.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()),
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 as u64).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()),
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<Block<Transaction>> for ExecutionPayload {
type Error = eyre::Report;

Expand Down
Loading

0 comments on commit ebc0428

Please sign in to comment.