Skip to content

Commit

Permalink
add sync for dag accumulator (#3986)
Browse files Browse the repository at this point in the history
  • Loading branch information
jackzhhuang authored Nov 10, 2023
1 parent 8ac3339 commit 5a44f52
Show file tree
Hide file tree
Showing 33 changed files with 760 additions and 610 deletions.
6 changes: 1 addition & 5 deletions block-relayer/src/block_relayer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,11 +241,7 @@ impl BlockRelayer {
)
.await?;

block_connector_service.notify(PeerNewBlock::new(
peer_id,
block,
compact_block_msg.message.tips_hash,
))?;
block_connector_service.notify(PeerNewBlock::new(peer_id, block))?;
}
Ok(())
};
Expand Down
5 changes: 1 addition & 4 deletions chain/api/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,7 @@ pub trait ChainWriter {
fn connect(&mut self, executed_block: ExecutedBlock) -> Result<ExecutedBlock>;

/// Verify, Execute and Connect block to current chain.
fn apply(
&mut self,
block: Block,
) -> Result<ExecutedBlock>;
fn apply(&mut self, block: Block) -> Result<ExecutedBlock>;

fn chain_state(&mut self) -> &ChainStateDB;
}
Expand Down
83 changes: 49 additions & 34 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, Ok};
use anyhow::{bail, format_err, Error, Ok, Result};
use starcoin_accumulator::node::AccumulatorStoreType;
use starcoin_accumulator::{Accumulator, MerkleAccumulator};
use starcoin_chain::BlockChain;
Expand All @@ -12,8 +12,10 @@ use starcoin_chain_api::{
use starcoin_config::NodeConfig;
use starcoin_consensus::BlockDAG;
use starcoin_crypto::HashValue;
use starcoin_flexidag::{FlexidagService, flexidag_service};
use starcoin_flexidag::flexidag_service::{GetDagAccumulatorLeafDetail, UpdateDagTips, GetDagBlockParents};
use starcoin_flexidag::flexidag_service::{
GetDagAccumulatorLeafDetail, GetDagBlockParents, UpdateDagTips,
};
use starcoin_flexidag::{flexidag_service, FlexidagService};
use starcoin_logger::prelude::*;
use starcoin_network_rpc_api::dag_protocol::{
GetDagAccumulatorLeaves, GetTargetDagAccumulatorLeafDetail, TargetDagAccumulatorLeaf,
Expand All @@ -25,6 +27,7 @@ use starcoin_service_registry::{
use starcoin_storage::{BlockStore, Storage, Store};
use starcoin_types::block::ExecutedBlock;
use starcoin_types::contract_event::ContractEventInfo;
use starcoin_types::dag_block::KTotalDifficulty;
use starcoin_types::filter::Filter;
use starcoin_types::system_events::NewHeadBlock;
use starcoin_types::transaction::RichTransactionInfo;
Expand Down Expand Up @@ -93,8 +96,7 @@ impl EventHandler<Self, NewHeadBlock> for ChainReaderService {
let new_head = event.0.block().header().clone();
if let Err(e) = if self.inner.get_main().can_connect(event.0.as_ref()) {
match self.inner.update_chain_head(event.0.as_ref().clone()) {
// wait for fixing: update_dag_accumulator should be in BlockChain
std::result::Result::Ok(_) => self.inner.update_dag_accumulator(new_head),
std::result::Result::Ok(_) => (),
Err(e) => Err(e),
}
} else {
Expand Down Expand Up @@ -266,12 +268,14 @@ impl ServiceHandler<Self, ChainRequest> for ChainReaderService {
ChainRequest::GetTargetDagAccumulatorLeafDetail {
leaf_index,
batch_size,
} => {
Ok(ChainResponse::TargetDagAccumulatorLeafDetail(self.inner.get_target_dag_accumulator_leaf_detail(GetTargetDagAccumulatorLeafDetail {
leaf_index,
batch_size,
})?))
},
} => Ok(ChainResponse::TargetDagAccumulatorLeafDetail(
self.inner.get_target_dag_accumulator_leaf_detail(
GetTargetDagAccumulatorLeafDetail {
leaf_index,
batch_size,
},
)?,
)),
}
}
}
Expand Down Expand Up @@ -335,6 +339,11 @@ impl ChainReaderServiceInner {
pub fn update_dag_accumulator(&mut self, new_block_header: BlockHeader) -> Result<()> {
async_std::task::block_on(self.flexidag_service.send(UpdateDagTips {
block_header: new_block_header,
current_head_block_id: self.main.status().info().id(),
k_total_difficulty: KTotalDifficulty {
head_block_id: self.main.status().info().id(),
total_difficulty: self.main.status().info().get_total_difficulty(),
},
}))?
}
}
Expand All @@ -357,11 +366,12 @@ impl ReadableChainService for ChainReaderServiceInner {
.into_iter()
.map(|block| {
if let Some(block) = block {
let result_parents = async_std::task::block_on(self.flexidag_service.send(GetDagBlockParents {
block_id: block.id(),
})).expect("failed to get the dag block parents");
let parents = match result_parents
{
let result_parents =
async_std::task::block_on(self.flexidag_service.send(GetDagBlockParents {
block_id: block.id(),
}))
.expect("failed to get the dag block parents");
let parents = match result_parents {
std::result::Result::Ok(parents) => parents.parents,
Err(_) => panic!("failed to get parents of block {}", block.id()),
};
Expand Down Expand Up @@ -510,32 +520,37 @@ impl ReadableChainService for ChainReaderServiceInner {
&self,
req: GetDagAccumulatorLeaves,
) -> anyhow::Result<Vec<TargetDagAccumulatorLeaf>> {
Ok(async_std::task::block_on(self.flexidag_service.send(flexidag_service::GetDagAccumulatorLeaves {
leaf_index: req.accumulator_leaf_index,
batch_size: req.batch_size,
reverse: true,
}))??.into_iter().map(|leaf| {
TargetDagAccumulatorLeaf {
accumulator_root: leaf.dag_accumulator_root,
leaf_index: leaf.leaf_index,
}
}).collect())
Ok(async_std::task::block_on(self.flexidag_service.send(
flexidag_service::GetDagAccumulatorLeaves {
leaf_index: req.accumulator_leaf_index,
batch_size: req.batch_size,
reverse: true,
},
))??
.into_iter()
.map(|leaf| TargetDagAccumulatorLeaf {
accumulator_root: leaf.dag_accumulator_root,
leaf_index: leaf.leaf_index,
})
.collect())
}

fn get_target_dag_accumulator_leaf_detail(
&self,
req: GetTargetDagAccumulatorLeafDetail,
) -> anyhow::Result<Vec<TargetDagAccumulatorLeafDetail>> {
let dag_details = async_std::task::block_on(self.flexidag_service.send(GetDagAccumulatorLeafDetail {
leaf_index: req.leaf_index,
batch_size: req.batch_size,
}))??;
Ok(dag_details.into_iter().map(|detail| {
TargetDagAccumulatorLeafDetail {
let dag_details =
async_std::task::block_on(self.flexidag_service.send(GetDagAccumulatorLeafDetail {
leaf_index: req.leaf_index,
batch_size: req.batch_size,
}))??;
Ok(dag_details
.into_iter()
.map(|detail| TargetDagAccumulatorLeafDetail {
accumulator_root: detail.accumulator_root,
tips: detail.tips,
}
}).collect())
})
.collect())
}
}

Expand Down
54 changes: 20 additions & 34 deletions chain/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use starcoin_storage::Store;
use starcoin_time_service::TimeService;
use starcoin_types::block::BlockIdAndNumber;
use starcoin_types::contract_event::ContractEventInfo;
use starcoin_types::dag_block::KTotalDifficulty;
use starcoin_types::filter::Filter;
use starcoin_types::header::DagHeader;
use starcoin_types::startup_info::{ChainInfo, ChainStatus};
Expand All @@ -47,6 +48,7 @@ use starcoin_vm_types::effects::Op;
use starcoin_vm_types::genesis_config::ConsensusStrategy;
use starcoin_vm_types::on_chain_resource::Epoch;
use std::cmp::min;
use std::collections::BTreeSet;
use std::iter::Extend;
use std::option::Option::{None, Some};
use std::{collections::HashMap, sync::Arc};
Expand Down Expand Up @@ -192,6 +194,13 @@ impl BlockChain {
.expect("failed to calculate the dag key"),
new_tips,
dag_accumulator.get_info(),
genesis_id,
[KTotalDifficulty {
head_block_id: genesis_id,
total_difficulty: executed_block.block_info().get_total_difficulty(),
}]
.into_iter()
.collect(),
)?;
Self::new(time_service, executed_block.block.id(), storage, net, None)
}
Expand Down Expand Up @@ -378,10 +387,7 @@ impl BlockChain {
V::verify_block(self, block)
}

pub fn apply_with_verifier<V>(
&mut self,
block: Block,
) -> Result<ExecutedBlock>
pub fn apply_with_verifier<V>(&mut self, block: Block) -> Result<ExecutedBlock>
where
V: BlockVerifier,
{
Expand All @@ -393,18 +399,12 @@ impl BlockChain {
}

//TODO remove this function.
pub fn update_chain_head(
&mut self,
block: Block,
) -> Result<ExecutedBlock> {
pub fn update_chain_head(&mut self, block: Block) -> Result<ExecutedBlock> {
let block_info = self
.storage
.get_block_info(block.id())?
.ok_or_else(|| format_err!("Can not find block info by hash {:?}", block.id()))?;
self.connect(ExecutedBlock {
block,
block_info,
})
self.connect(ExecutedBlock { block, block_info })
}

//TODO consider move this logic to BlockExecutor
Expand Down Expand Up @@ -572,10 +572,7 @@ impl BlockChain {
storage.save_table_infos(txn_table_infos)?;

watch(CHAIN_WATCH_NAME, "n26");
Ok(ExecutedBlock {
block,
block_info,
})
Ok(ExecutedBlock { block, block_info })
}

pub fn get_txn_accumulator(&self) -> &MerkleAccumulator {
Expand All @@ -593,12 +590,10 @@ impl ChainReader for BlockChain {
self.status.head.header().chain_id(),
self.genesis_hash,
self.status.status.clone(),
self.storage
.get_dag_accumulator_info()
.expect(&format!(
"the dag accumulator info cannot be found by id: {}",
self.status.head.header().id()
)),
self.storage.get_dag_accumulator_info().expect(&format!(
"the dag accumulator info cannot be found by id: {}",
self.status.head.header().id()
)),
)
}

Expand All @@ -607,10 +602,7 @@ impl ChainReader for BlockChain {
}

fn head_block(&self) -> ExecutedBlock {
ExecutedBlock::new(
self.status.head.clone(),
self.status.status.info.clone(),
)
ExecutedBlock::new(self.status.head.clone(), self.status.status.info.clone())
}

fn current_header(&self) -> BlockHeader {
Expand Down Expand Up @@ -1082,10 +1074,7 @@ impl ChainWriter for BlockChain {

self.statedb = ChainStateDB::new(self.storage.clone().into_super_arc(), Some(state_root));
self.status = ChainStatusWithBlock {
status: ChainStatus::new(
block.header().clone(),
block_info.clone(),
),
status: ChainStatus::new(block.header().clone(), block_info.clone()),
head: block.clone(),
};
if self.epoch.end_block_number() == block.header().number() {
Expand All @@ -1100,10 +1089,7 @@ impl ChainWriter for BlockChain {
Ok(executed_block)
}

fn apply(
&mut self,
block: Block,
) -> Result<ExecutedBlock> {
fn apply(&mut self, block: Block) -> Result<ExecutedBlock> {
self.apply_with_verifier::<FullVerifier>(block)
}

Expand Down
82 changes: 41 additions & 41 deletions chain/src/verifier/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,49 +184,49 @@ impl BlockVerifier for BasicVerifier {
// dag
// jacktest: TODO: the verifying should be modified!!!
// if chain_status.tips_hash.is_some() {
// let mut tips_hash = chain_status.tips_hash.clone().unwrap();
// tips_hash.sort();

// if it is a dag block
// if HashValue::sha3_256_of(&tips_hash.encode().expect("hash encode must be successful"))
// != new_block_parent
// {
// // or a block of a single chain
// verify_block!(
// VerifyBlockField::Header,
// expect_number == new_block_header.number(),
// "Invalid block: Unexpect block number, expect:{}, got: {}.",
// expect_number,
// new_block_header.number()
// );

// verify_block!(
// VerifyBlockField::Header,
// current_id == new_block_parent,
// "Invalid block: Parent id mismatch, expect:{}, got: {}, number:{}.",
// current_id,
// new_block_parent,
// new_block_header.number()
// );
// }
// let mut tips_hash = chain_status.tips_hash.clone().unwrap();
// tips_hash.sort();

// if it is a dag block
// if HashValue::sha3_256_of(&tips_hash.encode().expect("hash encode must be successful"))
// != new_block_parent
// {
// // or a block of a single chain
// verify_block!(
// VerifyBlockField::Header,
// expect_number == new_block_header.number(),
// "Invalid block: Unexpect block number, expect:{}, got: {}.",
// expect_number,
// new_block_header.number()
// );

// verify_block!(
// VerifyBlockField::Header,
// current_id == new_block_parent,
// "Invalid block: Parent id mismatch, expect:{}, got: {}, number:{}.",
// current_id,
// new_block_parent,
// new_block_header.number()
// );
// }
// } else {
// or a block of a single chain
verify_block!(
VerifyBlockField::Header,
expect_number == new_block_header.number(),
"Invalid block: Unexpect block number, expect:{}, got: {}.",
expect_number,
new_block_header.number()
);
// or a block of a single chain
verify_block!(
VerifyBlockField::Header,
expect_number == new_block_header.number(),
"Invalid block: Unexpect block number, expect:{}, got: {}.",
expect_number,
new_block_header.number()
);

verify_block!(
VerifyBlockField::Header,
current_id == new_block_parent,
"Invalid block: Parent id mismatch, expect:{}, got: {}, number:{}.",
current_id,
new_block_parent,
new_block_header.number()
);
verify_block!(
VerifyBlockField::Header,
current_id == new_block_parent,
"Invalid block: Parent id mismatch, expect:{}, got: {}, number:{}.",
current_id,
new_block_parent,
new_block_header.number()
);
// }
verify_block!(
VerifyBlockField::Header,
Expand Down
Loading

0 comments on commit 5a44f52

Please sign in to comment.