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_account.rs
137 lines (125 loc) · 4.01 KB
/
stake_account.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
#[cfg(RUSTC_WITH_SPECIALIZATION)]
use solana_frozen_abi::abi_example::AbiExample;
use {
solana_sdk::{
account::{AccountSharedData, ReadableAccount},
account_utils::StateMut,
instruction::InstructionError,
pubkey::Pubkey,
stake::state::{Delegation, StakeState},
},
std::marker::PhantomData,
thiserror::Error,
};
/// An account and a stake state deserialized from the account.
/// Generic type T enforces type-safety so that StakeAccount<Delegation> can
/// only wrap a stake-state which is a Delegation; whereas StakeAccount<()>
/// wraps any account with stake state.
#[derive(Clone, Debug, Default)]
pub struct StakeAccount<T> {
account: AccountSharedData,
stake_state: StakeState,
_phantom: PhantomData<T>,
}
#[derive(Debug, Error)]
#[allow(clippy::enum_variant_names)]
pub enum Error {
#[error(transparent)]
InstructionError(#[from] InstructionError),
#[error("Invalid delegation: {0:?}")]
InvalidDelegation(Box<StakeState>),
#[error("Invalid stake account owner: {0}")]
InvalidOwner(/*owner:*/ Pubkey),
}
impl<T> StakeAccount<T> {
#[inline]
pub(crate) fn lamports(&self) -> u64 {
self.account.lamports()
}
#[inline]
pub(crate) fn stake_state(&self) -> &StakeState {
&self.stake_state
}
pub(crate) fn account(&self) -> &AccountSharedData {
&self.account
}
}
impl StakeAccount<Delegation> {
#[inline]
pub(crate) fn delegation(&self) -> Delegation {
// Safe to unwrap here because StakeAccount<Delegation> will always
// only wrap a stake-state which is a delegation.
self.stake_state.delegation().unwrap()
}
}
impl TryFrom<AccountSharedData> for StakeAccount<()> {
type Error = Error;
fn try_from(account: AccountSharedData) -> Result<Self, Self::Error> {
if account.owner() != &solana_stake_program::id() {
return Err(Error::InvalidOwner(*account.owner()));
}
let stake_state = account.state()?;
Ok(Self {
account,
stake_state,
_phantom: PhantomData,
})
}
}
impl TryFrom<AccountSharedData> for StakeAccount<Delegation> {
type Error = Error;
fn try_from(account: AccountSharedData) -> Result<Self, Self::Error> {
let stake_account = StakeAccount::<()>::try_from(account)?;
if stake_account.stake_state.delegation().is_none() {
return Err(Error::InvalidDelegation(Box::new(
stake_account.stake_state,
)));
}
Ok(Self {
account: stake_account.account,
stake_state: stake_account.stake_state,
_phantom: PhantomData,
})
}
}
impl From<StakeAccount<Delegation>> for StakeAccount<()> {
#[inline]
fn from(stake_account: StakeAccount<Delegation>) -> Self {
Self {
account: stake_account.account,
stake_state: stake_account.stake_state,
_phantom: PhantomData,
}
}
}
impl<T> From<StakeAccount<T>> for (AccountSharedData, StakeState) {
#[inline]
fn from(stake_account: StakeAccount<T>) -> Self {
(stake_account.account, stake_account.stake_state)
}
}
impl<S, T> PartialEq<StakeAccount<S>> for StakeAccount<T> {
fn eq(&self, other: &StakeAccount<S>) -> bool {
let StakeAccount {
account,
stake_state,
_phantom,
} = other;
account == &self.account && stake_state == &self.stake_state
}
}
#[cfg(RUSTC_WITH_SPECIALIZATION)]
impl AbiExample for StakeAccount<Delegation> {
fn example() -> Self {
use solana_sdk::{
account::Account,
stake::state::{Meta, Stake},
};
let stake_state = StakeState::Stake(Meta::example(), Stake::example());
let mut account = Account::example();
account.data.resize(196, 0u8);
account.owner = solana_stake_program::id();
account.set_state(&stake_state).unwrap();
Self::try_from(AccountSharedData::from(account)).unwrap()
}
}