Remove `KeyedAccount` in builtin program "config" (#24347)
* Makes sure that there is only one KeyedAccount at a time. * Replaces KeyedAccount by BorrowedAccount in config. * Removes unused code.
This commit is contained in:
parent
d2c6c04d3e
commit
1ffbb91a62
|
@ -5,35 +5,35 @@ use {
|
||||||
bincode::deserialize,
|
bincode::deserialize,
|
||||||
solana_program_runtime::{ic_msg, invoke_context::InvokeContext},
|
solana_program_runtime::{ic_msg, invoke_context::InvokeContext},
|
||||||
solana_sdk::{
|
solana_sdk::{
|
||||||
account::{ReadableAccount, WritableAccount},
|
feature_set, instruction::InstructionError, program_utils::limited_deserialize,
|
||||||
feature_set,
|
|
||||||
instruction::InstructionError,
|
|
||||||
keyed_account::keyed_account_at_index,
|
|
||||||
program_utils::limited_deserialize,
|
|
||||||
pubkey::Pubkey,
|
pubkey::Pubkey,
|
||||||
},
|
},
|
||||||
std::collections::BTreeSet,
|
std::collections::BTreeSet,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn process_instruction(
|
pub fn process_instruction(
|
||||||
first_instruction_account: usize,
|
_first_instruction_account: usize,
|
||||||
invoke_context: &mut InvokeContext,
|
invoke_context: &mut InvokeContext,
|
||||||
) -> Result<(), InstructionError> {
|
) -> Result<(), InstructionError> {
|
||||||
let transaction_context = &invoke_context.transaction_context;
|
let transaction_context = &invoke_context.transaction_context;
|
||||||
let instruction_context = transaction_context.get_current_instruction_context()?;
|
let instruction_context = transaction_context.get_current_instruction_context()?;
|
||||||
let data = instruction_context.get_instruction_data();
|
let data = instruction_context.get_instruction_data();
|
||||||
let keyed_accounts = invoke_context.get_keyed_accounts()?;
|
|
||||||
|
|
||||||
let key_list: ConfigKeys = limited_deserialize(data)?;
|
let key_list: ConfigKeys = limited_deserialize(data)?;
|
||||||
let config_keyed_account =
|
let config_account_key =
|
||||||
&mut keyed_account_at_index(keyed_accounts, first_instruction_account)?;
|
transaction_context
|
||||||
|
.get_key_of_account_at_index(instruction_context.get_index_in_transaction(
|
||||||
|
instruction_context.get_number_of_program_accounts(),
|
||||||
|
)?)?;
|
||||||
|
let config_account =
|
||||||
|
instruction_context.try_borrow_instruction_account(transaction_context, 0)?;
|
||||||
|
let is_config_account_signer = config_account.is_signer();
|
||||||
let current_data: ConfigKeys = {
|
let current_data: ConfigKeys = {
|
||||||
let config_account = config_keyed_account.try_account_ref_mut()?;
|
if config_account.get_owner() != &crate::id() {
|
||||||
if config_account.owner() != &crate::id() {
|
|
||||||
return Err(InstructionError::InvalidAccountOwner);
|
return Err(InstructionError::InvalidAccountOwner);
|
||||||
}
|
}
|
||||||
|
|
||||||
deserialize(config_account.data()).map_err(|err| {
|
deserialize(config_account.get_data()).map_err(|err| {
|
||||||
ic_msg!(
|
ic_msg!(
|
||||||
invoke_context,
|
invoke_context,
|
||||||
"Unable to deserialize config account: {}",
|
"Unable to deserialize config account: {}",
|
||||||
|
@ -42,17 +42,18 @@ pub fn process_instruction(
|
||||||
InstructionError::InvalidAccountData
|
InstructionError::InvalidAccountData
|
||||||
})?
|
})?
|
||||||
};
|
};
|
||||||
|
drop(config_account);
|
||||||
|
|
||||||
let current_signer_keys: Vec<Pubkey> = current_data
|
let current_signer_keys: Vec<Pubkey> = current_data
|
||||||
.keys
|
.keys
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|(_, is_signer)| *is_signer)
|
.filter(|(_, is_signer)| *is_signer)
|
||||||
.map(|(pubkey, _)| *pubkey)
|
.map(|(pubkey, _)| *pubkey)
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
if current_signer_keys.is_empty() {
|
if current_signer_keys.is_empty() {
|
||||||
// Config account keypair must be a signer on account initialization,
|
// Config account keypair must be a signer on account initialization,
|
||||||
// or when no signers specified in Config data
|
// or when no signers specified in Config data
|
||||||
if config_keyed_account.signer_key().is_none() {
|
if !is_config_account_signer {
|
||||||
return Err(InstructionError::MissingRequiredSignature);
|
return Err(InstructionError::MissingRequiredSignature);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -60,9 +61,10 @@ pub fn process_instruction(
|
||||||
let mut counter = 0;
|
let mut counter = 0;
|
||||||
for (signer, _) in key_list.keys.iter().filter(|(_, is_signer)| *is_signer) {
|
for (signer, _) in key_list.keys.iter().filter(|(_, is_signer)| *is_signer) {
|
||||||
counter += 1;
|
counter += 1;
|
||||||
if signer != config_keyed_account.unsigned_key() {
|
if signer != config_account_key {
|
||||||
let signer_account =
|
let signer_account = instruction_context
|
||||||
keyed_account_at_index(keyed_accounts, counter + 1).map_err(|_| {
|
.try_borrow_instruction_account(transaction_context, counter)
|
||||||
|
.map_err(|_| {
|
||||||
ic_msg!(
|
ic_msg!(
|
||||||
invoke_context,
|
invoke_context,
|
||||||
"account {:?} is not in account list",
|
"account {:?} is not in account list",
|
||||||
|
@ -70,8 +72,7 @@ pub fn process_instruction(
|
||||||
);
|
);
|
||||||
InstructionError::MissingRequiredSignature
|
InstructionError::MissingRequiredSignature
|
||||||
})?;
|
})?;
|
||||||
let signer_key = signer_account.signer_key();
|
if !signer_account.is_signer() {
|
||||||
if signer_key.is_none() {
|
|
||||||
ic_msg!(
|
ic_msg!(
|
||||||
invoke_context,
|
invoke_context,
|
||||||
"account {:?} signer_key().is_none()",
|
"account {:?} signer_key().is_none()",
|
||||||
|
@ -79,7 +80,7 @@ pub fn process_instruction(
|
||||||
);
|
);
|
||||||
return Err(InstructionError::MissingRequiredSignature);
|
return Err(InstructionError::MissingRequiredSignature);
|
||||||
}
|
}
|
||||||
if signer_key.unwrap() != signer {
|
if signer_account.get_key() != signer {
|
||||||
ic_msg!(
|
ic_msg!(
|
||||||
invoke_context,
|
invoke_context,
|
||||||
"account[{:?}].signer_key() does not match Config data)",
|
"account[{:?}].signer_key() does not match Config data)",
|
||||||
|
@ -98,7 +99,7 @@ pub fn process_instruction(
|
||||||
);
|
);
|
||||||
return Err(InstructionError::MissingRequiredSignature);
|
return Err(InstructionError::MissingRequiredSignature);
|
||||||
}
|
}
|
||||||
} else if config_keyed_account.signer_key().is_none() {
|
} else if !is_config_account_signer {
|
||||||
ic_msg!(invoke_context, "account[0].signer_key().is_none()");
|
ic_msg!(invoke_context, "account[0].signer_key().is_none()");
|
||||||
return Err(InstructionError::MissingRequiredSignature);
|
return Err(InstructionError::MissingRequiredSignature);
|
||||||
}
|
}
|
||||||
|
@ -127,15 +128,13 @@ pub fn process_instruction(
|
||||||
return Err(InstructionError::MissingRequiredSignature);
|
return Err(InstructionError::MissingRequiredSignature);
|
||||||
}
|
}
|
||||||
|
|
||||||
if config_keyed_account.data_len()? < data.len() {
|
let mut config_account =
|
||||||
|
instruction_context.try_borrow_instruction_account(transaction_context, 0)?;
|
||||||
|
if config_account.get_data().len() < data.len() {
|
||||||
ic_msg!(invoke_context, "instruction data too large");
|
ic_msg!(invoke_context, "instruction data too large");
|
||||||
return Err(InstructionError::InvalidInstructionData);
|
return Err(InstructionError::InvalidInstructionData);
|
||||||
}
|
}
|
||||||
|
config_account.get_data_mut()[..data.len()].copy_from_slice(data);
|
||||||
config_keyed_account
|
|
||||||
.try_account_ref_mut()?
|
|
||||||
.data_as_mut_slice()[..data.len()]
|
|
||||||
.copy_from_slice(data);
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -148,7 +147,7 @@ mod tests {
|
||||||
serde_derive::{Deserialize, Serialize},
|
serde_derive::{Deserialize, Serialize},
|
||||||
solana_program_runtime::invoke_context::mock_process_instruction,
|
solana_program_runtime::invoke_context::mock_process_instruction,
|
||||||
solana_sdk::{
|
solana_sdk::{
|
||||||
account::AccountSharedData,
|
account::{AccountSharedData, ReadableAccount},
|
||||||
instruction::AccountMeta,
|
instruction::AccountMeta,
|
||||||
pubkey::Pubkey,
|
pubkey::Pubkey,
|
||||||
signature::{Keypair, Signer},
|
signature::{Keypair, Signer},
|
||||||
|
|
Loading…
Reference in New Issue