Skip to content

Commit

Permalink
Move vote program state and instructions to solana-program
Browse files Browse the repository at this point in the history
  • Loading branch information
mvines committed Aug 10, 2022
1 parent b9a5af0 commit ccfbc54
Show file tree
Hide file tree
Showing 28 changed files with 2,140 additions and 2,087 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 16 additions & 16 deletions core/src/commitment_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ mod tests {
solana_sdk::{account::Account, pubkey::Pubkey, signature::Signer},
solana_stake_program::stake_state,
solana_vote_program::{
vote_state::{self, VoteStateVersions},
vote_state::{self, process_slot_vote_unchecked, VoteStateVersions},
vote_transaction,
},
};
Expand Down Expand Up @@ -309,7 +309,7 @@ mod tests {

let root = ancestors[2];
vote_state.root_slot = Some(root);
vote_state.process_slot_vote_unchecked(*ancestors.last().unwrap());
process_slot_vote_unchecked(&mut vote_state, *ancestors.last().unwrap());
AggregateCommitmentService::aggregate_commitment_for_vote_account(
&mut commitment,
&mut rooted_stake,
Expand Down Expand Up @@ -341,8 +341,8 @@ mod tests {
let root = ancestors[2];
vote_state.root_slot = Some(root);
assert!(ancestors[4] + 2 >= ancestors[6]);
vote_state.process_slot_vote_unchecked(ancestors[4]);
vote_state.process_slot_vote_unchecked(ancestors[6]);
process_slot_vote_unchecked(&mut vote_state, ancestors[4]);
process_slot_vote_unchecked(&mut vote_state, ancestors[6]);
AggregateCommitmentService::aggregate_commitment_for_vote_account(
&mut commitment,
&mut rooted_stake,
Expand Down Expand Up @@ -431,30 +431,30 @@ mod tests {
// Create bank
let bank = Arc::new(Bank::new_for_tests(&genesis_config));

let mut vote_state1 = VoteState::from(&vote_account1).unwrap();
vote_state1.process_slot_vote_unchecked(3);
vote_state1.process_slot_vote_unchecked(5);
let mut vote_state1 = vote_state::from(&vote_account1).unwrap();
process_slot_vote_unchecked(&mut vote_state1, 3);
process_slot_vote_unchecked(&mut vote_state1, 5);
let versioned = VoteStateVersions::new_current(vote_state1);
VoteState::to(&versioned, &mut vote_account1).unwrap();
vote_state::to(&versioned, &mut vote_account1).unwrap();
bank.store_account(&pk1, &vote_account1);

let mut vote_state2 = VoteState::from(&vote_account2).unwrap();
vote_state2.process_slot_vote_unchecked(9);
vote_state2.process_slot_vote_unchecked(10);
let mut vote_state2 = vote_state::from(&vote_account2).unwrap();
process_slot_vote_unchecked(&mut vote_state2, 9);
process_slot_vote_unchecked(&mut vote_state2, 10);
let versioned = VoteStateVersions::new_current(vote_state2);
VoteState::to(&versioned, &mut vote_account2).unwrap();
vote_state::to(&versioned, &mut vote_account2).unwrap();
bank.store_account(&pk2, &vote_account2);

let mut vote_state3 = VoteState::from(&vote_account3).unwrap();
let mut vote_state3 = vote_state::from(&vote_account3).unwrap();
vote_state3.root_slot = Some(1);
let versioned = VoteStateVersions::new_current(vote_state3);
VoteState::to(&versioned, &mut vote_account3).unwrap();
vote_state::to(&versioned, &mut vote_account3).unwrap();
bank.store_account(&pk3, &vote_account3);

let mut vote_state4 = VoteState::from(&vote_account4).unwrap();
let mut vote_state4 = vote_state::from(&vote_account4).unwrap();
vote_state4.root_slot = Some(2);
let versioned = VoteStateVersions::new_current(vote_state4);
VoteState::to(&versioned, &mut vote_account4).unwrap();
vote_state::to(&versioned, &mut vote_account4).unwrap();
bank.store_account(&pk4, &vote_account4);

let (commitment, rooted_stake) =
Expand Down
24 changes: 12 additions & 12 deletions core/src/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ use {
solana_vote_program::{
vote_instruction,
vote_state::{
BlockTimestamp, Lockout, Vote, VoteState, VoteStateUpdate, VoteTransaction,
MAX_LOCKOUT_HISTORY,
process_slot_vote_unchecked, process_vote_unchecked, BlockTimestamp, Lockout, Vote,
VoteState, VoteStateUpdate, VoteTransaction, MAX_LOCKOUT_HISTORY,
},
},
std::{
Expand Down Expand Up @@ -169,7 +169,7 @@ impl TowerVersions {
}
}

#[frozen_abi(digest = "8Y9r3XAwXwmrVGMCyTuy4Kbdotnt1V6N8J6NEniBFD9x")]
#[frozen_abi(digest = "GrkFcKqGEkJNUYoK1M8rorehi2yyLF4N3Gsj6j8f47Jn")]
#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, AbiExample)]
pub struct Tower {
pub node_pubkey: Pubkey,
Expand Down Expand Up @@ -337,7 +337,7 @@ impl Tower {
);
}

vote_state.process_slot_vote_unchecked(bank_slot);
process_slot_vote_unchecked(&mut vote_state, bank_slot);

for vote in &vote_state.votes {
bank_weight += vote.lockout() as u128 * voted_stake as u128;
Expand Down Expand Up @@ -438,7 +438,7 @@ impl Tower {
last_voted_slot_in_bank: Option<Slot>,
) -> VoteTransaction {
let vote = Vote::new(vec![slot], hash);
local_vote_state.process_vote_unchecked(vote);
process_vote_unchecked(local_vote_state, vote);
let slots = if let Some(last_voted_slot) = last_voted_slot_in_bank {
local_vote_state
.votes
Expand Down Expand Up @@ -483,7 +483,7 @@ impl Tower {

let mut new_vote = if is_direct_vote_state_update_enabled {
let vote = Vote::new(vec![vote_slot], vote_hash);
self.vote_state.process_vote_unchecked(vote);
process_vote_unchecked(&mut self.vote_state, vote);
VoteTransaction::from(VoteStateUpdate::new(
self.vote_state.votes.clone(),
self.vote_state.root_slot,
Expand Down Expand Up @@ -608,7 +608,7 @@ impl Tower {
// remaining voted slots are on a different fork from the checked slot,
// it's still locked out.
let mut vote_state = self.vote_state.clone();
vote_state.process_slot_vote_unchecked(slot);
process_slot_vote_unchecked(&mut vote_state, slot);
for vote in &vote_state.votes {
if slot != vote.slot && !ancestors.contains(&vote.slot) {
return true;
Expand Down Expand Up @@ -980,7 +980,7 @@ impl Tower {
total_stake: Stake,
) -> bool {
let mut vote_state = self.vote_state.clone();
vote_state.process_slot_vote_unchecked(slot);
process_slot_vote_unchecked(&mut vote_state, slot);
let vote = vote_state.nth_recent_vote(self.threshold_depth);
if let Some(vote) = vote {
if let Some(fork_stake) = voted_stakes.get(&vote.slot) {
Expand Down Expand Up @@ -1432,7 +1432,7 @@ pub mod test {
signature::Signer,
slot_history::SlotHistory,
},
solana_vote_program::vote_state::{Vote, VoteStateVersions, MAX_LOCKOUT_HISTORY},
solana_vote_program::vote_state::{self, Vote, VoteStateVersions, MAX_LOCKOUT_HISTORY},
std::{
collections::{HashMap, VecDeque},
fs::{remove_file, OpenOptions},
Expand All @@ -1456,7 +1456,7 @@ pub mod test {
});
let mut vote_state = VoteState::default();
for slot in *votes {
vote_state.process_slot_vote_unchecked(*slot);
process_slot_vote_unchecked(&mut vote_state, *slot);
}
VoteState::serialize(
&VoteStateVersions::new_current(vote_state),
Expand Down Expand Up @@ -2409,7 +2409,7 @@ pub mod test {
hash: Hash::default(),
timestamp: None,
};
local.process_vote_unchecked(vote);
vote_state::process_vote_unchecked(&mut local, vote);
assert_eq!(local.votes.len(), 1);
let vote =
Tower::apply_vote_and_generate_vote_diff(&mut local, 1, Hash::default(), Some(0));
Expand All @@ -2425,7 +2425,7 @@ pub mod test {
hash: Hash::default(),
timestamp: None,
};
local.process_vote_unchecked(vote);
vote_state::process_vote_unchecked(&mut local, vote);
assert_eq!(local.votes.len(), 1);

// First vote expired, so should be evicted from tower. Thus even with
Expand Down
8 changes: 4 additions & 4 deletions core/src/replay_stage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3529,7 +3529,7 @@ pub(crate) mod tests {
solana_streamer::socket::SocketAddrSpace,
solana_transaction_status::VersionedTransactionWithStatusMeta,
solana_vote_program::{
vote_state::{VoteState, VoteStateVersions},
vote_state::{self, VoteStateVersions},
vote_transaction,
},
std::{
Expand Down Expand Up @@ -4220,10 +4220,10 @@ pub(crate) mod tests {
fn test_replay_commitment_cache() {
fn leader_vote(vote_slot: Slot, bank: &Arc<Bank>, pubkey: &Pubkey) {
let mut leader_vote_account = bank.get_account(pubkey).unwrap();
let mut vote_state = VoteState::from(&leader_vote_account).unwrap();
vote_state.process_slot_vote_unchecked(vote_slot);
let mut vote_state = vote_state::from(&leader_vote_account).unwrap();
vote_state::process_slot_vote_unchecked(&mut vote_state, vote_slot);
let versioned = VoteStateVersions::new_current(vote_state);
VoteState::to(&versioned, &mut leader_vote_account).unwrap();
vote_state::to(&versioned, &mut leader_vote_account).unwrap();
bank.store_account(pubkey, &leader_vote_account);
}

Expand Down
2 changes: 1 addition & 1 deletion core/src/tower1_7_14.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use {
solana_vote_program::vote_state::{BlockTimestamp, Vote, VoteState},
};

#[frozen_abi(digest = "7phMrqmBo2D3rXPdhBj8CpjRvvmx9qgpcU4cDGkL3W9q")]
#[frozen_abi(digest = "8EBpwHf9gys2irNgyRCEe6A5KSh4RK875Fa46yA2NSoN")]
#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, AbiExample)]
pub struct Tower1_7_14 {
pub(crate) node_pubkey: Pubkey,
Expand Down
4 changes: 2 additions & 2 deletions core/src/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ use {
},
solana_send_transaction_service::send_transaction_service,
solana_streamer::{socket::SocketAddrSpace, streamer::StakedNodes},
solana_vote_program::vote_state::VoteState,
solana_vote_program::vote_state,
std::{
collections::{HashMap, HashSet},
net::SocketAddr,
Expand Down Expand Up @@ -1206,7 +1206,7 @@ impl Validator {

fn active_vote_account_exists_in_bank(bank: &Arc<Bank>, vote_account: &Pubkey) -> bool {
if let Some(account) = &bank.get_account(vote_account) {
if let Some(vote_state) = VoteState::from(account) {
if let Some(vote_state) = vote_state::from(account) {
return !vote_state.votes.is_empty();
}
}
Expand Down
4 changes: 2 additions & 2 deletions local-cluster/src/local_cluster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ use {
solana_streamer::socket::SocketAddrSpace,
solana_vote_program::{
vote_instruction,
vote_state::{VoteInit, VoteState},
vote_state::{self, VoteInit},
},
std::{
collections::HashMap,
Expand Down Expand Up @@ -706,7 +706,7 @@ impl LocalCluster {
(Ok(Some(stake_account)), Ok(Some(vote_account))) => {
match (
stake_state::stake_from(&stake_account),
VoteState::from(&vote_account),
vote_state::from(&vote_account),
) {
(Some(stake_state), Some(vote_state)) => {
if stake_state.delegation.voter_pubkey != vote_account_pubkey
Expand Down
6 changes: 3 additions & 3 deletions program-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ use {
signature::{Keypair, Signer},
sysvar::{Sysvar, SysvarId},
},
solana_vote_program::vote_state::{VoteState, VoteStateVersions},
solana_vote_program::vote_state::{self, VoteState, VoteStateVersions},
std::{
cell::RefCell,
collections::{HashMap, HashSet},
Expand Down Expand Up @@ -1054,14 +1054,14 @@ impl ProgramTestContext {

// generate some vote activity for rewards
let mut vote_account = bank.get_account(vote_account_address).unwrap();
let mut vote_state = VoteState::from(&vote_account).unwrap();
let mut vote_state = vote_state::from(&vote_account).unwrap();

let epoch = bank.epoch();
for _ in 0..number_of_credits {
vote_state.increment_credits(epoch, 1);
}
let versioned = VoteStateVersions::new_current(vote_state);
VoteState::to(&versioned, &mut vote_account).unwrap();
vote_state::to(&versioned, &mut vote_account).unwrap();
bank.store_account(vote_account_address, &vote_account);
}

Expand Down
1 change: 1 addition & 0 deletions programs/bpf/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion programs/stake/src/stake_instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2267,7 +2267,7 @@ mod tests {
fn test_stake_delegate(feature_set: FeatureSet) {
let mut vote_state = VoteState::default();
for i in 0..1000 {
vote_state.process_slot_vote_unchecked(i);
vote_state::process_slot_vote_unchecked(&mut vote_state, i);
}
let vote_state_credits = vote_state.credits();
let vote_address = solana_sdk::pubkey::new_rand();
Expand Down
4 changes: 2 additions & 2 deletions programs/stake/src/stake_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use {
stake_history::{StakeHistory, StakeHistoryEntry},
transaction_context::{BorrowedAccount, InstructionContext, TransactionContext},
},
solana_vote_program::vote_state::{VoteState, VoteStateVersions},
solana_vote_program::vote_state::{self, VoteState, VoteStateVersions},
std::{collections::HashSet, convert::TryFrom},
};

Expand Down Expand Up @@ -1750,7 +1750,7 @@ fn do_create_account(
) -> AccountSharedData {
let mut stake_account = AccountSharedData::new(lamports, StakeState::size_of(), &id());

let vote_state = VoteState::from(vote_account).expect("vote_state");
let vote_state = vote_state::from(vote_account).expect("vote_state");

let rent_exempt_reserve = rent.minimum_balance(stake_account.data().len());

Expand Down
1 change: 1 addition & 0 deletions programs/vote/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ serde_derive = "1.0.103"
solana-frozen-abi = { path = "../../frozen-abi", version = "=1.12.0" }
solana-frozen-abi-macro = { path = "../../frozen-abi/macro", version = "=1.12.0" }
solana-metrics = { path = "../../metrics", version = "=1.12.0" }
solana-program = { path = "../../sdk/program", version = "=1.12.0" }
solana-program-runtime = { path = "../../program-runtime", version = "=1.12.0" }
solana-sdk = { path = "../../sdk", version = "=1.12.0" }
thiserror = "1.0"
Expand Down
8 changes: 4 additions & 4 deletions programs/vote/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
#![cfg_attr(RUSTC_WITH_SPECIALIZATION, feature(min_specialization))]
#![allow(clippy::integer_arithmetic)]

pub mod authorized_voters;
pub mod vote_error;
pub mod vote_instruction;
pub mod vote_processor;
pub mod vote_state;
pub mod vote_transaction;
Expand All @@ -14,4 +11,7 @@ extern crate solana_metrics;
#[macro_use]
extern crate solana_frozen_abi_macro;

pub use solana_sdk::vote::program::{check_id, id};
pub use solana_sdk::vote::{
authorized_voters, error as vote_error, instruction as vote_instruction,
program::{check_id, id},
};
19 changes: 10 additions & 9 deletions programs/vote/src/vote_processor.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
//! Vote program processor
use {
crate::{
id,
vote_instruction::VoteInstruction,
vote_state::{self, VoteAuthorize, VoteStateUpdate},
},
crate::vote_state,
log::*,
solana_program::vote::{
instruction::VoteInstruction,
program::id,
state::{VoteAuthorize, VoteStateUpdate},
},
solana_program_runtime::{
invoke_context::InvokeContext, sysvar_cache::get_sysvar_with_account_check,
},
Expand Down Expand Up @@ -143,7 +144,7 @@ pub fn process_instruction(
get_sysvar_with_account_check::slot_hashes(invoke_context, instruction_context, 1)?;
let clock =
get_sysvar_with_account_check::clock(invoke_context, instruction_context, 2)?;
vote_state::process_vote(
vote_state::process_vote_with_account(
&mut me,
&slot_hashes,
&clock,
Expand Down Expand Up @@ -264,7 +265,7 @@ mod tests {
vote_switch, withdraw, VoteInstruction,
},
vote_state::{
Lockout, Vote, VoteAuthorize, VoteAuthorizeCheckedWithSeedArgs,
self, Lockout, Vote, VoteAuthorize, VoteAuthorizeCheckedWithSeedArgs,
VoteAuthorizeWithSeedArgs, VoteInit, VoteState, VoteStateUpdate, VoteStateVersions,
},
},
Expand Down Expand Up @@ -462,7 +463,7 @@ mod tests {
let (vote_pubkey, vote_account) = create_test_account();
let vote_account_space = vote_account.data().len();

let mut vote_state = VoteState::from(&vote_account).unwrap();
let mut vote_state = vote_state::from(&vote_account).unwrap();
vote_state.authorized_withdrawer = vote_pubkey;
vote_state.epoch_credits = Vec::new();

Expand All @@ -482,7 +483,7 @@ mod tests {
let mut vote_account_with_epoch_credits =
AccountSharedData::new(lamports, vote_account_space, &id());
let versioned = VoteStateVersions::new_current(vote_state);
VoteState::to(&versioned, &mut vote_account_with_epoch_credits);
vote_state::to(&versioned, &mut vote_account_with_epoch_credits);

(vote_pubkey, vote_account_with_epoch_credits)
}
Expand Down
Loading

0 comments on commit ccfbc54

Please sign in to comment.