Skip to content

Commit

Permalink
Dag sync test case use 0 as fork number (starcoinorg#4130)
Browse files Browse the repository at this point in the history
* use 0 as fork number

* fix some test case that uses 0 as fork number

* check the blues returned by the ghostdata whether it is empty

* use genesis config to get the dag fork number

* fix clippy

* basic sync dag test case pass

* fix all sync test case running in the dag using the genesis config

* 1, use match to determine the way of the execution and verification
2, return test message in apply failed function

* fix test_flexidag_config_get_for_halley

* add dag test genesis

* add is dag test in test_example_config_compact

* remove the used files and codes

* fix test_custom_genesis

* add is dag test beside the is test

* fix test_generated_schema_are_up_to_date_in_git

* fix fmt

* add dag test in node.json

* add check genesis identical between two nodes in test_sync_dag_blocks

* return error if no blue uncle found, no expect no panic

* add test case test_dag_uncles

* fix genesis generating problem

* renew halley genesis

* renew halley genesis

* renew halley genesis

* remove the dag init in dag block execution process

* 1, change dag type to chain type
2, remove dag genesisi enum in chain type

* use 0 as fork number

* fix some test case that uses 0 as fork number

* check the blues returned by the ghostdata whether it is empty

* use genesis config to get the dag fork number

* fix clippy

* basic sync dag test case pass

* fix all sync test case running in the dag using the genesis config

* 1, use match to determine the way of the execution and verification
2, return test message in apply failed function

* fix test_flexidag_config_get_for_halley

* add dag test genesis

* add is dag test in test_example_config_compact

* remove the used files and codes

* fix test_custom_genesis

* add is dag test beside the is test

* fix test_generated_schema_are_up_to_date_in_git

* fix fmt

* add dag test in node.json

* add check genesis identical between two nodes in test_sync_dag_blocks

* return error if no blue uncle found, no expect no panic

* add test case test_dag_uncles

* fix genesis generating problem

* renew halley genesis

* renew halley genesis

* renew halley genesis

* remove the dag init in dag block execution process

* 1, change dag type to chain type
2, remove dag genesisi enum in chain type

* Fix clippy

* rebase dag master

* add new dag block subscribe

---------

Co-authored-by: 0xa <[email protected]>
  • Loading branch information
jackzhhuang and sanlee42 committed Jun 19, 2024
1 parent c13e581 commit 75f5b03
Show file tree
Hide file tree
Showing 47 changed files with 472 additions and 602 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

11 changes: 4 additions & 7 deletions chain/api/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ use starcoin_types::block::BlockIdAndNumber;
use starcoin_types::startup_info::{ChainInfo, ChainStatus};
use starcoin_types::transaction::RichTransactionInfo;
use starcoin_types::{
block::{Block, BlockHeader, BlockInfo, BlockNumber, DagHeaderType},
block::{Block, BlockHeader, BlockInfo, BlockNumber},
transaction::Transaction,
U256,
};
use starcoin_vm_types::on_chain_resource::Epoch;
use std::collections::HashMap;

use crate::TransactionInfoWithProof;
use crate::{ChainType, TransactionInfoWithProof};
pub use starcoin_types::block::ExecutedBlock;
use starcoin_vm_types::access_path::AccessPath;
use starcoin_vm_types::contract_event::ContractEvent;
Expand Down Expand Up @@ -102,12 +102,9 @@ pub trait ChainReader {
access_path: Option<AccessPath>,
) -> Result<Option<TransactionInfoWithProof>>;

fn current_tips_hash(
&self,
header: &BlockHeader,
) -> Result<Option<(HashValue, Vec<HashValue>)>>;
fn current_tips_hash(&self) -> Result<Option<(HashValue, Vec<HashValue>)>>;
fn has_dag_block(&self, header_id: HashValue) -> Result<bool>;
fn check_dag_type(&self, header: &BlockHeader) -> Result<DagHeaderType>;
fn check_chain_type(&self) -> Result<ChainType>;
}

