Skip to content

Commit

Permalink
add is ancestor of command for reachability viewing
Browse files Browse the repository at this point in the history
  • Loading branch information
jackzhhuang committed Sep 13, 2024
1 parent f088974 commit 9c3a1ac
Show file tree
Hide file tree
Showing 11 changed files with 103 additions and 5 deletions.
7 changes: 6 additions & 1 deletion chain/api/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use crate::{ChainType, TransactionInfoWithProof};
use anyhow::Result;
use starcoin_crypto::HashValue;
use starcoin_dag::consensusdb::consenses_state::DagStateView;
use starcoin_dag::consensusdb::consenses_state::{DagStateView, ReachabilityView};
use starcoin_dag::types::ghostdata::GhostdagData;
use starcoin_service_registry::ServiceRequest;
use starcoin_types::transaction::RichTransactionInfo;
Expand Down Expand Up @@ -68,6 +68,10 @@ pub enum ChainRequest {
GetDagStateView,
CheckChainType,
GetGhostdagData(HashValue),
IsAncestorOfCommand {
ancestor: HashValue,
descendants: Vec<HashValue>,
},
}

impl ServiceRequest for ChainRequest {
Expand Down Expand Up @@ -99,4 +103,5 @@ pub enum ChainResponse {
DagStateView(Box<DagStateView>),
CheckChainType(ChainType),
GhostdagDataOption(Box<Option<GhostdagData>>),
IsAncestorOfCommand { reachability_view: ReachabilityView },
}
24 changes: 23 additions & 1 deletion chain/api/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::message::{ChainRequest, ChainResponse};
use crate::{ChainType, TransactionInfoWithProof};
use anyhow::{bail, Result};
use starcoin_crypto::HashValue;
use starcoin_dag::consensusdb::consenses_state::DagStateView;
use starcoin_dag::consensusdb::consenses_state::{DagStateView, ReachabilityView};
use starcoin_dag::types::ghostdata::GhostdagData;
use starcoin_service_registry::{ActorService, ServiceHandler, ServiceRef};
use starcoin_types::contract_event::{ContractEvent, ContractEventInfo};
Expand Down Expand Up @@ -149,6 +149,11 @@ pub trait ChainAsyncService:
async fn get_dag_state(&self) -> Result<DagStateView>;
async fn check_chain_type(&self) -> Result<ChainType>;
async fn get_ghostdagdata(&self, id: HashValue) -> Result<Option<GhostdagData>>;
async fn is_ancestor_of(
&self,
ancestor: HashValue,
descendants: Vec<HashValue>,
) -> Result<starcoin_dag::consensusdb::consenses_state::ReachabilityView>;
}

