vote: Add helper for creating current-versioned states

This commit is contained in:
Trent Nelson
2020-12-21 16:19:04 -07:00
committed by Trent Nelson
parent 3881ae10fb
commit 5b903318b2
13 changed files with 37 additions and 33 deletions

View File

@@ -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());
}

View File

@@ -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) => {