Config instructions now only require one key (#4258)

This commit is contained in:
Michael Vines 2019-05-12 22:47:12 -07:00 committed by GitHub
parent 426d06b89b
commit 8542006259
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 45 deletions

View File

@ -6,6 +6,7 @@ use indicatif::{ProgressBar, ProgressStyle};
use ring::digest::{Context, Digest, SHA256}; use ring::digest::{Context, Digest, SHA256};
use solana_client::rpc_client::RpcClient; use solana_client::rpc_client::RpcClient;
use solana_config_api::config_instruction; use solana_config_api::config_instruction;
use solana_sdk::message::Message;
use solana_sdk::pubkey::Pubkey; use solana_sdk::pubkey::Pubkey;
use solana_sdk::signature::{read_keypair, Keypair, KeypairUtil, Signable}; use solana_sdk::signature::{read_keypair, Keypair, KeypairUtil, Signable};
use solana_sdk::transaction::Transaction; use solana_sdk::transaction::Transaction;
@ -220,13 +221,14 @@ fn store_update_manifest(
) -> Result<(), Box<dyn std::error::Error>> { ) -> Result<(), Box<dyn std::error::Error>> {
let recect_blockhash = rpc_client.get_recent_blockhash()?; let recect_blockhash = rpc_client.get_recent_blockhash()?;
let new_store = config_instruction::store::<SignedUpdateManifest>( let signers = [from_keypair, update_manifest_keypair];
&from_keypair.pubkey(), let instruction = config_instruction::store::<SignedUpdateManifest>(
&update_manifest_keypair.pubkey(), &update_manifest_keypair.pubkey(),
update_manifest, update_manifest,
); );
let mut transaction = Transaction::new_unsigned_instructions(vec![new_store]);
transaction.sign(&[from_keypair, update_manifest_keypair], recect_blockhash); let message = Message::new_with_payer(vec![instruction], Some(&from_keypair.pubkey()));
let mut transaction = Transaction::new(&signers, message, recect_blockhash);
rpc_client.send_and_confirm_transaction(&mut transaction, &[from_keypair])?; rpc_client.send_and_confirm_transaction(&mut transaction, &[from_keypair])?;
Ok(()) Ok(())
} }

View File

@ -20,14 +20,7 @@ pub fn create_account<T: ConfigState>(
} }
/// Store new data in a configuration account /// Store new data in a configuration account
pub fn store<T: ConfigState>( pub fn store<T: ConfigState>(config_account_pubkey: &Pubkey, data: &T) -> Instruction {
from_account_pubkey: &Pubkey, let account_metas = vec![AccountMeta::new(*config_account_pubkey, true)];
config_account_pubkey: &Pubkey,
data: &T,
) -> Instruction {
let account_metas = vec![
AccountMeta::new(*from_account_pubkey, true),
AccountMeta::new(*config_account_pubkey, true),
];
Instruction::new(id(), data, account_metas) Instruction::new(id(), data, account_metas)
} }

View File

