-
Notifications
You must be signed in to change notification settings - Fork 77
/
tests.rs
309 lines (260 loc) · 14.3 KB
/
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
/// Tests for Staking
use crate::mock::*;
use frame_support::{assert_err, assert_ok};
// type Event = crate::Event<Test>;
#[macro_export]
macro_rules! fixed {
($amount:expr) => {
sp_arithmetic::FixedI128::from($amount)
};
}
#[test]
#[cfg_attr(rustfmt, rustfmt_skip)]
fn reproduce_broken_state() {
run_test(|| {
use crate::pallet::*;
let account = VAULT.account_id;
let currency = Token(INTR);
let wrong_currency = Token(KBTC);
let f = |x: i128| {
SignedFixedPoint::from_inner(x)
};
// state at block 0x5aaf4dc2ca1a1043e2c37ba85a1d1487a0e4cc79d3beb30e6e646d2190223851
RewardPerToken::<Test>::insert(currency, (0, VAULT), f(8328262397106661114));
RewardTally::<Test>::insert(currency, (0, VAULT, account), f(594980318984627591665452302579139));
SlashPerToken::<Test>::insert(0, VAULT, f(10288025703175927));
SlashTally::<Test>::insert(0, (VAULT, account), f(734987987016863199590580128394));
Stake::<Test>::insert(0, (VAULT, account), f(71441111076342999999817587297983));
TotalCurrentStake::<Test>::insert(0, VAULT, f(71441111076343000000000000000000));
TotalRewards::<Test>::insert(currency, (0, VAULT), f(1135651379916000000000000000000));
TotalStake::<Test>::insert(0, VAULT, f(71441111076342999999817587297983));
assert_ok!(Staking::broken_slash_stake_do_not_use(wrong_currency, &VAULT, fixed!(109_808_965_219)));
assert_ok!(Staking::broken_slash_stake_do_not_use(wrong_currency, &VAULT, fixed!(109_808_965_219)));
assert_ok!(Staking::broken_slash_stake_do_not_use(wrong_currency, &VAULT, fixed!(1_479_196_975_788)));
// state at block 0xe56fd1e1c66ca658284cda6334865cb3ac413fdb6a9272f00f350b6f29787ba5
// Note: RewardPerToken is unchanged, incorrect!
assert_eq!(RewardPerToken::<Test>::get(currency, (0, VAULT)), f(8328262397106661114));
assert_eq!(RewardTally::<Test>::get(currency, (0, VAULT, account)), f(594980318984627591665452302579139));
assert_eq!(SlashPerToken::<Test>::get(0, VAULT,), f(34067259825257565));
assert_eq!(SlashTally::<Test>::get(0, (VAULT, account)), f(734987987016863199590580128394));
assert_eq!(Stake::<Test>::get(0, (VAULT, account)), f(71441111076342999999817587297983));
assert_eq!(TotalCurrentStake::<Test>::get(0, VAULT), f(69742296170117000000000000000000));
assert_eq!(TotalRewards::<Test>::get(currency, (0, VAULT)), f(1135651379916000000000000000000));
assert_eq!(TotalStake::<Test>::get(0, VAULT), f(71441111076342999999817587297983));
// The bug that we observed
Staking::distribute_reward(currency, &VAULT, f(14234191584160000000000000000000)).unwrap();
assert_eq!(Staking::withdraw_reward(currency, &VAULT, &account).unwrap(), 86015280993);
})
}
#[test]
#[cfg_attr(rustfmt, rustfmt_skip)]
fn slash_stake_does_not_break_state() {
run_test(|| {
use crate::pallet::*;
let account = VAULT.account_id;
let currency = Token(INTR);
let f = |x: i128| {
SignedFixedPoint::from_inner(x)
};
// state at block 0x5aaf4dc2ca1a1043e2c37ba85a1d1487a0e4cc79d3beb30e6e646d2190223851
RewardPerToken::<Test>::insert(currency, (0, VAULT), f(8328262397106661114));
RewardTally::<Test>::insert(currency, (0, VAULT, account), f(594980318984627591665452302579139));
SlashPerToken::<Test>::insert(0, VAULT, f(10288025703175927));
SlashTally::<Test>::insert(0, (VAULT, account), f(734987987016863199590580128394));
Stake::<Test>::insert(0, (VAULT, account), f(71441111076342999999817587297983));
TotalCurrentStake::<Test>::insert(0, VAULT, f(71441111076343000000000000000000));
TotalRewards::<Test>::insert(currency, (0, VAULT), f(1135651379916000000000000000000));
TotalStake::<Test>::insert(0, VAULT, f(71441111076342999999817587297983));
assert_ok!(Staking::slash_stake(&VAULT, fixed!(109_808_965_219)));
assert_ok!(Staking::slash_stake(&VAULT, fixed!(109_808_965_219)));
assert_ok!(Staking::slash_stake(&VAULT, fixed!(1_479_196_975_788)));
// state at block 0xe56fd1e1c66ca658284cda6334865cb3ac413fdb6a9272f00f350b6f29787ba5
// NOTE: RewardPerToken is updated correctly
// updated value in v0.9.26 since `CheckedDiv` lost precision with rounding (was 8531126040549884148)
assert_eq!(RewardPerToken::<Test>::get(currency, (0, VAULT)), f(8531126040549884146));
assert_eq!(RewardTally::<Test>::get(currency, (0, VAULT, account)), f(594980318984627591665452302579139));
assert_eq!(SlashPerToken::<Test>::get(0, VAULT,), f(34067259825257565));
assert_eq!(SlashTally::<Test>::get(0, (VAULT, account)), f(734987987016863199590580128394));
assert_eq!(Stake::<Test>::get(0, (VAULT, account)), f(71441111076342999999817587297983));
assert_eq!(TotalCurrentStake::<Test>::get(0, VAULT), f(69742296170117000000000000000000));
assert_eq!(TotalRewards::<Test>::get(currency, (0, VAULT)), f(1135651379916000000000000000000));
assert_eq!(TotalStake::<Test>::get(0, VAULT), f(71441111076342999999817587297983));
// The bug that we observed
Staking::distribute_reward(currency, &VAULT, f(14234191584160000000000000000000)).unwrap();
// same as distributed amount except for rounding error
assert_eq!(Staking::withdraw_reward(currency, &VAULT, &account).unwrap(), 14234191584159);
})
}
#[test]
fn should_stake_and_earn_rewards() {
run_test(|| {
assert_ok!(Staking::deposit_stake(&VAULT, &ALICE.account_id, fixed!(50)));
assert_ok!(Staking::deposit_stake(&VAULT, &BOB.account_id, fixed!(50)));
assert_ok!(Staking::distribute_reward(Token(IBTC), &VAULT, fixed!(100)));
assert_ok!(Staking::compute_reward(Token(IBTC), &VAULT, &ALICE.account_id), 50);
assert_ok!(Staking::compute_reward(Token(IBTC), &VAULT, &BOB.account_id), 50);
assert_ok!(Staking::slash_stake(&VAULT, fixed!(20)));
assert_ok!(Staking::compute_stake(&VAULT, &ALICE.account_id), 40);
assert_ok!(Staking::compute_stake(&VAULT, &BOB.account_id), 40);
assert_ok!(Staking::compute_reward(Token(IBTC), &VAULT, &ALICE.account_id), 50);
assert_ok!(Staking::compute_reward(Token(IBTC), &VAULT, &BOB.account_id), 50);
})
}
#[test]
fn continues_functioning_after_slash() {
run_test(|| {
// without the `apply_slash` in withdraw_rewards, the following sequence fails in the last step:
// [distribute_reward, slash_stake, withdraw_reward, distribute_reward, withdraw_reward]
// step 1: initial (normal) flow
assert_ok!(Staking::deposit_stake(&VAULT, &ALICE.account_id, fixed!(50)));
assert_ok!(Staking::distribute_reward(Token(IBTC), &VAULT, fixed!(10000)));
assert_ok!(Staking::compute_reward(Token(IBTC), &VAULT, &ALICE.account_id), 10000);
// step 2: slash
assert_ok!(Staking::slash_stake(&VAULT, fixed!(30)));
assert_ok!(Staking::compute_stake(&VAULT, &ALICE.account_id), 20);
// step 3: withdraw rewards
assert_ok!(Staking::compute_reward(Token(IBTC), &VAULT, &ALICE.account_id), 10000);
assert_ok!(Staking::withdraw_reward(Token(IBTC), &VAULT, &ALICE.account_id), 10000);
// Now distribute more rewards - behavior should be back to normal.
// The slash should not have any effect on this!
assert_ok!(Staking::distribute_reward(Token(IBTC), &VAULT, fixed!(10000)));
assert_ok!(Staking::compute_reward(Token(IBTC), &VAULT, &ALICE.account_id), 10000);
assert_ok!(Staking::withdraw_reward(Token(IBTC), &VAULT, &ALICE.account_id), 10000);
assert_ok!(Staking::compute_reward(Token(IBTC), &VAULT, &ALICE.account_id), 0);
})
}
#[test]
fn should_stake_and_distribute_and_withdraw() {
run_test(|| {
assert_ok!(Staking::deposit_stake(&VAULT, &ALICE.account_id, fixed!(10000)));
assert_ok!(Staking::deposit_stake(&VAULT, &BOB.account_id, fixed!(10000)));
assert_ok!(Staking::distribute_reward(Token(IBTC), &VAULT, fixed!(1000)));
assert_ok!(Staking::compute_reward(Token(IBTC), &VAULT, &ALICE.account_id), 500);
assert_ok!(Staking::compute_reward(Token(IBTC), &VAULT, &BOB.account_id), 500);
assert_ok!(Staking::slash_stake(&VAULT, fixed!(50)));
assert_ok!(Staking::slash_stake(&VAULT, fixed!(50)));
assert_ok!(Staking::deposit_stake(&VAULT, &ALICE.account_id, fixed!(1000)));
assert_ok!(Staking::distribute_reward(Token(IBTC), &VAULT, fixed!(1000)));
assert_ok!(Staking::compute_reward(Token(IBTC), &VAULT, &ALICE.account_id), 1023);
assert_ok!(Staking::compute_reward(Token(IBTC), &VAULT, &BOB.account_id), 976);
assert_ok!(Staking::withdraw_stake(&VAULT, &ALICE.account_id, fixed!(10000), None));
assert_ok!(Staking::compute_stake(&VAULT, &ALICE.account_id), 950);
assert_ok!(Staking::withdraw_stake(&VAULT, &ALICE.account_id, fixed!(950), None));
assert_ok!(Staking::compute_stake(&VAULT, &ALICE.account_id), 0);
assert_ok!(Staking::deposit_stake(&VAULT, &BOB.account_id, fixed!(10000)));
assert_ok!(Staking::compute_stake(&VAULT, &BOB.account_id), 19950);
assert_ok!(Staking::distribute_reward(Token(IBTC), &VAULT, fixed!(1000)));
assert_ok!(Staking::slash_stake(&VAULT, fixed!(10000)));
assert_ok!(Staking::compute_stake(&VAULT, &BOB.account_id), 9950);
assert_ok!(Staking::compute_reward(Token(IBTC), &VAULT, &ALICE.account_id), 1023);
assert_ok!(Staking::compute_reward(Token(IBTC), &VAULT, &BOB.account_id), 1976);
})
}
#[test]
fn should_stake_and_withdraw_rewards() {
run_test(|| {
assert_ok!(Staking::deposit_stake(&VAULT, &ALICE.account_id, fixed!(100)));
assert_ok!(Staking::distribute_reward(Token(IBTC), &VAULT, fixed!(100)));
assert_ok!(Staking::compute_reward(Token(IBTC), &VAULT, &ALICE.account_id), 100);
assert_ok!(Staking::withdraw_reward(Token(IBTC), &VAULT, &ALICE.account_id), 100);
assert_ok!(Staking::compute_reward(Token(IBTC), &VAULT, &ALICE.account_id), 0);
})
}
#[test]
fn should_not_withdraw_stake_if_balance_insufficient() {
run_test(|| {
assert_ok!(Staking::deposit_stake(&VAULT, &ALICE.account_id, fixed!(100)));
assert_ok!(Staking::compute_stake(&VAULT, &ALICE.account_id), 100);
assert_err!(
Staking::withdraw_stake(&VAULT, &ALICE.account_id, fixed!(200), None),
TestError::InsufficientFunds
);
})
}
#[test]
fn should_not_withdraw_stake_if_balance_insufficient_after_slashing() {
run_test(|| {
assert_ok!(Staking::deposit_stake(&VAULT, &ALICE.account_id, fixed!(100)));
assert_ok!(Staking::compute_stake(&VAULT, &ALICE.account_id), 100);
assert_ok!(Staking::slash_stake(&VAULT, fixed!(100)));
assert_ok!(Staking::compute_stake(&VAULT, &ALICE.account_id), 0);
assert_err!(
Staking::withdraw_stake(&VAULT, &ALICE.account_id, fixed!(100), None),
TestError::InsufficientFunds
);
})
}
#[test]
fn should_force_refund() {
run_test(|| {
let mut nonce = Staking::nonce(&VAULT);
assert_ok!(Staking::deposit_stake(&VAULT, &VAULT.account_id, fixed!(100)));
assert_ok!(Staking::deposit_stake(&VAULT, &ALICE.account_id, fixed!(100)));
assert_ok!(Staking::slash_stake(&VAULT, fixed!(100)));
assert_ok!(Staking::deposit_stake(&VAULT, &VAULT.account_id, fixed!(100)));
assert_ok!(Staking::deposit_stake(&VAULT, &ALICE.account_id, fixed!(10)));
assert_ok!(Staking::distribute_reward(Token(IBTC), &VAULT, fixed!(100)));
// vault stake & rewards pre-refund
assert_ok!(Staking::compute_stake_at_index(nonce, &VAULT, &VAULT.account_id), 150);
assert_ok!(
Staking::compute_reward_at_index(nonce, Token(IBTC), &VAULT, &VAULT.account_id),
71
);
// alice stake & rewards pre-refund
assert_ok!(Staking::compute_stake_at_index(nonce, &VAULT, &ALICE.account_id), 60);
assert_ok!(
Staking::compute_reward_at_index(nonce, Token(IBTC), &VAULT, &ALICE.account_id),
28
);
assert_ok!(Staking::force_refund(&VAULT));
nonce = Staking::nonce(&VAULT);
// vault stake & rewards post-refund
assert_ok!(Staking::compute_stake_at_index(nonce, &VAULT, &VAULT.account_id), 150);
assert_ok!(
Staking::compute_reward_at_index(nonce, Token(IBTC), &VAULT, &VAULT.account_id),
0
);
assert_ok!(
Staking::compute_reward_at_index(nonce - 1, Token(IBTC), &VAULT, &VAULT.account_id),
71
);
// alice stake & rewards post-refund
assert_ok!(Staking::compute_stake_at_index(nonce, &VAULT, &ALICE.account_id), 0);
assert_ok!(
Staking::compute_reward_at_index(nonce, Token(IBTC), &VAULT, &ALICE.account_id),
0
);
assert_ok!(
Staking::compute_stake_at_index(nonce - 1, &VAULT, &ALICE.account_id),
60
);
assert_ok!(
Staking::compute_reward_at_index(nonce - 1, Token(IBTC), &VAULT, &ALICE.account_id),
28
);
})
}
#[test]
fn should_compute_stake_after_adjustments() {
// this replicates a failing integration test due to repeated
// deposits and slashing which led to incorrect stake
run_test(|| {
assert_ok!(Staking::deposit_stake(&VAULT, &VAULT.account_id, fixed!(100)));
assert_ok!(Staking::deposit_stake(
&VAULT,
&VAULT.account_id,
fixed!(1152923504604516976)
));
assert_ok!(Staking::slash_stake(&VAULT, fixed!(1152923504604516976 + 100)));
assert_ok!(Staking::deposit_stake(&VAULT, &VAULT.account_id, fixed!(1_000_000)));
assert_ok!(Staking::deposit_stake(
&VAULT,
&VAULT.account_id,
fixed!(1152924504603286976)
));
assert_ok!(Staking::slash_stake(&VAULT, fixed!(1152924504603286976 + 1_000_000)));
assert_ok!(Staking::compute_stake(&VAULT, &VAULT.account_id), 0);
assert_ok!(Staking::deposit_stake(&VAULT, &VAULT.account_id, fixed!(1_000_000)));
assert_ok!(Staking::compute_stake(&VAULT, &VAULT.account_id), 1_000_000);
})
}