Refactor: Removes Rc
from Refcell<AccountSharedData>
in the program-runtime (#21927)
* Removes Rc from Rc<RefCell<AccountSharedData>> in the program-runtime. * Adjusts tests in bpf_loader, system_instruction_processor, config_processor, vote_instruction and stake_instruction
This commit is contained in:
committed by
GitHub
parent
56ec5245cc
commit
66fa8f9667
@ -147,22 +147,26 @@ mod tests {
|
||||
solana_program_runtime::invoke_context::mock_process_instruction,
|
||||
solana_sdk::{
|
||||
account::AccountSharedData,
|
||||
instruction::AccountMeta,
|
||||
pubkey::Pubkey,
|
||||
signature::{Keypair, Signer},
|
||||
system_instruction::SystemInstruction,
|
||||
},
|
||||
std::{cell::RefCell, rc::Rc},
|
||||
};
|
||||
|
||||
fn process_instruction(
|
||||
instruction_data: &[u8],
|
||||
keyed_accounts: &[(bool, bool, Pubkey, Rc<RefCell<AccountSharedData>>)],
|
||||
) -> Result<(), InstructionError> {
|
||||
transaction_accounts: Vec<(Pubkey, AccountSharedData)>,
|
||||
instruction_accounts: Vec<AccountMeta>,
|
||||
expected_result: Result<(), InstructionError>,
|
||||
) -> Vec<AccountSharedData> {
|
||||
mock_process_instruction(
|
||||
&id(),
|
||||
Vec::new(),
|
||||
instruction_data,
|
||||
keyed_accounts,
|
||||
transaction_accounts,
|
||||
instruction_accounts,
|
||||
expected_result,
|
||||
super::process_instruction,
|
||||
)
|
||||
}
|
||||
@ -191,16 +195,12 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
fn create_config_account(
|
||||
keys: Vec<(Pubkey, bool)>,
|
||||
) -> (Keypair, Rc<RefCell<AccountSharedData>>) {
|
||||
fn create_config_account(keys: Vec<(Pubkey, bool)>) -> (Keypair, AccountSharedData) {
|
||||
let from_pubkey = Pubkey::new_unique();
|
||||
let config_keypair = Keypair::new();
|
||||
let config_pubkey = config_keypair.pubkey();
|
||||
|
||||
let instructions =
|
||||
config_instruction::create_account::<MyConfig>(&from_pubkey, &config_pubkey, 1, keys);
|
||||
|
||||
let system_instruction = limited_deserialize(&instructions[0].data).unwrap();
|
||||
let space = match system_instruction {
|
||||
SystemInstruction::CreateAccount {
|
||||
@ -210,14 +210,18 @@ mod tests {
|
||||
} => space,
|
||||
_ => panic!("Not a CreateAccount system instruction"),
|
||||
};
|
||||
let config_account = AccountSharedData::new_ref(0, space as usize, &id());
|
||||
let keyed_accounts = [(true, false, config_pubkey, config_account.clone())];
|
||||
assert_eq!(
|
||||
process_instruction(&instructions[1].data, &keyed_accounts),
|
||||
Ok(())
|
||||
let config_account = AccountSharedData::new(0, space as usize, &id());
|
||||
let accounts = process_instruction(
|
||||
&instructions[1].data,
|
||||
vec![(config_pubkey, config_account)],
|
||||
vec![AccountMeta {
|
||||
pubkey: config_pubkey,
|
||||
is_signer: true,
|
||||
is_writable: false,
|
||||
}],
|
||||
Ok(()),
|
||||
);
|
||||
|
||||
(config_keypair, config_account)
|
||||
(config_keypair, accounts[0].clone())
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -226,7 +230,7 @@ mod tests {
|
||||
let (_, config_account) = create_config_account(vec![]);
|
||||
assert_eq!(
|
||||
Some(MyConfig::default()),
|
||||
deserialize(get_config_data(config_account.borrow().data()).unwrap()).ok()
|
||||
deserialize(get_config_data(config_account.data()).unwrap()).ok()
|
||||
);
|
||||
}
|
||||
|
||||
@ -239,14 +243,19 @@ mod tests {
|
||||
let my_config = MyConfig::new(42);
|
||||
|
||||
let instruction = config_instruction::store(&config_pubkey, true, keys, &my_config);
|
||||
let keyed_accounts = [(true, false, config_pubkey, config_account.clone())];
|
||||
assert_eq!(
|
||||
process_instruction(&instruction.data, &keyed_accounts),
|
||||
Ok(())
|
||||
let accounts = process_instruction(
|
||||
&instruction.data,
|
||||
vec![(config_pubkey, config_account)],
|
||||
vec![AccountMeta {
|
||||
pubkey: config_pubkey,
|
||||
is_signer: true,
|
||||
is_writable: false,
|
||||
}],
|
||||
Ok(()),
|
||||
);
|
||||
assert_eq!(
|
||||
Some(my_config),
|
||||
deserialize(get_config_data(config_account.borrow().data()).unwrap()).ok()
|
||||
deserialize(get_config_data(accounts[0].data()).unwrap()).ok()
|
||||
);
|
||||
}
|
||||
|
||||
@ -260,10 +269,15 @@ mod tests {
|
||||
|
||||
let mut instruction = config_instruction::store(&config_pubkey, true, keys, &my_config);
|
||||
instruction.data = vec![0; 123]; // <-- Replace data with a vector that's too large
|
||||
let keyed_accounts = [(true, false, config_pubkey, config_account)];
|
||||
assert_eq!(
|
||||
process_instruction(&instruction.data, &keyed_accounts),
|
||||
Err(InstructionError::InvalidInstructionData)
|
||||
process_instruction(
|
||||
&instruction.data,
|
||||
vec![(config_pubkey, config_account)],
|
||||
vec![AccountMeta {
|
||||
pubkey: config_pubkey,
|
||||
is_signer: true,
|
||||
is_writable: false,
|
||||
}],
|
||||
Err(InstructionError::InvalidInstructionData),
|
||||
);
|
||||
}
|
||||
|
||||
@ -277,10 +291,15 @@ mod tests {
|
||||
|
||||
let mut instruction = config_instruction::store(&config_pubkey, true, vec![], &my_config);
|
||||
instruction.accounts[0].is_signer = false; // <----- not a signer
|
||||
let keyed_accounts = [(false, false, config_pubkey, config_account)];
|
||||
assert_eq!(
|
||||
process_instruction(&instruction.data, &keyed_accounts),
|
||||
Err(InstructionError::MissingRequiredSignature)
|
||||
process_instruction(
|
||||
&instruction.data,
|
||||
vec![(config_pubkey, config_account)],
|
||||
vec![AccountMeta {
|
||||
pubkey: config_pubkey,
|
||||
is_signer: false,
|
||||
is_writable: false,
|
||||
}],
|
||||
Err(InstructionError::MissingRequiredSignature),
|
||||
);
|
||||
}
|
||||
|
||||
@ -298,24 +317,41 @@ mod tests {
|
||||
let (config_keypair, config_account) = create_config_account(keys.clone());
|
||||
let config_pubkey = config_keypair.pubkey();
|
||||
let my_config = MyConfig::new(42);
|
||||
let signer0_account = AccountSharedData::new(0, 0, &Pubkey::new_unique());
|
||||
let signer1_account = AccountSharedData::new(0, 0, &Pubkey::new_unique());
|
||||
|
||||
let instruction = config_instruction::store(&config_pubkey, true, keys.clone(), &my_config);
|
||||
let signer0_account = AccountSharedData::new_ref(0, 0, &Pubkey::new_unique());
|
||||
let signer1_account = AccountSharedData::new_ref(0, 0, &Pubkey::new_unique());
|
||||
let keyed_accounts = [
|
||||
(true, false, config_pubkey, config_account.clone()),
|
||||
(true, false, signer0_pubkey, signer0_account),
|
||||
(true, false, signer1_pubkey, signer1_account),
|
||||
];
|
||||
assert_eq!(
|
||||
process_instruction(&instruction.data, &keyed_accounts),
|
||||
Ok(())
|
||||
let accounts = process_instruction(
|
||||
&instruction.data,
|
||||
vec![
|
||||
(config_pubkey, config_account),
|
||||
(signer0_pubkey, signer0_account),
|
||||
(signer1_pubkey, signer1_account),
|
||||
],
|
||||
vec![
|
||||
AccountMeta {
|
||||
pubkey: config_pubkey,
|
||||
is_signer: true,
|
||||
is_writable: false,
|
||||
},
|
||||
AccountMeta {
|
||||
pubkey: signer0_pubkey,
|
||||
is_signer: true,
|
||||
is_writable: false,
|
||||
},
|
||||
AccountMeta {
|
||||
pubkey: signer1_pubkey,
|
||||
is_signer: true,
|
||||
is_writable: false,
|
||||
},
|
||||
],
|
||||
Ok(()),
|
||||
);
|
||||
let meta_data: ConfigKeys = deserialize(config_account.borrow().data()).unwrap();
|
||||
let meta_data: ConfigKeys = deserialize(accounts[0].data()).unwrap();
|
||||
assert_eq!(meta_data.keys, keys);
|
||||
assert_eq!(
|
||||
Some(my_config),
|
||||
deserialize(get_config_data(config_account.borrow().data()).unwrap()).ok()
|
||||
deserialize(get_config_data(accounts[0].data()).unwrap()).ok()
|
||||
);
|
||||
}
|
||||
|
||||
@ -328,13 +364,18 @@ mod tests {
|
||||
let (config_keypair, _) = create_config_account(keys.clone());
|
||||
let config_pubkey = config_keypair.pubkey();
|
||||
let my_config = MyConfig::new(42);
|
||||
let signer0_account = AccountSharedData::new(0, 0, &id());
|
||||
|
||||
let instruction = config_instruction::store(&config_pubkey, false, keys, &my_config);
|
||||
let signer0_account = AccountSharedData::new_ref(0, 0, &id());
|
||||
let keyed_accounts = [(true, false, signer0_pubkey, signer0_account)];
|
||||
assert_eq!(
|
||||
process_instruction(&instruction.data, &keyed_accounts),
|
||||
Err(InstructionError::InvalidAccountData)
|
||||
process_instruction(
|
||||
&instruction.data,
|
||||
vec![(signer0_pubkey, signer0_account)],
|
||||
vec![AccountMeta {
|
||||
pubkey: signer0_pubkey,
|
||||
is_signer: true,
|
||||
is_writable: false,
|
||||
}],
|
||||
Err(InstructionError::InvalidAccountData),
|
||||
);
|
||||
}
|
||||
|
||||
@ -343,30 +384,56 @@ mod tests {
|
||||
solana_logger::setup();
|
||||
let signer0_pubkey = Pubkey::new_unique();
|
||||
let signer1_pubkey = Pubkey::new_unique();
|
||||
let signer0_account = AccountSharedData::new_ref(0, 0, &Pubkey::new_unique());
|
||||
let signer1_account = AccountSharedData::new_ref(0, 0, &Pubkey::new_unique());
|
||||
let signer0_account = AccountSharedData::new(0, 0, &Pubkey::new_unique());
|
||||
let signer1_account = AccountSharedData::new(0, 0, &Pubkey::new_unique());
|
||||
let keys = vec![(signer0_pubkey, true)];
|
||||
let (config_keypair, config_account) = create_config_account(keys.clone());
|
||||
let config_pubkey = config_keypair.pubkey();
|
||||
let my_config = MyConfig::new(42);
|
||||
|
||||
let instruction = config_instruction::store(&config_pubkey, true, keys, &my_config);
|
||||
|
||||
// Config-data pubkey doesn't match signer
|
||||
let mut keyed_accounts = [
|
||||
(true, false, config_pubkey, config_account),
|
||||
(true, false, signer1_pubkey, signer1_account),
|
||||
];
|
||||
assert_eq!(
|
||||
process_instruction(&instruction.data, &keyed_accounts),
|
||||
Err(InstructionError::MissingRequiredSignature)
|
||||
let instruction = config_instruction::store(&config_pubkey, true, keys, &my_config);
|
||||
process_instruction(
|
||||
&instruction.data,
|
||||
vec![
|
||||
(config_pubkey, config_account.clone()),
|
||||
(signer1_pubkey, signer1_account),
|
||||
],
|
||||
vec![
|
||||
AccountMeta {
|
||||
pubkey: config_pubkey,
|
||||
is_signer: true,
|
||||
is_writable: false,
|
||||
},
|
||||
AccountMeta {
|
||||
pubkey: signer1_pubkey,
|
||||
is_signer: true,
|
||||
is_writable: false,
|
||||
},
|
||||
],
|
||||
Err(InstructionError::MissingRequiredSignature),
|
||||
);
|
||||
|
||||
// Config-data pubkey not a signer
|
||||
keyed_accounts[1] = (false, false, signer0_pubkey, signer0_account);
|
||||
assert_eq!(
|
||||
process_instruction(&instruction.data, &keyed_accounts),
|
||||
Err(InstructionError::MissingRequiredSignature)
|
||||
process_instruction(
|
||||
&instruction.data,
|
||||
vec![
|
||||
(config_pubkey, config_account),
|
||||
(signer0_pubkey, signer0_account),
|
||||
],
|
||||
vec![
|
||||
AccountMeta {
|
||||
pubkey: config_pubkey,
|
||||
is_signer: true,
|
||||
is_writable: false,
|
||||
},
|
||||
AccountMeta {
|
||||
pubkey: signer0_pubkey,
|
||||
is_signer: false,
|
||||
is_writable: false,
|
||||
},
|
||||
],
|
||||
Err(InstructionError::MissingRequiredSignature),
|
||||
);
|
||||
}
|
||||
|
||||
@ -377,9 +444,9 @@ mod tests {
|
||||
let signer0_pubkey = Pubkey::new_unique();
|
||||
let signer1_pubkey = Pubkey::new_unique();
|
||||
let signer2_pubkey = Pubkey::new_unique();
|
||||
let signer0_account = AccountSharedData::new_ref(0, 0, &Pubkey::new_unique());
|
||||
let signer1_account = AccountSharedData::new_ref(0, 0, &Pubkey::new_unique());
|
||||
let signer2_account = AccountSharedData::new_ref(0, 0, &Pubkey::new_unique());
|
||||
let signer0_account = AccountSharedData::new(0, 0, &Pubkey::new_unique());
|
||||
let signer1_account = AccountSharedData::new(0, 0, &Pubkey::new_unique());
|
||||
let signer2_account = AccountSharedData::new(0, 0, &Pubkey::new_unique());
|
||||
let keys = vec![
|
||||
(pubkey, false),
|
||||
(signer0_pubkey, true),
|
||||
@ -390,40 +457,98 @@ mod tests {
|
||||
let my_config = MyConfig::new(42);
|
||||
|
||||
let instruction = config_instruction::store(&config_pubkey, true, keys.clone(), &my_config);
|
||||
let mut keyed_accounts = [
|
||||
(true, false, config_pubkey, config_account.clone()),
|
||||
(true, false, signer0_pubkey, signer0_account),
|
||||
(true, false, signer1_pubkey, signer1_account),
|
||||
];
|
||||
assert_eq!(
|
||||
process_instruction(&instruction.data, &keyed_accounts),
|
||||
Ok(())
|
||||
let accounts = process_instruction(
|
||||
&instruction.data,
|
||||
vec![
|
||||
(config_pubkey, config_account),
|
||||
(signer0_pubkey, signer0_account.clone()),
|
||||
(signer1_pubkey, signer1_account.clone()),
|
||||
],
|
||||
vec![
|
||||
AccountMeta {
|
||||
pubkey: config_pubkey,
|
||||
is_signer: true,
|
||||
is_writable: false,
|
||||
},
|
||||
AccountMeta {
|
||||
pubkey: signer0_pubkey,
|
||||
is_signer: true,
|
||||
is_writable: false,
|
||||
},
|
||||
AccountMeta {
|
||||
pubkey: signer1_pubkey,
|
||||
is_signer: true,
|
||||
is_writable: false,
|
||||
},
|
||||
],
|
||||
Ok(()),
|
||||
);
|
||||
|
||||
// Update with expected signatures
|
||||
let new_config = MyConfig::new(84);
|
||||
let instruction =
|
||||
config_instruction::store(&config_pubkey, false, keys.clone(), &new_config);
|
||||
keyed_accounts[0].0 = false;
|
||||
assert_eq!(
|
||||
process_instruction(&instruction.data, &keyed_accounts),
|
||||
Ok(())
|
||||
let accounts = process_instruction(
|
||||
&instruction.data,
|
||||
vec![
|
||||
(config_pubkey, accounts[0].clone()),
|
||||
(signer0_pubkey, signer0_account.clone()),
|
||||
(signer1_pubkey, signer1_account.clone()),
|
||||
],
|
||||
vec![
|
||||
AccountMeta {
|
||||
pubkey: config_pubkey,
|
||||
is_signer: false,
|
||||
is_writable: false,
|
||||
},
|
||||
AccountMeta {
|
||||
pubkey: signer0_pubkey,
|
||||
is_signer: true,
|
||||
is_writable: false,
|
||||
},
|
||||
AccountMeta {
|
||||
pubkey: signer1_pubkey,
|
||||
is_signer: true,
|
||||
is_writable: false,
|
||||
},
|
||||
],
|
||||
Ok(()),
|
||||
);
|
||||
let meta_data: ConfigKeys = deserialize(config_account.borrow().data()).unwrap();
|
||||
let meta_data: ConfigKeys = deserialize(accounts[0].data()).unwrap();
|
||||
assert_eq!(meta_data.keys, keys);
|
||||
assert_eq!(
|
||||
new_config,
|
||||
MyConfig::deserialize(get_config_data(config_account.borrow().data()).unwrap())
|
||||
.unwrap()
|
||||
MyConfig::deserialize(get_config_data(accounts[0].data()).unwrap()).unwrap()
|
||||
);
|
||||
|
||||
// Attempt update with incomplete signatures
|
||||
let keys = vec![(pubkey, false), (signer0_pubkey, true)];
|
||||
let instruction = config_instruction::store(&config_pubkey, false, keys, &my_config);
|
||||
keyed_accounts[2].0 = false;
|
||||
assert_eq!(
|
||||
process_instruction(&instruction.data, &keyed_accounts),
|
||||
Err(InstructionError::MissingRequiredSignature)
|
||||
process_instruction(
|
||||
&instruction.data,
|
||||
vec![
|
||||
(config_pubkey, accounts[0].clone()),
|
||||
(signer0_pubkey, signer0_account.clone()),
|
||||
(signer1_pubkey, signer1_account),
|
||||
],
|
||||
vec![
|
||||
AccountMeta {
|
||||
pubkey: config_pubkey,
|
||||
is_signer: false,
|
||||
is_writable: false,
|
||||
},
|
||||
AccountMeta {
|
||||
pubkey: signer0_pubkey,
|
||||
is_signer: true,
|
||||
is_writable: false,
|
||||
},
|
||||
AccountMeta {
|
||||
pubkey: signer1_pubkey,
|
||||
is_signer: false,
|
||||
is_writable: false,
|
||||
},
|
||||
],
|
||||
Err(InstructionError::MissingRequiredSignature),
|
||||
);
|
||||
|
||||
// Attempt update with incorrect signatures
|
||||
@ -433,10 +558,31 @@ mod tests {
|
||||
(signer2_pubkey, true),
|
||||
];
|
||||
let instruction = config_instruction::store(&config_pubkey, false, keys, &my_config);
|
||||
keyed_accounts[2] = (true, false, signer2_pubkey, signer2_account);
|
||||
assert_eq!(
|
||||
process_instruction(&instruction.data, &keyed_accounts),
|
||||
Err(InstructionError::MissingRequiredSignature)
|
||||
process_instruction(
|
||||
&instruction.data,
|
||||
vec![
|
||||
(config_pubkey, accounts[0].clone()),
|
||||
(signer0_pubkey, signer0_account),
|
||||
(signer2_pubkey, signer2_account),
|
||||
],
|
||||
vec![
|
||||
AccountMeta {
|
||||
pubkey: config_pubkey,
|
||||
is_signer: false,
|
||||
is_writable: false,
|
||||
},
|
||||
AccountMeta {
|
||||
pubkey: signer0_pubkey,
|
||||
is_signer: true,
|
||||
is_writable: false,
|
||||
},
|
||||
AccountMeta {
|
||||
pubkey: signer2_pubkey,
|
||||
is_signer: true,
|
||||
is_writable: false,
|
||||
},
|
||||
],
|
||||
Err(InstructionError::MissingRequiredSignature),
|
||||
);
|
||||
}
|
||||
|
||||
@ -445,7 +591,7 @@ mod tests {
|
||||
solana_logger::setup();
|
||||
let config_address = Pubkey::new_unique();
|
||||
let signer0_pubkey = Pubkey::new_unique();
|
||||
let signer0_account = AccountSharedData::new_ref(0, 0, &Pubkey::new_unique());
|
||||
let signer0_account = AccountSharedData::new(0, 0, &Pubkey::new_unique());
|
||||
let keys = vec![
|
||||
(config_address, false),
|
||||
(signer0_pubkey, true),
|
||||
@ -457,13 +603,29 @@ mod tests {
|
||||
|
||||
// Attempt initialization with duplicate signer inputs
|
||||
let instruction = config_instruction::store(&config_pubkey, true, keys, &my_config);
|
||||
let keyed_accounts = [
|
||||
(true, false, config_pubkey, config_account),
|
||||
(true, false, signer0_pubkey, signer0_account.clone()),
|
||||
(true, false, signer0_pubkey, signer0_account),
|
||||
];
|
||||
assert_eq!(
|
||||
process_instruction(&instruction.data, &keyed_accounts),
|
||||
process_instruction(
|
||||
&instruction.data,
|
||||
vec![
|
||||
(config_pubkey, config_account),
|
||||
(signer0_pubkey, signer0_account),
|
||||
],
|
||||
vec![
|
||||
AccountMeta {
|
||||
pubkey: config_pubkey,
|
||||
is_signer: true,
|
||||
is_writable: false,
|
||||
},
|
||||
AccountMeta {
|
||||
pubkey: signer0_pubkey,
|
||||
is_signer: true,
|
||||
is_writable: false,
|
||||
},
|
||||
AccountMeta {
|
||||
pubkey: signer0_pubkey,
|
||||
is_signer: true,
|
||||
is_writable: false,
|
||||
},
|
||||
],
|
||||
Err(InstructionError::InvalidArgument),
|
||||
);
|
||||
}
|
||||
@ -474,8 +636,8 @@ mod tests {
|
||||
let config_address = Pubkey::new_unique();
|
||||
let signer0_pubkey = Pubkey::new_unique();
|
||||
let signer1_pubkey = Pubkey::new_unique();
|
||||
let signer0_account = AccountSharedData::new_ref(0, 0, &Pubkey::new_unique());
|
||||
let signer1_account = AccountSharedData::new_ref(0, 0, &Pubkey::new_unique());
|
||||
let signer0_account = AccountSharedData::new(0, 0, &Pubkey::new_unique());
|
||||
let signer1_account = AccountSharedData::new(0, 0, &Pubkey::new_unique());
|
||||
let keys = vec![
|
||||
(config_address, false),
|
||||
(signer0_pubkey, true),
|
||||
@ -486,13 +648,30 @@ mod tests {
|
||||
let my_config = MyConfig::new(42);
|
||||
|
||||
let instruction = config_instruction::store(&config_pubkey, true, keys, &my_config);
|
||||
let mut keyed_accounts = [
|
||||
(true, false, config_pubkey, config_account),
|
||||
(true, false, signer0_pubkey, signer0_account),
|
||||
(true, false, signer1_pubkey, signer1_account),
|
||||
];
|
||||
assert_eq!(
|
||||
process_instruction(&instruction.data, &keyed_accounts),
|
||||
let accounts = process_instruction(
|
||||
&instruction.data,
|
||||
vec![
|
||||
(config_pubkey, config_account),
|
||||
(signer0_pubkey, signer0_account.clone()),
|
||||
(signer1_pubkey, signer1_account),
|
||||
],
|
||||
vec![
|
||||
AccountMeta {
|
||||
pubkey: config_pubkey,
|
||||
is_signer: true,
|
||||
is_writable: false,
|
||||
},
|
||||
AccountMeta {
|
||||
pubkey: signer0_pubkey,
|
||||
is_signer: true,
|
||||
is_writable: false,
|
||||
},
|
||||
AccountMeta {
|
||||
pubkey: signer1_pubkey,
|
||||
is_signer: true,
|
||||
is_writable: false,
|
||||
},
|
||||
],
|
||||
Ok(()),
|
||||
);
|
||||
|
||||
@ -504,9 +683,29 @@ mod tests {
|
||||
(signer0_pubkey, true),
|
||||
];
|
||||
let instruction = config_instruction::store(&config_pubkey, false, dupe_keys, &new_config);
|
||||
keyed_accounts[2] = keyed_accounts[1].clone();
|
||||
assert_eq!(
|
||||
process_instruction(&instruction.data, &keyed_accounts),
|
||||
process_instruction(
|
||||
&instruction.data,
|
||||
vec![
|
||||
(config_pubkey, accounts[0].clone()),
|
||||
(signer0_pubkey, signer0_account),
|
||||
],
|
||||
vec![
|
||||
AccountMeta {
|
||||
pubkey: config_pubkey,
|
||||
is_signer: true,
|
||||
is_writable: false,
|
||||
},
|
||||
AccountMeta {
|
||||
pubkey: signer0_pubkey,
|
||||
is_signer: true,
|
||||
is_writable: false,
|
||||
},
|
||||
AccountMeta {
|
||||
pubkey: signer0_pubkey,
|
||||
is_signer: true,
|
||||
is_writable: false,
|
||||
},
|
||||
],
|
||||
Err(InstructionError::InvalidArgument),
|
||||
);
|
||||
}
|
||||
@ -516,7 +715,7 @@ mod tests {
|
||||
solana_logger::setup();
|
||||
let pubkey = Pubkey::new_unique();
|
||||
let signer0_pubkey = Pubkey::new_unique();
|
||||
let signer0_account = AccountSharedData::new_ref(0, 0, &Pubkey::new_unique());
|
||||
let signer0_account = AccountSharedData::new(0, 0, &Pubkey::new_unique());
|
||||
let keys = vec![
|
||||
(pubkey, false),
|
||||
(signer0_pubkey, true),
|
||||
@ -525,7 +724,6 @@ mod tests {
|
||||
let (config_keypair, config_account) = create_config_account(keys);
|
||||
let config_pubkey = config_keypair.pubkey();
|
||||
let my_config = MyConfig::new(42);
|
||||
|
||||
let keys = vec![
|
||||
(pubkey, false),
|
||||
(signer0_pubkey, true),
|
||||
@ -533,37 +731,70 @@ mod tests {
|
||||
];
|
||||
|
||||
let instruction = config_instruction::store(&config_pubkey, true, keys.clone(), &my_config);
|
||||
let keyed_accounts = [
|
||||
(true, false, config_pubkey, config_account.clone()),
|
||||
(true, false, signer0_pubkey, signer0_account),
|
||||
];
|
||||
assert_eq!(
|
||||
process_instruction(&instruction.data, &keyed_accounts),
|
||||
Ok(())
|
||||
let accounts = process_instruction(
|
||||
&instruction.data,
|
||||
vec![
|
||||
(config_pubkey, config_account),
|
||||
(signer0_pubkey, signer0_account.clone()),
|
||||
],
|
||||
vec![
|
||||
AccountMeta {
|
||||
pubkey: config_pubkey,
|
||||
is_signer: true,
|
||||
is_writable: false,
|
||||
},
|
||||
AccountMeta {
|
||||
pubkey: signer0_pubkey,
|
||||
is_signer: true,
|
||||
is_writable: false,
|
||||
},
|
||||
],
|
||||
Ok(()),
|
||||
);
|
||||
|
||||
// Update with expected signatures
|
||||
let new_config = MyConfig::new(84);
|
||||
let instruction =
|
||||
config_instruction::store(&config_pubkey, true, keys.clone(), &new_config);
|
||||
assert_eq!(
|
||||
process_instruction(&instruction.data, &keyed_accounts),
|
||||
Ok(())
|
||||
let accounts = process_instruction(
|
||||
&instruction.data,
|
||||
vec![
|
||||
(config_pubkey, accounts[0].clone()),
|
||||
(signer0_pubkey, signer0_account),
|
||||
],
|
||||
vec![
|
||||
AccountMeta {
|
||||
pubkey: config_pubkey,
|
||||
is_signer: true,
|
||||
is_writable: false,
|
||||
},
|
||||
AccountMeta {
|
||||
pubkey: signer0_pubkey,
|
||||
is_signer: true,
|
||||
is_writable: false,
|
||||
},
|
||||
],
|
||||
Ok(()),
|
||||
);
|
||||
let meta_data: ConfigKeys = deserialize(config_account.borrow().data()).unwrap();
|
||||
let meta_data: ConfigKeys = deserialize(accounts[0].data()).unwrap();
|
||||
assert_eq!(meta_data.keys, keys);
|
||||
assert_eq!(
|
||||
new_config,
|
||||
MyConfig::deserialize(get_config_data(config_account.borrow().data()).unwrap())
|
||||
.unwrap()
|
||||
MyConfig::deserialize(get_config_data(accounts[0].data()).unwrap()).unwrap()
|
||||
);
|
||||
|
||||
// Attempt update with incomplete signatures
|
||||
let keys = vec![(pubkey, false), (config_keypair.pubkey(), true)];
|
||||
let instruction = config_instruction::store(&config_pubkey, true, keys, &my_config);
|
||||
assert_eq!(
|
||||
process_instruction(&instruction.data, &keyed_accounts[0..1]),
|
||||
Err(InstructionError::MissingRequiredSignature)
|
||||
process_instruction(
|
||||
&instruction.data,
|
||||
vec![(config_pubkey, accounts[0].clone())],
|
||||
vec![AccountMeta {
|
||||
pubkey: config_pubkey,
|
||||
is_signer: true,
|
||||
is_writable: false,
|
||||
}],
|
||||
Err(InstructionError::MissingRequiredSignature),
|
||||
);
|
||||
}
|
||||
|
||||
@ -574,9 +805,11 @@ mod tests {
|
||||
let (_, _config_account) = create_config_account(vec![]);
|
||||
let instructions =
|
||||
config_instruction::create_account::<MyConfig>(&from_pubkey, &config_pubkey, 1, vec![]);
|
||||
assert_eq!(
|
||||
process_instruction(&instructions[1].data, &[]),
|
||||
Err(InstructionError::NotEnoughAccountKeys)
|
||||
process_instruction(
|
||||
&instructions[1].data,
|
||||
Vec::new(),
|
||||
Vec::new(),
|
||||
Err(InstructionError::NotEnoughAccountKeys),
|
||||
);
|
||||
}
|
||||
|
||||
@ -586,8 +819,8 @@ mod tests {
|
||||
let config_pubkey = Pubkey::new_unique();
|
||||
let new_config = MyConfig::new(84);
|
||||
let signer0_pubkey = Pubkey::new_unique();
|
||||
let signer0_account = AccountSharedData::new_ref(0, 0, &Pubkey::new_unique());
|
||||
let config_account = AccountSharedData::new_ref(0, 0, &Pubkey::new_unique());
|
||||
let signer0_account = AccountSharedData::new(0, 0, &Pubkey::new_unique());
|
||||
let config_account = AccountSharedData::new(0, 0, &Pubkey::new_unique());
|
||||
let (_, _config_account) = create_config_account(vec![]);
|
||||
let keys = vec![
|
||||
(from_pubkey, false),
|
||||
@ -596,13 +829,25 @@ mod tests {
|
||||
];
|
||||
|
||||
let instruction = config_instruction::store(&config_pubkey, true, keys, &new_config);
|
||||
let keyed_accounts = [
|
||||
(true, false, config_pubkey, config_account),
|
||||
(true, false, signer0_pubkey, signer0_account),
|
||||
];
|
||||
assert_eq!(
|
||||
process_instruction(&instruction.data, &keyed_accounts),
|
||||
Err(InstructionError::InvalidAccountOwner)
|
||||
process_instruction(
|
||||
&instruction.data,
|
||||
vec![
|
||||
(config_pubkey, config_account),
|
||||
(signer0_pubkey, signer0_account),
|
||||
],
|
||||
vec![
|
||||
AccountMeta {
|
||||
pubkey: config_pubkey,
|
||||
is_signer: true,
|
||||
is_writable: false,
|
||||
},
|
||||
AccountMeta {
|
||||
pubkey: signer0_pubkey,
|
||||
is_signer: true,
|
||||
is_writable: false,
|
||||
},
|
||||
],
|
||||
Err(InstructionError::InvalidAccountOwner),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user