Skip to content

Commit 6b9e5de

Browse files
committed
Add RPC devel_snapshot
1 parent 931562a commit 6b9e5de

File tree

8 files changed

+73
-7
lines changed

8 files changed

+73
-7
lines changed

core/src/client/client.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ use super::{
4545
};
4646
use crate::block::{ClosedBlock, IsBlock, OpenBlock, SealedBlock};
4747
use crate::blockchain::{BlockChain, BlockProvider, BodyProvider, HeaderProvider, InvoiceProvider, TransactionAddress};
48-
use crate::client::{ConsensusClient, TermInfo};
48+
use crate::client::{ConsensusClient, SnapshotClient, TermInfo};
4949
use crate::consensus::{CodeChainEngine, EngineError};
5050
use crate::encoded;
5151
use crate::error::{BlockImportError, Error, ImportError, SchemeError};
@@ -965,3 +965,11 @@ impl FindActionHandler for Client {
965965
self.engine.find_action_handler_for(id)
966966
}
967967
}
968+
969+
impl SnapshotClient for Client {
970+
fn notify_snapshot(&self, id: BlockId) {
971+
if let Some(header) = self.block_header(&id) {
972+
self.engine.send_snapshot_notify(header.hash())
973+
}
974+
}
975+
}

core/src/client/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,3 +348,7 @@ pub trait StateInfo {
348348
/// is unknown.
349349
fn state_at(&self, id: BlockId) -> Option<TopLevelState>;
350350
}
351+
352+
pub trait SnapshotClient {
353+
fn notify_snapshot(&self, id: BlockId);
354+
}

