From 8139286ae8a751cbdc87c7b65818543330ba52f6 Mon Sep 17 00:00:00 2001 From: refcell Date: Tue, 11 Jun 2024 10:01:15 -0600 Subject: [PATCH] fix(kona-derive): reuse upstream reqwest provider (#229) --- crates/derive/Cargo.toml | 16 ++++--- crates/derive/src/online/alloy_providers.rs | 50 ++++++++++----------- crates/derive/src/online/beacon_client.rs | 31 +++++++------ crates/derive/src/online/mod.rs | 2 +- 4 files changed, 51 insertions(+), 48 deletions(-) diff --git a/crates/derive/Cargo.toml b/crates/derive/Cargo.toml index 7dbf7dc3f..32a7b9779 100644 --- a/crates/derive/Cargo.toml +++ b/crates/derive/Cargo.toml @@ -9,18 +9,20 @@ repository.workspace = true homepage.workspace = true [dependencies] -# Workspace -anyhow.workspace = true -tracing.workspace = true +# Workspace Alloy Dependencies alloy-consensus.workspace = true alloy-primitives = { workspace = true, features = ["rlp"] } alloy-rlp = { workspace = true, features = ["derive"] } alloy-eips.workspace = true op-alloy-consensus.workspace = true -revm = { workspace = true, optional = true } -spin.workspace = true + +# Other Workspace Dependencies lru.workspace = true +spin.workspace = true +anyhow.workspace = true +tracing.workspace = true async-trait.workspace = true +revm = { workspace = true, optional = true } # Local kona-primitives = { path = "../primitives", version = "0.0.1" } @@ -37,7 +39,8 @@ serde = { version = "1.0.203", default-features = false, features = ["derive"], # `online` feature dependencies c-kzg = { version = "1.0.2", default-features = false, optional = true } sha2 = { version = "0.10.8", default-features = false, optional = true } -alloy-provider = { git = "https://github.com/alloy-rs/alloy", rev = "cb95183", optional = true} +alloy-transport = { git = "https://github.com/alloy-rs/alloy", rev = "cb95183", default-features = false, optional = true } +alloy-provider = { git = "https://github.com/alloy-rs/alloy", rev = "cb95183", default-features = false, optional = true } alloy-transport-http = { git = "https://github.com/alloy-rs/alloy", rev = "cb95183", optional = true } alloy-rpc-types = { git = "https://github.com/alloy-rs/alloy", rev = "cb95183", default-features = false, optional = true } serde_json = { version = "1.0.94", default-features = false, optional = true } @@ -47,7 +50,6 @@ reqwest = { version = "0.12.4", default-features = false, optional = true } alloy-node-bindings = { git = "https://github.com/alloy-rs/alloy", rev = "cb95183", default-features = false, optional = true } tracing-subscriber = { version = "0.3.18", optional = true } alloy-rpc-client = { git = "https://github.com/alloy-rs/alloy", rev = "cb95183", default-features = false, optional = true } -alloy-transport = { git = "https://github.com/alloy-rs/alloy", rev = "cb95183", default-features = false, optional = true } [dev-dependencies] tokio = { version = "1.38", features = ["full"] } diff --git a/crates/derive/src/online/alloy_providers.rs b/crates/derive/src/online/alloy_providers.rs index 12821efd2..ad497bdc9 100644 --- a/crates/derive/src/online/alloy_providers.rs +++ b/crates/derive/src/online/alloy_providers.rs @@ -11,17 +11,14 @@ use crate::{ use alloc::{boxed::Box, sync::Arc, vec::Vec}; use alloy_consensus::{Header, Receipt, ReceiptWithBloom, TxEnvelope, TxType}; use alloy_primitives::{Bytes, B256, U64}; +use alloy_provider::{Provider, ReqwestProvider}; use alloy_rlp::{Buf, Decodable}; -use alloy_rpc_client::RpcClient; +use alloy_transport::TransportResult; use anyhow::{anyhow, Result}; use async_trait::async_trait; use core::num::NonZeroUsize; use lru::LruCache; -// todo: once alloy revision is bumped, use defined ReqwestClient type. -/// A client using a [`reqwest`] HTTP transport. -pub type ReqwestClient = RpcClient>; - const CACHE_SIZE: usize = 16; /// The [AlloyChainProvider] is a concrete implementation of the [ChainProvider] trait, providing @@ -33,7 +30,7 @@ const CACHE_SIZE: usize = 16; #[derive(Debug, Clone)] pub struct AlloyChainProvider { /// The inner Ethereum JSON-RPC provider. - inner: ReqwestClient, + inner: ReqwestProvider, /// `header_by_hash` LRU cache. header_by_hash_cache: LruCache, /// `block_info_by_number` LRU cache. @@ -46,7 +43,7 @@ pub struct AlloyChainProvider { impl AlloyChainProvider { /// Creates a new [AlloyChainProvider] with the given alloy provider. - pub fn new(inner: ReqwestClient) -> Self { + pub fn new(inner: ReqwestProvider) -> Self { Self { inner, header_by_hash_cache: LruCache::new(NonZeroUsize::new(CACHE_SIZE).unwrap()), @@ -60,7 +57,7 @@ impl AlloyChainProvider { /// Creates a new [AlloyChainProvider] from the provided [reqwest::Url]. pub fn new_http(url: reqwest::Url) -> Self { - let inner = ReqwestClient::new_http(url); + let inner = ReqwestProvider::new_http(url); Self::new(inner) } } @@ -72,8 +69,9 @@ impl ChainProvider for AlloyChainProvider { return Ok(header.clone()); } - let raw_header: Bytes = - self.inner.request("debug_getRawHeader", [hash]).await.map_err(|e| anyhow!(e))?; + let raw_header: TransportResult = + self.inner.raw_request("debug_getRawHeader".into(), [hash]).await; + let raw_header: Bytes = raw_header.map_err(|e| anyhow!(e))?; Header::decode(&mut raw_header.as_ref()).map_err(|e| anyhow!(e)) } @@ -82,11 +80,9 @@ impl ChainProvider for AlloyChainProvider { return Ok(*block_info); } - let raw_header: Bytes = self - .inner - .request("debug_getRawHeader", [U64::from(number)]) - .await - .map_err(|e| anyhow!(e))?; + let raw_header: TransportResult = + self.inner.raw_request("debug_getRawHeader".into(), [U64::from(number)]).await; + let raw_header: Bytes = raw_header.map_err(|e| anyhow!(e))?; let header = Header::decode(&mut raw_header.as_ref()).map_err(|e| anyhow!(e))?; let block_info = BlockInfo { @@ -104,8 +100,9 @@ impl ChainProvider for AlloyChainProvider { return Ok(receipts.clone()); } - let raw_receipts: Vec = - self.inner.request("debug_getRawReceipts", [hash]).await.map_err(|e| anyhow!(e))?; + let raw_receipts: TransportResult> = + self.inner.raw_request("debug_getRawReceipts".into(), [hash]).await; + let raw_receipts: Vec = raw_receipts.map_err(|e| anyhow!(e))?; let receipts = raw_receipts .iter() @@ -133,8 +130,9 @@ impl ChainProvider for AlloyChainProvider { return Ok(block_info_and_txs.clone()); } - let raw_block: Bytes = - self.inner.request("debug_getRawBlock", [hash]).await.map_err(|e| anyhow!(e))?; + let raw_block: TransportResult = + self.inner.raw_request("debug_getRawBlock".into(), [hash]).await; + let raw_block: Bytes = raw_block.map_err(|e| anyhow!(e))?; let block = Block::decode(&mut raw_block.as_ref()).map_err(|e| anyhow!(e))?; let block_info = BlockInfo { @@ -157,7 +155,7 @@ impl ChainProvider for AlloyChainProvider { #[derive(Debug, Clone)] pub struct AlloyL2ChainProvider { /// The inner Ethereum JSON-RPC provider. - inner: ReqwestClient, + inner: ReqwestProvider, /// The rollup configuration. rollup_config: Arc, /// `payload_by_number` LRU cache. @@ -170,7 +168,7 @@ pub struct AlloyL2ChainProvider { impl AlloyL2ChainProvider { /// Creates a new [AlloyL2ChainProvider] with the given alloy provider and [RollupConfig]. - pub fn new(inner: ReqwestClient, rollup_config: Arc) -> Self { + pub fn new(inner: ReqwestProvider, rollup_config: Arc) -> Self { Self { inner, rollup_config, @@ -182,7 +180,7 @@ impl AlloyL2ChainProvider { /// Creates a new [AlloyL2ChainProvider] from the provided [reqwest::Url]. pub fn new_http(url: reqwest::Url, rollup_config: Arc) -> Self { - let inner = ReqwestClient::new_http(url); + let inner = ReqwestProvider::new_http(url); Self::new(inner, rollup_config) } } @@ -205,11 +203,9 @@ impl L2ChainProvider for AlloyL2ChainProvider { return Ok(payload.clone()); } - let raw_block: Bytes = self - .inner - .request("debug_getRawBlock", [U64::from(number)]) - .await - .map_err(|e| anyhow!(e))?; + let raw_block: TransportResult = + self.inner.raw_request("debug_getRawBlock".into(), [U64::from(number)]).await; + let raw_block: Bytes = raw_block.map_err(|e| anyhow!(e))?; let block = OpBlock::decode(&mut raw_block.as_ref()).map_err(|e| anyhow!(e))?; let payload_envelope: L2ExecutionPayloadEnvelope = block.into(); diff --git a/crates/derive/src/online/beacon_client.rs b/crates/derive/src/online/beacon_client.rs index ac0a5e84a..ec568d062 100644 --- a/crates/derive/src/online/beacon_client.rs +++ b/crates/derive/src/online/beacon_client.rs @@ -1,10 +1,11 @@ //! Contains an online implementation of the [BeaconClient] trait. -use crate::{ - online::ReqwestClient, - types::{APIConfigResponse, APIGenesisResponse, APIGetBlobSidecarsResponse, IndexedBlobHash}, +use crate::types::{ + APIConfigResponse, APIGenesisResponse, APIGetBlobSidecarsResponse, IndexedBlobHash, }; use alloc::{boxed::Box, string::String}; +use alloy_provider::{Provider, ReqwestProvider}; +use alloy_transport::TransportResult; use async_trait::async_trait; /// The node version engine api method. @@ -46,18 +47,18 @@ pub trait BeaconClient { #[derive(Debug, Clone)] pub struct OnlineBeaconClient { /// The inner Ethereum JSON-RPC provider. - inner: ReqwestClient, + inner: ReqwestProvider, } impl OnlineBeaconClient { /// Creates a new instance of the [OnlineBeaconClient]. - pub fn new(inner: ReqwestClient) -> Self { + pub fn new(inner: ReqwestProvider) -> Self { Self { inner } } /// Creates a new [OnlineBeaconClient] from the provided [reqwest::Url]. pub fn new_http(url: reqwest::Url) -> Self { - let inner = ReqwestClient::new_http(url); + let inner = ReqwestProvider::new_http(url); Self::new(inner) } } @@ -65,15 +66,20 @@ impl OnlineBeaconClient { #[async_trait] impl BeaconClient for OnlineBeaconClient { async fn node_version(&self) -> anyhow::Result { - self.inner.request(VERSION_METHOD, ()).await.map_err(|e| anyhow::anyhow!(e)) + let res: TransportResult = self.inner.raw_request(VERSION_METHOD.into(), ()).await; + res.map_err(|e| anyhow::anyhow!(e)) } async fn config_spec(&self) -> anyhow::Result { - self.inner.request(SPEC_METHOD, ()).await.map_err(|e| anyhow::anyhow!(e)) + let res: TransportResult = + self.inner.raw_request(SPEC_METHOD.into(), ()).await; + res.map_err(|e| anyhow::anyhow!(e)) } async fn beacon_genesis(&self) -> anyhow::Result { - self.inner.request(GENESIS_METHOD, ()).await.map_err(|e| anyhow::anyhow!(e)) + let res: TransportResult = + self.inner.raw_request(GENESIS_METHOD.into(), ()).await; + res.map_err(|e| anyhow::anyhow!(e)) } async fn beacon_blob_side_cars( @@ -83,9 +89,8 @@ impl BeaconClient for OnlineBeaconClient { hashes: &[IndexedBlobHash], ) -> anyhow::Result { let method = alloc::format!("{}{}", SIDECARS_METHOD_PREFIX, slot); - self.inner - .request(method, (fetch_all_sidecars, hashes)) - .await - .map_err(|e| anyhow::anyhow!(e)) + let res: TransportResult = + self.inner.raw_request(method.into(), (fetch_all_sidecars, hashes)).await; + res.map_err(|e| anyhow::anyhow!(e)) } } diff --git a/crates/derive/src/online/mod.rs b/crates/derive/src/online/mod.rs index 55aa774b0..2def4a8db 100644 --- a/crates/derive/src/online/mod.rs +++ b/crates/derive/src/online/mod.rs @@ -55,7 +55,7 @@ mod beacon_client; pub use beacon_client::{BeaconClient, OnlineBeaconClient}; mod alloy_providers; -pub use alloy_providers::{AlloyChainProvider, AlloyL2ChainProvider, ReqwestClient}; +pub use alloy_providers::{AlloyChainProvider, AlloyL2ChainProvider}; mod blob_provider; pub use blob_provider::{OnlineBlobProvider, SimpleSlotDerivation};