Skip to content

Commit

Permalink
Merge branch 'fg-flexidag' of github.com:starcoinorg/starcoin into fg…
Browse files Browse the repository at this point in the history
…-flexidag
  • Loading branch information
jackzhhuang committed Nov 21, 2023
2 parents 514b0fb + 07dd80c commit fd09130
Show file tree
Hide file tree
Showing 25 changed files with 275 additions and 194 deletions.
1 change: 0 additions & 1 deletion chain/api/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// SPDX-License-Identifier: Apache-2

use anyhow::Result;
use starcoin_accumulator::accumulator_info::AccumulatorInfo;
use starcoin_config::ChainNetworkID;
use starcoin_crypto::HashValue;
use starcoin_state_api::ChainStateReader;
Expand Down
32 changes: 21 additions & 11 deletions chain/service/src/chain_service.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
// Copyright (c) The Starcoin Core Contributors
// SPDX-License-Identifier: Apache-2.0

use anyhow::{bail, format_err, Error, Ok, Result};
use starcoin_accumulator::node::AccumulatorStoreType;
use starcoin_accumulator::{Accumulator, MerkleAccumulator};
use anyhow::{format_err, Error, Ok, Result};
use starcoin_chain::BlockChain;
use starcoin_chain_api::message::{ChainRequest, ChainResponse};
use starcoin_chain_api::{
Expand All @@ -12,10 +10,10 @@ use starcoin_chain_api::{
use starcoin_config::NodeConfig;
use starcoin_consensus::BlockDAG;
use starcoin_crypto::HashValue;
use starcoin_flexidag::flexidag_service::{
GetDagAccumulatorLeafDetail, GetDagBlockParents, UpdateDagTips,
use starcoin_flexidag::{
flexidag_service::{self, GetDagAccumulatorLeafDetail, UpdateDagTips},
FlexidagService,
};
use starcoin_flexidag::{flexidag_service, FlexidagService};
use starcoin_logger::prelude::*;
use starcoin_network_rpc_api::dag_protocol::{
GetDagAccumulatorLeaves, GetTargetDagAccumulatorLeafDetail, TargetDagAccumulatorLeaf,
Expand Down Expand Up @@ -78,7 +76,14 @@ impl ServiceFactory<Self> for ChainReaderService {
let dag = ctx.get_shared_opt::<BlockDAG>()?;
let vm_metrics = ctx.get_shared_opt::<VMMetrics>()?;
let flexidag_service = ctx.service_ref::<FlexidagService>()?.clone();
Self::new(config, startup_info, storage, flexidag_service, dag, vm_metrics)
Self::new(
config,
startup_info,
storage,
flexidag_service,
dag,
vm_metrics,
)
}
}

Expand All @@ -95,10 +100,15 @@ impl ActorService for ChainReaderService {
}

impl EventHandler<Self, NewHeadBlock> for ChainReaderService {
fn handle_event(&mut self, event: NewHeadBlock, ctx: &mut ServiceContext<ChainReaderService>) {
fn handle_event(&mut self, event: NewHeadBlock, _ctx: &mut ServiceContext<ChainReaderService>) {
let new_head = event.executed_block.block().header().clone();
if let Err(e) = if self.inner.get_main().can_connect(&event.executed_block.as_ref()) {
self.inner.update_chain_head(event.executed_block.as_ref().clone())
if let Err(e) = if self
.inner
.get_main()
.can_connect(event.executed_block.as_ref())
{
self.inner
.update_chain_head(event.executed_block.as_ref().clone())
} else {
self.inner.switch_main(new_head.id())
} {
Expand All @@ -111,7 +121,7 @@ impl ServiceHandler<Self, ChainRequest> for ChainReaderService {
fn handle(
&mut self,
msg: ChainRequest,
ctx: &mut ServiceContext<ChainReaderService>,
_ctx: &mut ServiceContext<ChainReaderService>,
) -> Result<ChainResponse> {
match msg {
ChainRequest::CurrentHeader() => Ok(ChainResponse::BlockHeader(Box::new(
Expand Down
67 changes: 41 additions & 26 deletions chain/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

use crate::verifier::{BlockVerifier, FullVerifier};
use anyhow::{anyhow, bail, ensure, format_err, Ok, Result};
use anyhow::{bail, ensure, format_err, Ok, Result};
use bcs_ext::BCSCodec;
use sp_utils::stop_watch::{watch, CHAIN_WATCH_NAME};
use starcoin_accumulator::inmemory::InMemoryAccumulator;
Expand All @@ -22,8 +22,6 @@ use starcoin_logger::prelude::*;
use starcoin_open_block::OpenedBlock;
use starcoin_state_api::{AccountStateReader, ChainStateReader, ChainStateWriter};
use starcoin_statedb::ChainStateDB;
use starcoin_storage::flexi_dag::SyncFlexiDagSnapshot;
use starcoin_storage::storage::CodecKVStore;
use starcoin_storage::Store;
use starcoin_time_service::TimeService;
use starcoin_types::block::BlockIdAndNumber;
Expand Down Expand Up @@ -53,8 +51,8 @@ pub struct ChainStatusWithBlock {
pub status: ChainStatus,
pub head: Block,
}
impl ChainStatusWithBlock{
pub fn dag_tips(&self,)->&Option<Vec<HashValue>>{
impl ChainStatusWithBlock {
pub fn dag_tips(&self) -> &Option<Vec<HashValue>> {
&self.status.tips_hash
}
}
Expand Down Expand Up @@ -109,7 +107,6 @@ impl BlockChain {
let genesis = storage
.get_genesis()?
.ok_or_else(|| format_err!("Can not find genesis hash in storage."))?;
let head_id = head_block.id();
watch(CHAIN_WATCH_NAME, "n1253");
let mut chain = Self {
genesis_hash: genesis,
Expand All @@ -125,7 +122,7 @@ impl BlockChain {
storage.as_ref(),
),
status: ChainStatusWithBlock {
status: ChainStatus::new(head_block.header.clone(), block_info,None), //FIXME:read from snapshot
status: ChainStatus::new(head_block.header.clone(), block_info, None), //FIXME:read from snapshot
head: head_block,
},
statedb: chain_state,
Expand Down Expand Up @@ -188,10 +185,17 @@ impl BlockChain {
head_block_id: genesis_id,
total_difficulty: executed_block.block_info().get_total_difficulty(),
}]
.into_iter()
.collect(),
.into_iter()
.collect(),
)?;
Self::new(time_service, executed_block.block.id(), storage, net, None, None)
Self::new(
time_service,
executed_block.block.id(),
storage,
net,
None,
None,
)
}

pub fn current_epoch_uncles_size(&self) -> u64 {
Expand Down Expand Up @@ -370,15 +374,15 @@ impl BlockChain {
}

pub fn verify_with_verifier<V>(&mut self, block: Block) -> Result<VerifiedBlock>
where
V: BlockVerifier,
where
V: BlockVerifier,
{
V::verify_block(self, block)
}

pub fn apply_with_verifier<V>(&mut self, block: Block) -> Result<ExecutedBlock>
where
V: BlockVerifier,
where
V: BlockVerifier,
{
let verified_block = self.verify_with_verifier::<V>(block)?;
watch(CHAIN_WATCH_NAME, "n1");
Expand Down Expand Up @@ -755,9 +759,17 @@ impl BlockChain {

impl ChainReader for BlockChain {
fn info(&self) -> ChainInfo {
let (dag_accumulator, k_total_difficulties) = self.storage.get_lastest_snapshot().unwrap_or(None).map(|snapshot| {
(Some(snapshot.accumulator_info), Some(snapshot.k_total_difficulties))
}).unwrap_or((None, None));
let (dag_accumulator, k_total_difficulties) = self
.storage
.get_lastest_snapshot()
.unwrap_or(None)
.map(|snapshot| {
(
Some(snapshot.accumulator_info),
Some(snapshot.k_total_difficulties),
)
})
.unwrap_or((None, None));
ChainInfo::new(
self.status.head.header().chain_id(),
self.genesis_hash,
Expand All @@ -779,10 +791,6 @@ impl ChainReader for BlockChain {
self.status.status.head().clone()
}

fn net_id(&self) -> ChainNetworkID {
self.net.clone()
}

fn get_header(&self, hash: HashValue) -> Result<Option<BlockHeader>> {
self.storage
.get_block_header_by_hash(hash)
Expand Down Expand Up @@ -1110,6 +1118,10 @@ impl ChainReader for BlockChain {
state_proof,
}))
}

fn net_id(&self) -> ChainNetworkID {
self.net.clone()
}
}

impl BlockChain {
Expand Down Expand Up @@ -1214,11 +1226,14 @@ impl BlockChain {
Ok(event_with_infos)
}

#[allow(dead_code)]
fn connect_dag(&mut self, executed_block: ExecutedBlock) -> Result<ExecutedBlock> {
let dag = self.dag.clone().expect("dag should init with blockdag");
let (new_tip_block, _) = (executed_block.block(), executed_block.block_info());
let mut tips = self
.status.dag_tips().as_ref()
.status
.dag_tips()
.as_ref()
.expect("Tips should exist on dag")
.clone();
let parents = executed_block
Expand Down Expand Up @@ -1278,18 +1293,18 @@ impl BlockChain {
impl ChainWriter for BlockChain {
fn can_connect(&self, executed_block: &ExecutedBlock) -> bool {
if executed_block.block.header().parent_hash() == self.status.status.head().id() {
return true;
true
} else {
return Self::calculate_dag_accumulator_key(
Self::calculate_dag_accumulator_key(
self.status
.status
.tips_hash
.as_ref()
.expect("dag blocks must have tips")
.clone(),
)
.expect("failed to calculate the tips hash")
== executed_block.block().header().parent_hash();
.expect("failed to calculate the tips hash")
== executed_block.block().header().parent_hash()
}
}

Expand Down
7 changes: 7 additions & 0 deletions consensus/src/consensusdb/access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ where
}
}

#[allow(dead_code)]
pub fn iterator(
&self,
) -> Result<impl Iterator<Item = Result<(Box<[u8]>, S::Value), Box<dyn Error>>> + '_, StoreError>
Expand Down Expand Up @@ -96,6 +97,7 @@ where
Ok(())
}

#[allow(dead_code)]
pub fn write_many(
&self,
mut writer: impl DbWriter,
Expand All @@ -109,6 +111,7 @@ where
}

/// Write directly from an iterator and do not cache any data. NOTE: this action also clears the cache
#[allow(dead_code)]
pub fn write_many_without_cache(
&self,
mut writer: impl DbWriter,
Expand All @@ -122,12 +125,14 @@ where
Ok(())
}

#[allow(dead_code)]
pub fn delete(&self, mut writer: impl DbWriter, key: S::Key) -> Result<(), StoreError> {
self.cache.remove(&key);
writer.delete::<S>(&key)?;
Ok(())
}

#[allow(dead_code)]
pub fn delete_many(
&self,
mut writer: impl DbWriter,
Expand All @@ -141,6 +146,7 @@ where
Ok(())
}

#[allow(dead_code)]
pub fn delete_all(&self, mut writer: impl DbWriter) -> Result<(), StoreError> {
self.cache.remove_all();
let keys = self
Expand All @@ -164,6 +170,7 @@ where

/// A dynamic iterator that can iterate through a specific prefix, and from a certain start point.
//TODO: loop and chain iterators for multi-prefix iterator.
#[allow(dead_code)]
pub fn seek_iterator(
&self,
seek_from: Option<S::Key>, // iter whole range if None
Expand Down
4 changes: 4 additions & 0 deletions consensus/src/consensusdb/consensus_ghostdag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ impl From<GhostdagData> for GhostDagDataWrapper {

impl GhostDagDataWrapper {
/// Returns an iterator to the mergeset in ascending blue work order (tie-breaking by hash)
#[allow(dead_code)]
pub fn ascending_mergeset_without_selected_parent<'a>(
&'a self,
store: &'a (impl GhostdagStoreReader + ?Sized),
Expand Down Expand Up @@ -91,6 +92,7 @@ impl GhostDagDataWrapper {
}

/// Returns an iterator to the mergeset in descending blue work order (tie-breaking by hash)
#[allow(dead_code)]
pub fn descending_mergeset_without_selected_parent<'a>(
&'a self,
store: &'a (impl GhostdagStoreReader + ?Sized),
Expand Down Expand Up @@ -129,6 +131,7 @@ impl GhostDagDataWrapper {
/// Returns an iterator to the mergeset in topological consensus order -- starting with the selected parent,
/// and adding the mergeset in increasing blue work order. Note that this is a topological order even though
/// the selected parent has highest blue work by def -- since the mergeset is in its anticone.
#[allow(dead_code)]
pub fn consensus_ordered_mergeset<'a>(
&'a self,
store: &'a (impl GhostdagStoreReader + ?Sized),
Expand All @@ -140,6 +143,7 @@ impl GhostDagDataWrapper {
}

/// Returns an iterator to the mergeset in topological consensus order without the selected parent
#[allow(dead_code)]
pub fn consensus_ordered_mergeset_without_selected_parent<'a>(
&'a self,
store: &'a (impl GhostdagStoreReader + ?Sized),
Expand Down
2 changes: 1 addition & 1 deletion consensus/src/consensusdb/consensus_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use starcoin_crypto::HashValue as Hash;
use starcoin_types::block::BlockHeader;
use starcoin_types::{
blockhash::BlockLevel,
consensus_header::{CompactHeaderData, ConsensusHeader, HeaderWithBlockLevel},
consensus_header::{CompactHeaderData, HeaderWithBlockLevel},
U256,
};
use std::sync::Arc;
Expand Down
3 changes: 3 additions & 0 deletions consensus/src/consensusdb/consensus_reachability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ pub struct StagingReachabilityStore<'a> {
}

impl<'a> StagingReachabilityStore<'a> {
#[allow(dead_code)]
pub fn new(store_read: RwLockUpgradableReadGuard<'a, DbReachabilityStore>) -> Self {
Self {
store_read,
Expand All @@ -248,6 +249,7 @@ impl<'a> StagingReachabilityStore<'a> {
}
}

#[allow(dead_code)]
pub fn commit(
self,
batch: &mut WriteBatch,
Expand All @@ -269,6 +271,7 @@ impl<'a> StagingReachabilityStore<'a> {
}

impl ReachabilityStore for StagingReachabilityStore<'_> {
#[allow(dead_code)]
fn init(&mut self, origin: Hash, capacity: Interval) -> Result<(), StoreError> {
self.insert(origin, Hash::new(blockhash::NONE), capacity, 0)?;
self.set_reindex_root(origin)?;
Expand Down
2 changes: 1 addition & 1 deletion consensus/src/consensusdb/consensus_relations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ mod tests {
test_relations_store(db.relations_store);
}

fn test_relations_store<T: RelationsStore>(mut store: T) {
fn test_relations_store<T: RelationsStore>(store: T) {
let parents = [
(1, vec![]),
(2, vec![1]),
Expand Down
Loading

0 comments on commit fd09130

Please sign in to comment.