-
Notifications
You must be signed in to change notification settings - Fork 993
/
Copy pathledger_tests.rs
2082 lines (1869 loc) · 65.6 KB
/
ledger_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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! By default, these tests will run in release mode. This can be disabled
//! by setting environment variable `ANOMA_E2E_DEBUG=true`. For debugging,
//! you'll typically also want to set `RUST_BACKTRACE=1`, e.g.:
//!
//! ```ignore,shell
//! ANOMA_E2E_DEBUG=true RUST_BACKTRACE=1 cargo test e2e::ledger_tests -- --test-threads=1 --nocapture
//! ```
//!
//! To keep the temporary files created by a test, use env var
//! `ANOMA_E2E_KEEP_TEMP=true`.
use std::process::Command;
use std::sync::Arc;
use std::time::{Duration, Instant};
use borsh::BorshSerialize;
use color_eyre::eyre::Result;
use data_encoding::HEXLOWER;
use namada::types::token;
use namada_apps::config::ethereum_bridge;
use namada_apps::config::genesis::genesis_config::{
GenesisConfig, ParametersConfig, PosParamsConfig,
};
use serde_json::json;
use setup::constants::*;
use super::setup::{disable_eth_fullnode, get_all_wasms_hashes};
use crate::e2e::helpers::{
find_address, find_voting_power, get_actor_rpc, get_epoch,
};
use crate::e2e::setup::{self, sleep, Bin, Who};
use crate::{run, run_as};
/// Test that when we "run-ledger" with all the possible command
/// combinations from fresh state, the node starts-up successfully for both a
/// validator and non-validator user.
#[test]
fn run_ledger() -> Result<()> {
let test = setup::single_node_net()?;
disable_eth_fullnode(&test, &test.net.chain_id, &Who::Validator(0));
let cmd_combinations = vec![vec!["ledger"], vec!["ledger", "run"]];
// Start the ledger as a validator
for args in &cmd_combinations {
let mut ledger =
run_as!(test, Who::Validator(0), Bin::Node, args, Some(40))?;
ledger.exp_string("Anoma ledger node started")?;
ledger.exp_string("This node is a validator")?;
}
// Start the ledger as a non-validator
for args in &cmd_combinations {
let mut ledger =
run_as!(test, Who::NonValidator, Bin::Node, args, Some(40))?;
ledger.exp_string("Anoma ledger node started")?;
ledger.exp_string("This node is not a validator")?;
}
Ok(())
}
/// In this test we:
/// 1. Run 2 genesis validator ledger nodes and 1 non-validator node
/// 2. Submit a valid token transfer tx
/// 3. Check that all the nodes processed the tx with the same result
#[test]
fn test_node_connectivity() -> Result<()> {
// Setup 2 genesis validator nodes
let test =
setup::network(|genesis| setup::add_validators(1, genesis), None)?;
disable_eth_fullnode(&test, &test.net.chain_id, &Who::Validator(0));
disable_eth_fullnode(&test, &test.net.chain_id, &Who::Validator(1));
// 1. Run 2 genesis validator ledger nodes and 1 non-validator node
let args = ["ledger"];
let mut validator_0 =
run_as!(test, Who::Validator(0), Bin::Node, args, Some(40))?;
validator_0.exp_string("Anoma ledger node started")?;
validator_0.exp_string("This node is a validator")?;
validator_0.exp_string("Starting RPC HTTP server on")?;
let mut validator_1 =
run_as!(test, Who::Validator(1), Bin::Node, args, Some(40))?;
validator_1.exp_string("Anoma ledger node started")?;
validator_1.exp_string("This node is a validator")?;
validator_1.exp_string("Starting RPC HTTP server on")?;
let mut non_validator =
run_as!(test, Who::NonValidator, Bin::Node, args, Some(40))?;
non_validator.exp_string("Anoma ledger node started")?;
non_validator.exp_string("This node is not a validator")?;
non_validator.exp_string("Starting RPC HTTP server on")?;
let bg_validator_0 = validator_0.background();
let bg_validator_1 = validator_1.background();
let _bg_non_validator = non_validator.background();
// 2. Submit a valid token transfer tx
let validator_one_rpc = get_actor_rpc(&test, &Who::Validator(0));
let tx_args = [
"transfer",
"--source",
BERTHA,
"--target",
ALBERT,
"--token",
XAN,
"--amount",
"10.1",
"--fee-amount",
"0",
"--gas-limit",
"0",
"--fee-token",
XAN,
"--ledger-address",
&validator_one_rpc,
];
let mut client = run!(test, Bin::Client, tx_args, Some(40))?;
client.exp_string("Transaction applied with result:")?;
client.exp_string("Transaction is valid.")?;
client.assert_success();
// 3. Check that all the nodes processed the tx and report the same balance
let mut validator_0 = bg_validator_0.foreground();
let mut validator_1 = bg_validator_1.foreground();
let expected_result = "all VPs accepted transaction";
// We cannot check this on non-validator node as it might sync without
// applying the tx itself, but its state should be the same, checked below.
validator_0.exp_string(expected_result)?;
validator_1.exp_string(expected_result)?;
let _bg_validator_0 = validator_0.background();
let _bg_validator_1 = validator_1.background();
let query_balance_args = |ledger_rpc| {
vec![
"balance",
"--owner",
ALBERT,
"--token",
XAN,
"--ledger-address",
ledger_rpc,
]
};
let validator_0_rpc = get_actor_rpc(&test, &Who::Validator(0));
let validator_1_rpc = get_actor_rpc(&test, &Who::Validator(1));
let non_validator_rpc = get_actor_rpc(&test, &Who::NonValidator);
for ledger_rpc in &[validator_0_rpc, validator_1_rpc, non_validator_rpc] {
let mut client =
run!(test, Bin::Client, query_balance_args(ledger_rpc), Some(40))?;
client.exp_string("XAN: 1000010.1")?;
client.assert_success();
}
Ok(())
}
/// In this test we:
/// 1. Start up the ledger
/// 2. Kill the tendermint process
/// 3. Check that the node detects this
/// 4. Check that the node shuts down
#[test]
fn test_anoma_shuts_down_if_tendermint_dies() -> Result<()> {
let test = setup::single_node_net()?;
disable_eth_fullnode(&test, &test.net.chain_id, &Who::Validator(0));
// 1. Run the ledger node
let mut ledger =
run_as!(test, Who::Validator(0), Bin::Node, &["ledger"], Some(40))?;
ledger.exp_string("Anoma ledger node started")?;
ledger.exp_string("Starting RPC HTTP server on")?;
// 2. Kill the tendermint node
sleep(1);
Command::new("pkill")
.args(&["tendermint"])
.spawn()
.expect("Test failed")
.wait()
.expect("Test failed");
// 3. Check that anoma detects that the tendermint node is dead
ledger.exp_string("Tendermint node is no longer running.")?;
// 4. Check that the ledger node shuts down
ledger.exp_string("Anoma ledger node has shut down.")?;
ledger.exp_eof()?;
Ok(())
}
/// In this test we:
/// 1. Run the ledger node
/// 2. Shut it down
/// 3. Run the ledger again, it should load its previous state
/// 4. Shut it down
/// 5. Reset the ledger's state
/// 6. Run the ledger again, it should start from fresh state
#[test]
fn run_ledger_load_state_and_reset() -> Result<()> {
let test = setup::single_node_net()?;
disable_eth_fullnode(&test, &test.net.chain_id, &Who::Validator(0));
// 1. Run the ledger node
let mut ledger =
run_as!(test, Who::Validator(0), Bin::Node, &["ledger"], Some(40))?;
ledger.exp_string("Anoma ledger node started")?;
// There should be no previous state
ledger.exp_string("No state could be found")?;
// Wait to commit a block
ledger.exp_regex(r"Committed block hash.*, height: [0-9]+")?;
// 2. Shut it down
ledger.send_control('c')?;
// Wait for the node to stop running to finish writing the state and tx
// queue
ledger.exp_string("Anoma ledger node has shut down.")?;
ledger.exp_eof()?;
drop(ledger);
// 3. Run the ledger again, it should load its previous state
let mut ledger =
run_as!(test, Who::Validator(0), Bin::Node, &["ledger"], Some(40))?;
ledger.exp_string("Anoma ledger node started")?;
// There should be previous state now
ledger.exp_string("Last state root hash:")?;
// 4. Shut it down
ledger.send_control('c')?;
// Wait for it to stop
ledger.exp_eof()?;
drop(ledger);
// 5. Reset the ledger's state
let mut session = run_as!(
test,
Who::Validator(0),
Bin::Node,
&["ledger", "reset"],
Some(10),
)?;
session.exp_eof()?;
// 6. Run the ledger again, it should start from fresh state
let mut session =
run_as!(test, Who::Validator(0), Bin::Node, &["ledger"], Some(40))?;
session.exp_string("Anoma ledger node started")?;
// There should be no previous state
session.exp_string("No state could be found")?;
Ok(())
}
/// In this test we:
/// 1. Run the ledger node
/// 2. Submit a token transfer tx
/// 3. Submit a transaction to update an account's validity predicate
/// 4. Submit a custom tx
/// 5. Submit a tx to initialize a new account
/// 6. Query token balance
/// 7. Query the raw bytes of a storage key
#[test]
fn ledger_txs_and_queries() -> Result<()> {
let test = setup::network(|genesis| genesis, None)?;
disable_eth_fullnode(&test, &test.net.chain_id, &Who::Validator(0));
// 1. Run the ledger node
let mut ledger =
run_as!(test, Who::Validator(0), Bin::Node, &["ledger"], Some(40))?;
ledger.exp_string("Starting RPC HTTP server on")?;
let _bg_ledger = ledger.background();
let vp_user = wasm_abs_path(VP_USER_WASM);
let vp_user = vp_user.to_string_lossy();
let tx_no_op = wasm_abs_path(TX_NO_OP_WASM);
let tx_no_op = tx_no_op.to_string_lossy();
let validator_one_rpc = get_actor_rpc(&test, &Who::Validator(0));
let txs_args = vec![
// 2. Submit a token transfer tx
vec![
"transfer",
"--source",
BERTHA,
"--target",
ALBERT,
"--token",
XAN,
"--amount",
"10.1",
"--fee-amount",
"0",
"--gas-limit",
"0",
"--fee-token",
XAN,
"--ledger-address",
&validator_one_rpc,
],
// 3. Submit a transaction to update an account's validity
// predicate
vec![
"update",
"--address",
BERTHA,
"--code-path",
&vp_user,
"--fee-amount",
"0",
"--gas-limit",
"0",
"--fee-token",
XAN,
"--ledger-address",
&validator_one_rpc,
],
// 4. Submit a custom tx
vec![
"tx",
"--signer",
BERTHA,
"--code-path",
&tx_no_op,
"--data-path",
"README.md",
"--fee-amount",
"0",
"--gas-limit",
"0",
"--fee-token",
XAN,
"--ledger-address",
&validator_one_rpc
],
// 5. Submit a tx to initialize a new account
vec![
"init-account",
"--source",
BERTHA,
"--public-key",
// Value obtained from `namada::types::key::ed25519::tests::gen_keypair`
"001be519a321e29020fa3cbfbfd01bd5e92db134305609270b71dace25b5a21168",
"--code-path",
&vp_user,
"--alias",
"Test-Account",
"--fee-amount",
"0",
"--gas-limit",
"0",
"--fee-token",
XAN,
"--ledger-address",
&validator_one_rpc,
],
];
for tx_args in &txs_args {
for &dry_run in &[true, false] {
let tx_args = if dry_run {
vec![tx_args.clone(), vec!["--dry-run"]].concat()
} else {
tx_args.clone()
};
let mut client = run!(test, Bin::Client, tx_args, Some(40))?;
if !dry_run {
client.exp_string("Transaction accepted")?;
client.exp_string("Transaction applied")?;
}
client.exp_string("Transaction is valid.")?;
client.assert_success();
}
}
let query_args_and_expected_response = vec![
// 6. Query token balance
(
vec![
"balance",
"--owner",
BERTHA,
"--token",
XAN,
"--ledger-address",
&validator_one_rpc,
],
// expect a decimal
r"XAN: \d+(\.\d+)?",
),
];
for (query_args, expected) in &query_args_and_expected_response {
let mut client = run!(test, Bin::Client, query_args, Some(40))?;
client.exp_regex(expected)?;
client.assert_success();
}
let christel = find_address(&test, CHRISTEL)?;
// as setup in `genesis/e2e-tests-single-node.toml`
let christel_balance = token::Amount::whole(1000000);
let xan = find_address(&test, XAN)?;
let storage_key = token::balance_key(&xan, &christel).to_string();
let query_args_and_expected_response = vec![
// 7. Query storage key and get hex-encoded raw bytes
(
vec![
"query-bytes",
"--storage-key",
&storage_key,
"--ledger-address",
&validator_one_rpc,
],
// expect hex encoded of borsh encoded bytes
HEXLOWER.encode(&christel_balance.try_to_vec().unwrap()),
),
];
for (query_args, expected) in &query_args_and_expected_response {
let mut client = run!(test, Bin::Client, query_args, Some(40))?;
client.exp_string(expected)?;
client.assert_success();
}
Ok(())
}
/// In this test we:
/// 1. Run the ledger node
/// 2. Submit an invalid transaction (disallowed by state machine)
/// 3. Shut down the ledger
/// 4. Restart the ledger
/// 5. Submit and invalid transactions (malformed)
#[test]
fn invalid_transactions() -> Result<()> {
let test = setup::single_node_net()?;
disable_eth_fullnode(&test, &test.net.chain_id, &Who::Validator(0));
// 1. Run the ledger node
let mut ledger =
run_as!(test, Who::Validator(0), Bin::Node, &["ledger"], Some(40))?;
ledger.exp_string("Anoma ledger node started")?;
ledger.exp_string("Starting RPC HTTP server on")?;
let bg_ledger = ledger.background();
// 2. Submit a an invalid transaction (trying to mint tokens should fail
// in the token's VP)
let tx_data_path = test.test_dir.path().join("tx.data");
let transfer = token::Transfer {
source: find_address(&test, DAEWON)?,
target: find_address(&test, ALBERT)?,
token: find_address(&test, XAN)?,
sub_prefix: None,
amount: token::Amount::whole(1),
};
let data = transfer
.try_to_vec()
.expect("Encoding unsigned transfer shouldn't fail");
let tx_wasm_path = wasm_abs_path(TX_MINT_TOKENS_WASM);
std::fs::write(&tx_data_path, data).unwrap();
let tx_wasm_path = tx_wasm_path.to_string_lossy();
let tx_data_path = tx_data_path.to_string_lossy();
let validator_one_rpc = get_actor_rpc(&test, &Who::Validator(0));
let tx_args = vec![
"tx",
"--code-path",
&tx_wasm_path,
"--data-path",
&tx_data_path,
"--signing-key",
DAEWON,
"--fee-amount",
"0",
"--gas-limit",
"0",
"--fee-token",
XAN,
"--ledger-address",
&validator_one_rpc,
];
let mut client = run!(test, Bin::Client, tx_args, Some(40))?;
client.exp_string("Transaction accepted")?;
client.exp_string("Transaction applied")?;
client.exp_string("Transaction is invalid")?;
client.exp_string(r#""code": "1"#)?;
client.assert_success();
let mut ledger = bg_ledger.foreground();
ledger.exp_string("some VPs rejected transaction")?;
// Wait to commit a block
ledger.exp_regex(r"Committed block hash.*, height: [0-9]+")?;
// 3. Shut it down
ledger.send_control('c')?;
// Wait for the node to stop running to finish writing the state and tx
// queue
ledger.exp_string("Anoma ledger node has shut down.")?;
ledger.exp_eof()?;
drop(ledger);
// 4. Restart the ledger
let mut ledger =
run_as!(test, Who::Validator(0), Bin::Node, &["ledger"], Some(40))?;
ledger.exp_string("Anoma ledger node started")?;
// There should be previous state now
ledger.exp_string("Last state root hash:")?;
let _bg_ledger = ledger.background();
// 5. Submit an invalid transactions (invalid token address)
let tx_args = vec![
"transfer",
"--source",
DAEWON,
"--signing-key",
DAEWON,
"--target",
ALBERT,
"--token",
BERTHA,
"--amount",
"1_000_000.1",
"--fee-amount",
"0",
"--gas-limit",
"0",
"--fee-token",
XAN,
// Force to ignore client check that fails on the balance check of the
// source address
"--force",
"--ledger-address",
&validator_one_rpc,
];
let mut client = run!(test, Bin::Client, tx_args, Some(40))?;
client.exp_string("Transaction accepted")?;
client.exp_string("Transaction applied")?;
client.exp_string("Error trying to apply a transaction")?;
client.exp_string(r#""code": "3"#)?;
client.assert_success();
Ok(())
}
/// PoS bonding, unbonding and withdrawal tests. In this test we:
///
/// 1. Run the ledger node with shorter epochs for faster progression
/// 2. Submit a self-bond for the genesis validator
/// 3. Submit a delegation to the genesis validator
/// 4. Submit an unbond of the self-bond
/// 5. Submit an unbond of the delegation
/// 6. Wait for the unbonding epoch
/// 7. Submit a withdrawal of the self-bond
/// 8. Submit a withdrawal of the delegation
#[test]
fn pos_bonds() -> Result<()> {
let unbonding_len = 2;
let test = setup::network(
|genesis| {
let parameters = ParametersConfig {
min_num_of_blocks: 2,
min_duration: 1,
max_expected_time_per_block: 1,
..genesis.parameters
};
let pos_params = PosParamsConfig {
pipeline_len: 1,
unbonding_len,
..genesis.pos_params
};
GenesisConfig {
parameters,
pos_params,
..genesis
}
},
None,
)?;
disable_eth_fullnode(&test, &test.net.chain_id, &Who::Validator(0));
// 1. Run the ledger node
let mut ledger =
run_as!(test, Who::Validator(0), Bin::Node, &["ledger"], Some(40))?;
ledger.exp_string("Starting RPC HTTP server on")?;
let _bg_ledger = ledger.background();
let validator_one_rpc = get_actor_rpc(&test, &Who::Validator(0));
// 2. Submit a self-bond for the gepnesis validator
let tx_args = vec![
"bond",
"--validator",
"validator-0",
"--amount",
"10.1",
"--fee-amount",
"0",
"--gas-limit",
"0",
"--fee-token",
XAN,
"--ledger-address",
&validator_one_rpc,
];
let mut client =
run_as!(test, Who::Validator(0), Bin::Client, tx_args, Some(40))?;
client.exp_string("Transaction applied with result:")?;
client.exp_string("Transaction is valid.")?;
client.assert_success();
// 3. Submit a delegation to the genesis validator
let tx_args = vec![
"bond",
"--validator",
"validator-0",
"--source",
BERTHA,
"--amount",
"10.1",
"--fee-amount",
"0",
"--gas-limit",
"0",
"--fee-token",
XAN,
"--ledger-address",
&validator_one_rpc,
];
let mut client = run!(test, Bin::Client, tx_args, Some(40))?;
client.exp_string("Transaction applied with result:")?;
client.exp_string("Transaction is valid.")?;
client.assert_success();
// 4. Submit an unbond of the self-bond
let tx_args = vec![
"unbond",
"--validator",
"validator-0",
"--amount",
"5.1",
"--fee-amount",
"0",
"--gas-limit",
"0",
"--fee-token",
XAN,
"--ledger-address",
&validator_one_rpc,
];
let mut client =
run_as!(test, Who::Validator(0), Bin::Client, tx_args, Some(40))?;
client.exp_string("Transaction applied with result:")?;
client.exp_string("Transaction is valid.")?;
client.assert_success();
// 5. Submit an unbond of the delegation
let tx_args = vec![
"unbond",
"--validator",
"validator-0",
"--source",
BERTHA,
"--amount",
"3.2",
"--fee-amount",
"0",
"--gas-limit",
"0",
"--fee-token",
XAN,
"--ledger-address",
&validator_one_rpc,
];
let mut client = run!(test, Bin::Client, tx_args, Some(40))?;
client.exp_string("Transaction applied with result:")?;
client.exp_string("Transaction is valid.")?;
client.assert_success();
// 6. Wait for the unbonding epoch
let epoch = get_epoch(&test, &validator_one_rpc)?;
let earliest_withdrawal_epoch = epoch + unbonding_len;
println!(
"Current epoch: {}, earliest epoch for withdrawal: {}",
epoch, earliest_withdrawal_epoch
);
let start = Instant::now();
let loop_timeout = Duration::new(20, 0);
loop {
if Instant::now().duration_since(start) > loop_timeout {
panic!(
"Timed out waiting for epoch: {}",
earliest_withdrawal_epoch
);
}
let epoch = get_epoch(&test, &validator_one_rpc)?;
if epoch >= earliest_withdrawal_epoch {
break;
}
}
// 7. Submit a withdrawal of the self-bond
let tx_args = vec![
"withdraw",
"--validator",
"validator-0",
"--fee-amount",
"0",
"--gas-limit",
"0",
"--fee-token",
XAN,
"--ledger-address",
&validator_one_rpc,
];
let mut client =
run_as!(test, Who::Validator(0), Bin::Client, tx_args, Some(40))?;
client.exp_string("Transaction applied with result:")?;
client.exp_string("Transaction is valid.")?;
client.assert_success();
// 8. Submit a withdrawal of the delegation
let tx_args = vec![
"withdraw",
"--validator",
"validator-0",
"--source",
BERTHA,
"--fee-amount",
"0",
"--gas-limit",
"0",
"--fee-token",
XAN,
"--ledger-address",
&validator_one_rpc,
];
let mut client = run!(test, Bin::Client, tx_args, Some(40))?;
client.exp_string("Transaction applied with result:")?;
client.exp_string("Transaction is valid.")?;
client.assert_success();
Ok(())
}
/// PoS validator creation test. In this test we:
///
/// 1. Run the ledger node with shorter epochs for faster progression
/// 2. Initialize a new validator account
/// 3. Submit a delegation to the new validator
/// 4. Transfer some XAN to the new validator
/// 5. Submit a self-bond for the new validator
/// 6. Wait for the pipeline epoch
/// 7. Check the new validator's voting power
#[test]
fn pos_init_validator() -> Result<()> {
let pipeline_len = 1;
let test = setup::network(
|genesis| {
let parameters = ParametersConfig {
min_num_of_blocks: 2,
min_duration: 1,
max_expected_time_per_block: 1,
..genesis.parameters
};
let pos_params = PosParamsConfig {
pipeline_len,
unbonding_len: 2,
..genesis.pos_params
};
GenesisConfig {
parameters,
pos_params,
..genesis
}
},
None,
)?;
disable_eth_fullnode(&test, &test.net.chain_id, &Who::Validator(0));
// 1. Run the ledger node
let mut ledger =
run_as!(test, Who::Validator(0), Bin::Node, &["ledger"], Some(40))?;
ledger.exp_string("Starting RPC HTTP server on")?;
let _bg_ledger = ledger.background();
let validator_one_rpc = get_actor_rpc(&test, &Who::Validator(0));
// 2. Initialize a new validator account
let new_validator = "new-validator";
let new_validator_key = format!("{}-key", new_validator);
let tx_args = vec![
"init-validator",
"--alias",
new_validator,
"--source",
BERTHA,
"--unsafe-dont-encrypt",
"--fee-amount",
"0",
"--gas-limit",
"0",
"--fee-token",
XAN,
"--ledger-address",
&validator_one_rpc,
];
let mut client = run!(test, Bin::Client, tx_args, Some(40))?;
client.exp_string("Transaction applied with result:")?;
client.exp_string("Transaction is valid.")?;
client.assert_success();
// 3. Submit a delegation to the new validator
// First, transfer some tokens to the validator's key for fees:
let tx_args = vec![
"transfer",
"--source",
BERTHA,
"--target",
&new_validator_key,
"--token",
XAN,
"--amount",
"0.5",
"--fee-amount",
"0",
"--gas-limit",
"0",
"--fee-token",
XAN,
"--ledger-address",
&validator_one_rpc,
];
let mut client = run!(test, Bin::Client, tx_args, Some(40))?;
client.exp_string("Transaction applied with result:")?;
client.exp_string("Transaction is valid.")?;
client.assert_success();
// Then self-bond the tokens:
let tx_args = vec![
"bond",
"--validator",
new_validator,
"--source",
BERTHA,
"--amount",
"1000.5",
"--fee-amount",
"0",
"--gas-limit",
"0",
"--fee-token",
XAN,
"--ledger-address",
&validator_one_rpc,
];
let mut client = run!(test, Bin::Client, tx_args, Some(40))?;
client.exp_string("Transaction applied with result:")?;
client.exp_string("Transaction is valid.")?;
client.assert_success();
// 4. Transfer some XAN to the new validator
let tx_args = vec![
"transfer",
"--source",
BERTHA,
"--target",
new_validator,
"--token",
XAN,
"--amount",
"10999.5",
"--fee-amount",
"0",
"--gas-limit",
"0",
"--fee-token",
XAN,
"--ledger-address",
&validator_one_rpc,
];
let mut client = run!(test, Bin::Client, tx_args, Some(40))?;
client.exp_string("Transaction applied with result:")?;
client.exp_string("Transaction is valid.")?;
client.assert_success();
// 5. Submit a self-bond for the new validator
let tx_args = vec![
"bond",
"--validator",
new_validator,
"--amount",
"10000",
"--fee-amount",
"0",
"--gas-limit",
"0",
"--fee-token",
XAN,
"--ledger-address",
&validator_one_rpc,
];
let mut client = run!(test, Bin::Client, tx_args, Some(40))?;
client.exp_string("Transaction applied with result:")?;
client.exp_string("Transaction is valid.")?;
client.assert_success();
// 6. Wait for the pipeline epoch when the validator's voting power should
// be non-zero
let epoch = get_epoch(&test, &validator_one_rpc)?;
let earliest_update_epoch = epoch + pipeline_len;
println!(
"Current epoch: {}, earliest epoch with updated voting power: {}",
epoch, earliest_update_epoch
);
let start = Instant::now();
let loop_timeout = Duration::new(20, 0);
loop {
if Instant::now().duration_since(start) > loop_timeout {
panic!("Timed out waiting for epoch: {}", earliest_update_epoch);
}
let epoch = get_epoch(&test, &validator_one_rpc)?;
if epoch >= earliest_update_epoch {
break;
}
}
// 7. Check the new validator's voting power
let voting_power =
find_voting_power(&test, new_validator, &validator_one_rpc)?;
assert_eq!(voting_power, 11);
Ok(())
}
/// Test that multiple txs submitted in the same block all get the tx result.
///
/// In this test we:
/// 1. Run the ledger node with 10s consensus timeout
/// 2. Spawn threads each submitting token transfer tx
#[test]
fn ledger_many_txs_in_a_block() -> Result<()> {
let test = Arc::new(setup::network(
|genesis| genesis,
// Set 10s consensus timeout to have more time to submit txs
Some("10s"),
)?);
disable_eth_fullnode(&test, &test.net.chain_id, &Who::Validator(0));
// 1. Run the ledger node
let mut ledger =
run_as!(*test, Who::Validator(0), Bin::Node, &["ledger"], Some(40))?;
ledger.exp_string("Starting RPC HTTP server on")?;
// Wait to commit a block
ledger.exp_regex(r"Committed block hash.*, height: [0-9]+")?;
let bg_ledger = ledger.background();
let validator_one_rpc = Arc::new(get_actor_rpc(&test, &Who::Validator(0)));
// A token transfer tx args
let tx_args = Arc::new(vec![
"transfer",
"--source",
BERTHA,
"--target",
ALBERT,
"--token",
XAN,
"--amount",
"10.1",
"--fee-amount",