Skip to content

Commit fd0336c

Browse files
authored
fix: total difficulty logic (#260)
* fix total difficulty logic * lints * refactor and address comments
1 parent 22f5ee3 commit fd0336c

File tree

6 files changed

+51
-10
lines changed

6 files changed

+51
-10
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/network/src/manager.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ pub struct ScrollNetworkManager<N> {
4343
from_handle_rx: UnboundedReceiverStream<NetworkHandleMessage>,
4444
/// The scroll wire protocol manager.
4545
scroll_wire: ScrollWireManager,
46+
/// The constant value that must be added to the block number to get the total difficulty.
47+
td_constant: U128,
4648
}
4749

4850
impl ScrollNetworkManager<RethNetworkHandle<ScrollNetworkPrimitives>> {
@@ -51,6 +53,7 @@ impl ScrollNetworkManager<RethNetworkHandle<ScrollNetworkPrimitives>> {
5153
pub async fn new<C: BlockNumReaderT + 'static>(
5254
mut network_config: RethNetworkConfig<C, ScrollNetworkPrimitives>,
5355
scroll_wire_config: ScrollWireConfig,
56+
td_constant: U128,
5457
) -> Self {
5558
// Create the scroll-wire protocol handler.
5659
let (scroll_wire_handler, events) = ScrollWireProtocolHandler::new(scroll_wire_config);
@@ -74,7 +77,7 @@ impl ScrollNetworkManager<RethNetworkHandle<ScrollNetworkPrimitives>> {
7477
// Spawn the inner network manager.
7578
tokio::spawn(inner_network_manager);
7679

77-
Self { handle, from_handle_rx: from_handle_rx.into(), scroll_wire }
80+
Self { handle, from_handle_rx: from_handle_rx.into(), scroll_wire, td_constant }
7881
}
7982
}
8083

@@ -83,7 +86,11 @@ impl<N: FullNetwork<Primitives = ScrollNetworkPrimitives>> ScrollNetworkManager<
8386
///
8487
/// This is used when the scroll-wire [`ScrollWireProtocolHandler`] and the inner network
8588
/// manager [`RethNetworkManager`] are instantiated externally.
86-
pub fn from_parts(inner_network_handle: N, events: UnboundedReceiver<ScrollWireEvent>) -> Self {
89+
pub fn from_parts(
90+
inner_network_handle: N,
91+
events: UnboundedReceiver<ScrollWireEvent>,
92+
td_constant: U128,
93+
) -> Self {
8794
// Create the channel for sending messages to the network manager from the network handle.
8895
let (to_manager_tx, from_handle_rx) = mpsc::unbounded_channel();
8996

@@ -92,7 +99,7 @@ impl<N: FullNetwork<Primitives = ScrollNetworkPrimitives>> ScrollNetworkManager<
9299

93100
let handle = ScrollNetworkHandle::new(to_manager_tx, inner_network_handle);
94101

95-
Self { handle, from_handle_rx: from_handle_rx.into(), scroll_wire }
102+
Self { handle, from_handle_rx: from_handle_rx.into(), scroll_wire, td_constant }
96103
}
97104

98105
/// Returns a new [`ScrollNetworkHandle`] instance.
@@ -119,7 +126,7 @@ impl<N: FullNetwork<Primitives = ScrollNetworkPrimitives>> ScrollNetworkManager<
119126
.collect();
120127

121128
let eth_wire_new_block = {
122-
let td = U128::from(block.block.header.number);
129+
let td = compute_td(self.td_constant, block.block.header.number);
123130
let mut eth_wire_block = block.block.clone();
124131
eth_wire_block.header.extra_data = block.signature.clone().into();
125132
EthWireNewBlock { block: eth_wire_block, td }
@@ -223,3 +230,8 @@ impl<N: FullNetwork<Primitives = ScrollNetworkPrimitives>> Stream for ScrollNetw
223230
Poll::Pending
224231
}
225232
}
233+
234+
/// Compute totally difficulty for a given block number.
235+
fn compute_td(td_constant: U128, block_number: u64) -> U128 {
236+
td_constant.saturating_add(U128::from(block_number))
237+
}

crates/node/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ workspace = true
1515

1616
[dependencies]
1717
# alloy
18+
alloy-chains.workspace = true
1819
alloy-primitives.workspace = true
1920
alloy-provider.workspace = true
2021
alloy-rpc-client.workspace = true
@@ -90,6 +91,7 @@ tokio.workspace = true
9091
tracing.workspace = true
9192

9293
[dev-dependencies]
94+
alloy-chains.workspace = true
9395
alloy-eips.workspace = true
9496
futures.workspace = true
9597
reth-e2e-test-utils.workspace = true

crates/node/src/args.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ use crate::{
55
};
66
use std::{fs, path::PathBuf, sync::Arc, time::Duration};
77

8-
use alloy_primitives::{hex, Address};
8+
use alloy_chains::NamedChain;
9+
use alloy_primitives::{hex, Address, U128};
910
use alloy_provider::{Provider, ProviderBuilder};
1011
use alloy_rpc_client::RpcClient;
1112
use alloy_signer::Signer;
@@ -139,9 +140,6 @@ impl ScrollRollupNodeConfig {
139140
"Building rollup node with config:\n{:#?}",
140141
self
141142
);
142-
// Instantiate the network manager
143-
let network = ctx.network;
144-
let scroll_network_manager = ScrollNetworkManager::from_parts(network.clone(), events);
145143

146144
// Get the chain spec.
147145
let chain_spec = ctx.chain_spec;
@@ -150,6 +148,11 @@ impl ScrollRollupNodeConfig {
150148
let named_chain = chain_spec.chain().named().expect("expected named chain");
151149
let node_config = Arc::new(NodeConfig::from_named_chain(named_chain));
152150

151+
// Instantiate the network manager
152+
let network = ctx.network;
153+
let scroll_network_manager =
154+
ScrollNetworkManager::from_parts(network.clone(), events, td_constant(named_chain));
155+
153156
// Create the engine api client.
154157
let engine_api = ScrollAuthApiEngineClient::new(rpc_server_handles.auth.http_client());
155158

@@ -596,6 +599,15 @@ pub struct GasPriceOracleArgs {
596599
pub default_suggested_priority_fee: u64,
597600
}
598601

602+
/// Returns the total difficulty constant for the given chain.
603+
const fn td_constant(chain: NamedChain) -> U128 {
604+
match chain {
605+
NamedChain::Scroll => constants::SCROLL_MAINNET_TD_CONSTANT,
606+
NamedChain::ScrollSepolia => constants::SCROLL_SEPOLIA_TD_CONSTANT,
607+
_ => U128::ZERO, // Default to zero for other chains
608+
}
609+
}
610+
599611
#[cfg(test)]
600612
mod tests {
601613
use super::*;

crates/node/src/constants.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! Constants related to the [`crate::ScrollRollupNode`]
22
3+
use alloy_primitives::U128;
4+
35
/// The max retries for the L1 provider.
46
pub(crate) const PROVIDER_MAX_RETRIES: u32 = 10;
57

@@ -27,3 +29,11 @@ pub(crate) const DEFAULT_SUGGEST_PRIORITY_FEE: u64 = 100;
2729
/// Scroll default gas limit.
2830
/// Should match <https://github.com/scroll-tech/reth/blob/scroll/crates/scroll/node/src/builder/payload.rs#L36>.
2931
pub const SCROLL_GAS_LIMIT: u64 = 20_000_000;
32+
33+
/// The constant value that must be added to the block number to get the total difficulty for Scroll
34+
/// mainnet.
35+
pub(crate) const SCROLL_MAINNET_TD_CONSTANT: U128 = U128::from_limbs([14906960, 0]);
36+
37+
/// The constant value that must be added to the block number to get the total difficulty for Scroll
38+
/// Sepolia.
39+
pub(crate) const SCROLL_SEPOLIA_TD_CONSTANT: U128 = U128::from_limbs([8484488, 0]);

crates/node/tests/e2e.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -495,8 +495,12 @@ async fn can_bridge_blocks() {
495495
.with_pow()
496496
.build_with_noop_provider(chain_spec.clone());
497497
let scroll_wire_config = ScrollWireConfig::new(true);
498-
let mut scroll_network =
499-
scroll_network::ScrollNetworkManager::new(network_config, scroll_wire_config).await;
498+
let mut scroll_network = scroll_network::ScrollNetworkManager::new(
499+
network_config,
500+
scroll_wire_config,
501+
Default::default(),
502+
)
503+
.await;
500504
let scroll_network_handle = scroll_network.handle();
501505

502506
// Connect the scroll-wire node to the scroll NetworkManager.

0 commit comments

Comments
 (0)