require stake, vote and executable accounts to be rent exempt (#5928)

* require vote account to be exempt

* make stake account rent exempt

* add rent exempted system instruction

* use rent exemption instruction in vote and stake api

* use rent exempted account while creating executable account

* updating chacha golden hash as instruction data has changed

* rent will be initialized for genesis bank too
This commit is contained in:
Parth
2019-09-20 16:52:17 +05:30
committed by GitHub
parent accd49f2e4
commit 11e6197a83
11 changed files with 199 additions and 18 deletions

View File

@ -2,6 +2,7 @@ use crate::instruction::{AccountMeta, Instruction};
use crate::instruction_processor_utils::DecodeError;
use crate::pubkey::Pubkey;
use crate::system_program;
use crate::sysvar::rent;
use num_derive::FromPrimitive;
#[derive(Serialize, Debug, Clone, PartialEq, FromPrimitive)]
@ -11,6 +12,7 @@ pub enum SystemError {
SourceNotSystemAccount,
InvalidProgramId,
InvalidAccountId,
InsufficientFunds,
}
impl<T> DecodeError<T> for SystemError {
@ -31,13 +33,16 @@ pub enum SystemInstruction {
/// Create a new account
/// * Transaction::keys[0] - source
/// * Transaction::keys[1] - new account key
/// * Transaction::keys[2] - rent sysvar account key (Only required if require_rent_exemption is true)
/// * lamports - number of lamports to transfer to the new account
/// * space - memory to allocate if greater then zero
/// * program_id - the program id of the new account
/// * require_rent_exemption - if set to true, only allow account creation if it's rent exempt
CreateAccount {
lamports: u64,
space: u64,
program_id: Pubkey,
require_rent_exemption: bool,
},
/// Assign account to a program
/// * Transaction::keys[0] - account to assign
@ -55,16 +60,43 @@ pub fn create_account(
space: u64,
program_id: &Pubkey,
) -> Instruction {
let account_metas = vec![
generate_create_account_instruction(from_pubkey, to_pubkey, lamports, space, program_id, false)
}
pub fn create_rent_exempted_account(
from_pubkey: &Pubkey,
to_pubkey: &Pubkey,
lamports: u64,
space: u64,
program_id: &Pubkey,
) -> Instruction {
generate_create_account_instruction(from_pubkey, to_pubkey, lamports, space, program_id, true)
}
pub(crate) fn generate_create_account_instruction(
from_pubkey: &Pubkey,
to_pubkey: &Pubkey,
lamports: u64,
space: u64,
program_id: &Pubkey,
require_rent_exemption: bool,
) -> Instruction {
let mut account_metas = vec![
AccountMeta::new(*from_pubkey, true),
AccountMeta::new(*to_pubkey, false),
];
if require_rent_exemption {
account_metas.push(AccountMeta::new(rent::id(), false));
}
Instruction::new(
system_program::id(),
&SystemInstruction::CreateAccount {
lamports,
space,
program_id: *program_id,
require_rent_exemption,
},
account_metas,
)