Revert "add genesis stake placeholders (#6969)" (#7109)

* Revert "add genesis stake placeholders (#6969)"

This reverts commit 8a879faac7.

* fixup! Revert "add genesis stake placeholders (#6969)"

* fixup! fixup! Revert "add genesis stake placeholders (#6969)"

* fixup! fixup! fixup! Revert "add genesis stake placeholders (#6969)"

* fixup! fixup! fixup! fixup! Revert "add genesis stake placeholders (#6969)"

* fmt
This commit is contained in:
anatoly yakovenko
2019-11-23 22:15:21 -08:00
committed by Michael Vines
parent b8cd0a1bc0
commit 702f7cc51d
18 changed files with 105 additions and 1322 deletions

View File

@ -5,8 +5,8 @@ use serde_derive::{Deserialize, Serialize};
use solana_config_program::{create_config_account, get_config_data, ConfigState};
use solana_sdk::{
account::{Account, KeyedAccount},
genesis_config::GenesisConfig,
instruction::InstructionError,
pubkey::Pubkey,
};
// stake config ID
@ -52,15 +52,8 @@ impl ConfigState for Config {
}
}
pub fn add_genesis_account(genesis_config: &mut GenesisConfig) -> u64 {
let mut account = create_config_account(vec![], &Config::default(), 0);
let lamports = genesis_config.rent.minimum_balance(account.data.len());
account.lamports = lamports.max(1);
genesis_config.add_account(id(), account);
lamports
pub fn create_genesis_account() -> (Pubkey, Account) {
(id(), create_config_account(vec![], &Config::default(), 100))
}
pub fn create_account(lamports: u64, config: &Config) -> Account {
@ -77,15 +70,19 @@ pub fn from_keyed_account(account: &KeyedAccount) -> Result<Config, InstructionE
#[cfg(test)]
mod tests {
use super::*;
use solana_sdk::pubkey::Pubkey;
#[test]
fn test() {
let mut account = create_account(0, &Config::default());
let mut account = create_account(1, &Config::default());
assert_eq!(Config::from(&account), Some(Config::default()));
assert_eq!(
from_keyed_account(&KeyedAccount::new(&Pubkey::default(), false, &mut account)),
Err(InstructionError::InvalidArgument)
);
let (pubkey, mut account) = create_genesis_account();
assert_eq!(
from_keyed_account(&KeyedAccount::new(&pubkey, false, &mut account)),
Ok(Config::default())
);
}
}

View File

@ -1,3 +1,6 @@
use crate::config::create_genesis_account;
use crate::rewards_pools::create_rewards_accounts;
use crate::stake_instruction::process_instruction;
use solana_sdk::genesis_config::GenesisConfig;
pub mod config;
@ -8,13 +11,14 @@ pub mod stake_state;
solana_sdk::declare_program!(
"Stake11111111111111111111111111111111111111",
solana_stake_program,
stake_instruction::process_instruction
process_instruction
);
pub fn add_genesis_accounts(genesis_config: &mut GenesisConfig) -> u64 {
for (pubkey, account) in rewards_pools::create_genesis_accounts() {
pub fn add_genesis_accounts(genesis_config: &mut GenesisConfig) {
for (pubkey, account) in create_rewards_accounts() {
genesis_config.add_rewards_pool(pubkey, account);
}
config::add_genesis_account(genesis_config)
let (pubkey, account) = create_genesis_account();
genesis_config.add_account(pubkey, account);
}

View File

@ -26,7 +26,7 @@ pub fn random_id() -> Pubkey {
Pubkey::new(id.as_ref())
}
pub fn create_genesis_accounts() -> Vec<(Pubkey, Account)> {
pub fn create_rewards_accounts() -> Vec<(Pubkey, Account)> {
let mut accounts = Vec::with_capacity(NUM_REWARDS_POOLS);
let mut pubkey = id();
@ -46,7 +46,7 @@ mod tests {
#[test]
fn test() {
let accounts = create_genesis_accounts();
let accounts = create_rewards_accounts();
for _i in 0..NUM_REWARDS_POOLS {
let id = random_id();

View File

@ -365,7 +365,7 @@ mod tests {
} else if sysvar::stake_history::check_id(&meta.pubkey) {
sysvar::stake_history::create_account(1, &StakeHistory::default())
} else if config::check_id(&meta.pubkey) {
config::create_account(0, &config::Config::default())
config::create_account(1, &config::Config::default())
} else if sysvar::rent::check_id(&meta.pubkey) {
sysvar::rent::create_account(1, &Rent::default())
} else {
@ -588,7 +588,7 @@ mod tests {
KeyedAccount::new(
&config::id(),
false,
&mut config::create_account(0, &config::Config::default())
&mut config::create_account(1, &config::Config::default())
),
],
&serialize(&StakeInstruction::DelegateStake).unwrap(),

View File

@ -72,9 +72,9 @@ pub enum StakeAuthorize {
#[derive(Default, Debug, Serialize, Deserialize, PartialEq, Clone, Copy)]
pub struct Lockup {
/// epoch at which this stake will allow withdrawal, unless
/// slot height at which this stake will allow withdrawal, unless
/// to the custodian
pub epoch: Epoch,
pub slot: Slot,
/// custodian account, the only account to which this stake will honor a
/// withdrawal before lockup expires. After lockup expires, custodian
/// is irrelevant
@ -94,6 +94,16 @@ pub struct Meta {
pub lockup: Lockup,
}
impl Meta {
pub fn auto(authorized: &Pubkey) -> Self {
Self {
authorized: Authorized::auto(authorized),
rent_exempt_reserve: Rent::default().minimum_balance(std::mem::size_of::<StakeState>()),
..Meta::default()
}
}
}
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone, Copy)]
pub struct Stake {
/// most recently delegated vote account pubkey
@ -705,7 +715,7 @@ impl<'a> StakeAccount for KeyedAccount<'a> {
// verify that lockup has expired or that the withdrawal is going back
// to the custodian
if lockup.epoch > clock.epoch && lockup.custodian != *to.unsigned_key() {
if lockup.slot > clock.slot && lockup.custodian != *to.unsigned_key() {
return Err(StakeError::LockupInForce.into());
}
@ -754,34 +764,7 @@ where
}
}
pub fn get_stake_rent_exempt_reserve(rent: &Rent) -> u64 {
rent.minimum_balance(std::mem::size_of::<StakeState>())
}
// genesis investor accounts
pub fn create_lockup_stake_account(
authorized: &Authorized,
lockup: &Lockup,
rent: &Rent,
lamports: u64,
) -> Account {
let mut stake_account = Account::new(lamports, std::mem::size_of::<StakeState>(), &id());
let rent_exempt_reserve = rent.minimum_balance(stake_account.data.len());
assert!(lamports >= rent_exempt_reserve);
stake_account
.set_state(&StakeState::Initialized(Meta {
authorized: *authorized,
lockup: *lockup,
rent_exempt_reserve,
}))
.expect("set_state");
stake_account
}
// utility function, used by Bank, tests, genesis for bootstrap
// utility function, used by Bank, tests, genesis
pub fn create_account(
authorized: &Pubkey,
voter_pubkey: &Pubkey,
@ -792,18 +775,19 @@ pub fn create_account(
let mut stake_account = Account::new(lamports, std::mem::size_of::<StakeState>(), &id());
let vote_state = VoteState::from(vote_account).expect("vote_state");
let rent_exempt_reserve = rent.minimum_balance(stake_account.data.len());
let rent_exempt_reserve = rent.minimum_balance(std::mem::size_of::<StakeState>());
stake_account
.set_state(&StakeState::Stake(
Meta {
authorized: Authorized::auto(authorized),
rent_exempt_reserve,
..Meta::default()
authorized: Authorized {
staker: *authorized,
withdrawer: *authorized,
},
lockup: Lockup::default(),
},
Stake::new(
lamports - rent_exempt_reserve, // underflow is an error, is basically: assert!(lamports > rent_exempt_reserve);
lamports - rent_exempt_reserve, // underflow is an error, assert!(lamports> rent_exempt_reserve);
voter_pubkey,
&vote_state,
std::u64::MAX,
@ -822,15 +806,6 @@ mod tests {
use solana_sdk::{account::Account, pubkey::Pubkey, system_program};
use solana_vote_program::vote_state;
impl Meta {
pub fn auto(authorized: &Pubkey) -> Self {
Self {
authorized: Authorized::auto(authorized),
..Meta::default()
}
}
}
#[test]
fn test_stake_state_stake_from_fail() {
let mut stake_account = Account::new(0, std::mem::size_of::<StakeState>(), &id());
@ -1349,10 +1324,7 @@ mod tests {
assert_eq!(
stake_keyed_account.initialize(
&Authorized::auto(&stake_pubkey),
&Lockup {
epoch: 1,
custodian
},
&Lockup { slot: 1, custodian },
&Rent::default(),
),
Ok(())
@ -1361,14 +1333,8 @@ mod tests {
assert_eq!(
StakeState::from(&stake_keyed_account.account).unwrap(),
StakeState::Initialized(Meta {
lockup: Lockup {
epoch: 1,
custodian
},
..Meta {
authorized: Authorized::auto(&stake_pubkey),
..Meta::default()
}
lockup: Lockup { slot: 1, custodian },
..Meta::auto(&stake_pubkey)
})
);
@ -1501,10 +1467,7 @@ mod tests {
stake_keyed_account
.initialize(
&Authorized::auto(&stake_pubkey),
&Lockup {
epoch: 0,
custodian,
},
&Lockup { slot: 0, custodian },
&Rent::default(),
)
.unwrap();
@ -1703,10 +1666,7 @@ mod tests {
let mut stake_account = Account::new_data_with_space(
total_lamports,
&StakeState::Initialized(Meta {
lockup: Lockup {
epoch: 1,
custodian,
},
lockup: Lockup { slot: 1, custodian },
..Meta::auto(&stake_pubkey)
}),
std::mem::size_of::<StakeState>(),
@ -1755,7 +1715,7 @@ mod tests {
// lockup has expired
let mut to_keyed_account = KeyedAccount::new(&to, false, &mut to_account);
clock.epoch += 1;
clock.slot += 1;
assert_eq!(
stake_keyed_account.withdraw(
total_lamports,

View File

@ -15,14 +15,13 @@ solana_sdk::declare_id!("StorageMiningPoo111111111111111111111111111");
// to cut down on collisions for redemptions, we make multiple accounts
pub const NUM_REWARDS_POOLS: usize = 32;
pub fn add_genesis_accounts(genesis_config: &mut GenesisConfig) -> u64 {
pub fn add_genesis_accounts(genesis_config: &mut GenesisConfig) {
let mut pubkey = id();
for _i in 0..NUM_REWARDS_POOLS {
genesis_config.add_rewards_pool(pubkey, create_rewards_pool());
pubkey = Pubkey::new(hash(pubkey.as_ref()).as_ref());
}
0 // didn't consume any lamports
}
pub fn random_id() -> Pubkey {