Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion chain/mock/src/mock_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ impl MockChain {
) -> Result<Self> {
let storage = Arc::new(Storage::new(StorageInstance::new_cache_instance())?);
let storage2 = Arc::new(Storage2(storage.clone()));
let dag = BlockDAG::create_for_testing_with_parameters(k)?;
let genesis_hash = genesis.block().id();
let dag = BlockDAG::create_for_testing_with_parameters(k, genesis_hash)?;
let chain_info =
genesis.execute_genesis_block(&net, storage.clone(), storage2.clone(), dag.clone())?;

Expand Down
3 changes: 2 additions & 1 deletion chain/tests/block_test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ pub fn genesis_strategy(storage: Arc<Storage>) -> impl Strategy<Value = Block> {
BuiltinNetworkID::Test.genesis_config2().clone(),
);
let genesis = Genesis::load_or_build(&net).unwrap();
let genesis_hash = genesis.block().id();
let storage2 = Arc::new(Storage2(storage.clone()));
let dag = starcoin_dag::blockdag::BlockDAG::create_for_testing().unwrap();
let dag = starcoin_dag::blockdag::BlockDAG::create_for_testing(genesis_hash).unwrap();
genesis
.execute_genesis_block(&net, storage, storage2, dag)
.unwrap();
Expand Down
10 changes: 8 additions & 2 deletions cmd/generator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ use starcoin_account::account_storage::AccountStorage;
use starcoin_account::AccountManager;
use starcoin_account_api::AccountInfo;
use starcoin_config::{NodeConfig, StarcoinOpt};
use starcoin_crypto::HashValue;
use starcoin_dag::blockdag::BlockDAG;
use starcoin_genesis::Genesis;
use starcoin_storage::cache_storage::CacheStorage;
use starcoin_storage::db_storage::DBStorage;
use starcoin_storage::storage::StorageInstance;
use starcoin_storage::{Storage, Storage2};
use starcoin_storage::{BlockStore, Storage, Storage2};
use starcoin_types::startup_info::ChainInfo;
use std::sync::Arc;

Expand Down Expand Up @@ -49,18 +50,23 @@ pub fn init_or_load_data_dir(
.genesis_config2()
.consensus_config
.base_max_uncles_per_block;
let dag = starcoin_dag::blockdag::BlockDAG::new(
let genesis_hash = storage.get_genesis()?.unwrap_or(HashValue::zero());
let mut dag = starcoin_dag::blockdag::BlockDAG::new(
starcoin_types::blockhash::KType::try_from(k)?,
config.miner.dag_merge_depth(),
config.miner.maximum_parents_count(),
dag_storage.clone(),
genesis_hash,
);
let (chain_info, _genesis) = Genesis::init_and_check_storage(
config.net(),
storage.clone(),
dag.clone(),
config.data_dir(),
)?;
if genesis_hash == HashValue::zero() {
dag.set_genesis(chain_info.genesis_hash());
}
let vault_config = &config.vault;
let account_storage =
AccountStorage::create_from_path(vault_config.dir(), config.storage.rocksdb_config())?;
Expand Down
47 changes: 35 additions & 12 deletions flexidag/src/blockdag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,22 @@ pub struct BlockDAG {
block_depth_manager: BlockDepthManager,
max_parents_count: usize,
commit_lock: Arc<Mutex<FlexiDagStorage>>,
genesis: Hash,
}

impl BlockDAG {
pub fn create_blockdag(dag_storage: FlexiDagStorage) -> Self {
pub fn create_blockdag(dag_storage: FlexiDagStorage, genesis: Hash) -> Self {
// Test defaults: k=8, merge_depth=3600, max_parents=8 (k >= max_parents)
Self::new(8, 3600, 8, dag_storage)
Self::new(8, 3600, 8, dag_storage, genesis)
}

pub fn new(k: KType, merge_depth: u64, max_parents_count: usize, db: FlexiDagStorage) -> Self {
pub fn new(
k: KType,
merge_depth: u64,
max_parents_count: usize,
db: FlexiDagStorage,
genesis: Hash,
) -> Self {
// Ensure k >= max_parents_count to prevent protocol violations
assert!(
k as usize >= max_parents_count,
Expand Down Expand Up @@ -109,37 +116,36 @@ impl BlockDAG {
block_depth_manager,
max_parents_count,
commit_lock: Arc::new(Mutex::new(db)),
genesis,
}
}

/// For testing only - do not use in production code
pub fn create_for_testing() -> anyhow::Result<Self> {
pub fn create_for_testing(genesis: Hash) -> anyhow::Result<Self> {
let config = FlexiDagStorageConfig {
cache_size: 1024,
..Default::default()
};
let dag_storage = FlexiDagStorage::create_from_path(temp_dir(), config)?;
// Test defaults: k=8, merge_depth=3600, max_parents=8 (k >= max_parents)
Ok(Self::new(8, 3600, 8, dag_storage))
Ok(Self::new(8, 3600, 8, dag_storage, genesis))
}

/// For testing only - do not use in production code
pub fn create_for_testing_with_parameters(k: KType) -> anyhow::Result<Self> {
pub fn create_for_testing_with_parameters(k: KType, genesis: Hash) -> anyhow::Result<Self> {
let dag_storage =
FlexiDagStorage::create_from_path(temp_dir(), FlexiDagStorageConfig::default())?;
// Test defaults: merge_depth=3600, max_parents=3
Ok(Self::new(k, 3600, 3, dag_storage))
Ok(Self::new(k, 3600, 3, dag_storage, genesis))
}

/// For testing only - do not use in production code
pub fn create_for_testing_with_k_and_merge_depth(
k: KType,
merge_depth: u64,
genesis: Hash,
) -> anyhow::Result<Self> {
let dag_storage =
FlexiDagStorage::create_from_path(temp_dir(), FlexiDagStorageConfig::default())?;
// Test default: max_parents=3 (safe for small k values)
Ok(Self::new(k, merge_depth, 3, dag_storage))
Ok(Self::new(k, merge_depth, 3, dag_storage, genesis))
}

pub fn has_block_connected(&self, block_header: &BlockHeader) -> anyhow::Result<bool> {
Expand Down Expand Up @@ -540,7 +546,24 @@ impl BlockDAG {
}

pub fn get_dag_state(&self, hash: Hash) -> anyhow::Result<DagState> {
Ok(self.storage.state_store.read().get_state_by_hash(hash)?)
let query_hash = if hash == Hash::zero() {
self.genesis
} else {
hash
};
Ok(self
.storage
.state_store
.read()
.get_state_by_hash(query_hash)?)
}

pub fn genesis(&self) -> Hash {
self.genesis
}

pub fn set_genesis(&mut self, genesis: Hash) {
self.genesis = genesis;
}

pub fn save_dag_state_directly(&self, hash: Hash, state: DagState) -> anyhow::Result<()> {
Expand Down
4 changes: 2 additions & 2 deletions flexidag/tests/test_commit_atomicity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ fn test_commit_atomicity() -> Result<()> {
let db_tempdir = tempfile::tempdir()?;
let config = FlexiDagStorageConfig::new();
let dag_storage = FlexiDagStorage::create_from_path(db_tempdir.path(), config)?;
let mut dag = BlockDAG::new(8, 8, 3, dag_storage);

// Create and initialize genesis
let genesis = BlockHeaderBuilder::new()
.with_number(0)
.with_parent_hash(HashValue::zero())
.build();
let mut dag = BlockDAG::new(8, 8, 3, dag_storage, genesis.id());

// Initialize DAG with genesis
dag.init_with_genesis(genesis.clone())?;
Expand Down Expand Up @@ -68,13 +68,13 @@ fn test_partial_write_detection() -> Result<()> {
let db_tempdir = tempfile::tempdir()?;
let config = FlexiDagStorageConfig::new();
let dag_storage = FlexiDagStorage::create_from_path(db_tempdir.path(), config)?;
let mut dag = BlockDAG::new(8, 8, 3, dag_storage);

// Create and initialize genesis
let genesis = BlockHeaderBuilder::new()
.with_number(0)
.with_parent_hash(HashValue::zero())
.build();
let mut dag = BlockDAG::new(8, 8, 3, dag_storage, genesis.id());

// Initialize DAG with genesis
dag.init_with_genesis(genesis.clone())?;
Expand Down
45 changes: 19 additions & 26 deletions flexidag/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ use std::{

#[test]
fn test_dag_commit() -> Result<()> {
let mut dag = BlockDAG::create_for_testing().unwrap();
let genesis = BlockHeader::random()
.as_builder()
.with_difficulty(0.into())
.build();
let mut dag = BlockDAG::create_for_testing(genesis.id()).unwrap();

let mut parents_hash = vec![genesis.id()];
let _origin = dag.init_with_genesis(genesis.clone())?;
Expand Down Expand Up @@ -94,7 +94,7 @@ fn test_dag_1() -> Result<()> {
.build();
let mut latest_id = block6.id();
let genesis_id = genesis.id();
let mut dag = BlockDAG::create_for_testing().unwrap();
let mut dag = BlockDAG::create_for_testing(genesis.id()).unwrap();
let expect_selected_parented = [block5.id(), block3.id(), block3_1.id(), genesis_id];
let _origin = dag.init_with_genesis(genesis.clone())?;

Expand Down Expand Up @@ -147,7 +147,7 @@ async fn test_with_spawn() {
.with_difficulty(2.into())
.with_parents_hash(vec![genesis.id()])
.build();
let mut dag = BlockDAG::create_for_testing().unwrap();
let mut dag = BlockDAG::create_for_testing(genesis.id()).unwrap();
let _origin = dag.init_with_genesis(genesis.clone()).unwrap();

dag.commit_trusted_block(
Expand Down Expand Up @@ -200,11 +200,11 @@ async fn test_with_spawn() {

#[test]
fn test_write_asynchronization() -> anyhow::Result<()> {
let mut dag = BlockDAG::create_for_testing()?;
let genesis = BlockHeader::random()
.as_builder()
.with_difficulty(0.into())
.build();
let mut dag = BlockDAG::create_for_testing(genesis.id())?;
let _real_origin = dag.init_with_genesis(genesis.clone())?;

let parent = BlockHeaderBuilder::random()
Expand Down Expand Up @@ -273,12 +273,12 @@ fn test_write_asynchronization() -> anyhow::Result<()> {
#[test]
fn test_dag_genesis_fork() {
// initialzie the dag firstly
let mut dag = BlockDAG::create_for_testing().unwrap();

let genesis = BlockHeader::random()
.as_builder()
.with_difficulty(0.into())
.build();
let mut dag = BlockDAG::create_for_testing(genesis.id()).unwrap();

dag.init_with_genesis(genesis.clone()).unwrap();

// normally add the dag blocks
Expand Down Expand Up @@ -348,7 +348,7 @@ fn test_dag_genesis_fork() {

#[test]
fn test_dag_tips_store() {
let dag = BlockDAG::create_for_testing().unwrap();
let dag = BlockDAG::create_for_testing(Hash::random()).unwrap();

let state = DagState {
tips: vec![Hash::random()],
Expand All @@ -372,9 +372,8 @@ fn test_dag_tips_store() {
#[test]
fn test_dag_multiple_commits() -> anyhow::Result<()> {
// initialzie the dag firstly
let mut dag = BlockDAG::create_for_testing().unwrap();

let genesis = BlockHeader::random();
let mut dag = BlockDAG::create_for_testing(genesis.id()).unwrap();

dag.init_with_genesis(genesis.clone()).unwrap();

Expand Down Expand Up @@ -405,7 +404,7 @@ fn test_dag_multiple_commits() -> anyhow::Result<()> {

#[test]
fn test_reachability_abort_add_block() -> anyhow::Result<()> {
let dag = BlockDAG::create_for_testing().unwrap();
let dag = BlockDAG::create_for_testing(Hash::random()).unwrap();
let reachability_store = dag.storage.reachability_store.clone();

let mut parent = Hash::random();
Expand Down Expand Up @@ -462,7 +461,7 @@ fn test_reachability_abort_add_block() -> anyhow::Result<()> {

#[test]
fn test_reachability_check_ancestor() -> anyhow::Result<()> {
let dag = BlockDAG::create_for_testing().unwrap();
let dag = BlockDAG::create_for_testing(Hash::random()).unwrap();
let reachability_store = dag.storage.reachability_store.clone();

let mut parent = Hash::random();
Expand Down Expand Up @@ -588,7 +587,7 @@ fn print_reachability_data(reachability: &DbReachabilityStore, key: &[Hash]) {

#[test]
fn test_reachability_not_ancestor() -> anyhow::Result<()> {
let dag = BlockDAG::create_for_testing().unwrap();
let dag = BlockDAG::create_for_testing(Hash::random()).unwrap();
let reachability_store = dag.storage.reachability_store.clone();

let origin = Hash::random();
Expand Down Expand Up @@ -655,7 +654,7 @@ fn test_reachability_not_ancestor() -> anyhow::Result<()> {
#[test]
#[ignore = "maxmum data testing for dev"]
fn test_hint_virtaul_selected_parent() -> anyhow::Result<()> {
let dag = BlockDAG::create_for_testing().unwrap();
let dag = BlockDAG::create_for_testing(Hash::random()).unwrap();
let reachability_store = dag.storage.reachability_store.clone();

let origin = Hash::random();
Expand Down Expand Up @@ -711,7 +710,7 @@ fn test_hint_virtaul_selected_parent() -> anyhow::Result<()> {

#[test]
fn test_reachability_algorithm() -> anyhow::Result<()> {
let dag = BlockDAG::create_for_testing().unwrap();
let dag = BlockDAG::create_for_testing(Hash::random()).unwrap();
let reachability_store = dag.storage.reachability_store.clone();

let origin = Hash::random();
Expand Down Expand Up @@ -887,9 +886,8 @@ fn add_and_print(
#[test]
fn test_dag_mergeset() -> anyhow::Result<()> {
// initialzie the dag firstly
let mut dag = BlockDAG::create_for_testing().unwrap();

let genesis = BlockHeader::random();
let mut dag = BlockDAG::create_for_testing(genesis.id()).unwrap();

dag.init_with_genesis(genesis.clone()).unwrap();

Expand Down Expand Up @@ -925,9 +923,8 @@ fn test_dag_mergeset() -> anyhow::Result<()> {
#[ignore = "this is the large amount of data testing for performance, dev only"]
fn test_big_data_commit() -> anyhow::Result<()> {
// initialzie the dag firstly
let mut dag = BlockDAG::create_for_testing().unwrap();

let genesis = BlockHeader::random();
let mut dag = BlockDAG::create_for_testing(genesis.id()).unwrap();

dag.init_with_genesis(genesis.clone()).unwrap();

Expand Down Expand Up @@ -979,10 +976,9 @@ fn test_prune() -> anyhow::Result<()> {
let pruning_depth = 4;
let pruning_finality = 3;

let mut dag = BlockDAG::create_for_testing_with_parameters(k).unwrap();

let genesis = BlockHeader::random();
println!("genesis: {}", genesis.id());
let mut dag = BlockDAG::create_for_testing_with_parameters(k, genesis.id()).unwrap();

dag.init_with_genesis(genesis.clone()).unwrap();

Expand Down Expand Up @@ -1144,9 +1140,8 @@ fn test_verification_blue_block() -> anyhow::Result<()> {
// initialzie the dag firstly
let k = 5;

let mut dag = BlockDAG::create_for_testing_with_parameters(k).unwrap();

let genesis = BlockHeader::random();
let mut dag = BlockDAG::create_for_testing_with_parameters(k, genesis.id()).unwrap();

dag.init_with_genesis(genesis.clone()).unwrap();

Expand Down Expand Up @@ -1569,9 +1564,8 @@ fn test_verification_blue_block() -> anyhow::Result<()> {
#[test]
fn test_check_ancestor_of() -> anyhow::Result<()> {
// initialzie the dag firstly
let mut dag = BlockDAG::create_for_testing().unwrap();

let genesis = BlockHeader::random();
let mut dag = BlockDAG::create_for_testing(genesis.id()).unwrap();

dag.init_with_genesis(genesis.clone()).unwrap();

Expand Down Expand Up @@ -1631,9 +1625,8 @@ fn test_check_ancestor_of() -> anyhow::Result<()> {
#[test]
fn test_get_blocks_in_batch() -> anyhow::Result<()> {
// initialzie the dag firstly
let mut dag = BlockDAG::create_for_testing().unwrap();

let genesis = BlockHeader::random();
let mut dag = BlockDAG::create_for_testing(genesis.id()).unwrap();

dag.init_with_genesis(genesis.clone()).unwrap();

Expand Down
Loading
Loading