core/src/consensus/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,8 @@ pub trait ConsensusEngine: Sync + Send {
268268

269269
fn register_snapshot_notify_sender(&self, _sender: SnapshotNotifySender) {}
270270

271+
fn send_snapshot_notify(&self, _block_hash: BlockHash) {}
272+
271273
fn get_best_block_from_best_proposal_header(&self, header: &HeaderView) -> BlockHash {
272274
header.hash()
273275
}

core/src/consensus/solo/mod.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@ use std::sync::{Arc, Weak};
2020

2121
use ckey::Address;
2222
use cstate::{ActionHandler, HitHandler};
23-
use ctypes::{CommonParams, Header};
23+
use ctypes::{BlockHash, CommonParams, Header};
2424
use parking_lot::RwLock;
2525

2626
use self::params::SoloParams;
2727
use super::stake;
2828
use super::{ConsensusEngine, Seal};
2929
use crate::block::{ExecutedBlock, IsBlock};
30+
use crate::client::snapshot_notify::NotifySender;
3031
use crate::client::ConsensusClient;
3132
use crate::codechain_machine::CodeChainMachine;
3233
use crate::consensus::{EngineError, EngineType};
@@ -38,6 +39,7 @@ pub struct Solo {
3839
params: SoloParams,
3940
machine: CodeChainMachine,
4041
action_handlers: Vec<Arc<dyn ActionHandler>>,
42+
snapshot_notify_sender: Arc<RwLock<Option<NotifySender>>>,
4143
}
4244

4345
impl Solo {
@@ -54,6 +56,7 @@ impl Solo {
5456
params,
5557
machine,
5658
action_handlers,
59+
snapshot_notify_sender: Arc::new(RwLock::new(None)),
5760
}
5861
}
5962

@@ -150,6 +153,18 @@ impl ConsensusEngine for Solo {
150153
1
151154
}
152155

156+
fn register_snapshot_notify_sender(&self, sender: NotifySender) {
157+
let mut guard = self.snapshot_notify_sender.write();
158+
assert!(guard.is_none(), "snapshot_notify_sender is registered twice");
159+
*guard = Some(sender);
160+
}
161+
162+
fn send_snapshot_notify(&self, block_hash: BlockHash) {
163+
if let Some(sender) = self.snapshot_notify_sender.read().as_ref() {
164+
sender.notify(block_hash)
165+
}
166+
}
167+
153168
fn action_handlers(&self) -> &[Arc<dyn ActionHandler>] {
154169
&self.action_handlers
155170
}

core/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ pub use crate::client::snapshot_notify;
8888
pub use crate::client::Error::Database;
8989
pub use crate::client::{
9090
AccountData, AssetClient, BlockChainClient, BlockChainTrait, ChainNotify, Client, ClientConfig, DatabaseClient,
91-
EngineClient, EngineInfo, ExecuteClient, ImportBlock, MiningBlockChainClient, Shard, StateInfo, TermInfo,
92-
TestBlockChainClient, TextClient,
91+
EngineClient, EngineInfo, ExecuteClient, ImportBlock, MiningBlockChainClient, Shard, SnapshotClient, StateInfo,
92+
TermInfo, TestBlockChainClient, TextClient,
9393
};
9494
pub use crate::consensus::{EngineType, TimeGapParams};
9595
pub use crate::db::{COL_STATE, NUM_COLUMNS};

rpc/src/v1/impls/devel.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use std::vec::Vec;
2323

2424
use ccore::{
2525
BlockId, DatabaseClient, EngineClient, EngineInfo, MinerService, MiningBlockChainClient, SignedTransaction,
26-
TermInfo, COL_STATE,
26+
SnapshotClient, TermInfo, COL_STATE,
2727
};
2828
use ccrypto::Blake;
2929
use cjson::bytes::Bytes;
@@ -33,7 +33,7 @@ use csync::BlockSyncEvent;
3333
use ctypes::transaction::{
3434
Action, AssetMintOutput, AssetOutPoint, AssetTransferInput, AssetTransferOutput, Transaction,
3535
};
36-
use ctypes::{Tracker, TxHash};
36+
use ctypes::{BlockHash, Tracker, TxHash};
3737
use jsonrpc_core::Result;
3838
use kvdb::KeyValueDB;
3939
use primitives::{H160, H256};
@@ -70,7 +70,7 @@ where
7070

7171
impl<C, M> Devel for DevelClient<C, M>
7272
where
73-
C: DatabaseClient + EngineInfo + EngineClient + MiningBlockChainClient + TermInfo + 'static,
73+
C: DatabaseClient + EngineInfo + EngineClient + MiningBlockChainClient + TermInfo + SnapshotClient + 'static,
7474
M: MinerService + 'static,
7575
{
7676
fn get_state_trie_keys(&self, offset: usize, limit: usize) -> Result<Vec<H256>> {
@@ -108,6 +108,11 @@ where
108108
}
109109
}
110110

111+
fn snapshot(&self, block_hash: BlockHash) -> Result<()> {
112+
self.client.notify_snapshot(BlockId::Hash(block_hash));
113+
Ok(())
114+
}
115+
111116
fn test_tps(&self, setting: TPSTestSetting) -> Result<f64> {
112117
let common_params = self.client.common_params(BlockId::Latest).unwrap();
113118
let mint_fee = common_params.min_asset_mint_cost();

rpc/src/v1/traits/devel.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use std::net::SocketAddr;
1818

1919
use cjson::bytes::Bytes;
20+
use ctypes::BlockHash;
2021
use jsonrpc_core::Result;
2122
use primitives::H256;
2223

@@ -39,6 +40,9 @@ pub trait Devel {
3940
#[rpc(name = "devel_getBlockSyncPeers")]
4041
fn get_block_sync_peers(&self) -> Result<Vec<SocketAddr>>;
4142

43+
#[rpc(name = "devel_snapshot")]
44+
fn snapshot(&self, hash: BlockHash) -> Result<()>;
45+
4246
#[rpc(name = "devel_testTPS")]
4347
fn test_tps(&self, setting: TPSTestSetting) -> Result<f64>;
4448
}

spec/JSON-RPC.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,7 @@ When `Transaction` is included in any response, there will be an additional fiel
367367
***
368368
* [devel_getStateTrieKeys](#devel_getstatetriekeys)
369369
* [devel_getStateTrieValue](#devel_getstatetrievalue)
370+
* [devel_snapshot](#devel_snapshot)
370371
* [devel_startSealing](#devel_startsealing)
371372
* [devel_stopSealing](#devel_stopsealing)
372373
* [devel_getBlockSyncPeers](#devel_getblocksyncpeers)
@@ -2979,6 +2980,33 @@ Gets the value of the state trie with the given key.
29792980

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

2983+
## devel_snapshot
2984+
Snapshot the state of the given block hash.
2985+
2986+
### Params
2987+
1. key: `H256`
2988+
2989+
### Returns
2990+
2991+
### Request Example
2992+
```
2993+
curl \
2994+
-H 'Content-Type: application/json' \
2995+
-d '{"jsonrpc": "2.0", "method": "devel_snapshot", "params": ["0xfc196ede542b03b55aee9f106004e7e3d7ea6a9600692e964b4735a260356b50"], "id": null}' \
2996+
localhost:8080
2997+
```
2998+
2999+
### Response Example
3000+
```
3001+
{
3002+
"jsonrpc":"2.0",
3003+
"result":[],
3004+
"id":null
3005+
}
3006+
```
3007+
3008+
[Back to **List of methods**](#list-of-methods)
3009+
29823010
## devel_startSealing
29833011
Starts and enables sealing blocks by the miner.
29843012

0 commit comments

Comments
 (0)