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
11 changes: 11 additions & 0 deletions config/src/txpool_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ pub struct TxPoolConfig {
/// interval(s) of tx propagation timer. default to 2.
tx_propagate_interval: Option<u64>,

#[serde(skip_serializing_if = "Option::is_none")]
#[clap(name = "txpool-cull-interval", long)]
/// interval(s) of cull expired transactions timer. default to 1.
cull_interval: Option<u64>,

Comment thread
jackzhhuang marked this conversation as resolved.
#[serde(skip_serializing_if = "Option::is_none")]
#[clap(name = "txpool-min-gas-price", long)]
/// reject transaction whose gas_price is less than the min_gas_price. default to 1.
Expand Down Expand Up @@ -80,6 +85,9 @@ impl TxPoolConfig {
pub fn tx_propagate_interval(&self) -> u64 {
self.tx_propagate_interval.unwrap_or(2)
}
pub fn cull_interval(&self) -> u64 {
self.cull_interval.unwrap_or(1).max(1)
}
Comment thread
jackzhhuang marked this conversation as resolved.
pub fn min_gas_price(&self) -> u64 {
self.min_gas_price.unwrap_or(1)
}
Expand Down Expand Up @@ -109,6 +117,9 @@ impl ConfigModule for TxPoolConfig {
if let Some(m) = txpool_opt.tx_propagate_interval.as_ref() {
self.tx_propagate_interval = Some(*m);
}
if let Some(m) = txpool_opt.cull_interval.as_ref() {
self.cull_interval = Some(*m);
}
if let Some(m) = txpool_opt.min_gas_price.as_ref() {
self.min_gas_price = Some(*m);
}
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;
}
Comment thread
jackzhhuang marked this conversation as resolved.

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
Loading
Loading