forked from jl777/SuperNET
-
Notifications
You must be signed in to change notification settings - Fork 94
/
tendermint_tests.rs
446 lines (368 loc) · 17.6 KB
/
tendermint_tests.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
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
use common::block_on;
use mm2_number::BigDecimal;
use mm2_test_helpers::for_tests::{atom_testnet_conf, disable_coin, disable_coin_err, enable_tendermint,
enable_tendermint_token, enable_tendermint_without_balance,
get_tendermint_my_tx_history, ibc_withdraw, iris_nimda_testnet_conf,
iris_testnet_conf, my_balance, send_raw_transaction, withdraw_v1, MarketMakerIt,
Mm2TestConf};
use mm2_test_helpers::structs::{RpcV2Response, TendermintActivationResult, TransactionDetails};
use serde_json::{self as json, json};
const ATOM_TEST_BALANCE_SEED: &str = "atom test seed";
const ATOM_TEST_WITHDRAW_SEED: &str = "atom test withdraw seed";
const ATOM_TICKER: &str = "ATOM";
const ATOM_TENDERMINT_RPC_URLS: &[&str] = &["https://rpc.sentry-02.theta-testnet.polypore.xyz"];
const IRIS_TEST_SEED: &str = "iris test seed";
const IRIS_TESTNET_RPC_URLS: &[&str] = &["http://34.80.202.172:26657"];
#[test]
fn test_tendermint_activation_and_balance() {
let coins = json!([atom_testnet_conf()]);
let expected_address = "cosmos1svaw0aqc4584x825ju7ua03g5xtxwd0ahl86hz";
let conf = Mm2TestConf::seednode(ATOM_TEST_BALANCE_SEED, &coins);
let mm = MarketMakerIt::start(conf.conf, conf.rpc_password, None).unwrap();
let activation_result = block_on(enable_tendermint(
&mm,
ATOM_TICKER,
&[],
ATOM_TENDERMINT_RPC_URLS,
false,
));
let result: RpcV2Response<TendermintActivationResult> = json::from_value(activation_result).unwrap();
assert_eq!(result.result.address, expected_address);
let expected_balance: BigDecimal = "8.0959".parse().unwrap();
assert_eq!(result.result.balance.unwrap().spendable, expected_balance);
let my_balance = block_on(my_balance(&mm, ATOM_TICKER));
assert_eq!(my_balance.balance, expected_balance);
assert_eq!(my_balance.unspendable_balance, BigDecimal::default());
assert_eq!(my_balance.address, expected_address);
assert_eq!(my_balance.coin, ATOM_TICKER);
}
#[test]
fn test_tendermint_activation_without_balance() {
let coins = json!([atom_testnet_conf()]);
let conf = Mm2TestConf::seednode(ATOM_TEST_BALANCE_SEED, &coins);
let mm = MarketMakerIt::start(conf.conf, conf.rpc_password, None).unwrap();
let activation_result = block_on(enable_tendermint_without_balance(
&mm,
ATOM_TICKER,
&[],
ATOM_TENDERMINT_RPC_URLS,
false,
));
let result: RpcV2Response<TendermintActivationResult> = json::from_value(activation_result).unwrap();
assert!(result.result.balance.is_none());
assert!(result.result.tokens_balances.is_none());
assert!(result.result.tokens_tickers.unwrap().is_empty());
}
#[test]
fn test_tendermint_withdraw() {
let coins = json!([atom_testnet_conf()]);
let conf = Mm2TestConf::seednode(ATOM_TEST_WITHDRAW_SEED, &coins);
let mm = MarketMakerIt::start(conf.conf, conf.rpc_password, None).unwrap();
let activation_res = block_on(enable_tendermint(
&mm,
ATOM_TICKER,
&[],
ATOM_TENDERMINT_RPC_URLS,
false,
));
println!("Activation {}", json::to_string(&activation_res).unwrap());
// just call withdraw without sending to check response correctness
let tx_details = block_on(withdraw_v1(
&mm,
ATOM_TICKER,
"cosmos1svaw0aqc4584x825ju7ua03g5xtxwd0ahl86hz",
"0.1",
));
println!("Withdraw to other {}", json::to_string(&tx_details).unwrap());
// TODO how to check it if the fee is dynamic?
/*
let expected_total: BigDecimal = "0.15".parse().unwrap();
assert_eq!(tx_details.total_amount, expected_total);
assert_eq!(tx_details.spent_by_me, expected_total);
assert_eq!(tx_details.my_balance_change, expected_total * BigDecimal::from(-1));
*/
assert_eq!(tx_details.received_by_me, BigDecimal::default());
assert_eq!(tx_details.to, vec![
"cosmos1svaw0aqc4584x825ju7ua03g5xtxwd0ahl86hz".to_owned()
]);
assert_eq!(tx_details.from, vec![
"cosmos1w5h6wud7a8zpa539rc99ehgl9gwkad3wjsjq8v".to_owned()
]);
// withdraw and send transaction to ourselves
let tx_details = block_on(withdraw_v1(
&mm,
ATOM_TICKER,
"cosmos1w5h6wud7a8zpa539rc99ehgl9gwkad3wjsjq8v",
"0.1",
));
println!("Withdraw to self {}", json::to_string(&tx_details).unwrap());
// TODO how to check it if the fee is dynamic?
/*
let expected_total: BigDecimal = "0.15".parse().unwrap();
let expected_balance_change: BigDecimal = "-0.05".parse().unwrap();
assert_eq!(tx_details.total_amount, expected_total);
assert_eq!(tx_details.spent_by_me, expected_total);
assert_eq!(tx_details.my_balance_change, expected_balance_change);
*/
let expected_received: BigDecimal = "0.1".parse().unwrap();
assert_eq!(tx_details.received_by_me, expected_received);
assert_eq!(tx_details.to, vec![
"cosmos1w5h6wud7a8zpa539rc99ehgl9gwkad3wjsjq8v".to_owned()
]);
assert_eq!(tx_details.from, vec![
"cosmos1w5h6wud7a8zpa539rc99ehgl9gwkad3wjsjq8v".to_owned()
]);
let tx_details = block_on(withdraw_v1(
&mm,
ATOM_TICKER,
"cosmos1w5h6wud7a8zpa539rc99ehgl9gwkad3wjsjq8v",
"0.1",
));
let send_raw_tx = block_on(send_raw_transaction(&mm, ATOM_TICKER, &tx_details.tx_hex));
println!("Send raw tx {}", json::to_string(&send_raw_tx).unwrap());
}
#[test]
fn test_custom_gas_limit_on_tendermint_withdraw() {
let coins = json!([atom_testnet_conf()]);
let conf = Mm2TestConf::seednode(ATOM_TEST_WITHDRAW_SEED, &coins);
let mm = MarketMakerIt::start(conf.conf, conf.rpc_password, None).unwrap();
let activation_res = block_on(enable_tendermint(
&mm,
ATOM_TICKER,
&[],
ATOM_TENDERMINT_RPC_URLS,
false,
));
println!("Activation {}", json::to_string(&activation_res).unwrap());
let request = block_on(mm.rpc(&json!({
"userpass": mm.userpass,
"method": "withdraw",
"coin": ATOM_TICKER,
"to": "cosmos1svaw0aqc4584x825ju7ua03g5xtxwd0ahl86hz",
"amount": "0.1",
"fee": {
"type": "CosmosGas",
"gas_limit": 150000,
"gas_price": 0.1
}
})))
.unwrap();
assert_eq!(request.0, common::StatusCode::OK, "'withdraw' failed: {}", request.1);
let tx_details: TransactionDetails = json::from_str(&request.1).unwrap();
assert_eq!(tx_details.fee_details["gas_limit"], 150000);
}
#[test]
fn test_tendermint_ibc_withdraw() {
const IBC_SOURCE_CHANNEL: &str = "channel-81";
const IBC_TARGET_ADDRESS: &str = "cosmos1r5v5srda7xfth3hn2s26txvrcrntldjumt8mhl";
const MY_ADDRESS: &str = "iaa1e0rx87mdj79zejewuc4jg7ql9ud2286g2us8f2";
let coins = json!([iris_testnet_conf(), iris_nimda_testnet_conf()]);
let platform_coin = coins[0]["coin"].as_str().unwrap();
let token = coins[1]["coin"].as_str().unwrap();
let conf = Mm2TestConf::seednode(IRIS_TEST_SEED, &coins);
let mm = MarketMakerIt::start(conf.conf, conf.rpc_password, None).unwrap();
let activation_res = block_on(enable_tendermint(&mm, platform_coin, &[], IRIS_TESTNET_RPC_URLS, false));
println!("Activation with assets {}", json::to_string(&activation_res).unwrap());
let activation_res = block_on(enable_tendermint_token(&mm, token));
println!("Token activation {}", json::to_string(&activation_res).unwrap());
let tx_details = block_on(ibc_withdraw(&mm, IBC_SOURCE_CHANNEL, token, IBC_TARGET_ADDRESS, "0.1"));
println!("IBC transfer to atom address {}", json::to_string(&tx_details).unwrap());
let expected_spent: BigDecimal = "0.1".parse().unwrap();
assert_eq!(tx_details.spent_by_me, expected_spent);
assert_eq!(tx_details.to, vec![IBC_TARGET_ADDRESS.to_owned()]);
assert_eq!(tx_details.from, vec![MY_ADDRESS.to_owned()]);
let tx_details = block_on(ibc_withdraw(&mm, IBC_SOURCE_CHANNEL, token, IBC_TARGET_ADDRESS, "0.1"));
let send_raw_tx = block_on(send_raw_transaction(&mm, token, &tx_details.tx_hex));
println!("Send raw tx {}", json::to_string(&send_raw_tx).unwrap());
}
#[test]
fn test_tendermint_token_activation_and_withdraw() {
let coins = json!([iris_testnet_conf(), iris_nimda_testnet_conf()]);
let platform_coin = coins[0]["coin"].as_str().unwrap();
let token = coins[1]["coin"].as_str().unwrap();
let conf = Mm2TestConf::seednode(IRIS_TEST_SEED, &coins);
let mm = MarketMakerIt::start(conf.conf, conf.rpc_password, None).unwrap();
let activation_res = block_on(enable_tendermint(&mm, platform_coin, &[], IRIS_TESTNET_RPC_URLS, false));
println!("Activation with assets {}", json::to_string(&activation_res).unwrap());
let activation_res = block_on(enable_tendermint_token(&mm, token));
println!("Token activation {}", json::to_string(&activation_res).unwrap());
// just call withdraw without sending to check response correctness
let tx_details = block_on(withdraw_v1(
&mm,
token,
"iaa1llp0f6qxemgh4g4m5ewk0ew0hxj76avuz8kwd5",
"0.1",
));
println!("Withdraw to other {}", json::to_string(&tx_details).unwrap());
let expected_total: BigDecimal = "0.1".parse().unwrap();
assert_eq!(tx_details.total_amount, expected_total);
// TODO How to check it if the fee is dynamic?
/*
let expected_fee: BigDecimal = "0.05".parse().unwrap();
let actual_fee: BigDecimal = tx_details.fee_details["amount"].as_str().unwrap().parse().unwrap();
assert_eq!(actual_fee, expected_fee);
*/
assert_eq!(tx_details.spent_by_me, expected_total);
assert_eq!(tx_details.my_balance_change, expected_total * BigDecimal::from(-1));
assert_eq!(tx_details.received_by_me, BigDecimal::default());
assert_eq!(tx_details.to, vec![
"iaa1llp0f6qxemgh4g4m5ewk0ew0hxj76avuz8kwd5".to_owned()
]);
assert_eq!(tx_details.from, vec![
"iaa1e0rx87mdj79zejewuc4jg7ql9ud2286g2us8f2".to_owned()
]);
// withdraw and send transaction to ourselves
let tx_details = block_on(withdraw_v1(
&mm,
token,
"iaa1e0rx87mdj79zejewuc4jg7ql9ud2286g2us8f2",
"0.1",
));
println!("Withdraw to self {}", json::to_string(&tx_details).unwrap());
let expected_total: BigDecimal = "0.1".parse().unwrap();
let expected_received: BigDecimal = "0.1".parse().unwrap();
assert_eq!(tx_details.total_amount, expected_total);
// TODO How to check it if the fee is dynamic?
/*
let expected_fee: BigDecimal = "0.05".parse().unwrap();
let actual_fee: BigDecimal = tx_details.fee_details["amount"].as_str().unwrap().parse().unwrap();
assert_eq!(actual_fee, expected_fee);
*/
assert_eq!(tx_details.spent_by_me, expected_total);
assert_eq!(tx_details.received_by_me, expected_received);
assert_eq!(tx_details.to, vec![
"iaa1e0rx87mdj79zejewuc4jg7ql9ud2286g2us8f2".to_owned()
]);
assert_eq!(tx_details.from, vec![
"iaa1e0rx87mdj79zejewuc4jg7ql9ud2286g2us8f2".to_owned()
]);
let tx_details = block_on(withdraw_v1(
&mm,
token,
"iaa1e0rx87mdj79zejewuc4jg7ql9ud2286g2us8f2",
"0.1",
));
let send_raw_tx = block_on(send_raw_transaction(&mm, token, &tx_details.tx_hex));
println!("Send raw tx {}", json::to_string(&send_raw_tx).unwrap());
}
#[test]
fn test_tendermint_tx_history() {
const TEST_SEED: &str = "Vdo8Xt8pTAetRlMq3kV0LzE393eVYbPSn5Mhtw4p";
const TX_FINISHED_LOG: &str = "Tx history fetching finished for IRIS-TEST.";
const TX_HISTORY_PAGE_LIMIT: usize = 50;
const IRIS_TEST_EXPECTED_TX_COUNT: u64 = 16;
const IRIS_NIMDA_EXPECTED_TX_COUNT: u64 = 10;
let iris_test_constant_history_txs = include_str!("../../../mm2_test_helpers/dummy_files/iris_test_history.json");
let iris_test_constant_history_txs: Vec<TransactionDetails> =
serde_json::from_str(iris_test_constant_history_txs).unwrap();
let iris_nimda_constant_history_txs = include_str!("../../../mm2_test_helpers/dummy_files/iris_nimda_history.json");
let iris_nimda_constant_history_txs: Vec<TransactionDetails> =
serde_json::from_str(iris_nimda_constant_history_txs).unwrap();
let coins = json!([iris_testnet_conf(), iris_nimda_testnet_conf()]);
let platform_coin = coins[0]["coin"].as_str().unwrap();
let token = coins[1]["coin"].as_str().unwrap();
let conf = Mm2TestConf::seednode(TEST_SEED, &coins);
let mut mm = block_on(MarketMakerIt::start_async(conf.conf, conf.rpc_password, None)).unwrap();
block_on(enable_tendermint(
&mm,
platform_coin,
&[token],
IRIS_TESTNET_RPC_URLS,
true,
));
if block_on(mm.wait_for_log(60., |log| log.contains(TX_FINISHED_LOG))).is_err() {
println!("{}", mm.log_as_utf8().unwrap());
panic!("Tx history didn't finish which is not expected");
}
// testing IRIS-TEST history
let iris_tx_history_response = block_on(get_tendermint_my_tx_history(
&mm,
platform_coin,
TX_HISTORY_PAGE_LIMIT,
1,
));
let total_txs = iris_tx_history_response["result"]["total"].as_u64().unwrap();
assert_eq!(total_txs, IRIS_TEST_EXPECTED_TX_COUNT);
let mut iris_txs_from_request = iris_tx_history_response["result"]["transactions"].clone();
for i in 0..IRIS_TEST_EXPECTED_TX_COUNT {
iris_txs_from_request[i as usize]
.as_object_mut()
.unwrap()
.remove("confirmations");
}
let iris_txs_from_request: Vec<TransactionDetails> = serde_json::from_value(iris_txs_from_request).unwrap();
assert_eq!(iris_test_constant_history_txs, iris_txs_from_request);
// testing IRIS-NIMDA history
let nimda_tx_history_response = block_on(get_tendermint_my_tx_history(&mm, token, TX_HISTORY_PAGE_LIMIT, 1));
let total_txs = nimda_tx_history_response["result"]["total"].as_u64().unwrap();
assert_eq!(total_txs, IRIS_NIMDA_EXPECTED_TX_COUNT);
let mut nimda_txs_from_request = nimda_tx_history_response["result"]["transactions"].clone();
for i in 0..IRIS_NIMDA_EXPECTED_TX_COUNT {
nimda_txs_from_request[i as usize]
.as_object_mut()
.unwrap()
.remove("confirmations");
}
let nimda_txs_from_request: Vec<TransactionDetails> = serde_json::from_value(nimda_txs_from_request).unwrap();
assert_eq!(iris_nimda_constant_history_txs, nimda_txs_from_request);
block_on(mm.stop()).unwrap();
}
#[test]
fn test_disable_tendermint_platform_coin_with_token() {
const TEST_SEED: &str = "iris test seed";
let coins = json!([iris_testnet_conf(), iris_nimda_testnet_conf()]);
let platform_coin = coins[0]["coin"].as_str().unwrap();
let token = coins[1]["coin"].as_str().unwrap();
let conf = Mm2TestConf::seednode(TEST_SEED, &coins);
let mm = MarketMakerIt::start(conf.conf, conf.rpc_password, None).unwrap();
// Enable platform coin IRIS-TEST
let activation_res = block_on(enable_tendermint(&mm, platform_coin, &[], IRIS_TESTNET_RPC_URLS, false));
assert!(&activation_res.get("result").unwrap().get("address").is_some());
// Enable platform coin token IRIS-NIMDA
let activation_res = block_on(enable_tendermint_token(&mm, token));
assert!(&activation_res.get("result").unwrap().get("balances").is_some());
// Try to passive platform coin, IRIS-TEST.
let res = block_on(disable_coin(&mm, "IRIS-TEST", false));
assert!(res.passivized);
// Try to disable IRIS-NIMDA token when platform coin is passived.
// This should work, because platform coin is still in the memory.
let res = block_on(disable_coin(&mm, "IRIS-NIMDA", false));
assert!(!res.passivized);
// Then try to force disable IRIS-TEST platform coin.
let res = block_on(disable_coin(&mm, "IRIS-TEST", true));
assert!(!res.passivized);
}
#[test]
fn test_passive_coin_and_force_disable() {
const TEST_SEED: &str = "iris test seed";
let coins = json!([iris_testnet_conf(), iris_nimda_testnet_conf()]);
let platform_coin = coins[0]["coin"].as_str().unwrap();
let token = coins[1]["coin"].as_str().unwrap();
let conf = Mm2TestConf::seednode(TEST_SEED, &coins);
let mm = MarketMakerIt::start(conf.conf, conf.rpc_password, None).unwrap();
// Enable platform coin IRIS-TEST
let activation_res = block_on(enable_tendermint(&mm, platform_coin, &[], IRIS_TESTNET_RPC_URLS, false));
assert!(&activation_res.get("result").unwrap().get("address").is_some());
// Enable platform coin token IRIS-NIMDA
let activation_res = block_on(enable_tendermint_token(&mm, token));
assert!(&activation_res.get("result").unwrap().get("balances").is_some());
// Try to passive platform coin, IRIS-TEST.
let res = block_on(disable_coin(&mm, "IRIS-TEST", false));
assert!(res.passivized);
// Try to disable IRIS-NIMDA token when platform coin is passived.
// This should work, because platform coin is still in the memory.
let res = block_on(disable_coin(&mm, "IRIS-NIMDA", false));
assert!(!res.passivized);
// Re-activate passive coin
let activation_res = block_on(enable_tendermint(&mm, platform_coin, &[], IRIS_TESTNET_RPC_URLS, false));
assert!(&activation_res.get("result").unwrap().get("address").is_some());
// Enable platform coin token IRIS-NIMDA
let activation_res = block_on(enable_tendermint_token(&mm, token));
assert!(&activation_res.get("result").unwrap().get("balances").is_some());
// Try to force disable platform coin, IRIS-TEST.
let res = block_on(disable_coin(&mm, "IRIS-TEST", true));
assert!(!res.passivized);
// Try to disable IRIS-NIMDA token when platform coin force disabled.
// This should failed, because platform coin was purged with it's tokens.
block_on(disable_coin_err(&mm, "IRIS-NIMDA", false));
}