@ -11,17 +11,17 @@ pub fn process_instruction(
data: &[u8], data: &[u8],
_tick_height: u64, _tick_height: u64,
) -> Result<(), InstructionError> { ) -> Result<(), InstructionError> {
if keyed_accounts[1].signer_key().is_none() { if keyed_accounts[0].signer_key().is_none() {
error!("account[1] should sign the transaction"); error!("account[0].signer_key().is_none()");
Err(InstructionError::MissingRequiredSignature)?; Err(InstructionError::MissingRequiredSignature)?;
} }
if keyed_accounts[1].account.data.len() < data.len() { if keyed_accounts[0].account.data.len() < data.len() {
error!("instruction data too large"); error!("instruction data too large");
Err(InstructionError::InvalidInstructionData)?; Err(InstructionError::InvalidInstructionData)?;
} }
keyed_accounts[1].account.data[0..data.len()].copy_from_slice(data); keyed_accounts[0].account.data[0..data.len()].copy_from_slice(data);
Ok(()) Ok(())
} }
@ -65,17 +65,11 @@ mod tests {
(bank, mint_keypair) (bank, mint_keypair)
} }
fn create_config_client(bank: Bank, mint_keypair: Keypair) -> (BankClient, Keypair, Keypair) { fn create_config_account(bank: Bank, mint_keypair: Keypair) -> (BankClient, Keypair) {
let from_keypair = Keypair::new();
let from_pubkey = from_keypair.pubkey();
let config_keypair = Keypair::new(); let config_keypair = Keypair::new();
let config_pubkey = config_keypair.pubkey(); let config_pubkey = config_keypair.pubkey();
let bank_client = BankClient::new(bank); let bank_client = BankClient::new(bank);
bank_client
.transfer(42, &mint_keypair, &from_pubkey)
.unwrap();
bank_client bank_client
.send_instruction( .send_instruction(
&mint_keypair, &mint_keypair,
@ -87,14 +81,14 @@ mod tests {
) )
.expect("new_account"); .expect("new_account");
(bank_client, from_keypair, config_keypair) (bank_client, config_keypair)
} }
#[test] #[test]
fn test_process_create_ok() { fn test_process_create_ok() {
solana_logger::setup(); solana_logger::setup();
let (bank, from_keypair) = create_bank(10_000); let (bank, mint_keypair) = create_bank(10_000);
let (bank_client, _, config_keypair) = create_config_client(bank, from_keypair); let (bank_client, config_keypair) = create_config_account(bank, mint_keypair);
let config_account_data = bank_client let config_account_data = bank_client
.get_account_data(&config_keypair.pubkey()) .get_account_data(&config_keypair.pubkey())
.unwrap() .unwrap()
@ -109,15 +103,15 @@ mod tests {
fn test_process_store_ok() { fn test_process_store_ok() {
solana_logger::setup(); solana_logger::setup();
let (bank, mint_keypair) = create_bank(10_000); let (bank, mint_keypair) = create_bank(10_000);
let (bank_client, from_keypair, config_keypair) = create_config_client(bank, mint_keypair); let (bank_client, config_keypair) = create_config_account(bank, mint_keypair);
let config_pubkey = config_keypair.pubkey(); let config_pubkey = config_keypair.pubkey();
let my_config = MyConfig::new(42); let my_config = MyConfig::new(42);
let instruction =
config_instruction::store(&from_keypair.pubkey(), &config_pubkey, &my_config); let instruction = config_instruction::store(&config_pubkey, &my_config);
let message = Message::new(vec![instruction]); let message = Message::new(vec![instruction]);
bank_client bank_client
.send_message(&[&from_keypair, &config_keypair], message) .send_message(&[&config_keypair], message)
.unwrap(); .unwrap();
let config_account_data = bank_client let config_account_data = bank_client
@ -134,39 +128,36 @@ mod tests {
fn test_process_store_fail_instruction_data_too_large() { fn test_process_store_fail_instruction_data_too_large() {
solana_logger::setup(); solana_logger::setup();
let (bank, mint_keypair) = create_bank(10_000); let (bank, mint_keypair) = create_bank(10_000);
let (bank_client, from_keypair, config_keypair) = create_config_client(bank, mint_keypair); let (bank_client, config_keypair) = create_config_account(bank, mint_keypair);
let config_pubkey = config_keypair.pubkey();
let my_config = MyConfig::new(42); let my_config = MyConfig::new(42);
// Replace instruction data with a vector that's too large let mut instruction = config_instruction::store(&config_pubkey, &my_config);
let mut instruction = instruction.data = vec![0; 123]; // <-- Replace data with a vector that's too large
config_instruction::store(&from_keypair.pubkey(), &config_keypair.pubkey(), &my_config);
instruction.data = vec![0; 123];
let message = Message::new(vec![instruction]); let message = Message::new(vec![instruction]);
bank_client bank_client
.send_message(&[&from_keypair, &config_keypair], message) .send_message(&[&config_keypair], message)
.unwrap_err(); .unwrap_err();
} }
#[test] #[test]
fn test_process_store_fail_account1_not_signer() { fn test_process_store_fail_account0_not_signer() {
solana_logger::setup(); solana_logger::setup();
let (bank, mint_keypair) = create_bank(10_000); let (bank, mint_keypair) = create_bank(10_000);
let system_keypair = Keypair::new(); let system_keypair = Keypair::new();
let system_pubkey = system_keypair.pubkey(); let system_pubkey = system_keypair.pubkey();
bank.transfer(42, &mint_keypair, &system_pubkey).unwrap(); bank.transfer(42, &mint_keypair, &system_pubkey).unwrap();
let (bank_client, from_keypair, config_keypair) = create_config_client(bank, mint_keypair); let (bank_client, config_keypair) = create_config_account(bank, mint_keypair);
let config_pubkey = config_keypair.pubkey();
let transfer_instruction = let transfer_instruction =
system_instruction::transfer(&system_pubkey, &Pubkey::default(), 42); system_instruction::transfer(&system_pubkey, &Pubkey::default(), 42);
let my_config = MyConfig::new(42); let my_config = MyConfig::new(42);
let mut store_instruction = let mut store_instruction = config_instruction::store(&config_pubkey, &my_config);
config_instruction::store(&from_keypair.pubkey(), &config_keypair.pubkey(), &my_config); store_instruction.accounts[0].is_signer = false; // <----- not a signer
store_instruction.accounts[0].is_signer = false;
store_instruction.accounts[1].is_signer = false;
// Don't sign the transaction with `config_client`
let message = Message::new(vec![transfer_instruction, store_instruction]); let message = Message::new(vec![transfer_instruction, store_instruction]);
bank_client bank_client
.send_message(&[&system_keypair], message) .send_message(&[&system_keypair], message)