pub trait ChainWriter {
Expand Down
6 changes: 6 additions & 0 deletions chain/api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ use starcoin_crypto::HashValue;
use starcoin_vm_types::access_path::AccessPath;
use starcoin_vm_types::contract_event::ContractEvent;

#[derive(PartialEq, Eq, Debug)]
pub enum ChainType {
Single, // Single chain
Dag, // Dag chain
}

#[derive(Debug, Eq, PartialEq, Clone, Serialize, Deserialize)]
pub struct EventWithProof {
pub event: ContractEvent,
Expand Down
9 changes: 3 additions & 6 deletions chain/api/src/message.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
// Copyright (c) The Starcoin Core Contributors
// SPDX-License-Identifier: Apache-2

use crate::TransactionInfoWithProof;
use crate::{ChainType, TransactionInfoWithProof};
use anyhow::Result;
use starcoin_crypto::HashValue;
use starcoin_dag::consensusdb::consenses_state::DagStateView;
use starcoin_dag::types::ghostdata::GhostdagData;
use starcoin_service_registry::ServiceRequest;
use starcoin_types::block::DagHeaderType;
use starcoin_types::transaction::RichTransactionInfo;
use starcoin_types::{
block::{Block, BlockHeader, BlockInfo, BlockNumber},
Expand Down Expand Up @@ -67,8 +66,7 @@ pub enum ChainRequest {
block_ids: Vec<HashValue>,
},
GetDagStateView,
CheckDagType(HashValue),
DagForkHeigh,
CheckChainType,
GetGhostdagData(HashValue),
}

Expand Down Expand Up @@ -99,7 +97,6 @@ pub enum ChainResponse {
TransactionProof(Box<Option<TransactionInfoWithProof>>),
BlockInfoVec(Box<Vec<Option<BlockInfo>>>),
DagStateView(Box<DagStateView>),
CheckDagType(DagHeaderType),
DagForkHeight(Option<BlockNumber>),
CheckChainType(ChainType),
GhostdagDataOption(Box<Option<GhostdagData>>),
}
25 changes: 7 additions & 18 deletions chain/api/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@
// SPDX-License-Identifier: Apache-2

use crate::message::{ChainRequest, ChainResponse};
use crate::TransactionInfoWithProof;
use crate::{ChainType, TransactionInfoWithProof};
use anyhow::{bail, Result};
use starcoin_crypto::HashValue;
use starcoin_dag::consensusdb::consenses_state::DagStateView;
use starcoin_dag::types::ghostdata::GhostdagData;
use starcoin_service_registry::{ActorService, ServiceHandler, ServiceRef};
use starcoin_types::block::DagHeaderType;
use starcoin_types::contract_event::{ContractEvent, ContractEventInfo};
use starcoin_types::filter::Filter;
use starcoin_types::startup_info::ChainStatus;
Expand Down Expand Up @@ -77,8 +76,7 @@ pub trait ReadableChainService {
fn get_block_infos(&self, ids: Vec<HashValue>) -> Result<Vec<Option<BlockInfo>>>;
fn get_dag_block_children(&self, ids: Vec<HashValue>) -> Result<Vec<HashValue>>;
fn get_dag_state(&self) -> Result<DagStateView>;
fn check_dag_type(&self, header: &BlockHeader) -> Result<DagHeaderType>;
fn dag_fork_height(&self) -> Result<Option<BlockNumber>>;
fn check_chain_type(&self) -> Result<ChainType>;
fn get_ghostdagdata(&self, id: HashValue) -> Result<Option<GhostdagData>>;
}

Expand Down Expand Up @@ -149,8 +147,7 @@ pub trait ChainAsyncService:
async fn get_block_infos(&self, hashes: Vec<HashValue>) -> Result<Vec<Option<BlockInfo>>>;
async fn get_dag_block_children(&self, hashes: Vec<HashValue>) -> Result<Vec<HashValue>>;
async fn get_dag_state(&self) -> Result<DagStateView>;
async fn check_dag_type(&self, id: HashValue) -> Result<DagHeaderType>;
async fn dag_fork_height(&self) -> Result<Option<BlockNumber>>;
async fn check_chain_type(&self) -> Result<ChainType>;
async fn get_ghostdagdata(&self, id: HashValue) -> Result<Option<GhostdagData>>;
}

Expand Down Expand Up @@ -470,20 +467,12 @@ where
}
}

