Skip to content

Commit

Permalink
Ghostdagdata rpc (#4137)
Browse files Browse the repository at this point in the history
* rpc: Add chain.get_ghostdagdata

* Update schema
  • Loading branch information
sanlee42 authored Jun 18, 2024
1 parent b8dcb96 commit 715fb1a
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 2 deletions.
3 changes: 3 additions & 0 deletions chain/api/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::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;
Expand Down Expand Up @@ -68,6 +69,7 @@ pub enum ChainRequest {
GetDagStateView,
CheckDagType(HashValue),
DagForkHeigh,
GetGhostdagData(HashValue),
}

impl ServiceRequest for ChainRequest {
Expand Down Expand Up @@ -99,4 +101,5 @@ pub enum ChainResponse {
DagStateView(Box<DagStateView>),
CheckDagType(DagHeaderType),
DagForkHeight(Option<BlockNumber>),
GhostdagDataOption(Box<Option<GhostdagData>>),
}
14 changes: 14 additions & 0 deletions chain/api/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::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};
Expand Down Expand Up @@ -78,6 +79,7 @@ pub trait ReadableChainService {
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 get_ghostdagdata(&self, id: HashValue) -> Result<Option<GhostdagData>>;
}

/// Writeable block chain service trait
Expand Down Expand Up @@ -149,6 +151,7 @@ pub trait ChainAsyncService:
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 get_ghostdagdata(&self, id: HashValue) -> Result<Option<GhostdagData>>;
}

#[async_trait::async_trait]
Expand Down Expand Up @@ -483,4 +486,15 @@ where
bail!("failed to get dag fork height")
}
}

async fn get_ghostdagdata(&self, block_hash: HashValue) -> Result<Option<GhostdagData>> {
let response = self
.send(ChainRequest::GetGhostdagData(block_hash))
.await??;
if let ChainResponse::GhostdagDataOption(ghostdag_data) = response {
Ok(*ghostdag_data)
} else {
bail!("failed to get ghostdag data")
}
}
}
13 changes: 13 additions & 0 deletions chain/service/src/chain_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use starcoin_config::NodeConfig;
use starcoin_crypto::HashValue;
use starcoin_dag::blockdag::BlockDAG;
use starcoin_dag::consensusdb::consenses_state::DagStateView;
use starcoin_dag::types::ghostdata::GhostdagData;
use starcoin_logger::prelude::*;
use starcoin_service_registry::{
ActorService, EventHandler, ServiceContext, ServiceFactory, ServiceHandler,
Expand Down Expand Up @@ -257,6 +258,9 @@ impl ServiceHandler<Self, ChainRequest> for ChainReaderService {
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 @@ -480,6 +484,15 @@ impl ReadableChainService for ChainReaderServiceInner {
fn dag_fork_height(&self) -> Result<Option<BlockNumber>> {
self.main.dag_fork_height()
}
fn get_ghostdagdata(&self, id: HashValue) -> Result<Option<GhostdagData>> {
self.dag
.ghostdata_by_hash(id)
.map(|option_arc_ghostdagdata| {
option_arc_ghostdagdata.map(|arc_ghostdagdata| {
Arc::try_unwrap(arc_ghostdagdata).unwrap_or_else(|arc| (*arc).clone())
})
})
}
}

#[cfg(test)]
Expand Down
4 changes: 3 additions & 1 deletion flexidag/src/types/ghostdata.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use schemars::{self, JsonSchema};
use serde::{Deserialize, Serialize};
use starcoin_crypto::HashValue as Hash;
use starcoin_types::blockhash::{BlockHashMap, BlockHashes, BlueWorkType, HashKTypeMap, KType};

#[derive(Clone, Serialize, Deserialize, Default, Debug)]
#[derive(Clone, Serialize, Deserialize, Default, Debug, JsonSchema)]
pub struct GhostdagData {
pub blue_score: u64,
#[schemars(with = "String")]
pub blue_work: BlueWorkType,
pub selected_parent: Hash,
pub mergeset_blues: BlockHashes,
Expand Down
69 changes: 69 additions & 0 deletions rpc/api/generated_rpc_schema/chain.json
Original file line number Diff line number Diff line change
Expand Up @@ -4018,6 +4018,75 @@
}
}
}
},
{
"name": "chain.get_ghostdagdata",
"params": [
{
"name": "block_hash",
"schema": {
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "HashValue",
"type": "string",
"format": "HashValue"
}
}
],
"result": {
"name": "Option < GhostdagData >",
"schema": {
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Nullable_GhostdagData",
"type": [
"object",
"null"
],
"required": [
"blue_score",
"blue_work",
"blues_anticone_sizes",
"mergeset_blues",
"mergeset_reds",
"selected_parent"
],
"properties": {
"blue_score": {
"type": "integer",
"format": "uint64",
"minimum": 0.0
},
"blue_work": {
"type": "string"
},
"blues_anticone_sizes": {
"type": "object",
"additionalProperties": {
"type": "integer",
"format": "uint16",
"minimum": 0.0
}
},
"mergeset_blues": {
"type": "array",
"items": {
"type": "string",
"format": "HashValue"
}
},
"mergeset_reds": {
"type": "array",
"items": {
"type": "string",
"format": "HashValue"
}
},
"selected_parent": {
"type": "string",
"format": "HashValue"
}
}
}
}
}
]
}
5 changes: 5 additions & 0 deletions rpc/api/src/chain/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use schemars::{self, JsonSchema};
use serde::{Deserialize, Serialize};
use starcoin_crypto::HashValue;
use starcoin_dag::consensusdb::consenses_state::DagStateView;
use starcoin_dag::types::ghostdata::GhostdagData;
use starcoin_types::block::BlockNumber;
use starcoin_vm_types::access_path::AccessPath;

