Merge native programs parts into one unit (#7047)
This commit is contained in:
21
programs/config/Cargo.toml
Normal file
21
programs/config/Cargo.toml
Normal file
@@ -0,0 +1,21 @@
|
||||
[package]
|
||||
name = "solana-config-program"
|
||||
version = "0.21.0"
|
||||
description = "config program"
|
||||
authors = ["Solana Maintainers <maintainers@solana.com>"]
|
||||
repository = "https://github.com/solana-labs/solana"
|
||||
license = "Apache-2.0"
|
||||
homepage = "https://solana.com/"
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
bincode = "1.2.0"
|
||||
log = "0.4.8"
|
||||
serde = "1.0.102"
|
||||
serde_derive = "1.0.102"
|
||||
solana-logger = { path = "../../logger", version = "0.21.0" }
|
||||
solana-sdk = { path = "../../sdk", version = "0.21.0" }
|
||||
|
||||
[lib]
|
||||
crate-type = ["lib", "cdylib"]
|
||||
name = "solana_config_program"
|
50
programs/config/src/config_instruction.rs
Normal file
50
programs/config/src/config_instruction.rs
Normal file
@@ -0,0 +1,50 @@
|
||||
use crate::id;
|
||||
use crate::{ConfigKeys, ConfigState};
|
||||
use solana_sdk::{
|
||||
instruction::{AccountMeta, Instruction},
|
||||
pubkey::Pubkey,
|
||||
system_instruction,
|
||||
};
|
||||
|
||||
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
|
||||
pub fn create_account<T: ConfigState>(
|
||||
from_account_pubkey: &Pubkey,
|
||||
config_account_pubkey: &Pubkey,
|
||||
lamports: u64,
|
||||
keys: Vec<(Pubkey, bool)>,
|
||||
) -> 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
|
||||
pub fn store<T: ConfigState>(
|
||||
config_account_pubkey: &Pubkey,
|
||||
is_config_signer: bool,
|
||||
keys: Vec<(Pubkey, bool)>,
|
||||
data: &T,
|
||||
) -> Instruction {
|
||||
let mut account_metas = vec![AccountMeta::new(*config_account_pubkey, is_config_signer)];
|
||||
for (signer_pubkey, _) in keys.iter().filter(|(_, is_signer)| *is_signer) {
|
||||
if signer_pubkey != config_account_pubkey {
|
||||
account_metas.push(AccountMeta::new(*signer_pubkey, true));
|
||||
}
|
||||
}
|
||||
let account_data = (ConfigKeys { keys }, data);
|
||||
Instruction::new(id(), &account_data, account_metas)
|
||||
}
|
97
programs/config/src/config_processor.rs
Normal file
97
programs/config/src/config_processor.rs
Normal file
@@ -0,0 +1,97 @@
|
||||
//! Config program
|
||||
|
||||
use crate::ConfigKeys;
|
||||
use bincode::deserialize;
|
||||
use log::*;
|
||||
use solana_sdk::account::KeyedAccount;
|
||||
use solana_sdk::instruction::InstructionError;
|
||||
use solana_sdk::instruction_processor_utils::{limited_deserialize, next_keyed_account};
|
||||
use solana_sdk::pubkey::Pubkey;
|
||||
|
||||
pub fn process_instruction(
|
||||
_program_id: &Pubkey,
|
||||
keyed_accounts: &mut [KeyedAccount],
|
||||
data: &[u8],
|
||||
) -> Result<(), InstructionError> {
|
||||
let key_list: ConfigKeys = limited_deserialize(data)?;
|
||||
let keyed_accounts_iter = &mut keyed_accounts.iter_mut();
|
||||
let config_keyed_account = &mut next_keyed_account(keyed_accounts_iter)?;
|
||||
let current_data: ConfigKeys =
|
||||
deserialize(&config_keyed_account.account.data).map_err(|err| {
|
||||
error!("Invalid data in account[0]: {:?} {:?}", data, err);
|
||||
InstructionError::InvalidAccountData
|
||||
})?;
|
||||
let current_signer_keys: Vec<Pubkey> = current_data
|
||||
.keys
|
||||
.iter()
|
||||
.filter(|(_, is_signer)| *is_signer)
|
||||
.map(|(pubkey, _)| *pubkey)
|
||||
.collect();
|
||||
|
||||
if current_signer_keys.is_empty() {
|
||||
// Config account keypair must be a signer on account initialization,
|
||||
// or when no signers specified in Config data
|
||||
if config_keyed_account.signer_key().is_none() {
|
||||
error!("account[0].signer_key().is_none()");
|
||||
return Err(InstructionError::MissingRequiredSignature);
|
||||
}
|
||||
}
|
||||
|
||||
let mut counter = 0;
|
||||
for (signer, _) in key_list.keys.iter().filter(|(_, is_signer)| *is_signer) {
|
||||
counter += 1;
|
||||
if signer != config_keyed_account.unsigned_key() {
|
||||
let signer_account = keyed_accounts_iter.next();
|
||||
if signer_account.is_none() {
|
||||
error!("account {:?} is not in account list", signer);
|
||||
return Err(InstructionError::MissingRequiredSignature);
|
||||
}
|
||||
let signer_key = signer_account.unwrap().signer_key();
|
||||
if signer_key.is_none() {
|
||||
error!("account {:?} signer_key().is_none()", signer);
|
||||
return Err(InstructionError::MissingRequiredSignature);
|
||||
}
|
||||
if signer_key.unwrap() != signer {
|
||||
error!(
|
||||
"account[{:?}].signer_key() does not match Config data)",
|
||||
counter + 1
|
||||
);
|
||||
return Err(InstructionError::MissingRequiredSignature);
|
||||
}
|
||||
// If Config account is already initialized, update signatures must match Config data
|
||||
if !current_data.keys.is_empty()
|
||||
&& current_signer_keys
|
||||
.iter()
|
||||
.find(|&pubkey| pubkey == signer)
|
||||
.is_none()
|
||||
{
|
||||
error!("account {:?} is not in stored signer list", signer);
|
||||
return Err(InstructionError::MissingRequiredSignature);
|
||||
}
|
||||
} else if config_keyed_account.signer_key().is_none() {
|
||||
error!("account[0].signer_key().is_none()");
|
||||
return Err(InstructionError::MissingRequiredSignature);
|
||||
}
|
||||
}
|
||||
|
||||
// Check for Config data signers not present in incoming account update
|
||||
if current_signer_keys.len() > counter {
|
||||
error!(
|
||||
"too few signers: {:?}; expected: {:?}",
|
||||
counter,
|
||||
current_signer_keys.len()
|
||||
);
|
||||
return Err(InstructionError::MissingRequiredSignature);
|
||||
}
|
||||
|
||||
if config_keyed_account.account.data.len() < data.len() {
|
||||
error!("instruction data too large");
|
||||
return Err(InstructionError::InvalidInstructionData);
|
||||
}
|
||||
|
||||
config_keyed_account.account.data[0..data.len()].copy_from_slice(&data);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {}
|
61
programs/config/src/lib.rs
Normal file
61
programs/config/src/lib.rs
Normal file
@@ -0,0 +1,61 @@
|
||||
pub mod config_instruction;
|
||||
pub mod config_processor;
|
||||
|
||||
use crate::config_processor::process_instruction;
|
||||
use bincode::{deserialize, serialize, serialized_size};
|
||||
use serde_derive::{Deserialize, Serialize};
|
||||
use solana_sdk::{account::Account, pubkey::Pubkey, short_vec};
|
||||
|
||||
const CONFIG_PROGRAM_ID: [u8; 32] = [
|
||||
3, 6, 74, 163, 0, 47, 116, 220, 200, 110, 67, 49, 15, 12, 5, 42, 248, 197, 218, 39, 246, 16,
|
||||
64, 25, 163, 35, 239, 160, 0, 0, 0, 0,
|
||||
];
|
||||
|
||||
solana_sdk::declare_program!(
|
||||
CONFIG_PROGRAM_ID,
|
||||
"Config1111111111111111111111111111111111111",
|
||||
solana_config_program,
|
||||
process_instruction
|
||||
);
|
||||
|
||||
pub trait ConfigState: serde::Serialize + Default {
|
||||
/// Maximum space that the serialized representation will require
|
||||
fn max_space() -> u64;
|
||||
}
|
||||
|
||||
/// 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)>) -> u64 {
|
||||
serialized_size(&ConfigKeys { keys }).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_config_data(bytes: &[u8]) -> Result<&[u8], bincode::Error> {
|
||||
deserialize::<ConfigKeys>(bytes)
|
||||
.and_then(|keys| serialized_size(&keys))
|
||||
.map(|offset| &bytes[offset as usize..])
|
||||
}
|
||||
|
||||
// utility for pre-made Accounts
|
||||
pub fn create_config_account<T: ConfigState>(
|
||||
keys: Vec<(Pubkey, bool)>,
|
||||
config_data: &T,
|
||||
lamports: u64,
|
||||
) -> Account {
|
||||
let mut data = serialize(&ConfigKeys { keys }).unwrap();
|
||||
data.extend_from_slice(&serialize(config_data).unwrap());
|
||||
Account {
|
||||
lamports,
|
||||
data,
|
||||
owner: id(),
|
||||
..Account::default()
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user