-
Notifications
You must be signed in to change notification settings - Fork 29
/
ckb.rs
277 lines (248 loc) · 11.5 KB
/
ckb.rs
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
use ckb_jsonrpc_types::{
Alert, BannedAddr, Block, BlockEconomicState, BlockFilter, BlockNumber, BlockResponse,
BlockTemplate, BlockView, Capacity, CellWithStatus, ChainInfo, Consensus,
DaoWithdrawingCalculationKind, DeploymentsInfo, EpochNumber, EpochNumberWithFraction,
EpochView, EstimateCycles, ExtraLoggerConfig, FeeRateStatistics, HeaderView, JsonBytes,
LocalNode, MainLoggerConfig, OutPoint, OutputsValidator, PoolTxDetailInfo, RawTxPool,
RemoteNode, SyncState, Timestamp, Transaction, TransactionAndWitnessProof, TransactionProof,
TransactionWithStatusResponse, TxPoolInfo, Uint32, Uint64, Version,
};
use ckb_types::{core::Cycle, H256};
use super::{ckb_indexer::CellsCapacity, ResponseFormatGetter};
pub use super::ckb_indexer::{Cell, Order, Pagination, SearchKey, Tip, Tx};
crate::jsonrpc!(pub struct CkbRpcClient {
// Chain
pub fn get_block(&self, hash: H256) -> Option<BlockView>;
pub fn get_block_by_number(&self, number: BlockNumber) -> Option<BlockView>;
pub fn get_block_hash(&self, number: BlockNumber) -> Option<H256>;
pub fn get_block_filter(&self, block_hash: H256) -> Option<BlockFilter>;
pub fn get_current_epoch(&self) -> EpochView;
pub fn get_epoch_by_number(&self, number: EpochNumber) -> Option<EpochView>;
pub fn get_header(&self, hash: H256) -> Option<HeaderView>;
pub fn get_header_by_number(&self, number: BlockNumber) -> Option<HeaderView>;
pub fn get_live_cell(&self, out_point: OutPoint, with_data: bool) -> CellWithStatus;
pub fn get_tip_block_number(&self) -> BlockNumber;
pub fn get_tip_header(&self) -> HeaderView;
pub fn get_transaction(&self, hash: H256) -> Option<TransactionWithStatusResponse>;
pub fn get_transaction_proof(
&self,
tx_hashes: Vec<H256>,
block_hash: Option<H256>
) -> TransactionProof;
pub fn verify_transaction_proof(&self, tx_proof: TransactionProof) -> Vec<H256>;
pub fn get_transaction_and_witness_proof(&self, tx_hashes: Vec<H256>,
block_hash: Option<H256>) -> TransactionAndWitnessProof;
pub fn verify_transaction_and_witness_proof(&self, tx_proof: TransactionAndWitnessProof) -> Vec<H256>;
pub fn get_fork_block(&self, block_hash: H256) -> Option<BlockView>;
pub fn get_consensus(&self) -> Consensus;
pub fn get_deployments_info(&self) -> DeploymentsInfo;
pub fn get_block_median_time(&self, block_hash: H256) -> Option<Timestamp>;
pub fn get_block_economic_state(&self, block_hash: H256) -> Option<BlockEconomicState>;
pub fn estimate_cycles(&self, tx: Transaction)-> EstimateCycles;
pub fn get_fee_rate_statics(&self, target:Option<Uint64>) -> Option<FeeRateStatistics>;
pub fn get_fee_rate_statistics(&self, target:Option<Uint64>) -> Option<FeeRateStatistics>;
// Indexer
pub fn get_indexer_tip(&self) -> Option<Tip>;
pub fn get_cells(&self, search_key: SearchKey, order: Order, limit: Uint32, after: Option<JsonBytes>) -> Pagination<Cell>;
pub fn get_transactions(&self, search_key: SearchKey, order: Order, limit: Uint32, after: Option<JsonBytes>) -> Pagination<Tx>;
pub fn get_cells_capacity(&self, search_key: SearchKey) -> Option<CellsCapacity>;
// Net
pub fn get_banned_addresses(&self) -> Vec<BannedAddr>;
pub fn get_peers(&self) -> Vec<RemoteNode>;
pub fn local_node_info(&self) -> LocalNode;
pub fn set_ban(
&self,
address: String,
command: String,
ban_time: Option<Timestamp>,
absolute: Option<bool>,
reason: Option<String>
) -> ();
pub fn sync_state(&self) -> SyncState;
pub fn set_network_active(&self, state: bool) -> ();
pub fn add_node(&self, peer_id: String, address: String) -> ();
pub fn remove_node(&self, peer_id: String) -> ();
pub fn clear_banned_addresses(&self) -> ();
pub fn ping_peers(&self) -> ();
// Pool
pub fn send_transaction(&self, tx: Transaction, outputs_validator: Option<OutputsValidator>) -> H256;
pub fn remove_transaction(&self, tx_hash: H256) -> bool;
pub fn tx_pool_info(&self) -> TxPoolInfo;
pub fn get_pool_tx_detail_info(&self, tx_hash: H256) -> PoolTxDetailInfo;
pub fn clear_tx_pool(&self) -> ();
pub fn get_raw_tx_pool(&self, verbose: Option<bool>) -> RawTxPool;
pub fn tx_pool_ready(&self) -> bool;
// Stats
pub fn get_blockchain_info(&self) -> ChainInfo;
// Miner
pub fn get_block_template(&self, bytes_limit: Option<Uint64>, proposals_limit: Option<Uint64>, max_version: Option<Version>) -> BlockTemplate;
pub fn submit_block(&self, _work_id: String, _data: Block) -> H256;
// Alert
pub fn send_alert(&self, alert: Alert) -> ();
// IntegrationTest
pub fn process_block_without_verify(&self, data: Block, broadcast: bool) -> Option<H256>;
pub fn truncate(&self, target_tip_hash: H256) -> ();
pub fn generate_block(&self) -> H256;
pub fn generate_epochs(&self, num_epochs: EpochNumberWithFraction) -> EpochNumberWithFraction;
pub fn notify_transaction(&self, tx: Transaction) -> H256;
pub fn calculate_dao_field(&self, block_template: BlockTemplate) -> JsonBytes;
pub fn generate_block_with_template(&self, block_template: BlockTemplate) -> H256;
// Debug
pub fn jemalloc_profiling_dump(&self) -> String;
pub fn update_main_logger(&self, config: MainLoggerConfig) -> ();
pub fn set_extra_logger(&self, name: String, config_opt: Option<ExtraLoggerConfig>) -> ();
// Experimental
pub fn calculate_dao_maximum_withdraw(&self, out_point: OutPoint, kind: DaoWithdrawingCalculationKind) -> Capacity;
});
fn transform_cycles(cycles: Option<Vec<ckb_jsonrpc_types::Cycle>>) -> Vec<Cycle> {
cycles
.map(|c| c.into_iter().map(Into::into).collect())
.unwrap_or_default()
}
impl CkbRpcClient {
pub fn get_packed_block(&self, hash: H256) -> Result<Option<JsonBytes>, crate::RpcError> {
self.post("get_block", (hash, Some(Uint32::from(0u32))))
}
// turn block response into BlockView and cycle vec
fn transform_block_view_with_cycle(
opt_resp: Option<BlockResponse>,
) -> Result<Option<(BlockView, Vec<Cycle>)>, crate::rpc::RpcError> {
opt_resp
.map(|resp| match resp {
BlockResponse::Regular(block_view) => Ok((block_view.get_value()?, vec![])),
BlockResponse::WithCycles(block_cycles) => {
let cycles = transform_cycles(block_cycles.cycles);
Ok((block_cycles.block.get_value()?, cycles))
}
})
.transpose()
}
/// Same as get_block except with parameter with_cycles and return BlockResponse
pub fn get_block_with_cycles(
&self,
hash: H256,
) -> Result<Option<(BlockView, Vec<Cycle>)>, crate::rpc::RpcError> {
let res = self.post::<_, Option<BlockResponse>>("get_block", (hash, None::<u32>, true))?;
Self::transform_block_view_with_cycle(res)
}
// turn BlockResponse to JsonBytes and Cycle tuple
fn blockresponse2bytes(
opt_resp: Option<BlockResponse>,
) -> Result<Option<(JsonBytes, Vec<Cycle>)>, crate::rpc::RpcError> {
opt_resp
.map(|resp| match resp {
BlockResponse::Regular(block_view) => Ok((block_view.get_json_bytes()?, vec![])),
BlockResponse::WithCycles(block_cycles) => {
let cycles = transform_cycles(block_cycles.cycles);
Ok((block_cycles.block.get_json_bytes()?, cycles))
}
})
.transpose()
}
pub fn get_packed_block_with_cycles(
&self,
hash: H256,
) -> Result<Option<(JsonBytes, Vec<Cycle>)>, crate::rpc::RpcError> {
let res = self.post::<_, Option<BlockResponse>>(
"get_block",
(hash, Some(Uint32::from(0u32)), true),
)?;
Self::blockresponse2bytes(res)
}
/// Same as get_block_by_number except with parameter with_cycles and return BlockResponse
pub fn get_packed_block_by_number(
&self,
number: BlockNumber,
) -> Result<Option<JsonBytes>, crate::rpc::RpcError> {
self.post("get_block_by_number", (number, Some(Uint32::from(0u32))))
}
pub fn get_block_by_number_with_cycles(
&self,
number: BlockNumber,
) -> Result<Option<(BlockView, Vec<Cycle>)>, crate::rpc::RpcError> {
let res = self
.post::<_, Option<BlockResponse>>("get_block_by_number", (number, None::<u32>, true))?;
Self::transform_block_view_with_cycle(res)
}
pub fn get_packed_block_by_number_with_cycles(
&self,
number: BlockNumber,
) -> Result<Option<(JsonBytes, Vec<Cycle>)>, crate::rpc::RpcError> {
let res = self.post::<_, Option<BlockResponse>>(
"get_block_by_number",
(number, Some(Uint32::from(0u32)), true),
)?;
Self::blockresponse2bytes(res)
}
pub fn get_packed_header(&self, hash: H256) -> Result<Option<JsonBytes>, crate::rpc::RpcError> {
self.post::<_, Option<JsonBytes>>("get_header", (hash, Some(Uint32::from(0u32))))
}
pub fn get_packed_header_by_number(
&self,
number: BlockNumber,
) -> Result<Option<JsonBytes>, crate::rpc::RpcError> {
self.post::<_, Option<JsonBytes>>(
"get_header_by_number",
(number, Some(Uint32::from(0u32))),
)
}
// get transaction with only_committed=true
pub fn get_only_committed_transaction(
&self,
hash: H256,
) -> Result<TransactionWithStatusResponse, crate::rpc::RpcError> {
self.post::<_, TransactionWithStatusResponse>(
"get_transaction",
(hash, Some(Uint32::from(2u32)), true),
)
}
// get transaction with verbosity=0
pub fn get_packed_transaction(
&self,
hash: H256,
) -> Result<TransactionWithStatusResponse, crate::rpc::RpcError> {
self.post::<_, TransactionWithStatusResponse>(
"get_transaction",
(hash, Some(Uint32::from(0u32))),
)
}
// get transaction with verbosity=0 and only_committed=true
pub fn get_only_committed_packed_transaction(
&self,
hash: H256,
) -> Result<TransactionWithStatusResponse, crate::rpc::RpcError> {
self.post::<_, TransactionWithStatusResponse>(
"get_transaction",
(hash, Some(Uint32::from(0u32)), true),
)
}
// get transaction with verbosity=1, so the result transaction field is None
pub fn get_transaction_status(
&self,
hash: H256,
) -> Result<TransactionWithStatusResponse, crate::rpc::RpcError> {
self.post::<_, TransactionWithStatusResponse>(
"get_transaction",
(hash, Some(Uint32::from(1u32))),
)
}
// get transaction with verbosity=1 and only_committed=true, so the result transaction field is None
pub fn get_only_committed_transaction_status(
&self,
hash: H256,
) -> Result<TransactionWithStatusResponse, crate::rpc::RpcError> {
self.post::<_, TransactionWithStatusResponse>(
"get_transaction",
(hash, Some(Uint32::from(1u32)), true),
)
}
pub fn get_packed_tip_header(&self) -> Result<JsonBytes, crate::rpc::RpcError> {
self.post::<_, JsonBytes>("get_tip_header", (Some(Uint32::from(0u32)),))
}
pub fn get_packed_fork_block(
&self,
block_hash: H256,
) -> Result<Option<JsonBytes>, crate::rpc::RpcError> {
self.post::<_, Option<JsonBytes>>("get_fork_block", (block_hash, Some(Uint32::from(0u32))))
}
}