Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into feat/eip4844
Browse files Browse the repository at this point in the history
  • Loading branch information
Wodann committed May 9, 2024
2 parents ebd08b7 + bd992ad commit 68db137
Show file tree
Hide file tree
Showing 64 changed files with 668 additions and 411 deletions.
File renamed without changes.
5 changes: 5 additions & 0 deletions .changeset/lovely-tables-impress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nomicfoundation/edr": patch
---

Upgraded revm to commit aceb093
5 changes: 5 additions & 0 deletions .changeset/old-crews-remain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nomicfoundation/edr": patch
---

Added retry for sporadic "missing trie node" JSON-RPC errors
7 changes: 3 additions & 4 deletions .github/workflows/edr-benchmark.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,16 @@ defaults:

concurrency:
group: ${{github.workflow}}-${{github.ref}}
cancel-in-progress: true
# Don't cancel in progress jobs in main
cancel-in-progress: ${{ github.ref != 'refs/heads/main' }}

jobs:
js-benchmark:
name: Run JS scenario runner benchmark
environment: github-action-benchmark
runs-on: self-hosted
# Only run for trusted collaborators since third-parties could run malicious code on the self-hosted benchmark runner.
# Temporarily disabled after moving to our own repo; see https://github.com/NomicFoundation/edr/issues/402
# if: github.ref == 'refs/heads/main' || (github.event.pull_request.author_association == 'OWNER' || github.event.pull_request.author_association == 'MEMBER' || github.event.pull_request.author_association == 'COLLABORATOR')
if: false
if: github.ref == 'refs/heads/main' || (github.event.pull_request.author_association == 'OWNER' || github.event.pull_request.author_association == 'MEMBER' || github.event.pull_request.author_association == 'COLLABORATOR')
steps:
- uses: actions/checkout@v3