async fn check_dag_type(&self, id: HashValue) -> Result<DagHeaderType> {
let response = self.send(ChainRequest::CheckDagType(id)).await??;
if let ChainResponse::CheckDagType(dag_type) = response {
async fn check_chain_type(&self) -> Result<ChainType> {
let response = self.send(ChainRequest::CheckChainType).await??;
if let ChainResponse::CheckChainType(dag_type) = response {
Ok(dag_type)
} else {
bail!("check dag type error")
}
}
async fn dag_fork_height(&self) -> Result<Option<BlockNumber>> {
let response = self.send(ChainRequest::DagForkHeigh).await??;
if let ChainResponse::DagForkHeight(dag_fork_height) = response {
Ok(dag_fork_height)
} else {
bail!("failed to get dag fork height")
bail!("check chain type error")
}
}

Expand Down
34 changes: 9 additions & 25 deletions chain/service/src/chain_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use anyhow::{bail, format_err, Error, Result};
use starcoin_chain::BlockChain;
use starcoin_chain_api::message::{ChainRequest, ChainResponse};
use starcoin_chain_api::{
ChainReader, ChainWriter, ReadableChainService, TransactionInfoWithProof,
ChainReader, ChainType, ChainWriter, ReadableChainService, TransactionInfoWithProof,
};
use starcoin_config::NodeConfig;
use starcoin_crypto::HashValue;
Expand All @@ -17,7 +17,7 @@ use starcoin_service_registry::{
ActorService, EventHandler, ServiceContext, ServiceFactory, ServiceHandler,
};
use starcoin_storage::{BlockStore, Storage, Store};
use starcoin_types::block::{DagHeaderType, ExecutedBlock};
use starcoin_types::block::ExecutedBlock;
use starcoin_types::contract_event::ContractEventInfo;
use starcoin_types::filter::Filter;
use starcoin_types::system_events::NewHeadBlock;
Expand Down Expand Up @@ -248,16 +248,9 @@ impl ServiceHandler<Self, ChainRequest> for ChainReaderService {
ChainRequest::GetDagStateView => Ok(ChainResponse::DagStateView(Box::new(
self.inner.get_dag_state()?,
))),
ChainRequest::CheckDagType(id) => Ok(ChainResponse::CheckDagType({
let header = self
.inner
.get_header_by_hash(id)?
.ok_or_else(|| format_err!("non-existent block header"))?;
self.inner.check_dag_type(&header)?
ChainRequest::CheckChainType => Ok(ChainResponse::CheckChainType({
self.inner.check_chain_type()?
})),
ChainRequest::DagForkHeigh => {
Ok(ChainResponse::DagForkHeight(self.inner.dag_fork_height()?))
}
ChainRequest::GetGhostdagData(id) => Ok(ChainResponse::GhostdagDataOption(Box::new(
self.inner.get_ghostdagdata(id)?,
))),
Expand Down Expand Up @@ -462,27 +455,18 @@ impl ReadableChainService for ChainReaderServiceInner {
}

fn get_dag_state(&self) -> Result<DagStateView> {
let head = self.main.current_header();
if self.main.check_dag_type(&head)? != DagHeaderType::Normal {
bail!(
"The chain is still not a dag and its dag fork number is {:?} and the current block's header number is {:?}.",
self.main.dag_fork_height()?,
head.number(),
);
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(&head)?;
let (dag_genesis, state) = self.main.get_dag_state_by_block()?;
Ok(DagStateView {
dag_genesis,
tips: state.tips,
})
}

fn check_dag_type(&self, header: &BlockHeader) -> Result<DagHeaderType> {
self.main.check_dag_type(header)
}

fn dag_fork_height(&self) -> Result<Option<BlockNumber>> {
self.main.dag_fork_height()
fn check_chain_type(&self) -> Result<ChainType> {
self.main.check_chain_type()
}
fn get_ghostdagdata(&self, id: HashValue) -> Result<Option<GhostdagData>> {
self.dag
Expand Down
Loading

0 comments on commit 75f5b03

Please sign in to comment.