Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion rpc/src/v1/impls/devel.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2018-2019 Kodebox, Inc.
// Copyright 2018-2020 Kodebox, Inc.
// This file is part of CodeChain.
//
// This program is free software: you can redistribute it and/or modify
Expand Down Expand Up @@ -101,6 +101,26 @@ where
}
}

fn get_peer_best_block_hashes(&self) -> Result<Vec<(SocketAddr, BlockHash)>> {
if let Some(block_sync) = self.block_sync.as_ref() {
let (sender, receiver) = unbounded_event_callback();
block_sync.send(BlockSyncEvent::GetPeerBestBlockHashes(sender)).unwrap();
Ok(receiver.iter().collect())
} else {
Ok(Vec::new())
}
}

fn get_target_block_hashes(&self) -> Result<Vec<BlockHash>> {
if let Some(block_sync) = self.block_sync.as_ref() {
let (sender, receiver) = unbounded_event_callback();
block_sync.send(BlockSyncEvent::GetTargetBlockHashes(sender)).unwrap();
Ok(receiver.iter().collect())
} else {
Ok(Vec::new())
}
}

fn snapshot(&self, block_hash: BlockHash) -> Result<()> {
self.client.notify_snapshot(BlockId::Hash(block_hash));
Ok(())
Expand Down
8 changes: 7 additions & 1 deletion rpc/src/v1/traits/devel.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2018 Kodebox, Inc.
// Copyright 2018-2020 Kodebox, Inc.
// This file is part of CodeChain.
//
// This program is free software: you can redistribute it and/or modify
Expand Down Expand Up @@ -38,6 +38,12 @@ pub trait Devel {
#[rpc(name = "devel_getBlockSyncPeers")]
fn get_block_sync_peers(&self) -> Result<Vec<SocketAddr>>;

#[rpc(name = "devel_getPeerBestBlockHashes")]
fn get_peer_best_block_hashes(&self) -> Result<Vec<(SocketAddr, BlockHash)>>;

#[rpc(name = "devel_getTargetBlockHashes")]
fn get_target_block_hashes(&self) -> Result<Vec<BlockHash>>;

#[rpc(name = "devel_snapshot")]
fn snapshot(&self, hash: BlockHash) -> Result<()>;

Expand Down
73 changes: 72 additions & 1 deletion spec/JSON-RPC.md
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,8 @@ When `Transaction` is included in any response, there will be an additional fiel
* [devel_startSealing](#devel_startsealing)
* [devel_stopSealing](#devel_stopsealing)
* [devel_getBlockSyncPeers](#devel_getblocksyncpeers)

* [devel_getPeerBestBlockHashes](#devel_getpeerbestblockhases)
* [devel_getTargetBlockHashes](#devel_gettargetblockhashes)

# Specification

Expand Down Expand Up @@ -3063,6 +3064,76 @@ No parameters

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

## devel_getPeerBestBlockHashes

Get IP address and best block hash of each peer.

### Params

No parameters

### Returns

[ 'string', 'H256' ][]

### Request Example

```
curl \
-H 'Content-Type: application/json' \
-d '{"jsonrpc": "2.0", "method": "devel_getPeerBestBlockHashes", "params": [], "id": 3}' \
localhost:8080
```

### Response Example
```
{
"jsonrpc":"2.0",
"result": [
["1.2.3.4:3485", "0x56642f04d519ae3262c7ba6facf1c5b11450ebaeb7955337cfbc45420d573077"],
["1.2.3.5:3485", "0x7f7104b580f9418d444560009e5a92a4573d42d2c51cd0c6045afdc761826249"]
],
"id":3
}
```

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

## devel_getTargetBlockHashes

Get hashes of target blocks

### Params

No parameters

### Returns

'`H256[]`

### Request Example

```
curl \
-H 'Content-Type: application/json' \
-d '{"jsonrpc": "2.0", "method": "devel_getTargetBlockHashes", "params": [], "id": 3}' \
localhost:8080
```

### Response Example
```
{
"jsonrpc":"2.0",
"result": [
"0x56642f04d519ae3262c7ba6facf1c5b11450ebaeb7955337cfbc45420d573077",
"0x7f7104b580f9418d444560009e5a92a4573d42d2c51cd0c6045afdc761826249"
],
"id":3
}
```

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

## devel_testTPS

Test TPS as the parameters.
Expand Down
6 changes: 5 additions & 1 deletion sync/src/block/downloader/body.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2018-2019 Kodebox, Inc.
// Copyright 2018-2020 Kodebox, Inc.
// This file is part of CodeChain.
//
// This program is free software: you can redistribute it and/or modify
Expand Down Expand Up @@ -67,6 +67,10 @@ impl BodyDownloader {
self.downloading.shrink_to_fit();
}

pub fn get_target_hashes(&self) -> Vec<BlockHash> {
self.targets.iter().map(|t| t.hash).collect()
}

pub fn add_target(&mut self, header: &Header) {
cdebug!(SYNC, "Add download target: {}", header.hash());
self.targets.push(Target {
Expand Down
15 changes: 14 additions & 1 deletion sync/src/block/extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use ccore::{
ImportError, StateInfo, UnverifiedTransaction,
};
use cdb::AsHashDB;
use cnetwork::{Api, EventSender, NetworkExtension, NodeId};
use cnetwork::{Api, EventSender, IntoSocketAddr, NetworkExtension, NodeId};
use codechain_crypto::BLAKE_NULL_RLP;
use cstate::{FindActionHandler, TopLevelState, TopStateView};
use ctimer::TimerToken;
Expand All @@ -41,6 +41,7 @@ use std::collections::hash_map::Entry;
use std::collections::{HashMap, HashSet};
use std::fs;
use std::mem::discriminant;
use std::net::SocketAddr;
use std::sync::Arc;
use std::time::Duration;
use token_generator::TokenGenerator;
Expand Down Expand Up @@ -655,6 +656,16 @@ impl NetworkExtension<Event> for Extension {
channel.send(*peer).unwrap();
}
}
Event::GetPeerBestBlockHashes(channel) => {
for (node_id, header_downloader) in self.header_downloaders.iter() {
channel.send((SocketAddr::from(node_id.into_addr()), header_downloader.best_hash())).unwrap();
}
}
Event::GetTargetBlockHashes(channel) => {
for target in self.body_downloader.get_target_hashes() {
channel.send(target).unwrap();
}
}
Event::NewHeaders {
imported,
enacted,
Expand All @@ -673,6 +684,8 @@ impl NetworkExtension<Event> for Extension {

pub enum Event {
GetPeers(EventSender<NodeId>),
GetPeerBestBlockHashes(EventSender<(SocketAddr, BlockHash)>),
GetTargetBlockHashes(EventSender<BlockHash>),
NewHeaders {
imported: Vec<BlockHash>,
enacted: Vec<BlockHash>,
Expand Down