-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathckb_indexer_rpc.rs
295 lines (253 loc) · 10.4 KB
/
ckb_indexer_rpc.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
use crate::{
rpc::{
ckb_indexer::{Order, SearchKey, SearchMode},
CkbRpcClient,
},
traits::{CellQueryOptions, ValueRangeOption},
};
use ckb_types::{core::ScriptHashType, h256, prelude::*, H256};
// use serde_json;
const TEST_CKB_RPC_URL: &str = "https://testnet.ckb.dev";
const CODE_HASH: H256 = h256!("0x00000000000000000000000000000000000000000000000000545950455f4944");
// 8536c9d5d908bd89fc70099e4284870708b6632356aad98734fcf43f6f71c304
const ARGS: [u8; 32] = [
0x85, 0x36, 0xc9, 0xd5, 0xd9, 0x08, 0xbd, 0x89, 0xfc, 0x70, 0x09, 0x9e, 0x42, 0x84, 0x87, 0x07,
0x08, 0xb6, 0x63, 0x23, 0x56, 0xaa, 0xd9, 0x87, 0x34, 0xfc, 0xf4, 0x3f, 0x6f, 0x71, 0xc3, 0x04,
];
#[test]
fn test_get_indexer_tip() {
let ckb_client = CkbRpcClient::new(TEST_CKB_RPC_URL);
let indexer_tip_block_number = ckb_client
.get_indexer_tip()
.unwrap()
.unwrap()
.block_number
.value();
let tip_block_number = ckb_client.get_tip_block_number().unwrap().value();
assert!(tip_block_number - indexer_tip_block_number <= 1);
}
#[test]
fn test_cells_search_mode_default_partitial() {
let ckb_client = CkbRpcClient::new(TEST_CKB_RPC_URL);
let block_range = Some(ValueRangeOption::new(0, 1));
// default with partitial args
let script = ckb_types::packed::Script::new_builder()
.code_hash(CODE_HASH.pack())
.hash_type(ScriptHashType::Type.into())
.args(ARGS[0..2].pack())
.build();
let mut query = CellQueryOptions::new(script, crate::traits::PrimaryScriptType::Type);
query.block_range = block_range;
query.script_search_mode = None;
query.min_total_capacity = u64::MAX;
let search_key = SearchKey::from(query);
let page = ckb_client
.get_cells(search_key, Order::Desc, 10u32.into(), None)
.unwrap();
assert_eq!(page.objects.len(), 1);
}
#[test]
fn test_cells_search_mode_prefix_partitial() {
let ckb_client = CkbRpcClient::new(TEST_CKB_RPC_URL);
let block_range = Some(ValueRangeOption::new(0, 1));
// prefix with partitial args
let script = ckb_types::packed::Script::new_builder()
.code_hash(CODE_HASH.pack())
.hash_type(ScriptHashType::Type.into())
.args(ARGS[0..2].pack())
.build();
let mut query = CellQueryOptions::new(script, crate::traits::PrimaryScriptType::Type);
query.block_range = block_range;
query.script_search_mode = Some(SearchMode::Prefix);
query.min_total_capacity = u64::MAX;
let search_key = SearchKey::from(query);
let page = ckb_client
.get_cells(search_key, Order::Desc, 10u32.into(), None)
.unwrap();
assert_eq!(page.objects.len(), 1);
}
#[test]
fn test_cells_search_mode_exact_partitial() {
let ckb_client = CkbRpcClient::new(TEST_CKB_RPC_URL);
let block_range = Some(ValueRangeOption::new(0, 1));
let script = ckb_types::packed::Script::new_builder()
.code_hash(CODE_HASH.pack())
.hash_type(ScriptHashType::Type.into())
.args(ARGS[0..2].pack())
.build();
// exact with partitial args
let mut query = CellQueryOptions::new(script, crate::traits::PrimaryScriptType::Type);
query.script_search_mode = Some(SearchMode::Exact);
query.block_range = block_range;
query.min_total_capacity = u64::MAX;
let search_key = SearchKey::from(query);
let page = ckb_client
.get_cells(search_key, Order::Desc, 10u32.into(), None)
.unwrap();
assert_eq!(0, page.objects.len());
}
#[test]
fn test_cells_search_mode_exact() {
let ckb_client = CkbRpcClient::new(TEST_CKB_RPC_URL);
let block_range = Some(ValueRangeOption::new(0, 1));
let script = ckb_types::packed::Script::new_builder()
.code_hash(CODE_HASH.pack())
.hash_type(ScriptHashType::Type.into())
.args(ARGS[..].pack())
.build();
let mut query = CellQueryOptions::new(script, crate::traits::PrimaryScriptType::Type);
query.script_search_mode = Some(SearchMode::Exact);
query.block_range = block_range;
query.min_total_capacity = u64::MAX;
let search_key = SearchKey::from(query);
let page = ckb_client
.get_cells(search_key, Order::Desc, 10u32.into(), None)
.unwrap();
assert_eq!(page.objects.len(), 1);
}
#[test]
fn test_get_transactions_search_mode_default() {
let ckb_client = CkbRpcClient::new(TEST_CKB_RPC_URL);
let block_range = Some(ValueRangeOption::new(0, 1));
let script = ckb_types::packed::Script::new_builder()
.code_hash(CODE_HASH.pack())
.hash_type(ScriptHashType::Type.into())
.args(ARGS[0..2].pack())
.build();
let mut query = CellQueryOptions::new(script, crate::traits::PrimaryScriptType::Type);
query.block_range = block_range;
query.script_search_mode = None;
query.min_total_capacity = u64::MAX;
let search_key = SearchKey::from(query);
let page = ckb_client
.get_transactions(search_key, Order::Desc, 10u32.into(), None)
.unwrap();
assert_eq!(page.objects.len(), 1);
}
#[test]
fn test_get_transactions_search_mode_prefix_partial() {
let ckb_client = CkbRpcClient::new(TEST_CKB_RPC_URL);
let block_range = Some(ValueRangeOption::new(0, 1));
let script = ckb_types::packed::Script::new_builder()
.code_hash(CODE_HASH.pack())
.hash_type(ScriptHashType::Type.into())
.args(ARGS[0..2].pack())
.build();
let mut query = CellQueryOptions::new(script, crate::traits::PrimaryScriptType::Type);
query.block_range = block_range;
query.script_search_mode = Some(SearchMode::Prefix);
query.min_total_capacity = u64::MAX;
let search_key = SearchKey::from(query);
let page = ckb_client
.get_transactions(search_key, Order::Desc, 10u32.into(), None)
.unwrap();
assert_eq!(page.objects.len(), 1);
}
#[test]
fn test_get_transactions_search_mode_exact_partitial() {
let ckb_client = CkbRpcClient::new(TEST_CKB_RPC_URL);
let block_range = Some(ValueRangeOption::new(0, 1));
let script = ckb_types::packed::Script::new_builder()
.code_hash(CODE_HASH.pack())
.hash_type(ScriptHashType::Type.into())
.args(ARGS[0..2].pack())
.build();
let mut query = CellQueryOptions::new(script, crate::traits::PrimaryScriptType::Type);
query.script_search_mode = Some(SearchMode::Exact);
query.block_range = block_range;
query.min_total_capacity = u64::MAX;
let search_key = SearchKey::from(query);
let page = ckb_client
.get_transactions(search_key, Order::Desc, 10u32.into(), None)
.unwrap();
assert_eq!(0, page.objects.len());
}
#[test]
fn test_get_transactions_search_mode_exact_full() {
let ckb_client = CkbRpcClient::new(TEST_CKB_RPC_URL);
let block_range = Some(ValueRangeOption::new(0, 1));
// exact search
let script = ckb_types::packed::Script::new_builder()
.code_hash(CODE_HASH.pack())
.hash_type(ScriptHashType::Type.into())
.args(ARGS[..].pack())
.build();
let mut query = CellQueryOptions::new(script, crate::traits::PrimaryScriptType::Type);
query.script_search_mode = Some(SearchMode::Exact);
query.block_range = block_range;
query.min_total_capacity = u64::MAX;
let search_key = SearchKey::from(query);
let page = ckb_client
.get_transactions(search_key, Order::Desc, 10u32.into(), None)
.unwrap();
assert_eq!(page.objects.len(), 1);
}
#[test]
fn test_get_cells_capacity_search_mode_default() {
let ckb_client = CkbRpcClient::new(TEST_CKB_RPC_URL);
let block_range = Some(ValueRangeOption::new(0, 1));
let script = ckb_types::packed::Script::new_builder()
.code_hash(CODE_HASH.pack())
.hash_type(ScriptHashType::Type.into())
.args(ARGS[0..2].pack())
.build();
let mut query = CellQueryOptions::new(script, crate::traits::PrimaryScriptType::Type);
query.block_range = block_range;
query.script_search_mode = None;
query.min_total_capacity = u64::MAX;
let search_key = SearchKey::from(query);
let cells_capacity = ckb_client.get_cells_capacity(search_key).unwrap().unwrap();
assert_eq!(cells_capacity.capacity.value(), 0x9184e72a000);
}
#[test]
fn test_get_cells_capacity_search_mode_prefix_partial() {
let ckb_client = CkbRpcClient::new(TEST_CKB_RPC_URL);
let block_range = Some(ValueRangeOption::new(0, 1));
let script = ckb_types::packed::Script::new_builder()
.code_hash(CODE_HASH.pack())
.hash_type(ScriptHashType::Type.into())
.args(ARGS[0..2].pack())
.build();
let mut query = CellQueryOptions::new(script, crate::traits::PrimaryScriptType::Type);
query.block_range = block_range;
query.script_search_mode = Some(SearchMode::Prefix);
query.min_total_capacity = u64::MAX;
let search_key = SearchKey::from(query);
let cells_capacity = ckb_client.get_cells_capacity(search_key).unwrap().unwrap();
assert_eq!(cells_capacity.capacity.value(), 0x9184e72a000);
}
#[test]
fn test_get_cells_capacity_search_mode_exact_partital() {
let ckb_client = CkbRpcClient::new(TEST_CKB_RPC_URL);
let block_range = Some(ValueRangeOption::new(0, 1));
let script = ckb_types::packed::Script::new_builder()
.code_hash(CODE_HASH.pack())
.hash_type(ScriptHashType::Type.into())
.args(ARGS[0..2].pack())
.build();
// exact with partitial args
let mut query = CellQueryOptions::new(script, crate::traits::PrimaryScriptType::Type);
query.script_search_mode = Some(SearchMode::Exact);
query.block_range = block_range;
query.min_total_capacity = u64::MAX;
let search_key = SearchKey::from(query);
let cells_capacity = ckb_client.get_cells_capacity(search_key).unwrap().unwrap();
assert_eq!(cells_capacity.capacity.value(), 0);
}
#[test]
fn test_get_cells_capacity_search_mode_exact() {
let ckb_client = CkbRpcClient::new(TEST_CKB_RPC_URL);
let block_range = Some(ValueRangeOption::new(0, 1));
let script = ckb_types::packed::Script::new_builder()
.code_hash(CODE_HASH.pack())
.hash_type(ScriptHashType::Type.into())
.args(ARGS[..].pack())
.build();
let mut query = CellQueryOptions::new(script, crate::traits::PrimaryScriptType::Type);
query.script_search_mode = Some(SearchMode::Exact);
query.block_range = block_range;
query.min_total_capacity = u64::MAX;
let search_key = SearchKey::from(query);
let cells_capacity = ckb_client.get_cells_capacity(search_key).unwrap().unwrap();
assert_eq!(cells_capacity.capacity.value(), 0x9184e72a000);
}