runtime: transpose `VoteAccount::vote_state()` return to improve ergonomics (#31256)

This commit is contained in:
Trent Nelson 2023-04-18 14:48:52 -06:00 committed by GitHub
parent ad30a60059
commit f34a6bcfce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 21 additions and 29 deletions

View File

@ -302,7 +302,7 @@ impl Tower {
continue;
}
trace!("{} {} with stake {}", vote_account_pubkey, key, voted_stake);
let mut vote_state = match account.vote_state().as_ref() {
let mut vote_state = match account.vote_state().cloned() {
Err(_) => {
datapoint_warn!(
"tower_warn",
@ -314,7 +314,7 @@ impl Tower {
);
continue;
}
Ok(vote_state) => vote_state.clone(),
Ok(vote_state) => vote_state,
};
for vote in &vote_state.votes {
lockout_intervals
@ -1284,9 +1284,8 @@ impl Tower {
if let Some(vote_account) = bank.get_vote_account(vote_account_pubkey) {
self.vote_state = vote_account
.vote_state()
.as_ref()
.expect("vote_account isn't a VoteState?")
.clone();
.cloned()
.expect("vote_account isn't a VoteState?");
self.initialize_root(root);
self.initialize_lockouts(|v| v.slot() > root);
trace!(

View File

@ -2894,9 +2894,7 @@ impl ReplayStage {
Tower::is_direct_vote_state_update_enabled(bank),
bank.get_vote_account(my_vote_pubkey),
) {
if let Some(mut bank_vote_state) =
vote_account.vote_state().as_ref().ok().cloned()
{
if let Ok(mut bank_vote_state) = vote_account.vote_state().cloned() {
if bank_vote_state.last_voted_slot()
> tower.vote_state.last_voted_slot()
{

View File

@ -520,7 +520,7 @@ fn graph_forks(bank_forks: &BankForks, config: &GraphConfig) -> String {
.sum();
for (stake, vote_account) in bank.vote_accounts().values() {
let vote_state = vote_account.vote_state();
let vote_state = vote_state.as_ref().unwrap_or(&default_vote_state);
let vote_state = vote_state.unwrap_or(&default_vote_state);
if let Some(last_vote) = vote_state.votes.iter().last() {
let entry = last_votes.entry(vote_state.node_pubkey).or_insert((
last_vote.slot(),
@ -561,7 +561,7 @@ fn graph_forks(bank_forks: &BankForks, config: &GraphConfig) -> String {
loop {
for (_, vote_account) in bank.vote_accounts().values() {
let vote_state = vote_account.vote_state();
let vote_state = vote_state.as_ref().unwrap_or(&default_vote_state);
let vote_state = vote_state.unwrap_or(&default_vote_state);
if let Some(last_vote) = vote_state.votes.iter().last() {
let validator_votes = all_votes.entry(vote_state.node_pubkey).or_default();
validator_votes

View File

@ -939,7 +939,7 @@ impl JsonRpcRequestProcessor {
}
let vote_state = account.vote_state();
let vote_state = vote_state.as_ref().unwrap_or(&default_vote_state);
let vote_state = vote_state.unwrap_or(&default_vote_state);
let last_vote = if let Some(vote) = vote_state.votes.iter().last() {
vote.slot()
} else {

View File

@ -171,7 +171,7 @@ use {
collections::{HashMap, HashSet},
convert::{TryFrom, TryInto},
fmt, mem,
ops::{AddAssign, Deref, RangeInclusive},
ops::{AddAssign, RangeInclusive},
path::PathBuf,
rc::Rc,
sync::{
@ -2740,8 +2740,8 @@ impl Bank {
invalid_vote_keys.insert(vote_pubkey, InvalidCacheEntryReason::WrongOwner);
return None;
}
let vote_state = match vote_account.vote_state().deref() {
Ok(vote_state) => vote_state.clone(),
let vote_state = match vote_account.vote_state().cloned() {
Ok(vote_state) => vote_state,
Err(_) => {
invalid_vote_keys.insert(vote_pubkey, InvalidCacheEntryReason::BadState);
return None;

View File

@ -65,12 +65,13 @@ impl VoteAccount {
self.0.account.owner()
}
pub fn vote_state(&self) -> &Result<VoteState, Error> {
pub fn vote_state(&self) -> Result<&VoteState, &Error> {
// VoteState::deserialize deserializes a VoteStateVersions and then
// calls VoteStateVersions::convert_to_current.
self.0
.vote_state
.get_or_init(|| VoteState::deserialize(self.0.account.data()).map_err(Error::from))
.as_ref()
}
pub(crate) fn is_deserialized(&self) -> bool {
@ -79,7 +80,7 @@ impl VoteAccount {
/// VoteState.node_pubkey of this vote-account.
pub fn node_pubkey(&self) -> Option<Pubkey> {
Some(self.vote_state().as_ref().ok()?.node_pubkey)
Some(self.vote_state().ok()?.node_pubkey)
}
}
@ -395,9 +396,9 @@ mod tests {
let lamports = account.lamports();
let vote_account = VoteAccount::try_from(account).unwrap();
assert_eq!(lamports, vote_account.lamports());
assert_eq!(vote_state, *vote_account.vote_state().as_ref().unwrap());
assert_eq!(vote_state, *vote_account.vote_state().unwrap());
// 2nd call to .vote_state() should return the cached value.
assert_eq!(vote_state, *vote_account.vote_state().as_ref().unwrap());
assert_eq!(vote_state, *vote_account.vote_state().unwrap());
}
#[test]
@ -405,7 +406,7 @@ mod tests {
let mut rng = rand::thread_rng();
let (account, vote_state) = new_rand_vote_account(&mut rng, None);
let vote_account = VoteAccount::try_from(account.clone()).unwrap();
assert_eq!(vote_state, *vote_account.vote_state().as_ref().unwrap());
assert_eq!(vote_state, *vote_account.vote_state().unwrap());
// Assert than VoteAccount has the same wire format as Account.
assert_eq!(
bincode::serialize(&account).unwrap(),
@ -419,13 +420,10 @@ mod tests {
let (account, vote_state) = new_rand_vote_account(&mut rng, None);
let data = bincode::serialize(&account).unwrap();
let vote_account = VoteAccount::try_from(account).unwrap();
assert_eq!(vote_state, *vote_account.vote_state().as_ref().unwrap());
assert_eq!(vote_state, *vote_account.vote_state().unwrap());
let other_vote_account: VoteAccount = bincode::deserialize(&data).unwrap();
assert_eq!(vote_account, other_vote_account);
assert_eq!(
vote_state,
*other_vote_account.vote_state().as_ref().unwrap()
);
assert_eq!(vote_state, *other_vote_account.vote_state().unwrap());
}
#[test]
@ -433,15 +431,12 @@ mod tests {
let mut rng = rand::thread_rng();
let (account, vote_state) = new_rand_vote_account(&mut rng, None);
let vote_account = VoteAccount::try_from(account).unwrap();
assert_eq!(vote_state, *vote_account.vote_state().as_ref().unwrap());
assert_eq!(vote_state, *vote_account.vote_state().unwrap());
let data = bincode::serialize(&vote_account).unwrap();
let other_vote_account: VoteAccount = bincode::deserialize(&data).unwrap();
// Assert that serialize->deserialized returns the same VoteAccount.
assert_eq!(vote_account, other_vote_account);
assert_eq!(
vote_state,
*other_vote_account.vote_state().as_ref().unwrap()
);
assert_eq!(vote_state, *other_vote_account.vote_state().unwrap());
}
#[test]