renames solana_runtime::vote_account::VoteAccount and makes it private (backport #19153) (#22141)

* makes solana_runtime::vote_account::VoteAccount private

VoteAccount is an implementation detail, and should not be public.
Only ArcVoteAccount is the public type.

(cherry picked from commit 1403eaeefc)

# Conflicts:
#	runtime/src/vote_account.rs

* renames solana_runtime::vote_account::VoteAccount

Rename:
  VoteAccount    -> VoteAccountInner  # the private type
  ArcVoteAccount -> VoteAccount       # the public type
(cherry picked from commit 00e5e12906)

# Conflicts:
#	core/src/progress_map.rs
#	ledger/src/blockstore_processor.rs
#	ledger/src/staking_utils.rs
#	runtime/src/bank.rs
#	runtime/src/epoch_stakes.rs
#	runtime/src/serde_snapshot/tests.rs
#	runtime/src/stakes.rs
#	runtime/src/vote_account.rs

* removes backport merge conflicts

Co-authored-by: behzad nouri <behzadnouri@gmail.com>
This commit is contained in:
mergify[bot]
2021-12-28 18:26:23 +00:00
committed by GitHub
parent bfb02029bf
commit 262b157d21
10 changed files with 77 additions and 98 deletions

View File

@@ -61,7 +61,7 @@ use {
status_cache::{SlotDelta, StatusCache},
system_instruction_processor::{get_system_account_kind, SystemAccountKind},
transaction_batch::TransactionBatch,
vote_account::ArcVoteAccount,
vote_account::VoteAccount,
},
byteorder::{ByteOrder, LittleEndian},
dashmap::DashMap,
@@ -3809,7 +3809,7 @@ impl Bank {
#[allow(clippy::needless_collect)]
fn distribute_rent_to_validators<I>(&self, vote_accounts: I, rent_to_be_distributed: u64)
where
I: IntoIterator<Item = (Pubkey, (u64, ArcVoteAccount))>,
I: IntoIterator<Item = (Pubkey, (u64, VoteAccount))>,
{
let mut total_staked = 0;
@@ -5196,7 +5196,7 @@ impl Bank {
/// attributed to each account
/// Note: This clones the entire vote-accounts hashmap. For a single
/// account lookup use get_vote_account instead.
pub fn vote_accounts(&self) -> Vec<(Pubkey, (u64 /*stake*/, ArcVoteAccount))> {
pub fn vote_accounts(&self) -> Vec<(Pubkey, (/*stake:*/ u64, VoteAccount))> {
self.stakes_cache
.stakes()
.vote_accounts()
@@ -5206,10 +5206,7 @@ impl Bank {
}
/// Vote account for the given vote account pubkey along with the stake.
pub fn get_vote_account(
&self,
vote_account: &Pubkey,
) -> Option<(u64 /*stake*/, ArcVoteAccount)> {
pub fn get_vote_account(&self, vote_account: &Pubkey) -> Option<(/*stake:*/ u64, VoteAccount)> {
self.stakes_cache
.stakes()
.vote_accounts()
@@ -5235,7 +5232,7 @@ impl Bank {
pub fn epoch_vote_accounts(
&self,
epoch: Epoch,
) -> Option<&HashMap<Pubkey, (u64, ArcVoteAccount)>> {
) -> Option<&HashMap<Pubkey, (u64, VoteAccount)>> {
self.epoch_stakes
.get(&epoch)
.map(|epoch_stakes| Stakes::vote_accounts(epoch_stakes.stakes()))

View File

@@ -1,5 +1,5 @@
use {
crate::{stakes::Stakes, vote_account::ArcVoteAccount},
crate::{stakes::Stakes, vote_account::VoteAccount},
serde::{Deserialize, Serialize},
solana_sdk::{clock::Epoch, pubkey::Pubkey},
std::{collections::HashMap, sync::Arc},
@@ -59,7 +59,7 @@ impl EpochStakes {
}
fn parse_epoch_vote_accounts(
epoch_vote_accounts: &HashMap<Pubkey, (u64, ArcVoteAccount)>,
epoch_vote_accounts: &HashMap<Pubkey, (u64, VoteAccount)>,
leader_schedule_epoch: Epoch,
) -> (u64, NodeIdToVoteAccounts, EpochAuthorizedVoters) {
let mut node_id_to_vote_accounts: NodeIdToVoteAccounts = HashMap::new();
@@ -190,7 +190,7 @@ pub(crate) mod tests {
vote_accounts.iter().map(|v| {
(
v.vote_account,
(stake_per_account, ArcVoteAccount::from(v.account.clone())),
(stake_per_account, VoteAccount::from(v.account.clone())),
)
})
})

View File

@@ -296,7 +296,7 @@ mod test_bank_serialize {
// This some what long test harness is required to freeze the ABI of
// Bank's serialization due to versioned nature
#[frozen_abi(digest = "9NFbb7BMUbSjUr8xwRGhW5kT5jCTQj2U2vZtMxvovsTn")]
#[frozen_abi(digest = "9jiGyuG8pK3nBNbCEtLZBghXRNg3PHhkf1jS1GgTTdGz")]
#[derive(Serialize, AbiExample)]
pub struct BankAbiTestWrapperFuture {
#[serde(serialize_with = "wrapper_future")]

View File

@@ -23,7 +23,7 @@ pub(crate) struct MaxAllowableDrift {
pub(crate) fn calculate_stake_weighted_timestamp<I, K, V, T>(
unique_timestamps: I,
stakes: &HashMap<Pubkey, (u64, T /*Account|ArcVoteAccount*/)>,
stakes: &HashMap<Pubkey, (u64, T /*Account|VoteAccount*/)>,
slot: Slot,
slot_duration: Duration,
epoch_start_timestamp: Option<(Slot, UnixTimestamp)>,

View File

@@ -1,7 +1,7 @@
//! Stakes serve as a cache of stake and vote accounts to derive
//! node stakes
use {
crate::vote_account::{ArcVoteAccount, VoteAccounts, VoteAccountsHashMap},
crate::vote_account::{VoteAccount, VoteAccounts, VoteAccountsHashMap},
dashmap::DashMap,
num_derive::ToPrimitive,
num_traits::ToPrimitive,
@@ -268,7 +268,7 @@ impl Stakes {
);
self.vote_accounts
.insert(*pubkey, (stake, ArcVoteAccount::from(account.clone())));
.insert(*pubkey, (stake, VoteAccount::from(account.clone())));
}
} else if stake::program::check_id(account.owner()) {
// old_stake is stake lamports and voter_pubkey from the pre-store() version

View File

@@ -13,7 +13,6 @@ use {
cmp::Ordering,
collections::{hash_map::Entry, HashMap},
iter::FromIterator,
ops::Deref,
sync::{Arc, Once, RwLock, RwLockReadGuard},
},
};
@@ -24,20 +23,22 @@ const INVALID_VOTE_STATE: Result<VoteState, InstructionError> =
Err(InstructionError::InvalidAccountData);
#[derive(Clone, Debug, Default, PartialEq, AbiExample)]
pub struct ArcVoteAccount(Arc<VoteAccount>);
pub struct VoteAccount(Arc<VoteAccountInner>);
#[derive(Debug, AbiExample)]
pub struct VoteAccount {
struct VoteAccountInner {
account: Account,
vote_state: RwLock<Result<VoteState, InstructionError>>,
vote_state_once: Once,
}
pub type VoteAccountsHashMap = HashMap<Pubkey, (/*stake:*/ u64, ArcVoteAccount)>;
pub type VoteAccountsHashMap = HashMap<Pubkey, (/*stake:*/ u64, VoteAccount)>;
#[derive(Debug, AbiExample)]
pub struct VoteAccounts {
vote_accounts: VoteAccountsHashMap,
// Inner Arc is meant to implement copy-on-write semantics as opposed to
// sharing mutations (hence RwLock<Arc<...>> instead of Arc<RwLock<...>>).
staked_nodes: RwLock<
HashMap<
Pubkey, // VoteAccount.vote_state.node_pubkey.
@@ -48,19 +49,17 @@ pub struct VoteAccounts {
}
impl VoteAccount {
pub fn account(&self) -> &Account {
&self.account
}
pub fn lamports(&self) -> u64 {
self.account.lamports
self.0.account.lamports
}
pub fn vote_state(&self) -> RwLockReadGuard<Result<VoteState, InstructionError>> {
self.vote_state_once.call_once(|| {
*self.vote_state.write().unwrap() = VoteState::deserialize(&self.account.data);
let inner = &self.0;
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.
@@ -88,11 +87,11 @@ impl VoteAccounts {
self.staked_nodes.read().unwrap().clone()
}
pub fn iter(&self) -> impl Iterator<Item = (&Pubkey, &(u64, ArcVoteAccount))> {
pub fn iter(&self) -> impl Iterator<Item = (&Pubkey, &(u64, VoteAccount))> {
self.vote_accounts.iter()
}
pub fn insert(&mut self, pubkey: Pubkey, (stake, vote_account): (u64, ArcVoteAccount)) {
pub fn insert(&mut self, pubkey: Pubkey, (stake, vote_account): (u64, VoteAccount)) {
self.add_node_stake(stake, &vote_account);
if let Some((stake, vote_account)) =
self.vote_accounts.insert(pubkey, (stake, vote_account))
@@ -101,7 +100,7 @@ impl VoteAccounts {
}
}
pub fn remove(&mut self, pubkey: &Pubkey) -> Option<(u64, ArcVoteAccount)> {
pub fn remove(&mut self, pubkey: &Pubkey) -> Option<(u64, VoteAccount)> {
let value = self.vote_accounts.remove(pubkey);
if let Some((stake, ref vote_account)) = value {
self.sub_node_stake(stake, vote_account);
@@ -127,7 +126,7 @@ impl VoteAccounts {
}
}
fn add_node_stake(&mut self, stake: u64, vote_account: &ArcVoteAccount) {
fn add_node_stake(&mut self, stake: u64, vote_account: &VoteAccount) {
if stake != 0 && self.staked_nodes_once.is_completed() {
if let Some(node_pubkey) = vote_account.node_pubkey() {
self.staked_nodes
@@ -140,7 +139,7 @@ impl VoteAccounts {
}
}
fn sub_node_stake(&mut self, stake: u64, vote_account: &ArcVoteAccount) {
fn sub_node_stake(&mut self, stake: u64, vote_account: &VoteAccount) {
if stake != 0 && self.staked_nodes_once.is_completed() {
if let Some(node_pubkey) = vote_account.node_pubkey() {
match self.staked_nodes.write().unwrap().entry(node_pubkey) {
@@ -158,24 +157,16 @@ impl VoteAccounts {
}
}
impl Deref for ArcVoteAccount {
type Target = VoteAccount;
fn deref(&self) -> &Self::Target {
self.0.deref()
}
}
impl Serialize for ArcVoteAccount {
impl Serialize for VoteAccount {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
self.account.serialize(serializer)
self.0.account.serialize(serializer)
}
}
impl<'de> Deserialize<'de> for ArcVoteAccount {
impl<'de> Deserialize<'de> for VoteAccount {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
@@ -185,34 +176,25 @@ impl<'de> Deserialize<'de> for ArcVoteAccount {
}
}
impl From<AccountSharedData> for ArcVoteAccount {
fn from(account: AccountSharedData) -> Self {
Self(Arc::new(VoteAccount::from(account)))
}
}
impl From<Account> for ArcVoteAccount {
fn from(account: Account) -> Self {
Self(Arc::new(VoteAccount::from(account)))
}
}
impl AsRef<VoteAccount> for ArcVoteAccount {
fn as_ref(&self) -> &VoteAccount {
&self.0
}
}
impl From<AccountSharedData> for VoteAccount {
fn from(account: AccountSharedData) -> Self {
Self {
account: Account::from(account),
vote_state: RwLock::new(INVALID_VOTE_STATE),
vote_state_once: Once::new(),
}
Self(Arc::new(VoteAccountInner::from(account)))
}
}
impl From<Account> for VoteAccount {
fn from(account: Account) -> Self {
Self(Arc::new(VoteAccountInner::from(account)))
}
}
impl From<AccountSharedData> for VoteAccountInner {
fn from(account: AccountSharedData) -> Self {
Self::from(Account::from(account))
}
}
impl From<Account> for VoteAccountInner {
fn from(account: Account) -> Self {
Self {
account,
@@ -222,7 +204,7 @@ impl From<Account> for VoteAccount {
}
}
impl Default for VoteAccount {
impl Default for VoteAccountInner {
fn default() -> Self {
Self {
account: Account::default(),
@@ -232,7 +214,7 @@ impl Default for VoteAccount {
}
}
impl PartialEq<VoteAccount> for VoteAccount {
impl PartialEq<VoteAccountInner> for VoteAccountInner {
fn eq(&self, other: &Self) -> bool {
self.account == other.account
}
@@ -291,10 +273,10 @@ impl AsRef<VoteAccountsHashMap> for VoteAccounts {
}
}
impl FromIterator<(Pubkey, (u64 /*stake*/, ArcVoteAccount))> for VoteAccounts {
impl FromIterator<(Pubkey, (/*stake:*/ u64, VoteAccount))> for VoteAccounts {
fn from_iter<I>(iter: I) -> Self
where
I: IntoIterator<Item = (Pubkey, (u64, ArcVoteAccount))>,
I: IntoIterator<Item = (Pubkey, (u64, VoteAccount))>,
{
Self::from(HashMap::from_iter(iter))
}
@@ -360,19 +342,19 @@ mod tests {
fn new_rand_vote_accounts<R: Rng>(
rng: &mut R,
num_nodes: usize,
) -> impl Iterator<Item = (Pubkey, (u64 /*stake*/, ArcVoteAccount))> + '_ {
) -> impl Iterator<Item = (Pubkey, (/*stake:*/ u64, VoteAccount))> + '_ {
let nodes: Vec<_> = repeat_with(Pubkey::new_unique).take(num_nodes).collect();
repeat_with(move || {
let node = nodes[rng.gen_range(0, nodes.len())];
let (account, _) = new_rand_vote_account(rng, Some(node));
let stake = rng.gen_range(0, 997);
(Pubkey::new_unique(), (stake, ArcVoteAccount::from(account)))
(Pubkey::new_unique(), (stake, VoteAccount::from(account)))
})
}
fn staked_nodes<'a, I>(vote_accounts: I) -> HashMap<Pubkey, u64>
where
I: IntoIterator<Item = &'a (Pubkey, (u64, ArcVoteAccount))>,
I: IntoIterator<Item = &'a (Pubkey, (u64, VoteAccount))>,
{
let mut staked_nodes = HashMap::new();
for (_, (stake, vote_account)) in vote_accounts
@@ -394,7 +376,7 @@ mod tests {
let mut rng = rand::thread_rng();
let (account, vote_state) = new_rand_vote_account(&mut rng, None);
let lamports = account.lamports;
let vote_account = ArcVoteAccount::from(account);
let vote_account = VoteAccount::from(account);
assert_eq!(lamports, vote_account.lamports());
assert_eq!(vote_state, *vote_account.vote_state().as_ref().unwrap());
// 2nd call to .vote_state() should return the cached value.
@@ -405,9 +387,9 @@ mod tests {
fn test_vote_account_serialize() {
let mut rng = rand::thread_rng();
let (account, vote_state) = new_rand_vote_account(&mut rng, None);
let vote_account = ArcVoteAccount::from(account.clone());
let vote_account = VoteAccount::from(account.clone());
assert_eq!(vote_state, *vote_account.vote_state().as_ref().unwrap());
// Assert than ArcVoteAccount has the same wire format as Account.
// Assert than VoteAccount has the same wire format as Account.
assert_eq!(
bincode::serialize(&account).unwrap(),
bincode::serialize(&vote_account).unwrap()
@@ -419,9 +401,9 @@ mod tests {
let mut rng = rand::thread_rng();
let (account, vote_state) = new_rand_vote_account(&mut rng, None);
let data = bincode::serialize(&account).unwrap();
let vote_account = ArcVoteAccount::from(account);
let vote_account = VoteAccount::from(account);
assert_eq!(vote_state, *vote_account.vote_state().as_ref().unwrap());
let other_vote_account: ArcVoteAccount = bincode::deserialize(&data).unwrap();
let other_vote_account: VoteAccount = bincode::deserialize(&data).unwrap();
assert_eq!(vote_account, other_vote_account);
assert_eq!(
vote_state,
@@ -433,11 +415,11 @@ mod tests {
fn test_vote_account_round_trip() {
let mut rng = rand::thread_rng();
let (account, vote_state) = new_rand_vote_account(&mut rng, None);
let vote_account = ArcVoteAccount::from(account);
let vote_account = VoteAccount::from(account);
assert_eq!(vote_state, *vote_account.vote_state().as_ref().unwrap());
let data = bincode::serialize(&vote_account).unwrap();
let other_vote_account: ArcVoteAccount = bincode::deserialize(&data).unwrap();
// Assert that serialize->deserialized returns the same ArcVoteAccount.
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,
@@ -448,7 +430,7 @@ mod tests {
#[test]
fn test_vote_accounts_serialize() {
let mut rng = rand::thread_rng();
let vote_accounts_hash_map: HashMap<Pubkey, (u64, ArcVoteAccount)> =
let vote_accounts_hash_map: HashMap<Pubkey, (u64, VoteAccount)> =
new_rand_vote_accounts(&mut rng, 64).take(1024).collect();
let vote_accounts = VoteAccounts::from(vote_accounts_hash_map.clone());
assert!(vote_accounts.staked_nodes().len() > 32);
@@ -467,7 +449,7 @@ mod tests {
#[test]
fn test_vote_accounts_deserialize() {
let mut rng = rand::thread_rng();
let vote_accounts_hash_map: HashMap<Pubkey, (u64, ArcVoteAccount)> =
let vote_accounts_hash_map: HashMap<Pubkey, (u64, VoteAccount)> =
new_rand_vote_accounts(&mut rng, 64).take(1024).collect();
let data = bincode::serialize(&vote_accounts_hash_map).unwrap();
let vote_accounts: VoteAccounts = bincode::deserialize(&data).unwrap();