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
use crate::error::RPCError;
use ckb_dao::DaoCalculator;
use ckb_jsonrpc_types::{
Capacity, DaoWithdrawingCalculationKind, DryRunResult, OutPoint, Transaction,
};
use ckb_shared::{shared::Shared, Snapshot};
use ckb_store::ChainStore;
use ckb_types::{
core::{
self,
cell::{
resolve_transaction_with_options, CellProvider, CellStatus, HeaderChecker,
ResolveOptions,
},
error::OutPointError,
},
packed,
prelude::*,
};
use ckb_verification::{ScriptVerifier, TxVerifyEnv};
use jsonrpc_core::Result;
use jsonrpc_derive::rpc;
use std::collections::HashSet;
/// RPC Module Experiment for experimenting methods.
///
/// **EXPERIMENTAL warning**
///
/// The methods here may be removed or changed in future releases without prior notifications.
#[rpc(server)]
pub trait ExperimentRpc {
/// Dry run a transaction and return the execution cycles.
///
/// This method will not check the transaction validity, but only run the lock script
/// and type script and then return the execution cycles.
///
/// It is used to debug transaction scripts and query how many cycles the scripts consume.
///
/// ## Errors
///
/// * [`TransactionFailedToResolve (-301)`](../enum.RPCError.html#variant.TransactionFailedToResolve) - Failed to resolve the referenced cells and headers used in the transaction, as inputs or dependencies.
/// * [`TransactionFailedToVerify (-302)`](../enum.RPCError.html#variant.TransactionFailedToVerify) - There is a script returns with an error.
///
/// ## Examples
///
/// Request
///
/// ```json
/// {
/// "id": 42,
/// "jsonrpc": "2.0",
/// "method": "dry_run_transaction",
/// "params": [
/// {
/// "cell_deps": [
/// {
/// "dep_type": "code",
/// "out_point": {
/// "index": "0x0",
/// "tx_hash": "0xa4037a893eb48e18ed4ef61034ce26eba9c585f15c9cee102ae58505565eccc3"
/// }
/// }
/// ],
/// "header_deps": [
/// "0x7978ec7ce5b507cfb52e149e36b1a23f6062ed150503c85bbf825da3599095ed"
/// ],
/// "inputs": [
/// {
/// "previous_output": {
/// "index": "0x0",
/// "tx_hash": "0x365698b50ca0da75dca2c87f9e7b563811d3b5813736b8cc62cc3b106faceb17"
/// },
/// "since": "0x0"
/// }
/// ],
/// "outputs": [
/// {
/// "capacity": "0x2540be400",
/// "lock": {
/// "code_hash": "0x28e83a1277d48add8e72fadaa9248559e1b632bab2bd60b27955ebc4c03800a5",
/// "hash_type": "data",
/// "args": "0x"
/// },
/// "type": null
/// }
/// ],
/// "outputs_data": [
/// "0x"
/// ],
/// "version": "0x0",
/// "witnesses": []
/// }
/// ]
/// }
/// ```
///
/// Response
///
/// ```json
/// {
/// "id": 42,
/// "jsonrpc": "2.0",
/// "result": {
/// "cycles": "0x219"
/// }
/// }
/// ```
#[rpc(name = "dry_run_transaction")]
fn dry_run_transaction(&self, tx: Transaction) -> Result<DryRunResult>;
/// Calculates the maximum withdrawal one can get, given a referenced DAO cell, and
/// a withdrawing block hash.
///
/// ## Params
///
/// * `out_point` - Reference to the DAO cell, the depositing transaction's output.
/// * `kind` - Two kinds of dao withdrawal amount calculation option.
///
/// option 1, the assumed reference block hash for withdrawing phase 1 transaction, this block must be in the
/// [canonical chain](trait.ChainRpc.html#canonical-chain), the calculation of occupied capacity will be based on the depositing transaction's output, assuming the output of phase 1 transaction is the same as the depositing transaction's output.
///
/// option 2, the out point of the withdrawing phase 1 transaction, the calculation of occupied capacity will be based on corresponding phase 1 transaction's output.
///
/// ## Returns
///
/// The RPC returns the final capacity when the cell `out_point` is withdrawn using the block hash or withdrawing phase 1 transaction out point as the reference.
///
/// In CKB, scripts cannot get the information about in which block the transaction is
/// committed. A workaround is letting the transaction reference a block hash so the script
/// knows that the transaction is committed at least after the reference block.
///
/// ## Errors
///
/// * [`DaoError (-5)`](../enum.RPCError.html#variant.DaoError) - The given out point is not a valid cell for DAO computation.
/// * [`CKBInternalError (-1)`](../enum.RPCError.html#variant.CKBInternalError) - Mathematics overflow.
///
/// ## Examples
///
/// Request
///
/// ```json
/// {
/// "id": 42,
/// "jsonrpc": "2.0",
/// "method": "calculate_dao_maximum_withdraw",
/// "params": [
/// {
/// "index": "0x0",
/// "tx_hash": "0xa4037a893eb48e18ed4ef61034ce26eba9c585f15c9cee102ae58505565eccc3"
/// },
/// "0xa5f5c85987a15de25661e5a214f2c1449cd803f071acc7999820f25246471f40"
/// ]
/// }
/// ```
///
/// Response
///
/// ```json
/// {
/// "id": 42,
/// "jsonrpc": "2.0",
/// "result": "0x4a8b4e8a4"
/// }
/// ```
#[rpc(name = "calculate_dao_maximum_withdraw")]
fn calculate_dao_maximum_withdraw(
&self,
out_point: OutPoint,
kind: DaoWithdrawingCalculationKind,
) -> Result<Capacity>;
}
pub(crate) struct ExperimentRpcImpl {
pub shared: Shared,
}
impl ExperimentRpc for ExperimentRpcImpl {
fn dry_run_transaction(&self, tx: Transaction) -> Result<DryRunResult> {
let tx: packed::Transaction = tx.into();
DryRunner::new(&self.shared).run(tx)
}
fn calculate_dao_maximum_withdraw(
&self,
out_point: OutPoint,
kind: DaoWithdrawingCalculationKind,
) -> Result<Capacity> {
let snapshot: &Snapshot = &self.shared.snapshot();
let consensus = snapshot.consensus();
let out_point: packed::OutPoint = out_point.into();
let data_loader = snapshot.as_data_provider();
let calculator = DaoCalculator::new(consensus, &data_loader);
match kind {
DaoWithdrawingCalculationKind::WithdrawingHeaderHash(withdrawing_header_hash) => {
let (tx, deposit_header_hash) = snapshot
.get_transaction(&out_point.tx_hash())
.ok_or_else(|| RPCError::invalid_params("invalid out_point"))?;
let output = tx
.outputs()
.get(out_point.index().unpack())
.ok_or_else(|| RPCError::invalid_params("invalid out_point"))?;
let output_data = tx
.outputs_data()
.get(out_point.index().unpack())
.ok_or_else(|| RPCError::invalid_params("invalid out_point"))?;
match calculator.calculate_maximum_withdraw(
&output,
core::Capacity::bytes(output_data.len()).expect("should not overlfow"),
&deposit_header_hash,
&withdrawing_header_hash.pack(),
) {
Ok(capacity) => Ok(capacity.into()),
Err(err) => Err(RPCError::from_ckb_error(err)),
}
}
DaoWithdrawingCalculationKind::WithdrawingOutPoint(withdrawing_out_point) => {
let (_tx, deposit_header_hash) = snapshot
.get_transaction(&out_point.tx_hash())
.ok_or_else(|| RPCError::invalid_params("invalid out_point"))?;
let withdrawing_out_point: packed::OutPoint = withdrawing_out_point.into();
let (withdrawing_tx, withdrawing_header_hash) = snapshot
.get_transaction(&withdrawing_out_point.tx_hash())
.ok_or_else(|| RPCError::invalid_params("invalid withdrawing_out_point"))?;
let output = withdrawing_tx
.outputs()
.get(withdrawing_out_point.index().unpack())
.ok_or_else(|| RPCError::invalid_params("invalid withdrawing_out_point"))?;
let output_data = withdrawing_tx
.outputs_data()
.get(withdrawing_out_point.index().unpack())
.ok_or_else(|| RPCError::invalid_params("invalid withdrawing_out_point"))?;
match calculator.calculate_maximum_withdraw(
&output,
core::Capacity::bytes(output_data.len()).expect("should not overlfow"),
&deposit_header_hash,
&withdrawing_header_hash,
) {
Ok(capacity) => Ok(capacity.into()),
Err(err) => Err(RPCError::from_ckb_error(err)),
}
}
}
}
}
// DryRunner dry run given transaction, and return the result, including execution cycles.
pub(crate) struct DryRunner<'a> {
shared: &'a Shared,
}
impl<'a> CellProvider for DryRunner<'a> {
fn cell(&self, out_point: &packed::OutPoint, eager_load: bool) -> CellStatus {
let snapshot = self.shared.snapshot();
snapshot
.get_cell(out_point)
.map(|mut cell_meta| {
if eager_load {
if let Some((data, data_hash)) = snapshot.get_cell_data(out_point) {
cell_meta.mem_cell_data = Some(data);
cell_meta.mem_cell_data_hash = Some(data_hash);
}
}
CellStatus::live_cell(cell_meta)
}) // treat as live cell, regardless of live or dead
.unwrap_or(CellStatus::Unknown)
}
}
impl<'a> HeaderChecker for DryRunner<'a> {
fn check_valid(&self, block_hash: &packed::Byte32) -> std::result::Result<(), OutPointError> {
self.shared.snapshot().check_valid(block_hash)
}
}
impl<'a> DryRunner<'a> {
pub(crate) fn new(shared: &'a Shared) -> Self {
Self { shared }
}
pub(crate) fn run(&self, tx: packed::Transaction) -> Result<DryRunResult> {
let snapshot: &Snapshot = &self.shared.snapshot();
let consensus = snapshot.consensus();
let tip_header = snapshot.tip_header();
let tx_env = TxVerifyEnv::new_submit(&tip_header);
let resolve_opts = {
let proposal_window = consensus.tx_proposal_window();
let epoch_number = tx_env.epoch_number(proposal_window);
let hardfork_switch = consensus.hardfork_switch();
ResolveOptions::new().apply_current_features(hardfork_switch, epoch_number)
};
match resolve_transaction_with_options(
tx.into_view(),
&mut HashSet::new(),
self,
self,
resolve_opts,
) {
Ok(resolved) => {
let max_cycles = consensus.max_block_cycles;
match ScriptVerifier::new(
&resolved,
consensus,
&snapshot.as_data_provider(),
&tx_env,
)
.verify(max_cycles)
{
Ok(cycles) => Ok(DryRunResult {
cycles: cycles.into(),
}),
Err(err) => Err(RPCError::custom_with_error(
RPCError::TransactionFailedToVerify,
err,
)),
}
}
Err(err) => Err(RPCError::custom_with_error(
RPCError::TransactionFailedToResolve,
err,
)),
}
}
}