Skip to content

Commit 6b48085

Browse files
authored
test: unit test for validate_tipset (#6471)
1 parent b1b1a2c commit 6b48085

File tree

13 files changed

+415
-210
lines changed

13 files changed

+415
-210
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535

3636
### Changed
3737

38+
- [#6471](https://github.com/ChainSafe/forest/pull/6471) Moved `forest-tool state` subcommand to `forest-dev`.
39+
3840
### Removed
3941

4042
### Fixed

docs/docs/users/reference/cli.sh

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,6 @@ generate_markdown_section "forest-tool" "benchmark graph-traversal"
105105
generate_markdown_section "forest-tool" "benchmark forest-encoding"
106106
generate_markdown_section "forest-tool" "benchmark export"
107107

108-
generate_markdown_section "forest-tool" "state"
109-
generate_markdown_section "forest-tool" "state compute"
110-
generate_markdown_section "forest-tool" "state replay-compute"
111-
112108
generate_markdown_section "forest-tool" "state-migration"
113109
generate_markdown_section "forest-tool" "state-migration actor-bundle"
114110

@@ -158,3 +154,13 @@ generate_markdown_section "forest-tool" "shed openrpc"
158154

159155
generate_markdown_section "forest-tool" "index"
160156
generate_markdown_section "forest-tool" "index backfill"
157+
158+
generate_markdown_section "forest-dev" ""
159+
160+
generate_markdown_section "forest-dev" "fetch-rpc-tests"
161+
162+
generate_markdown_section "forest-dev" "state"
163+
generate_markdown_section "forest-dev" "state compute"
164+
generate_markdown_section "forest-dev" "state replay-compute"
165+
generate_markdown_section "forest-dev" "state validate"
166+
generate_markdown_section "forest-dev" "state replay-validate"

src/chain_sync/chain_follower.rs

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -828,7 +828,6 @@ impl SyncTask {
828828
bad_block_cache: Option<Arc<BadBlockCache>>,
829829
) -> Option<SyncEvent> {
830830
tracing::trace!("SyncTask::execute {self}");
831-
let cs = state_manager.chain_store();
832831
match self {
833832
SyncTask::ValidateTipset {
834833
tipset,
@@ -840,29 +839,19 @@ impl SyncTask {
840839
SyncTask::ValidateTipset {
841840
tipset,
842841
is_proposed_head,
843-
} => {
844-
let genesis = cs.genesis_tipset();
845-
match validate_tipset(
846-
&state_manager,
847-
cs,
848-
tipset.clone(),
849-
&genesis,
850-
bad_block_cache,
851-
)
852-
.await
853-
{
854-
Ok(()) => Some(SyncEvent::ValidatedTipset {
855-
tipset,
856-
is_proposed_head,
857-
}),
858-
Err(e) => {
859-
warn!("Error validating tipset: {}", e);
860-
Some(SyncEvent::BadTipset(tipset))
861-
}
842+
} => match validate_tipset(&state_manager, tipset.clone(), bad_block_cache).await {
843+
Ok(()) => Some(SyncEvent::ValidatedTipset {
844+
tipset,
845+
is_proposed_head,
846+
}),
847+
Err(e) => {
848+
warn!("Error validating tipset: {e}");
849+
Some(SyncEvent::BadTipset(tipset))
862850
}
863-
}
851+
},
864852
SyncTask::FetchTipset(key, epoch) => {
865-
match get_full_tipset_batch(&network, cs, None, &key).await {
853+
match get_full_tipset_batch(&network, state_manager.chain_store(), None, &key).await
854+
{
866855
Ok(parents) => Some(SyncEvent::NewFullTipsets(parents)),
867856
Err(e) => {
868857
tracing::warn!(%key, %epoch, "failed to fetch tipset: {e}");

src/chain_sync/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ pub mod consensus;
88
pub mod metrics;
99
pub mod network_context;
1010
mod sync_status;
11-
mod tipset_syncer;
11+
pub(crate) mod tipset_syncer;
1212
mod validation;
1313

1414
pub use self::{

src/chain_sync/tipset_syncer.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,13 @@ impl TipsetSyncerError {
9999
/// validation.
100100
pub async fn validate_tipset<DB: Blockstore + Send + Sync + 'static>(
101101
state_manager: &Arc<StateManager<DB>>,
102-
chainstore: &ChainStore<DB>,
103102
full_tipset: FullTipset,
104-
genesis: &Tipset,
105103
bad_block_cache: Option<Arc<BadBlockCache>>,
106104
) -> Result<(), TipsetSyncerError> {
107-
if full_tipset.key().eq(genesis.key()) {
105+
if full_tipset
106+
.key()
107+
.eq(state_manager.chain_store().genesis_tipset().key())
108+
{
108109
trace!("Skipping genesis tipset validation");
109110
return Ok(());
110111
}
@@ -123,7 +124,9 @@ pub async fn validate_tipset<DB: Blockstore + Send + Sync + 'static>(
123124
while let Some(result) = validations.join_next().await {
124125
match result? {
125126
Ok(block) => {
126-
chainstore.add_to_tipset_tracker(block.header());
127+
state_manager
128+
.chain_store()
129+
.add_to_tipset_tracker(block.header());
127130
}
128131
Err((cid, why)) => {
129132
warn!("Validating block [CID = {cid}] in EPOCH = {epoch} failed: {why}");

src/dev/subcommands/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright 2019-2026 ChainSafe Systems
22
// SPDX-License-Identifier: Apache-2.0, MIT
33

4+
mod state_cmd;
5+
46
use crate::cli_shared::cli::HELP_MESSAGE;
57
use crate::rpc::Client;
68
use crate::utils::net::{DownloadFileOption, download_file_with_cache};
@@ -30,12 +32,15 @@ pub struct Cli {
3032
pub enum Subcommand {
3133
/// Fetch RPC test snapshots to the local cache
3234
FetchRpcTests,
35+
#[command(subcommand)]
36+
State(state_cmd::StateCommand),
3337
}
3438

3539
impl Subcommand {
3640
pub async fn run(self, _client: Client) -> anyhow::Result<()> {
3741
match self {
3842
Self::FetchRpcTests => fetch_rpc_tests().await,
43+
Self::State(cmd) => cmd.run().await,
3944
}
4045
}
4146
}

0 commit comments

Comments
 (0)