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

This reverts commit 702f7cc51d.
This commit is contained in:
Rob Walker
2019-11-25 15:11:55 -08:00
committed by GitHub
parent acbe89a159
commit ef64f00cbb
18 changed files with 1322 additions and 105 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
@ -48,8 +48,15 @@ impl ConfigState for Config {
}
}
pub fn create_genesis_account() -> (Pubkey, Account) {
(id(), create_config_account(vec![], &Config::default(), 100))
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_account(lamports: u64, config: &Config) -> Account {
@ -66,19 +73,15 @@ 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(1, &Config::default());
let mut account = create_account(0, &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,6 +1,3 @@
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;
@ -11,14 +8,13 @@ pub mod stake_state;
solana_sdk::declare_program!(
"Stake11111111111111111111111111111111111111",
solana_stake_program,
process_instruction
stake_instruction::process_instruction
);
pub fn add_genesis_accounts(genesis_config: &mut GenesisConfig) {
for (pubkey, account) in create_rewards_accounts() {
pub fn add_genesis_accounts(genesis_config: &mut GenesisConfig) -> u64 {
for (pubkey, account) in rewards_pools::create_genesis_accounts() {
genesis_config.add_rewards_pool(pubkey, account);
}
let (pubkey, account) = create_genesis_account();
genesis_config.add_account(pubkey, account);
config::add_genesis_account(genesis_config)
}

View File

@ -26,7 +26,7 @@ pub fn random_id() -> Pubkey {
Pubkey::new(id.as_ref())
}
pub fn create_rewards_accounts() -> Vec<(Pubkey, Account)> {
pub fn create_genesis_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_rewards_accounts();
let accounts = create_genesis_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(1, &config::Config::default())
config::create_account(0, &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(1, &config::Config::default())
&mut config::create_account(0, &config::Config::default())
),
],
&serialize(&StakeInstruction::DelegateStake).unwrap(),

View File

@ -82,9 +82,9 @@ pub enum StakeAuthorize {
#[derive(Default, Debug, Serialize, Deserialize, PartialEq, Clone, Copy)]
pub struct Lockup {
/// slot height at which this stake will allow withdrawal, unless
/// epoch at which this stake will allow withdrawal, unless
/// to the custodian
pub slot: Slot,
pub epoch: Epoch,
/// custodian account, the only account to which this stake will honor a
/// withdrawal before lockup expires. After lockup expires, custodian
/// is irrelevant
@ -104,16 +104,6 @@ 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 Delegation {
/// to whom the stake is delegated
@ -762,7 +752,7 @@ impl<'a> StakeAccount for KeyedAccount<'a> {
// verify that lockup has expired or that the withdrawal is going back
// to the custodian
if lockup.slot > clock.slot && lockup.custodian != *to.unsigned_key() {
if lockup.epoch > clock.epoch && lockup.custodian != *to.unsigned_key() {
return Err(StakeError::LockupInForce.into());
}
@ -811,7 +801,34 @@ where
}
}
// utility function, used by Bank, tests, genesis
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
pub fn create_account(
authorized: &Pubkey,
voter_pubkey: &Pubkey,
@ -822,19 +839,18 @@ 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(std::mem::size_of::<StakeState>());
let rent_exempt_reserve = rent.minimum_balance(stake_account.data.len());
stake_account
.set_state(&StakeState::Stake(
Meta {
authorized: Authorized::auto(authorized),
rent_exempt_reserve,
authorized: Authorized {
staker: *authorized,
withdrawer: *authorized,
},
lockup: Lockup::default(),
..Meta::default()
},
Stake::new(
lamports - rent_exempt_reserve, // underflow is an error, assert!(lamports> rent_exempt_reserve);
lamports - rent_exempt_reserve, // underflow is an error, is basically: assert!(lamports > rent_exempt_reserve);
voter_pubkey,
&vote_state,
std::u64::MAX,
@ -853,6 +869,15 @@ 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());
@ -1378,7 +1403,10 @@ mod tests {
assert_eq!(
stake_keyed_account.initialize(
&Authorized::auto(&stake_pubkey),
&Lockup { slot: 1, custodian },
&Lockup {
epoch: 1,
custodian
},
&Rent::default(),
),
Ok(())
@ -1387,8 +1415,14 @@ mod tests {
assert_eq!(
StakeState::from(&stake_keyed_account.account).unwrap(),
StakeState::Initialized(Meta {
lockup: Lockup { slot: 1, custodian },
..Meta::auto(&stake_pubkey)
lockup: Lockup {
epoch: 1,
custodian
},
..Meta {
authorized: Authorized::auto(&stake_pubkey),
..Meta::default()
}
})
);
@ -1521,7 +1555,10 @@ mod tests {
stake_keyed_account
.initialize(
&Authorized::auto(&stake_pubkey),
&Lockup { slot: 0, custodian },
&Lockup {
epoch: 0,
custodian,
},
&Rent::default(),
)
.unwrap();
@ -1722,7 +1759,10 @@ mod tests {
let mut stake_account = Account::new_data_with_space(
total_lamports,
&StakeState::Initialized(Meta {
lockup: Lockup { slot: 1, custodian },
lockup: Lockup {
epoch: 1,
custodian,
},
..Meta::auto(&stake_pubkey)
}),
std::mem::size_of::<StakeState>(),
@ -1771,7 +1811,7 @@ mod tests {
// lockup has expired
let mut to_keyed_account = KeyedAccount::new(&to, false, &mut to_account);
clock.slot += 1;
clock.epoch += 1;
assert_eq!(
stake_keyed_account.withdraw(
total_lamports,

View File

@ -15,13 +15,14 @@ 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) {
pub fn add_genesis_accounts(genesis_config: &mut GenesisConfig) -> u64 {
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 {