ckb-testkit 0.0.1

ckb testkit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
mod id_generator;
#[macro_use]
mod macros;
mod error;
mod v2019;
mod v2021;

use ckb_error::AnyError;
// TODO replace json types with core types
use ckb_jsonrpc_types::{
    Alert, BannedAddr, Block, BlockTemplate, BlockView, CellWithStatus, ChainInfo, Consensus,
    DryRunResult, EpochView, HeaderView, LocalNode, OutPoint, RawTxPool, RemoteNode, Timestamp,
    Transaction, TransactionWithStatus, TxPoolInfo,
};
use ckb_types::core::{
    BlockNumber as CoreBlockNumber, Capacity as CoreCapacity, EpochNumber as CoreEpochNumber,
    Version as CoreVersion,
};
use ckb_types::{packed::Byte32, prelude::*};
use lazy_static::lazy_static;
use v2019::Inner2019;
use v2021::Inner2021;

lazy_static! {
    pub static ref HTTP_CLIENT: reqwest::blocking::Client = reqwest::blocking::Client::builder()
        .timeout(::std::time::Duration::from_secs(30))
        .build()
        .expect("reqwest Client build");
}

macro_rules! item2019_to_item2021 {
    ($item2019:expr) => {{
        let raw2019 = serde_json::to_string(&$item2019).unwrap();
        let raw2021 = raw2019
            // interfaces that includes block header
            .replace("uncles_hash", "extra_hash")
            // get_consensus
            .replace(
                "\"permanent_difficulty_in_dummy\":",
                "\"hardfork_features\":[],\"permanent_difficulty_in_dummy\":",
            );
        serde_json::from_str(&raw2021).unwrap()
    }};
}

macro_rules! item2021_to_item2019 {
    ($item2021:expr) => {{
        let raw2021 = serde_json::to_string(&$item2021).unwrap();
        let raw2019 = raw2021.replace("extra_hash", "uncles_hash");
        serde_json::from_str(&raw2019).unwrap()
    }};
}

pub struct RpcClient {
    pub ckb2021: bool,
    inner2019: Inner2019,
    inner2021: Inner2021,
}

impl Clone for RpcClient {
    fn clone(&self) -> RpcClient {
        RpcClient::new(self.inner2021.url.as_str(), self.ckb2021)
    }
}

impl RpcClient {
    pub fn new(uri: &str, ckb2021: bool) -> Self {
        Self {
            inner2019: Inner2019::new(uri),
            inner2021: Inner2021::new(uri),
            ckb2021,
        }
    }

    pub fn url(&self) -> &str {
        self.inner2021.url.as_ref()
    }

    pub fn inner(&self) -> &Inner2021 {
        &self.inner2021
    }

    pub fn get_block(&self, hash: Byte32) -> Option<BlockView> {
        if self.ckb2021 {
            self.inner2021
                .get_block(hash.unpack())
                .expect("rpc call get_block")
        } else {
            item2019_to_item2021!(self
                .inner2019
                .get_block(hash.unpack())
                .expect("rpc call get_block"))
        }
    }

    pub fn get_fork_block(&self, hash: Byte32) -> Option<BlockView> {
        if self.ckb2021 {
            self.inner2021
                .get_fork_block(hash.unpack())
                .expect("rpc call get_fork_block")
        } else {
            item2019_to_item2021!(self
                .inner2019
                .get_fork_block(hash.unpack())
                .expect("rpc call get_fork_block"))
        }
    }

    pub fn get_block_by_number(&self, number: CoreBlockNumber) -> Option<BlockView> {
        if self.ckb2021 {
            self.inner2021
                .get_block_by_number(number.into())
                .expect("rpc call get_block_by_number")
        } else {
            item2019_to_item2021!(self
                .inner2019
                .get_block_by_number(number.into())
                .expect("rpc call get_block_by_number"))
        }
    }

    pub fn get_header(&self, hash: Byte32) -> Option<HeaderView> {
        if self.ckb2021 {
            self.inner2021
                .get_header(hash.unpack())
                .expect("rpc call get_header")
        } else {
            item2019_to_item2021!(self
                .inner2019
                .get_header(hash.unpack())
                .expect("rpc call get_header"))
        }
    }

