diff --git a/Cargo.lock b/Cargo.lock index 8d6123c02a..411c67899a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1788,6 +1788,7 @@ name = "edr_defaults" version = "0.3.8" dependencies = [ "alloy-primitives 0.7.7", + "ruint", ] [[package]] diff --git a/crates/edr_defaults/Cargo.toml b/crates/edr_defaults/Cargo.toml index ece17cb710..7395f8bd88 100644 --- a/crates/edr_defaults/Cargo.toml +++ b/crates/edr_defaults/Cargo.toml @@ -5,6 +5,7 @@ edition.workspace = true [dependencies] alloy-primitives.workspace = true +ruint = "1.12.3" [lints] workspace = true diff --git a/crates/edr_defaults/src/lib.rs b/crates/edr_defaults/src/lib.rs index 4597fb1f43..e3bdddca45 100644 --- a/crates/edr_defaults/src/lib.rs +++ b/crates/edr_defaults/src/lib.rs @@ -1,4 +1,5 @@ use alloy_primitives::{address, Address}; +use ruint::aliases::U256; /// The default secret keys from which the local accounts will be derived. pub const SECRET_KEYS: [&str; 20] = [ @@ -53,3 +54,6 @@ pub const DEV_CHAIN_ID: u64 = 31337; /// The first four bytes of the call data for a function call specifies the /// function to be called. pub const SELECTOR_LEN: usize = 4; + +/// Terminal total difficulty for the Merge on main net +pub const TERMINAL_TOTAL_DIFFICULTY: U256 = ruint::uint!(58750000000000000000000_U256); diff --git a/crates/edr_evm/src/blockchain/remote.rs b/crates/edr_evm/src/blockchain/remote.rs index aadc111692..cb192419d3 100644 --- a/crates/edr_evm/src/blockchain/remote.rs +++ b/crates/edr_evm/src/blockchain/remote.rs @@ -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?; @@ -227,9 +231,13 @@ where cache: RwLockUpgradableReadGuard<'_, SparseBlockchainStorage>, block: ChainSpecT::RpcBlock, ) -> Result { + // 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 = block.into_remote_block(self.client.clone(), self.runtime.clone())?; diff --git a/crates/edr_napi/CHANGELOG.md b/crates/edr_napi/CHANGELOG.md index 3f35ed54a5..3fc62ba87a 100644 --- a/crates/edr_napi/CHANGELOG.md +++ b/crates/edr_napi/CHANGELOG.md @@ -1,5 +1,11 @@ # @nomicfoundation/edr +## 0.6.4 + +### Patch Changes + +- 14620c9: Fix panic due to remote nodes not returning total difficulty by adding fallback value + ## 0.6.3 ### Patch Changes