-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathtests.rs
More file actions
620 lines (549 loc) · 18.3 KB
/
tests.rs
File metadata and controls
620 lines (549 loc) · 18.3 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
// This file is part of Substrate.
// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! Tests for pallet-delegated-staking.
use super::*;
use crate::mock::*;
use frame_support::{assert_noop, assert_ok, traits::fungible::InspectHold};
use pallet_staking::Error as StakingError;
#[test]
fn create_a_agent_with_first_delegator() {
ExtBuilder::default().build_and_execute(|| {
let agent: AccountId = 200;
let reward_account: AccountId = 201;
let delegator: AccountId = 202;
// set intention to accept delegation.
fund(&agent, 1000);
assert_ok!(DelegatedStaking::register_agent(
RawOrigin::Signed(agent).into(),
reward_account
));
// delegate to this account
fund(&delegator, 1000);
assert_ok!(DelegatedStaking::delegate_to_agent(
RawOrigin::Signed(delegator).into(),
agent,
100
));
// verify
assert!(DelegatedStaking::is_agent(&agent));
assert_eq!(DelegatedStaking::stakeable_balance(&agent), 100);
assert_eq!(Balances::balance_on_hold(&HoldReason::Delegating.into(), &delegator), 100);
});
}
#[test]
fn cannot_become_agent() {
ExtBuilder::default().build_and_execute(|| {
// cannot set reward account same as agent account
assert_noop!(
DelegatedStaking::register_agent(RawOrigin::Signed(100).into(), 100),
Error::<T>::InvalidRewardDestination
);
// an existing validator cannot become agent
assert_noop!(
DelegatedStaking::register_agent(
RawOrigin::Signed(mock::GENESIS_VALIDATOR).into(),
100
),
Error::<T>::AlreadyStaking
);
// an existing nominator cannot become agent
assert_noop!(
DelegatedStaking::register_agent(
RawOrigin::Signed(mock::GENESIS_NOMINATOR_ONE).into(),
100
),
Error::<T>::AlreadyStaking
);
assert_noop!(
DelegatedStaking::register_agent(
RawOrigin::Signed(mock::GENESIS_NOMINATOR_TWO).into(),
100
),
Error::<T>::AlreadyStaking
);
});
}
#[test]
fn create_multiple_delegators() {
ExtBuilder::default().build_and_execute(|| {
let agent: AccountId = 200;
let reward_account: AccountId = 201;
// stakeable balance is 0 for non agent
fund(&agent, 1000);
assert!(!DelegatedStaking::is_agent(&agent));
assert_eq!(DelegatedStaking::stakeable_balance(&agent), 0);
// set intention to accept delegation.
assert_ok!(DelegatedStaking::register_agent(
RawOrigin::Signed(agent).into(),
reward_account
));
// create 100 delegators
for i in 202..302 {
fund(&i, 100 + ExistentialDeposit::get());
assert_ok!(DelegatedStaking::delegate_to_agent(
RawOrigin::Signed(i).into(),
agent,
100
));
// Balance of 100 held on delegator account for delegating to the agent.
assert_eq!(Balances::balance_on_hold(&HoldReason::Delegating.into(), &i), 100);
}
// verify
assert!(DelegatedStaking::is_agent(&agent));
assert_eq!(DelegatedStaking::stakeable_balance(&agent), 100 * 100);
});
}
#[test]
fn agent_restrictions() {
// Similar to creating a nomination pool
ExtBuilder::default().build_and_execute(|| {
let agent_one = 200;
let delegator_one = 210;
fund(&agent_one, 100);
assert_ok!(DelegatedStaking::register_agent(
RawOrigin::Signed(agent_one).into(),
agent_one + 1
));
fund(&delegator_one, 200);
assert_ok!(DelegatedStaking::delegate_to_agent(
RawOrigin::Signed(delegator_one).into(),
agent_one,
100
));
let agent_two = 300;
let delegator_two = 310;
fund(&agent_two, 100);
assert_ok!(DelegatedStaking::register_agent(
RawOrigin::Signed(agent_two).into(),
agent_two + 1
));
fund(&delegator_two, 200);
assert_ok!(DelegatedStaking::delegate_to_agent(
RawOrigin::Signed(delegator_two).into(),
agent_two,
100
));
// agent one tries to delegate to agent 2
assert_noop!(
DelegatedStaking::delegate_to_agent(RawOrigin::Signed(agent_one).into(), agent_two, 10),
Error::<T>::InvalidDelegation
);
// agent one tries to delegate to a delegator
assert_noop!(
DelegatedStaking::delegate_to_agent(
RawOrigin::Signed(agent_one).into(),
delegator_one,
10
),
Error::<T>::InvalidDelegation
);
assert_noop!(
DelegatedStaking::delegate_to_agent(
RawOrigin::Signed(agent_one).into(),
delegator_two,
10
),
Error::<T>::InvalidDelegation
);
// delegator one tries to delegate to agent 2 as well (it already delegates to agent
// 1)
assert_noop!(
DelegatedStaking::delegate_to_agent(
RawOrigin::Signed(delegator_one).into(),
agent_two,
10
),
Error::<T>::InvalidDelegation
);
});
}
use sp_staking::DelegationInterface;
#[test]
fn apply_pending_slash() {
ExtBuilder::default().build_and_execute(|| {
start_era(1);
let agent: AccountId = 200;
let reward_acc: AccountId = 201;
let delegators: Vec<AccountId> = (301..=350).collect();
let reporter: AccountId = 400;
let total_staked = setup_delegation_stake(agent, reward_acc, delegators.clone(), 10, 10);
start_era(4);
// slash half of the stake
pallet_staking::slashing::do_slash::<T>(
&agent,
total_staked / 2,
&mut Default::default(),
&mut Default::default(),
3,
);
// agent cannot slash an account that is not its delegator.
setup_delegation_stake(210, 211, (351..=352).collect(), 100, 0);
assert_noop!(
<DelegatedStaking as DelegationInterface>::delegator_slash(&agent, &351, 1, Some(400)),
Error::<T>::NotAgent
);
// or a non delegator account
fund(&353, 100);
assert_noop!(
<DelegatedStaking as DelegationInterface>::delegator_slash(&agent, &353, 1, Some(400)),
Error::<T>::NotDelegator
);
// ensure bookkept pending slash is correct.
assert_eq!(get_agent(&agent).ledger.pending_slash, total_staked / 2);
let mut old_reporter_balance = Balances::free_balance(reporter);
// lets apply the pending slash on delegators.
for i in delegators {
// balance before slash
let initial_pending_slash = get_agent(&agent).ledger.pending_slash;
assert!(initial_pending_slash > 0);
let unslashed_balance = held_balance(&i);
let slash = unslashed_balance / 2;
// slash half of delegator's delegation.
assert_ok!(<DelegatedStaking as DelegationInterface>::delegator_slash(
&agent,
&i,
slash,
Some(400)
));
// balance after slash.
assert_eq!(held_balance(&i), unslashed_balance - slash);
// pending slash is reduced by the amount slashed.
assert_eq!(get_agent(&agent).ledger.pending_slash, initial_pending_slash - slash);
// reporter get 10% of the slash amount.
assert_eq!(
Balances::free_balance(reporter) - old_reporter_balance,
<Staking as StakingInterface>::slash_reward_fraction() * slash,
);
// update old balance
old_reporter_balance = Balances::free_balance(reporter);
}
// nothing to slash anymore
assert_eq!(get_agent(&agent).ledger.pending_slash, 0);
// cannot slash anymore
assert_noop!(
<DelegatedStaking as DelegationInterface>::delegator_slash(&agent, &350, 1, None),
Error::<T>::NothingToSlash
);
});
}
/// Integration tests with pallet-staking.
mod staking_integration {
use super::*;
use pallet_staking::RewardDestination;
use sp_staking::Stake;
#[test]
fn bond() {
ExtBuilder::default().build_and_execute(|| {
let agent: AccountId = 99;
let reward_acc: AccountId = 100;
assert_eq!(Staking::status(&agent), Err(StakingError::<T>::NotStash.into()));
// set intention to become an agent
fund(&agent, 100);
assert_ok!(DelegatedStaking::register_agent(
RawOrigin::Signed(agent).into(),
reward_acc
));
assert_eq!(DelegatedStaking::stakeable_balance(&agent), 0);
let mut delegated_balance: Balance = 0;
// set some delegations
for delegator in 200..250 {
fund(&delegator, 200);
assert_ok!(DelegatedStaking::delegate_to_agent(
RawOrigin::Signed(delegator).into(),
agent,
100
));
delegated_balance += 100;
assert_eq!(
Balances::balance_on_hold(&HoldReason::Delegating.into(), &delegator),
100
);
let agent_obj = get_agent(&agent);
assert_eq!(agent_obj.ledger.stakeable_balance(), delegated_balance);
assert_eq!(agent_obj.available_to_bond(), 0);
assert_eq!(agent_obj.bonded_stake(), delegated_balance);
}
assert_eq!(Staking::stake(&agent).unwrap(), Stake { total: 50 * 100, active: 50 * 100 })
});
}
#[test]
fn withdraw_test() {
ExtBuilder::default().build_and_execute(|| {
// initial era
start_era(1);
let agent: AccountId = 200;
let reward_acc: AccountId = 201;
let delegators: Vec<AccountId> = (301..=350).collect();
let total_staked =
setup_delegation_stake(agent, reward_acc, delegators.clone(), 10, 10);
// lets go to a new era
start_era(2);
assert!(eq_stake(agent, total_staked, total_staked));
// Withdrawing without unbonding would fail.
assert_noop!(
DelegatedStaking::release_delegation(RawOrigin::Signed(agent).into(), 301, 50, 0),
Error::<T>::NotEnoughFunds
);
// assert_noop!(DelegatedStaking::release_delegation(RawOrigin::Signed(agent).into(),
// 200, 50, 0), Error::<T>::NotAllowed); active and total stake remains same
assert!(eq_stake(agent, total_staked, total_staked));
// 305 wants to unbond 50 in era 2, withdrawable in era 5.
assert_ok!(Staking::unbond(RawOrigin::Signed(agent).into(), 50));
// 310 wants to unbond 100 in era 3, withdrawable in era 6.
start_era(3);
assert_ok!(Staking::unbond(RawOrigin::Signed(agent).into(), 100));
// 320 wants to unbond 200 in era 4, withdrawable in era 7.
start_era(4);
assert_ok!(Staking::unbond(RawOrigin::Signed(agent).into(), 200));
// active stake is now reduced..
let expected_active = total_staked - (50 + 100 + 200);
assert!(eq_stake(agent, total_staked, expected_active));
// nothing to withdraw at era 4
assert_noop!(
DelegatedStaking::release_delegation(RawOrigin::Signed(agent).into(), 305, 50, 0),
Error::<T>::NotEnoughFunds
);
assert!(eq_stake(agent, total_staked, expected_active));
assert_eq!(get_agent(&agent).available_to_bond(), 0);
// full amount is still delegated
assert_eq!(get_agent(&agent).ledger.effective_balance(), total_staked);
start_era(5);
// at era 5, 50 tokens are withdrawable, cannot withdraw more.
assert_noop!(
DelegatedStaking::release_delegation(RawOrigin::Signed(agent).into(), 305, 51, 0),
Error::<T>::NotEnoughFunds
);
// less is possible
assert_ok!(DelegatedStaking::release_delegation(
RawOrigin::Signed(agent).into(),
305,
30,
0
));
assert_ok!(DelegatedStaking::release_delegation(
RawOrigin::Signed(agent).into(),
305,
20,
0
));
// Lets go to future era where everything is unbonded. Withdrawable amount: 100 + 200
start_era(7);
// 305 has no more amount delegated so it cannot withdraw.
assert_noop!(
DelegatedStaking::release_delegation(RawOrigin::Signed(agent).into(), 305, 5, 0),
Error::<T>::NotDelegator
);
// 309 is an active delegator but has total delegation of 90, so it cannot withdraw more
// than that.
assert_noop!(
DelegatedStaking::release_delegation(RawOrigin::Signed(agent).into(), 309, 91, 0),
Error::<T>::NotEnoughFunds
);
// 310 cannot withdraw more than delegated funds.
assert_noop!(
DelegatedStaking::release_delegation(RawOrigin::Signed(agent).into(), 310, 101, 0),
Error::<T>::NotEnoughFunds
);
// but can withdraw all its delegation amount.
assert_ok!(DelegatedStaking::release_delegation(
RawOrigin::Signed(agent).into(),
310,
100,
0
));
// 320 can withdraw all its delegation amount.
assert_ok!(DelegatedStaking::release_delegation(
RawOrigin::Signed(agent).into(),
320,
200,
0
));
// cannot withdraw anything more..
assert_noop!(
DelegatedStaking::release_delegation(RawOrigin::Signed(agent).into(), 301, 1, 0),
Error::<T>::NotEnoughFunds
);
assert_noop!(
DelegatedStaking::release_delegation(RawOrigin::Signed(agent).into(), 350, 1, 0),
Error::<T>::NotEnoughFunds
);
});
}
#[test]
fn withdraw_happens_with_unbonded_balance_first() {
ExtBuilder::default().build_and_execute(|| {
let agent = 200;
setup_delegation_stake(agent, 201, (300..350).collect(), 100, 0);
// verify withdraw not possible yet
assert_noop!(
DelegatedStaking::release_delegation(RawOrigin::Signed(agent).into(), 300, 100, 0),
Error::<T>::NotEnoughFunds
);
// add new delegation that is not staked
// FIXME(ank4n): add scenario where staked funds are withdrawn from ledger but not
// withdrawn and test its claimed from there first.
// fund(&300, 1000);
// assert_ok!(DelegatedStaking::delegate_to_agent(RawOrigin::Signed(300.into()),
// delegate, 100));
//
// // verify unbonded balance
// assert_eq!(get_agent(&agent).available_to_bond(), 100);
//
// // withdraw works now without unbonding
// assert_ok!(DelegatedStaking::release_delegation(RawOrigin::Signed(agent).into(), 300,
// 100, 0)); assert_eq!(get_agent(&agent).available_to_bond(), 0);
});
}
#[test]
fn reward_destination_restrictions() {
ExtBuilder::default().build_and_execute(|| {
// give some funds to 200
fund(&200, 1000);
let balance_200 = Balances::free_balance(200);
// `Agent` account cannot be reward destination
assert_noop!(
DelegatedStaking::register_agent(RawOrigin::Signed(200).into(), 200),
Error::<T>::InvalidRewardDestination
);
// different reward account works
assert_ok!(DelegatedStaking::register_agent(RawOrigin::Signed(200).into(), 201));
// add some delegations to it
fund(&300, 1000);
assert_ok!(DelegatedStaking::delegate_to_agent(
RawOrigin::Signed(300).into(),
200,
100
));
// update_payee to self fails.
assert_noop!(
<Staking as StakingInterface>::update_payee(&200, &200),
StakingError::<T>::RewardDestinationRestricted
);
// passing correct reward destination works
assert_ok!(<Staking as StakingInterface>::update_payee(&200, &201));
// amount is staked correctly
assert!(eq_stake(200, 100, 100));
assert_eq!(get_agent(&200).available_to_bond(), 0);
assert_eq!(get_agent(&200).ledger.effective_balance(), 100);
// free balance of delegate is untouched
assert_eq!(Balances::free_balance(200), balance_200);
});
}
#[test]
fn agent_restrictions() {
ExtBuilder::default().build_and_execute(|| {
setup_delegation_stake(200, 201, (202..203).collect(), 100, 0);
// Registering again is noop
assert_noop!(
DelegatedStaking::register_agent(RawOrigin::Signed(200).into(), 201),
Error::<T>::NotAllowed
);
// a delegator cannot become delegate
assert_noop!(
DelegatedStaking::register_agent(RawOrigin::Signed(202).into(), 203),
Error::<T>::NotAllowed
);
// existing staker cannot become a delegate
assert_noop!(
DelegatedStaking::register_agent(
RawOrigin::Signed(GENESIS_NOMINATOR_ONE).into(),
201
),
Error::<T>::AlreadyStaking
);
assert_noop!(
DelegatedStaking::register_agent(RawOrigin::Signed(GENESIS_VALIDATOR).into(), 201),
Error::<T>::AlreadyStaking
);
});
}
#[test]
fn slash_works() {
ExtBuilder::default().build_and_execute(|| {
setup_delegation_stake(200, 201, (210..250).collect(), 100, 0);
start_era(1);
// fixme(ank4n): add tests for slashing
});
}
#[test]
fn migration_works() {
ExtBuilder::default().build_and_execute(|| {
// add a nominator
fund(&200, 5000);
let staked_amount = 4000;
assert_ok!(Staking::bond(
RuntimeOrigin::signed(200),
staked_amount,
RewardDestination::Account(201)
));
assert_ok!(Staking::nominate(RuntimeOrigin::signed(200), vec![GENESIS_VALIDATOR],));
let init_stake = Staking::stake(&200).unwrap();
// scenario: 200 is a pool account, and the stake comes from its 4 delegators (300..304)
// in equal parts. lets try to migrate this nominator into delegate based stake.
// all balance currently is in 200
assert_eq!(Balances::free_balance(200), 5000);
// to migrate, nominator needs to set an account as a proxy delegator where staked funds
// will be moved and delegated back to this old nominator account. This should be funded
// with at least ED.
let proxy_delegator = DelegatedStaking::sub_account(AccountType::ProxyDelegator, 200);
assert_ok!(DelegatedStaking::migrate_to_agent(RawOrigin::Signed(200).into(), 201));
// verify all went well
let mut expected_proxy_delegated_amount = staked_amount;
assert_eq!(
Balances::balance_on_hold(&HoldReason::Delegating.into(), &proxy_delegator),
expected_proxy_delegated_amount
);
// ED + stake amount is transferred from delegate to proxy delegator account.
assert_eq!(
Balances::free_balance(200),
5000 - staked_amount - ExistentialDeposit::get()
);
assert_eq!(Staking::stake(&200).unwrap(), init_stake);
assert_eq!(get_agent(&200).ledger.effective_balance(), 4000);
assert_eq!(get_agent(&200).available_to_bond(), 0);
// now lets migrate the delegators
let delegator_share = staked_amount / 4;
for delegator in 300..304 {
assert_eq!(Balances::free_balance(delegator), 0);
// fund them with ED
fund(&delegator, ExistentialDeposit::get());
// migrate 1/4th amount into each delegator
assert_ok!(DelegatedStaking::claim_delegation(
RawOrigin::Signed(200).into(),
delegator,
delegator_share
));
assert_eq!(
Balances::balance_on_hold(&HoldReason::Delegating.into(), &delegator),
delegator_share
);
expected_proxy_delegated_amount -= delegator_share;
assert_eq!(
Balances::balance_on_hold(&HoldReason::Delegating.into(), &proxy_delegator),
expected_proxy_delegated_amount
);
// delegate stake is unchanged.
assert_eq!(Staking::stake(&200).unwrap(), init_stake);
assert_eq!(get_agent(&200).ledger.effective_balance(), 4000);
assert_eq!(get_agent(&200).available_to_bond(), 0);
}
// cannot use migrate delegator anymore
assert_noop!(
DelegatedStaking::claim_delegation(RawOrigin::Signed(200).into(), 305, 1),
Error::<T>::NotEnoughFunds
);
});
}
}