Skip to content

Commit

Permalink
Fix status command (#4186)
Browse files Browse the repository at this point in the history
* fix cmd: status

* update  rpc/api/generated_rpc_schema/sync_manager.json
  • Loading branch information
jackzhhuang committed Aug 28, 2024
1 parent ee335e6 commit 101be23
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 41 deletions.
4 changes: 2 additions & 2 deletions cmd/starcoin/src/node/sync/status_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::StarcoinOpt;
use anyhow::Result;
use clap::Parser;
use scmd::{CommandAction, ExecContext};
use starcoin_types::sync_status::SyncStatus;
use starcoin_rpc_api::types::SyncStatusView;

#[derive(Debug, Parser, Default)]
#[clap(name = "status")]
Expand All @@ -18,7 +18,7 @@ impl CommandAction for StatusCommand {
type State = CliState;
type GlobalOpt = StarcoinOpt;
type Opt = StatusOpt;
type ReturnItem = SyncStatus;
type ReturnItem = SyncStatusView;

fn run(
&self,
Expand Down
3 changes: 2 additions & 1 deletion cmd/tx-factory/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use starcoin_tx_factory::txn_generator::MockTxnGenerator;
use starcoin_types::account::DEFAULT_EXPIRATION_TIME;
use starcoin_types::account_address::AccountAddress;
use starcoin_types::account_config::association_address;
use starcoin_types::sync_status::SyncStatus;
use starcoin_types::transaction::RawUserTransaction;
use std::path::PathBuf;
use std::sync::atomic::{AtomicBool, Ordering};
Expand Down Expand Up @@ -537,7 +538,7 @@ impl TxnMocker {

fn stress_test(&self, accounts: Vec<AccountInfo>, round_num: u32) -> Result<()> {
//check node status
let sync_status = self.client.sync_status()?;
let sync_status: SyncStatus = self.client.sync_status()?.into();
if sync_status.is_syncing() {
info!("node syncing, pause stress");
return Ok(());
Expand Down
44 changes: 17 additions & 27 deletions rpc/api/generated_rpc_schema/sync_manager.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,17 @@
"name": "sync.status",
"params": [],
"result": {
"name": "SyncStatus",
"name": "SyncStatusView",
"schema": {
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "SyncStatus",
"title": "SyncStatusView",
"type": "object",
"required": [
"chain_status",
"state"
],
"properties": {
"chain_status": {
"description": "The latest status of a chain.",
"type": "object",
"required": [
"head",
Expand All @@ -33,6 +32,7 @@
"required": [
"author",
"block_accumulator_root",
"block_hash",
"body_hash",
"chain_id",
"difficulty",
Expand All @@ -55,14 +55,18 @@
"format": "AccountAddress"
},
"author_auth_key": {
"description": "Block author auth key. this field is deprecated",
"description": "Block author auth key.",
"type": [
"string",
"null"
]
},
"block_accumulator_root": {
"description": "The parent block info's block accumulator root hash.",
"description": "The block accumulator root hash.",
"type": "string",
"format": "HashValue"
},
"block_hash": {
"type": "string",
"format": "HashValue"
},
Expand All @@ -73,17 +77,9 @@
},
"chain_id": {
"description": "The chain id",
"type": "object",
"required": [
"id"
],
"properties": {
"id": {
"type": "integer",
"format": "uint8",
"minimum": 0.0
}
}
"type": "integer",
"format": "uint8",
"minimum": 0.0
},
"difficulty": {
"description": "Block difficulty",
Expand All @@ -95,9 +91,7 @@
},
"gas_used": {
"description": "Gas used for contracts execution.",
"type": "integer",
"format": "uint64",
"minimum": 0.0
"type": "string"
},
"nonce": {
"description": "Consensus nonce field.",
Expand All @@ -107,17 +101,15 @@
},
"number": {
"description": "Block number.",
"type": "integer",
"format": "uint64",
"minimum": 0.0
"type": "string"
},
"parent_hash": {
"description": "Parent hash.",
"type": "string",
"format": "HashValue"
},
"parents_hash": {
"description": "Parents hash.",
"description": "block parents",
"type": "array",
"items": {
"type": "string",
Expand All @@ -136,17 +128,15 @@
},
"timestamp": {
"description": "Block timestamp.",
"type": "integer",
"format": "uint64",
"minimum": 0.0
"type": "string"
},
"txn_accumulator_root": {
"description": "The transaction accumulator root hash after executing this block.",
"type": "string",
"format": "HashValue"
},
"version": {
"description": "Header version",
"description": "version",
"type": "integer",
"format": "uint32",
"minimum": 0.0
Expand Down
5 changes: 2 additions & 3 deletions rpc/api/src/sync_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@
// SPDX-License-Identifier: Apache-2

pub use self::gen_client::Client as SyncManagerClient;
use crate::FutureResult;
use crate::{types::SyncStatusView, FutureResult};
use network_api::PeerStrategy;
use network_p2p_types::peer_id::PeerId;
use openrpc_derive::openrpc;
use starcoin_sync_api::{PeerScoreResponse, SyncProgressReport};
use starcoin_types::sync_status::SyncStatus;

#[openrpc]
pub trait SyncManagerApi {
#[rpc(name = "sync.status")]
fn status(&self) -> FutureResult<SyncStatus>;
fn status(&self) -> FutureResult<SyncStatusView>;

#[rpc(name = "sync.cancel")]
fn cancel(&self) -> FutureResult<()>;
Expand Down
50 changes: 49 additions & 1 deletion rpc/api/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ use starcoin_types::event::EventKey;
use starcoin_types::genesis_config;
use starcoin_types::language_storage::TypeTag;
use starcoin_types::proof::SparseMerkleProof;
use starcoin_types::startup_info::ChainInfo;
use starcoin_types::startup_info::{ChainInfo, ChainStatus};
use starcoin_types::sync_status::{SyncState, SyncStatus};
use starcoin_types::transaction::authenticator::{AuthenticationKey, TransactionAuthenticator};
use starcoin_types::transaction::{RawUserTransaction, ScriptFunction, TransactionArgument};
use starcoin_types::vm_error::AbortLocation;
Expand Down Expand Up @@ -1975,6 +1976,53 @@ impl From<TableInfoView> for TableInfo {
}
}

#[derive(Eq, PartialEq, Deserialize, Serialize, Clone, Debug, JsonSchema)]
pub struct ChainStatusView {
/// Chain head block's header.
pub head: BlockHeaderView,
/// Chain block info
pub info: BlockInfo,
}

impl From<ChainStatusView> for ChainStatus {
fn from(value: ChainStatusView) -> Self {
Self {
head: value.head.into(),
info: value.info,
}
}
}

impl From<ChainStatus> for ChainStatusView {
fn from(value: ChainStatus) -> Self {
Self {
head: value.head.into(),
info: value.info,
}
}
}

#[derive(Eq, PartialEq, Deserialize, Serialize, Clone, Debug, JsonSchema)]
pub struct SyncStatusView {
pub chain_status: ChainStatusView,
pub state: SyncState,
}

impl From<SyncStatusView> for SyncStatus {
fn from(value: SyncStatusView) -> Self {
Self::new_with_state(value.chain_status.into(), value.state)
}
}

impl From<SyncStatus> for SyncStatusView {
fn from(value: SyncStatus) -> Self {
Self {
chain_status: value.chain_status().clone().into(),
state: value.sync_status().clone(),
}
}
}

#[cfg(test)]
mod tests {
use crate::types::{ByteCodeOrScriptFunction, FunctionId, StrView};
Expand Down
5 changes: 2 additions & 3 deletions rpc/client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use starcoin_rpc_api::types::{
DryRunTransactionRequest, FactoryAction, FunctionIdView, ListCodeView, ListResourceView,
MintedBlockView, ModuleIdView, PeerInfoView, ResourceView, SignedMessageView,
SignedUserTransactionView, StateWithProofView, StateWithTableItemProofView, StrView,
StructTagView, TableInfoView, TransactionEventResponse, TransactionInfoView,
StructTagView, SyncStatusView, TableInfoView, TransactionEventResponse, TransactionInfoView,
TransactionInfoWithProofView, TransactionRequest, TransactionView,
};
use starcoin_rpc_api::{
Expand All @@ -55,7 +55,6 @@ use starcoin_types::account_address::AccountAddress;
use starcoin_types::account_state::AccountState;
use starcoin_types::block::BlockNumber;
use starcoin_types::sign_message::SigningMessage;
use starcoin_types::sync_status::SyncStatus;
use starcoin_types::system_events::MintBlockEvent;
use starcoin_types::transaction::{RawUserTransaction, SignedUserTransaction};
use starcoin_vm_types::language_storage::{ModuleId, StructTag};
Expand Down Expand Up @@ -1042,7 +1041,7 @@ impl RpcClient {
result
}

pub fn sync_status(&self) -> anyhow::Result<SyncStatus> {
pub fn sync_status(&self) -> anyhow::Result<SyncStatusView> {
self.call_rpc_blocking(|inner| inner.sync_client.status())
.map_err(map_err)
}
Expand Down
7 changes: 3 additions & 4 deletions rpc/server/src/module/sync_manager_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ use futures::future::TryFutureExt;
use futures::FutureExt;
use network_api::PeerStrategy;
use network_p2p_types::peer_id::PeerId;
use starcoin_rpc_api::sync_manager::SyncManagerApi;
use starcoin_rpc_api::FutureResult;
use starcoin_rpc_api::{sync_manager::SyncManagerApi, types::SyncStatusView};
use starcoin_sync_api::{PeerScoreResponse, SyncAsyncService, SyncProgressReport};
use starcoin_types::sync_status::SyncStatus;

pub struct SyncManagerRpcImpl<S>
where
Expand All @@ -31,11 +30,11 @@ impl<S> SyncManagerApi for SyncManagerRpcImpl<S>
where
S: SyncAsyncService,
{
fn status(&self) -> FutureResult<SyncStatus> {
fn status(&self) -> FutureResult<SyncStatusView> {
let service = self.service.clone();
let fut = async move {
let result = service.status().await?;
Ok(result)
Ok(result.into())
}
.map_err(map_err);
Box::pin(fut.boxed())
Expand Down
7 changes: 7 additions & 0 deletions types/src/sync_status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ impl SyncStatus {
}
}

pub fn new_with_state(chain_status: ChainStatus, state: SyncState) -> Self {
Self {
chain_status,
state,
}
}

pub fn sync_begin(&mut self, target: BlockIdAndNumber, total_difficulty: U256) {
self.state = SyncState::Synchronizing {
target,
Expand Down

0 comments on commit 101be23

Please sign in to comment.