Skip to content

Commit

Permalink
Feat/support 4844 (risc0#35)
Browse files Browse the repository at this point in the history
* calc blob tx hash in raiko-host

* uusing patched ethers-core to support 4844 tx

* add cargo.lock

* code fmt

* add blob hash opt into taiko extra

* sync with latest taiko-mono

* move get blob into provider

Signed-off-by: smtmfft <[email protected]>

* update file provider

Signed-off-by: smtmfft <[email protected]>

* fix decode blob issue

* cargo fmt

* fix clippy

* sync with latest protocol

Signed-off-by: smtmfft <[email protected]>

* update pi calculation

Signed-off-by: smtmfft <[email protected]>

* update test & revert manifest change

Signed-off-by: smtmfft <[email protected]>

* remove debug print

Signed-off-by: smtmfft <[email protected]>

* code refine

Signed-off-by: smtmfft <[email protected]>

* update const

Signed-off-by: smtmfft <[email protected]>

---------

Signed-off-by: smtmfft <[email protected]>
  • Loading branch information
smtmfft authored and petarvujovic98 committed Mar 5, 2024
1 parent d6a567f commit f37830b
Show file tree
Hide file tree
Showing 11 changed files with 1,126 additions and 59 deletions.
37 changes: 37 additions & 0 deletions Cargo.lock

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

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ lto = true
[profile.release.build-override]
opt-level = 3

[patch.crates-io]
# use optimized risc0 circuit
revm = { git = "https://github.com/johntaiko/revm", branch = "feat/taiko" }
revm-primitives = { git = "https://github.com/johntaiko/revm", branch = "feat/taiko" }


[workspace.dependencies]
Expand Down
4 changes: 4 additions & 0 deletions lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ thiserror-no-std = "2.0.2"
zeth-primitives = { path = "../primitives", features = ["revm"] }
hex = { version = "0.4.3", default-features = false, optional = true}
url = "2.5.0"
reqwest = "0.11.22"
sha2 = "0.10.8"
proptest = "1.4.0"
c-kzg = "0.4.0"

# [target.'cfg(feature = "std")'.dependencies]
ethers-core = { version = "2.0", optional = true }
Expand Down
25 changes: 21 additions & 4 deletions lib/src/host/provider/cached_rpc_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ use ethers_core::types::{
};
use alloy_rpc_types::EIP1186AccountProofResponse;
use super::{
file_provider::FileProvider, rpc_provider::RpcProvider, AccountQuery, BlockQuery, MutProvider,
ProofQuery, Provider, StorageQuery,
file_provider::FileProvider, rpc_provider::RpcProvider, AccountQuery, BlockQuery,
GetBlobsResponse, MutProvider, ProofQuery, Provider, StorageQuery,
};
#[cfg(feature = "taiko")]
use crate::host::provider::LogsQuery;
Expand All @@ -34,12 +34,16 @@ pub struct CachedRpcProvider {
}

impl CachedRpcProvider {
pub fn new(cache_path: PathBuf, rpc_url: String) -> Result<Self> {
pub fn new(
cache_path: PathBuf,
rpc_url: String,
beacon_rpc_url: Option<String>,
) -> Result<Self> {
let cache = match FileProvider::from_file(&cache_path) {
Ok(provider) => provider,
Err(_) => FileProvider::empty(cache_path),
};
let rpc = RpcProvider::new(rpc_url)?;
let rpc = RpcProvider::new(rpc_url, beacon_rpc_url)?;

Ok(CachedRpcProvider { cache, rpc })
}
Expand Down Expand Up @@ -182,4 +186,17 @@ impl Provider for CachedRpcProvider {

Ok(out)
}

#[cfg(feature = "taiko")]
fn get_blob_data(&mut self, block_id: u64) -> Result<GetBlobsResponse> {
let cache_out = self.cache.get_blob_data(block_id);
if cache_out.is_ok() {
return cache_out;
}

let out = self.rpc.get_blob_data(block_id)?;
self.cache.insert_blob(block_id, out.clone());

Ok(out)
}
}
33 changes: 24 additions & 9 deletions lib/src/host/provider/file_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,7 @@ use ethers_core::types::{
use serde::{Deserialize, Serialize};
use serde_with::serde_as;

// #[cfg(feature = "taiko")]
// use zeth_primitives::taiko::BlockProposed;
use super::{AccountQuery, BlockQuery, MutProvider, ProofQuery, Provider, StorageQuery};
#[cfg(feature = "taiko")]
use super::{LogsQuery, TxQuery};

#[serde_as]
#[derive(Default, Deserialize, Serialize)]
Expand Down Expand Up @@ -63,8 +59,7 @@ pub struct FileProvider {
#[serde_as(as = "Vec<(_, _)>")]
logs: HashMap<LogsQuery, Vec<Log>>,
#[cfg(feature = "taiko")]
#[serde_as(as = "Vec<(_, _)>")]
transactions: HashMap<TxQuery, Transaction>,
propose: Option<(Transaction, BlockProposed)>,
}

impl FileProvider {
Expand All @@ -81,9 +76,7 @@ impl FileProvider {
code: HashMap::new(),
storage: HashMap::new(),
#[cfg(feature = "taiko")]
logs: HashMap::new(),
#[cfg(feature = "taiko")]
transactions: HashMap::new(),
propose: Default::default(),
}
}

Expand Down Expand Up @@ -174,6 +167,14 @@ impl Provider for FileProvider {
}
}

#[cfg(feature = "taiko")]
fn get_propose(&mut self, query: &super::ProposeQuery) -> Result<(Transaction, BlockProposed)> {
match self.propose {
Some(ref val) => Ok(val.clone()),
None => Err(anyhow!("No data for {:?}", query)),
}
}

#[cfg(feature = "taiko")]
fn get_logs(&mut self, query: &LogsQuery) -> Result<Vec<Log>> {
match self.logs.get(query) {
Expand All @@ -189,6 +190,14 @@ impl Provider for FileProvider {
None => Err(anyhow!("No data for {query:?}")),
}
}

#[cfg(feature = "taiko")]
fn get_blob_data(&mut self, block_id: u64) -> Result<GetBlobsResponse> {
match self.blobs.get(&block_id) {
Some(val) => Ok(val.clone()),
None => Err(anyhow!("No data for block id: {block_id:?}")),
}
}
}

impl MutProvider for FileProvider {
Expand Down Expand Up @@ -243,6 +252,12 @@ impl MutProvider for FileProvider {
self.transactions.insert(query, val);
self.dirty = true;
}

#[cfg(feature = "taiko")]
fn insert_blob(&mut self, block_id: u64, val: GetBlobsResponse) {
self.blobs.insert(block_id, val);
self.dirty = true;
}
}

#[cfg(feature = "taiko")]
Expand Down
2 changes: 1 addition & 1 deletion lib/src/host/provider/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ pub fn new_provider(
match (cache_path, rpc_url) {
(Some(cache_path), Some(rpc_url)) => new_cached_rpc_provider(cache_path, rpc_url),
(Some(cache_path), None) => new_file_provider(cache_path),
(None, Some(rpc_url)) => new_rpc_provider(rpc_url),
(None, Some(rpc_url)) => new_rpc_provider(rpc_url, beacon_rpc_url),
(None, None) => Err(anyhow!("No cache_path or rpc_url given")),
}
}
Expand Down
31 changes: 29 additions & 2 deletions lib/src/host/provider/rpc_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,24 @@ use alloy_transport_http::Http;
use hex::FromHex;
use url::Url;
use log::info;
use reqwest;
#[cfg(feature = "taiko")]
use tracing::info;
#[cfg(feature = "taiko")]
use zeth_primitives::taiko::{filter_propose_block_event, BlockProposed};

use super::{AccountQuery, BlockQuery, ProofQuery, Provider, StorageQuery};
use super::{AccountQuery, BlockQuery, GetBlobsResponse, ProofQuery, Provider, StorageQuery};
use crate::host::provider::LogsQuery;

pub struct RpcProvider {
ethers_provider: ethers_providers::Provider<RetryClient<ethers_Http>>,
alloy_provider: HttpProvider,
beacon_rpc_url: Option<String>,
tokio_handle: tokio::runtime::Handle,
}

impl RpcProvider {
pub fn new(rpc_url: String) -> Result<Self> {
pub fn new(rpc_url: String, beacon_rpc_url: Option<String>) -> Result<Self> {
//TODO(Brecht): switch to alloy provider for everything
let ethers_provider =
ethers_providers::Provider::<RetryClient<ethers_Http>>::new_client(&rpc_url, 3, 500)?;
Expand All @@ -49,6 +55,7 @@ impl RpcProvider {
Ok(RpcProvider {
ethers_provider,
alloy_provider,
beacon_rpc_url,
tokio_handle,
})
}
Expand Down Expand Up @@ -188,4 +195,24 @@ impl Provider for RpcProvider {
None => Err(anyhow!("No data for {query:?}")),
}
}

#[cfg(feature = "taiko")]
fn get_blob_data(&mut self, block_id: u64) -> Result<GetBlobsResponse> {
match self.beacon_rpc_url {
Some(ref url) => self.tokio_handle.block_on(async {
let url = format!("{}/eth/v1/beacon/blob_sidecars/{}", url, block_id);
let response = reqwest::get(url.clone()).await?;
if response.status().is_success() {
let blob_response: GetBlobsResponse = response.json().await?;
Ok(blob_response)
} else {
Err(anyhow::anyhow!(
"Request failed with status code: {}",
response.status()
))
}
}),
None => Err(anyhow!("No beacon_rpc_url given")),
}
}
}
Loading

0 comments on commit f37830b

Please sign in to comment.