Pārlūkot izejas kodu

add rent reserve for bootstrap stakes (#6876)

* genesis investor stakes

* assert rent is sufficient for these bootstrappers
Rob Walker 6 gadi atpakaļ
vecāks
revīzija
bb00904fc8

+ 4 - 2
core/src/commitment.rs

@@ -438,11 +438,13 @@ mod tests {
         let sk1 = Pubkey::new_rand();
         let pk1 = Pubkey::new_rand();
         let mut vote_account1 = vote_state::create_account(&pk1, &Pubkey::new_rand(), 0, 100);
-        let stake_account1 = stake_state::create_account(&sk1, &pk1, &vote_account1, 100);
+        let stake_account1 =
+            stake_state::create_account(&sk1, &pk1, &vote_account1, &genesis_config.rent, 100);
         let sk2 = Pubkey::new_rand();
         let pk2 = Pubkey::new_rand();
         let mut vote_account2 = vote_state::create_account(&pk2, &Pubkey::new_rand(), 0, 50);
-        let stake_account2 = stake_state::create_account(&sk2, &pk2, &vote_account2, 50);
+        let stake_account2 =
+            stake_state::create_account(&sk2, &pk2, &vote_account2, &genesis_config.rent, 50);
 
         genesis_config.accounts.extend(vec![
             (pk1, vote_account1.clone()),

+ 8 - 6
genesis/src/main.rs

@@ -319,10 +319,18 @@ fn main() -> Result<(), Box<dyn error::Error>> {
 
     let bootstrap_leader_vote_account =
         vote_state::create_account(&bootstrap_vote_pubkey, &bootstrap_leader_pubkey, 0, 1);
+
+    let rent = Rent {
+        lamports_per_byte_year: value_t_or_exit!(matches, "lamports_per_byte_year", u64),
+        exemption_threshold: value_t_or_exit!(matches, "rent_exemption_threshold", f64),
+        burn_percent: value_t_or_exit!(matches, "rent_burn_percentage", u8),
+    };
+
     let bootstrap_leader_stake_account = stake_state::create_account(
         &bootstrap_leader_pubkey,
         &bootstrap_vote_pubkey,
         &bootstrap_leader_vote_account,
+        &rent,
         bootstrap_leader_stake_lamports,
     );
 
@@ -352,12 +360,6 @@ fn main() -> Result<(), Box<dyn error::Error>> {
         value_t_or_exit!(matches, "target_signatures_per_slot", usize),
     );
 
-    let rent = Rent {
-        lamports_per_byte_year: value_t_or_exit!(matches, "lamports_per_byte_year", u64),
-        exemption_threshold: value_t_or_exit!(matches, "rent_exemption_threshold", f64),
-        burn_percent: value_t_or_exit!(matches, "rent_burn_percentage", u8),
-    };
-
     let mut poh_config = PohConfig::default();
     poh_config.target_tick_duration =
         Duration::from_millis(value_t_or_exit!(matches, "target_tick_duration", u64));

+ 17 - 15
programs/stake_api/src/stake_state.rs

@@ -356,16 +356,6 @@ impl Stake {
         ))
     }
 
-    fn new_bootstrap(stake: u64, voter_pubkey: &Pubkey, vote_state: &VoteState) -> Self {
-        Self::new(
-            stake,
-            voter_pubkey,
-            vote_state,
-            std::u64::MAX,
-            &Config::default(),
-        )
-    }
-
     fn redelegate(
         &mut self,
         voter_pubkey: &Pubkey,
@@ -779,24 +769,30 @@ pub fn create_account(
     authorized: &Pubkey,
     voter_pubkey: &Pubkey,
     vote_account: &Account,
+    rent: &Rent,
     lamports: u64,
 ) -> 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>());
     stake_account
         .set_state(&StakeState::Stake(
             Meta {
-                rent_exempt_reserve: Rent::default()
-                    .minimum_balance(std::mem::size_of::<StakeState>()),
+                rent_exempt_reserve,
                 authorized: Authorized {
                     staker: *authorized,
                     withdrawer: *authorized,
                 },
                 lockup: Lockup::default(),
             },
-            Stake::new_bootstrap(lamports, voter_pubkey, &vote_state),
+            Stake::new(
+                lamports - rent_exempt_reserve, // underflow is an error, assert!(lamports> rent_exempt_reserve);
+                voter_pubkey,
+                &vote_state,
+                std::u64::MAX,
+                &Config::default(),
+            ),
         ))
         .expect("set_state");
 
@@ -1737,7 +1733,13 @@ mod tests {
         let mut vote_state = VoteState::default();
         // assume stake.stake() is right
         // bootstrap means fully-vested stake at epoch 0
-        let mut stake = Stake::new_bootstrap(1, &Pubkey::default(), &vote_state);
+        let mut stake = Stake::new(
+            1,
+            &Pubkey::default(),
+            &vote_state,
+            std::u64::MAX,
+            &Config::default(),
+        );
 
         // this one can't collect now, credits_observed == vote_state.credits()
         assert_eq!(

+ 5 - 0
runtime/src/genesis_utils.rs

@@ -3,6 +3,7 @@ use solana_sdk::{
     fee_calculator::FeeCalculator,
     genesis_config::GenesisConfig,
     pubkey::Pubkey,
+    rent::Rent,
     signature::{Keypair, KeypairUtil},
     system_program::{self, solana_system_program},
 };
@@ -39,10 +40,13 @@ pub fn create_genesis_config_with_leader(
         bootstrap_leader_stake_lamports,
     );
 
+    let rent = Rent::free();
+
     let stake_account = stake_state::create_account(
         &staking_keypair.pubkey(),
         &voting_keypair.pubkey(),
         &vote_account,
+        &rent,
         bootstrap_leader_stake_lamports,
     );
 
@@ -77,6 +81,7 @@ pub fn create_genesis_config_with_leader(
         accounts,
         native_instruction_processors,
         fee_calculator,
+        rent,
         ..GenesisConfig::default()
     };
 

+ 2 - 1
runtime/src/stakes.rs

@@ -209,7 +209,7 @@ impl Stakes {
 #[cfg(test)]
 pub mod tests {
     use super::*;
-    use solana_sdk::pubkey::Pubkey;
+    use solana_sdk::{pubkey::Pubkey, rent::Rent};
     use solana_stake_api::stake_state;
     use solana_vote_api::vote_state::{self, VoteState, MAX_LOCKOUT_HISTORY};
 
@@ -232,6 +232,7 @@ pub mod tests {
                 &stake_pubkey,
                 &vote_pubkey,
                 &vote_state::create_account(&vote_pubkey, &Pubkey::new_rand(), 0, 1),
+                &Rent::free(),
                 stake,
             ),
         )

+ 7 - 0
sdk/src/rent.rs

@@ -59,6 +59,13 @@ impl Rent {
             )
         }
     }
+
+    pub fn free() -> Self {
+        Self {
+            lamports_per_byte_year: 0,
+            ..Rent::default()
+        }
+    }
 }
 
 #[cfg(test)]