Skip to content

Commit 07c5956

Browse files
chore: move CliHeader to primitives traits and rename to HeaderMut (#20001)
1 parent d3f6c4c commit 07c5956

File tree

8 files changed

+64
-58
lines changed

8 files changed

+64
-58
lines changed

crates/cli/commands/src/common.rs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! Contains common `reth` arguments
22
3+
pub use reth_primitives_traits::header::HeaderMut;
4+
35
use alloy_primitives::B256;
46
use clap::Parser;
57
use reth_chainspec::EthChainSpec;
@@ -227,17 +229,6 @@ type FullTypesAdapter<T> = FullNodeTypesAdapter<
227229
BlockchainProvider<NodeTypesWithDBAdapter<T, Arc<DatabaseEnv>>>,
228230
>;
229231

230-
/// Trait for block headers that can be modified through CLI operations.
231-
pub trait CliHeader {
232-
fn set_number(&mut self, number: u64);
233-
}
234-
235-
impl CliHeader for alloy_consensus::Header {
236-
fn set_number(&mut self, number: u64) {
237-
self.number = number;
238-
}
239-
}
240-
241232
/// Helper trait with a common set of requirements for the
242233
/// [`NodeTypes`] in CLI.
243234
pub trait CliNodeTypes: Node<FullTypesAdapter<Self>> + NodeTypesForProvider {

crates/cli/commands/src/init_state/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
//! Command that initializes the node from a genesis file.
22
3-
use crate::common::{AccessRights, CliHeader, CliNodeTypes, Environment, EnvironmentArgs};
3+
use crate::common::{AccessRights, CliNodeTypes, Environment, EnvironmentArgs};
44
use alloy_consensus::BlockHeader as AlloyBlockHeader;
55
use alloy_primitives::{Sealable, B256};
66
use clap::Parser;
77
use reth_chainspec::{EthChainSpec, EthereumHardforks};
88
use reth_cli::chainspec::ChainSpecParser;
99
use reth_db_common::init::init_from_state_dump;
1010
use reth_node_api::NodePrimitives;
11-
use reth_primitives_traits::{BlockHeader, SealedHeader};
11+
use reth_primitives_traits::{header::HeaderMut, SealedHeader};
1212
use reth_provider::{
1313
BlockNumReader, DBProvider, DatabaseProviderFactory, StaticFileProviderFactory,
1414
StaticFileWriter,
@@ -69,7 +69,7 @@ impl<C: ChainSpecParser<ChainSpec: EthChainSpec + EthereumHardforks>> InitStateC
6969
where
7070
N: CliNodeTypes<
7171
ChainSpec = C::ChainSpec,
72-
Primitives: NodePrimitives<BlockHeader: BlockHeader + CliHeader>,
72+
Primitives: NodePrimitives<BlockHeader: HeaderMut>,
7373
>,
7474
{
7575
info!(target: "reth::cli", "Reth init-state starting");

crates/ethereum/cli/src/app.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use eyre::{eyre, Result};
33
use reth_chainspec::{ChainSpec, EthChainSpec, Hardforks};
44
use reth_cli::chainspec::ChainSpecParser;
55
use reth_cli_commands::{
6-
common::{CliComponentsBuilder, CliHeader, CliNodeTypes},
6+
common::{CliComponentsBuilder, CliNodeTypes, HeaderMut},
77
launcher::{FnLauncher, Launcher},
88
};
99
use reth_cli_runner::CliRunner;
@@ -81,7 +81,7 @@ where
8181
) -> Result<()>,
8282
) -> Result<()>
8383
where
84-
N: CliNodeTypes<Primitives: NodePrimitives<BlockHeader: CliHeader>, ChainSpec: Hardforks>,
84+
N: CliNodeTypes<Primitives: NodePrimitives<BlockHeader: HeaderMut>, ChainSpec: Hardforks>,
8585
C: ChainSpecParser<ChainSpec = N::ChainSpec>,
8686
{
8787
let runner = match self.runner.take() {
@@ -130,7 +130,7 @@ where
130130
C: ChainSpecParser<ChainSpec = N::ChainSpec>,
131131
Ext: clap::Args + fmt::Debug,
132132
Rpc: RpcModuleValidator,
133-
N: CliNodeTypes<Primitives: NodePrimitives<BlockHeader: CliHeader>, ChainSpec: Hardforks>,
133+
N: CliNodeTypes<Primitives: NodePrimitives<BlockHeader: HeaderMut>, ChainSpec: Hardforks>,
134134
{
135135
match cli.command {
136136
Commands::Node(command) => {

crates/ethereum/cli/src/interface.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use clap::{Parser, Subcommand};
88
use reth_chainspec::{ChainSpec, EthChainSpec, Hardforks};
99
use reth_cli::chainspec::ChainSpecParser;
1010
use reth_cli_commands::{
11-
common::{CliComponentsBuilder, CliHeader, CliNodeTypes},
11+
common::{CliComponentsBuilder, CliNodeTypes, HeaderMut},
1212
config_cmd, db, download, dump_genesis, export_era, import, import_era, init_cmd, init_state,
1313
launcher::FnLauncher,
1414
node::{self, NoArgs},
@@ -149,7 +149,7 @@ impl<C: ChainSpecParser, Ext: clap::Args + fmt::Debug, Rpc: RpcModuleValidator>
149149
) -> eyre::Result<()>,
150150
) -> eyre::Result<()>
151151
where
152-
N: CliNodeTypes<Primitives: NodePrimitives<BlockHeader: CliHeader>, ChainSpec: Hardforks>,
152+
N: CliNodeTypes<Primitives: NodePrimitives<BlockHeader: HeaderMut>, ChainSpec: Hardforks>,
153153
C: ChainSpecParser<ChainSpec = N::ChainSpec>,
154154
{
155155
self.with_runner_and_components(CliRunner::try_default_runtime()?, components, launcher)
@@ -197,7 +197,7 @@ impl<C: ChainSpecParser, Ext: clap::Args + fmt::Debug, Rpc: RpcModuleValidator>
197197
) -> eyre::Result<()>,
198198
) -> eyre::Result<()>
199199
where
200-
N: CliNodeTypes<Primitives: NodePrimitives<BlockHeader: CliHeader>, ChainSpec: Hardforks>,
200+
N: CliNodeTypes<Primitives: NodePrimitives<BlockHeader: HeaderMut>, ChainSpec: Hardforks>,
201201
C: ChainSpecParser<ChainSpec = N::ChainSpec>,
202202
{
203203
// Add network name if available to the logs dir

crates/optimism/cli/src/commands/init_state.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
use alloy_consensus::Header;
44
use clap::Parser;
55
use reth_cli::chainspec::ChainSpecParser;
6-
use reth_cli_commands::common::{AccessRights, CliHeader, CliNodeTypes, Environment};
6+
use reth_cli_commands::common::{AccessRights, CliNodeTypes, Environment};
77
use reth_db_common::init::init_from_state_dump;
88
use reth_optimism_chainspec::OpChainSpec;
99
use reth_optimism_primitives::{
1010
bedrock::{BEDROCK_HEADER, BEDROCK_HEADER_HASH},
1111
OpPrimitives,
1212
};
13-
use reth_primitives_traits::SealedHeader;
13+
use reth_primitives_traits::{header::HeaderMut, SealedHeader};
1414
use reth_provider::{
1515
BlockNumReader, DBProvider, DatabaseProviderFactory, StaticFileProviderFactory,
1616
StaticFileWriter,
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//! Mutable header utilities.
2+
3+
use crate::BlockHeader;
4+
use alloy_consensus::Header;
5+
use alloy_primitives::{BlockHash, BlockNumber, B256, U256};
6+
7+
/// A helper trait for [`Header`]s that allows for mutable access to the headers values.
8+
///
9+
/// This allows for modifying the header for testing and mocking purposes.
10+
pub trait HeaderMut: BlockHeader {
11+
/// Updates the parent block hash.
12+
fn set_parent_hash(&mut self, hash: BlockHash);
13+
14+
/// Updates the block number.
15+
fn set_block_number(&mut self, number: BlockNumber);
16+
17+
/// Updates the block state root.
18+
fn set_state_root(&mut self, state_root: B256);
19+
20+
/// Updates the block difficulty.
21+
fn set_difficulty(&mut self, difficulty: U256);
22+
23+
/// Updates the block number (alias for CLI compatibility).
24+
fn set_number(&mut self, number: u64) {
25+
self.set_block_number(number);
26+
}
27+
}
28+
29+
impl HeaderMut for Header {
30+
fn set_parent_hash(&mut self, hash: BlockHash) {
31+
self.parent_hash = hash;
32+
}
33+
34+
fn set_block_number(&mut self, number: BlockNumber) {
35+
self.number = number;
36+
}
37+
38+
fn set_state_root(&mut self, state_root: B256) {
39+
self.state_root = state_root;
40+
}
41+
42+
fn set_difficulty(&mut self, difficulty: U256) {
43+
self.difficulty = difficulty;
44+
}
45+
}

crates/primitives-traits/src/header/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
mod sealed;
22
pub use sealed::{Header, SealedHeader, SealedHeaderFor};
33

4+
mod header_mut;
5+
pub use header_mut::HeaderMut;
6+
47
#[cfg(any(test, feature = "test-utils", feature = "arbitrary"))]
58
pub mod test_utils;
69

crates/primitives-traits/src/header/test_utils.rs

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,12 @@
11
//! Test utilities for the block header.
22
3-
use crate::BlockHeader;
43
use alloy_consensus::Header;
5-
use alloy_primitives::{BlockHash, BlockNumber, B256, U256};
4+
use alloy_primitives::B256;
65
use proptest::{arbitrary::any, prop_compose};
76
use proptest_arbitrary_interop::arb;
87

9-
/// A helper trait for [`Header`]s that allows for mutable access to the headers values.
10-
///
11-
/// This allows for modifying the header for testing purposes.
12-
pub trait TestHeader: BlockHeader {
13-
/// Updates the parent block hash.
14-
fn set_parent_hash(&mut self, hash: BlockHash);
15-
16-
/// Updates the block number.
17-
fn set_block_number(&mut self, number: BlockNumber);
18-
19-
/// Updates the block state root.
20-
fn set_state_root(&mut self, state_root: B256);
21-
22-
/// Updates the block difficulty.
23-
fn set_difficulty(&mut self, difficulty: U256);
24-
}
25-
26-
impl TestHeader for Header {
27-
fn set_parent_hash(&mut self, hash: BlockHash) {
28-
self.parent_hash = hash
29-
}
30-
31-
fn set_block_number(&mut self, number: BlockNumber) {
32-
self.number = number;
33-
}
34-
35-
fn set_state_root(&mut self, state_root: B256) {
36-
self.state_root = state_root;
37-
}
38-
39-
fn set_difficulty(&mut self, difficulty: U256) {
40-
self.difficulty = difficulty;
41-
}
42-
}
8+
/// Re-export `HeaderMut` for backward compatibility in tests.
9+
pub use super::HeaderMut as TestHeader;
4310

4411
/// Generates a header which is valid __with respect to past and future forks__. This means, for
4512
/// example, that if the withdrawals root is present, the base fee per gas is also present.

0 commit comments

Comments
 (0)