From 101be2368dafb72e9acc305848eb37f29449764a Mon Sep 17 00:00:00 2001 From: Jack Huang Date: Wed, 28 Aug 2024 14:31:27 +0800 Subject: [PATCH] Fix status command (#4186) * fix cmd: status * update rpc/api/generated_rpc_schema/sync_manager.json --- cmd/starcoin/src/node/sync/status_cmd.rs | 4 +- cmd/tx-factory/src/main.rs | 3 +- .../generated_rpc_schema/sync_manager.json | 44 +++++++--------- rpc/api/src/sync_manager.rs | 5 +- rpc/api/src/types.rs | 50 ++++++++++++++++++- rpc/client/src/lib.rs | 5 +- rpc/server/src/module/sync_manager_rpc.rs | 7 ++- types/src/sync_status.rs | 7 +++ 8 files changed, 84 insertions(+), 41 deletions(-) diff --git a/cmd/starcoin/src/node/sync/status_cmd.rs b/cmd/starcoin/src/node/sync/status_cmd.rs index 59219013c7..fa5a8cfb2d 100644 --- a/cmd/starcoin/src/node/sync/status_cmd.rs +++ b/cmd/starcoin/src/node/sync/status_cmd.rs @@ -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")] @@ -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, diff --git a/cmd/tx-factory/src/main.rs b/cmd/tx-factory/src/main.rs index f2606c14bc..3e6bcffa0b 100644 --- a/cmd/tx-factory/src/main.rs +++ b/cmd/tx-factory/src/main.rs @@ -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}; @@ -537,7 +538,7 @@ impl TxnMocker { fn stress_test(&self, accounts: Vec, 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(()); diff --git a/rpc/api/generated_rpc_schema/sync_manager.json b/rpc/api/generated_rpc_schema/sync_manager.json index 3d1dde294a..2e17cc3665 100644 --- a/rpc/api/generated_rpc_schema/sync_manager.json +++ b/rpc/api/generated_rpc_schema/sync_manager.json @@ -9,10 +9,10 @@ "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", @@ -20,7 +20,6 @@ ], "properties": { "chain_status": { - "description": "The latest status of a chain.", "type": "object", "required": [ "head", @@ -33,6 +32,7 @@ "required": [ "author", "block_accumulator_root", + "block_hash", "body_hash", "chain_id", "difficulty", @@ -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" }, @@ -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", @@ -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.", @@ -107,9 +101,7 @@ }, "number": { "description": "Block number.", - "type": "integer", - "format": "uint64", - "minimum": 0.0 + "type": "string" }, "parent_hash": { "description": "Parent hash.", @@ -117,7 +109,7 @@ "format": "HashValue" }, "parents_hash": { - "description": "Parents hash.", + "description": "block parents", "type": "array", "items": { "type": "string", @@ -136,9 +128,7 @@ }, "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.", @@ -146,7 +136,7 @@ "format": "HashValue" }, "version": { - "description": "Header version", + "description": "version", "type": "integer", "format": "uint32", "minimum": 0.0 diff --git a/rpc/api/src/sync_manager.rs b/rpc/api/src/sync_manager.rs index 4d24eda588..c2576555b8 100644 --- a/rpc/api/src/sync_manager.rs +++ b/rpc/api/src/sync_manager.rs @@ -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; + fn status(&self) -> FutureResult; #[rpc(name = "sync.cancel")] fn cancel(&self) -> FutureResult<()>; diff --git a/rpc/api/src/types.rs b/rpc/api/src/types.rs index f1098b5bc7..3e7e90b5cc 100644 --- a/rpc/api/src/types.rs +++ b/rpc/api/src/types.rs @@ -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; @@ -1975,6 +1976,53 @@ impl From 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 for ChainStatus { + fn from(value: ChainStatusView) -> Self { + Self { + head: value.head.into(), + info: value.info, + } + } +} + +impl From 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 for SyncStatus { + fn from(value: SyncStatusView) -> Self { + Self::new_with_state(value.chain_status.into(), value.state) + } +} + +impl From 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}; diff --git a/rpc/client/src/lib.rs b/rpc/client/src/lib.rs index a20424b8c1..569ff82333 100644 --- a/rpc/client/src/lib.rs +++ b/rpc/client/src/lib.rs @@ -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::{ @@ -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}; @@ -1042,7 +1041,7 @@ impl RpcClient { result } - pub fn sync_status(&self) -> anyhow::Result { + pub fn sync_status(&self) -> anyhow::Result { self.call_rpc_blocking(|inner| inner.sync_client.status()) .map_err(map_err) } diff --git a/rpc/server/src/module/sync_manager_rpc.rs b/rpc/server/src/module/sync_manager_rpc.rs index 7474f8e247..1c0630d057 100644 --- a/rpc/server/src/module/sync_manager_rpc.rs +++ b/rpc/server/src/module/sync_manager_rpc.rs @@ -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 where @@ -31,11 +30,11 @@ impl SyncManagerApi for SyncManagerRpcImpl where S: SyncAsyncService, { - fn status(&self) -> FutureResult { + fn status(&self) -> FutureResult { 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()) diff --git a/types/src/sync_status.rs b/types/src/sync_status.rs index 1f66aa36c5..8fa57f90ba 100644 --- a/types/src/sync_status.rs +++ b/types/src/sync_status.rs @@ -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,