Skip to content

Commit f26af1c

Browse files
committed
Add RPC devel_snapshot
1 parent 320200f commit f26af1c

File tree

8 files changed

+73
-6
lines changed

8 files changed

+73
-6
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};
@@ -939,3 +939,11 @@ impl FindActionHandler for Client {
939939
self.engine.find_action_handler_for(id)
940940
}
941941
}
942+
943+
impl SnapshotClient for Client {
944+
fn notify_snapshot(&self, id: BlockId) {
945+
if let Some(header) = self.block_header(&id) {
946+
self.engine.send_snapshot_notify(header.hash())
947+
}
948+
}
949+
}

core/src/client/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,3 +343,7 @@ pub trait StateInfo {
343343
/// is unknown.
344344
fn state_at(&self, id: BlockId) -> Option<TopLevelState>;
345345
}
346+
347+
pub trait SnapshotClient {
348+
fn notify_snapshot(&self, id: BlockId);
349+
}

core/src/consensus/mod.rs

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

266266
fn register_snapshot_notify_sender(&self, _sender: SnapshotNotifySender) {}
267267

268+
fn send_snapshot_notify(&self, _block_hash: BlockHash) {}
269+
268270
fn get_best_block_from_best_proposal_header(&self, header: &HeaderView) -> BlockHash {
269271
header.hash()
270272
}

core/src/consensus/solo/mod.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@ use std::sync::Arc;
2020

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

2526
use self::params::SoloParams;
2627
use super::stake;
2728
use super::{ConsensusEngine, Seal};
2829
use crate::block::{ExecutedBlock, IsBlock};
30+
use crate::client::snapshot_notify::NotifySender;
2931
use crate::codechain_machine::CodeChainMachine;
3032
use crate::consensus::{EngineError, EngineType};
3133
use crate::error::Error;
@@ -35,6 +37,7 @@ pub struct Solo {
3537
params: SoloParams,
3638
machine: CodeChainMachine,
3739
action_handlers: Vec<Arc<dyn ActionHandler>>,
40+
snapshot_notify_sender: Arc<RwLock<Option<NotifySender>>>,
3841
}
3942

4043
impl Solo {
@@ -50,6 +53,7 @@ impl Solo {
5053
params,
5154
machine,
5255
action_handlers,
56+
snapshot_notify_sender: Arc::new(RwLock::new(None)),
5357
}
5458
}
5559
}
@@ -135,6 +139,18 @@ impl ConsensusEngine for Solo {
135139
1
136140
}
137141

142+
fn register_snapshot_notify_sender(&self, sender: NotifySender) {
143+
let mut guard = self.snapshot_notify_sender.write();
144+
assert!(guard.is_none(), "snapshot_notify_sender is registered twice");
145+
*guard = Some(sender);
146+
}
147+
148+
fn send_snapshot_notify(&self, block_hash: BlockHash) {
149+
if let Some(sender) = self.snapshot_notify_sender.read().as_ref() {
150+
sender.notify(block_hash)
151+
}
152+
}
153+
138154
fn action_handlers(&self) -> &[Arc<dyn ActionHandler>] {
139155
&self.action_handlers
140156
}

core/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ pub use crate::client::Error::Database;
8989
pub use crate::client::{
9090
AccountData, AssetClient, BlockChainClient, BlockChainTrait, ChainNotify, Client, ClientConfig, DatabaseClient,
9191
EngineClient, EngineInfo, ExecuteClient, ImportBlock, MiningBlockChainClient, Shard, StateInfo, TermInfo,
92-
TestBlockChainClient, TextClient,
92+
TestBlockChainClient, TextClient, SnapshotClient
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)