Expand Down Expand Up @@ -127,6 +128,10 @@ pub trait ChainApi {
/// Get the state of a dag.
#[rpc(name = "chain.get_dag_state")]
fn get_dag_state(&self) -> FutureResult<DagStateView>;

/// Get block ghostdag data
#[rpc(name = "chain.get_ghostdagdata")]
fn get_ghostdagdata(&self, block_hash: HashValue) -> FutureResult<Option<GhostdagData>>;
}

#[derive(Copy, Clone, Default, Serialize, Deserialize, JsonSchema)]
Expand Down
7 changes: 7 additions & 0 deletions rpc/server/src/module/chain_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use starcoin_chain_service::ChainAsyncService;
use starcoin_config::NodeConfig;
use starcoin_crypto::HashValue;
use starcoin_dag::consensusdb::consenses_state::DagStateView;
use starcoin_dag::types::ghostdata::GhostdagData;
use starcoin_logger::prelude::*;
use starcoin_resource_viewer::MoveValueAnnotator;
use starcoin_rpc_api::chain::{
Expand Down Expand Up @@ -478,6 +479,12 @@ where

Box::pin(fut.boxed())
}

fn get_ghostdagdata(&self, block_hash: HashValue) -> FutureResult<Option<GhostdagData>> {
let service = self.service.clone();
let fut = async move { service.get_ghostdagdata(block_hash).await }.map_err(map_err);
Box::pin(fut.boxed())
}
}

fn try_decode_block_txns(state: &dyn StateView, block: &mut BlockView) -> anyhow::Result<()> {
Expand Down
6 changes: 5 additions & 1 deletion vm/starcoin-transactional-test-harness/src/fork_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use starcoin_accumulator::{node::AccumulatorStoreType, Accumulator, MerkleAccumu
use starcoin_config::{BuiltinNetworkID, ChainNetworkID};
use starcoin_crypto::HashValue;
use starcoin_dag::consensusdb::consenses_state::DagStateView;
use starcoin_dag::types::ghostdata::GhostdagData;
use starcoin_rpc_api::chain::{ChainApi, GetBlockOption};
use starcoin_rpc_api::chain::{ChainApiClient, GetBlocksOption};
use starcoin_rpc_api::types::{
Expand All @@ -34,7 +35,6 @@ use starcoin_vm_types::access_path::AccessPath;
use std::hash::Hash;
use std::option::Option::{None, Some};
use std::sync::{Arc, Mutex};

#[derive(Eq, PartialEq, Hash, Clone, Debug)]
pub struct ChainStatusWithBlock {
pub status: ChainStatus,
Expand Down Expand Up @@ -501,6 +501,10 @@ impl ChainApi for MockChainApi {
fn get_dag_state(&self) -> FutureResult<DagStateView> {
todo!("not implement yet")
}

fn get_ghostdagdata(&self, _block_hash: HashValue) -> FutureResult<Option<GhostdagData>> {
unimplemented!()
}
}

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

0 comments on commit 715fb1a

Please sign in to comment.