vote: Add helper for creating current-versioned states
This commit is contained in:
committed by
Trent Nelson
parent
3881ae10fb
commit
5b903318b2
@@ -1004,7 +1004,7 @@ mod tests {
|
||||
let vote_keyed_account = KeyedAccount::new(&vote_pubkey, false, &vote_account);
|
||||
let vote_state_credits = vote_state.credits();
|
||||
vote_keyed_account
|
||||
.set_state(&VoteStateVersions::Current(Box::new(vote_state)))
|
||||
.set_state(&VoteStateVersions::new_current(vote_state))
|
||||
.unwrap();
|
||||
|
||||
let stake_pubkey = solana_sdk::pubkey::new_rand();
|
||||
@@ -1902,7 +1902,7 @@ mod tests {
|
||||
));
|
||||
let vote_keyed_account = KeyedAccount::new(&vote_pubkey, false, &vote_account);
|
||||
vote_keyed_account
|
||||
.set_state(&VoteStateVersions::Current(Box::new(VoteState::default())))
|
||||
.set_state(&VoteStateVersions::new_current(VoteState::default()))
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
stake_keyed_account.delegate(
|
||||
@@ -1993,7 +1993,7 @@ mod tests {
|
||||
));
|
||||
let vote_keyed_account = KeyedAccount::new(&vote_pubkey, false, &vote_account);
|
||||
vote_keyed_account
|
||||
.set_state(&VoteStateVersions::Current(Box::new(VoteState::default())))
|
||||
.set_state(&VoteStateVersions::new_current(VoteState::default()))
|
||||
.unwrap();
|
||||
|
||||
stake_keyed_account
|
||||
@@ -2235,7 +2235,7 @@ mod tests {
|
||||
));
|
||||
let vote_keyed_account = KeyedAccount::new(&vote_pubkey, false, &vote_account);
|
||||
vote_keyed_account
|
||||
.set_state(&VoteStateVersions::Current(Box::new(VoteState::default())))
|
||||
.set_state(&VoteStateVersions::new_current(VoteState::default()))
|
||||
.unwrap();
|
||||
let signers = vec![stake_pubkey].into_iter().collect();
|
||||
assert_eq!(
|
||||
@@ -2351,7 +2351,7 @@ mod tests {
|
||||
));
|
||||
let vote_keyed_account = KeyedAccount::new(&vote_pubkey, false, &vote_account);
|
||||
vote_keyed_account
|
||||
.set_state(&VoteStateVersions::Current(Box::new(VoteState::default())))
|
||||
.set_state(&VoteStateVersions::new_current(VoteState::default()))
|
||||
.unwrap();
|
||||
let signers = vec![stake_pubkey].into_iter().collect();
|
||||
assert_eq!(
|
||||
|
@@ -1641,7 +1641,7 @@ mod tests {
|
||||
let vote_keyed_account = KeyedAccount::new(&vote_pubkey, false, &vote_account);
|
||||
let vote_state_credits = vote_state.credits();
|
||||
vote_keyed_account
|
||||
.set_state(&VoteStateVersions::Current(Box::new(vote_state)))
|
||||
.set_state(&VoteStateVersions::new_current(vote_state))
|
||||
.unwrap();
|
||||
|
||||
let stake_pubkey = solana_sdk::pubkey::new_rand();
|
||||
@@ -2539,7 +2539,7 @@ mod tests {
|
||||
));
|
||||
let vote_keyed_account = KeyedAccount::new(&vote_pubkey, false, &vote_account);
|
||||
vote_keyed_account
|
||||
.set_state(&VoteStateVersions::Current(Box::new(VoteState::default())))
|
||||
.set_state(&VoteStateVersions::new_current(VoteState::default()))
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
stake_keyed_account.delegate(
|
||||
@@ -2630,7 +2630,7 @@ mod tests {
|
||||
));
|
||||
let vote_keyed_account = KeyedAccount::new(&vote_pubkey, false, &vote_account);
|
||||
vote_keyed_account
|
||||
.set_state(&VoteStateVersions::Current(Box::new(VoteState::default())))
|
||||
.set_state(&VoteStateVersions::new_current(VoteState::default()))
|
||||
.unwrap();
|
||||
|
||||
stake_keyed_account
|
||||
@@ -2872,7 +2872,7 @@ mod tests {
|
||||
));
|
||||
let vote_keyed_account = KeyedAccount::new(&vote_pubkey, false, &vote_account);
|
||||
vote_keyed_account
|
||||
.set_state(&VoteStateVersions::Current(Box::new(VoteState::default())))
|
||||
.set_state(&VoteStateVersions::new_current(VoteState::default()))
|
||||
.unwrap();
|
||||
let signers = vec![stake_pubkey].into_iter().collect();
|
||||
assert_eq!(
|
||||
@@ -2988,7 +2988,7 @@ mod tests {
|
||||
));
|
||||
let vote_keyed_account = KeyedAccount::new(&vote_pubkey, false, &vote_account);
|
||||
vote_keyed_account
|
||||
.set_state(&VoteStateVersions::Current(Box::new(VoteState::default())))
|
||||
.set_state(&VoteStateVersions::new_current(VoteState::default()))
|
||||
.unwrap();
|
||||
let signers = vec![stake_pubkey].into_iter().collect();
|
||||
assert_eq!(
|
||||
|
@@ -214,7 +214,7 @@ impl VoteState {
|
||||
pub fn size_of() -> usize {
|
||||
// Upper limit on the size of the Vote State. Equal to
|
||||
// size_of(VoteState) when votes.len() is MAX_LOCKOUT_HISTORY
|
||||
let vote_state = VoteStateVersions::Current(Box::new(Self::get_max_sized_vote_state()));
|
||||
let vote_state = VoteStateVersions::new_current(Self::get_max_sized_vote_state());
|
||||
serialized_size(&vote_state).unwrap() as usize
|
||||
}
|
||||
|
||||
@@ -592,7 +592,7 @@ pub fn authorize<S: std::hash::BuildHasher>(
|
||||
}
|
||||
}
|
||||
|
||||
vote_account.set_state(&VoteStateVersions::Current(Box::new(vote_state)))
|
||||
vote_account.set_state(&VoteStateVersions::new_current(vote_state))
|
||||
}
|
||||
|
||||
/// Update the node_pubkey, requires signature of the authorized voter
|
||||
@@ -612,7 +612,7 @@ pub fn update_validator_identity<S: std::hash::BuildHasher>(
|
||||
|
||||
vote_state.node_pubkey = *node_pubkey;
|
||||
|
||||
vote_account.set_state(&VoteStateVersions::Current(Box::new(vote_state)))
|
||||
vote_account.set_state(&VoteStateVersions::new_current(vote_state))
|
||||
}
|
||||
|
||||
/// Update the vote account's commission
|
||||
@@ -629,7 +629,7 @@ pub fn update_commission<S: std::hash::BuildHasher>(
|
||||
|
||||
vote_state.commission = commission;
|
||||
|
||||
vote_account.set_state(&VoteStateVersions::Current(Box::new(vote_state)))
|
||||
vote_account.set_state(&VoteStateVersions::new_current(vote_state))
|
||||
}
|
||||
|
||||
fn verify_authorized_signer<S: std::hash::BuildHasher>(
|
||||
@@ -681,9 +681,9 @@ pub fn initialize_account<S: std::hash::BuildHasher>(
|
||||
// node must agree to accept this vote account
|
||||
verify_authorized_signer(&vote_init.node_pubkey, signers)?;
|
||||
|
||||
vote_account.set_state(&VoteStateVersions::Current(Box::new(VoteState::new(
|
||||
vote_account.set_state(&VoteStateVersions::new_current(VoteState::new(
|
||||
vote_init, clock,
|
||||
))))
|
||||
)))
|
||||
}
|
||||
|
||||
pub fn process_vote<S: std::hash::BuildHasher>(
|
||||
@@ -711,7 +711,7 @@ pub fn process_vote<S: std::hash::BuildHasher>(
|
||||
.ok_or(VoteError::EmptySlots)
|
||||
.and_then(|slot| vote_state.process_timestamp(*slot, timestamp))?;
|
||||
}
|
||||
vote_account.set_state(&VoteStateVersions::Current(Box::new(vote_state)))
|
||||
vote_account.set_state(&VoteStateVersions::new_current(vote_state))
|
||||
}
|
||||
|
||||
pub fn create_account_with_authorized(
|
||||
@@ -733,7 +733,7 @@ pub fn create_account_with_authorized(
|
||||
&Clock::default(),
|
||||
);
|
||||
|
||||
let versioned = VoteStateVersions::Current(Box::new(vote_state));
|
||||
let versioned = VoteStateVersions::new_current(vote_state);
|
||||
VoteState::to(&versioned, &mut vote_account).unwrap();
|
||||
|
||||
vote_account
|
||||
@@ -911,11 +911,11 @@ mod tests {
|
||||
vote_state
|
||||
.votes
|
||||
.resize(MAX_LOCKOUT_HISTORY, Lockout::default());
|
||||
let versioned = VoteStateVersions::Current(Box::new(vote_state));
|
||||
let versioned = VoteStateVersions::new_current(vote_state);
|
||||
assert!(VoteState::serialize(&versioned, &mut buffer[0..4]).is_err());
|
||||
VoteState::serialize(&versioned, &mut buffer).unwrap();
|
||||
assert_eq!(
|
||||
VoteStateVersions::Current(Box::new(VoteState::deserialize(&buffer).unwrap())),
|
||||
VoteStateVersions::new_current(VoteState::deserialize(&buffer).unwrap()),
|
||||
versioned
|
||||
);
|
||||
}
|
||||
@@ -1985,7 +1985,7 @@ mod tests {
|
||||
)
|
||||
});
|
||||
|
||||
let versioned = VoteStateVersions::Current(Box::new(vote_state.take().unwrap()));
|
||||
let versioned = VoteStateVersions::new_current(vote_state.take().unwrap());
|
||||
VoteState::serialize(&versioned, &mut max_sized_data).unwrap();
|
||||
vote_state = Some(versioned.convert_to_current());
|
||||
}
|
||||
|
@@ -8,6 +8,10 @@ pub enum VoteStateVersions {
|
||||
}
|
||||
|
||||
impl VoteStateVersions {
|
||||
pub fn new_current(vote_state: VoteState) -> Self {
|
||||
Self::Current(Box::new(vote_state))
|
||||
}
|
||||
|
||||
pub fn convert_to_current(self) -> VoteState {
|
||||
match self {
|
||||
VoteStateVersions::V0_23_5(state) => {
|
||||
|
Reference in New Issue
Block a user