This repository has been archived by the owner on Jan 22, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
Copy pathstake.rs
574 lines (508 loc) · 17.2 KB
/
stake.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
#![allow(clippy::integer_arithmetic)]
use solana_runtime::{
bank::Bank,
bank_client::BankClient,
genesis_utils::{create_genesis_config_with_leader, GenesisConfigInfo},
};
use solana_sdk::{
account::from_account,
account_utils::StateMut,
client::SyncClient,
message::Message,
pubkey::Pubkey,
signature::{Keypair, Signer},
system_instruction::SystemError,
sysvar::{self, stake_history::StakeHistory},
transaction::TransactionError,
};
use solana_stake_program::{
stake_instruction::{self},
stake_state::{self, StakeState},
};
use solana_vote_program::{
vote_instruction,
vote_state::{Vote, VoteInit, VoteState, VoteStateVersions},
};
use std::sync::Arc;
fn next_epoch(bank: &Arc<Bank>) -> Arc<Bank> {
bank.squash();
Arc::new(Bank::new_from_parent(
&bank,
&Pubkey::default(),
bank.get_slots_in_epoch(bank.epoch()) + bank.slot(),
))
}
fn fill_epoch_with_votes(
bank: &Arc<Bank>,
vote_keypair: &Keypair,
mint_keypair: &Keypair,
) -> Arc<Bank> {
let mint_pubkey = mint_keypair.pubkey();
let vote_pubkey = vote_keypair.pubkey();
let old_epoch = bank.epoch();
let mut bank = bank.clone();
while bank.epoch() != old_epoch + 1 {
bank.squash();
bank = Arc::new(Bank::new_from_parent(
&bank,
&Pubkey::default(),
1 + bank.slot(),
));
let bank_client = BankClient::new_shared(&bank);
let parent = bank.parent().unwrap();
let message = Message::new(
&[vote_instruction::vote(
&vote_pubkey,
&vote_pubkey,
Vote::new(vec![parent.slot() as u64], parent.hash()),
)],
Some(&mint_pubkey),
);
assert!(bank_client
.send_and_confirm_message(&[mint_keypair, vote_keypair], message)
.is_ok());
}
bank
}
fn warmed_up(bank: &Bank, stake_pubkey: &Pubkey) -> bool {
let stake = StakeState::stake_from(&bank.get_account(stake_pubkey).unwrap()).unwrap();
stake.delegation.stake
== stake.stake(
bank.epoch(),
Some(
&from_account::<StakeHistory, _>(
&bank.get_account(&sysvar::stake_history::id()).unwrap(),
)
.unwrap(),
),
true,
)
}
fn get_staked(bank: &Bank, stake_pubkey: &Pubkey) -> u64 {
StakeState::stake_from(&bank.get_account(stake_pubkey).unwrap())
.unwrap()
.stake(
bank.epoch(),
Some(
&from_account::<StakeHistory, _>(
&bank.get_account(&sysvar::stake_history::id()).unwrap(),
)
.unwrap(),
),
true,
)
}
#[test]
fn test_stake_create_and_split_single_signature() {
solana_logger::setup();
let GenesisConfigInfo {
genesis_config,
mint_keypair: staker_keypair,
..
} = create_genesis_config_with_leader(
100_000_000_000,
&solana_sdk::pubkey::new_rand(),
1_000_000,
);
let staker_pubkey = staker_keypair.pubkey();
let bank_client = BankClient::new_shared(&Arc::new(Bank::new(&genesis_config)));
let stake_address =
Pubkey::create_with_seed(&staker_pubkey, "stake", &solana_stake_program::id()).unwrap();
let authorized = stake_state::Authorized::auto(&staker_pubkey);
let lamports = 1_000_000;
// Create stake account with seed
let message = Message::new(
&stake_instruction::create_account_with_seed(
&staker_pubkey, // from
&stake_address, // to
&staker_pubkey, // base
"stake", // seed
&authorized,
&stake_state::Lockup::default(),
lamports,
),
Some(&staker_pubkey),
);
// only one signature required
bank_client
.send_and_confirm_message(&[&staker_keypair], message)
.expect("failed to create and delegate stake account");
// split the stake
let split_stake_address =
Pubkey::create_with_seed(&staker_pubkey, "split_stake", &solana_stake_program::id())
.unwrap();
// Test split
let message = Message::new(
&stake_instruction::split_with_seed(
&stake_address, // original
&staker_pubkey, // authorized
lamports / 2,
&split_stake_address, // new address
&staker_pubkey, // base
"split_stake", // seed
),
Some(&staker_keypair.pubkey()),
);
assert!(bank_client
.send_and_confirm_message(&[&staker_keypair], message)
.is_ok());
// w00t!
}
#[test]
fn test_stake_create_and_split_to_existing_system_account() {
// Ensure stake-split does not allow the user to promote an existing system account into
// a stake account.
solana_logger::setup();
let GenesisConfigInfo {
genesis_config,
mint_keypair: staker_keypair,
..
} = create_genesis_config_with_leader(
100_000_000_000,
&solana_sdk::pubkey::new_rand(),
1_000_000,
);
let staker_pubkey = staker_keypair.pubkey();
let bank_client = BankClient::new_shared(&Arc::new(Bank::new(&genesis_config)));
let stake_address =
Pubkey::create_with_seed(&staker_pubkey, "stake", &solana_stake_program::id()).unwrap();
let authorized = stake_state::Authorized::auto(&staker_pubkey);
let lamports = 1_000_000;
// Create stake account with seed
let message = Message::new(
&stake_instruction::create_account_with_seed(
&staker_pubkey, // from
&stake_address, // to
&staker_pubkey, // base
"stake", // seed
&authorized,
&stake_state::Lockup::default(),
lamports,
),
Some(&staker_pubkey),
);
bank_client
.send_and_confirm_message(&[&staker_keypair], message)
.expect("failed to create and delegate stake account");
let split_stake_address =
Pubkey::create_with_seed(&staker_pubkey, "split_stake", &solana_stake_program::id())
.unwrap();
// First, put a system account where we want the new stake account
let existing_lamports = 42;
bank_client
.transfer_and_confirm(existing_lamports, &staker_keypair, &split_stake_address)
.unwrap();
assert_eq!(
bank_client.get_balance(&split_stake_address).unwrap(),
existing_lamports
);
// Verify the split fails because the account is already in use
let message = Message::new(
&stake_instruction::split_with_seed(
&stake_address, // original
&staker_pubkey, // authorized
lamports / 2,
&split_stake_address, // new address
&staker_pubkey, // base
"split_stake", // seed
),
Some(&staker_keypair.pubkey()),
);
assert_eq!(
bank_client
.send_and_confirm_message(&[&staker_keypair], message)
.unwrap_err()
.unwrap(),
TransactionError::InstructionError(0, SystemError::AccountAlreadyInUse.into())
);
assert_eq!(
bank_client.get_balance(&split_stake_address).unwrap(),
existing_lamports
);
}
#[test]
fn test_stake_account_lifetime() {
let stake_keypair = Keypair::new();
let stake_pubkey = stake_keypair.pubkey();
let vote_keypair = Keypair::new();
let vote_pubkey = vote_keypair.pubkey();
let identity_keypair = Keypair::new();
let identity_pubkey = identity_keypair.pubkey();
let GenesisConfigInfo {
genesis_config,
mint_keypair,
..
} = create_genesis_config_with_leader(
100_000_000_000,
&solana_sdk::pubkey::new_rand(),
1_000_000,
);
let bank = Bank::new(&genesis_config);
let mint_pubkey = mint_keypair.pubkey();
let mut bank = Arc::new(bank);
let bank_client = BankClient::new_shared(&bank);
// Create Vote Account
let message = Message::new(
&vote_instruction::create_account(
&mint_pubkey,
&vote_pubkey,
&VoteInit {
node_pubkey: identity_pubkey,
authorized_voter: vote_pubkey,
authorized_withdrawer: vote_pubkey,
commission: 50,
},
10,
),
Some(&mint_pubkey),
);
bank_client
.send_and_confirm_message(&[&mint_keypair, &vote_keypair, &identity_keypair], message)
.expect("failed to create vote account");
let authorized = stake_state::Authorized::auto(&stake_pubkey);
// Create stake account and delegate to vote account
let message = Message::new(
&stake_instruction::create_account_and_delegate_stake(
&mint_pubkey,
&stake_pubkey,
&vote_pubkey,
&authorized,
&stake_state::Lockup::default(),
1_000_000,
),
Some(&mint_pubkey),
);
bank_client
.send_and_confirm_message(&[&mint_keypair, &stake_keypair], message)
.expect("failed to create and delegate stake account");
// Test that correct lamports are staked
let account = bank.get_account(&stake_pubkey).expect("account not found");
let stake_state = account.state().expect("couldn't unpack account data");
if let StakeState::Stake(_meta, stake) = stake_state {
assert_eq!(stake.delegation.stake, 1_000_000);
} else {
panic!("wrong account type found")
}
// Test that we cannot withdraw anything until deactivation
let message = Message::new(
&[stake_instruction::withdraw(
&stake_pubkey,
&stake_pubkey,
&solana_sdk::pubkey::new_rand(),
1,
None,
)],
Some(&mint_pubkey),
);
assert!(bank_client
.send_and_confirm_message(&[&mint_keypair, &stake_keypair], message)
.is_err());
// Test that lamports are still staked
let account = bank.get_account(&stake_pubkey).expect("account not found");
let stake_state = account.state().expect("couldn't unpack account data");
if let StakeState::Stake(_meta, stake) = stake_state {
assert_eq!(stake.delegation.stake, 1_000_000);
} else {
panic!("wrong account type found")
}
loop {
if warmed_up(&bank, &stake_pubkey) {
break;
}
// Cycle thru banks until we're fully warmed up
bank = next_epoch(&bank);
}
// Reward redemption
// Submit enough votes to generate rewards
bank = fill_epoch_with_votes(&bank, &vote_keypair, &mint_keypair);
// Test that votes and credits are there
let account = bank.get_account(&vote_pubkey).expect("account not found");
let vote_state: VoteState = StateMut::<VoteStateVersions>::state(&account)
.expect("couldn't unpack account data")
.convert_to_current();
// 1 less vote, as the first vote should have cleared the lockout
assert_eq!(vote_state.votes.len(), 31);
// one vote per slot, might be more slots than 32 in the epoch
assert!(vote_state.credits() >= 1);
bank = fill_epoch_with_votes(&bank, &vote_keypair, &mint_keypair);
let pre_staked = get_staked(&bank, &stake_pubkey);
// next epoch bank should pay rewards
bank = next_epoch(&bank);
// Test that balance increased, and that the balance got staked
let staked = get_staked(&bank, &stake_pubkey);
let lamports = bank.get_balance(&stake_pubkey);
assert!(staked > pre_staked);
assert!(lamports > 1_000_000);
// split the stake
let split_stake_keypair = Keypair::new();
let split_stake_pubkey = split_stake_keypair.pubkey();
let bank_client = BankClient::new_shared(&bank);
// Test split
let message = Message::new(
&stake_instruction::split(
&stake_pubkey,
&stake_pubkey,
lamports / 2,
&split_stake_pubkey,
),
Some(&mint_pubkey),
);
assert!(bank_client
.send_and_confirm_message(
&[&mint_keypair, &stake_keypair, &split_stake_keypair],
message
)
.is_ok());
// Deactivate the split
let message = Message::new(
&[stake_instruction::deactivate_stake(
&split_stake_pubkey,
&stake_pubkey,
)],
Some(&mint_pubkey),
);
assert!(bank_client
.send_and_confirm_message(&[&mint_keypair, &stake_keypair], message)
.is_ok());
let split_staked = get_staked(&bank, &split_stake_pubkey);
// Test that we cannot withdraw above what's staked
let message = Message::new(
&[stake_instruction::withdraw(
&split_stake_pubkey,
&stake_pubkey,
&solana_sdk::pubkey::new_rand(),
lamports / 2 - split_staked + 1,
None,
)],
Some(&mint_pubkey),
);
assert!(bank_client
.send_and_confirm_message(&[&mint_keypair, &stake_keypair], message)
.is_err());
let mut bank = next_epoch(&bank);
let bank_client = BankClient::new_shared(&bank);
// assert we're still cooling down
let split_staked = get_staked(&bank, &split_stake_pubkey);
assert!(split_staked > 0);
// withdrawal in cooldown
let message = Message::new(
&[stake_instruction::withdraw(
&split_stake_pubkey,
&stake_pubkey,
&solana_sdk::pubkey::new_rand(),
lamports / 2,
None,
)],
Some(&mint_pubkey),
);
assert!(bank_client
.send_and_confirm_message(&[&mint_keypair, &stake_keypair], message)
.is_err());
// but we can withdraw unstaked
let message = Message::new(
&[stake_instruction::withdraw(
&split_stake_pubkey,
&stake_pubkey,
&solana_sdk::pubkey::new_rand(),
lamports / 2 - split_staked,
None,
)],
Some(&mint_pubkey),
);
// assert we can withdraw unstaked tokens
assert!(bank_client
.send_and_confirm_message(&[&mint_keypair, &stake_keypair], message)
.is_ok());
// finish cooldown
loop {
if get_staked(&bank, &split_stake_pubkey) == 0 {
break;
}
bank = next_epoch(&bank);
}
let bank_client = BankClient::new_shared(&bank);
// Test that we can withdraw everything else out of the split
let message = Message::new(
&[stake_instruction::withdraw(
&split_stake_pubkey,
&stake_pubkey,
&solana_sdk::pubkey::new_rand(),
split_staked,
None,
)],
Some(&mint_pubkey),
);
assert!(bank_client
.send_and_confirm_message(&[&mint_keypair, &stake_keypair], message)
.is_ok());
// verify all the math sums to zero
assert_eq!(bank.get_balance(&split_stake_pubkey), 0);
assert_eq!(bank.get_balance(&stake_pubkey), lamports - lamports / 2);
}
#[test]
fn test_create_stake_account_from_seed() {
let vote_keypair = Keypair::new();
let vote_pubkey = vote_keypair.pubkey();
let identity_keypair = Keypair::new();
let identity_pubkey = identity_keypair.pubkey();
let GenesisConfigInfo {
genesis_config,
mint_keypair,
..
} = create_genesis_config_with_leader(
100_000_000_000,
&solana_sdk::pubkey::new_rand(),
1_000_000,
);
let bank = Bank::new(&genesis_config);
let mint_pubkey = mint_keypair.pubkey();
let bank = Arc::new(bank);
let bank_client = BankClient::new_shared(&bank);
let seed = "test-string";
let stake_pubkey =
Pubkey::create_with_seed(&mint_pubkey, seed, &solana_stake_program::id()).unwrap();
// Create Vote Account
let message = Message::new(
&vote_instruction::create_account(
&mint_pubkey,
&vote_pubkey,
&VoteInit {
node_pubkey: identity_pubkey,
authorized_voter: vote_pubkey,
authorized_withdrawer: vote_pubkey,
commission: 50,
},
10,
),
Some(&mint_pubkey),
);
bank_client
.send_and_confirm_message(&[&mint_keypair, &vote_keypair, &identity_keypair], message)
.expect("failed to create vote account");
let authorized = stake_state::Authorized::auto(&mint_pubkey);
// Create stake account and delegate to vote account
let message = Message::new(
&stake_instruction::create_account_with_seed_and_delegate_stake(
&mint_pubkey,
&stake_pubkey,
&mint_pubkey,
seed,
&vote_pubkey,
&authorized,
&stake_state::Lockup::default(),
1_000_000,
),
Some(&mint_pubkey),
);
bank_client
.send_and_confirm_message(&[&mint_keypair], message)
.expect("failed to create and delegate stake account");
// Test that correct lamports are staked
let account = bank.get_account(&stake_pubkey).expect("account not found");
let stake_state = account.state().expect("couldn't unpack account data");
if let StakeState::Stake(_meta, stake) = stake_state {
assert_eq!(stake.delegation.stake, 1_000_000);
} else {
panic!("wrong account type found")
}
}