ckb_sdk/rpc/
ckb.rs

1use ckb_jsonrpc_types::{
2    Alert, BannedAddr, Block, BlockEconomicState, BlockFilter, BlockNumber, BlockResponse,
3    BlockTemplate, BlockView, Capacity, CellWithStatus, ChainInfo, Consensus,
4    DaoWithdrawingCalculationKind, DeploymentsInfo, EntryCompleted, EpochNumber,
5    EpochNumberWithFraction, EpochView, EstimateCycles, ExtraLoggerConfig, FeeRateStatistics,
6    HeaderView, JsonBytes, LocalNode, MainLoggerConfig, OutPoint, OutputsValidator,
7    PoolTxDetailInfo, RawTxPool, RemoteNode, SyncState, Timestamp, Transaction,
8    TransactionAndWitnessProof, TransactionProof, TransactionWithStatusResponse, TxPoolInfo,
9    Uint32, Uint64, Version,
10};
11use ckb_types::{core::Cycle, H256};
12
13use super::{ckb_indexer::CellsCapacity, ResponseFormatGetter};
14
15pub use super::ckb_indexer::{Cell, Order, Pagination, SearchKey, Tip, Tx};
16#[cfg(not(target_arch = "wasm32"))]
17crate::jsonrpc!(pub struct CkbRpcClient {
18    // Chain
19    pub fn get_block(&self, hash: H256) -> Option<BlockView>;
20    pub fn get_block_by_number(&self, number: BlockNumber) -> Option<BlockView>;
21    pub fn get_block_hash(&self, number: BlockNumber) -> Option<H256>;
22    pub fn get_block_filter(&self, block_hash: H256) -> Option<BlockFilter>;
23    pub fn get_current_epoch(&self) -> EpochView;
24    pub fn get_epoch_by_number(&self, number: EpochNumber) -> Option<EpochView>;
25    pub fn get_header(&self, hash: H256) -> Option<HeaderView>;
26    pub fn get_header_by_number(&self, number: BlockNumber) -> Option<HeaderView>;
27    pub fn get_live_cell(&self, out_point: OutPoint, with_data: bool) -> CellWithStatus;
28    pub fn get_tip_block_number(&self) -> BlockNumber;
29    pub fn get_tip_header(&self) -> HeaderView;
30    pub fn get_transaction(&self, hash: H256) -> Option<TransactionWithStatusResponse>;
31    pub fn get_transaction_proof(
32        &self,
33        tx_hashes: Vec<H256>,
34        block_hash: Option<H256>
35    ) -> TransactionProof;
36    pub fn verify_transaction_proof(&self, tx_proof: TransactionProof) -> Vec<H256>;
37    pub fn get_transaction_and_witness_proof(&self, tx_hashes: Vec<H256>,
38        block_hash: Option<H256>) -> TransactionAndWitnessProof;
39    pub fn verify_transaction_and_witness_proof(&self, tx_proof: TransactionAndWitnessProof) -> Vec<H256>;
40    pub fn get_fork_block(&self, block_hash: H256) -> Option<BlockView>;
41    pub fn get_consensus(&self) -> Consensus;
42    pub fn get_deployments_info(&self) -> DeploymentsInfo;
43    pub fn get_block_median_time(&self, block_hash: H256) -> Option<Timestamp>;
44    pub fn get_block_economic_state(&self, block_hash: H256) -> Option<BlockEconomicState>;
45    pub fn estimate_cycles(&self, tx: Transaction)-> EstimateCycles;
46    pub fn get_fee_rate_statics(&self, target:Option<Uint64>) -> Option<FeeRateStatistics>;
47    pub fn get_fee_rate_statistics(&self, target:Option<Uint64>) -> Option<FeeRateStatistics>;
48
49    // Indexer
50    pub fn get_indexer_tip(&self) -> Option<Tip>;
51    pub fn get_cells(&self, search_key: SearchKey, order: Order, limit: Uint32, after: Option<JsonBytes>) -> Pagination<Cell>;
52    pub fn get_transactions(&self, search_key: SearchKey, order: Order, limit: Uint32, after: Option<JsonBytes>) -> Pagination<Tx>;
53    pub fn get_cells_capacity(&self, search_key: SearchKey) -> Option<CellsCapacity>;
54
55    // Net
56    pub fn get_banned_addresses(&self) -> Vec<BannedAddr>;
57    pub fn get_peers(&self) -> Vec<RemoteNode>;
58    pub fn local_node_info(&self) -> LocalNode;
59    pub fn set_ban(
60        &self,
61        address: String,
62        command: String,
63        ban_time: Option<Timestamp>,
64        absolute: Option<bool>,
65        reason: Option<String>
66    ) -> ();
67    pub fn sync_state(&self) -> SyncState;
68    pub fn set_network_active(&self, state: bool) -> ();
69    pub fn add_node(&self, peer_id: String, address: String) -> ();
70    pub fn remove_node(&self, peer_id: String) -> ();
71    pub fn clear_banned_addresses(&self) -> ();
72    pub fn ping_peers(&self) -> ();
73
74    // Pool
75    pub fn send_transaction(&self, tx: Transaction, outputs_validator: Option<OutputsValidator>) -> H256;
76    pub fn remove_transaction(&self, tx_hash: H256) -> bool;
77    pub fn tx_pool_info(&self) -> TxPoolInfo;
78    pub fn get_pool_tx_detail_info(&self, tx_hash: H256) -> PoolTxDetailInfo;
79    pub fn clear_tx_pool(&self) -> ();
80    pub fn get_raw_tx_pool(&self, verbose: Option<bool>) -> RawTxPool;
81    pub fn tx_pool_ready(&self) -> bool;
82    pub fn test_tx_pool_accept(&self, tx: Transaction, outputs_validator: Option<OutputsValidator>) -> EntryCompleted;
83    pub fn clear_tx_verify_queue(&self) -> ();
84
85    // Stats
86    pub fn get_blockchain_info(&self) -> ChainInfo;
87
88    // Miner
89    pub fn get_block_template(&self, bytes_limit: Option<Uint64>, proposals_limit: Option<Uint64>, max_version: Option<Version>) -> BlockTemplate;
90    pub fn submit_block(&self, _work_id: String, _data: Block) -> H256;
91
92    // Alert
93    pub fn send_alert(&self, alert: Alert) -> ();
94
95    // IntegrationTest
96    pub fn process_block_without_verify(&self, data: Block, broadcast: bool) -> Option<H256>;
97    pub fn truncate(&self, target_tip_hash: H256) -> ();
98    pub fn generate_block(&self) -> H256;
99    pub fn generate_epochs(&self, num_epochs: EpochNumberWithFraction) -> EpochNumberWithFraction;
100    pub fn notify_transaction(&self, tx: Transaction) -> H256;
101    pub fn calculate_dao_field(&self, block_template: BlockTemplate) -> JsonBytes;
102    pub fn generate_block_with_template(&self, block_template: BlockTemplate) -> H256;
103
104    // Debug
105    pub fn jemalloc_profiling_dump(&self) -> String;
106    pub fn update_main_logger(&self, config: MainLoggerConfig) -> ();
107    pub fn set_extra_logger(&self, name: String, config_opt: Option<ExtraLoggerConfig>) -> ();
108
109    // Experimental
110    pub fn calculate_dao_maximum_withdraw(&self, out_point: OutPoint, kind: DaoWithdrawingCalculationKind) -> Capacity;
111});
112
113crate::jsonrpc_async!(pub struct CkbRpcAsyncClient {
114    // Chain
115    pub fn get_block(&self, hash: H256) -> Option<BlockView>;
116    pub fn get_block_by_number(&self, number: BlockNumber) -> Option<BlockView>;
117    pub fn get_block_hash(&self, number: BlockNumber) -> Option<H256>;
118    pub fn get_block_filter(&self, block_hash: H256) -> Option<BlockFilter>;
119    pub fn get_current_epoch(&self) -> EpochView;
120    pub fn get_epoch_by_number(&self, number: EpochNumber) -> Option<EpochView>;
121    pub fn get_header(&self, hash: H256) -> Option<HeaderView>;
122    pub fn get_header_by_number(&self, number: BlockNumber) -> Option<HeaderView>;
123    pub fn get_live_cell(&self, out_point: OutPoint, with_data: bool) -> CellWithStatus;
124    pub fn get_tip_block_number(&self) -> BlockNumber;
125    pub fn get_tip_header(&self) -> HeaderView;
126    pub fn get_transaction(&self, hash: H256) -> Option<TransactionWithStatusResponse>;
127    pub fn get_transaction_proof(
128        &self,
129        tx_hashes: Vec<H256>,
130        block_hash: Option<H256>
131    ) -> TransactionProof;
132    pub fn verify_transaction_proof(&self, tx_proof: TransactionProof) -> Vec<H256>;
133    pub fn get_transaction_and_witness_proof(&self, tx_hashes: Vec<H256>,
134        block_hash: Option<H256>) -> TransactionAndWitnessProof;
135    pub fn verify_transaction_and_witness_proof(&self, tx_proof: TransactionAndWitnessProof) -> Vec<H256>;
136    pub fn get_fork_block(&self, block_hash: H256) -> Option<BlockView>;
137    pub fn get_consensus(&self) -> Consensus;
138    pub fn get_deployments_info(&self) -> DeploymentsInfo;
139    pub fn get_block_median_time(&self, block_hash: H256) -> Option<Timestamp>;
140    pub fn get_block_economic_state(&self, block_hash: H256) -> Option<BlockEconomicState>;
141    pub fn estimate_cycles(&self, tx: Transaction)-> EstimateCycles;
142    pub fn get_fee_rate_statics(&self, target:Option<Uint64>) -> Option<FeeRateStatistics>;
143    pub fn get_fee_rate_statistics(&self, target:Option<Uint64>) -> Option<FeeRateStatistics>;
144
145    // Indexer
146    pub fn get_indexer_tip(&self) -> Option<Tip>;
147    pub fn get_cells(&self, search_key: SearchKey, order: Order, limit: Uint32, after: Option<JsonBytes>) -> Pagination<Cell>;
148    pub fn get_transactions(&self, search_key: SearchKey, order: Order, limit: Uint32, after: Option<JsonBytes>) -> Pagination<Tx>;
149    pub fn get_cells_capacity(&self, search_key: SearchKey) -> Option<CellsCapacity>;
150
151    // Net
152    pub fn get_banned_addresses(&self) -> Vec<BannedAddr>;
153    pub fn get_peers(&self) -> Vec<RemoteNode>;
154    pub fn local_node_info(&self) -> LocalNode;
155    pub fn set_ban(
156        &self,
157        address: String,
158        command: String,
159        ban_time: Option<Timestamp>,
160        absolute: Option<bool>,
161        reason: Option<String>
162    ) -> ();
163    pub fn sync_state(&self) -> SyncState;
164    pub fn set_network_active(&self, state: bool) -> ();
165    pub fn add_node(&self, peer_id: String, address: String) -> ();
166    pub fn remove_node(&self, peer_id: String) -> ();
167    pub fn clear_banned_addresses(&self) -> ();
168    pub fn ping_peers(&self) -> ();
169
170    // Pool
171    pub fn send_transaction(&self, tx: Transaction, outputs_validator: Option<OutputsValidator>) -> H256;
172    pub fn remove_transaction(&self, tx_hash: H256) -> bool;
173    pub fn tx_pool_info(&self) -> TxPoolInfo;
174    pub fn get_pool_tx_detail_info(&self, tx_hash: H256) -> PoolTxDetailInfo;
175    pub fn clear_tx_pool(&self) -> ();
176    pub fn get_raw_tx_pool(&self, verbose: Option<bool>) -> RawTxPool;
177    pub fn tx_pool_ready(&self) -> bool;
178    pub fn test_tx_pool_accept(&self, tx: Transaction, outputs_validator: Option<OutputsValidator>) -> EntryCompleted;
179    pub fn clear_tx_verify_queue(&self) -> ();
180
181    // Stats
182    pub fn get_blockchain_info(&self) -> ChainInfo;
183
184    // Miner
185    pub fn get_block_template(&self, bytes_limit: Option<Uint64>, proposals_limit: Option<Uint64>, max_version: Option<Version>) -> BlockTemplate;
186    pub fn submit_block(&self, _work_id: String, _data: Block) -> H256;
187
188    // Alert
189    pub fn send_alert(&self, alert: Alert) -> ();
190
191    // IntegrationTest
192    pub fn process_block_without_verify(&self, data: Block, broadcast: bool) -> Option<H256>;
193    pub fn truncate(&self, target_tip_hash: H256) -> ();
194    pub fn generate_block(&self) -> H256;
195    pub fn generate_epochs(&self, num_epochs: EpochNumberWithFraction) -> EpochNumberWithFraction;
196    pub fn notify_transaction(&self, tx: Transaction) -> H256;
197    pub fn calculate_dao_field(&self, block_template: BlockTemplate) -> JsonBytes;
198    pub fn generate_block_with_template(&self, block_template: BlockTemplate) -> H256;
199
200    // Debug
201    pub fn jemalloc_profiling_dump(&self) -> String;
202    pub fn update_main_logger(&self, config: MainLoggerConfig) -> ();
203    pub fn set_extra_logger(&self, name: String, config_opt: Option<ExtraLoggerConfig>) -> ();
204
205    // Experimental
206    pub fn calculate_dao_maximum_withdraw(&self, out_point: OutPoint, kind: DaoWithdrawingCalculationKind) -> Capacity;
207});
208
209fn transform_cycles(cycles: Option<Vec<ckb_jsonrpc_types::Cycle>>) -> Vec<Cycle> {
210    cycles
211        .map(|c| c.into_iter().map(Into::into).collect())
212        .unwrap_or_default()
213}
214#[cfg(not(target_arch = "wasm32"))]
215impl From<&CkbRpcClient> for CkbRpcAsyncClient {
216    fn from(value: &CkbRpcClient) -> Self {
217        Self {
218            client: value.client.clone(),
219            id: 0.into(),
220        }
221    }
222}
223#[cfg(not(target_arch = "wasm32"))]
224impl CkbRpcClient {
225    pub fn get_packed_block(&self, hash: H256) -> Result<Option<JsonBytes>, crate::RpcError> {
226        self.post("get_block", (hash, Some(Uint32::from(0u32))))
227    }
228
229    /// Same as get_block except with parameter with_cycles and return BlockResponse
230    pub fn get_block_with_cycles(
231        &self,
232        hash: H256,
233    ) -> Result<Option<(BlockView, Vec<Cycle>)>, crate::rpc::RpcError> {
234        let res = self.post::<_, Option<BlockResponse>>("get_block", (hash, None::<u32>, true))?;
235        transform_block_view_with_cycle(res)
236    }
237
238    pub fn get_packed_block_with_cycles(
239        &self,
240        hash: H256,
241    ) -> Result<Option<(JsonBytes, Vec<Cycle>)>, crate::rpc::RpcError> {
242        let res = self.post::<_, Option<BlockResponse>>(
243            "get_block",
244            (hash, Some(Uint32::from(0u32)), true),
245        )?;
246        blockresponse2bytes(res)
247    }
248
249    /// Same as get_block_by_number except with parameter with_cycles and return BlockResponse
250    pub fn get_packed_block_by_number(
251        &self,
252        number: BlockNumber,
253    ) -> Result<Option<JsonBytes>, crate::rpc::RpcError> {
254        self.post("get_block_by_number", (number, Some(Uint32::from(0u32))))
255    }
256
257    pub fn get_block_by_number_with_cycles(
258        &self,
259        number: BlockNumber,
260    ) -> Result<Option<(BlockView, Vec<Cycle>)>, crate::rpc::RpcError> {
261        let res = self
262            .post::<_, Option<BlockResponse>>("get_block_by_number", (number, None::<u32>, true))?;
263        transform_block_view_with_cycle(res)
264    }
265
266    pub fn get_packed_block_by_number_with_cycles(
267        &self,
268        number: BlockNumber,
269    ) -> Result<Option<(JsonBytes, Vec<Cycle>)>, crate::rpc::RpcError> {
270        let res = self.post::<_, Option<BlockResponse>>(
271            "get_block_by_number",
272            (number, Some(Uint32::from(0u32)), true),
273        )?;
274        blockresponse2bytes(res)
275    }
276
277    pub fn get_packed_header(&self, hash: H256) -> Result<Option<JsonBytes>, crate::rpc::RpcError> {
278        self.post::<_, Option<JsonBytes>>("get_header", (hash, Some(Uint32::from(0u32))))
279    }
280
281    pub fn get_packed_header_by_number(
282        &self,
283        number: BlockNumber,
284    ) -> Result<Option<JsonBytes>, crate::rpc::RpcError> {
285        self.post::<_, Option<JsonBytes>>(
286            "get_header_by_number",
287            (number, Some(Uint32::from(0u32))),
288        )
289    }
290
291    pub fn get_live_cell_with_include_tx_pool(
292        &self,
293        out_point: OutPoint,
294        with_data: bool,
295        include_tx_pool: bool,
296    ) -> Result<CellWithStatus, crate::rpc::RpcError> {
297        self.post::<_, CellWithStatus>(
298            "get_live_cell",
299            (out_point, with_data, Some(include_tx_pool)),
300        )
301    }
302
303    // get transaction with only_committed=true
304    pub fn get_only_committed_transaction(
305        &self,
306        hash: H256,
307    ) -> Result<TransactionWithStatusResponse, crate::rpc::RpcError> {
308        self.post::<_, TransactionWithStatusResponse>(
309            "get_transaction",
310            (hash, Some(Uint32::from(2u32)), true),
311        )
312    }
313
314    // get transaction with verbosity=0
315    pub fn get_packed_transaction(
316        &self,
317        hash: H256,
318    ) -> Result<TransactionWithStatusResponse, crate::rpc::RpcError> {
319        self.post::<_, TransactionWithStatusResponse>(
320            "get_transaction",
321            (hash, Some(Uint32::from(0u32))),
322        )
323    }
324
325    // get transaction with verbosity=0 and only_committed=true
326    pub fn get_only_committed_packed_transaction(
327        &self,
328        hash: H256,
329    ) -> Result<TransactionWithStatusResponse, crate::rpc::RpcError> {
330        self.post::<_, TransactionWithStatusResponse>(
331            "get_transaction",
332            (hash, Some(Uint32::from(0u32)), true),
333        )
334    }
335
336    // get transaction with verbosity=1, so the result transaction field is None
337    pub fn get_transaction_status(
338        &self,
339        hash: H256,
340    ) -> Result<TransactionWithStatusResponse, crate::rpc::RpcError> {
341        self.post::<_, TransactionWithStatusResponse>(
342            "get_transaction",
343            (hash, Some(Uint32::from(1u32))),
344        )
345    }
346
347    // get transaction with verbosity=1 and only_committed=true, so the result transaction field is None
348    pub fn get_only_committed_transaction_status(
349        &self,
350        hash: H256,
351    ) -> Result<TransactionWithStatusResponse, crate::rpc::RpcError> {
352        self.post::<_, TransactionWithStatusResponse>(
353            "get_transaction",
354            (hash, Some(Uint32::from(1u32)), true),
355        )
356    }
357
358    pub fn get_packed_tip_header(&self) -> Result<JsonBytes, crate::rpc::RpcError> {
359        self.post::<_, JsonBytes>("get_tip_header", (Some(Uint32::from(0u32)),))
360    }
361
362    pub fn get_packed_fork_block(
363        &self,
364        block_hash: H256,
365    ) -> Result<Option<JsonBytes>, crate::rpc::RpcError> {
366        self.post::<_, Option<JsonBytes>>("get_fork_block", (block_hash, Some(Uint32::from(0u32))))
367    }
368}
369
370impl CkbRpcAsyncClient {
371    pub async fn get_packed_block(&self, hash: H256) -> Result<Option<JsonBytes>, crate::RpcError> {
372        self.post("get_block", (hash, Some(Uint32::from(0u32))))
373            .await
374    }
375
376    /// Same as get_block except with parameter with_cycles and return BlockResponse
377    pub async fn get_block_with_cycles(
378        &self,
379        hash: H256,
380    ) -> Result<Option<(BlockView, Vec<Cycle>)>, crate::rpc::RpcError> {
381        let res = self
382            .post::<_, Option<BlockResponse>>("get_block", (hash, None::<u32>, true))
383            .await?;
384        transform_block_view_with_cycle(res)
385    }
386
387    pub async fn get_packed_block_with_cycles(
388        &self,
389        hash: H256,
390    ) -> Result<Option<(JsonBytes, Vec<Cycle>)>, crate::rpc::RpcError> {
391        let res = self
392            .post::<_, Option<BlockResponse>>("get_block", (hash, Some(Uint32::from(0u32)), true))
393            .await?;
394        blockresponse2bytes(res)
395    }
396
397    /// Same as get_block_by_number except with parameter with_cycles and return BlockResponse
398    pub async fn get_packed_block_by_number(
399        &self,
400        number: BlockNumber,
401    ) -> Result<Option<JsonBytes>, crate::rpc::RpcError> {
402        self.post("get_block_by_number", (number, Some(Uint32::from(0u32))))
403            .await
404    }
405
406    pub async fn get_block_by_number_with_cycles(
407        &self,
408        number: BlockNumber,
409    ) -> Result<Option<(BlockView, Vec<Cycle>)>, crate::rpc::RpcError> {
410        let res = self
411            .post::<_, Option<BlockResponse>>("get_block_by_number", (number, None::<u32>, true))
412            .await?;
413        transform_block_view_with_cycle(res)
414    }
415
416    pub async fn get_packed_block_by_number_with_cycles(
417        &self,
418        number: BlockNumber,
419    ) -> Result<Option<(JsonBytes, Vec<Cycle>)>, crate::rpc::RpcError> {
420        let res = self
421            .post::<_, Option<BlockResponse>>(
422                "get_block_by_number",
423                (number, Some(Uint32::from(0u32)), true),
424            )
425            .await?;
426        blockresponse2bytes(res)
427    }
428
429    pub async fn get_packed_header(
430        &self,
431        hash: H256,
432    ) -> Result<Option<JsonBytes>, crate::rpc::RpcError> {
433        self.post::<_, Option<JsonBytes>>("get_header", (hash, Some(Uint32::from(0u32))))
434            .await
435    }
436
437    pub async fn get_packed_header_by_number(
438        &self,
439        number: BlockNumber,
440    ) -> Result<Option<JsonBytes>, crate::rpc::RpcError> {
441        self.post::<_, Option<JsonBytes>>(
442            "get_header_by_number",
443            (number, Some(Uint32::from(0u32))),
444        )
445        .await
446    }
447
448    pub async fn get_live_cell_with_include_tx_pool(
449        &self,
450        out_point: OutPoint,
451        with_data: bool,
452        include_tx_pool: bool,
453    ) -> Result<CellWithStatus, crate::rpc::RpcError> {
454        self.post::<_, CellWithStatus>(
455            "get_live_cell",
456            (out_point, with_data, Some(include_tx_pool)),
457        )
458        .await
459    }
460
461    // get transaction with only_committed=true
462    pub async fn get_only_committed_transaction(
463        &self,
464        hash: H256,
465    ) -> Result<TransactionWithStatusResponse, crate::rpc::RpcError> {
466        self.post::<_, TransactionWithStatusResponse>(
467            "get_transaction",
468            (hash, Some(Uint32::from(2u32)), true),
469        )
470        .await
471    }
472
473    // get transaction with verbosity=0
474    pub async fn get_packed_transaction(
475        &self,
476        hash: H256,
477    ) -> Result<TransactionWithStatusResponse, crate::rpc::RpcError> {
478        self.post::<_, TransactionWithStatusResponse>(
479            "get_transaction",
480            (hash, Some(Uint32::from(0u32))),
481        )
482        .await
483    }
484
485    // get transaction with verbosity=0 and only_committed=true
486    pub async fn get_only_committed_packed_transaction(
487        &self,
488        hash: H256,
489    ) -> Result<TransactionWithStatusResponse, crate::rpc::RpcError> {
490        self.post::<_, TransactionWithStatusResponse>(
491            "get_transaction",
492            (hash, Some(Uint32::from(0u32)), true),
493        )
494        .await
495    }
496
497    // get transaction with verbosity=1, so the result transaction field is None
498    pub async fn get_transaction_status(
499        &self,
500        hash: H256,
501    ) -> Result<TransactionWithStatusResponse, crate::rpc::RpcError> {
502        self.post::<_, TransactionWithStatusResponse>(
503            "get_transaction",
504            (hash, Some(Uint32::from(1u32))),
505        )
506        .await
507    }
508
509    // get transaction with verbosity=1 and only_committed=true, so the result transaction field is None
510    pub async fn get_only_committed_transaction_status(
511        &self,
512        hash: H256,
513    ) -> Result<TransactionWithStatusResponse, crate::rpc::RpcError> {
514        self.post::<_, TransactionWithStatusResponse>(
515            "get_transaction",
516            (hash, Some(Uint32::from(1u32)), true),
517        )
518        .await
519    }
520
521    pub async fn get_packed_tip_header(&self) -> Result<JsonBytes, crate::rpc::RpcError> {
522        self.post::<_, JsonBytes>("get_tip_header", (Some(Uint32::from(0u32)),))
523            .await
524    }
525
526    pub async fn get_packed_fork_block(
527        &self,
528        block_hash: H256,
529    ) -> Result<Option<JsonBytes>, crate::rpc::RpcError> {
530        self.post::<_, Option<JsonBytes>>("get_fork_block", (block_hash, Some(Uint32::from(0u32))))
531            .await
532    }
533}
534
535// turn BlockResponse to JsonBytes and Cycle tuple
536fn blockresponse2bytes(
537    opt_resp: Option<BlockResponse>,
538) -> Result<Option<(JsonBytes, Vec<Cycle>)>, crate::rpc::RpcError> {
539    opt_resp
540        .map(|resp| match resp {
541            BlockResponse::Regular(block_view) => Ok((block_view.get_json_bytes()?, vec![])),
542            BlockResponse::WithCycles(block_cycles) => {
543                let cycles = transform_cycles(block_cycles.cycles);
544                Ok((block_cycles.block.get_json_bytes()?, cycles))
545            }
546        })
547        .transpose()
548}
549
550// turn block response into BlockView and cycle vec
551fn transform_block_view_with_cycle(
552    opt_resp: Option<BlockResponse>,
553) -> Result<Option<(BlockView, Vec<Cycle>)>, crate::rpc::RpcError> {
554    opt_resp
555        .map(|resp| match resp {
556            BlockResponse::Regular(block_view) => Ok((block_view.get_value()?, vec![])),
557            BlockResponse::WithCycles(block_cycles) => {
558                let cycles = transform_cycles(block_cycles.cycles);
559                Ok((block_cycles.block.get_value()?, cycles))
560            }
561        })
562        .transpose()
563}