ckb_sdk/traits/
dummy_impls.rs

1use ckb_types::{
2    bytes::Bytes,
3    core::{HeaderView, TransactionView},
4    packed::{Byte32, CellOutput, OutPoint, Transaction},
5};
6
7use crate::traits::{
8    CellCollector, CellCollectorError, CellQueryOptions, HeaderDepResolver, LiveCell,
9    TransactionDependencyError, TransactionDependencyProvider,
10};
11use anyhow::anyhow;
12
13/// A dummy CellCollector. All methods will return error if possible.
14#[derive(Clone, Default)]
15pub struct DummyCellCollector;
16
17#[cfg_attr(target_arch="wasm32", async_trait::async_trait(?Send))]
18#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
19impl CellCollector for DummyCellCollector {
20    async fn collect_live_cells_async(
21        &mut self,
22        _query: &CellQueryOptions,
23        _apply_changes: bool,
24    ) -> Result<(Vec<LiveCell>, u64), CellCollectorError> {
25        Err(CellCollectorError::Other(anyhow!(
26            "dummy collect_live_cells"
27        )))
28    }
29
30    fn lock_cell(
31        &mut self,
32        _out_point: OutPoint,
33        _tip_block_num: u64,
34    ) -> Result<(), CellCollectorError> {
35        Err(CellCollectorError::Other(anyhow!("dummy lock_cell")))
36    }
37
38    fn apply_tx(
39        &mut self,
40        _tx: Transaction,
41        _tip_block_num: u64,
42    ) -> Result<(), CellCollectorError> {
43        Err(CellCollectorError::Other(anyhow!("dummy apply_tx")))
44    }
45    fn reset(&mut self) {}
46}
47
48/// A dummy HeaderDepResolver. All methods will return error if possible.
49#[derive(Default)]
50pub struct DummyHeaderDepResolver;
51
52#[cfg_attr(target_arch="wasm32", async_trait::async_trait(?Send))]
53#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
54impl HeaderDepResolver for DummyHeaderDepResolver {
55    async fn resolve_by_tx_async(
56        &self,
57        _tx_hash: &Byte32,
58    ) -> Result<Option<HeaderView>, anyhow::Error> {
59        Err(anyhow!("dummy resolve_by_tx"))
60    }
61    async fn resolve_by_number_async(
62        &self,
63        _number: u64,
64    ) -> Result<Option<HeaderView>, anyhow::Error> {
65        Err(anyhow!("dummy resolve_by_number"))
66    }
67}
68
69/// A dummy HeaderDepResolver. All methods will return error if possible.
70#[derive(Default)]
71pub struct DummyTransactionDependencyProvider;
72
73#[cfg_attr(target_arch="wasm32", async_trait::async_trait(?Send))]
74#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
75impl TransactionDependencyProvider for DummyTransactionDependencyProvider {
76    // For verify certain cell belong to certain transaction
77    async fn get_transaction_async(
78        &self,
79        _tx_hash: &Byte32,
80    ) -> Result<TransactionView, TransactionDependencyError> {
81        Err(TransactionDependencyError::Other(anyhow!(
82            "dummy get_transaction"
83        )))
84    }
85    // For get the output information of inputs or cell_deps, those cell should be live cell
86    async fn get_cell_async(
87        &self,
88        _out_point: &OutPoint,
89    ) -> Result<CellOutput, TransactionDependencyError> {
90        Err(TransactionDependencyError::Other(anyhow!("dummy get_cell")))
91    }
92    // For get the output data information of inputs or cell_deps
93    async fn get_cell_data_async(
94        &self,
95        _out_point: &OutPoint,
96    ) -> Result<Bytes, TransactionDependencyError> {
97        Err(TransactionDependencyError::Other(anyhow!(
98            "dummy get_cell_data"
99        )))
100    }
101    // For get the header information of header_deps
102    async fn get_header_async(
103        &self,
104        _block_hash: &Byte32,
105    ) -> Result<HeaderView, TransactionDependencyError> {
106        Err(TransactionDependencyError::Other(anyhow!(
107            "dummy get_header"
108        )))
109    }
110    async fn get_block_extension_async(
111        &self,
112        _block_hash: &Byte32,
113    ) -> Result<Option<ckb_types::packed::Bytes>, TransactionDependencyError> {
114        Err(TransactionDependencyError::Other(anyhow!(
115            "dummy get_block_extension"
116        )))
117    }
118}