Skip to content

Commit

Permalink
Update the block header to ensure compatibility with future updates (#…
Browse files Browse the repository at this point in the history
…4175)

* add sync process compare log

* add main value

* enlarge the number of buffer size

* sync the block as a single chain

* check the selected parent only

* not a future block is its selected parent executed but other parents

* ensure the selected parent only

* recover the single chain code to pass the test code

* add pruning logic and compatible logic
fix some test cases

* fix clippy

* fix test case

* remove some single chain test case

* fix clippy

* fix flexdag's test case

* add proxima update number

* fix clippy and remove the testing logs

* fix clippy

* fix test case test_handshake_message

* remove the unused code and fix test cases

* remove the used testing code

* add rational random to pass the deserialization verification

* generated new schema

* merge dag-master

* fix storage test case

* comment the helly test case which contains a request to the online halley server which needs to be updated in the lastest release

* fix bugs: blockmeta changes into latest version

* add update db code

* comment the structs that cannot be checked by json and should be tested in other testcase

* add dag db update

* update dag db

* update dag db

* add version in genesis config

* add new config

* rm genesis config to update the config

* fix clippy and fmt

* skip the parents checking for genesis

* vega 3200000 will be version 1
halley 3000000 will be version 1

* vega 3300000 to version 1
halley 3100000 to version 1
remove the singel chain sync

* pull all parents block
  • Loading branch information
