Skip to content

Commit

Permalink
feat: block backfill (#391)
Browse files Browse the repository at this point in the history
* wip

* feat: add execution block backfill

* handle finalized blocks

* cleanup

* fix timing in wasm

* remove web_sys from core

* typo

* rebase fixes
  • Loading branch information
ncitron authored Oct 11, 2024
1 parent 2a5b8b7 commit 31cfa61
Show file tree
Hide file tree
Showing 22 changed files with 327 additions and 145 deletions.
23 changes: 20 additions & 3 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ eyre.workspace = true
hex.workspace = true
tracing.workspace = true
thiserror.workspace = true
zduny-wasm-timer.workspace = true

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
jsonrpsee = { version = "0.19.0", features = ["full"] }
Expand All @@ -33,6 +32,7 @@ openssl.workspace = true
[target.'cfg(target_arch = "wasm32")'.dependencies]
wasm-bindgen-futures = "0.4.33"
gloo-timers = "0.3.0"
wasmtimer = "0.2.0"

[target.wasm32-unknown-unknown.dependencies]
parking_lot = { version = "0.12.2" }
6 changes: 3 additions & 3 deletions core/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ use alloy::primitives::{Address, Bytes, B256, U256};
use alloy::rpc::types::{Filter, Log, SyncStatus};
use eyre::Result;
use tracing::{info, warn};
use zduny_wasm_timer::Delay;

use crate::client::node::Node;
#[cfg(not(target_arch = "wasm32"))]
use crate::client::rpc::Rpc;
use crate::consensus::Consensus;
use crate::network_spec::NetworkSpec;
use crate::time::interval;
use crate::types::{Block, BlockTag};

pub mod node;
Expand Down Expand Up @@ -192,12 +192,12 @@ impl<N: NetworkSpec, C: Consensus<N::TransactionResponse>> Client<N, C> {
}

pub async fn wait_synced(&self) {
let mut interval = interval(Duration::from_millis(100));
loop {
interval.tick().await;
if let Ok(SyncStatus::None) = self.syncing().await {
break;
}

Delay::new(Duration::from_millis(100)).await.unwrap();
}
}
}
6 changes: 3 additions & 3 deletions core/src/client/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::sync::Arc;
use alloy::primitives::{Address, Bytes, B256, U256};
use alloy::rpc::types::{Filter, Log, SyncInfo, SyncStatus};
use eyre::{eyre, Result};
use zduny_wasm_timer::{SystemTime, UNIX_EPOCH};

use crate::consensus::Consensus;
use crate::errors::ClientError;
Expand All @@ -12,6 +11,7 @@ use crate::execution::rpc::http_rpc::HttpRpc;
use crate::execution::state::State;
use crate::execution::ExecutionClient;
use crate::network_spec::NetworkSpec;
use crate::time::{SystemTime, UNIX_EPOCH};
use crate::types::{Block, BlockTag};

pub struct Node<N: NetworkSpec, C: Consensus<N::TransactionResponse>> {
Expand All @@ -25,7 +25,7 @@ impl<N: NetworkSpec, C: Consensus<N::TransactionResponse>> Node<N, C> {
let block_recv = consensus.block_recv().take().unwrap();
let finalized_block_recv = consensus.finalized_block_recv().take().unwrap();

let state = State::new(block_recv, finalized_block_recv, 256);
let state = State::new(block_recv, finalized_block_recv, 256, execution_rpc);
let execution = Arc::new(
ExecutionClient::new(execution_rpc, state).map_err(ClientError::InternalError)?,
);
Expand Down Expand Up @@ -239,7 +239,7 @@ impl<N: NetworkSpec, C: Consensus<N::TransactionResponse>> Node<N, C> {
async fn check_head_age(&self) -> Result<(), ClientError> {
let timestamp = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.unwrap_or_else(|_| panic!("unreachable"))
.as_secs();

let block_timestamp = self
Expand Down
4 changes: 2 additions & 2 deletions core/src/execution/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ mod proof;
#[derive(Clone)]
pub struct ExecutionClient<N: NetworkSpec, R: ExecutionRpc<N>> {
pub rpc: R,
state: State<N>,
state: State<N, R>,
}

impl<N: NetworkSpec, R: ExecutionRpc<N>> ExecutionClient<N, R> {
pub fn new(rpc: &str, state: State<N>) -> Result<Self> {
pub fn new(rpc: &str, state: State<N, R>) -> Result<Self> {
let rpc: R = ExecutionRpc::new(rpc)?;
Ok(ExecutionClient::<N, R> { rpc, state })
}
Expand Down
14 changes: 12 additions & 2 deletions core/src/execution/rpc/http_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ use alloy::rpc::types::{BlockId, EIP1186AccountProofResponse, FeeHistory, Filter
use alloy::transports::http::Http;
use alloy::transports::layers::{RetryBackoffLayer, RetryBackoffService};
use async_trait::async_trait;
use eyre::Result;
use eyre::{eyre, Result};
use reqwest::Client;
use revm::primitives::AccessList;

use crate::errors::RpcError;
use crate::network_spec::NetworkSpec;
use crate::types::BlockTag;
use crate::types::{Block, BlockTag};

use super::ExecutionRpc;

Expand Down Expand Up @@ -190,4 +190,14 @@ impl<N: NetworkSpec> ExecutionRpc<N> for HttpRpc<N> {
.await
.map_err(|e| RpcError::new("fee_history", e))?)
}

async fn get_block(&self, hash: B256) -> Result<Block<N::TransactionResponse>> {
self.provider
.raw_request::<_, Option<Block<N::TransactionResponse>>>(
"eth_getBlockByHash".into(),
(hash, true),
)
.await?
.ok_or(eyre!("block not found"))
}
}
6 changes: 5 additions & 1 deletion core/src/execution/rpc/mock_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use eyre::{eyre, Result};

use super::ExecutionRpc;
use crate::network_spec::NetworkSpec;
use crate::types::BlockTag;
use crate::types::{Block, BlockTag};

#[derive(Clone)]
pub struct MockRpc {
Expand Down Expand Up @@ -89,6 +89,10 @@ impl<N: NetworkSpec> ExecutionRpc<N> for MockRpc {
Err(eyre!("not implemented"))
}

async fn get_block(&self, _hash: B256) -> Result<Block<N::TransactionResponse>> {
Err(eyre!("not implemented"))
}

async fn get_fee_history(
&self,
_block_count: u64,
Expand Down
3 changes: 2 additions & 1 deletion core/src/execution/rpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use async_trait::async_trait;
use eyre::Result;

use crate::network_spec::NetworkSpec;
use crate::types::BlockTag;
use crate::types::{Block, BlockTag};

pub mod http_rpc;
pub mod mock_rpc;
Expand Down Expand Up @@ -40,6 +40,7 @@ pub trait ExecutionRpc<N: NetworkSpec>: Send + Clone + Sync + 'static {
async fn get_new_block_filter(&self) -> Result<U256>;
async fn get_new_pending_transaction_filter(&self) -> Result<U256>;
async fn chain_id(&self) -> Result<u64>;
async fn get_block(&self, hash: B256) -> Result<Block<N::TransactionResponse>>;

async fn get_fee_history(
&self,
Expand Down
Loading

0 comments on commit 31cfa61

Please sign in to comment.