Refactor stake program into solana_program (#17906)

* Move stake state / instructions into solana_program

* Update account-decoder

* Update cli and runtime

* Update all other parts

* Commit Cargo.lock changes in programs/bpf

* Update cli stake instruction import

* Allow integer arithmetic

* Update ABI digest

* Bump rust mem instruction count

* Remove useless structs

* Move stake::id() -> stake::program::id()

* Re-export from solana_sdk and mark deprecated

* Address feedback

* Run cargo fmt
This commit is contained in:
Jon Cinque
2021-06-15 18:04:00 +02:00
committed by GitHub
parent 36b09db2d1
commit 1b1d34da59
59 changed files with 1765 additions and 1711 deletions

View File

@ -96,6 +96,7 @@ use solana_sdk::{
signature::{Keypair, Signature},
slot_hashes::SlotHashes,
slot_history::SlotHistory,
stake::{self, state::Delegation},
stake_weighted_timestamp::{
calculate_stake_weighted_timestamp, MaxAllowableDrift, MAX_ALLOWABLE_DRIFT_PERCENTAGE,
MAX_ALLOWABLE_DRIFT_PERCENTAGE_FAST, MAX_ALLOWABLE_DRIFT_PERCENTAGE_SLOW,
@ -105,9 +106,7 @@ use solana_sdk::{
timing::years_as_slots,
transaction::{self, Result, Transaction, TransactionError},
};
use solana_stake_program::stake_state::{
self, Delegation, InflationPointCalculationEvent, PointValue,
};
use solana_stake_program::stake_state::{self, InflationPointCalculationEvent, PointValue};
use solana_vote_program::vote_instruction::VoteInstruction;
use std::{
borrow::Cow,
@ -1918,7 +1917,7 @@ impl Bank {
if self
.feature_set
.is_active(&feature_set::filter_stake_delegation_accounts::id())
&& (stake_account.owner() != &solana_stake_program::id()
&& (stake_account.owner() != &stake::program::id()
|| vote_account.owner() != &solana_vote_program::id())
{
datapoint_warn!(
@ -5357,15 +5356,15 @@ pub(crate) mod tests {
process_instruction::InvokeContext,
rent::Rent,
signature::{keypair_from_seed, Keypair, Signer},
stake::{
instruction as stake_instruction,
state::{Authorized, Delegation, Lockup, Stake},
},
system_instruction::{self, SystemError},
system_program,
sysvar::{fees::Fees, rewards::Rewards},
timing::duration_as_s,
};
use solana_stake_program::{
stake_instruction,
stake_state::{self, Authorized, Delegation, Lockup, Stake},
};
use solana_vote_program::{
vote_instruction,
vote_state::{
@ -9558,7 +9557,7 @@ pub(crate) mod tests {
let pubkey = solana_sdk::pubkey::new_rand();
genesis_config.add_account(
pubkey,
solana_stake_program::stake_state::create_lockup_stake_account(
stake_state::create_lockup_stake_account(
&Authorized::auto(&pubkey),
&Lockup::default(),
&Rent::default(),

View File

@ -7,7 +7,7 @@ use solana_sdk::{
instruction::InstructionError,
process_instruction::{stable_log, InvokeContext, ProcessInstructionWithContext},
pubkey::Pubkey,
system_program,
stake, system_program,
};
fn process_instruction_with_program_logging(
@ -56,7 +56,7 @@ fn genesis_builtins() -> Vec<Builtin> {
),
Builtin::new(
"stake_program",
solana_stake_program::id(),
stake::program::id(),
with_program_logging!(solana_stake_program::stake_instruction::process_instruction),
),
Builtin::new(

View File

@ -8,10 +8,10 @@ use solana_sdk::{
pubkey::Pubkey,
rent::Rent,
signature::{Keypair, Signer},
stake::state::StakeState,
system_program,
};
use solana_stake_program::stake_state;
use solana_stake_program::stake_state::StakeState;
use solana_vote_program::vote_state;
use std::borrow::Borrow;

View File

@ -4,8 +4,12 @@ use {
bank::Bank,
},
log::*,
solana_sdk::{account::ReadableAccount, pubkey::Pubkey},
solana_stake_program::stake_state::StakeState,
solana_sdk::{
account::ReadableAccount,
pubkey::Pubkey,
stake::{self, state::StakeState},
},
solana_stake_program::stake_state,
std::{collections::HashSet, sync::Arc},
};
@ -32,19 +36,19 @@ pub fn calculate_non_circulating_supply(bank: &Arc<Bank>) -> ScanResult<NonCircu
.contains(&AccountIndex::ProgramId)
{
bank.get_filtered_indexed_accounts(
&IndexKey::ProgramId(solana_stake_program::id()),
&IndexKey::ProgramId(stake::program::id()),
// The program-id account index checks for Account owner on inclusion. However, due to
// the current AccountsDb implementation, an account may remain in storage as a
// zero-lamport Account::Default() after being wiped and reinitialized in later
// updates. We include the redundant filter here to avoid returning these accounts.
|account| account.owner() == &solana_stake_program::id(),
|account| account.owner() == &stake::program::id(),
)?
} else {
bank.get_program_accounts(&solana_stake_program::id())?
bank.get_program_accounts(&stake::program::id())?
};
for (pubkey, account) in stake_accounts.iter() {
let stake_account = StakeState::from(account).unwrap_or_default();
let stake_account = stake_state::from(account).unwrap_or_default();
match stake_account {
StakeState::Initialized(meta) => {
if meta.lockup.is_in_force(&clock, None)
@ -196,8 +200,8 @@ mod tests {
account::AccountSharedData,
epoch_schedule::EpochSchedule,
genesis_config::{ClusterType, GenesisConfig},
stake::state::{Authorized, Lockup, Meta},
};
use solana_stake_program::stake_state::{Authorized, Lockup, Meta, StakeState};
use std::{collections::BTreeMap, sync::Arc};
fn new_from_parent(parent: &Arc<Bank>) -> Bank {
@ -236,7 +240,7 @@ mod tests {
balance,
&StakeState::Initialized(meta),
std::mem::size_of::<StakeState>(),
&solana_stake_program::id(),
&stake::program::id(),
)
.unwrap();
accounts.insert(pubkey, stake_account);

View File

@ -293,7 +293,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 = "DuRGntVwLGNAv5KooafUSpxk67BPAx2yC7Z8A9c8wr2G")]
#[frozen_abi(digest = "6msodEzE7YzFtorBhiP6ax4PKBaPZTkmYdGAdpoxLCvV")]
#[derive(Serialize, AbiExample)]
pub struct BankAbiTestWrapperFuture {
#[serde(serialize_with = "wrapper_future")]

View File

@ -5,9 +5,13 @@ use solana_sdk::{
account::{AccountSharedData, ReadableAccount},
clock::Epoch,
pubkey::Pubkey,
stake::{
self,
state::{Delegation, StakeState},
},
sysvar::stake_history::StakeHistory,
};
use solana_stake_program::stake_state::{new_stake_history_entry, Delegation, StakeState};
use solana_stake_program::stake_state;
use solana_vote_program::vote_state::VoteState;
use std::{borrow::Borrow, collections::HashMap};
@ -42,7 +46,7 @@ impl Stakes {
let mut stake_history_upto_prev_epoch = self.stake_history.clone();
stake_history_upto_prev_epoch.add(
prev_epoch,
new_stake_history_entry(
stake_state::new_stake_history_entry(
prev_epoch,
self.stake_delegations
.iter()
@ -111,7 +115,7 @@ impl Stakes {
pub fn is_stake(account: &AccountSharedData) -> bool {
solana_vote_program::check_id(account.owner())
|| solana_stake_program::check_id(account.owner())
|| stake::program::check_id(account.owner())
&& account.data().len() >= std::mem::size_of::<StakeState>()
}
@ -148,7 +152,7 @@ impl Stakes {
.insert(*pubkey, (stake, ArcVoteAccount::from(account.clone())));
}
old.map(|(_, account)| account)
} else if solana_stake_program::check_id(account.owner()) {
} else if stake::program::check_id(account.owner()) {
// old_stake is stake lamports and voter_pubkey from the pre-store() version
let old_stake = self.stake_delegations.get(pubkey).map(|delegation| {
(
@ -157,7 +161,7 @@ impl Stakes {
)
});
let delegation = StakeState::delegation_from(account);
let delegation = stake_state::delegation_from(account);
let stake = delegation.map(|delegation| {
(
@ -308,7 +312,7 @@ pub mod tests {
stakes.store(&vote_pubkey, &vote_account, true, true);
stakes.store(&stake_pubkey, &stake_account, true, true);
let stake = StakeState::stake_from(&stake_account).unwrap();
let stake = stake_state::stake_from(&stake_account).unwrap();
{
let vote_accounts = stakes.vote_accounts();
assert!(vote_accounts.get(&vote_pubkey).is_some());
@ -332,7 +336,7 @@ pub mod tests {
// activate more
let (_stake_pubkey, mut stake_account) = create_stake_account(42, &vote_pubkey);
stakes.store(&stake_pubkey, &stake_account, true, true);
let stake = StakeState::stake_from(&stake_account).unwrap();
let stake = stake_state::stake_from(&stake_account).unwrap();
{
let vote_accounts = stakes.vote_accounts();
assert!(vote_accounts.get(&vote_pubkey).is_some());
@ -463,7 +467,7 @@ pub mod tests {
// delegates to vote_pubkey
stakes.store(&stake_pubkey, &stake_account, true, true);
let stake = StakeState::stake_from(&stake_account).unwrap();
let stake = stake_state::stake_from(&stake_account).unwrap();
{
let vote_accounts = stakes.vote_accounts();
@ -523,7 +527,7 @@ pub mod tests {
stakes.store(&vote_pubkey, &vote_account, true, true);
stakes.store(&stake_pubkey, &stake_account, true, true);
let stake = StakeState::stake_from(&stake_account).unwrap();
let stake = stake_state::stake_from(&stake_account).unwrap();
{
let vote_accounts = stakes.vote_accounts();
@ -564,7 +568,7 @@ pub mod tests {
// not a stake account, and whacks above entry
stakes.store(
&stake_pubkey,
&AccountSharedData::new(1, 0, &solana_stake_program::id()),
&AccountSharedData::new(1, 0, &stake::program::id()),
true,
true,
);

View File

@ -11,12 +11,13 @@ use solana_sdk::{
message::Message,
pubkey::Pubkey,
signature::{Keypair, Signer},
stake::{
self, instruction as stake_instruction,
state::{Authorized, Lockup, StakeState},
},
sysvar::{self, stake_history::StakeHistory},
};
use solana_stake_program::{
stake_instruction::{self},
stake_state::{self, StakeState},
};
use solana_stake_program::stake_state;
use solana_vote_program::{
vote_instruction,
vote_state::{Vote, VoteInit, VoteState, VoteStateVersions},
@ -69,7 +70,7 @@ fn fill_epoch_with_votes(
}
fn warmed_up(bank: &Bank, stake_pubkey: &Pubkey) -> bool {
let stake = StakeState::stake_from(&bank.get_account(stake_pubkey).unwrap()).unwrap();
let stake = stake_state::stake_from(&bank.get_account(stake_pubkey).unwrap()).unwrap();
stake.delegation.stake
== stake.stake(
@ -85,7 +86,7 @@ fn warmed_up(bank: &Bank, stake_pubkey: &Pubkey) -> bool {
}
fn get_staked(bank: &Bank, stake_pubkey: &Pubkey) -> u64 {
StakeState::stake_from(&bank.get_account(stake_pubkey).unwrap())
stake_state::stake_from(&bank.get_account(stake_pubkey).unwrap())
.unwrap()
.stake(
bank.epoch(),
@ -118,9 +119,9 @@ fn test_stake_create_and_split_single_signature() {
let bank_client = BankClient::new_shared(&Arc::new(Bank::new(&genesis_config)));
let stake_address =
Pubkey::create_with_seed(&staker_pubkey, "stake", &solana_stake_program::id()).unwrap();
Pubkey::create_with_seed(&staker_pubkey, "stake", &stake::program::id()).unwrap();
let authorized = stake_state::Authorized::auto(&staker_pubkey);
let authorized = Authorized::auto(&staker_pubkey);
let lamports = 1_000_000;
@ -132,7 +133,7 @@ fn test_stake_create_and_split_single_signature() {
&staker_pubkey, // base
"stake", // seed
&authorized,
&stake_state::Lockup::default(),
&Lockup::default(),
lamports,
),
Some(&staker_pubkey),
@ -145,8 +146,7 @@ fn test_stake_create_and_split_single_signature() {
// split the stake
let split_stake_address =
Pubkey::create_with_seed(&staker_pubkey, "split_stake", &solana_stake_program::id())
.unwrap();
Pubkey::create_with_seed(&staker_pubkey, "split_stake", &stake::program::id()).unwrap();
// Test split
let message = Message::new(
&stake_instruction::split_with_seed(
@ -189,9 +189,9 @@ fn test_stake_create_and_split_to_existing_system_account() {
let bank_client = BankClient::new_shared(&Arc::new(Bank::new(&genesis_config)));
let stake_address =
Pubkey::create_with_seed(&staker_pubkey, "stake", &solana_stake_program::id()).unwrap();
Pubkey::create_with_seed(&staker_pubkey, "stake", &stake::program::id()).unwrap();
let authorized = stake_state::Authorized::auto(&staker_pubkey);
let authorized = Authorized::auto(&staker_pubkey);
let lamports = 1_000_000;
@ -203,7 +203,7 @@ fn test_stake_create_and_split_to_existing_system_account() {
&staker_pubkey, // base
"stake", // seed
&authorized,
&stake_state::Lockup::default(),
&Lockup::default(),
lamports,
),
Some(&staker_pubkey),
@ -214,8 +214,7 @@ fn test_stake_create_and_split_to_existing_system_account() {
.expect("failed to create and delegate stake account");
let split_stake_address =
Pubkey::create_with_seed(&staker_pubkey, "split_stake", &solana_stake_program::id())
.unwrap();
Pubkey::create_with_seed(&staker_pubkey, "split_stake", &stake::program::id()).unwrap();
// First, put a system account where we want the new stake account
let existing_lamports = 42;
@ -290,7 +289,7 @@ fn test_stake_account_lifetime() {
.send_and_confirm_message(&[&mint_keypair, &vote_keypair, &identity_keypair], message)
.expect("failed to create vote account");
let authorized = stake_state::Authorized::auto(&stake_pubkey);
let authorized = Authorized::auto(&stake_pubkey);
// Create stake account and delegate to vote account
let message = Message::new(
&stake_instruction::create_account_and_delegate_stake(
@ -298,7 +297,7 @@ fn test_stake_account_lifetime() {
&stake_pubkey,
&vote_pubkey,
&authorized,
&stake_state::Lockup::default(),
&Lockup::default(),
1_000_000,
),
Some(&mint_pubkey),
@ -516,8 +515,7 @@ fn test_create_stake_account_from_seed() {
let bank_client = BankClient::new_shared(&bank);
let seed = "test-string";
let stake_pubkey =
Pubkey::create_with_seed(&mint_pubkey, seed, &solana_stake_program::id()).unwrap();
let stake_pubkey = Pubkey::create_with_seed(&mint_pubkey, seed, &stake::program::id()).unwrap();
// Create Vote Account
let message = Message::new(
@ -538,7 +536,7 @@ fn test_create_stake_account_from_seed() {
.send_and_confirm_message(&[&mint_keypair, &vote_keypair, &identity_keypair], message)
.expect("failed to create vote account");
let authorized = stake_state::Authorized::auto(&mint_pubkey);
let authorized = Authorized::auto(&mint_pubkey);
// Create stake account and delegate to vote account
let message = Message::new(
&stake_instruction::create_account_with_seed_and_delegate_stake(
@ -548,7 +546,7 @@ fn test_create_stake_account_from_seed() {
seed,
&vote_pubkey,
&authorized,
&stake_state::Lockup::default(),
&Lockup::default(),
1_000_000,
),
Some(&mint_pubkey),