Skip to content

Commit c06cc03

Browse files
byeongjeemajecty
authored andcommitted
Add RPCs for checking peer best blocks & target blocks
1 parent 6e83f0d commit c06cc03

File tree

5 files changed

+119
-5
lines changed

5 files changed

+119
-5
lines changed

rpc/src/v1/impls/devel.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2018-2019 Kodebox, Inc.
1+
// Copyright 2018-2020 Kodebox, Inc.
22
// This file is part of CodeChain.
33
//
44
// This program is free software: you can redistribute it and/or modify
@@ -101,6 +101,26 @@ where
101101
}
102102
}
103103

104+
fn get_peer_best_block_hashes(&self) -> Result<Vec<(SocketAddr, BlockHash)>> {
105+
if let Some(block_sync) = self.block_sync.as_ref() {
106+
let (sender, receiver) = unbounded_event_callback();
107+
block_sync.send(BlockSyncEvent::GetPeerBestBlockHashes(sender)).unwrap();
108+
Ok(receiver.iter().collect())
109+
} else {
110+
Ok(Vec::new())
111+
}
112+
}
113+
114+
fn get_target_block_hashes(&self) -> Result<Vec<BlockHash>> {
115+
if let Some(block_sync) = self.block_sync.as_ref() {
116+
let (sender, receiver) = unbounded_event_callback();
117+
block_sync.send(BlockSyncEvent::GetTargetBlockHashes(sender)).unwrap();
118+
Ok(receiver.iter().collect())
119+
} else {
120+
Ok(Vec::new())
121+
}
122+
}
123+
104124
fn snapshot(&self, block_hash: BlockHash) -> Result<()> {
105125
self.client.notify_snapshot(BlockId::Hash(block_hash));
106126
Ok(())

rpc/src/v1/traits/devel.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2018 Kodebox, Inc.
1+
// Copyright 2018-2020 Kodebox, Inc.
22
// This file is part of CodeChain.
33
//
44
// This program is free software: you can redistribute it and/or modify
@@ -38,6 +38,12 @@ pub trait Devel {
3838
#[rpc(name = "devel_getBlockSyncPeers")]
3939
fn get_block_sync_peers(&self) -> Result<Vec<SocketAddr>>;
4040

41+
#[rpc(name = "devel_getPeerBestBlockHashes")]
42+
fn get_peer_best_block_hashes(&self) -> Result<Vec<(SocketAddr, BlockHash)>>;
43+
44+
#[rpc(name = "devel_getTargetBlockHashes")]
45+
fn get_target_block_hashes(&self) -> Result<Vec<BlockHash>>;
46+
4147
#[rpc(name = "devel_snapshot")]
4248
fn snapshot(&self, hash: BlockHash) -> Result<()>;
4349

spec/JSON-RPC.md

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,8 @@ When `Transaction` is included in any response, there will be an additional fiel
348348
* [devel_startSealing](#devel_startsealing)
349349
* [devel_stopSealing](#devel_stopsealing)
350350
* [devel_getBlockSyncPeers](#devel_getblocksyncpeers)
351-
351+
* [devel_getPeerBestBlockHashes](#devel_getpeerbestblockhases)
352+
* [devel_getTargetBlockHashes](#devel_gettargetblockhashes)
352353

353354
# Specification
354355

@@ -3063,6 +3064,76 @@ No parameters
30633064

30643065
[Back to **List of methods**](#list-of-methods)
30653066

3067+
## devel_getPeerBestBlockHashes
3068+
3069+
Get IP address and best block hash of each peer.
3070+
3071+
### Params
3072+
3073+
No parameters
3074+
3075+
### Returns
3076+
3077+
[ 'string', 'H256' ][]
3078+
3079+
### Request Example
3080+
3081+
```
3082+
curl \
3083+
-H 'Content-Type: application/json' \
3084+
-d '{"jsonrpc": "2.0", "method": "devel_getPeerBestBlockHashes", "params": [], "id": 3}' \
3085+
localhost:8080
3086+
```
3087+
3088+
### Response Example
3089+
```
3090+
{
3091+
"jsonrpc":"2.0",
3092+
"result": [
3093+
["1.2.3.4:3485", "0x56642f04d519ae3262c7ba6facf1c5b11450ebaeb7955337cfbc45420d573077"],
3094+
["1.2.3.5:3485", "0x7f7104b580f9418d444560009e5a92a4573d42d2c51cd0c6045afdc761826249"]
3095+
],
3096+
"id":3
3097+
}
3098+
```
3099+
3100+
[Back to **List of methods**](#list-of-methods)
3101+
3102+
## devel_getTargetBlockHashes
3103+
3104+
Get hashes of target blocks
3105+
3106+
### Params
3107+
3108+
No parameters
3109+
3110+
### Returns
3111+
3112+
'`H256[]`
3113+
3114+
### Request Example
3115+
3116+
```
3117+
curl \
3118+
-H 'Content-Type: application/json' \
3119+
-d '{"jsonrpc": "2.0", "method": "devel_getTargetBlockHashes", "params": [], "id": 3}' \
3120+
localhost:8080
3121+
```
3122+
3123+
### Response Example
3124+
```
3125+
{
3126+
"jsonrpc":"2.0",
3127+
"result": [
3128+
"0x56642f04d519ae3262c7ba6facf1c5b11450ebaeb7955337cfbc45420d573077",
3129+
"0x7f7104b580f9418d444560009e5a92a4573d42d2c51cd0c6045afdc761826249"
3130+
],
3131+
"id":3
3132+
}
3133+
```
3134+
3135+
[Back to **List of methods**](#list-of-methods)
3136+
30663137
## devel_testTPS
30673138

30683139
Test TPS as the parameters.

sync/src/block/downloader/body.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2018-2019 Kodebox, Inc.
1+
// Copyright 2018-2020 Kodebox, Inc.
22
// This file is part of CodeChain.
33
//
44
// This program is free software: you can redistribute it and/or modify
@@ -67,6 +67,10 @@ impl BodyDownloader {
6767
self.downloading.shrink_to_fit();
6868
}
6969

70+
pub fn get_target_hashes(&self) -> Vec<BlockHash> {
71+
self.targets.iter().map(|t| t.hash).collect()
72+
}
73+
7074
pub fn add_target(&mut self, header: &Header) {
7175
cdebug!(SYNC, "Add download target: {}", header.hash());
7276
self.targets.push(Target {

sync/src/block/extension.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use ccore::{
2323
ImportError, StateInfo, UnverifiedTransaction,
2424
};
2525
use cdb::AsHashDB;
26-
use cnetwork::{Api, EventSender, NetworkExtension, NodeId};
26+
use cnetwork::{Api, EventSender, IntoSocketAddr, NetworkExtension, NodeId};
2727
use codechain_crypto::BLAKE_NULL_RLP;
2828
use cstate::{FindActionHandler, TopLevelState, TopStateView};
2929
use ctimer::TimerToken;
@@ -41,6 +41,7 @@ use std::collections::hash_map::Entry;
4141
use std::collections::{HashMap, HashSet};
4242
use std::fs;
4343
use std::mem::discriminant;
44+
use std::net::SocketAddr;
4445
use std::sync::Arc;
4546
use std::time::Duration;
4647
use token_generator::TokenGenerator;
@@ -655,6 +656,16 @@ impl NetworkExtension<Event> for Extension {
655656
channel.send(*peer).unwrap();
656657
}
657658
}
659+
Event::GetPeerBestBlockHashes(channel) => {
660+
for (node_id, header_downloader) in self.header_downloaders.iter() {
661+
channel.send((SocketAddr::from(node_id.into_addr()), header_downloader.best_hash())).unwrap();
662+
}
663+
}
664+
Event::GetTargetBlockHashes(channel) => {
665+
for target in self.body_downloader.get_target_hashes() {
666+
channel.send(target).unwrap();
667+
}
668+
}
658669
Event::NewHeaders {
659670
imported,
660671
enacted,
@@ -673,6 +684,8 @@ impl NetworkExtension<Event> for Extension {
673684

674685
pub enum Event {
675686
GetPeers(EventSender<NodeId>),
687+
GetPeerBestBlockHashes(EventSender<(SocketAddr, BlockHash)>),
688+
GetTargetBlockHashes(EventSender<BlockHash>),
676689
NewHeaders {
677690
imported: Vec<BlockHash>,
678691
enacted: Vec<BlockHash>,

0 commit comments

Comments
 (0)