update config_api with initialization and recovery utilities (#5523)
* update config_api with initialization and recovery utilities * nits * move tests to config_tests to eliminate config_api solana_runtime dependency * fixups
This commit is contained in:
@@ -1,27 +1,15 @@
|
||||
use crate::id;
|
||||
use crate::ConfigState;
|
||||
use bincode::serialize;
|
||||
use serde_derive::{Deserialize, Serialize};
|
||||
use solana_sdk::instruction::{AccountMeta, Instruction};
|
||||
use solana_sdk::pubkey::Pubkey;
|
||||
use solana_sdk::short_vec;
|
||||
use solana_sdk::system_instruction;
|
||||
use crate::{ConfigKeys, ConfigState};
|
||||
use solana_sdk::{
|
||||
instruction::{AccountMeta, Instruction},
|
||||
pubkey::Pubkey,
|
||||
system_instruction,
|
||||
};
|
||||
|
||||
/// A collection of keys to be stored in Config account data.
|
||||
#[derive(Debug, Default, Deserialize, Serialize)]
|
||||
pub struct ConfigKeys {
|
||||
// Each key tuple comprises a unique `Pubkey` identifier,
|
||||
// and `bool` whether that key is a signer of the data
|
||||
#[serde(with = "short_vec")]
|
||||
pub keys: Vec<(Pubkey, bool)>,
|
||||
}
|
||||
|
||||
impl ConfigKeys {
|
||||
pub fn serialized_size(keys: Vec<(Pubkey, bool)>) -> usize {
|
||||
serialize(&ConfigKeys { keys })
|
||||
.unwrap_or_else(|_| vec![])
|
||||
.len()
|
||||
}
|
||||
fn initialize_account<T: ConfigState>(config_pubkey: &Pubkey) -> Instruction {
|
||||
let account_metas = vec![AccountMeta::new(*config_pubkey, true)];
|
||||
let account_data = (ConfigKeys { keys: vec![] }, T::default());
|
||||
Instruction::new(id(), &account_data, account_metas)
|
||||
}
|
||||
|
||||
/// Create a new, empty configuration account
|
||||
@@ -30,15 +18,18 @@ pub fn create_account<T: ConfigState>(
|
||||
config_account_pubkey: &Pubkey,
|
||||
lamports: u64,
|
||||
keys: Vec<(Pubkey, bool)>,
|
||||
) -> Instruction {
|
||||
let space = T::max_space() + ConfigKeys::serialized_size(keys) as u64;
|
||||
system_instruction::create_account(
|
||||
from_account_pubkey,
|
||||
config_account_pubkey,
|
||||
lamports,
|
||||
space,
|
||||
&id(),
|
||||
)
|
||||
) -> Vec<Instruction> {
|
||||
let space = T::max_space() + ConfigKeys::serialized_size(keys);
|
||||
vec![
|
||||
system_instruction::create_account(
|
||||
from_account_pubkey,
|
||||
config_account_pubkey,
|
||||
lamports,
|
||||
space,
|
||||
&id(),
|
||||
),
|
||||
initialize_account::<T>(config_account_pubkey),
|
||||
]
|
||||
}
|
||||
|
||||
/// Store new data in a configuration account
|
||||
|
Reference in New Issue
Block a user