Resized accounts must be rent exempt

This commit is contained in:
Jack May
2022-02-24 17:49:33 -08:00
parent 44109c0cd4
commit 3bee925967
10 changed files with 639 additions and 142 deletions

View File

@ -767,26 +767,6 @@ dependencies = [
"cfg-if 1.0.0",
]
[[package]]
name = "enum-iterator"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4eeac5c5edb79e4e39fe8439ef35207780a11f69c52cbe424ce3dfad4cb78de6"
dependencies = [
"enum-iterator-derive",
]
[[package]]
name = "enum-iterator-derive"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c134c37760b27a871ba422106eedbb8247da973a09e82558bf26d619c882b159"
dependencies = [
"proc-macro2 1.0.36",
"quote 1.0.14",
"syn 1.0.86",
]
[[package]]
name = "enum-ordinalize"
version = "3.1.10"
@ -3277,7 +3257,6 @@ dependencies = [
"crossbeam-channel",
"dashmap",
"dir-diff",
"enum-iterator",
"flate2",
"fnv",
"index_list",

View File

@ -190,7 +190,7 @@ fn process_instruction(
&system_instruction::create_account(
accounts[0].key,
accounts[1].key,
1,
3000000, // large enough for rent exemption
pre_len as u64,
program_id,
),

View File

@ -53,6 +53,7 @@ use solana_sdk::{
loader_instruction,
message::{Message, SanitizedMessage},
pubkey::Pubkey,
rent::Rent,
signature::{keypair_from_seed, Keypair, Signer},
system_instruction::{self, MAX_PERMITTED_DATA_LENGTH},
system_program, sysvar,
@ -2624,11 +2625,13 @@ fn test_program_bpf_ro_account_modify() {
fn test_program_bpf_realloc() {
solana_logger::setup();
const START_BALANCE: u64 = 100_000_000_000;
let GenesisConfigInfo {
genesis_config,
mint_keypair,
..
} = create_genesis_config(50);
} = create_genesis_config(1_000_000_000_000);
let mint_pubkey = mint_keypair.pubkey();
let signer = &[&mint_keypair];
@ -2648,7 +2651,7 @@ fn test_program_bpf_realloc() {
let mut bump = 0;
let keypair = Keypair::new();
let pubkey = keypair.pubkey();
let account = AccountSharedData::new(42, 5, &program_id);
let account = AccountSharedData::new(START_BALANCE, 5, &program_id);
bank.store_account(&pubkey, &account);
// Realloc RO account
@ -2880,11 +2883,14 @@ fn test_program_bpf_realloc() {
fn test_program_bpf_realloc_invoke() {
solana_logger::setup();
const START_BALANCE: u64 = 100_000_000_000;
let GenesisConfigInfo {
genesis_config,
mut genesis_config,
mint_keypair,
..
} = create_genesis_config(50);
} = create_genesis_config(1_000_000_000_000);
genesis_config.rent = Rent::default();
let mint_pubkey = mint_keypair.pubkey();
let signer = &[&mint_keypair];
@ -2911,7 +2917,7 @@ fn test_program_bpf_realloc_invoke() {
let mut bump = 0;
let keypair = Keypair::new();
let pubkey = keypair.pubkey().clone();
let account = AccountSharedData::new(42, 5, &realloc_program_id);
let account = AccountSharedData::new(START_BALANCE, 5, &realloc_program_id);
bank.store_account(&pubkey, &account);
let invoke_keypair = Keypair::new();
let invoke_pubkey = invoke_keypair.pubkey().clone();
@ -2937,6 +2943,8 @@ fn test_program_bpf_realloc_invoke() {
.unwrap(),
TransactionError::InstructionError(0, InstructionError::ReadonlyDataModified)
);
let account = bank.get_account(&pubkey).unwrap();
assert_eq!(account.lamports(), START_BALANCE);
// Realloc account to 0
bank_client
@ -2948,6 +2956,8 @@ fn test_program_bpf_realloc_invoke() {
),
)
.unwrap();
let account = bank.get_account(&pubkey).unwrap();
assert_eq!(account.lamports(), START_BALANCE);
let data = bank_client.get_account_data(&pubkey).unwrap().unwrap();
assert_eq!(0, data.len());
@ -2995,54 +3005,7 @@ fn test_program_bpf_realloc_invoke() {
TransactionError::InstructionError(0, InstructionError::InvalidRealloc)
);
// Realloc to max length in max increase increments
for i in 0..MAX_PERMITTED_DATA_LENGTH as usize / MAX_PERMITTED_DATA_INCREASE {
bank_client
.send_and_confirm_message(
signer,
Message::new(
&[Instruction::new_with_bytes(
realloc_invoke_program_id,
&[INVOKE_REALLOC_EXTEND_MAX, 1, i as u8, (i / 255) as u8],
vec![
AccountMeta::new(pubkey, false),
AccountMeta::new_readonly(realloc_program_id, false),
],
)],
Some(&mint_pubkey),
),
)
.unwrap();
let data = bank_client.get_account_data(&pubkey).unwrap().unwrap();
assert_eq!((i + 1) * MAX_PERMITTED_DATA_INCREASE, data.len());
}
for i in 0..data.len() {
assert_eq!(data[i], 1);
}
// and one more time should fail
assert_eq!(
bank_client
.send_and_confirm_message(
signer,
Message::new(
&[Instruction::new_with_bytes(
realloc_invoke_program_id,
&[INVOKE_REALLOC_EXTEND_MAX, 2, 1, 1],
vec![
AccountMeta::new(pubkey, false),
AccountMeta::new_readonly(realloc_program_id, false),
],
)],
Some(&mint_pubkey),
)
)
.unwrap_err()
.unwrap(),
TransactionError::InstructionError(0, InstructionError::InvalidRealloc)
);
// Realloc to 0
// Realloc account to 0
bank_client
.send_and_confirm_message(
signer,
@ -3052,6 +3015,8 @@ fn test_program_bpf_realloc_invoke() {
),
)
.unwrap();
let account = bank.get_account(&pubkey).unwrap();
assert_eq!(account.lamports(), START_BALANCE);
let data = bank_client.get_account_data(&pubkey).unwrap().unwrap();
assert_eq!(0, data.len());
@ -3152,7 +3117,7 @@ fn test_program_bpf_realloc_invoke() {
assert_eq!(0, data.len());
// Realloc to 100 and check via CPI
let invoke_account = AccountSharedData::new(42, 5, &realloc_invoke_program_id);
let invoke_account = AccountSharedData::new(START_BALANCE, 5, &realloc_invoke_program_id);
bank.store_account(&invoke_pubkey, &invoke_account);
bank_client
.send_and_confirm_message(
@ -3182,42 +3147,6 @@ fn test_program_bpf_realloc_invoke() {
assert_eq!(data[i], 2);
}
// Realloc rescursively and fill data
let invoke_keypair = Keypair::new();
let invoke_pubkey = invoke_keypair.pubkey().clone();
let invoke_account = AccountSharedData::new(42, 0, &realloc_invoke_program_id);
bank.store_account(&invoke_pubkey, &invoke_account);
let mut instruction_data = vec![];
instruction_data.extend_from_slice(&[INVOKE_REALLOC_RECURSIVE, 1]);
instruction_data.extend_from_slice(&100_usize.to_le_bytes());
bank_client
.send_and_confirm_message(
signer,
Message::new(
&[Instruction::new_with_bytes(
realloc_invoke_program_id,
&instruction_data,
vec![
AccountMeta::new(invoke_pubkey, false),
AccountMeta::new_readonly(realloc_invoke_program_id, false),
],
)],
Some(&mint_pubkey),
),
)
.unwrap();
let data = bank_client
.get_account_data(&invoke_pubkey)
.unwrap()
.unwrap();
assert_eq!(200, data.len());
for i in 0..100 {
assert_eq!(data[i], 1);
}
for i in 100..200 {
assert_eq!(data[i], 2);
}
// Create account, realloc, check
let new_keypair = Keypair::new();
let new_pubkey = new_keypair.pubkey().clone();
@ -3250,7 +3179,7 @@ fn test_program_bpf_realloc_invoke() {
// Invoke, dealloc, and assign
let pre_len = 100;
let new_len = pre_len * 2;
let mut invoke_account = AccountSharedData::new(42, pre_len, &realloc_program_id);
let mut invoke_account = AccountSharedData::new(START_BALANCE, pre_len, &realloc_program_id);
invoke_account.set_data_from_slice(&vec![1; pre_len]);
bank.store_account(&invoke_pubkey, &invoke_account);
let mut instruction_data = vec![];
@ -3330,6 +3259,102 @@ fn test_program_bpf_realloc_invoke() {
.unwrap(),
TransactionError::InstructionError(0, InstructionError::InvalidRealloc)
);
// Realloc to 0
bank_client
.send_and_confirm_message(
signer,
Message::new(
&[realloc(&realloc_program_id, &pubkey, 0, &mut bump)],
Some(&mint_pubkey),
),
)
.unwrap();
let data = bank_client.get_account_data(&pubkey).unwrap().unwrap();
assert_eq!(0, data.len());
// Realloc to max length in max increase increments
for i in 0..MAX_PERMITTED_DATA_LENGTH as usize / MAX_PERMITTED_DATA_INCREASE {
bank_client
.send_and_confirm_message(
signer,
Message::new(
&[Instruction::new_with_bytes(
realloc_invoke_program_id,
&[INVOKE_REALLOC_EXTEND_MAX, 1, i as u8, (i / 255) as u8],
vec![
AccountMeta::new(pubkey, false),
AccountMeta::new_readonly(realloc_program_id, false),
],
)],
Some(&mint_pubkey),
),
)
.unwrap();
let data = bank_client.get_account_data(&pubkey).unwrap().unwrap();
assert_eq!((i + 1) * MAX_PERMITTED_DATA_INCREASE, data.len());
}
for i in 0..data.len() {
assert_eq!(data[i], 1);
}
// and one more time should fail
assert_eq!(
bank_client
.send_and_confirm_message(
signer,
Message::new(
&[Instruction::new_with_bytes(
realloc_invoke_program_id,
&[INVOKE_REALLOC_EXTEND_MAX, 2, 1, 1],
vec![
AccountMeta::new(pubkey, false),
AccountMeta::new_readonly(realloc_program_id, false),
],
)],
Some(&mint_pubkey),
)
)
.unwrap_err()
.unwrap(),
TransactionError::InstructionError(0, InstructionError::InvalidRealloc)
);
// Realloc rescursively and fill data
let invoke_keypair = Keypair::new();
let invoke_pubkey = invoke_keypair.pubkey().clone();
let invoke_account = AccountSharedData::new(START_BALANCE, 0, &realloc_invoke_program_id);
bank.store_account(&invoke_pubkey, &invoke_account);
let mut instruction_data = vec![];
instruction_data.extend_from_slice(&[INVOKE_REALLOC_RECURSIVE, 1]);
instruction_data.extend_from_slice(&100_usize.to_le_bytes());
bank_client
.send_and_confirm_message(
signer,
Message::new(
&[Instruction::new_with_bytes(
realloc_invoke_program_id,
&instruction_data,
vec![
AccountMeta::new(invoke_pubkey, false),
AccountMeta::new_readonly(realloc_invoke_program_id, false),
],
)],
Some(&mint_pubkey),
),
)
.unwrap();
let data = bank_client
.get_account_data(&invoke_pubkey)
.unwrap()
.unwrap();
assert_eq!(200, data.len());
for i in 0..100 {
assert_eq!(data[i], 1);
}
for i in 100..200 {
assert_eq!(data[i], 2);
}
}
#[test]