Skip to content

Commit

Permalink
ad mock dag upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
jackzhhuang committed Feb 4, 2024
1 parent 11428ef commit 5cf3867
Show file tree
Hide file tree
Showing 22 changed files with 448 additions and 306 deletions.
11 changes: 8 additions & 3 deletions chain/mock/src/mock_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use starcoin_dag::blockdag::BlockDAG;
use starcoin_genesis::Genesis;
use starcoin_logger::prelude::*;
use starcoin_storage::{BlockStore, Storage};
use starcoin_types::block::BlockNumber;
use starcoin_types::block::{BlockNumber, TEST_FLEXIDAG_FORK_HEIGHT_NEVER_REACH};
use starcoin_types::block::{Block, BlockHeader};
use starcoin_types::startup_info::ChainInfo;
use std::sync::Arc;
Expand All @@ -25,8 +25,13 @@ pub struct MockChain {

impl MockChain {
pub fn new(net: ChainNetwork) -> Result<Self> {
let (storage, chain_info, _, dag) =
Genesis::init_storage_for_test(&net).expect("init storage by genesis fail.");
Self::new_with_fork(net, TEST_FLEXIDAG_FORK_HEIGHT_NEVER_REACH)
}


pub fn new_with_fork(net: ChainNetwork, fork_number: BlockNumber) -> Result<Self> {
let (storage, chain_info, _, dag) = Genesis::init_storage_for_test(&net, fork_number)
.expect("init storage by genesis fail.");

let chain = BlockChain::new(
net.time_service(),
Expand Down
3 changes: 2 additions & 1 deletion chain/service/src/chain_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,12 +456,13 @@ mod tests {
use starcoin_chain_api::ChainAsyncService;
use starcoin_config::NodeConfig;
use starcoin_service_registry::{RegistryAsyncService, RegistryService};
use starcoin_types::block::TEST_FLEXIDAG_FORK_HEIGHT_NEVER_REACH;

#[stest::test]
async fn test_actor_launch() -> Result<()> {
let config = Arc::new(NodeConfig::random_for_test());
let (storage, chain_info, _, dag) =
test_helper::Genesis::init_storage_for_test(config.net())?;
test_helper::Genesis::init_storage_for_test(config.net(), TEST_FLEXIDAG_FORK_HEIGHT_NEVER_REACH)?;
let registry = RegistryService::launch();
registry.put_shared(dag).await?;
registry.put_shared(config).await?;
Expand Down
14 changes: 9 additions & 5 deletions chain/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use starcoin_chain_api::{
use starcoin_consensus::Consensus;
use starcoin_crypto::hash::PlainCryptoHash;
use starcoin_crypto::HashValue;
use starcoin_dag::block_dag_config::BlockDAGType;
use starcoin_dag::blockdag::BlockDAG;
use starcoin_dag::consensusdb::prelude::StoreError;
use starcoin_executor::VMMetrics;
Expand Down Expand Up @@ -1142,11 +1143,14 @@ impl ChainReader for BlockChain {
fn dag_fork_height(&self) -> Result<BlockNumber> {
// todo: change return type to Result<BlockNumber>,
// try to handle db io error
Ok(self
.statedb
.get_on_chain_config::<FlexiDagConfig>()?
.map(|c| c.effective_height)
.unwrap_or(u64::MAX))
match self.dag.block_dag_config() {
BlockDAGType::BlockDAGFormal => Ok(self
.statedb
.get_on_chain_config::<FlexiDagConfig>()?
.map(|c| c.effective_height)
.unwrap_or(u64::MAX)),
BlockDAGType::BlockDAGTestMock(dag_mock_config) => Ok(dag_mock_config.fork_number),
}
}

fn is_dag(&self, block_header: &BlockHeader) -> Result<bool> {
Expand Down
12 changes: 12 additions & 0 deletions flexidag/dag/src/block_dag_config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use starcoin_types::block::BlockNumber;

#[derive(Clone, Debug)]
pub struct BlockDAGConfigMock {
pub fork_number: BlockNumber,
}

#[derive(Clone, Debug)]
pub enum BlockDAGType {
BlockDAGFormal,
BlockDAGTestMock(BlockDAGConfigMock),
}
33 changes: 30 additions & 3 deletions flexidag/dag/src/blockdag.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::reachability::{inquirer, reachability_service::MTReachabilityService};
use super::types::ghostdata::GhostdagData;
use crate::block_dag_config::{BlockDAGConfigMock, BlockDAGType};
use crate::consensusdb::prelude::{FlexiDagStorageConfig, StoreError};
use crate::consensusdb::schemadb::GhostdagStoreReader;
use crate::consensusdb::{
Expand All @@ -14,7 +15,7 @@ use anyhow::{anyhow, bail, Ok};
use parking_lot::RwLock;
use starcoin_config::{temp_dir, RocksdbConfig};
use starcoin_crypto::{HashValue as Hash, HashValue};
use starcoin_types::block::BlockHeader;
use starcoin_types::block::{BlockHeader, TEST_FLEXIDAG_FORK_HEIGHT_NEVER_REACH};
use starcoin_types::{
blockhash::{BlockHashes, KType},
consensus_header::ConsensusHeader,
Expand All @@ -33,10 +34,11 @@ pub type DbGhostdagManager = GhostdagManager<
pub struct BlockDAG {
pub storage: FlexiDagStorage,
ghostdag_manager: DbGhostdagManager,
dag_config: BlockDAGType,
}

impl BlockDAG {
pub fn new(k: KType, db: FlexiDagStorage) -> Self {
pub fn new_with_type(k: KType, db: FlexiDagStorage, dag_config: BlockDAGType) -> Self {
let ghostdag_store = db.ghost_dag_store.clone();
let header_store = db.header_store.clone();
let relations_store = db.relations_store.clone();
Expand All @@ -54,12 +56,33 @@ impl BlockDAG {
Self {
ghostdag_manager,
storage: db,
dag_config,
}
}

pub fn new(k: KType, db: FlexiDagStorage) -> Self {
Self::new_with_type(k, db, BlockDAGType::BlockDAGFormal)
}
pub fn create_for_testing() -> anyhow::Result<Self> {
let dag_storage =
FlexiDagStorage::create_from_path(temp_dir(), FlexiDagStorageConfig::default())?;
Ok(BlockDAG::new(8, dag_storage))
Ok(BlockDAG::new_with_type(
8,
dag_storage,
BlockDAGType::BlockDAGTestMock(BlockDAGConfigMock {
fork_number: TEST_FLEXIDAG_FORK_HEIGHT_NEVER_REACH,
}),
))
}

pub fn create_for_testing_mock(config: BlockDAGConfigMock) -> anyhow::Result<Self> {
let dag_storage =
FlexiDagStorage::create_from_path(temp_dir(), FlexiDagStorageConfig::default())?;
Ok(BlockDAG::new_with_type(
8,
dag_storage,
BlockDAGType::BlockDAGTestMock(config),
))
}

pub fn new_by_config(db_path: &Path) -> anyhow::Result<BlockDAG> {
Expand All @@ -69,6 +92,10 @@ impl BlockDAG {
Ok(dag)
}

pub fn block_dag_config(&self) -> BlockDAGType {
self.dag_config.clone()
}

pub fn has_dag_block(&self, hash: Hash) -> anyhow::Result<bool> {
Ok(self.storage.header_store.has(hash)?)
}
Expand Down
6 changes: 3 additions & 3 deletions flexidag/dag/src/ghostdag/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl<
.collect::<Result<Vec<_>, StoreError>>()?
.into_iter()
.max()
.ok_or_else(|| StoreError::MaxBlueworkNotFound)?
.ok_or(StoreError::MaxBlueworkNotFound)?
.hash)
}

Expand Down Expand Up @@ -216,7 +216,7 @@ impl<

if *candidate_blues_anticone_sizes
.get(&block)
.ok_or_else(|| StoreError::AnticoreSizeNotFound)?
.ok_or(StoreError::AnticoreSizeNotFound)?
== self.k
{
// k-cluster violation: A block in candidate's blue anticone already
Expand All @@ -229,7 +229,7 @@ impl<
assert!(
*candidate_blues_anticone_sizes
.get(&block)
.ok_or_else(|| StoreError::AnticoreSizeNotFound)?
.ok_or(StoreError::AnticoreSizeNotFound)?
<= self.k,
"found blue anticone larger than K"
);
Expand Down
1 change: 1 addition & 0 deletions flexidag/dag/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod block_dag_config;
pub mod blockdag;
pub mod consensusdb;
pub mod ghostdag;
Expand Down
5 changes: 4 additions & 1 deletion genesis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use starcoin_chain::{BlockChain, ChainReader};
use starcoin_config::{
genesis_key_pair, BuiltinNetworkID, ChainNetwork, ChainNetworkID, GenesisBlockParameter,
};
use starcoin_dag::block_dag_config::BlockDAGConfigMock;
use starcoin_dag::blockdag::BlockDAG;
use starcoin_logger::prelude::*;
use starcoin_state_api::ChainStateWriter;
Expand All @@ -24,6 +25,7 @@ use starcoin_storage::table_info::TableInfoStore;
use starcoin_storage::{BlockStore, Storage, Store};
use starcoin_transaction_builder::build_stdlib_package_with_modules;
use starcoin_transaction_builder::{build_stdlib_package, StdLibOptions};
use starcoin_types::block::BlockNumber;
use starcoin_types::block::LegacyBlock;
use starcoin_types::startup_info::{ChainInfo, StartupInfo};
use starcoin_types::transaction::Package;
Expand Down Expand Up @@ -380,11 +382,12 @@ impl Genesis {

pub fn init_storage_for_test(
net: &ChainNetwork,
fork_number: BlockNumber,
) -> Result<(Arc<Storage>, ChainInfo, Genesis, BlockDAG)> {
debug!("init storage by genesis for test. {net:?}");
let storage = Arc::new(Storage::new(StorageInstance::new_cache_instance())?);
let genesis = Genesis::load_or_build(net)?;
let dag = BlockDAG::create_for_testing()?;
let dag = BlockDAG::create_for_testing_mock(BlockDAGConfigMock { fork_number })?;
let chain_info = genesis.execute_genesis_block(net, storage.clone(), dag.clone())?;
Ok((storage, chain_info, genesis, dag))
}
Expand Down
49 changes: 35 additions & 14 deletions miner/src/create_block_template/test_create_block_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use starcoin_service_registry::{RegistryAsyncService, RegistryService};
use starcoin_storage::BlockStore;
use starcoin_time_service::MockTimeService;
use starcoin_txpool::TxPoolService;
use starcoin_types::block::TEST_FLEXIDAG_FORK_HEIGHT_NEVER_REACH;
use std::sync::Arc;

#[stest::test]
Expand All @@ -36,9 +37,11 @@ fn test_create_block_template_by_net(net: ChainNetworkID) {
opt.base_data_dir = Some(temp_path.path().to_path_buf());

let node_config = Arc::new(NodeConfig::load_with_opt(&opt).unwrap());
let (storage, chain_info, genesis, dag) =
StarcoinGenesis::init_storage_for_test(node_config.net())
.expect("init storage by genesis fail.");
let (storage, chain_info, genesis, dag) = StarcoinGenesis::init_storage_for_test(
node_config.net(),
TEST_FLEXIDAG_FORK_HEIGHT_NEVER_REACH,
)
.expect("init storage by genesis fail.");
let genesis_id = genesis.block().id();
let miner_account = AccountInfo::random();
let inner = Inner::new(
Expand All @@ -63,8 +66,11 @@ fn test_create_block_template_by_net(net: ChainNetworkID) {
#[stest::test(timeout = 120)]
fn test_switch_main() {
let node_config = Arc::new(NodeConfig::random_for_test());
let (storage, _, genesis, dag) = StarcoinGenesis::init_storage_for_test(node_config.net())
.expect("init storage by genesis fail.");
let (storage, _, genesis, dag) = StarcoinGenesis::init_storage_for_test(
node_config.net(),
TEST_FLEXIDAG_FORK_HEIGHT_NEVER_REACH,
)
.expect("init storage by genesis fail.");
let genesis_id = genesis.block().id();
let times = 10;

Expand Down Expand Up @@ -195,8 +201,11 @@ fn test_switch_main() {
#[stest::test]
fn test_do_uncles() {
let node_config = Arc::new(NodeConfig::random_for_test());
let (storage, _, genesis, dag) = StarcoinGenesis::init_storage_for_test(node_config.net())
.expect("init storage by genesis fail.");
let (storage, _, genesis, dag) = StarcoinGenesis::init_storage_for_test(
node_config.net(),
TEST_FLEXIDAG_FORK_HEIGHT_NEVER_REACH,
)
.expect("init storage by genesis fail.");
let genesis_id = genesis.block().id();
let times = 2;

Expand Down Expand Up @@ -323,8 +332,11 @@ fn test_do_uncles() {
#[stest::test(timeout = 120)]
fn test_new_head() {
let node_config = Arc::new(NodeConfig::random_for_test());
let (storage, _, genesis, dag) = StarcoinGenesis::init_storage_for_test(node_config.net())
.expect("init storage by genesis fail.");
let (storage, _, genesis, dag) = StarcoinGenesis::init_storage_for_test(
node_config.net(),
TEST_FLEXIDAG_FORK_HEIGHT_NEVER_REACH,
)
.expect("init storage by genesis fail.");
let genesis_id = genesis.block().id();
let times = 10;

Expand Down Expand Up @@ -367,8 +379,11 @@ fn test_new_head() {
#[stest::test(timeout = 120)]
fn test_new_branch() {
let node_config = Arc::new(NodeConfig::random_for_test());
let (storage, _, genesis, dag) = StarcoinGenesis::init_storage_for_test(node_config.net())
.expect("init storage by genesis fail.");
let (storage, _, genesis, dag) = StarcoinGenesis::init_storage_for_test(
node_config.net(),
TEST_FLEXIDAG_FORK_HEIGHT_NEVER_REACH,
)
.expect("init storage by genesis fail.");
let genesis_id = genesis.block().id();
let times = 5;

Expand Down Expand Up @@ -449,8 +464,11 @@ async fn test_create_block_template_actor() {
let registry = RegistryService::launch();
registry.put_shared(node_config.clone()).await.unwrap();

let (storage, _, genesis, dag) = StarcoinGenesis::init_storage_for_test(node_config.net())
.expect("init storage by genesis fail.");
let (storage, _, genesis, dag) = StarcoinGenesis::init_storage_for_test(
node_config.net(),
TEST_FLEXIDAG_FORK_HEIGHT_NEVER_REACH,
)
.expect("init storage by genesis fail.");
let genesis_id = genesis.block().id();
let chain_header = storage
.get_block_header_by_hash(genesis_id)
Expand Down Expand Up @@ -480,7 +498,10 @@ async fn test_create_block_template_actor() {
fn test_create_block_template_by_adjust_time() -> Result<()> {
let node_config = Arc::new(NodeConfig::random_for_test());

let (storage, _, genesis, dag) = StarcoinGenesis::init_storage_for_test(node_config.net())?;
let (storage, _, genesis, dag) = StarcoinGenesis::init_storage_for_test(
node_config.net(),
TEST_FLEXIDAG_FORK_HEIGHT_NEVER_REACH,
)?;
let mut inner = Inner::new(
node_config.net(),
storage,
Expand Down
4 changes: 3 additions & 1 deletion miner/tests/miner_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use starcoin_miner::{
use starcoin_service_registry::{RegistryAsyncService, RegistryService};
use starcoin_storage::BlockStore;
use starcoin_txpool::TxPoolService;
use starcoin_types::block::TEST_FLEXIDAG_FORK_HEIGHT_NEVER_REACH;
use starcoin_types::{system_events::GenerateBlockEvent, U256};
use std::sync::Arc;
use std::time::Duration;
Expand All @@ -24,7 +25,8 @@ async fn test_miner_service() {
let node_config = Arc::new(config.clone());
registry.put_shared(node_config.clone()).await.unwrap();
let (storage, _chain_info, genesis, dag) =
Genesis::init_storage_for_test(config.net()).unwrap();
Genesis::init_storage_for_test(config.net(), TEST_FLEXIDAG_FORK_HEIGHT_NEVER_REACH)
.unwrap();
registry.put_shared(storage.clone()).await.unwrap();
registry.put_shared(dag).await.unwrap();

Expand Down
15 changes: 14 additions & 1 deletion node/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use starcoin_block_relayer::BlockRelayer;
use starcoin_chain_notify::ChainNotifyHandlerService;
use starcoin_chain_service::ChainReaderService;
use starcoin_config::NodeConfig;
use starcoin_dag::block_dag_config::{BlockDAGConfigMock, BlockDAGType};
use starcoin_genesis::{Genesis, GenesisError};
use starcoin_logger::prelude::*;
use starcoin_logger::structured_log::init_slog_logger;
Expand Down Expand Up @@ -52,6 +53,7 @@ use starcoin_sync::sync::SyncService;
use starcoin_sync::txn_sync::TxnSyncService;
use starcoin_sync::verified_rpc_client::VerifiedRpcClient;
use starcoin_txpool::{TxPoolActorService, TxPoolService};
use starcoin_types::block::TEST_FLEXIDAG_FORK_HEIGHT_FOR_DAG;
use starcoin_types::system_events::{SystemShutdown, SystemStarted};
use starcoin_vm_runtime::metrics::VMMetrics;
use std::sync::Arc;
Expand Down Expand Up @@ -319,7 +321,18 @@ impl NodeService {
config.storage.dag_dir(),
config.storage.clone().into(),
)?;
let dag = starcoin_dag::blockdag::BlockDAG::new(8, dag_storage.clone());
let dag = match config.base().net().id() {
starcoin_config::ChainNetworkID::Builtin(starcoin_config::BuiltinNetworkID::Test) => {
starcoin_dag::blockdag::BlockDAG::new_with_type(
8,
dag_storage.clone(),
BlockDAGType::BlockDAGTestMock(BlockDAGConfigMock {
fork_number: TEST_FLEXIDAG_FORK_HEIGHT_FOR_DAG,
}),
)
},
_ => starcoin_dag::blockdag::BlockDAG::new(8, dag_storage.clone()),
};
registry.put_shared(dag.clone()).await?;
let (chain_info, genesis) =
Genesis::init_and_check_storage(config.net(), storage.clone(), dag, config.data_dir())?;
Expand Down
4 changes: 2 additions & 2 deletions state/service/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,13 +267,13 @@ mod tests {
use starcoin_config::NodeConfig;
use starcoin_service_registry::{RegistryAsyncService, RegistryService};
use starcoin_state_api::ChainStateAsyncService;
use starcoin_types::account_config::genesis_address;
use starcoin_types::{account_config::genesis_address, block::TEST_FLEXIDAG_FORK_HEIGHT_NEVER_REACH};

#[stest::test]
async fn test_actor_launch() -> Result<()> {
let config = Arc::new(NodeConfig::random_for_test());
let (storage, _startup_info, _, _) =
test_helper::Genesis::init_storage_for_test(config.net())?;
test_helper::Genesis::init_storage_for_test(config.net(), TEST_FLEXIDAG_FORK_HEIGHT_NEVER_REACH)?;
let registry = RegistryService::launch();
registry.put_shared(config).await?;
registry.put_shared(storage).await?;
Expand Down
Loading

0 comments on commit 5cf3867

Please sign in to comment.