2019-03-21 16:24:54 -06:00
|
|
|
//! Config program
|
|
|
|
|
2019-07-08 18:33:56 -05:00
|
|
|
use crate::config_instruction::ConfigKeys;
|
|
|
|
use bincode::deserialize;
|
2019-03-21 16:24:54 -06:00
|
|
|
use log::*;
|
|
|
|
use solana_sdk::account::KeyedAccount;
|
2019-03-23 21:12:27 -06:00
|
|
|
use solana_sdk::instruction::InstructionError;
|
2019-03-21 16:24:54 -06:00
|
|
|
use solana_sdk::pubkey::Pubkey;
|
|
|
|
|
|
|
|
pub fn process_instruction(
|
|
|
|
_program_id: &Pubkey,
|
|
|
|
keyed_accounts: &mut [KeyedAccount],
|
|
|
|
data: &[u8],
|
|
|
|
) -> Result<(), InstructionError> {
|
2019-07-09 13:37:18 -06:00
|
|
|
let key_list: ConfigKeys = deserialize(data).map_err(|err| {
|
|
|
|
error!("Invalid ConfigKeys data: {:?} {:?}", data, err);
|
|
|
|
InstructionError::InvalidInstructionData
|
|
|
|
})?;
|
2019-07-10 02:00:49 -06:00
|
|
|
|
|
|
|
let current_data: ConfigKeys = deserialize(&keyed_accounts[0].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 initilization,
|
|
|
|
// or when no signers specified in Config data
|
|
|
|
if keyed_accounts[0].signer_key().is_none() {
|
|
|
|
error!("account[0].signer_key().is_none()");
|
|
|
|
Err(InstructionError::MissingRequiredSignature)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-09 13:37:18 -06:00
|
|
|
let mut counter = 0;
|
2019-07-08 18:33:56 -05:00
|
|
|
for (i, (signer, _)) in key_list
|
|
|
|
.keys
|
|
|
|
.iter()
|
|
|
|
.filter(|(_, is_signer)| *is_signer)
|
|
|
|
.enumerate()
|
|
|
|
{
|
2019-07-09 13:37:18 -06:00
|
|
|
counter += 1;
|
2019-07-10 02:00:49 -06:00
|
|
|
if signer != keyed_accounts[0].unsigned_key() {
|
|
|
|
let account_index = i + 1;
|
|
|
|
let signer_account = keyed_accounts.get(account_index);
|
|
|
|
if signer_account.is_none() {
|
|
|
|
error!("account {:?} is not in account list", signer);
|
|
|
|
Err(InstructionError::MissingRequiredSignature)?;
|
|
|
|
}
|
|
|
|
let signer_key = signer_account.unwrap().signer_key();
|
|
|
|
if signer_key.is_none() {
|
|
|
|
error!("account {:?} signer_key().is_none()", signer);
|
|
|
|
Err(InstructionError::MissingRequiredSignature)?;
|
|
|
|
}
|
|
|
|
if signer_key.unwrap() != signer {
|
|
|
|
error!(
|
|
|
|
"account[{:?}].signer_key() does not match Config data)",
|
|
|
|
account_index
|
|
|
|
);
|
|
|
|
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);
|
|
|
|
Err(InstructionError::MissingRequiredSignature)?;
|
|
|
|
}
|
|
|
|
} else if keyed_accounts[0].signer_key().is_none() {
|
2019-07-09 13:37:18 -06:00
|
|
|
error!("account[0].signer_key().is_none()");
|
|
|
|
Err(InstructionError::MissingRequiredSignature)?;
|
|
|
|
}
|
|
|
|
}
|
2019-07-08 18:33:56 -05:00
|
|
|
|
2019-07-10 02:00:49 -06:00
|
|
|
// 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()
|
|
|
|
);
|
|
|
|
Err(InstructionError::MissingRequiredSignature)?;
|
|
|
|
}
|
|
|
|
|
2019-05-12 22:47:12 -07:00
|
|
|
if keyed_accounts[0].account.data.len() < data.len() {
|
2019-03-21 16:24:54 -06:00
|
|
|
error!("instruction data too large");
|
|
|
|
Err(InstructionError::InvalidInstructionData)?;
|
|
|
|
}
|
|
|
|
|
2019-07-08 18:33:56 -05:00
|
|
|
keyed_accounts[0].account.data[0..data.len()].copy_from_slice(&data);
|
2019-03-21 16:24:54 -06:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2019-04-03 09:45:57 -06:00
|
|
|
use crate::{config_instruction, id, ConfigState};
|
2019-03-21 16:24:54 -06:00
|
|
|
use bincode::{deserialize, serialized_size};
|
|
|
|
use serde_derive::{Deserialize, Serialize};
|
|
|
|
use solana_runtime::bank::Bank;
|
|
|
|
use solana_runtime::bank_client::BankClient;
|
2019-04-11 00:25:14 -07:00
|
|
|
use solana_sdk::client::SyncClient;
|
2019-05-07 11:16:22 -07:00
|
|
|
use solana_sdk::genesis_block::create_genesis_block;
|
2019-03-27 05:36:01 -06:00
|
|
|
use solana_sdk::message::Message;
|
2019-03-21 16:24:54 -06:00
|
|
|
use solana_sdk::signature::{Keypair, KeypairUtil};
|
2019-04-03 09:45:57 -06:00
|
|
|
use solana_sdk::system_instruction;
|
2019-03-21 16:24:54 -06:00
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize, Default, Debug, PartialEq)]
|
|
|
|
struct MyConfig {
|
|
|
|
pub item: u64,
|
|
|
|
}
|
|
|
|
impl MyConfig {
|
|
|
|
pub fn new(item: u64) -> Self {
|
|
|
|
Self { item }
|
|
|
|
}
|
|
|
|
pub fn deserialize(input: &[u8]) -> Option<Self> {
|
|
|
|
deserialize(input).ok()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ConfigState for MyConfig {
|
|
|
|
fn max_space() -> u64 {
|
|
|
|
serialized_size(&Self::default()).unwrap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create_bank(lamports: u64) -> (Bank, Keypair) {
|
2019-05-07 11:16:22 -07:00
|
|
|
let (genesis_block, mint_keypair) = create_genesis_block(lamports);
|
2019-03-21 16:24:54 -06:00
|
|
|
let mut bank = Bank::new(&genesis_block);
|
|
|
|
bank.add_instruction_processor(id(), process_instruction);
|
|
|
|
(bank, mint_keypair)
|
|
|
|
}
|
|
|
|
|
2019-07-08 18:33:56 -05:00
|
|
|
fn create_config_account(
|
|
|
|
bank: Bank,
|
|
|
|
mint_keypair: &Keypair,
|
|
|
|
keys: Vec<(Pubkey, bool)>,
|
|
|
|
) -> (BankClient, Keypair) {
|
2019-03-27 07:34:01 -06:00
|
|
|
let config_keypair = Keypair::new();
|
|
|
|
let config_pubkey = config_keypair.pubkey();
|
|
|
|
|
2019-04-11 11:29:59 -07:00
|
|
|
let bank_client = BankClient::new(bank);
|
2019-03-27 07:34:01 -06:00
|
|
|
bank_client
|
2019-04-03 15:11:08 -06:00
|
|
|
.send_instruction(
|
2019-05-17 20:17:50 -07:00
|
|
|
mint_keypair,
|
2019-04-03 09:45:57 -06:00
|
|
|
config_instruction::create_account::<MyConfig>(
|
2019-03-27 07:34:01 -06:00
|
|
|
&mint_keypair.pubkey(),
|
|
|
|
&config_pubkey,
|
|
|
|
1,
|
2019-07-08 18:33:56 -05:00
|
|
|
keys,
|
2019-03-27 07:34:01 -06:00
|
|
|
),
|
|
|
|
)
|
2019-03-21 16:24:54 -06:00
|
|
|
.expect("new_account");
|
|
|
|
|
2019-05-12 22:47:12 -07:00
|
|
|
(bank_client, config_keypair)
|
2019-03-21 16:24:54 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_process_create_ok() {
|
|
|
|
solana_logger::setup();
|
2019-05-12 22:47:12 -07:00
|
|
|
let (bank, mint_keypair) = create_bank(10_000);
|
2019-07-08 18:33:56 -05:00
|
|
|
let (bank_client, config_keypair) = create_config_account(bank, &mint_keypair, vec![]);
|
2019-04-11 11:29:59 -07:00
|
|
|
let config_account_data = bank_client
|
|
|
|
.get_account_data(&config_keypair.pubkey())
|
|
|
|
.unwrap()
|
|
|
|
.unwrap();
|
2019-03-21 16:24:54 -06:00
|
|
|
assert_eq!(
|
|
|
|
MyConfig::default(),
|
2019-04-11 11:29:59 -07:00
|
|
|
MyConfig::deserialize(&config_account_data).unwrap()
|
2019-03-21 16:24:54 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_process_store_ok() {
|
|
|
|
solana_logger::setup();
|
|
|
|
let (bank, mint_keypair) = create_bank(10_000);
|
2019-07-08 18:33:56 -05:00
|
|
|
let keys = vec![];
|
|
|
|
let (bank_client, config_keypair) =
|
|
|
|
create_config_account(bank, &mint_keypair, keys.clone());
|
2019-03-27 07:34:01 -06:00
|
|
|
let config_pubkey = config_keypair.pubkey();
|
2019-03-21 16:24:54 -06:00
|
|
|
|
|
|
|
let my_config = MyConfig::new(42);
|
2019-05-12 22:47:12 -07:00
|
|
|
|
2019-07-09 13:37:18 -06:00
|
|
|
let instruction = config_instruction::store(&config_pubkey, true, keys.clone(), &my_config);
|
2019-05-17 20:17:50 -07:00
|
|
|
let message = Message::new_with_payer(vec![instruction], Some(&mint_keypair.pubkey()));
|
2019-07-08 18:33:56 -05:00
|
|
|
|
2019-03-27 07:34:01 -06:00
|
|
|
bank_client
|
2019-05-17 20:17:50 -07:00
|
|
|
.send_message(&[&mint_keypair, &config_keypair], message)
|
2019-03-27 07:34:01 -06:00
|
|
|
.unwrap();
|
2019-03-21 16:24:54 -06:00
|
|
|
|
2019-04-03 21:40:29 -06:00
|
|
|
let config_account_data = bank_client
|
|
|
|
.get_account_data(&config_pubkey)
|
|
|
|
.unwrap()
|
|
|
|
.unwrap();
|
2019-07-08 18:33:56 -05:00
|
|
|
let meta_length = ConfigKeys::serialized_size(keys);
|
|
|
|
let config_account_data = &config_account_data[meta_length..config_account_data.len()];
|
2019-03-21 16:24:54 -06:00
|
|
|
assert_eq!(
|
|
|
|
my_config,
|
2019-04-03 16:36:10 -06:00
|
|
|
MyConfig::deserialize(&config_account_data).unwrap()
|
2019-03-21 16:24:54 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_process_store_fail_instruction_data_too_large() {
|
|
|
|
solana_logger::setup();
|
|
|
|
let (bank, mint_keypair) = create_bank(10_000);
|
2019-07-08 18:33:56 -05:00
|
|
|
let (bank_client, config_keypair) = create_config_account(bank, &mint_keypair, vec![]);
|
2019-05-12 22:47:12 -07:00
|
|
|
let config_pubkey = config_keypair.pubkey();
|
2019-03-21 16:24:54 -06:00
|
|
|
|
|
|
|
let my_config = MyConfig::new(42);
|
|
|
|
|
2019-07-09 13:37:18 -06:00
|
|
|
let mut instruction = config_instruction::store(&config_pubkey, true, vec![], &my_config);
|
2019-05-12 22:47:12 -07:00
|
|
|
instruction.data = vec![0; 123]; // <-- Replace data with a vector that's too large
|
2019-03-27 07:34:01 -06:00
|
|
|
let message = Message::new(vec![instruction]);
|
|
|
|
bank_client
|
2019-05-12 22:47:12 -07:00
|
|
|
.send_message(&[&config_keypair], message)
|
2019-03-27 07:34:01 -06:00
|
|
|
.unwrap_err();
|
2019-03-21 16:24:54 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-05-12 22:47:12 -07:00
|
|
|
fn test_process_store_fail_account0_not_signer() {
|
2019-03-21 16:24:54 -06:00
|
|
|
solana_logger::setup();
|
|
|
|
let (bank, mint_keypair) = create_bank(10_000);
|
|
|
|
let system_keypair = Keypair::new();
|
|
|
|
let system_pubkey = system_keypair.pubkey();
|
2019-05-12 22:47:12 -07:00
|
|
|
|
2019-03-27 05:59:30 -06:00
|
|
|
bank.transfer(42, &mint_keypair, &system_pubkey).unwrap();
|
2019-07-08 18:33:56 -05:00
|
|
|
let (bank_client, config_keypair) = create_config_account(bank, &mint_keypair, vec![]);
|
2019-05-12 22:47:12 -07:00
|
|
|
let config_pubkey = config_keypair.pubkey();
|
2019-03-21 16:24:54 -06:00
|
|
|
|
2019-04-23 13:30:42 -06:00
|
|
|
let transfer_instruction =
|
2019-05-23 18:19:53 -04:00
|
|
|
system_instruction::transfer(&system_pubkey, &Pubkey::new_rand(), 42);
|
2019-03-21 16:24:54 -06:00
|
|
|
let my_config = MyConfig::new(42);
|
2019-07-09 13:37:18 -06:00
|
|
|
let mut store_instruction =
|
|
|
|
config_instruction::store(&config_pubkey, true, vec![], &my_config);
|
2019-05-12 22:47:12 -07:00
|
|
|
store_instruction.accounts[0].is_signer = false; // <----- not a signer
|
2019-03-21 16:24:54 -06:00
|
|
|
|
2019-04-23 13:30:42 -06:00
|
|
|
let message = Message::new(vec![transfer_instruction, store_instruction]);
|
2019-03-27 07:34:01 -06:00
|
|
|
bank_client
|
2019-04-03 15:11:08 -06:00
|
|
|
.send_message(&[&system_keypair], message)
|
2019-03-27 07:34:01 -06:00
|
|
|
.unwrap_err();
|
2019-03-21 16:24:54 -06:00
|
|
|
}
|
2019-07-08 18:33:56 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_process_store_with_additional_signers() {
|
|
|
|
solana_logger::setup();
|
|
|
|
let (bank, mint_keypair) = create_bank(10_000);
|
|
|
|
let pubkey = Pubkey::new_rand();
|
|
|
|
let signer0 = Keypair::new();
|
|
|
|
let signer1 = Keypair::new();
|
|
|
|
let keys = vec![
|
|
|
|
(pubkey, false),
|
|
|
|
(signer0.pubkey(), true),
|
|
|
|
(signer1.pubkey(), true),
|
|
|
|
];
|
|
|
|
let (bank_client, config_keypair) =
|
|
|
|
create_config_account(bank, &mint_keypair, keys.clone());
|
|
|
|
let config_pubkey = config_keypair.pubkey();
|
|
|
|
|
|
|
|
let my_config = MyConfig::new(42);
|
|
|
|
|
2019-07-09 13:37:18 -06:00
|
|
|
let instruction = config_instruction::store(&config_pubkey, true, keys.clone(), &my_config);
|
2019-07-08 18:33:56 -05:00
|
|
|
let message = Message::new_with_payer(vec![instruction], Some(&mint_keypair.pubkey()));
|
|
|
|
|
|
|
|
bank_client
|
|
|
|
.send_message(
|
|
|
|
&[&mint_keypair, &config_keypair, &signer0, &signer1],
|
|
|
|
message,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let config_account_data = bank_client
|
|
|
|
.get_account_data(&config_pubkey)
|
|
|
|
.unwrap()
|
|
|
|
.unwrap();
|
|
|
|
let meta_length = ConfigKeys::serialized_size(keys.clone());
|
|
|
|
let meta_data: ConfigKeys = deserialize(&config_account_data[0..meta_length]).unwrap();
|
|
|
|
assert_eq!(meta_data.keys, keys);
|
|
|
|
let config_account_data = &config_account_data[meta_length..config_account_data.len()];
|
|
|
|
assert_eq!(
|
|
|
|
my_config,
|
|
|
|
MyConfig::deserialize(&config_account_data).unwrap()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-07-09 13:37:18 -06:00
|
|
|
#[test]
|
|
|
|
fn test_process_store_without_config_signer() {
|
|
|
|
solana_logger::setup();
|
|
|
|
let (bank, mint_keypair) = create_bank(10_000);
|
|
|
|
let pubkey = Pubkey::new_rand();
|
|
|
|
let signer0 = Keypair::new();
|
|
|
|
let keys = vec![(pubkey, false), (signer0.pubkey(), true)];
|
|
|
|
let (bank_client, config_keypair) =
|
|
|
|
create_config_account(bank, &mint_keypair, keys.clone());
|
|
|
|
let config_pubkey = config_keypair.pubkey();
|
|
|
|
|
|
|
|
let my_config = MyConfig::new(42);
|
|
|
|
|
|
|
|
let instruction =
|
|
|
|
config_instruction::store(&config_pubkey, false, keys.clone(), &my_config);
|
|
|
|
let message = Message::new_with_payer(vec![instruction], Some(&mint_keypair.pubkey()));
|
|
|
|
|
|
|
|
bank_client
|
|
|
|
.send_message(&[&mint_keypair, &signer0], message)
|
2019-07-10 02:00:49 -06:00
|
|
|
.unwrap_err();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_process_store_with_bad_additional_signer() {
|
|
|
|
solana_logger::setup();
|
|
|
|
let (bank, mint_keypair) = create_bank(10_000);
|
|
|
|
let signer0 = Keypair::new();
|
|
|
|
let signer1 = Keypair::new();
|
|
|
|
let keys = vec![(signer0.pubkey(), true)];
|
|
|
|
let (bank_client, config_keypair) =
|
|
|
|
create_config_account(bank, &mint_keypair, keys.clone());
|
|
|
|
let config_pubkey = config_keypair.pubkey();
|
|
|
|
|
|
|
|
let my_config = MyConfig::new(42);
|
|
|
|
|
|
|
|
// Config-data pubkey doesn't match signer
|
|
|
|
let instruction = config_instruction::store(&config_pubkey, true, keys.clone(), &my_config);
|
|
|
|
let mut message =
|
|
|
|
Message::new_with_payer(vec![instruction.clone()], Some(&mint_keypair.pubkey()));
|
|
|
|
message.account_keys[2] = signer1.pubkey();
|
|
|
|
bank_client
|
|
|
|
.send_message(&[&mint_keypair, &config_keypair, &signer1], message)
|
|
|
|
.unwrap_err();
|
|
|
|
|
|
|
|
// Config-data pubkey not a signer
|
|
|
|
let mut message = Message::new_with_payer(vec![instruction], Some(&mint_keypair.pubkey()));
|
|
|
|
message.header.num_required_signatures = 2;
|
|
|
|
bank_client
|
|
|
|
.send_message(&[&mint_keypair, &config_keypair], message)
|
|
|
|
.unwrap_err();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_config_updates() {
|
|
|
|
solana_logger::setup();
|
|
|
|
let (bank, mint_keypair) = create_bank(10_000);
|
|
|
|
let pubkey = Pubkey::new_rand();
|
|
|
|
let signer0 = Keypair::new();
|
|
|
|
let signer1 = Keypair::new();
|
|
|
|
let keys = vec![
|
|
|
|
(pubkey, false),
|
|
|
|
(signer0.pubkey(), true),
|
|
|
|
(signer1.pubkey(), true),
|
|
|
|
];
|
|
|
|
let (bank_client, config_keypair) =
|
|
|
|
create_config_account(bank, &mint_keypair, keys.clone());
|
|
|
|
let config_pubkey = config_keypair.pubkey();
|
|
|
|
|
|
|
|
let my_config = MyConfig::new(42);
|
|
|
|
|
|
|
|
let instruction = config_instruction::store(&config_pubkey, true, keys.clone(), &my_config);
|
|
|
|
let message = Message::new_with_payer(vec![instruction], Some(&mint_keypair.pubkey()));
|
|
|
|
|
|
|
|
bank_client
|
|
|
|
.send_message(
|
|
|
|
&[&mint_keypair, &config_keypair, &signer0, &signer1],
|
|
|
|
message,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
// Update with expected signatures
|
|
|
|
let new_config = MyConfig::new(84);
|
|
|
|
let instruction =
|
|
|
|
config_instruction::store(&config_pubkey, false, keys.clone(), &new_config);
|
|
|
|
let message = Message::new_with_payer(vec![instruction], Some(&mint_keypair.pubkey()));
|
|
|
|
bank_client
|
|
|
|
.send_message(&[&mint_keypair, &signer0, &signer1], message)
|
2019-07-09 13:37:18 -06:00
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let config_account_data = bank_client
|
|
|
|
.get_account_data(&config_pubkey)
|
|
|
|
.unwrap()
|
|
|
|
.unwrap();
|
|
|
|
let meta_length = ConfigKeys::serialized_size(keys.clone());
|
|
|
|
let meta_data: ConfigKeys = deserialize(&config_account_data[0..meta_length]).unwrap();
|
|
|
|
assert_eq!(meta_data.keys, keys);
|
|
|
|
let config_account_data = &config_account_data[meta_length..config_account_data.len()];
|
|
|
|
assert_eq!(
|
2019-07-10 02:00:49 -06:00
|
|
|
new_config,
|
2019-07-09 13:37:18 -06:00
|
|
|
MyConfig::deserialize(&config_account_data).unwrap()
|
|
|
|
);
|
2019-07-10 02:00:49 -06:00
|
|
|
|
|
|
|
// Attempt update with incomplete signatures
|
|
|
|
let keys = vec![(pubkey, false), (signer0.pubkey(), true)];
|
|
|
|
let instruction =
|
|
|
|
config_instruction::store(&config_pubkey, false, keys.clone(), &my_config);
|
|
|
|
let message = Message::new_with_payer(vec![instruction], Some(&mint_keypair.pubkey()));
|
|
|
|
bank_client
|
|
|
|
.send_message(&[&mint_keypair, &signer0], message)
|
|
|
|
.unwrap_err();
|
|
|
|
|
|
|
|
// Attempt update with incorrect signatures
|
|
|
|
let signer2 = Keypair::new();
|
|
|
|
let keys = vec![
|
|
|
|
(pubkey, false),
|
|
|
|
(signer0.pubkey(), true),
|
|
|
|
(signer2.pubkey(), true),
|
|
|
|
];
|
|
|
|
let instruction =
|
|
|
|
config_instruction::store(&config_pubkey, false, keys.clone(), &my_config);
|
|
|
|
let message = Message::new_with_payer(vec![instruction], Some(&mint_keypair.pubkey()));
|
|
|
|
bank_client
|
|
|
|
.send_message(&[&mint_keypair, &signer0, &signer2], message)
|
|
|
|
.unwrap_err();
|
2019-07-09 13:37:18 -06:00
|
|
|
}
|
|
|
|
|
2019-07-08 18:33:56 -05:00
|
|
|
#[test]
|
2019-07-10 02:00:49 -06:00
|
|
|
fn test_config_updates_requiring_config() {
|
2019-07-08 18:33:56 -05:00
|
|
|
solana_logger::setup();
|
|
|
|
let (bank, mint_keypair) = create_bank(10_000);
|
2019-07-10 02:00:49 -06:00
|
|
|
let pubkey = Pubkey::new_rand();
|
2019-07-08 18:33:56 -05:00
|
|
|
let signer0 = Keypair::new();
|
2019-07-10 02:00:49 -06:00
|
|
|
let keys = vec![
|
|
|
|
(pubkey, false),
|
|
|
|
(signer0.pubkey(), true),
|
|
|
|
(signer0.pubkey(), true),
|
|
|
|
]; // Dummy keys for account sizing
|
2019-07-08 18:33:56 -05:00
|
|
|
let (bank_client, config_keypair) =
|
|
|
|
create_config_account(bank, &mint_keypair, keys.clone());
|
|
|
|
let config_pubkey = config_keypair.pubkey();
|
2019-07-10 02:00:49 -06:00
|
|
|
let keys = vec![
|
|
|
|
(pubkey, false),
|
|
|
|
(signer0.pubkey(), true),
|
|
|
|
(config_keypair.pubkey(), true),
|
|
|
|
];
|
2019-07-08 18:33:56 -05:00
|
|
|
|
|
|
|
let my_config = MyConfig::new(42);
|
|
|
|
|
2019-07-09 13:37:18 -06:00
|
|
|
let instruction = config_instruction::store(&config_pubkey, true, keys.clone(), &my_config);
|
2019-07-10 02:00:49 -06:00
|
|
|
let message = Message::new_with_payer(vec![instruction], Some(&mint_keypair.pubkey()));
|
|
|
|
|
2019-07-08 18:33:56 -05:00
|
|
|
bank_client
|
2019-07-10 02:00:49 -06:00
|
|
|
.send_message(&[&mint_keypair, &config_keypair, &signer0], message)
|
|
|
|
.unwrap();
|
2019-07-08 18:33:56 -05:00
|
|
|
|
2019-07-10 02:00:49 -06:00
|
|
|
// Update with expected signatures
|
|
|
|
let new_config = MyConfig::new(84);
|
|
|
|
let instruction =
|
|
|
|
config_instruction::store(&config_pubkey, true, keys.clone(), &new_config);
|
|
|
|
let message = Message::new_with_payer(vec![instruction], Some(&mint_keypair.pubkey()));
|
|
|
|
bank_client
|
|
|
|
.send_message(&[&mint_keypair, &config_keypair, &signer0], message)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let config_account_data = bank_client
|
|
|
|
.get_account_data(&config_pubkey)
|
|
|
|
.unwrap()
|
|
|
|
.unwrap();
|
|
|
|
let meta_length = ConfigKeys::serialized_size(keys.clone());
|
|
|
|
let meta_data: ConfigKeys = deserialize(&config_account_data[0..meta_length]).unwrap();
|
|
|
|
assert_eq!(meta_data.keys, keys);
|
|
|
|
let config_account_data = &config_account_data[meta_length..config_account_data.len()];
|
|
|
|
assert_eq!(
|
|
|
|
new_config,
|
|
|
|
MyConfig::deserialize(&config_account_data).unwrap()
|
|
|
|
);
|
|
|
|
|
|
|
|
// Attempt update with incomplete signatures
|
|
|
|
let keys = vec![(pubkey, false), (config_keypair.pubkey(), true)];
|
|
|
|
let instruction = config_instruction::store(&config_pubkey, true, keys.clone(), &my_config);
|
|
|
|
let message = Message::new_with_payer(vec![instruction], Some(&mint_keypair.pubkey()));
|
2019-07-08 18:33:56 -05:00
|
|
|
bank_client
|
|
|
|
.send_message(&[&mint_keypair, &config_keypair], message)
|
|
|
|
.unwrap_err();
|
|
|
|
}
|
2019-03-21 16:24:54 -06:00
|
|
|
}
|