Skip to content

Commit

Permalink
fix: total difficulty fallback for remote blocks (#697)
Browse files Browse the repository at this point in the history
* fix: total difficulty fallback for remote blocks

* Add changeset
  • Loading branch information
agostbiro authored Oct 17, 2024
1 parent 8aded8b commit 14620c9
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/mighty-turtles-wait.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nomicfoundation/edr": patch
---

Fix panic due to remote nodes not returning total difficulty
3 changes: 3 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions crates/edr_defaults/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ version = "0.3.5"
edition = "2021"

[dependencies]
ruint = "1.12.3"

[lints]
workspace = true
5 changes: 5 additions & 0 deletions crates/edr_defaults/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use ruint::aliases::U256;

/// The default secret keys from which the local accounts will be derived.
pub const SECRET_KEYS: [&str; 20] = [
// these were taken from the standard output of a run of `hardhat node`
Expand Down Expand Up @@ -36,3 +38,6 @@ pub const MIX_HASH_SEED: &str = "randomMixHashSeed";

/// Seed value for the generator of state root hashes.
pub const STATE_ROOT_HASH_SEED: &str = "seed";

/// Terminal total difficulty for the Merge on main net
pub const TERMINAL_TOTAL_DIFFICULTY: U256 = ruint::uint!(58750000000000000000000_U256);
12 changes: 10 additions & 2 deletions crates/edr_evm/src/blockchain/remote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,13 @@ where
.get_block_by_hash_with_transaction_data(*hash)
.await?
{
// Geth has recently removed the total difficulty field from block RPC
// responses, so we fall back to the terminal total difficulty of main net to
// provide backwards compatibility.
// TODO https://github.com/NomicFoundation/edr/issues/696
let total_difficulty = *block
.total_difficulty()
.expect("Must be present as this is not a pending transaction");
.unwrap_or(&edr_defaults::TERMINAL_TOTAL_DIFFICULTY);

self.fetch_and_cache_block(cache, block).await?;

Expand All @@ -227,9 +231,13 @@ where
cache: RwLockUpgradableReadGuard<'_, SparseBlockchainStorage<BlockT, ChainSpecT>>,
block: ChainSpecT::RpcBlock<ChainSpecT::RpcTransaction>,
) -> Result<BlockT, ForkedBlockchainError> {
// Geth has recently removed the total difficulty field from block RPC
// responses, so we fall back to the terminal total difficulty of main net to
// provide backwards compatibility.
// TODO https://github.com/NomicFoundation/edr/issues/696
let total_difficulty = *block
.total_difficulty()
.expect("Must be present as this is not a pending block");
.unwrap_or(&edr_defaults::TERMINAL_TOTAL_DIFFICULTY);

let block: RemoteBlock<ChainSpecT> =
block.into_remote_block(self.client.clone(), self.runtime.clone())?;
Expand Down

0 comments on commit 14620c9

Please sign in to comment.