makes solana_runtime::vote_account::VoteAccount private

VoteAccount is an implementation detail, and should not be public.
Only ArcVoteAccount is the public type.
This commit is contained in:
behzad nouri
2021-08-05 16:44:07 -04:00
parent 3b506b568a
commit 1403eaeefc

View File

@ -11,7 +11,6 @@ use {
cmp::Ordering, cmp::Ordering,
collections::{hash_map::Entry, HashMap}, collections::{hash_map::Entry, HashMap},
iter::FromIterator, iter::FromIterator,
ops::Deref,
sync::{Arc, Once, RwLock, RwLockReadGuard}, sync::{Arc, Once, RwLock, RwLockReadGuard},
}, },
}; };
@ -25,7 +24,7 @@ const INVALID_VOTE_STATE: Result<VoteState, InstructionError> =
pub struct ArcVoteAccount(Arc<VoteAccount>); pub struct ArcVoteAccount(Arc<VoteAccount>);
#[derive(Debug, AbiExample)] #[derive(Debug, AbiExample)]
pub struct VoteAccount { struct VoteAccount {
account: Account, account: Account,
vote_state: RwLock<Result<VoteState, InstructionError>>, vote_state: RwLock<Result<VoteState, InstructionError>>,
vote_state_once: Once, vote_state_once: Once,
@ -47,16 +46,18 @@ pub struct VoteAccounts {
staked_nodes_once: Once, staked_nodes_once: Once,
} }
impl VoteAccount { impl ArcVoteAccount {
pub fn lamports(&self) -> u64 { pub fn lamports(&self) -> u64 {
self.account.lamports self.0.account.lamports
} }
pub fn vote_state(&self) -> RwLockReadGuard<Result<VoteState, InstructionError>> { pub fn vote_state(&self) -> RwLockReadGuard<Result<VoteState, InstructionError>> {
self.vote_state_once.call_once(|| { let inner = &self.0;
*self.vote_state.write().unwrap() = VoteState::deserialize(&self.account.data); inner.vote_state_once.call_once(|| {
let vote_state = VoteState::deserialize(&inner.account.data);
*inner.vote_state.write().unwrap() = vote_state;
}); });
self.vote_state.read().unwrap() inner.vote_state.read().unwrap()
} }
/// VoteState.node_pubkey of this vote-account. /// VoteState.node_pubkey of this vote-account.
@ -155,20 +156,12 @@ impl VoteAccounts {
} }
} }
impl Deref for ArcVoteAccount {
type Target = VoteAccount;
fn deref(&self) -> &Self::Target {
self.0.deref()
}
}
impl Serialize for ArcVoteAccount { impl Serialize for ArcVoteAccount {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where where
S: Serializer, S: Serializer,
{ {
self.account.serialize(serializer) self.0.account.serialize(serializer)
} }
} }
@ -187,6 +180,7 @@ impl From<AccountSharedData> for ArcVoteAccount {
Self(Arc::new(VoteAccount::from(account))) Self(Arc::new(VoteAccount::from(account)))
} }
} }
impl From<Account> for ArcVoteAccount { impl From<Account> for ArcVoteAccount {
fn from(account: Account) -> Self { fn from(account: Account) -> Self {
Self(Arc::new(VoteAccount::from(account))) Self(Arc::new(VoteAccount::from(account)))
@ -195,11 +189,7 @@ impl From<Account> for ArcVoteAccount {
impl From<AccountSharedData> for VoteAccount { impl From<AccountSharedData> for VoteAccount {
fn from(account: AccountSharedData) -> Self { fn from(account: AccountSharedData) -> Self {
Self { Self::from(Account::from(account))
account: Account::from(account),
vote_state: RwLock::new(INVALID_VOTE_STATE),
vote_state_once: Once::new(),
}
} }
} }