forked from casper-network/casper-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbids.rs
More file actions
5210 lines (4469 loc) · 167 KB
/
bids.rs
File metadata and controls
5210 lines (4469 loc) · 167 KB
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
use std::{
collections::{BTreeMap, BTreeSet},
iter::FromIterator,
};
use assert_matches::assert_matches;
use num_traits::{One, Zero};
use once_cell::sync::Lazy;
use tempfile::TempDir;
use casper_engine_test_support::{
utils, ChainspecConfig, ExecuteRequestBuilder, LmdbWasmTestBuilder, StepRequestBuilder,
DEFAULT_ACCOUNTS, DEFAULT_ACCOUNT_ADDR, DEFAULT_ACCOUNT_INITIAL_BALANCE,
DEFAULT_CHAINSPEC_REGISTRY, DEFAULT_EXEC_CONFIG, DEFAULT_GENESIS_CONFIG_HASH,
DEFAULT_GENESIS_TIMESTAMP_MILLIS, DEFAULT_LOCKED_FUNDS_PERIOD_MILLIS, DEFAULT_PROTOCOL_VERSION,
DEFAULT_UNBONDING_DELAY, LOCAL_GENESIS_REQUEST, MINIMUM_ACCOUNT_CREATION_BALANCE, SYSTEM_ADDR,
TIMESTAMP_MILLIS_INCREMENT,
};
use casper_execution_engine::{
engine_state::{self, engine_config::DEFAULT_MINIMUM_DELEGATION_AMOUNT, Error},
execution::ExecError,
};
use casper_storage::data_access_layer::GenesisRequest;
use casper_types::{
self,
account::AccountHash,
addressable_entity::EntityKindTag,
api_error::ApiError,
runtime_args,
system::{
self,
auction::{
self, BidsExt, DelegationRate, EraValidators, Error as AuctionError, UnbondingPurses,
ValidatorWeights, ARG_AMOUNT, ARG_DELEGATION_RATE, ARG_DELEGATOR, ARG_ENTRY_POINT,
ARG_INACTIVE_VALIDATOR_UNDELEGATION_DELAY, ARG_NEW_PUBLIC_KEY, ARG_NEW_VALIDATOR,
ARG_PUBLIC_KEY, ARG_REWARDS_MAP, ARG_VALIDATOR, ERA_ID_KEY, INITIAL_ERA_ID,
METHOD_DISTRIBUTE,
},
},
EntityAddr, EraId, GenesisAccount, GenesisConfigBuilder, GenesisValidator, Key, Motes,
ProtocolVersion, PublicKey, SecretKey, U256, U512,
};
const ARG_TARGET: &str = "target";
const CONTRACT_TRANSFER_TO_ACCOUNT: &str = "transfer_to_account_u512.wasm";
const CONTRACT_ACTIVATE_BID: &str = "activate_bid.wasm";
const CONTRACT_ADD_BID: &str = "add_bid.wasm";
const CONTRACT_WITHDRAW_BID: &str = "withdraw_bid.wasm";
const CONTRACT_DELEGATE: &str = "delegate.wasm";
const CONTRACT_UNDELEGATE: &str = "undelegate.wasm";
const CONTRACT_REDELEGATE: &str = "redelegate.wasm";
const CONTRACT_CHANGE_BID_PUBLIC_KEY: &str = "change_bid_public_key.wasm";
const TRANSFER_AMOUNT: u64 = MINIMUM_ACCOUNT_CREATION_BALANCE + 1000;
const ADD_BID_AMOUNT_1: u64 = 95_000;
const ADD_BID_AMOUNT_2: u64 = 47_500;
const ADD_BID_AMOUNT_3: u64 = 200_000;
const ADD_BID_DELEGATION_RATE_1: DelegationRate = 10;
const BID_AMOUNT_2: u64 = 5_000;
const ADD_BID_DELEGATION_RATE_2: DelegationRate = 15;
const WITHDRAW_BID_AMOUNT_2: u64 = 15_000;
const ADD_BID_DELEGATION_RATE_3: DelegationRate = 20;
const DELEGATE_AMOUNT_1: u64 = 125_000 + DEFAULT_MINIMUM_DELEGATION_AMOUNT;
const DELEGATE_AMOUNT_2: u64 = 15_000 + DEFAULT_MINIMUM_DELEGATION_AMOUNT;
const UNDELEGATE_AMOUNT_1: u64 = 35_000;
const UNDELEGATE_AMOUNT_2: u64 = 5_000;
const SYSTEM_TRANSFER_AMOUNT: u64 = MINIMUM_ACCOUNT_CREATION_BALANCE;
const WEEK_MILLIS: u64 = 7 * 24 * 60 * 60 * 1000;
static NON_FOUNDER_VALIDATOR_1_PK: Lazy<PublicKey> = Lazy::new(|| {
let secret_key = SecretKey::ed25519_from_bytes([3; SecretKey::ED25519_LENGTH]).unwrap();
PublicKey::from(&secret_key)
});
static NON_FOUNDER_VALIDATOR_1_ADDR: Lazy<AccountHash> =
Lazy::new(|| AccountHash::from(&*NON_FOUNDER_VALIDATOR_1_PK));
static NON_FOUNDER_VALIDATOR_2_PK: Lazy<PublicKey> = Lazy::new(|| {
let secret_key = SecretKey::ed25519_from_bytes([4; SecretKey::ED25519_LENGTH]).unwrap();
PublicKey::from(&secret_key)
});
static NON_FOUNDER_VALIDATOR_2_ADDR: Lazy<AccountHash> =
Lazy::new(|| AccountHash::from(&*NON_FOUNDER_VALIDATOR_2_PK));
static NON_FOUNDER_VALIDATOR_3_PK: Lazy<PublicKey> = Lazy::new(|| {
let secret_key = SecretKey::ed25519_from_bytes([5; SecretKey::ED25519_LENGTH]).unwrap();
PublicKey::from(&secret_key)
});
static NON_FOUNDER_VALIDATOR_3_ADDR: Lazy<AccountHash> =
Lazy::new(|| AccountHash::from(&*NON_FOUNDER_VALIDATOR_3_PK));
static ACCOUNT_1_PK: Lazy<PublicKey> = Lazy::new(|| {
let secret_key = SecretKey::ed25519_from_bytes([200; SecretKey::ED25519_LENGTH]).unwrap();
PublicKey::from(&secret_key)
});
static ACCOUNT_1_ADDR: Lazy<AccountHash> = Lazy::new(|| AccountHash::from(&*ACCOUNT_1_PK));
const ACCOUNT_1_BALANCE: u64 = MINIMUM_ACCOUNT_CREATION_BALANCE;
const ACCOUNT_1_BOND: u64 = 100_000;
static ACCOUNT_2_PK: Lazy<PublicKey> = Lazy::new(|| {
let secret_key = SecretKey::ed25519_from_bytes([202; SecretKey::ED25519_LENGTH]).unwrap();
PublicKey::from(&secret_key)
});
static ACCOUNT_2_ADDR: Lazy<AccountHash> = Lazy::new(|| AccountHash::from(&*ACCOUNT_2_PK));
const ACCOUNT_2_BALANCE: u64 = MINIMUM_ACCOUNT_CREATION_BALANCE;
const ACCOUNT_2_BOND: u64 = 200_000;
static BID_ACCOUNT_1_PK: Lazy<PublicKey> = Lazy::new(|| {
let secret_key = SecretKey::ed25519_from_bytes([204; SecretKey::ED25519_LENGTH]).unwrap();
PublicKey::from(&secret_key)
});
static BID_ACCOUNT_1_ADDR: Lazy<AccountHash> = Lazy::new(|| AccountHash::from(&*BID_ACCOUNT_1_PK));
const BID_ACCOUNT_1_BALANCE: u64 = MINIMUM_ACCOUNT_CREATION_BALANCE;
static BID_ACCOUNT_2_PK: Lazy<PublicKey> = Lazy::new(|| {
let secret_key = SecretKey::ed25519_from_bytes([206; SecretKey::ED25519_LENGTH]).unwrap();
PublicKey::from(&secret_key)
});
static BID_ACCOUNT_2_ADDR: Lazy<AccountHash> = Lazy::new(|| AccountHash::from(&*BID_ACCOUNT_2_PK));
const BID_ACCOUNT_2_BALANCE: u64 = MINIMUM_ACCOUNT_CREATION_BALANCE;
static VALIDATOR_1: Lazy<PublicKey> = Lazy::new(|| {
let secret_key = SecretKey::ed25519_from_bytes([3; SecretKey::ED25519_LENGTH]).unwrap();
PublicKey::from(&secret_key)
});
static DELEGATOR_1: Lazy<PublicKey> = Lazy::new(|| {
let secret_key = SecretKey::ed25519_from_bytes([205; SecretKey::ED25519_LENGTH]).unwrap();
PublicKey::from(&secret_key)
});
static DELEGATOR_2: Lazy<PublicKey> = Lazy::new(|| {
let secret_key = SecretKey::ed25519_from_bytes([207; SecretKey::ED25519_LENGTH]).unwrap();
PublicKey::from(&secret_key)
});
static VALIDATOR_1_ADDR: Lazy<AccountHash> = Lazy::new(|| AccountHash::from(&*VALIDATOR_1));
static DELEGATOR_1_ADDR: Lazy<AccountHash> = Lazy::new(|| AccountHash::from(&*DELEGATOR_1));
static DELEGATOR_2_ADDR: Lazy<AccountHash> = Lazy::new(|| AccountHash::from(&*DELEGATOR_2));
const VALIDATOR_1_STAKE: u64 = 1_000_000;
const DELEGATOR_1_STAKE: u64 = 1_500_000 + DEFAULT_MINIMUM_DELEGATION_AMOUNT;
const DELEGATOR_1_BALANCE: u64 = DEFAULT_ACCOUNT_INITIAL_BALANCE;
const DELEGATOR_2_STAKE: u64 = 2_000_000 + DEFAULT_MINIMUM_DELEGATION_AMOUNT;
const DELEGATOR_2_BALANCE: u64 = DEFAULT_ACCOUNT_INITIAL_BALANCE;
const VALIDATOR_1_DELEGATION_RATE: DelegationRate = 0;
const EXPECTED_INITIAL_RELEASE_TIMESTAMP_MILLIS: u64 =
DEFAULT_GENESIS_TIMESTAMP_MILLIS + CASPER_LOCKED_FUNDS_PERIOD_MILLIS;
const WEEK_TIMESTAMPS: [u64; 14] = [
EXPECTED_INITIAL_RELEASE_TIMESTAMP_MILLIS,
EXPECTED_INITIAL_RELEASE_TIMESTAMP_MILLIS + WEEK_MILLIS,
EXPECTED_INITIAL_RELEASE_TIMESTAMP_MILLIS + (WEEK_MILLIS * 2),
EXPECTED_INITIAL_RELEASE_TIMESTAMP_MILLIS + (WEEK_MILLIS * 3),
EXPECTED_INITIAL_RELEASE_TIMESTAMP_MILLIS + (WEEK_MILLIS * 4),
EXPECTED_INITIAL_RELEASE_TIMESTAMP_MILLIS + (WEEK_MILLIS * 5),
EXPECTED_INITIAL_RELEASE_TIMESTAMP_MILLIS + (WEEK_MILLIS * 6),
EXPECTED_INITIAL_RELEASE_TIMESTAMP_MILLIS + (WEEK_MILLIS * 7),
EXPECTED_INITIAL_RELEASE_TIMESTAMP_MILLIS + (WEEK_MILLIS * 8),
EXPECTED_INITIAL_RELEASE_TIMESTAMP_MILLIS + (WEEK_MILLIS * 9),
EXPECTED_INITIAL_RELEASE_TIMESTAMP_MILLIS + (WEEK_MILLIS * 10),
EXPECTED_INITIAL_RELEASE_TIMESTAMP_MILLIS + (WEEK_MILLIS * 11),
EXPECTED_INITIAL_RELEASE_TIMESTAMP_MILLIS + (WEEK_MILLIS * 12),
EXPECTED_INITIAL_RELEASE_TIMESTAMP_MILLIS + (WEEK_MILLIS * 13),
];
const DAY_MILLIS: u64 = 24 * 60 * 60 * 1000;
const CASPER_VESTING_SCHEDULE_PERIOD_MILLIS: u64 = 91 * DAY_MILLIS;
const CASPER_LOCKED_FUNDS_PERIOD_MILLIS: u64 = 90 * DAY_MILLIS;
#[ignore]
#[test]
fn should_add_new_bid() {
let accounts = {
let mut tmp: Vec<GenesisAccount> = DEFAULT_ACCOUNTS.clone();
let account_1 = GenesisAccount::account(
BID_ACCOUNT_1_PK.clone(),
Motes::new(BID_ACCOUNT_1_BALANCE),
None,
);
tmp.push(account_1);
tmp
};
let run_genesis_request = utils::create_run_genesis_request(accounts);
let mut builder = LmdbWasmTestBuilder::default();
builder.run_genesis(run_genesis_request);
let exec_request_1 = ExecuteRequestBuilder::standard(
*BID_ACCOUNT_1_ADDR,
CONTRACT_ADD_BID,
runtime_args! {
ARG_PUBLIC_KEY => BID_ACCOUNT_1_PK.clone(),
ARG_AMOUNT => U512::from(ADD_BID_AMOUNT_1),
ARG_DELEGATION_RATE => ADD_BID_DELEGATION_RATE_1,
},
)
.build();
builder.exec(exec_request_1).expect_success().commit();
let bids = builder.get_bids();
assert_eq!(bids.len(), 1);
let active_bid = bids.validator_bid(&BID_ACCOUNT_1_PK.clone()).unwrap();
assert_eq!(
builder.get_purse_balance(*active_bid.bonding_purse()),
U512::from(ADD_BID_AMOUNT_1)
);
assert_eq!(*active_bid.delegation_rate(), ADD_BID_DELEGATION_RATE_1);
}
#[ignore]
#[test]
fn should_increase_existing_bid() {
let accounts = {
let mut tmp: Vec<GenesisAccount> = DEFAULT_ACCOUNTS.clone();
let account_1 = GenesisAccount::account(
BID_ACCOUNT_1_PK.clone(),
Motes::new(BID_ACCOUNT_1_BALANCE),
None,
);
tmp.push(account_1);
tmp
};
let run_genesis_request = utils::create_run_genesis_request(accounts);
let mut builder = LmdbWasmTestBuilder::default();
builder.run_genesis(run_genesis_request);
let exec_request_1 = ExecuteRequestBuilder::standard(
*BID_ACCOUNT_1_ADDR,
CONTRACT_ADD_BID,
runtime_args! {
ARG_PUBLIC_KEY => BID_ACCOUNT_1_PK.clone(),
ARG_AMOUNT => U512::from(ADD_BID_AMOUNT_1),
ARG_DELEGATION_RATE => ADD_BID_DELEGATION_RATE_1,
},
)
.build();
builder.exec(exec_request_1).expect_success().commit();
// 2nd bid top-up
let exec_request_2 = ExecuteRequestBuilder::standard(
*BID_ACCOUNT_1_ADDR,
CONTRACT_ADD_BID,
runtime_args! {
ARG_PUBLIC_KEY => BID_ACCOUNT_1_PK.clone(),
ARG_AMOUNT => U512::from(BID_AMOUNT_2),
ARG_DELEGATION_RATE => ADD_BID_DELEGATION_RATE_2,
},
)
.build();
builder.exec(exec_request_2).expect_success().commit();
let bids = builder.get_bids();
assert_eq!(bids.len(), 1);
let active_bid = bids.validator_bid(&BID_ACCOUNT_1_PK.clone()).unwrap();
assert_eq!(
builder.get_purse_balance(*active_bid.bonding_purse()),
U512::from(ADD_BID_AMOUNT_1 + BID_AMOUNT_2)
);
assert_eq!(*active_bid.delegation_rate(), ADD_BID_DELEGATION_RATE_2);
}
#[ignore]
#[test]
fn should_decrease_existing_bid() {
let accounts = {
let mut tmp: Vec<GenesisAccount> = DEFAULT_ACCOUNTS.clone();
let account_1 = GenesisAccount::account(
BID_ACCOUNT_1_PK.clone(),
Motes::new(BID_ACCOUNT_1_BALANCE),
None,
);
tmp.push(account_1);
tmp
};
let run_genesis_request = utils::create_run_genesis_request(accounts);
let mut builder = LmdbWasmTestBuilder::default();
builder.run_genesis(run_genesis_request);
let bid_request = ExecuteRequestBuilder::standard(
*BID_ACCOUNT_1_ADDR,
CONTRACT_ADD_BID,
runtime_args! {
ARG_PUBLIC_KEY => BID_ACCOUNT_1_PK.clone(),
ARG_AMOUNT => U512::from(ADD_BID_AMOUNT_1),
ARG_DELEGATION_RATE => ADD_BID_DELEGATION_RATE_1,
},
)
.build();
builder.exec(bid_request).expect_success().commit();
// withdraw some amount
let withdraw_request = ExecuteRequestBuilder::standard(
*BID_ACCOUNT_1_ADDR,
CONTRACT_WITHDRAW_BID,
runtime_args! {
ARG_PUBLIC_KEY => BID_ACCOUNT_1_PK.clone(),
ARG_AMOUNT => U512::from(WITHDRAW_BID_AMOUNT_2),
},
)
.build();
builder.exec(withdraw_request).commit().expect_success();
let bids = builder.get_bids();
assert_eq!(bids.len(), 1);
let active_bid = bids.validator_bid(&BID_ACCOUNT_1_PK.clone()).unwrap();
assert_eq!(
builder.get_purse_balance(*active_bid.bonding_purse()),
// Since we don't pay out immediately `WITHDRAW_BID_AMOUNT_2` is locked in unbonding queue
U512::from(ADD_BID_AMOUNT_1)
);
let unbonding_purses: UnbondingPurses = builder.get_unbonds();
let unbond_list = unbonding_purses
.get(&BID_ACCOUNT_1_ADDR)
.expect("should have unbonded");
assert_eq!(unbond_list.len(), 1);
let unbonding_purse = unbond_list[0].clone();
assert_eq!(unbonding_purse.unbonder_public_key(), &*BID_ACCOUNT_1_PK);
assert_eq!(unbonding_purse.validator_public_key(), &*BID_ACCOUNT_1_PK);
// `WITHDRAW_BID_AMOUNT_2` is in unbonding list
assert_eq!(unbonding_purse.amount(), &U512::from(WITHDRAW_BID_AMOUNT_2),);
assert_eq!(unbonding_purse.era_of_creation(), INITIAL_ERA_ID,);
}
#[ignore]
#[test]
fn should_run_delegate_and_undelegate() {
let accounts = {
let mut tmp: Vec<GenesisAccount> = DEFAULT_ACCOUNTS.clone();
let account_1 = GenesisAccount::account(
BID_ACCOUNT_1_PK.clone(),
Motes::new(BID_ACCOUNT_1_BALANCE),
None,
);
tmp.push(account_1);
tmp
};
let run_genesis_request = utils::create_run_genesis_request(accounts);
let mut builder = LmdbWasmTestBuilder::default();
builder.run_genesis(run_genesis_request);
let transfer_request_1 = ExecuteRequestBuilder::standard(
*DEFAULT_ACCOUNT_ADDR,
CONTRACT_TRANSFER_TO_ACCOUNT,
runtime_args! {
ARG_TARGET => *SYSTEM_ADDR,
ARG_AMOUNT => U512::from(TRANSFER_AMOUNT)
},
)
.build();
let transfer_request_2 = ExecuteRequestBuilder::standard(
*DEFAULT_ACCOUNT_ADDR,
CONTRACT_TRANSFER_TO_ACCOUNT,
runtime_args! {
ARG_TARGET => *NON_FOUNDER_VALIDATOR_1_ADDR,
ARG_AMOUNT => U512::from(TRANSFER_AMOUNT)
},
)
.build();
// non-founding validator request
let add_bid_request_1 = ExecuteRequestBuilder::standard(
*NON_FOUNDER_VALIDATOR_1_ADDR,
CONTRACT_ADD_BID,
runtime_args! {
ARG_PUBLIC_KEY => NON_FOUNDER_VALIDATOR_1_PK.clone(),
ARG_AMOUNT => U512::from(ADD_BID_AMOUNT_1),
ARG_DELEGATION_RATE => ADD_BID_DELEGATION_RATE_1,
},
)
.build();
builder.exec(transfer_request_1).expect_success().commit();
builder.exec(transfer_request_2).expect_success().commit();
builder.exec(add_bid_request_1).expect_success().commit();
let auction_hash = builder.get_auction_contract_hash();
let bids = builder.get_bids();
assert_eq!(bids.len(), 1);
let active_bid = bids.validator_bid(&NON_FOUNDER_VALIDATOR_1_PK).unwrap();
assert_eq!(
builder.get_purse_balance(*active_bid.bonding_purse()),
U512::from(ADD_BID_AMOUNT_1)
);
assert_eq!(*active_bid.delegation_rate(), ADD_BID_DELEGATION_RATE_1);
let auction_key = Key::addressable_entity_key(EntityKindTag::System, auction_hash);
let auction_stored_value = builder
.query(None, auction_key, &[])
.expect("should query auction hash");
let _auction = auction_stored_value
.as_addressable_entity()
.expect("should be contract");
//
let exec_request_1 = ExecuteRequestBuilder::standard(
*BID_ACCOUNT_1_ADDR,
CONTRACT_DELEGATE,
runtime_args! {
ARG_AMOUNT => U512::from(DELEGATE_AMOUNT_1),
ARG_VALIDATOR => NON_FOUNDER_VALIDATOR_1_PK.clone(),
ARG_DELEGATOR => BID_ACCOUNT_1_PK.clone(),
},
)
.build();
builder.exec(exec_request_1).commit().expect_success();
let bids = builder.get_bids();
assert_eq!(bids.len(), 2);
let delegators = bids
.delegators_by_validator_public_key(&NON_FOUNDER_VALIDATOR_1_PK)
.expect("should have delegators");
assert_eq!(delegators.len(), 1);
let delegator = bids
.delegator_by_public_keys(&NON_FOUNDER_VALIDATOR_1_PK, &BID_ACCOUNT_1_PK)
.expect("should have account1 delegation");
let delegated_amount_1 = delegator.staked_amount();
assert_eq!(delegated_amount_1, U512::from(DELEGATE_AMOUNT_1));
// 2nd bid top-up
let exec_request_2 = ExecuteRequestBuilder::standard(
*BID_ACCOUNT_1_ADDR,
CONTRACT_DELEGATE,
runtime_args! {
ARG_AMOUNT => U512::from(DELEGATE_AMOUNT_2),
ARG_VALIDATOR => NON_FOUNDER_VALIDATOR_1_PK.clone(),
ARG_DELEGATOR => BID_ACCOUNT_1_PK.clone(),
},
)
.build();
builder.exec(exec_request_2).commit().expect_success();
let bids = builder.get_bids();
assert_eq!(bids.len(), 2);
let delegators = bids
.delegators_by_validator_public_key(&NON_FOUNDER_VALIDATOR_1_PK)
.expect("should have delegators");
assert_eq!(delegators.len(), 1);
let delegator = bids
.delegator_by_public_keys(&NON_FOUNDER_VALIDATOR_1_PK, &BID_ACCOUNT_1_PK)
.expect("should have account1 delegation");
let delegated_amount_1 = delegator.staked_amount();
assert_eq!(
delegated_amount_1,
U512::from(DELEGATE_AMOUNT_1 + DELEGATE_AMOUNT_2)
);
let exec_request_3 = ExecuteRequestBuilder::standard(
*BID_ACCOUNT_1_ADDR,
CONTRACT_UNDELEGATE,
runtime_args! {
ARG_AMOUNT => U512::from(UNDELEGATE_AMOUNT_1),
ARG_VALIDATOR => NON_FOUNDER_VALIDATOR_1_PK.clone(),
ARG_DELEGATOR => BID_ACCOUNT_1_PK.clone(),
},
)
.build();
builder.exec(exec_request_3).expect_success().commit();
let bids = builder.get_bids();
assert_eq!(bids.len(), 2);
let delegators = bids
.delegators_by_validator_public_key(&NON_FOUNDER_VALIDATOR_1_PK)
.expect("should have delegators");
assert_eq!(delegators.len(), 1);
let delegator = bids
.delegator_by_public_keys(&NON_FOUNDER_VALIDATOR_1_PK, &BID_ACCOUNT_1_PK)
.expect("should have account1 delegation");
let delegated_amount_1 = delegator.staked_amount();
assert_eq!(
delegated_amount_1,
U512::from(DELEGATE_AMOUNT_1 + DELEGATE_AMOUNT_2 - UNDELEGATE_AMOUNT_1)
);
let unbonding_purses: UnbondingPurses = builder.get_unbonds();
assert_eq!(unbonding_purses.len(), 1);
let unbond_list = unbonding_purses
.get(&BID_ACCOUNT_1_ADDR)
.expect("should have unbonding purse for non founder validator");
assert_eq!(unbond_list.len(), 1);
assert_eq!(
unbond_list[0].validator_public_key(),
&*NON_FOUNDER_VALIDATOR_1_PK
);
assert_eq!(unbond_list[0].unbonder_public_key(), &*BID_ACCOUNT_1_PK);
assert_eq!(unbond_list[0].amount(), &U512::from(UNDELEGATE_AMOUNT_1));
assert!(!unbond_list[0].is_validator());
assert_eq!(unbond_list[0].era_of_creation(), INITIAL_ERA_ID);
}
#[ignore]
#[test]
fn should_calculate_era_validators() {
assert_ne!(*ACCOUNT_1_ADDR, *ACCOUNT_2_ADDR,);
assert_ne!(*ACCOUNT_2_ADDR, *BID_ACCOUNT_1_ADDR,);
assert_ne!(*ACCOUNT_2_ADDR, *DEFAULT_ACCOUNT_ADDR,);
let accounts = {
let mut tmp: Vec<GenesisAccount> = DEFAULT_ACCOUNTS.clone();
let account_1 = GenesisAccount::account(
ACCOUNT_1_PK.clone(),
Motes::new(ACCOUNT_1_BALANCE),
Some(GenesisValidator::new(
Motes::new(ACCOUNT_1_BOND),
DelegationRate::zero(),
)),
);
let account_2 = GenesisAccount::account(
ACCOUNT_2_PK.clone(),
Motes::new(ACCOUNT_2_BALANCE),
Some(GenesisValidator::new(
Motes::new(ACCOUNT_2_BOND),
DelegationRate::zero(),
)),
);
let account_3 = GenesisAccount::account(
BID_ACCOUNT_1_PK.clone(),
Motes::new(BID_ACCOUNT_1_BALANCE),
None,
);
tmp.push(account_1);
tmp.push(account_2);
tmp.push(account_3);
tmp
};
let run_genesis_request = utils::create_run_genesis_request(accounts);
let mut builder = LmdbWasmTestBuilder::default();
builder.run_genesis(run_genesis_request);
let transfer_request_1 = ExecuteRequestBuilder::standard(
*DEFAULT_ACCOUNT_ADDR,
CONTRACT_TRANSFER_TO_ACCOUNT,
runtime_args! {
ARG_TARGET => *SYSTEM_ADDR,
ARG_AMOUNT => U512::from(TRANSFER_AMOUNT)
},
)
.build();
let transfer_request_2 = ExecuteRequestBuilder::standard(
*DEFAULT_ACCOUNT_ADDR,
CONTRACT_TRANSFER_TO_ACCOUNT,
runtime_args! {
ARG_TARGET => *NON_FOUNDER_VALIDATOR_1_ADDR,
ARG_AMOUNT => U512::from(TRANSFER_AMOUNT)
},
)
.build();
let auction_hash = builder.get_auction_contract_hash();
let bids = builder.get_bids();
assert_eq!(bids.len(), 2, "founding validators {:?}", bids);
// Verify first era validators
let first_validator_weights: ValidatorWeights = builder
.get_validator_weights(INITIAL_ERA_ID)
.expect("should have first era validator weights");
assert_eq!(
first_validator_weights
.keys()
.cloned()
.collect::<BTreeSet<_>>(),
BTreeSet::from_iter(vec![ACCOUNT_1_PK.clone(), ACCOUNT_2_PK.clone()])
);
builder.exec(transfer_request_1).commit().expect_success();
builder.exec(transfer_request_2).commit().expect_success();
// non-founding validator request
let add_bid_request_1 = ExecuteRequestBuilder::standard(
*BID_ACCOUNT_1_ADDR,
CONTRACT_ADD_BID,
runtime_args! {
ARG_PUBLIC_KEY => BID_ACCOUNT_1_PK.clone(),
ARG_AMOUNT => U512::from(ADD_BID_AMOUNT_1),
ARG_DELEGATION_RATE => ADD_BID_DELEGATION_RATE_1,
},
)
.build();
builder.exec(add_bid_request_1).commit().expect_success();
let pre_era_id: EraId = builder.get_value(EntityAddr::System(auction_hash.value()), ERA_ID_KEY);
assert_eq!(pre_era_id, EraId::from(0));
builder.run_auction(
DEFAULT_GENESIS_TIMESTAMP_MILLIS + DEFAULT_LOCKED_FUNDS_PERIOD_MILLIS,
Vec::new(),
);
let post_era_id: EraId =
builder.get_value(EntityAddr::System(auction_hash.value()), ERA_ID_KEY);
assert_eq!(post_era_id, EraId::from(1));
let era_validators: EraValidators = builder.get_era_validators();
// Check if there are no missing eras after the calculation, but we don't care about what the
// elements are
let auction_delay = builder.get_auction_delay();
let eras: Vec<_> = era_validators.keys().copied().collect();
assert!(!era_validators.is_empty());
assert!(era_validators.len() >= auction_delay as usize); // definitely more than 1 element
let (first_era, _) = era_validators.iter().min().unwrap();
let (last_era, _) = era_validators.iter().max().unwrap();
let expected_eras: Vec<EraId> = {
let lo: u64 = (*first_era).into();
let hi: u64 = (*last_era).into();
(lo..=hi).map(EraId::from).collect()
};
assert_eq!(eras, expected_eras, "Eras {:?}", eras);
assert!(post_era_id > EraId::from(0));
let consensus_next_era_id: EraId = post_era_id + auction_delay + 1;
let snapshot_size = auction_delay as usize + 1;
assert_eq!(
era_validators.len(),
snapshot_size,
"era_id={} {:?}",
consensus_next_era_id,
era_validators
); // eraindex==1 - ran once
let lookup_era_id = consensus_next_era_id - 1;
let validator_weights = era_validators
.get(&lookup_era_id) // indexed from 0
.unwrap_or_else(|| {
panic!(
"should have era_index=={} entry {:?}",
consensus_next_era_id, era_validators
)
});
assert_eq!(
validator_weights.len(),
3,
"{:?} {:?}",
era_validators,
validator_weights
); //2 genesis validators "winners"
assert_eq!(
validator_weights
.get(&BID_ACCOUNT_1_PK)
.expect("should have bid account in this era"),
&U512::from(ADD_BID_AMOUNT_1)
);
// Check validator weights using the API
let era_validators_result = builder
.get_validator_weights(lookup_era_id)
.expect("should have validator weights");
assert_eq!(era_validators_result, *validator_weights);
// Make sure looked up era validators are different than initial era validators
assert_ne!(era_validators_result, first_validator_weights);
}
#[ignore]
#[test]
fn should_get_first_seigniorage_recipients() {
let accounts = {
let mut tmp: Vec<GenesisAccount> = DEFAULT_ACCOUNTS.clone();
let account_1 = GenesisAccount::account(
ACCOUNT_1_PK.clone(),
Motes::new(ACCOUNT_1_BALANCE),
Some(GenesisValidator::new(
Motes::new(ACCOUNT_1_BOND),
DelegationRate::zero(),
)),
);
let account_2 = GenesisAccount::account(
ACCOUNT_2_PK.clone(),
Motes::new(ACCOUNT_2_BALANCE),
Some(GenesisValidator::new(
Motes::new(ACCOUNT_2_BOND),
DelegationRate::zero(),
)),
);
tmp.push(account_1);
tmp.push(account_2);
tmp
};
// We can't use `utils::create_run_genesis_request` as the snapshot used an auction delay of 3.
let auction_delay = 3;
let exec_config = GenesisConfigBuilder::new()
.with_accounts(accounts)
.with_auction_delay(auction_delay)
.with_locked_funds_period_millis(CASPER_LOCKED_FUNDS_PERIOD_MILLIS)
.build();
let run_genesis_request = GenesisRequest::new(
DEFAULT_GENESIS_CONFIG_HASH,
DEFAULT_PROTOCOL_VERSION,
exec_config,
DEFAULT_CHAINSPEC_REGISTRY.clone(),
);
let mut builder = LmdbWasmTestBuilder::default();
builder.run_genesis(run_genesis_request);
let transfer_request_1 = ExecuteRequestBuilder::standard(
*DEFAULT_ACCOUNT_ADDR,
CONTRACT_TRANSFER_TO_ACCOUNT,
runtime_args! {
ARG_TARGET => *SYSTEM_ADDR,
ARG_AMOUNT => U512::from(TRANSFER_AMOUNT)
},
)
.build();
let bids = builder.get_bids();
assert_eq!(bids.len(), 2);
let founding_validator_1 = bids
.validator_bid(&ACCOUNT_1_PK)
.expect("should have account 1 pk");
assert_eq!(
founding_validator_1
.vesting_schedule()
.map(|vesting_schedule| vesting_schedule.initial_release_timestamp_millis()),
Some(DEFAULT_GENESIS_TIMESTAMP_MILLIS + CASPER_LOCKED_FUNDS_PERIOD_MILLIS)
);
let founding_validator_2 = bids
.validator_bid(&ACCOUNT_2_PK)
.expect("should have account 2 pk");
assert_eq!(
founding_validator_2
.vesting_schedule()
.map(|vesting_schedule| vesting_schedule.initial_release_timestamp_millis()),
Some(DEFAULT_GENESIS_TIMESTAMP_MILLIS + CASPER_LOCKED_FUNDS_PERIOD_MILLIS)
);
builder.exec(transfer_request_1).commit().expect_success();
// run_auction should be executed first
builder.run_auction(
DEFAULT_GENESIS_TIMESTAMP_MILLIS + CASPER_LOCKED_FUNDS_PERIOD_MILLIS,
Vec::new(),
);
let mut era_validators: EraValidators = builder.get_era_validators();
let auction_delay = builder.get_auction_delay();
let snapshot_size = auction_delay as usize + 1;
assert_eq!(era_validators.len(), snapshot_size, "{:?}", era_validators); // eraindex==1 - ran once
assert!(era_validators.contains_key(&(EraId::from(auction_delay).successor())));
let era_id = EraId::from(auction_delay);
let validator_weights = era_validators.remove(&era_id).unwrap_or_else(|| {
panic!(
"should have era_index=={} entry {:?}",
era_id, era_validators
)
});
// 2 genesis validators "winners" with non-zero bond
assert_eq!(validator_weights.len(), 2, "{:?}", validator_weights);
assert_eq!(
validator_weights.get(&ACCOUNT_1_PK).unwrap(),
&U512::from(ACCOUNT_1_BOND)
);
assert_eq!(
validator_weights.get(&ACCOUNT_2_PK).unwrap(),
&U512::from(ACCOUNT_2_BOND)
);
let first_validator_weights = builder
.get_validator_weights(era_id)
.expect("should have validator weights");
assert_eq!(first_validator_weights, validator_weights);
}
#[ignore]
#[test]
fn should_release_founder_stake() {
const NEW_MINIMUM_DELEGATION_AMOUNT: u64 = 0;
// ACCOUNT_1_BOND / 14 = 7_142
const EXPECTED_WEEKLY_RELEASE: u64 = 7_142;
const EXPECTED_REMAINDER: u64 = 12;
const EXPECTED_LOCKED_AMOUNTS: [u64; 14] = [
92858, 85716, 78574, 71432, 64290, 57148, 50006, 42864, 35722, 28580, 21438, 14296, 7154, 0,
];
let expected_locked_amounts: Vec<U512> = EXPECTED_LOCKED_AMOUNTS
.iter()
.cloned()
.map(U512::from)
.collect();
let expect_unbond_success = |builder: &mut LmdbWasmTestBuilder, amount: u64| {
let partial_unbond = ExecuteRequestBuilder::standard(
*ACCOUNT_1_ADDR,
CONTRACT_WITHDRAW_BID,
runtime_args! {
ARG_PUBLIC_KEY => ACCOUNT_1_PK.clone(),
ARG_AMOUNT => U512::from(amount),
},
)
.build();
builder.exec(partial_unbond).commit().expect_success();
};
let expect_unbond_failure = |builder: &mut LmdbWasmTestBuilder, amount: u64| {
let full_unbond = ExecuteRequestBuilder::standard(
*ACCOUNT_1_ADDR,
CONTRACT_WITHDRAW_BID,
runtime_args! {
ARG_PUBLIC_KEY => ACCOUNT_1_PK.clone(),
ARG_AMOUNT => U512::from(amount),
},
)
.build();
builder.exec(full_unbond).commit();
let error = builder
.get_last_exec_result()
.expect("should have last exec result")
.error()
.cloned()
.expect("should have error");
assert_matches!(
error,
engine_state::Error::Exec(ExecError::Revert(ApiError::AuctionError(15)))
);
};
let accounts = {
let mut tmp: Vec<GenesisAccount> = DEFAULT_ACCOUNTS.clone();
let account_1 = GenesisAccount::account(
ACCOUNT_1_PK.clone(),
Motes::new(ACCOUNT_1_BALANCE),
Some(GenesisValidator::new(
Motes::new(ACCOUNT_1_BOND),
DelegationRate::zero(),
)),
);
tmp.push(account_1);
tmp
};
//let run_genesis_request = utils::create_run_genesis_request(accounts);
let run_genesis_request = {
let exec_config = GenesisConfigBuilder::default()
.with_accounts(accounts)
.with_locked_funds_period_millis(CASPER_LOCKED_FUNDS_PERIOD_MILLIS)
.build();
GenesisRequest::new(
DEFAULT_GENESIS_CONFIG_HASH,
DEFAULT_PROTOCOL_VERSION,
exec_config,
DEFAULT_CHAINSPEC_REGISTRY.clone(),
)
};
let chainspec = ChainspecConfig::default()
.with_minimum_delegation_amount(NEW_MINIMUM_DELEGATION_AMOUNT)
.with_vesting_schedule_period_millis(CASPER_VESTING_SCHEDULE_PERIOD_MILLIS);
let mut builder = LmdbWasmTestBuilder::new_temporary_with_config(chainspec);
builder.run_genesis(run_genesis_request);
let fund_system_account = ExecuteRequestBuilder::standard(
*DEFAULT_ACCOUNT_ADDR,
CONTRACT_TRANSFER_TO_ACCOUNT,
runtime_args! {
ARG_TARGET => *SYSTEM_ADDR,
ARG_AMOUNT => U512::from(DEFAULT_ACCOUNT_INITIAL_BALANCE / 10)
},
)
.build();
builder.exec(fund_system_account).commit().expect_success();
// Check bid and its vesting schedule
{
let bids = builder.get_bids();
assert_eq!(bids.len(), 1);
let entry = bids.validator_bid(&ACCOUNT_1_PK).unwrap();
let vesting_schedule = entry.vesting_schedule().unwrap();
let initial_release = vesting_schedule.initial_release_timestamp_millis();
assert_eq!(initial_release, EXPECTED_INITIAL_RELEASE_TIMESTAMP_MILLIS);
let locked_amounts = vesting_schedule.locked_amounts().map(|arr| arr.to_vec());
assert!(locked_amounts.is_none());
}
builder.run_auction(DEFAULT_GENESIS_TIMESTAMP_MILLIS, Vec::new());
{
// Attempt unbond of one mote
expect_unbond_failure(&mut builder, u64::one());
}
builder.run_auction(WEEK_TIMESTAMPS[0], Vec::new());
// Check bid and its vesting schedule
{
let bids = builder.get_bids();
assert_eq!(bids.len(), 1);
let entry = bids.validator_bid(&ACCOUNT_1_PK).unwrap();
let vesting_schedule = entry.vesting_schedule().unwrap();
let initial_release = vesting_schedule.initial_release_timestamp_millis();
assert_eq!(initial_release, EXPECTED_INITIAL_RELEASE_TIMESTAMP_MILLIS);
let locked_amounts = vesting_schedule.locked_amounts().map(|arr| arr.to_vec());
assert_eq!(locked_amounts, Some(expected_locked_amounts));
}
let mut total_unbonded = 0;
{
// Attempt full unbond
expect_unbond_failure(&mut builder, ACCOUNT_1_BOND);
// Attempt unbond of released amount
expect_unbond_success(&mut builder, EXPECTED_WEEKLY_RELEASE);
total_unbonded += EXPECTED_WEEKLY_RELEASE;
assert_eq!(ACCOUNT_1_BOND - total_unbonded, EXPECTED_LOCKED_AMOUNTS[0])
}
for i in 1..13 {
// Run auction forward by almost a week
builder.run_auction(WEEK_TIMESTAMPS[i] - 1, Vec::new());
// Attempt unbond of 1 mote
expect_unbond_failure(&mut builder, u64::one());
// Run auction forward by one millisecond
builder.run_auction(WEEK_TIMESTAMPS[i], Vec::new());
// Attempt unbond of more than weekly release
expect_unbond_failure(&mut builder, EXPECTED_WEEKLY_RELEASE + 1);
// Attempt unbond of released amount
expect_unbond_success(&mut builder, EXPECTED_WEEKLY_RELEASE);
total_unbonded += EXPECTED_WEEKLY_RELEASE;
assert_eq!(ACCOUNT_1_BOND - total_unbonded, EXPECTED_LOCKED_AMOUNTS[i])
}
{
// Run auction forward by almost a week
builder.run_auction(WEEK_TIMESTAMPS[13] - 1, Vec::new());
// Attempt unbond of 1 mote
expect_unbond_failure(&mut builder, u64::one());
// Run auction forward by one millisecond
builder.run_auction(WEEK_TIMESTAMPS[13], Vec::new());
// Attempt unbond of released amount + remainder
expect_unbond_success(&mut builder, EXPECTED_WEEKLY_RELEASE + EXPECTED_REMAINDER);