-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathinstant_remove_validator.rs
94 lines (78 loc) · 2.78 KB
/
instant_remove_validator.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
use crate::{
errors::StewardError,
utils::{
deserialize_stake_pool, get_stake_pool_address, get_validator_list,
get_validator_list_length, tally_stake_status,
},
Config, StewardStateAccount,
};
use anchor_lang::prelude::*;
#[derive(Accounts)]
pub struct InstantRemoveValidator<'info> {
pub config: AccountLoader<'info, Config>,
#[account(
mut,
seeds = [StewardStateAccount::SEED, config.key().as_ref()],
bump
)]
pub state_account: AccountLoader<'info, StewardStateAccount>,
/// CHECK: Correct account guaranteed if address is correct
#[account(address = get_validator_list(&config)?)]
pub validator_list: AccountInfo<'info>,
/// CHECK: Correct account guaranteed if address is correct
#[account(
address = get_stake_pool_address(&config)?
)]
pub stake_pool: AccountInfo<'info>,
}
/// Removes validators from the pool that have been marked for immediate removal
pub fn handler(
ctx: Context<InstantRemoveValidator>,
validator_index_to_remove: usize,
) -> Result<()> {
let stake_pool = deserialize_stake_pool(&ctx.accounts.stake_pool)?;
let mut state_account = ctx.accounts.state_account.load_mut()?;
let clock = Clock::get()?;
let validators_for_immediate_removal =
state_account.state.validators_for_immediate_removal.count();
let validators_in_list = get_validator_list_length(&ctx.accounts.validator_list)?;
require!(
state_account.state.current_epoch == clock.epoch,
StewardError::EpochMaintenanceNotComplete
);
require!(
clock.epoch == stake_pool.last_update_epoch,
StewardError::StakePoolNotUpdated
);
require!(
state_account
.state
.validators_for_immediate_removal
.get(validator_index_to_remove)?,
StewardError::ValidatorNotInList
);
let stake_status_tally = tally_stake_status(&ctx.accounts.validator_list)?;
let total_deactivating = stake_status_tally.deactivating_all
+ stake_status_tally.deactivating_transient
+ stake_status_tally.deactivating_validator
+ stake_status_tally.ready_for_removal;
require!(
total_deactivating == state_account.state.validators_to_remove.count() as u64,
StewardError::ValidatorsHaveNotBeenRemoved
);
require!(
stake_status_tally.ready_for_removal == 0,
StewardError::ValidatorsNeedToBeRemoved
);
require!(
state_account.state.num_pool_validators as usize
+ state_account.state.validators_added as usize
- validators_for_immediate_removal
== validators_in_list,
StewardError::ListStateMismatch
);
state_account
.state
.remove_validator(validator_index_to_remove)?;
Ok(())
}