Expand Down
12 changes: 6 additions & 6 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 crates/edr_eth/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ reqwest = { version = "0.11", features = ["blocking", "json"] }
reqwest-middleware = { version = "0.2.4", default-features = false }
reqwest-retry = { version = "0.3.0", default-features = false }
reqwest-tracing = { version = "0.4.7", default-features = false, optional = true }
revm-primitives = { git = "https://github.com/Wodann/revm", rev = "451f122", version = "3.1", default-features = false, features = ["c-kzg", "hashbrown"] }
revm-primitives = { git = "https://github.com/NomicFoundation/revm", rev = "aceb093", version = "3.1", default-features = false, features = ["c-kzg", "hashbrown"] }
serde = { version = "1.0.147", default-features = false, features = ["derive"], optional = true }
serde_json = { version = "1.0.89", optional = true }
sha2 = { version = "0.10.8", default-features = false }
Expand Down
7 changes: 4 additions & 3 deletions crates/edr_eth/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,10 @@ pub struct Header {
/// The block's nonce
// #[cfg_attr(feature = "serde", serde(with = "crate::serde::u64"))]
pub nonce: B64,
/// BaseFee was added by EIP-1559 and is ignored in legacy headers.
/// `BaseFee` was added by EIP-1559 and is ignored in legacy headers.
pub base_fee_per_gas: Option<U256>,
/// WithdrawalsHash was added by EIP-4895 and is ignored in legacy headers.
/// `WithdrawalsHash` was added by EIP-4895 and is ignored in legacy
/// headers.
pub withdrawals_root: Option<B256>,
/// Blob gas was added by EIP-4844 and is ignored in older headers.
#[cfg_attr(feature = "serde", serde(flatten))]
Expand Down Expand Up @@ -181,7 +182,7 @@ pub struct PartialHeader {
pub mix_hash: B256,
/// The block's nonce
pub nonce: B64,
/// BaseFee was added by EIP-1559 and is ignored in legacy headers.
/// `BaseFee` was added by EIP-1559 and is ignored in legacy headers.
pub base_fee: Option<U256>,
/// Blob gas was added by EIP-4844 and is ignored in older headers.
pub blob_gas: Option<BlobGas>,
Expand Down
2 changes: 1 addition & 1 deletion crates/edr_eth/src/block/difficulty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fn bomb_delay(spec_id: SpecId) -> u64 {
// SpecId::LONDON => 9500000, // EIP-3554
SpecId::ARROW_GLACIER => 10700000,
SpecId::GRAY_GLACIER => 11400000,
SpecId::MERGE | SpecId::SHANGHAI | SpecId::CANCUN | SpecId::LATEST => {
_ => {
unreachable!("Post-merge hardforks don't have a bomb delay")
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/edr_eth/src/block/reward.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ pub fn miner_reward(spec_id: SpecId) -> Option<U256> {
| SpecId::LONDON
| SpecId::ARROW_GLACIER
| SpecId::GRAY_GLACIER => Some(U256::from(2_000_000_000_000_000_000u128)),
SpecId::MERGE | SpecId::SHANGHAI | SpecId::CANCUN | SpecId::LATEST => None,
_ => None,
}
}
22 changes: 11 additions & 11 deletions crates/edr_eth/src/remote/cacheable_method_invocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,56 +25,56 @@ pub(super) fn try_write_cache_key(method: &RequestMethod) -> Option<WriteCacheKe
/// Potentially cacheable Ethereum JSON-RPC methods.
#[derive(Clone, Debug)]
enum CacheableRequestMethod<'a> {
/// eth_feeHistory
/// `eth_feeHistory`
FeeHistory {
block_count: &'a U256,
newest_block: CacheableBlockSpec<'a>,
reward_percentiles: &'a Option<Vec<RewardPercentile>>,
},
/// eth_getBalance
/// `eth_getBalance`
GetBalance {
address: &'a Address,
block_spec: CacheableBlockSpec<'a>,
},
/// eth_getBlockByNumber
/// `eth_getBlockByNumber`
GetBlockByNumber {
block_spec: CacheableBlockSpec<'a>,

/// include transaction data
include_tx_data: bool,
},
/// eth_getBlockByHash
/// `eth_getBlockByHash`
GetBlockByHash {
/// hash
block_hash: &'a B256,
/// include transaction data
include_tx_data: bool,
},
/// eth_getCode
/// `eth_getCode`
GetCode {
address: &'a Address,
block_spec: CacheableBlockSpec<'a>,
},
/// eth_getLogs
/// `eth_getLogs`
GetLogs {
params: CacheableLogFilterOptions<'a>,
},
/// eth_getStorageAt
/// `eth_getStorageAt`
GetStorageAt {
address: &'a Address,
position: &'a U256,
block_spec: CacheableBlockSpec<'a>,
},
/// eth_getTransactionByHash
/// `eth_getTransactionByHash`
GetTransactionByHash { transaction_hash: &'a B256 },
/// eth_getTransactionCount
/// `eth_getTransactionCount`
GetTransactionCount {
address: &'a Address,
block_spec: CacheableBlockSpec<'a>,
},
/// eth_getTransactionReceipt
/// `eth_getTransactionReceipt`
GetTransactionReceipt { transaction_hash: &'a B256 },
/// net_version
/// `net_version`
NetVersion,
}

Expand Down
67 changes: 45 additions & 22 deletions crates/edr_eth/src/remote/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::{
time::{Duration, Instant},
};

use futures::stream::StreamExt;
use futures::{future, stream::StreamExt, TryFutureExt};
use hyper::header::HeaderValue;
pub use hyper::{header, http::Error as HttpError, HeaderMap};
use reqwest::Client as HttpClient;
Expand Down Expand Up @@ -220,27 +220,38 @@ impl RpcClient {
})
}

fn parse_response_str<T: DeserializeOwned>(response: &str) -> Result<T, RpcClientError> {
serde_json::from_str(response).map_err(|error| RpcClientError::InvalidResponse {
response: response.to_string(),
expected_type: std::any::type_name::<T>(),
fn parse_response_str<T: DeserializeOwned>(
response: String,
) -> Result<jsonrpc::Response<T>, RpcClientError> {
serde_json::from_str(&response).map_err(|error| RpcClientError::InvalidResponse {
response,
expected_type: std::any::type_name::<jsonrpc::Response<T>>(),
error,
})
}

fn extract_result<T: DeserializeOwned>(
async fn retry_on_sporadic_failure<T: DeserializeOwned>(
&self,
error: jsonrpc::Error,
request: SerializedRequest,
response: String,
) -> Result<T, RpcClientError> {
let response: jsonrpc::Response<T> = Self::parse_response_str(&response)?;
let is_missing_trie_node_error =
error.code == -32000 && error.message.to_lowercase().contains("missing trie node");

response
.data
.into_result()
.map_err(|error| RpcClientError::JsonRpcError {
error,
request: request.to_json_string(),
})
let result = if is_missing_trie_node_error {
self.send_request_body(&request)
.await
.and_then(Self::parse_response_str)?
.data
.into_result()
} else {
Err(error)
};

result.map_err(|error| RpcClientError::JsonRpcError {
error,
request: request.to_json_string(),
})
}

async fn make_cache_path(&self, cache_key: &str) -> Result<PathBuf, RpcClientError> {
Expand Down Expand Up @@ -466,6 +477,23 @@ impl RpcClient {
Ok(())
}

async fn send_request_and_extract_result<T: DeserializeOwned>(
&self,
request: SerializedRequest,
) -> Result<T, RpcClientError> {
future::ready(
self.send_request_body(&request)
.await
.and_then(Self::parse_response_str)?
.data
.into_result(),
)
// We retry at the application level because Alchemy has sporadic failures that are returned
// in the JSON-RPC layer
.or_else(|error| async { self.retry_on_sporadic_failure(error, request).await })
.await
}

#[cfg_attr(feature = "tracing", tracing::instrument(level = "trace", skip_all))]
async fn send_request_body(
&self,
Expand Down Expand Up @@ -549,10 +577,7 @@ impl RpcClient {
#[cfg(feature = "tracing")]
tracing::trace!("Cache miss: {}", method.name());

let result: T = self
.send_request_body(&request)
.await
.and_then(|response| Self::extract_result(request, response))?;
let result: T = self.send_request_and_extract_result(request).await?;

self.try_write_response_to_cache(&method, &result, &resolve_block_number)
.await?;
Expand All @@ -569,9 +594,7 @@ impl RpcClient {
) -> Result<T, RpcClientError> {
let request = self.serialize_request(&method)?;

self.send_request_body(&request)
.await
.and_then(|response| Self::extract_result(request, response))
self.send_request_and_extract_result(request).await
}

/// Calls `eth_blockNumber` and returns the block number.
Expand Down
26 changes: 13 additions & 13 deletions crates/edr_eth/src/remote/request_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ use crate::{
#[derive(Clone, Debug, PartialEq, serde::Serialize)]
#[serde(tag = "method", content = "params")]
pub enum RequestMethod {
/// eth_blockNumber
/// `eth_blockNumber`
#[serde(rename = "eth_blockNumber", with = "crate::serde::empty_params")]
BlockNumber(()),
/// eth_feeHistory
/// `eth_feeHistory`
#[serde(rename = "eth_feeHistory")]
FeeHistory(
/// block count
Expand All @@ -25,10 +25,10 @@ pub enum RequestMethod {
#[serde(skip_serializing_if = "Option::is_none")]
Option<Vec<RewardPercentile>>,
),
/// eth_chainId
/// `eth_chainId`
#[serde(rename = "eth_chainId", with = "crate::serde::empty_params")]
ChainId(()),
/// eth_getBalance
/// `eth_getBalance`
#[serde(rename = "eth_getBalance")]
GetBalance(
Address,
Expand All @@ -38,22 +38,22 @@ pub enum RequestMethod {
)]
Option<BlockSpec>,
),
/// eth_getBlockByNumber
/// `eth_getBlockByNumber`
#[serde(rename = "eth_getBlockByNumber")]
GetBlockByNumber(
PreEip1898BlockSpec,
/// include transaction data
bool,
),
/// eth_getBlockByHash
/// `eth_getBlockByHash`
#[serde(rename = "eth_getBlockByHash")]
GetBlockByHash(
/// hash
B256,
/// include transaction data
bool,
),
/// eth_getCode
/// `eth_getCode`
#[serde(rename = "eth_getCode")]
GetCode(
Address,
Expand All @@ -63,10 +63,10 @@ pub enum RequestMethod {
)]
Option<BlockSpec>,
),
/// eth_getLogs
/// `eth_getLogs`
#[serde(rename = "eth_getLogs", with = "crate::serde::sequence")]
GetLogs(LogFilterOptions),
/// eth_getStorageAt
/// `eth_getStorageAt`
#[serde(rename = "eth_getStorageAt")]
GetStorageAt(
Address,
Expand All @@ -78,10 +78,10 @@ pub enum RequestMethod {
)]
Option<BlockSpec>,
),
/// eth_getTransactionByHash
/// `eth_getTransactionByHash`
#[serde(rename = "eth_getTransactionByHash", with = "crate::serde::sequence")]
GetTransactionByHash(B256),
/// eth_getTransactionCount
/// `eth_getTransactionCount`
#[serde(rename = "eth_getTransactionCount")]
GetTransactionCount(
Address,
Expand All @@ -91,10 +91,10 @@ pub enum RequestMethod {
)]
Option<BlockSpec>,
),
/// eth_getTransactionReceipt
/// `eth_getTransactionReceipt`
#[serde(rename = "eth_getTransactionReceipt", with = "crate::serde::sequence")]
GetTransactionReceipt(B256),
/// net_version
/// `net_version`
#[serde(rename = "net_version", with = "crate::serde::empty_params")]
NetVersion(()),
}
Expand Down
Loading

0 comments on commit 68db137

Please sign in to comment.