Skip to content

Commit

Permalink
chore(runner): Migrate to Alloy Types (#238)
Browse files Browse the repository at this point in the history
* chore(runner): port types and providers to alloy

* fix(cargo): use alloy-rlp published

* fix: alloy types

* fix: clippy lints:

* fix: clippy lints
  • Loading branch information
refcell authored Apr 30, 2024
1 parent 3dd50b3 commit 3fa1617
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 62 deletions.
74 changes: 59 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 @@ -28,9 +28,12 @@ futures-timer = "0.3.0"
again = "0.1"

# Alloy types
alloy-rlp = { version = "0.3", default-features = false }
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-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,
}
}
}
47 changes: 47 additions & 0 deletions src/engine/payload.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
use alloy_consensus::TxEnvelope;
use alloy_eips::eip2718::Encodable2718;
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 +54,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 = tx.try_into().unwrap();
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.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
7 changes: 2 additions & 5 deletions src/rpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ impl RpcServer for RpcServerImpl {
async fn output_at_block(&self, block_number: u64) -> Result<OutputRootResponse, Error> {
let url = reqwest::Url::parse(&self.config.l2_rpc_url)
.map_err(|err| Error::Custom(format!("unable to parse l2_rpc_url: {err}")))?;
let l2_provider = ProviderBuilder::new()
.on_http(url)
.map_err(|err| Error::Custom(format!("unable to create provider: {err}")))?;
let l2_provider = ProviderBuilder::new().on_http(url);

let block = convert_err(
l2_provider
Expand All @@ -69,14 +67,13 @@ impl RpcServer for RpcServerImpl {
.hash
.ok_or(Error::Custom("block hash not found".to_string()))?;
let locations = vec![];
let block_id = Some(BlockId::from(block_hash));

let state_proof = convert_err(
l2_provider
.get_proof(
self.config.chain.l2_to_l1_message_passer,
locations,
block_id,
BlockId::from(block_hash),
)
.await,
)?;
Expand Down
Loading

0 comments on commit 3fa1617

Please sign in to comment.