Skip to content

Commit

Permalink
Merge #2188
Browse files Browse the repository at this point in the history
2188: feat: add sync state rpc r=quake,doitian a=driftluo



Co-authored-by: driftluo <driftluo@foxmail.com>
  • Loading branch information
bors[bot] and driftluo committed Jul 30, 2020
2 parents 130bc44 + fa16413 commit 7cf407d
Show file tree
Hide file tree
Showing 10 changed files with 168 additions and 9 deletions.
4 changes: 2 additions & 2 deletions ckb-bin/src/subcommand/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ pub fn run(args: RunArgs, version: Version) -> Result<(), ExitCode> {
.enable_chain(shared.clone())
.enable_pool(
shared.clone(),
sync_shared,
Arc::clone(&sync_shared),
args.config.tx_pool.min_fee_rate,
args.config.rpc.reject_ill_transactions,
)
Expand All @@ -136,7 +136,7 @@ pub fn run(args: RunArgs, version: Version) -> Result<(), ExitCode> {
chain_controller.clone(),
miner_enable,
)
.enable_net(network_controller.clone())
.enable_net(network_controller.clone(), sync_shared)
.enable_stats(shared.clone(), synchronizer, Arc::clone(&alert_notifier))
.enable_experiment(shared.clone())
.enable_integration_test(shared.clone(), network_controller.clone(), chain_controller)
Expand Down
47 changes: 47 additions & 0 deletions rpc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ Subscriptions require a full duplex connection. CKB offers such connections in t
* [`get_peers`](#get_peers)
* [`get_banned_addresses`](#get_banned_addresses)
* [`set_ban`](#set_ban)
* [`sync_state`](#sync_state)
* [`Pool`](#pool)
* [`send_transaction`](#send_transaction)
* [`tx_pool_info`](#tx_pool_info)
Expand Down Expand Up @@ -2001,6 +2002,52 @@ http://localhost:8114
}
```

### `sync_state`

Returns sync state of this client

#### Returns

best_known_block_number - Height of the most difficult header observed across the network
best_known_block_timestamp - Timestamp of the most difficult header observed across the network
ibd - Whether the node is in IBD status, i.e. whether the local data is within one day of the latest
inflight_blocks_count - Number of blocks being requested for download
orphan_blocks_count - Number of blocks that have been downloaded but can't find the corresponding parents yet
fast_time - The download scheduler's time analysis data, the fast is the 1/3 of the cut-off point, unit ms
normal_time - The download scheduler's time analysis data, the normal is the 4/5 of the cut-off point, unit ms
low_time - The download scheduler's time analysis data, the low is the 9/10 of the cut-off point, unit ms

#### Examples

```bash
echo '{
"id": 2,
"jsonrpc": "2.0",
"method": "sync_state",
"params": []
}' \
| tr -d '\n' \
| curl -H 'content-type: application/json' -d @- \
http://localhost:8114
```

```json
{
"id": 2,
"jsonrpc": "2.0",
"result": {
"best_known_block_number": "0x248623",
"best_known_block_timestamp": "0x173943c36e4",
"fast_time": "0x3e8",
"ibd": false,
"inflight_blocks_count": "0x0",
"low_time": "0x5dc",
"normal_time": "0x4e2",
"orphan_blocks_count": "0x0"
}
}
```

## Pool

### `send_transaction`
Expand Down
43 changes: 43 additions & 0 deletions rpc/json/rpc.json
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,49 @@
}
]
},
{
"description": "Returns sync state of this client",
"method": "sync_state",
"module": "net",
"params": [],
"result": {
"best_known_block_number": "0x248623",
"best_known_block_timestamp": "0x173943c36e4",
"fast_time": "0x3e8",
"ibd": false,
"inflight_blocks_count": "0x0",
"low_time": "0x5dc",
"normal_time": "0x4e2",
"orphan_blocks_count": "0x0"
},
"returns": [
{
"best_known_block_number": "Height of the most difficult header observed across the network"
},
{
"best_known_block_timestamp": "Timestamp of the most difficult header observed across the network"
},
{
"ibd": "Whether the node is in IBD status, i.e. whether the local data is within one day of the latest"
},
{
"inflight_blocks_count": "Number of blocks being requested for download"
},
{
"orphan_blocks_count": "Number of blocks that have been downloaded but can't find the corresponding parents yet"
},
{
"fast_time": "The download scheduler's time analysis data, the fast is the 1/3 of the cut-off point, unit ms"
},
{
"normal_time": "The download scheduler's time analysis data, the normal is the 4/5 of the cut-off point, unit ms"
},
{
"low_time": "The download scheduler's time analysis data, the low is the 9/10 of the cut-off point, unit ms"
}
],
"skip": true
},
{
"description": "Return state info of blockchain",
"method": "get_blockchain_info",
Expand Down
30 changes: 28 additions & 2 deletions rpc/src/module/net.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use crate::error::RPCError;
use ckb_jsonrpc_types::{BannedAddr, LocalNode, NodeAddress, RemoteNode, Timestamp};
use ckb_jsonrpc_types::{BannedAddr, LocalNode, NodeAddress, RemoteNode, SyncState, Timestamp};
use ckb_network::{MultiaddrExt, NetworkController};
use ckb_sync::SyncShared;
use faketime::unix_time_as_millis;
use jsonrpc_core::Result;
use jsonrpc_derive::rpc;
use std::collections::HashMap;
use std::{collections::HashMap, sync::Arc};

const MAX_ADDRS: usize = 50;
const DEFAULT_BAN_DURATION: u64 = 24 * 60 * 60 * 1000; // 1 day
Expand Down Expand Up @@ -33,10 +34,15 @@ pub trait NetworkRpc {
absolute: Option<bool>,
reason: Option<String>,
) -> Result<()>;

// curl -d '{"id": 2, "jsonrpc": "2.0", "method":"sync_state","params": []}' -H 'content-type:application/json' 'http://localhost:8114'
#[rpc(name = "sync_state")]
fn sync_state(&self) -> Result<SyncState>;
}

pub(crate) struct NetworkRpcImpl {
pub network_controller: NetworkController,
pub sync_shared: Arc<SyncShared>,
}

impl NetworkRpc for NetworkRpcImpl {
Expand Down Expand Up @@ -150,4 +156,24 @@ impl NetworkRpc for NetworkRpcImpl {
))),
}
}

fn sync_state(&self) -> Result<SyncState> {
let chain = self.sync_shared.active_chain();
let state = chain.shared().state();
let (fast_time, normal_time, low_time) = state.read_inflight_blocks().division_point();
let best_known = state.shared_best_header();
let sync_state = SyncState {
ibd: chain.is_initial_block_download(),
best_known_block_number: best_known.number().into(),
best_known_block_timestamp: best_known.timestamp().into(),
orphan_blocks_count: (state.orphan_pool().len() as u64).into(),
inflight_blocks_count: (state.read_inflight_blocks().total_inflight_count() as u64)
.into(),
fast_time: fast_time.into(),
normal_time: normal_time.into(),
low_time: low_time.into(),
};

Ok(sync_state)
}
}
12 changes: 10 additions & 2 deletions rpc/src/service_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,16 @@ impl<'a> ServiceBuilder<'a> {
self
}

pub fn enable_net(mut self, network_controller: NetworkController) -> Self {
let rpc_method = NetworkRpcImpl { network_controller }.to_delegate();
pub fn enable_net(
mut self,
network_controller: NetworkController,
sync_shared: Arc<SyncShared>,
) -> Self {
let rpc_method = NetworkRpcImpl {
network_controller,
sync_shared,
}
.to_delegate();
if self.config.net_enable() {
self.io_handler.extend_with(rpc_method);
} else {
Expand Down
9 changes: 8 additions & 1 deletion rpc/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,11 +226,18 @@ fn setup_node(height: u64) -> (Shared, ChainController, RpcServer) {
.to_delegate(),
);
io.extend_with(
PoolRpcImpl::new(shared.clone(), sync_shared, FeeRate::zero(), true).to_delegate(),
PoolRpcImpl::new(
shared.clone(),
Arc::clone(&sync_shared),
FeeRate::zero(),
true,
)
.to_delegate(),
);
io.extend_with(
NetworkRpcImpl {
network_controller: network_controller.clone(),
sync_shared,
}
.to_delegate(),
);
Expand Down
4 changes: 4 additions & 0 deletions sync/src/orphan_block_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ impl OrphanBlockPool {
.and_then(|value| value.get(hash).cloned())
})
}

pub fn len(&self) -> usize {
self.parents.read().len()
}
}

#[cfg(test)]
Expand Down
12 changes: 12 additions & 0 deletions sync/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,14 @@ impl InflightBlocks {
self.inflight_states.len()
}

pub fn division_point(&self) -> (u64, u64, u64) {
(
self.time_analyzer.fast_time,
self.time_analyzer.normal_time,
self.time_analyzer.low_time,
)
}

pub fn peer_inflight_count(&self, peer: PeerIndex) -> usize {
self.download_schedulers
.get(&peer)
Expand Down Expand Up @@ -1603,6 +1611,10 @@ impl SyncState {
blocks
}

pub fn orphan_pool(&self) -> &OrphanBlockPool {
&self.orphan_block_pool
}

pub fn insert_block_status(&self, block_hash: Byte32, status: BlockStatus) {
self.block_status_map.lock().insert(block_hash, status);
}
Expand Down
2 changes: 1 addition & 1 deletion util/jsonrpc-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub use self::fixed_bytes::Byte32;
pub use self::indexer::{
CellTransaction, LiveCell, LockHashCapacity, LockHashIndexState, TransactionPoint,
};
pub use self::net::{BannedAddr, LocalNode, NodeAddress, RemoteNode};
pub use self::net::{BannedAddr, LocalNode, NodeAddress, RemoteNode, SyncState};
pub use self::pool::{OutputsValidator, TxPoolInfo};
pub use self::proposal_short_id::ProposalShortId;
pub use self::sync::PeerState;
Expand Down
14 changes: 13 additions & 1 deletion util/jsonrpc-types/src/net.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{Timestamp, Uint64};
use crate::{BlockNumber, Timestamp, Uint64};
use serde::{Deserialize, Serialize};

#[derive(Clone, Default, Serialize, Deserialize, PartialEq, Eq, Hash, Debug)]
Expand Down Expand Up @@ -29,3 +29,15 @@ pub struct BannedAddr {
pub ban_reason: String,
pub created_at: Timestamp,
}

#[derive(Clone, Default, Serialize, Deserialize, PartialEq, Eq, Hash, Debug)]
pub struct SyncState {
pub ibd: bool,
pub best_known_block_number: BlockNumber,
pub best_known_block_timestamp: Timestamp,
pub orphan_blocks_count: Uint64,
pub inflight_blocks_count: Uint64,
pub fast_time: Uint64,
pub normal_time: Uint64,
pub low_time: Uint64,
}

0 comments on commit 7cf407d

Please sign in to comment.