    pub fn get_header_by_number(&self, number: CoreBlockNumber) -> Option<HeaderView> {
        if self.ckb2021 {
            self.inner2021
                .get_header_by_number(number.into())
                .expect("rpc call get_header_by_number")
        } else {
            item2019_to_item2021!(self
                .inner2019
                .get_header_by_number(number.into())
                .expect("rpc call get_header_by_number"))
        }
    }

    pub fn get_transaction(&self, hash: Byte32) -> Option<TransactionWithStatus> {
        if self.ckb2021 {
            self.inner2021
                .get_transaction(hash.unpack())
                .expect("rpc call get_transaction")
        } else {
            item2019_to_item2021!(self
                .inner2019
                .get_transaction(hash.unpack())
                .expect("rpc call get_transaction"))
        }
    }

    pub fn get_block_hash(&self, number: CoreBlockNumber) -> Option<Byte32> {
        self.inner()
            .get_block_hash(number.into())
            .expect("rpc call get_block_hash")
            .map(|x| x.pack())
    }

    pub fn get_tip_header(&self) -> HeaderView {
        if self.ckb2021 {
            self.inner2021
                .get_tip_header()
                .expect("rpc call get_block_hash")
        } else {
            item2019_to_item2021!(self
                .inner2019
                .get_tip_header()
                .expect("rpc call get_block_hash"))
        }
    }

    pub fn get_live_cell(&self, out_point: OutPoint, with_data: bool) -> CellWithStatus {
        if self.ckb2021 {
            self.inner2021
                .get_live_cell(out_point, with_data)
                .expect("rpc call get_live_cell")
        } else {
            let out_point = item2019_to_item2021!(out_point);
            item2019_to_item2021!(self
                .inner2019
                .get_live_cell(out_point, with_data)
                .expect("rpc call get_live_cell"))
        }
    }

    pub fn get_tip_block_number(&self) -> CoreBlockNumber {
        self.inner()
            .get_tip_block_number()
            .expect("rpc call get_tip_block_number")
            .into()
    }

    pub fn get_current_epoch(&self) -> EpochView {
        self.inner()
            .get_current_epoch()
            .expect("rpc call get_current_epoch")
    }

    pub fn get_epoch_by_number(&self, number: CoreEpochNumber) -> Option<EpochView> {
        self.inner()
            .get_epoch_by_number(number.into())
            .expect("rpc call get_epoch_by_number")
    }

    pub fn get_consensus(&self) -> Consensus {
        if self.ckb2021 {
            self.inner2021
                .get_consensus()
                .expect("rpc call get_consensus")
        } else {
            item2019_to_item2021!(self
                .inner2019
                .get_consensus()
                .expect("rpc call get_consensus"))
        }
    }

    pub fn local_node_info(&self) -> LocalNode {
        self.inner()
            .local_node_info()
            .expect("rpc call local_node_info")
    }

    pub fn get_peers(&self) -> Vec<RemoteNode> {
        self.inner().get_peers().expect("rpc call get_peers")
    }

    pub fn get_banned_addresses(&self) -> Vec<BannedAddr> {
        self.inner()
            .get_banned_addresses()
            .expect("rpc call get_banned_addresses")
    }

    pub fn set_ban(
        &self,
        address: String,
        command: String,
        ban_time: Option<Timestamp>,
        absolute: Option<bool>,
        reason: Option<String>,
    ) {
        self.inner()
            .set_ban(address, command, ban_time, absolute, reason)
            .expect("rpc call set_ban")
    }

    pub fn get_block_template(
        &self,
        bytes_limit: Option<u64>,
        proposals_limit: Option<u64>,
        max_version: Option<CoreVersion>,
    ) -> BlockTemplate {
        if self.ckb2021 {
            let bytes_limit = bytes_limit.map(Into::into);
            let proposals_limit = proposals_limit.map(Into::into);
            let max_version = max_version.map(Into::into);
            self.inner2021
                .get_block_template(bytes_limit, proposals_limit, max_version)
                .expect("rpc call get_block_template2021")
        } else {
            let bytes_limit = bytes_limit.map(Into::into);
            let proposals_limit = proposals_limit.map(Into::into);
            let max_version = max_version.map(Into::into);
            item2019_to_item2021!(self
                .inner2019
                .get_block_template(bytes_limit, proposals_limit, max_version)
                .expect("rpc call get_block_template2019"))
        }
    }

