Skip to content

Commit

Permalink
fix the code for robot's comments
Browse files Browse the repository at this point in the history
  • Loading branch information
jackzhhuang committed Sep 30, 2024
1 parent ba5d0b2 commit 218e7a0
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 181 deletions.
16 changes: 9 additions & 7 deletions chain/mock/src/mock_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ impl MockChain {
pruning_finality: u64,
) -> Result<Self> {
let (storage, chain_info, _, dag) =
Genesis::init_storage_for_test_with_param(&net, k, pruning_depth, pruning_finality)
.expect("init storage by genesis fail.");
Genesis::init_storage_for_test_with_param(&net, k, pruning_depth, pruning_finality)?;

let chain = BlockChain::new(
net.time_service(),
Expand Down Expand Up @@ -283,16 +282,19 @@ impl MockChain {
*self.miner.address(),
selected_header,
vec![],
blue_blocks[1..]
blue_blocks
.get(1..)
.unwrap_or(&[])
.iter()
.map(|block_id| {
self.head()
.get_storage()
.get_block_header_by_hash(*block_id)
.unwrap()
.unwrap()
.get_block_header_by_hash(*block_id)?
.ok_or_else(|| {
format_err!("Block header not found for hash: {:?}", block_id)
})
})
.collect(),
.collect::<Result<Vec<_>>>()?,
None,
pruned_tips,
pruning_point,
Expand Down
40 changes: 14 additions & 26 deletions chain/tests/test_prune.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,29 @@ fn test_block_chain_prune() -> anyhow::Result<()> {
let genesis = mock_chain.head().status().head.clone();

// blue blocks
let block_blue_1 = mock_chain.produce_block_by_tips(genesis.clone(), vec![genesis.id()])?;
mock_chain.apply(block_blue_1.clone())?;
let block_blue_1 = mock_chain.produce_and_apply_by_tips(genesis.clone(), vec![genesis.id()])?;
let block_blue_2 =
mock_chain.produce_block_by_tips(block_blue_1.header().clone(), vec![block_blue_1.id()])?;
mock_chain.apply(block_blue_2.clone())?;
mock_chain.produce_and_apply_by_tips(block_blue_1.clone(), vec![block_blue_1.id()])?;
let block_blue_3 =
mock_chain.produce_block_by_tips(block_blue_2.header().clone(), vec![block_blue_2.id()])?;
mock_chain.apply(block_blue_3.clone())?;
mock_chain.produce_and_apply_by_tips(block_blue_2.clone(), vec![block_blue_2.id()])?;
let block_blue_3_1 =
mock_chain.produce_block_by_tips(block_blue_2.header().clone(), vec![block_blue_2.id()])?;
mock_chain.apply(block_blue_3_1.clone())?;
let block_blue_4 = mock_chain.produce_block_by_tips(
block_blue_3.header().clone(),
mock_chain.produce_and_apply_by_tips(block_blue_2.clone(), vec![block_blue_2.id()])?;
let block_blue_4 = mock_chain.produce_and_apply_by_tips(
block_blue_3.clone(),
vec![block_blue_3.id(), block_blue_3_1.id()],
)?;
mock_chain.apply(block_blue_4.clone())?;
let block_blue_5 =
mock_chain.produce_block_by_tips(block_blue_4.header().clone(), vec![block_blue_4.id()])?;
mock_chain.apply(block_blue_5.clone())?;
mock_chain.produce_and_apply_by_tips(block_blue_4.clone(), vec![block_blue_4.id()])?;

// red blocks
let block_red_2 =
mock_chain.produce_block_by_tips(block_blue_1.header().clone(), vec![block_blue_1.id()])?;
mock_chain.apply(block_red_2.clone())?;
mock_chain.produce_and_apply_by_tips(block_blue_1.clone(), vec![block_blue_1.id()])?;
let block_red_2_1 =
mock_chain.produce_block_by_tips(block_blue_1.header().clone(), vec![block_blue_1.id()])?;
mock_chain.apply(block_red_2_1.clone())?;
let block_red_3 = mock_chain.produce_block_by_tips(
block_red_2.header().clone(),
mock_chain.produce_and_apply_by_tips(block_blue_1.clone(), vec![block_blue_1.id()])?;
let block_red_3 = mock_chain.produce_and_apply_by_tips(
block_red_2.clone(),
vec![block_red_2.id(), block_red_2_1.id()],
)?;
mock_chain.apply(block_red_3.clone())?;

debug!(
"tips: {:?}, pruning point: {:?}",
Expand All @@ -59,14 +50,11 @@ fn test_block_chain_prune() -> anyhow::Result<()> {
);

let block_blue_6 =
mock_chain.produce_block_by_tips(block_blue_5.header().clone(), vec![block_blue_5.id()])?;
mock_chain.apply(block_blue_6.clone())?;
mock_chain.produce_and_apply_by_tips(block_blue_5.clone(), vec![block_blue_5.id()])?;
let block_blue_6_1 =
mock_chain.produce_block_by_tips(block_blue_5.header().clone(), vec![block_blue_5.id()])?;
mock_chain.apply(block_blue_6_1.clone())?;
mock_chain.produce_and_apply_by_tips(block_blue_5.clone(), vec![block_blue_5.id()])?;
let block_red_4 =
mock_chain.produce_block_by_tips(block_red_3.header().clone(), vec![block_red_3.id()])?;
mock_chain.apply(block_red_4.clone())?;
mock_chain.produce_and_apply_by_tips(block_red_3.clone(), vec![block_red_3.id()])?;

debug!(
"tips: {:?}, pruning point: {:?}",
Expand Down
8 changes: 2 additions & 6 deletions cmd/db-exporter/src/force_deploy_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ use anyhow::format_err;
use clap::Parser;
use starcoin_chain::{BlockChain, ChainReader, ChainWriter};
use starcoin_cmd::dev::dev_helper;
use starcoin_config::genesis_config::{G_PRUNING_DEPTH, G_PRUNING_FINALITY};
use starcoin_config::{BuiltinNetworkID, ChainNetwork};
use starcoin_dag::blockdag::{BlockDAG, DEFAULT_GHOSTDAG_K};
use starcoin_dag::blockdag::BlockDAG;
use starcoin_dag::consensusdb::prelude::FlexiDagStorageConfig;
use starcoin_genesis::Genesis;
use starcoin_state_api::ChainStateWriter;
Expand Down Expand Up @@ -78,14 +77,11 @@ pub fn force_deploy_output(
db_storage,
))?);

let dag = BlockDAG::new(
DEFAULT_GHOSTDAG_K,
let dag = BlockDAG::create_blockdag(
starcoin_dag::consensusdb::prelude::FlexiDagStorage::create_from_path(
network_path.join("dag/db/starcoindb"),
FlexiDagStorageConfig::new(),
)?,
G_PRUNING_DEPTH,
G_PRUNING_FINALITY,
);
let (chain_info, _) =
Genesis::init_and_check_storage(&net, storage.clone(), dag.clone(), network_path.as_ref())?;
Expand Down
91 changes: 15 additions & 76 deletions cmd/db-exporter/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@ use starcoin_chain::{
verifier::{BasicVerifier, ConsensusVerifier, FullVerifier, NoneVerifier, Verifier},
BlockChain, ChainReader, ChainWriter,
};
use starcoin_config::genesis_config::{G_PRUNING_DEPTH, G_PRUNING_FINALITY};
use starcoin_config::{BuiltinNetworkID, ChainNetwork, RocksdbConfig};
use starcoin_consensus::Consensus;
use starcoin_crypto::HashValue;
use starcoin_dag::blockdag::{BlockDAG, DEFAULT_GHOSTDAG_K};
use starcoin_dag::blockdag::BlockDAG;
use starcoin_dag::consensusdb::prelude::FlexiDagStorageConfig;
use starcoin_genesis::Genesis;
use starcoin_resource_viewer::{AnnotatedMoveStruct, AnnotatedMoveValue, MoveValueAnnotator};
Expand Down Expand Up @@ -769,12 +768,7 @@ pub fn export_block_range(
from_dir.join("dag/db/starcoindb"),
FlexiDagStorageConfig::new(),
)?;
let dag = starcoin_dag::blockdag::BlockDAG::new(
DEFAULT_GHOSTDAG_K,
dag_storage,
G_PRUNING_DEPTH,
G_PRUNING_FINALITY,
);
let dag = starcoin_dag::blockdag::BlockDAG::create_blockdag(dag_storage);

let storage = Arc::new(Storage::new(StorageInstance::new_cache_and_db_instance(
CacheStorage::new(None),
Expand Down Expand Up @@ -901,12 +895,7 @@ pub fn apply_block(
to_dir.join("dag/db/starcoindb"),
FlexiDagStorageConfig::new(),
)?;
let dag = starcoin_dag::blockdag::BlockDAG::new(
DEFAULT_GHOSTDAG_K,
dag_storage,
G_PRUNING_DEPTH,
G_PRUNING_FINALITY,
);
let dag = starcoin_dag::blockdag::BlockDAG::create_blockdag(dag_storage);
// StarcoinVM::set_concurrency_level_once(num_cpus::get());
let (chain_info, _) =
Genesis::init_and_check_storage(&net, storage.clone(), dag.clone(), to_dir.as_ref())?;
Expand Down Expand Up @@ -997,12 +986,7 @@ pub fn startup_info_back(
to_dir.join("dag/db/starcoindb"),
FlexiDagStorageConfig::new(),
)?;
let dag = starcoin_dag::blockdag::BlockDAG::new(
DEFAULT_GHOSTDAG_K,
dag_storage,
G_PRUNING_DEPTH,
G_PRUNING_FINALITY,
);
let dag = starcoin_dag::blockdag::BlockDAG::create_blockdag(dag_storage);
let (chain_info, _) =
Genesis::init_and_check_storage(&net, storage.clone(), dag.clone(), to_dir.as_ref())?;
let chain = BlockChain::new(
Expand Down Expand Up @@ -1054,12 +1038,7 @@ pub fn gen_block_transactions(
to_dir.join("dag/db/starcoindb"),
FlexiDagStorageConfig::new(),
)?;
let dag = starcoin_dag::blockdag::BlockDAG::new(
DEFAULT_GHOSTDAG_K,
dag_storage,
G_PRUNING_DEPTH,
G_PRUNING_FINALITY,
);
let dag = starcoin_dag::blockdag::BlockDAG::create_blockdag(dag_storage);
let (chain_info, _) =
Genesis::init_and_check_storage(&net, storage.clone(), dag.clone(), to_dir.as_ref())?;
let mut chain = BlockChain::new(
Expand Down Expand Up @@ -1585,12 +1564,7 @@ pub fn export_snapshot(
from_dir.join("dag/db/starcoindb"),
FlexiDagStorageConfig::new(),
)?;
let dag = starcoin_dag::blockdag::BlockDAG::new(
DEFAULT_GHOSTDAG_K,
dag_storage,
G_PRUNING_DEPTH,
G_PRUNING_FINALITY,
);
let dag = starcoin_dag::blockdag::BlockDAG::create_blockdag(dag_storage);
let (chain_info, _) =
Genesis::init_and_check_storage(&net, storage.clone(), dag.clone(), from_dir.as_ref())?;
let chain = BlockChain::new(
Expand Down Expand Up @@ -1942,12 +1916,7 @@ pub fn apply_snapshot(
to_dir.join("dag/db/starcoindb"),
FlexiDagStorageConfig::new(),
)?;
let dag = starcoin_dag::blockdag::BlockDAG::new(
DEFAULT_GHOSTDAG_K,
dag_storage,
G_PRUNING_DEPTH,
G_PRUNING_FINALITY,
);
let dag = starcoin_dag::blockdag::BlockDAG::create_blockdag(dag_storage);

let (chain_info, _) =
Genesis::init_and_check_storage(&net, storage.clone(), dag.clone(), to_dir.as_ref())?;
Expand Down Expand Up @@ -2289,12 +2258,7 @@ pub fn gen_turbo_stm_transactions(to_dir: PathBuf, block_num: Option<u64>) -> an
to_dir.join("dag/db/starcoindb"),
FlexiDagStorageConfig::new(),
)?;
let dag = starcoin_dag::blockdag::BlockDAG::new(
DEFAULT_GHOSTDAG_K,
dag_storage,
G_PRUNING_DEPTH,
G_PRUNING_FINALITY,
);
let dag = starcoin_dag::blockdag::BlockDAG::create_blockdag(dag_storage);
let (chain_info, _) =
Genesis::init_and_check_storage(&net, storage.clone(), dag.clone(), to_dir.as_ref())?;
let mut chain = BlockChain::new(
Expand Down Expand Up @@ -2326,12 +2290,7 @@ pub fn apply_turbo_stm_block(
to_dir.join("dag/db/starcoindb"),
FlexiDagStorageConfig::new(),
)?;
let dag = starcoin_dag::blockdag::BlockDAG::new(
DEFAULT_GHOSTDAG_K,
dag_storage,
G_PRUNING_DEPTH,
G_PRUNING_FINALITY,
);
let dag = starcoin_dag::blockdag::BlockDAG::create_blockdag(dag_storage);
let (chain_info_seq, _) =
Genesis::init_and_check_storage(&net, storage_seq.clone(), dag.clone(), to_dir.as_ref())?;
let mut chain_seq = BlockChain::new(
Expand Down Expand Up @@ -2395,12 +2354,7 @@ pub fn apply_turbo_stm_block(
to_dir.join("dag/db/starcoindb"),
FlexiDagStorageConfig::new(),
)?;
let dag = starcoin_dag::blockdag::BlockDAG::new(
DEFAULT_GHOSTDAG_K,
dag_storage,
G_PRUNING_DEPTH,
G_PRUNING_FINALITY,
);
let dag = starcoin_dag::blockdag::BlockDAG::create_blockdag(dag_storage);
let (chain_info_stm, _) = Genesis::init_and_check_storage(
&net,
storage_stm.clone(),
Expand Down Expand Up @@ -2458,14 +2412,11 @@ pub fn verify_block(
db_storage,
))?);

let dag = BlockDAG::new(
DEFAULT_GHOSTDAG_K,
let dag = BlockDAG::create_blockdag(
starcoin_dag::consensusdb::prelude::FlexiDagStorage::create_from_path(
from_dir.join("dag/db/starcoindb"),
FlexiDagStorageConfig::new(),
)?,
G_PRUNING_DEPTH,
G_PRUNING_FINALITY,
);
let (chain_info, _) =
Genesis::init_and_check_storage(&net, storage.clone(), dag.clone(), from_dir.as_ref())?;
Expand Down Expand Up @@ -2574,14 +2525,11 @@ pub fn block_output(
db_storage,
))?);

let dag = BlockDAG::new(
DEFAULT_GHOSTDAG_K,
let dag = BlockDAG::create_blockdag(
starcoin_dag::consensusdb::prelude::FlexiDagStorage::create_from_path(
from_dir.join("dag/db/starcoindb"),
FlexiDagStorageConfig::new(),
)?,
G_PRUNING_DEPTH,
G_PRUNING_FINALITY,
);
let (chain_info, _) =
Genesis::init_and_check_storage(&net, storage.clone(), dag.clone(), from_dir.as_ref())?;
Expand Down Expand Up @@ -2621,14 +2569,11 @@ pub fn apply_block_output(
CacheStorage::new(None),
db_storage,
))?);
let dag = BlockDAG::new(
DEFAULT_GHOSTDAG_K,
let dag = BlockDAG::create_blockdag(
starcoin_dag::consensusdb::prelude::FlexiDagStorage::create_from_path(
to_dir.join("dag/db/starcoindb"),
FlexiDagStorageConfig::new(),
)?,
G_PRUNING_DEPTH,
G_PRUNING_FINALITY,
);
let (_chain_info, _) =
Genesis::init_and_check_storage(&net, storage.clone(), dag.clone(), to_dir.as_ref())?;
Expand Down Expand Up @@ -2684,14 +2629,11 @@ fn save_startup_info(
CacheStorage::new(None),
db_storage,
))?);
let dag = BlockDAG::new(
DEFAULT_GHOSTDAG_K,
let dag = BlockDAG::create_blockdag(
starcoin_dag::consensusdb::prelude::FlexiDagStorage::create_from_path(
to_dir.join("dag/db/starcoindb"),
FlexiDagStorageConfig::new(),
)?,
G_PRUNING_DEPTH,
G_PRUNING_FINALITY,
);
let (_chain_info, _) =
Genesis::init_and_check_storage(&net, storage.clone(), dag, to_dir.as_ref())?;
Expand Down Expand Up @@ -2722,14 +2664,11 @@ fn token_supply(
CacheStorage::new(None),
db_storage,
))?);
let dag = BlockDAG::new(
DEFAULT_GHOSTDAG_K,
let dag = BlockDAG::create_blockdag(
starcoin_dag::consensusdb::prelude::FlexiDagStorage::create_from_path(
from_dir.join("dag/db/starcoindb"),
FlexiDagStorageConfig::new(),
)?,
G_PRUNING_DEPTH,
G_PRUNING_FINALITY,
);
let (chain_info, _) =
Genesis::init_and_check_storage(&net, storage.clone(), dag.clone(), from_dir.as_ref())?;
Expand Down
10 changes: 2 additions & 8 deletions cmd/generator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ use anyhow::{bail, Result};
use starcoin_account::account_storage::AccountStorage;
use starcoin_account::AccountManager;
use starcoin_account_api::AccountInfo;
use starcoin_config::genesis_config::{G_PRUNING_DEPTH, G_PRUNING_FINALITY};
use starcoin_config::{NodeConfig, StarcoinOpt};
use starcoin_dag::blockdag::{BlockDAG, DEFAULT_GHOSTDAG_K};
use starcoin_dag::blockdag::BlockDAG;
use starcoin_genesis::Genesis;
use starcoin_storage::cache_storage::CacheStorage;
use starcoin_storage::db_storage::DBStorage;
Expand Down Expand Up @@ -37,12 +36,7 @@ pub fn init_or_load_data_dir(
config.storage.dag_dir(),
config.storage.clone().into(),
)?;
let dag = starcoin_dag::blockdag::BlockDAG::new(
DEFAULT_GHOSTDAG_K,
dag_storage,
G_PRUNING_DEPTH,
G_PRUNING_FINALITY,
);
let dag = starcoin_dag::blockdag::BlockDAG::create_blockdag(dag_storage);
let (chain_info, _genesis) = Genesis::init_and_check_storage(
config.net(),
storage.clone(),
Expand Down
Loading

0 comments on commit 218e7a0

Please sign in to comment.