#[async_trait::async_trait]
Expand Down Expand Up @@ -486,4 +491,21 @@ where
bail!("failed to get ghostdag data")
}
}
async fn is_ancestor_of(
&self,
ancestor: HashValue,
descendants: Vec<HashValue>,
) -> Result<ReachabilityView> {
let response = self
.send(ChainRequest::IsAncestorOfCommand {
ancestor,
descendants,
})
.await??;
if let ChainResponse::IsAncestorOfCommand { reachability_view } = response {
Ok(reachability_view)
} else {
bail!("failed to get ghostdag data")
}
}
}
6 changes: 6 additions & 0 deletions chain/service/src/chain_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,12 @@ impl ServiceHandler<Self, ChainRequest> for ChainReaderService {
ChainRequest::GetGhostdagData(id) => Ok(ChainResponse::GhostdagDataOption(Box::new(
self.inner.get_ghostdagdata(id)?,
))),
ChainRequest::IsAncestorOfCommand {
ancestor,
descendants,
} => Ok(ChainResponse::IsAncestorOfCommand {
reachability_view: self.inner.dag.is_ancestor_of(ancestor, descendants)?,
}),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion chain/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1578,7 +1578,7 @@ impl BlockChain {
if chain_id.is_vega() {
4000000
} else if chain_id.is_proxima() {
700000
500
} else if chain_id.is_halley() {
4200000
} else if chain_id.is_main() {
Expand Down
2 changes: 2 additions & 0 deletions cmd/starcoin/src/chain/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ mod get_txn_info_list_cmd;
mod get_txn_infos_cmd;
pub mod get_txn_proof_cmd;
mod info_cmd;
mod is_ancestor_of_cmd;
mod list_block_cmd;

pub use epoch_info::*;
Expand All @@ -24,4 +25,5 @@ pub use get_txn_info_cmd::*;
pub use get_txn_info_list_cmd::*;
pub use get_txn_infos_cmd::*;
pub use info_cmd::*;
pub use is_ancestor_of_cmd::*;
pub use list_block_cmd::*;
22 changes: 21 additions & 1 deletion flexidag/src/blockdag.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use super::reachability::{inquirer, reachability_service::MTReachabilityService};
use super::types::ghostdata::GhostdagData;
use crate::consensusdb::consenses_state::{DagState, DagStateReader, DagStateStore};
use crate::consensusdb::consenses_state::{
DagState, DagStateReader, DagStateStore, ReachabilityView,
};
use crate::consensusdb::prelude::{FlexiDagStorageConfig, StoreError};
use crate::consensusdb::schemadb::{GhostdagStoreReader, ReachabilityStore, REINDEX_ROOT_KEY};
use crate::consensusdb::{
Expand Down Expand Up @@ -579,4 +581,22 @@ impl BlockDAG {

anyhow::Ok(())
}

pub fn is_ancestor_of(
&self,
ancestor: Hash,
descendants: Vec<Hash>,
) -> anyhow::Result<ReachabilityView> {
let de = descendants
.into_iter()
.filter(|descendant| {
self.check_ancestor_of(ancestor, vec![*descendant])
.unwrap_or(false)
})
.collect::<Vec<_>>();
anyhow::Ok(ReachabilityView {
ancestor,
descendants: de,
})
}
}
6 changes: 6 additions & 0 deletions flexidag/src/consensusdb/consenses_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,9 @@ impl DagStateView {
DagState { tips: self.tips }
}
}

#[derive(Eq, PartialEq, Hash, Deserialize, Serialize, Clone, Debug, JsonSchema)]
pub struct ReachabilityView {
pub ancestor: Hash,
pub descendants: Vec<Hash>,
}
8 changes: 8 additions & 0 deletions rpc/api/src/chain/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,14 @@ pub trait ChainApi {
/// Get block ghostdag data
#[rpc(name = "chain.get_ghostdagdata")]
fn get_ghostdagdata(&self, block_hash: HashValue) -> FutureResult<Option<GhostdagData>>;

/// Check the ancestor and descendants' relationship
#[rpc(name = "chain.is_ancestor_of")]
fn is_ancestor_of(
&self,
ancestor: HashValue,
descendants: Vec<HashValue>,
) -> FutureResult<starcoin_dag::consensusdb::consenses_state::ReachabilityView>;
}

#[derive(Copy, Clone, Default, Serialize, Deserialize, JsonSchema)]
Expand Down
11 changes: 10 additions & 1 deletion rpc/client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use serde_json::Value;
use starcoin_abi_types::{FunctionABI, ModuleABI, StructInstantiation};
use starcoin_account_api::AccountInfo;
use starcoin_crypto::HashValue;
use starcoin_dag::consensusdb::consenses_state::DagStateView;
use starcoin_dag::consensusdb::consenses_state::{DagStateView, ReachabilityView};
use starcoin_logger::{prelude::*, LogPattern};
use starcoin_rpc_api::chain::{
GetBlockOption, GetBlocksOption, GetEventOption, GetTransactionOption,
Expand Down Expand Up @@ -790,6 +790,15 @@ impl RpcClient {
.map_err(map_err)
}

pub fn is_ancestor_of(
&self,
ancestor: HashValue,
descendants: Vec<HashValue>,
) -> anyhow::Result<ReachabilityView> {
self.call_rpc_blocking(|inner| inner.chain_client.is_ancestor_of(ancestor, descendants))
.map_err(map_err)
}

pub fn chain_get_blocks_by_number(
&self,
number: Option<BlockNumber>,
Expand Down
12 changes: 12 additions & 0 deletions rpc/server/src/module/chain_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,18 @@ where
let fut = async move { service.get_ghostdagdata(block_hash).await }.map_err(map_err);
Box::pin(fut.boxed())
}

#[doc = " Check the ancestor and descendants\' relationship "]
fn is_ancestor_of(
&self,
ancestor: HashValue,
descendants: Vec<HashValue>,
) -> FutureResult<starcoin_dag::consensusdb::consenses_state::ReachabilityView> {
let service = self.service.clone();
let fut =
async move { service.is_ancestor_of(ancestor, descendants).await }.map_err(map_err);
Box::pin(fut.boxed())
}
}

fn try_decode_block_txns(state: &dyn StateView, block: &mut BlockView) -> anyhow::Result<()> {
Expand Down
8 changes: 8 additions & 0 deletions vm/starcoin-transactional-test-harness/src/fork_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,14 @@ impl ChainApi for MockChainApi {
fn get_ghostdagdata(&self, _block_hash: HashValue) -> FutureResult<Option<GhostdagData>> {
unimplemented!()
}

fn is_ancestor_of(
&self,
_ancestor: HashValue,
_descendants: Vec<HashValue>,
) -> FutureResult<starcoin_dag::consensusdb::consenses_state::ReachabilityView> {
unimplemented!()
}
}

fn try_decode_block_txns(state: &dyn StateView, block: &mut BlockView) -> anyhow::Result<()> {
Expand Down

0 comments on commit 9c3a1ac

Please sign in to comment.