From f0ce53645ac034e1f5926251df90b868f163f2b9 Mon Sep 17 00:00:00 2001 From: Agost Biro Date: Tue, 15 Oct 2024 19:51:11 +0000 Subject: [PATCH] fix: total difficulty fallback for remote blocks --- Cargo.lock | 3 +++ crates/edr_defaults/Cargo.toml | 1 + crates/edr_defaults/src/lib.rs | 5 +++++ crates/edr_evm/src/blockchain/remote.rs | 12 ++++++++++-- 4 files changed, 19 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5defcc094..f6bd66531 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1079,6 +1079,9 @@ dependencies = [ [[package]] name = "edr_defaults" version = "0.3.5" +dependencies = [ + "ruint", +] [[package]] name = "edr_eth" diff --git a/crates/edr_defaults/Cargo.toml b/crates/edr_defaults/Cargo.toml index be20c2750..a65aa2cbc 100644 --- a/crates/edr_defaults/Cargo.toml +++ b/crates/edr_defaults/Cargo.toml @@ -4,6 +4,7 @@ version = "0.3.5" edition = "2021" [dependencies] +ruint = "1.12.3" [lints] workspace = true diff --git a/crates/edr_defaults/src/lib.rs b/crates/edr_defaults/src/lib.rs index b688f8563..bb8479b63 100644 --- a/crates/edr_defaults/src/lib.rs +++ b/crates/edr_defaults/src/lib.rs @@ -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` @@ -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); diff --git a/crates/edr_evm/src/blockchain/remote.rs b/crates/edr_evm/src/blockchain/remote.rs index aadc11169..cb192419d 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())?;