From 55bdf16fe62501ab579951e38e83034833f3e2fa Mon Sep 17 00:00:00 2001 From: jackzhhuang Date: Wed, 24 Jan 2024 09:45:47 +0800 Subject: [PATCH] add fork number in block chain --- Cargo.lock | 1 + chain/api/src/chain.rs | 4 + chain/mock/Cargo.toml | 1 + chain/mock/src/mock_chain.rs | 5 + chain/src/chain.rs | 42 ++++-- chain/src/verifier/mod.rs | 6 +- chain/tests/test_txn_info_and_proof.rs | 1 + flexidag/dag/src/blockdag.rs | 20 ++- flexidag/src/lib.rs | 52 +++---- .../test_write_dag_block_chain.rs | 1 - sync/src/tasks/block_sync_task.rs | 2 +- sync/src/tasks/mock.rs | 4 + sync/src/tasks/test_tools.rs | 6 +- sync/src/tasks/tests.rs | 2 +- sync/src/tasks/tests_dag.rs | 21 ++- sync/tests/test_rpc_client.rs | 2 - types/Cargo.toml | 2 + types/src/block/mod.rs | 142 +++++++++--------- 18 files changed, 186 insertions(+), 128 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2b4da80572..007c5cd1fe 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -11065,6 +11065,7 @@ dependencies = [ "hex", "lazy_static 1.4.0", "num_enum", + "parking_lot 0.12.1", "proptest", "proptest-derive", "rand 0.8.5", diff --git a/chain/api/src/chain.rs b/chain/api/src/chain.rs index a69427704a..601dde1c29 100644 --- a/chain/api/src/chain.rs +++ b/chain/api/src/chain.rs @@ -103,6 +103,10 @@ pub trait ChainReader { fn current_tips_hash(&self) -> Result>>; fn has_dag_block(&self, hash: HashValue) -> Result; + fn dag_fork_height(&self) -> BlockNumber; + fn is_dag(&self, block_header: &BlockHeader) -> bool; + fn is_legacy(&self, block_header: &BlockHeader) -> bool; + fn is_dag_genesis(&self, block_header: &BlockHeader) -> bool; } pub trait ChainWriter { diff --git a/chain/mock/Cargo.toml b/chain/mock/Cargo.toml index d0c895861d..cb89288f27 100644 --- a/chain/mock/Cargo.toml +++ b/chain/mock/Cargo.toml @@ -27,6 +27,7 @@ starcoin-dag = { workspace = true } [dev-dependencies] proptest = { workspace = true } proptest-derive = { workspace = true } +starcoin-chain = { workspace = true } [features] default = [] diff --git a/chain/mock/src/mock_chain.rs b/chain/mock/src/mock_chain.rs index e059e1f47c..5134dfeeca 100644 --- a/chain/mock/src/mock_chain.rs +++ b/chain/mock/src/mock_chain.rs @@ -14,6 +14,7 @@ use starcoin_storage::Storage; use starcoin_types::block::{Block, BlockHeader}; use starcoin_types::startup_info::ChainInfo; use std::sync::Arc; +use starcoin_types::block::BlockNumber; pub struct MockChain { net: ChainNetwork, @@ -105,6 +106,10 @@ impl MockChain { ) } + pub fn set_test_flexidag_fork_height(&mut self, fork_number: BlockNumber) { + self.head.set_test_flexidag_fork_height(fork_number); + } + pub fn fork(&self, head_id: Option) -> Result { let chain = self.fork_new_branch(head_id)?; Ok(Self { diff --git a/chain/src/chain.rs b/chain/src/chain.rs index 5852d9ca6e..7e9f3762d6 100644 --- a/chain/src/chain.rs +++ b/chain/src/chain.rs @@ -24,7 +24,7 @@ use starcoin_state_api::{AccountStateReader, ChainStateReader, ChainStateWriter} use starcoin_statedb::ChainStateDB; use starcoin_storage::Store; use starcoin_time_service::TimeService; -use starcoin_types::block::BlockIdAndNumber; +use starcoin_types::block::{BlockIdAndNumber, TEST_FLEXIDAG_FORK_HEIGHT_NEVER_REACH}; use starcoin_types::contract_event::ContractEventInfo; use starcoin_types::filter::Filter; use starcoin_types::startup_info::{ChainInfo, ChainStatus, DagState}; @@ -63,6 +63,7 @@ pub struct BlockChain { epoch: Epoch, vm_metrics: Option, dag: BlockDAG, + dag_fork_number: BlockNumber, } impl BlockChain { @@ -123,6 +124,7 @@ impl BlockChain { epoch, vm_metrics, dag, + dag_fork_number: TEST_FLEXIDAG_FORK_HEIGHT_NEVER_REACH, }; watch(CHAIN_WATCH_NAME, "n1251"); match uncles { @@ -180,6 +182,10 @@ impl BlockChain { self.dag.clone() } + pub fn set_test_flexidag_fork_height(&mut self, fork_number: BlockNumber) { + self.dag_fork_number = fork_number; + } + //TODO lazy init uncles cache. fn update_uncle_cache(&mut self) -> Result<()> { self.uncles = self.epoch_uncles()?; @@ -1002,7 +1008,7 @@ impl ChainReader for BlockChain { fn execute(&self, verified_block: VerifiedBlock) -> Result { let header = verified_block.0.header().clone(); - if !header.is_dag() { + if !self.is_dag(&header) { let executed = Self::execute_block_and_save( self.storage.as_ref(), self.statedb.fork(), @@ -1013,7 +1019,7 @@ impl ChainReader for BlockChain { verified_block.0, self.vm_metrics.clone(), )?; - if header.is_dag_genesis() { + if self.is_dag_genesis(&header) { let dag_genesis_id = header.id(); self.dag.init_with_genesis(header)?; self.storage.save_dag_state(DagState { @@ -1131,6 +1137,28 @@ impl ChainReader for BlockChain { fn has_dag_block(&self, hash: HashValue) -> Result { self.dag.has_dag_block(hash) } + + #[cfg(not(test))] + fn dag_fork_height(&self) -> BlockNumber { + 100000 + } + + #[cfg(test)] + fn dag_fork_height(&self) -> BlockNumber { + self.dag_fork_number + } + + fn is_dag(&self, block_header: &BlockHeader) -> bool { + block_header.number() > self.dag_fork_height() + } + + fn is_legacy(&self, block_header: &BlockHeader) -> bool { + !self.is_dag(block_header) && block_header.parents_hash().is_none() + } + + fn is_dag_genesis(&self, block_header: &BlockHeader) -> bool { + block_header.number() == self.dag_fork_height() + } } impl BlockChain { @@ -1301,10 +1329,6 @@ impl BlockChain { self.storage.save_dag_state(DagState { tips })?; Ok(executed_block) } - - pub fn dag_fork_height(&self) -> BlockNumber { - self.status.head.header().dag_fork_height() - } } impl ChainWriter for BlockChain { @@ -1313,7 +1337,7 @@ impl ChainWriter for BlockChain { } fn connect(&mut self, executed_block: ExecutedBlock) -> Result { - if executed_block.block.is_dag() { + if self.is_dag(executed_block.block.header()) { info!( "connect a dag block, {:?}, number: {:?}", executed_block.block.id(), @@ -1355,7 +1379,7 @@ impl ChainWriter for BlockChain { } fn apply(&mut self, block: Block) -> Result { - if !block.is_dag() { + if !self.is_dag(block.header()) { self.apply_with_verifier::(block) } else { self.apply_with_verifier::(block) diff --git a/chain/src/verifier/mod.rs b/chain/src/verifier/mod.rs index f929a7ab7b..6bc4438d3b 100644 --- a/chain/src/verifier/mod.rs +++ b/chain/src/verifier/mod.rs @@ -273,14 +273,14 @@ impl BlockVerifier for BasicVerifier { verify_block!( VerifyBlockField::Header, - !new_block_header.is_dag() + !current_chain.is_dag(new_block_header) && new_block_header .parents_hash() .unwrap_or_default() .is_empty(), "Single chain block is invalid: number {} fork_height {} parents_hash len {}", new_block_header.number(), - new_block_header.dag_fork_height(), + current_chain.dag_fork_height(), new_block_header.parents_hash().unwrap_or_default().len() ); Ok(()) @@ -369,7 +369,7 @@ impl BlockVerifier for DagVerifier { "Invalid parents_hash {:?} for a dag block {}, fork height {}", new_block_header.parents_hash(), new_block_header.number(), - new_block_header.dag_fork_height() + current_chain.dag_fork_height(), ); verify_block!( diff --git a/chain/tests/test_txn_info_and_proof.rs b/chain/tests/test_txn_info_and_proof.rs index f0b444faeb..f8f9c9ab9f 100644 --- a/chain/tests/test_txn_info_and_proof.rs +++ b/chain/tests/test_txn_info_and_proof.rs @@ -47,6 +47,7 @@ fn test_transaction_info_and_proof_1() -> Result<()> { // generate 5 block let config = Arc::new(NodeConfig::random_for_test()); let mut block_chain = test_helper::gen_blockchain_for_test(config.net())?; + block_chain.set_test_flexidag_fork_height(2); let _current_header = block_chain.current_header(); let miner_account = AccountInfo::random(); let mut seq_num = 0; diff --git a/flexidag/dag/src/blockdag.rs b/flexidag/dag/src/blockdag.rs index 9c7cc13362..5e20d5091e 100644 --- a/flexidag/dag/src/blockdag.rs +++ b/flexidag/dag/src/blockdag.rs @@ -83,7 +83,7 @@ impl BlockDAG { self.storage .relations_store .insert(origin, BlockHashes::new(vec![]))?; - self.commit(genesis)?; + self.commit_genesis(genesis)?; Ok(()) } pub fn ghostdata(&self, parents: &[HashValue]) -> GhostdagData { @@ -98,11 +98,19 @@ impl BlockDAG { } } + fn commit_genesis(&self, genesis: BlockHeader) -> anyhow::Result<()> { + self.commit_inner(genesis, true) + } + pub fn commit(&self, header: BlockHeader) -> anyhow::Result<()> { + self.commit_inner(header, false) + } + + fn commit_inner(&self, header: BlockHeader, is_dag_genesis: bool) -> anyhow::Result<()> { // Generate ghostdag data let parents = header.parents(); let ghostdata = self.ghostdata_by_hash(header.id())?.unwrap_or_else(|| { - Arc::new(if header.is_dag_genesis() { + Arc::new(if is_dag_genesis { self.ghostdag_manager.genesis_ghostdag_data(&header) } else { self.ghostdag_manager.ghostdag(&parents) @@ -161,7 +169,7 @@ mod tests { use super::*; use crate::consensusdb::prelude::FlexiDagStorageConfig; use starcoin_config::RocksdbConfig; - use starcoin_types::block::{BlockHeader, BlockHeaderBuilder}; + use starcoin_types::block::{BlockHeader, BlockHeaderBuilder, TEST_FLEXIDAG_FORK_HEIGHT_FOR_DAG}; use std::{env, fs}; fn build_block_dag(k: KType) -> BlockDAG { @@ -183,7 +191,7 @@ mod tests { #[test] fn test_dag_0() { let dag = BlockDAG::create_for_testing().unwrap(); - let genesis = BlockHeader::dag_genesis_random() + let genesis = BlockHeader::dag_genesis_random(TEST_FLEXIDAG_FORK_HEIGHT_FOR_DAG) .as_builder() .with_difficulty(0.into()) .build(); @@ -205,7 +213,7 @@ mod tests { #[test] fn test_dag_1() { - let genesis = BlockHeader::dag_genesis_random() + let genesis = BlockHeader::dag_genesis_random(TEST_FLEXIDAG_FORK_HEIGHT_FOR_DAG) .as_builder() .with_difficulty(0.into()) .build(); @@ -262,7 +270,7 @@ mod tests { #[tokio::test] async fn test_with_spawn() { use starcoin_types::block::{BlockHeader, BlockHeaderBuilder}; - let genesis = BlockHeader::dag_genesis_random() + let genesis = BlockHeader::dag_genesis_random(TEST_FLEXIDAG_FORK_HEIGHT_FOR_DAG) .as_builder() .with_difficulty(0.into()) .build(); diff --git a/flexidag/src/lib.rs b/flexidag/src/lib.rs index 39b4dd474f..319bf240fb 100644 --- a/flexidag/src/lib.rs +++ b/flexidag/src/lib.rs @@ -1,36 +1,34 @@ use std::path::Path; -use std::sync::Arc; -use starcoin_config::{ChainNetworkID, NodeConfig, RocksdbConfig}; +use starcoin_config::{ChainNetworkID, RocksdbConfig}; use starcoin_dag::blockdag::BlockDAG; use starcoin_dag::consensusdb::prelude::{FlexiDagStorage, FlexiDagStorageConfig}; -use starcoin_storage::Store; -pub fn try_init_with_storage( - storage: Arc, - config: Arc, -) -> anyhow::Result { - let dag = new_by_config( - config.data_dir().join("flexidag").as_path(), - config.net().id().clone(), - )?; - let startup_info = storage - .get_startup_info()? - .expect("startup info must exist"); +// pub fn try_init_with_storage( +// storage: Arc, +// config: Arc, +// ) -> anyhow::Result { +// let dag = new_by_config( +// config.data_dir().join("flexidag").as_path(), +// config.net().id().clone(), +// )?; +// let startup_info = storage +// .get_startup_info()? +// .expect("startup info must exist"); - let block_header = storage - .get_block_header_by_hash(*startup_info.get_main())? - .expect("the genesis block in dag accumulator must none be none"); - let fork_height = block_header.dag_fork_height(); - match block_header.number().cmp(&fork_height) { - std::cmp::Ordering::Greater | std::cmp::Ordering::Less => Ok(dag), - std::cmp::Ordering::Equal => { - // dag.commit(block_header)?; - dag.init_with_genesis(block_header)?; - Ok(dag) - } - } -} +// let block_header = storage +// .get_block_header_by_hash(*startup_info.get_main())? +// .expect("the genesis block in dag accumulator must none be none"); +// let fork_height = block_header.dag_fork_height(); +// match block_header.number().cmp(&fork_height) { +// std::cmp::Ordering::Greater | std::cmp::Ordering::Less => Ok(dag), +// std::cmp::Ordering::Equal => { +// // dag.commit(block_header)?; +// dag.init_with_genesis(block_header)?; +// Ok(dag) +// } +// } +// } pub fn new_by_config(db_path: &Path, _net: ChainNetworkID) -> anyhow::Result { let config = FlexiDagStorageConfig::create_with_params(1, RocksdbConfig::default()); diff --git a/sync/src/block_connector/test_write_dag_block_chain.rs b/sync/src/block_connector/test_write_dag_block_chain.rs index 5a57c8cc48..9d1a95d57e 100644 --- a/sync/src/block_connector/test_write_dag_block_chain.rs +++ b/sync/src/block_connector/test_write_dag_block_chain.rs @@ -9,7 +9,6 @@ use starcoin_chain_service::WriteableChainService; use starcoin_config::NodeConfig; use starcoin_consensus::Consensus; use starcoin_crypto::HashValue; -use starcoin_dag::blockdag::BlockDAG; use starcoin_time_service::TimeService; use starcoin_txpool_mock_service::MockTxPoolService; use starcoin_types::block::Block; diff --git a/sync/src/tasks/block_sync_task.rs b/sync/src/tasks/block_sync_task.rs index 619eadf2e0..474c89de65 100644 --- a/sync/src/tasks/block_sync_task.rs +++ b/sync/src/tasks/block_sync_task.rs @@ -451,7 +451,7 @@ where } pub fn ensure_dag_parent_blocks_exist(&mut self, block_header: BlockHeader) -> Result<()> { - if !block_header.is_dag() { + if !self.chain.is_dag(&block_header) { info!( "the block is not a dag block, skipping, its id: {:?}, its number {:?}", block_header.id(), diff --git a/sync/src/tasks/mock.rs b/sync/src/tasks/mock.rs index bc111f0db7..526f7b280b 100644 --- a/sync/src/tasks/mock.rs +++ b/sync/src/tasks/mock.rs @@ -243,6 +243,10 @@ impl SyncNodeMocker { } } + pub fn set_test_flexidag_fork_height(&mut self, fork_number: BlockNumber) { + self.chain_mocker.set_test_flexidag_fork_height(fork_number); + } + pub fn peer_info(&self) -> PeerInfo { PeerInfo::new( self.peer_id.clone(), diff --git a/sync/src/tasks/test_tools.rs b/sync/src/tasks/test_tools.rs index faa428ef5e..405b5b68b5 100644 --- a/sync/src/tasks/test_tools.rs +++ b/sync/src/tasks/test_tools.rs @@ -129,9 +129,12 @@ impl SyncTestSystem { } #[cfg(test)] -pub async fn full_sync_new_node() -> Result<()> { +pub async fn full_sync_new_node(fork_number: BlockNumber) -> Result<()> { + use starcoin_types::block::BlockNumber; + let net1 = ChainNetwork::new_builtin(BuiltinNetworkID::Test); let mut node1 = SyncNodeMocker::new(net1, 300, 0)?; + node1.set_test_flexidag_fork_height(fork_number); node1.produce_block(10)?; let mut arc_node1 = Arc::new(node1); @@ -139,6 +142,7 @@ pub async fn full_sync_new_node() -> Result<()> { let net2 = ChainNetwork::new_builtin(BuiltinNetworkID::Test); let node2 = SyncNodeMocker::new(net2.clone(), 300, 0)?; + node2.set_test_flexidag_fork_height(fork_number); let target = arc_node1.sync_target(); diff --git a/sync/src/tasks/tests.rs b/sync/src/tasks/tests.rs index de417cb480..5e38c9930f 100644 --- a/sync/src/tasks/tests.rs +++ b/sync/src/tasks/tests.rs @@ -46,7 +46,7 @@ use super::BlockConnectedEvent; #[stest::test(timeout = 120)] pub async fn test_full_sync_new_node() -> Result<()> { - full_sync_new_node().await + full_sync_new_node(false).await } #[stest::test] diff --git a/sync/src/tasks/tests_dag.rs b/sync/src/tasks/tests_dag.rs index c0fff798e4..018431aee8 100644 --- a/sync/src/tasks/tests_dag.rs +++ b/sync/src/tasks/tests_dag.rs @@ -14,15 +14,14 @@ use starcoin_chain_service::ChainReaderService; use starcoin_logger::prelude::*; use starcoin_service_registry::{RegistryAsyncService, RegistryService, ServiceRef}; use starcoin_txpool_mock_service::MockTxPoolService; +use starcoin_types::block::TEST_FLEXIDAG_FORK_HEIGHT_FOR_DAG; use test_helper::DummyNetworkService; #[stest::test(timeout = 120)] pub async fn test_full_sync_new_node_dag() { - starcoin_types::block::set_test_flexidag_fork_height(10); - full_sync_new_node() + full_sync_new_node(true) .await .expect("dag full sync should success"); - starcoin_types::block::reset_test_custom_fork_height(); } async fn sync_block_process( @@ -101,8 +100,13 @@ async fn sync_block_in_block_connection_service_mock( #[stest::test(timeout = 600)] async fn test_sync_single_chain_to_dag_chain() -> Result<()> { - starcoin_types::block::set_test_flexidag_fork_height(10); let test_system = super::test_tools::SyncTestSystem::initialize_sync_system().await?; + test_system + .target_node + .set_test_flexidag_fork_height(TEST_FLEXIDAG_FORK_HEIGHT_FOR_DAG); + test_system + .local_node + .set_test_flexidag_fork_height(TEST_FLEXIDAG_FORK_HEIGHT_FOR_DAG); let (_local_node, _target_node) = sync_block_in_block_connection_service_mock( Arc::new(test_system.target_node), Arc::new(test_system.local_node), @@ -110,16 +114,20 @@ async fn test_sync_single_chain_to_dag_chain() -> Result<()> { 40, ) .await?; - starcoin_types::block::reset_test_custom_fork_height(); Ok(()) } #[stest::test(timeout = 600)] async fn test_sync_red_blocks_dag() -> Result<()> { - starcoin_types::block::set_test_flexidag_fork_height(10); let test_system = super::test_tools::SyncTestSystem::initialize_sync_system() .await .expect("failed to init system"); + test_system + .target_node + .set_test_flexidag_fork_height(TEST_FLEXIDAG_FORK_HEIGHT_FOR_DAG); + test_system + .local_node + .set_test_flexidag_fork_height(TEST_FLEXIDAG_FORK_HEIGHT_FOR_DAG); let mut target_node = Arc::new(test_system.target_node); let local_node = Arc::new(test_system.local_node); Arc::get_mut(&mut target_node) @@ -183,6 +191,5 @@ async fn test_sync_red_blocks_dag() -> Result<()> { // // genertate the red blocks // Arc::get_mut(&mut target_node).unwrap().produce_block_by_header(dag_genesis_header, 5).expect("failed to produce block"); - starcoin_types::block::reset_test_custom_fork_height(); Ok(()) } diff --git a/sync/tests/test_rpc_client.rs b/sync/tests/test_rpc_client.rs index 7379588ae1..d9f493d142 100644 --- a/sync/tests/test_rpc_client.rs +++ b/sync/tests/test_rpc_client.rs @@ -20,7 +20,6 @@ struct DagBlockInfo { #[stest::test] fn test_verified_client_for_dag() { - starcoin_types::block::set_test_flexidag_fork_height(10); let (local_handle, target_handle, target_peer_id) = init_two_node().expect("failed to initalize the local and target node"); @@ -53,7 +52,6 @@ fn test_verified_client_for_dag() { .into_iter() .all(|child| { target_dag_block.children.contains(&child) })); }); - starcoin_types::block::reset_test_custom_fork_height(); target_handle.stop().unwrap(); local_handle.stop().unwrap(); } diff --git a/types/Cargo.toml b/types/Cargo.toml index 50c240ce85..2839ed498a 100644 --- a/types/Cargo.toml +++ b/types/Cargo.toml @@ -19,6 +19,8 @@ starcoin-uint = { workspace = true } starcoin-vm-types = { workspace = true } thiserror = { workspace = true } lazy_static= { workspace = true } +parking_lot = { workspace = true } + [features] default = [] fuzzing = ["proptest", "proptest-derive", "starcoin-vm-types/fuzzing"] diff --git a/types/src/block/mod.rs b/types/src/block/mod.rs index 19a4a31384..5158019efc 100644 --- a/types/src/block/mod.rs +++ b/types/src/block/mod.rs @@ -12,7 +12,6 @@ use crate::language_storage::CORE_CODE_ADDRESS; use crate::transaction::SignedUserTransaction; use crate::U256; use bcs_ext::Sample; -use lazy_static::lazy_static; pub use legacy::{ Block as LegacyBlock, BlockBody as LegacyBlockBody, BlockHeader as LegacyBlockHeader, }; @@ -29,46 +28,47 @@ use starcoin_vm_types::account_config::genesis_address; use starcoin_vm_types::transaction::authenticator::AuthenticationKey; use std::fmt::Formatter; use std::hash::Hash; -use std::sync::Mutex; /// Type for block number. pub type BlockNumber = u64; pub type ParentsHash = Option>; //TODO: make sure height -static DEV_FLEXIDAG_FORK_HEIGHT: BlockNumber = 2; -static PROXIMA_FLEXIDAG_FORK_HEIGHT: BlockNumber = 10000; -static HALLEY_FLEXIDAG_FORK_HEIGHT: BlockNumber = 10000; -static BARNARD_FLEXIDAG_FORK_HEIGHT: BlockNumber = 10000; -static MAIN_FLEXIDAG_FORK_HEIGHT: BlockNumber = 1000000; - -lazy_static! { - static ref TEST_FLEXIDAG_FORK_HEIGHT: Mutex = Mutex::new(10000); - static ref CUSTOM_FLEXIDAG_FORK_HEIGHT: Mutex = Mutex::new(10000); -} - -pub fn get_test_flexidag_fork_height() -> BlockNumber { - *TEST_FLEXIDAG_FORK_HEIGHT.lock().unwrap() -} - -pub fn get_custom_flexidag_fork_height() -> BlockNumber { - *CUSTOM_FLEXIDAG_FORK_HEIGHT.lock().unwrap() -} - -// TODO: support a macro such as #[cfg(test:consensus=dag)] to set fork height for testing customly and reset after executing. -pub fn set_test_flexidag_fork_height(value: BlockNumber) { - let mut num = TEST_FLEXIDAG_FORK_HEIGHT.lock().unwrap(); - *num = value; -} - -pub fn set_customm_flexidag_fork_height(value: BlockNumber) { - let mut num = TEST_FLEXIDAG_FORK_HEIGHT.lock().unwrap(); - *num = value; -} - -pub fn reset_test_custom_fork_height() { - *TEST_FLEXIDAG_FORK_HEIGHT.lock().unwrap() = 10000; - *CUSTOM_FLEXIDAG_FORK_HEIGHT.lock().unwrap() = 10000; -} +pub static TEST_FLEXIDAG_FORK_HEIGHT_FOR_DAG: BlockNumber = 8; +pub static TEST_FLEXIDAG_FORK_HEIGHT_NEVER_REACH: BlockNumber = 100000; +// static DEV_FLEXIDAG_FORK_HEIGHT: BlockNumber = 2; +// static PROXIMA_FLEXIDAG_FORK_HEIGHT: BlockNumber = 10000; +// static HALLEY_FLEXIDAG_FORK_HEIGHT: BlockNumber = 10000; +// static BARNARD_FLEXIDAG_FORK_HEIGHT: BlockNumber = 10000; +// static MAIN_FLEXIDAG_FORK_HEIGHT: BlockNumber = 1000000; + +// lazy_static! { +// static ref TEST_FLEXIDAG_FORK_HEIGHT: Mutex = Mutex::new(10000); +// static ref CUSTOM_FLEXIDAG_FORK_HEIGHT: Mutex = Mutex::new(10000); +// } + +// pub fn get_test_flexidag_fork_height() -> BlockNumber { +// *TEST_FLEXIDAG_FORK_HEIGHT.lock().unwrap() +// } + +// pub fn get_custom_flexidag_fork_height() -> BlockNumber { +// *CUSTOM_FLEXIDAG_FORK_HEIGHT.lock().unwrap() +// } + +// // TODO: support a macro such as #[cfg(test:consensus=dag)] to set fork height for testing customly and reset after executing. +// pub fn set_test_flexidag_fork_height(value: BlockNumber) { +// let mut num = TEST_FLEXIDAG_FORK_HEIGHT.lock().unwrap(); +// *num = value; +// } + +// pub fn set_customm_flexidag_fork_height(value: BlockNumber) { +// let mut num = TEST_FLEXIDAG_FORK_HEIGHT.lock().unwrap(); +// *num = value; +// } + +// pub fn reset_test_custom_fork_height() { +// *TEST_FLEXIDAG_FORK_HEIGHT.lock().unwrap() = 10000; +// *CUSTOM_FLEXIDAG_FORK_HEIGHT.lock().unwrap() = 10000; +// } /// Type for block header extra #[derive(Clone, Default, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, JsonSchema)] @@ -362,33 +362,33 @@ impl BlockHeader { pub fn is_genesis(&self) -> bool { self.number == 0 } - pub fn dag_fork_height(&self) -> BlockNumber { - if self.chain_id.is_test() { - get_test_flexidag_fork_height() - } else if self.chain_id.is_halley() { - HALLEY_FLEXIDAG_FORK_HEIGHT - } else if self.chain_id.is_proxima() { - PROXIMA_FLEXIDAG_FORK_HEIGHT - } else if self.chain_id.is_barnard() { - BARNARD_FLEXIDAG_FORK_HEIGHT - } else if self.chain_id.is_main() { - MAIN_FLEXIDAG_FORK_HEIGHT - } else if self.chain_id.is_dev() { - DEV_FLEXIDAG_FORK_HEIGHT - } else { - get_custom_flexidag_fork_height() - } - } - - pub fn is_dag(&self) -> bool { - self.number > self.dag_fork_height() - } + // pub fn dag_fork_height(&self) -> BlockNumber { + // if self.chain_id.is_test() { + // get_test_flexidag_fork_height() + // } else if self.chain_id.is_halley() { + // HALLEY_FLEXIDAG_FORK_HEIGHT + // } else if self.chain_id.is_proxima() { + // PROXIMA_FLEXIDAG_FORK_HEIGHT + // } else if self.chain_id.is_barnard() { + // BARNARD_FLEXIDAG_FORK_HEIGHT + // } else if self.chain_id.is_main() { + // MAIN_FLEXIDAG_FORK_HEIGHT + // } else if self.chain_id.is_dev() { + // DEV_FLEXIDAG_FORK_HEIGHT + // } else { + // get_custom_flexidag_fork_height() + // } + // } + + // pub fn is_dag(&self) -> bool { + // self.number > self.dag_fork_height() + // } pub fn is_legacy(&self) -> bool { - !self.is_dag() && self.parents_hash.is_none() - } - pub fn is_dag_genesis(&self) -> bool { - self.number == self.dag_fork_height() + self.parents_hash.is_none() } + // pub fn is_dag_genesis(&self) -> bool { + // self.number == self.dag_fork_height() + // } pub fn genesis_block_header( parent_hash: HashValue, @@ -417,10 +417,10 @@ impl BlockHeader { ) } //for test - pub fn dag_genesis_random() -> Self { + pub fn dag_genesis_random(dag_genesis_number: BlockNumber) -> Self { let mut header = Self::random(); header.parents_hash = Some(vec![header.parent_hash]); - header.number = get_test_flexidag_fork_height(); + header.number = dag_genesis_number; header } @@ -436,7 +436,9 @@ impl BlockHeader { // 1 - upgraded but non-dag header // 2 - dag block header pub fn random_with_opt(header_type: u8) -> Self { - let base = get_test_flexidag_fork_height().checked_add(1).unwrap(); + let base: u64 = TEST_FLEXIDAG_FORK_HEIGHT_NEVER_REACH + .checked_add(1) + .unwrap(); let (number, parents_hash) = if header_type == 0 { (rand::random::().checked_rem(base).unwrap(), None) } else if header_type == 1 { @@ -797,15 +799,15 @@ impl Block { } } - pub fn is_dag(&self) -> bool { - self.header.is_dag() - } + // pub fn is_dag(&self) -> bool { + // self.header.is_dag() + // } pub fn is_legacy(&self) -> bool { self.header.is_legacy() } - pub fn is_dag_genesis_block(&self) -> bool { - self.header.is_dag_genesis() - } + // pub fn is_dag_genesis_block(&self) -> bool { + // self.header.is_dag_genesis() + // } pub fn parent_hash(&self) -> HashValue { self.header.parent_hash()