    pub fn submit_block(&self, work_id: String, block: Block) -> Result<Byte32, AnyError> {
        if self.ckb2021 {
            self.inner2021
                .submit_block(work_id, block)
                .map(|x| x.pack())
        } else {
            let block2019 = item2021_to_item2019!(&block);
            self.inner2019
                .submit_block(work_id, block2019)
                .map(|x| x.pack())
        }
    }

    pub fn get_blockchain_info(&self) -> ChainInfo {
        self.inner()
            .get_blockchain_info()
            .expect("rpc call get_blockchain_info")
    }

    pub fn get_block_median_time(&self, block_hash: Byte32) -> Option<Timestamp> {
        self.inner()
            .get_block_median_time(block_hash.unpack())
            .expect("rpc call get_block_median_time")
    }

    pub fn send_transaction(&self, tx: Transaction) -> Byte32 {
        self.send_transaction_result(tx)
            .expect("rpc call send_transaction")
    }

    pub fn send_transaction_result(&self, tx: Transaction) -> Result<Byte32, AnyError> {
        if self.ckb2021 {
            self.inner2021
                .send_transaction(tx, Some("passthrough".to_string()))
                .map(|h256| h256.pack())
        } else {
            let tx = item2021_to_item2019!(tx);
            self.inner2019
                .send_transaction(tx, Some("passthrough".to_string()))
                .map(|h256| h256.pack())
        }
    }

    pub fn dry_run_transaction(&self, tx: Transaction) -> DryRunResult {
        if self.ckb2021 {
            self.inner2021
                .dry_run_transaction(tx)
                .expect("rpc call dry_run_transaction")
        } else {
            let tx = item2019_to_item2021!(tx);
            item2019_to_item2021!(self
                .inner2019
                .dry_run_transaction(tx)
                .expect("rpc call dry_run_transaction"))
        }
    }

    pub fn send_alert(&self, alert: Alert) {
        self.inner().send_alert(alert).expect("rpc call send_alert")
    }

    pub fn tx_pool_info(&self) -> TxPoolInfo {
        self.inner().tx_pool_info().expect("rpc call tx_pool_info")
    }

    pub fn add_node(&self, peer_id: String, address: String) {
        self.inner()
            .add_node(peer_id, address)
            .expect("rpc call add_node");
    }

    pub fn remove_node(&self, peer_id: String) {
        self.inner()
            .remove_node(peer_id)
            .expect("rpc call remove_node")
    }

    pub fn truncate(&self, target_tip_hash: Byte32) {
        self.inner()
            .truncate(target_tip_hash.unpack())
            .expect("rpc call truncate")
    }

    pub fn calculate_dao_maximum_withdraw(
        &self,
        out_point: OutPoint,
        hash: Byte32,
    ) -> CoreCapacity {
        self.inner()
            .calculate_dao_maximum_withdraw(out_point, hash.unpack())
            .expect("rpc call calculate_dao_maximum_withdraw")
            .into()
    }

    pub fn process_block_without_verify(
        &self,
        block: Block,
        should_broadcast: bool,
    ) -> Option<Byte32> {
        if self.ckb2021 {
            self.inner2021
                .process_block_without_verify(block, should_broadcast)
                .expect("rpc call process_block_without_verify")
                .map(|h256| h256.pack())
        } else {
            let block = item2021_to_item2019!(block);
            self.inner2019
                .process_block_without_verify(block, should_broadcast)
                .expect("rpc call process_block_without_verify")
                .map(|h256| h256.pack())
        }
    }

    pub fn calculate_dao_field(&self, block_template: BlockTemplate) -> Result<Byte32, AnyError> {
        assert!(self.ckb2021);
        self.inner2021
            .calculate_dao_field(block_template)
            .map(Into::into)
    }

    pub fn get_raw_tx_pool(&self, verbose: Option<bool>) -> Result<RawTxPool, AnyError> {
        assert!(self.ckb2021);
        self.inner2021.get_raw_tx_pool(verbose)
    }
}