Skip to content

Commit

Permalink
Merge 0ed3a57 into 561bf86
Browse files Browse the repository at this point in the history
  • Loading branch information
simonjiao authored May 31, 2024
2 parents 561bf86 + 0ed3a57 commit 87967c6
Show file tree
Hide file tree
Showing 14 changed files with 63 additions and 69 deletions.
3 changes: 2 additions & 1 deletion chain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
62 changes: 37 additions & 25 deletions chain/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)?;
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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
)),
}
}
Expand Down Expand Up @@ -2344,22 +2348,22 @@ 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<Option<BlockNumber>> {
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);
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::<FlexiDagConfig>()?
.map(|c| c.effective_height))
}
Ok(self
.statedb
.get_on_chain_config::<FlexiDagConfig>()?
.map(|c| c.effective_height))
}
} else {
Ok(self
Expand All @@ -2368,6 +2372,14 @@ impl BlockChain {
.map(|c| c.effective_height))
}
}

#[cfg(not(feature = "sync-dag-test"))]
pub fn dag_fork_height(&self) -> Result<Option<BlockNumber>> {
Ok(self
.statedb
.get_on_chain_config::<FlexiDagConfig>()?
.map(|c| c.effective_height))
}
}

impl ChainWriter for BlockChain {
Expand Down
15 changes: 8 additions & 7 deletions chain/tests/test_block_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
5 changes: 1 addition & 4 deletions cmd/replay/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ tempfile = { workspace = true }
thiserror = { workspace = true }
toml = { features = ["default"], workspace = true }

[features]
default = []

[package]
authors = { workspace = true }
edition = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion config/example/proxima/genesis_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -527,5 +527,5 @@
},
"time_service_type": "RealTimeService",
"transaction_timeout": 86400,
"dag_effective_height": 1
"dag_effective_height": 0
}
8 changes: 3 additions & 5 deletions config/src/genesis_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,7 @@ pub static G_DEV_CONFIG: Lazy<GenesisConfig> = Lazy::new(|| {
min_action_delay: 60 * 60 * 1000, // 1h
},
transaction_timeout: ONE_DAY,
dag_effective_height: u64::MAX,
dag_effective_height: 0u64,
}
});

Expand Down Expand Up @@ -881,8 +881,7 @@ pub static G_HALLEY_CONFIG: Lazy<GenesisConfig> = 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,
}
});

Expand Down Expand Up @@ -941,8 +940,7 @@ pub static G_PROXIMA_CONFIG: Lazy<GenesisConfig> = 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,
}
});

Expand Down
9 changes: 0 additions & 9 deletions config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down
4 changes: 2 additions & 2 deletions executor/tests/executor_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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]
Expand Down
Binary file modified genesis/generated/halley/genesis
Binary file not shown.
Binary file modified genesis/generated/proxima/genesis
Binary file not shown.
5 changes: 4 additions & 1 deletion genesis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,13 +279,16 @@ impl Genesis {
dag: BlockDAG,
) -> Result<ChainInfo> {
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
Expand Down
3 changes: 2 additions & 1 deletion sync/src/tasks/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
13 changes: 0 additions & 13 deletions testsuite/features/cmd.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 87967c6

Please sign in to comment.