jackzhhuang authored Aug 27, 2024
1 parent 565aa3b commit ee335e6
Show file tree
Hide file tree
Showing 85 changed files with 2,622 additions and 2,482 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion benchmarks/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use starcoin_chain::BlockChain;
use starcoin_chain::{ChainReader, ChainWriter};
use starcoin_config::{temp_dir, ChainNetwork, DataDirPath, RocksdbConfig};
use starcoin_consensus::Consensus;
use starcoin_crypto::HashValue;
use starcoin_genesis::Genesis;
use starcoin_storage::cache_storage::CacheStorage;
use starcoin_storage::db_storage::DBStorage;
Expand Down Expand Up @@ -73,7 +74,15 @@ impl ChainBencher {
let (block_template, _) = self
.chain
.read()
.create_block_template(*self.account.address(), None, vec![], vec![], None, None)
.create_block_template(
*self.account.address(),
None,
vec![],
vec![],
None,
vec![],
HashValue::zero(),
)
.unwrap();
let block = ConsensusStrategy::Dummy
.create_block(block_template, self.net.time_service().as_ref())
Expand Down
2 changes: 1 addition & 1 deletion chain/api/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ pub trait ChainReader {
access_path: Option<AccessPath>,
) -> Result<Option<TransactionInfoWithProof>>;

fn current_tips_hash(&self) -> Result<Option<(HashValue, Vec<HashValue>)>>;
fn current_tips_hash(&self) -> Result<Vec<HashValue>>;
fn has_dag_block(&self, header_id: HashValue) -> Result<bool>;
fn check_chain_type(&self) -> Result<ChainType>;
}
Expand Down
39 changes: 35 additions & 4 deletions chain/mock/src/mock_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ impl MockChain {
Some(id) => id,
None => self.head.current_header().id(),
};
assert!(self.head.exist_block(block_id)?);
assert!(self.head.has_dag_block(block_id)?);
BlockChain::new(
self.head.time_service(),
block_id,
Expand Down Expand Up @@ -155,7 +155,8 @@ impl MockChain {
vec![],
vec![],
None,
None,
vec![],
HashValue::zero(),
)?;
self.head
.consensus()
Expand All @@ -169,7 +170,8 @@ impl MockChain {
vec![],
vec![],
None,
None,
vec![],
HashValue::zero(),
)?;
self.head
.consensus()
Expand All @@ -187,7 +189,8 @@ impl MockChain {
vec![],
vec![],
None,
Some(tips),
tips,
HashValue::zero(),
)?;
self.head
.consensus()
Expand All @@ -206,13 +209,41 @@ impl MockChain {
Ok(header)
}

pub fn produce_and_apply_by_tips(
&mut self,
parent_header: BlockHeader,
tips: Vec<HashValue>,
) -> Result<BlockHeader> {
let block = self.produce_block_by_tips(parent_header, tips)?;
let header = block.header().clone();
self.apply(block)?;
Ok(header)
}

pub fn produce_and_apply_times(&mut self, times: u64) -> Result<()> {
for _i in 0..times {
self.produce_and_apply()?;
}
Ok(())
}

pub fn produce_and_apply_times_for_fork(
&mut self,
fork_point: BlockHeader,
times: u64,
) -> Result<BlockHeader> {
let mut parent_header = fork_point;
let mut tips = vec![parent_header.id()];
let mut last = parent_header.clone();
for _i in 0..times {
let block_header = self.produce_and_apply_by_tips(parent_header, tips)?;
parent_header = block_header.clone();
tips = vec![block_header.id()];
last = block_header.clone();
}
Ok(last)
}

pub fn produce_fork_chain(&mut self, one_count: u64, two_count: u64) -> Result<()> {
let start_header = self.head.current_header();

Expand Down
20 changes: 14 additions & 6 deletions chain/open-block/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use starcoin_logger::prelude::*;
use starcoin_state_api::{ChainStateReader, ChainStateWriter};
use starcoin_statedb::ChainStateDB;
use starcoin_storage::Store;
use starcoin_types::block::{Block, BlockNumber};
use starcoin_types::block::{Block, BlockNumber, Version};
use starcoin_types::genesis_config::{ChainId, ConsensusStrategy};
use starcoin_types::vm_error::KeptVMStatus;
use starcoin_types::{
Expand Down Expand Up @@ -58,7 +58,9 @@ pub struct OpenedBlock {
difficulty: U256,
strategy: ConsensusStrategy,
vm_metrics: Option<VMMetrics>,
blue_blocks: Option<Vec<Block>>,
blue_blocks: Vec<Block>,
version: Version,
pruning_point: HashValue,
}

impl OpenedBlock {
Expand All @@ -72,8 +74,10 @@ impl OpenedBlock {
difficulty: U256,
strategy: ConsensusStrategy,
vm_metrics: Option<VMMetrics>,
tips_hash: Option<Vec<HashValue>>,
blue_blocks: Option<Vec<Block>>,
tips_hash: Vec<HashValue>,
blue_blocks: Vec<Block>,
version: Version,
pruning_point: HashValue,
) -> Result<Self> {
let previous_block_id = previous_header.id();
let block_info = storage
Expand All @@ -97,7 +101,7 @@ impl OpenedBlock {
previous_header.number() + 1,
chain_id,
previous_header.gas_used(),
tips_hash.unwrap_or_default(),
tips_hash,
);
let mut opened_block = Self {
previous_block_info: block_info,
Expand All @@ -113,6 +117,8 @@ impl OpenedBlock {
strategy,
vm_metrics,
blue_blocks,
version,
pruning_point,
};

opened_block.initialize()?;
Expand Down Expand Up @@ -165,7 +171,7 @@ impl OpenedBlock {
/// TODO: make the function can be called again even last call returns error.
pub fn push_txns(&mut self, user_txns: Vec<SignedUserTransaction>) -> Result<ExcludedTxns> {
let mut txns = vec![];
for block in self.blue_blocks.as_ref().unwrap_or(&vec![]) {
for block in &self.blue_blocks {
txns.extend(
block
.transactions()
Expand Down Expand Up @@ -362,6 +368,8 @@ impl OpenedBlock {
self.difficulty,
self.strategy,
self.block_meta,
self.version,
self.pruning_point,
);
Ok(block_template)
}
Expand Down
10 changes: 4 additions & 6 deletions chain/service/src/chain_service.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) The Starcoin Core Contributors
// SPDX-License-Identifier: Apache-2.0

use anyhow::{bail, format_err, Error, Result};
use anyhow::{format_err, Error, Result};
use starcoin_chain::BlockChain;
use starcoin_chain_api::message::{ChainRequest, ChainResponse};
use starcoin_chain_api::{
Expand Down Expand Up @@ -455,13 +455,11 @@ impl ReadableChainService for ChainReaderServiceInner {
}

fn get_dag_state(&self) -> Result<DagStateView> {
if self.main.check_chain_type()? != ChainType::Dag {
bail!("The dag block is not built yet.");
}
let (dag_genesis, state) = self.main.get_dag_state_by_block()?;
let state = self.main.get_dag_state()?;
let pruning_point = self.main.status().head().pruning_point();
Ok(DagStateView {
dag_genesis,
tips: state.tips,
pruning_point,
})
}

Expand Down
1,012 changes: 85 additions & 927 deletions chain/src/chain.rs

Large diffs are not rendered by default.

37 changes: 10 additions & 27 deletions chain/src/verifier/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use anyhow::{format_err, Result};
use sp_utils::stop_watch::{watch, CHAIN_WATCH_NAME};
use starcoin_chain_api::{
verify_block, ChainReader, ChainType, ConnectBlockError, VerifiedBlock, VerifyBlockField,
verify_block, ChainReader, ConnectBlockError, VerifiedBlock, VerifyBlockField,
};
use starcoin_consensus::{Consensus, ConsensusVerifyError};
use starcoin_logger::prelude::debug;
Expand Down Expand Up @@ -265,17 +265,6 @@ impl BlockVerifier for BasicVerifier {
new_block_header.block_accumulator_root(),
);

verify_block!(
VerifyBlockField::Header,
current_chain.check_chain_type()? == ChainType::Single
&& new_block_header
.parents_hash()
.unwrap_or_default()
.is_empty(),
"Single chain block is invalid: number {} parents_hash len {:?}",
new_block_header.number(),
new_block_header.parents_hash().map(|p| p.len())
);
Ok(())
}
}
Expand Down Expand Up @@ -351,30 +340,24 @@ impl BlockVerifier for DagVerifier {
where
R: ChainReader,
{
let parents_hash = new_block_header.parents_hash().unwrap_or_default();
let mut parents_hash_to_check = parents_hash.clone();
parents_hash_to_check.sort();
parents_hash_to_check.dedup();
let parents_hash = new_block_header.parents_hash();

verify_block!(
VerifyBlockField::Header,
!parents_hash_to_check.is_empty() && parents_hash.len() == parents_hash_to_check.len(),
"Invalid parents_hash in dag verifier {:?} for a dag block {}",
new_block_header.parents_hash(),
new_block_header.number(),
parents_hash.len() == parents_hash.iter().collect::<HashSet<_>>().len(),
"The dag block contains repeated hash values, block header: {:?}",
new_block_header,
);

verify_block!(
VerifyBlockField::Header,
parents_hash_to_check.contains(&new_block_header.parent_hash())
&& current_chain
.get_block_info(Some(new_block_header.parent_hash()))?
.is_some(),
"Invalid block: parent {} might not exist.",
parents_hash.contains(&new_block_header.parent_hash()),
"header: {:?}, tips {:?} do not contain the selected parent {:?}",
new_block_header,
parents_hash,
new_block_header.parent_hash()
);

parents_hash_to_check.iter().try_for_each(|parent_hash| {
parents_hash.iter().try_for_each(|parent_hash| {
verify_block!(
VerifyBlockField::Header,
current_chain.has_dag_block(*parent_hash).map_err(|e| {
Expand Down
4 changes: 3 additions & 1 deletion chain/tests/block_test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ fn gen_header(
parent_header.chain_id(),
0,
BlockHeaderExtra::new([0u8; 4]),
None,
vec![],
0,
HashValue::zero(),
)
}

Expand Down
Loading

0 comments on commit ee335e6

Please sign in to comment.