From ba8acb9e8bdb1cab4f7e3ec51d01539a00253192 Mon Sep 17 00:00:00 2001 From: simonjiao Date: Fri, 24 May 2024 13:56:49 +0800 Subject: [PATCH 1/5] rolback dag-effective-height to zero * init dag when executing genesis block * fix check_dag_type * restore dag_fork_height * build dev network as dag chain * Update generated genesis --- chain/Cargo.toml | 3 +- chain/src/chain.rs | 49 ++++++++++++++------- cmd/starcoin/Cargo.toml | 3 +- config/Cargo.toml | 4 ++ config/example/proxima/genesis_config.json | 2 +- config/src/genesis_config.rs | 13 +++--- executor/tests/executor_test.rs | 4 +- genesis/generated/halley/genesis | Bin 92748 -> 92748 bytes genesis/generated/proxima/genesis | Bin 92700 -> 92700 bytes genesis/src/lib.rs | 5 ++- 10 files changed, 56 insertions(+), 27 deletions(-) diff --git a/chain/Cargo.toml b/chain/Cargo.toml index 459f5097ae..f3d91f3430 100644 --- a/chain/Cargo.toml +++ b/chain/Cargo.toml @@ -49,9 +49,10 @@ starcoin-network-rpc-api = { workspace = true } starcoin-force-upgrade = { workspace = true } [features] -default = [] +default = ["sync-dag-test"] fuzzing = ["proptest", "proptest-derive", "starcoin-types/fuzzing"] force-deploy = ["starcoin-vm-runtime/force-deploy", "starcoin-vm-runtime", "starcoin-executor/force-deploy"] +sync-dag-test = [] [package] authors = { workspace = true } diff --git a/chain/src/chain.rs b/chain/src/chain.rs index 3a200381f0..e4f638ccf4 100644 --- a/chain/src/chain.rs +++ b/chain/src/chain.rs @@ -14,7 +14,6 @@ use starcoin_chain_api::{ verify_block, ChainReader, ChainWriter, ConnectBlockError, EventWithProof, ExcludedTxns, ExecutedBlock, MintedUncleNumber, TransactionInfoWithProof, VerifiedBlock, VerifyBlockField, }; -use starcoin_config::genesis_config::{G_TEST_DAG_FORK_HEIGHT, G_TEST_DAG_FORK_STATE_KEY}; use starcoin_consensus::Consensus; use starcoin_crypto::hash::PlainCryptoHash; use starcoin_crypto::HashValue; @@ -812,13 +811,16 @@ impl BlockChain { let final_block_gas_limit = block_gas_limit .map(|block_gas_limit| min(block_gas_limit, on_chain_block_gas_limit)) .unwrap_or(on_chain_block_gas_limit); - let (_, tips_hash) = if current_number <= self.dag_fork_height()?.unwrap_or(u64::MAX) { - (None, None) + let tips_hash = if current_number <= self.dag_fork_height()?.unwrap_or(u64::MAX) { + None } else if tips.is_some() { - (Some(self.get_block_dag_genesis(&previous_header)?), tips) + tips } else { - let result = self.current_tips_hash(&previous_header)?.expect("the block number is larger than the dag fork number but the state data doese not exis"); - (Some(result.0), Some(result.1)) + Some( + self.current_tips_hash(&previous_header)? + .map(|r| r.1) + .expect("Creating a Dag block but tips don't exist"), + ) }; let strategy = epoch.strategy(); let difficulty = strategy.calculate_next_difficulty(self)?; @@ -852,8 +854,8 @@ impl BlockChain { } }; debug!( - "Blue blocks:{:?} in chain/create_block_template_by_header", - blue_blocks + "current_number: {}, Blue blocks:{:?} tips_hash {:?} in chain/create_block_template_by_header", + current_number, blue_blocks, tips_hash ); let mut opened_block = OpenedBlock::new( self.storage.clone(), @@ -1713,25 +1715,27 @@ impl BlockChain { use std::cmp::Ordering; let dag_height = self.dag_fork_height()?.unwrap_or(u64::MAX); - if header.is_genesis() { - return Ok(DagHeaderType::Single); - } let no_parents = header.parents_hash().unwrap_or_default().is_empty(); match (no_parents, header.number().cmp(&dag_height)) { - (true, Ordering::Greater) => { - Err(anyhow!("block header with suitable height but no parents")) - } + (true, Ordering::Greater) => Err(anyhow!( + "block header with suitable height {}/{} but no parents", + header.number(), + dag_height + )), (false, Ordering::Greater) => Ok(DagHeaderType::Normal), (true, Ordering::Equal) => Ok(DagHeaderType::Genesis), (false, Ordering::Equal) => Err(anyhow!( - "block header with dag genesis height but having parents" + "block header with dag genesis height {} but having parents", + dag_height )), (true, Ordering::Less) => Ok(DagHeaderType::Single), (false, Ordering::Less) => Err(anyhow!( - "block header with smaller height but having parents" + "block header with smaller height {}/{} but having parents", + header.number(), + dag_height )), } } @@ -2344,7 +2348,12 @@ impl BlockChain { Ok(executed_block) } + // todo: please remove me. + // Try to set custom dag_effective_height for `test` network for different test cases, + // or using different features to set the height. + #[cfg(feature = "sync-dag-test")] pub fn dag_fork_height(&self) -> Result> { + use starcoin_config::genesis_config::{G_TEST_DAG_FORK_HEIGHT, G_TEST_DAG_FORK_STATE_KEY}; let chain_id = self.status().head().chain_id(); if chain_id.is_test() { let result = self.dag.get_dag_state(*G_TEST_DAG_FORK_STATE_KEY); @@ -2368,6 +2377,14 @@ impl BlockChain { .map(|c| c.effective_height)) } } + + #[cfg(not(feature = "sync-dag-test"))] + pub fn dag_fork_height(&self) -> Result> { + Ok(self + .statedb + .get_on_chain_config::()? + .map(|c| c.effective_height)) + } } impl ChainWriter for BlockChain { diff --git a/cmd/starcoin/Cargo.toml b/cmd/starcoin/Cargo.toml index 4c6c6269b3..48de2cadf3 100644 --- a/cmd/starcoin/Cargo.toml +++ b/cmd/starcoin/Cargo.toml @@ -60,8 +60,9 @@ stest = { workspace = true } test-helper = { workspace = true } [features] -default = [] +default = ["dag-dev-network"] force-deploy = ["starcoin-node/force-deploy"] +dag-dev-network = ["starcoin-config/dag-dev-network"] [package] authors = { workspace = true } diff --git a/config/Cargo.toml b/config/Cargo.toml index 27410effca..bac6813b7d 100644 --- a/config/Cargo.toml +++ b/config/Cargo.toml @@ -34,6 +34,10 @@ tempfile = { workspace = true } thiserror = { workspace = true } toml = { features = ["default"], workspace = true } +[features] +default = [] +dag-dev-network = [] + [package] authors = { workspace = true } edition = { workspace = true } diff --git a/config/example/proxima/genesis_config.json b/config/example/proxima/genesis_config.json index deded93b32..938b472b79 100644 --- a/config/example/proxima/genesis_config.json +++ b/config/example/proxima/genesis_config.json @@ -527,5 +527,5 @@ }, "time_service_type": "RealTimeService", "transaction_timeout": 86400, - "dag_effective_height": 1 + "dag_effective_height": 0 } diff --git a/config/src/genesis_config.rs b/config/src/genesis_config.rs index 562f08e61d..d2cda9e8c2 100644 --- a/config/src/genesis_config.rs +++ b/config/src/genesis_config.rs @@ -779,6 +779,11 @@ pub static G_DEV_CONFIG: Lazy = Lazy::new(|| { let mut gas_constant = G_TEST_GAS_CONSTANTS.clone(); gas_constant.min_price_per_gas_unit = 1; + #[cfg(feature = "dag-dev-network")] + let dag_effective_height = 0u64; + #[cfg(not(feature = "dag-dev-network"))] + let dag_effective_height = u64::MAX; + let stdlib_version = StdlibVersion::Latest; GenesisConfig { genesis_block_parameter: GenesisBlockParameterConfig::Static(GenesisBlockParameter { @@ -822,7 +827,7 @@ pub static G_DEV_CONFIG: Lazy = Lazy::new(|| { min_action_delay: 60 * 60 * 1000, // 1h }, transaction_timeout: ONE_DAY, - dag_effective_height: u64::MAX, + dag_effective_height, } }); @@ -881,8 +886,7 @@ pub static G_HALLEY_CONFIG: Lazy = Lazy::new(|| { min_action_delay: 60 * 60 * 1000, // 1h }, transaction_timeout: ONE_DAY, - // todo: rollback it to zero and initialize BlockDag properly - dag_effective_height: 1u64, + dag_effective_height: 0u64, } }); @@ -941,8 +945,7 @@ pub static G_PROXIMA_CONFIG: Lazy = Lazy::new(|| { min_action_delay: 60 * 1000, // 1 minute }, transaction_timeout: ONE_DAY, - // todo: rollback it to zero and initialize BlockDag properly - dag_effective_height: 1u64, + dag_effective_height: 0u64, } }); diff --git a/executor/tests/executor_test.rs b/executor/tests/executor_test.rs index d45015e78f..cd9c02e726 100644 --- a/executor/tests/executor_test.rs +++ b/executor/tests/executor_test.rs @@ -123,7 +123,7 @@ fn test_flexidag_config_get_for_halley() { vm.get_flexidag_config().unwrap().effective_height }; - assert_eq!(version, 1); + assert_eq!(version, 0); } #[stest::test] @@ -137,7 +137,7 @@ fn test_flexidag_config_get_for_proxima() { vm.get_flexidag_config().unwrap().effective_height }; - assert_eq!(version, 1); + assert_eq!(version, 0); } #[stest::test] diff --git a/genesis/generated/halley/genesis b/genesis/generated/halley/genesis index 22cac4c47f7e392e9c6e6555ff79b8ac7522b483..3b625674187146bbea8dd9343fceaac1b3b7b239 100644 GIT binary patch delta 200 zcmV;(05|{4)CJ7c1&~7^^mSQA6Zz2mVby)il!ubQA4XV0$q9EJu{y_`HSFetkz*nt zq3(Nv3k?%rz5X&~kvd9mpOXBxY@O-O$i}xmS4~$RlhKYJk-!KdWFQhf#w%)MAlQyj zny-d|jMbS{vzaW;MYxX)Zh;+{%9nxeweA7SXpylPm(ghfIze+z^f!DDH&I4wRaE{0 zP7z8qzUgf@xz-Z>oX=8xOzr?&%0J_8B+l-*R3;TQRh!DNS}jkz*nt zZ0_U>c8Q?t|Jgt)dF1u%!+q{Ci5sJUIKfw<|NUoOlhKYJk-!KdWFYwSm?c@+sZ6>^ z8l*BnN%GPMMUga;5_1r6pf9T6voV40weA7SXaNC{u@{%oX#qMx#PS3<&cHx)V@6fE z0+}Gn_`rbmRTgRhY5~fCyoge-8QXk_Yqq6tN@(OR%Zcp1m#AKPm1)P-g`L_|l?uxa E06fxT5&!@I diff --git a/genesis/generated/proxima/genesis b/genesis/generated/proxima/genesis index 663e4e5645a6d125e47ba10ddbfeaab6b77d5310..ca55e25f9871d51a589dc57903d7943560b99942 100644 GIT binary patch delta 200 zcmV;(05|`f)CHW>1&~7^wiXuU>xTt`rk&-$@I^!RpOJX%jrLMMqAp;Ie5_ozkz*nt zr{bZ7xEXOp{I3UlON4ArWam3d3#(@qsvl73=6A$clhKYJk-!KdWFQ#k!s1E|ku=sn z{-OI&$~2*R<#`6@nGuWyh}x`?pr3*6weA6%XpylPm!W9^IzgUY$bSHTn%e5-1#ll$ z(~4@~6Rx>}mIN8bU-90S0lhKigXo=tkF=-Emk1&~7^%zqu}BMH4T4F2ZDb9|Q?`?}K)oJeIfU*2wM573KCkz*nt zZ!m$j6(X%@YQlUCbBKaC=djDc1=H@*?p{++r!d+klhKYJk-!KdWFR##^J^zfdmt%l znf{o%LGtZmtuKa3*pPiPe(V+89j}4zweA6%XaNC{u@{%2X#qMxDqBXP{T*{Ky%$;G z!4$T5E6t{iI8MCjG{K1uW`+1+@04uDqGsI<2Z<`7#OjTZ)$zPv%=N(oB{1s^KjF&+ E0M|EQ=l}o! diff --git a/genesis/src/lib.rs b/genesis/src/lib.rs index 71c0784439..b60f74f8e5 100644 --- a/genesis/src/lib.rs +++ b/genesis/src/lib.rs @@ -279,13 +279,16 @@ impl Genesis { dag: BlockDAG, ) -> Result { storage.save_genesis(self.block.id())?; - let genesis_chain = BlockChain::new_with_genesis( + let mut genesis_chain = BlockChain::new_with_genesis( net.time_service(), storage.clone(), net.genesis_epoch(), self.block.clone(), dag, )?; + + genesis_chain.init_dag_with_genesis(self.block.header().clone())?; + let startup_info = StartupInfo::new(genesis_chain.current_header().id()); storage.save_startup_info(startup_info)?; storage From 9d854c032865e8731b99e623b2b689fae35ae790 Mon Sep 17 00:00:00 2001 From: simonjiao Date: Mon, 27 May 2024 09:44:02 +0800 Subject: [PATCH 2/5] fix sync case for dag chain --- cmd/replay/src/main.rs | 5 +---- config/src/lib.rs | 9 --------- sync/src/tasks/tests.rs | 3 ++- 3 files changed, 3 insertions(+), 14 deletions(-) diff --git a/cmd/replay/src/main.rs b/cmd/replay/src/main.rs index 896d0c2f98..c0c6a9ba80 100644 --- a/cmd/replay/src/main.rs +++ b/cmd/replay/src/main.rs @@ -49,10 +49,7 @@ fn main() -> anyhow::Result<()> { let _logger = starcoin_logger::init(); let opts: ReplayOpt = ReplayOpt::parse(); - let network = match opts.net { - Some(network) => network, - None => BuiltinNetworkID::Proxima, - }; + let network = opts.net.unwrap_or(BuiltinNetworkID::Proxima); let net = ChainNetwork::new_builtin(network); let from_dir = opts.from; diff --git a/config/src/lib.rs b/config/src/lib.rs index 87320ca097..868f8b6dd2 100644 --- a/config/src/lib.rs +++ b/config/src/lib.rs @@ -484,15 +484,6 @@ impl NodeConfig { Self::load_with_opt(&opt).expect("Auto generate test config should success.") } - pub fn proxima_for_test(dir: PathBuf) -> Self { - let opt = StarcoinOpt { - net: Some(BuiltinNetworkID::Proxima.into()), - base_data_dir: Some(dir), - ..StarcoinOpt::default() - }; - Self::load_with_opt(&opt).expect("Auto generate proxima config should success.") - } - pub fn customize_for_test() -> Self { let opt = StarcoinOpt { net: Some(BuiltinNetworkID::Test.into()), diff --git a/sync/src/tasks/tests.rs b/sync/src/tasks/tests.rs index 748b8a32a3..2b809dfd6e 100644 --- a/sync/src/tasks/tests.rs +++ b/sync/src/tasks/tests.rs @@ -107,7 +107,8 @@ pub async fn test_sync_invalid_target() -> Result<()> { #[stest::test] pub async fn test_failed_block() -> Result<()> { - let net = ChainNetwork::new_builtin(BuiltinNetworkID::Halley); + // todo: fix this test for dag chain (dag effective height is ZERO) + let net = ChainNetwork::new_builtin(BuiltinNetworkID::Test); let (storage, chain_info, _, dag) = Genesis::init_storage_for_test(&net)?; let chain = BlockChain::new( From acd474bd2c330969e6ea0f354538d58c59851a66 Mon Sep 17 00:00:00 2001 From: simonjiao Date: Mon, 27 May 2024 14:36:55 +0800 Subject: [PATCH 3/5] remove unsupported feature from integration tests --- testsuite/features/cmd.feature | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/testsuite/features/cmd.feature b/testsuite/features/cmd.feature index b64334a995..ab6cb21831 100644 --- a/testsuite/features/cmd.feature +++ b/testsuite/features/cmd.feature @@ -184,19 +184,6 @@ Feature: cmd integration test Examples: | | - #StarcoinFramework checkpoint - Scenario Outline: [ignore] starcoin-framework checkpoint - Then cmd: "dev get-coin" - Then cmd: "account unlock" - Then cmd: "account execute-function --function 0x1::Block::checkpoint_entry -b" - Then cmd: "dev call-api chain.get_block_by_number [1,{\"raw\":true}]" - Then cmd: "account execute-function --function 0x1::Block::update_state_root_entry --arg {{$.dev[1].ok.raw.header}} -b" - Then cmd: "dev call --function 0x1::Block::latest_state_root" - Then assert: "{{$.dev[2].ok[1]}} == {{$.dev[1].ok.header.state_root}}" - - Examples: - | | - #flexidagconfig dao testing Scenario Outline: [cmd] starcoin flexidagconfig dao # 1. deposit to default account which is a proposer From 79e7a0695c2d5ed7ecf8d07f193a472138e3d0e7 Mon Sep 17 00:00:00 2001 From: simonjiao Date: Mon, 27 May 2024 15:11:19 +0800 Subject: [PATCH 4/5] config dev network as dag chain --- chain/tests/test_block_chain.rs | 15 ++++++++------- cmd/starcoin/Cargo.toml | 3 +-- config/Cargo.toml | 1 - config/src/genesis_config.rs | 7 +------ 4 files changed, 10 insertions(+), 16 deletions(-) diff --git a/chain/tests/test_block_chain.rs b/chain/tests/test_block_chain.rs index a985516aee..33620bb616 100644 --- a/chain/tests/test_block_chain.rs +++ b/chain/tests/test_block_chain.rs @@ -145,20 +145,21 @@ fn test_block_chain() -> Result<()> { } #[stest::test(timeout = 480)] -fn test_halley_consensus() { - let mut mock_chain = - MockChain::new(ChainNetwork::new_builtin(BuiltinNetworkID::Halley)).unwrap(); +fn test_halley_consensus() -> Result<()> { + let mut mock_chain = MockChain::new(ChainNetwork::new_builtin(BuiltinNetworkID::Halley))?; let times = 20; - mock_chain.produce_and_apply_times(times).unwrap(); + mock_chain.produce_and_apply_times(times)?; assert_eq!(mock_chain.head().current_header().number(), times); + Ok(()) } #[stest::test(timeout = 240)] -fn test_dev_consensus() { - let mut mock_chain = MockChain::new(ChainNetwork::new_builtin(BuiltinNetworkID::Dev)).unwrap(); +fn test_dev_consensus() -> Result<()> { + let mut mock_chain = MockChain::new(ChainNetwork::new_builtin(BuiltinNetworkID::Dev))?; let times = 20; - mock_chain.produce_and_apply_times(times).unwrap(); + mock_chain.produce_and_apply_times(times)?; assert_eq!(mock_chain.head().current_header().number(), times); + Ok(()) } #[stest::test] diff --git a/cmd/starcoin/Cargo.toml b/cmd/starcoin/Cargo.toml index 48de2cadf3..4c6c6269b3 100644 --- a/cmd/starcoin/Cargo.toml +++ b/cmd/starcoin/Cargo.toml @@ -60,9 +60,8 @@ stest = { workspace = true } test-helper = { workspace = true } [features] -default = ["dag-dev-network"] +default = [] force-deploy = ["starcoin-node/force-deploy"] -dag-dev-network = ["starcoin-config/dag-dev-network"] [package] authors = { workspace = true } diff --git a/config/Cargo.toml b/config/Cargo.toml index bac6813b7d..e86c15fa1e 100644 --- a/config/Cargo.toml +++ b/config/Cargo.toml @@ -36,7 +36,6 @@ toml = { features = ["default"], workspace = true } [features] default = [] -dag-dev-network = [] [package] authors = { workspace = true } diff --git a/config/src/genesis_config.rs b/config/src/genesis_config.rs index d2cda9e8c2..b667f05b5a 100644 --- a/config/src/genesis_config.rs +++ b/config/src/genesis_config.rs @@ -779,11 +779,6 @@ pub static G_DEV_CONFIG: Lazy = Lazy::new(|| { let mut gas_constant = G_TEST_GAS_CONSTANTS.clone(); gas_constant.min_price_per_gas_unit = 1; - #[cfg(feature = "dag-dev-network")] - let dag_effective_height = 0u64; - #[cfg(not(feature = "dag-dev-network"))] - let dag_effective_height = u64::MAX; - let stdlib_version = StdlibVersion::Latest; GenesisConfig { genesis_block_parameter: GenesisBlockParameterConfig::Static(GenesisBlockParameter { @@ -827,7 +822,7 @@ pub static G_DEV_CONFIG: Lazy = Lazy::new(|| { min_action_delay: 60 * 60 * 1000, // 1h }, transaction_timeout: ONE_DAY, - dag_effective_height, + dag_effective_height: 0u64, } }); From 0ed3a57b95a8a580edf1453d7f61a03262dd72c6 Mon Sep 17 00:00:00 2001 From: jackzhhuang Date: Fri, 31 May 2024 13:02:03 +0800 Subject: [PATCH 5/5] use FlexiDagConfig when reading dag fork height if G_TEST_DAG_FORK_STATE_KEY is not set --- chain/src/chain.rs | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/chain/src/chain.rs b/chain/src/chain.rs index e4f638ccf4..2255b0bbd3 100644 --- a/chain/src/chain.rs +++ b/chain/src/chain.rs @@ -2360,15 +2360,10 @@ impl BlockChain { if result.is_ok() { Ok(Some(G_TEST_DAG_FORK_HEIGHT)) } else { - let result = self.dag.get_dag_state(self.current_header().id()); - if result.is_ok() { - Ok(Some(G_TEST_DAG_FORK_HEIGHT)) - } else { - Ok(self - .statedb - .get_on_chain_config::()? - .map(|c| c.effective_height)) - } + Ok(self + .statedb + .get_on_chain_config::()? + .map(|c| c.effective_height)) } } else { Ok(self