Remove Instruction wrapper structs and name functions after enum fields

This commit is contained in:
Greg Fitzgerald
2019-04-03 09:45:57 -06:00
committed by Grimes
parent 867f6f107b
commit 35298e01a8
53 changed files with 835 additions and 922 deletions

View File

@ -2,36 +2,32 @@ use crate::id;
use crate::ConfigState;
use solana_sdk::instruction::{AccountMeta, Instruction};
use solana_sdk::pubkey::Pubkey;
use solana_sdk::system_instruction::SystemInstruction;
use solana_sdk::system_instruction;
pub struct ConfigInstruction {}
impl ConfigInstruction {
/// Create a new, empty configuration account
pub fn new_account<T: ConfigState>(
from_account_pubkey: &Pubkey,
config_account_pubkey: &Pubkey,
lamports: u64,
) -> Instruction {
SystemInstruction::new_account(
from_account_pubkey,
config_account_pubkey,
lamports,
T::max_space(),
&id(),
)
}
/// Store new data in a configuration account
pub fn new_store<T: ConfigState>(
from_account_pubkey: &Pubkey,
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)
}
/// Create a new, empty configuration account
pub fn create_account<T: ConfigState>(
from_account_pubkey: &Pubkey,
config_account_pubkey: &Pubkey,
lamports: u64,
) -> Instruction {
system_instruction::create_account(
from_account_pubkey,
config_account_pubkey,
lamports,
T::max_space(),
&id(),
)
}
/// Store new data in a configuration account
pub fn store<T: ConfigState>(
from_account_pubkey: &Pubkey,
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)
}

View File

@ -28,7 +28,7 @@ pub fn process_instruction(
#[cfg(test)]
mod tests {
use super::*;
use crate::{id, ConfigInstruction, ConfigState};
use crate::{config_instruction, id, ConfigState};
use bincode::{deserialize, serialized_size};
use serde_derive::{Deserialize, Serialize};
use solana_runtime::bank::Bank;
@ -36,7 +36,7 @@ mod tests {
use solana_sdk::genesis_block::GenesisBlock;
use solana_sdk::message::Message;
use solana_sdk::signature::{Keypair, KeypairUtil};
use solana_sdk::system_instruction::SystemInstruction;
use solana_sdk::system_instruction;
#[derive(Serialize, Deserialize, Default, Debug, PartialEq)]
struct MyConfig {
@ -78,7 +78,7 @@ mod tests {
bank_client
.process_instruction(
&mint_keypair,
ConfigInstruction::new_account::<MyConfig>(
config_instruction::create_account::<MyConfig>(
&mint_keypair.pubkey(),
&config_pubkey,
1,
@ -111,7 +111,7 @@ mod tests {
let my_config = MyConfig::new(42);
let instruction =
ConfigInstruction::new_store(&from_keypair.pubkey(), &config_pubkey, &my_config);
config_instruction::store(&from_keypair.pubkey(), &config_pubkey, &my_config);
let message = Message::new(vec![instruction]);
bank_client
.process_message(&[&from_keypair, &config_keypair], message)
@ -133,11 +133,8 @@ mod tests {
let my_config = MyConfig::new(42);
// Replace instruction data with a vector that's too large
let mut instruction = ConfigInstruction::new_store(
&from_keypair.pubkey(),
&config_keypair.pubkey(),
&my_config,
);
let mut instruction =
config_instruction::store(&from_keypair.pubkey(), &config_keypair.pubkey(), &my_config);
instruction.data = vec![0; 123];
let message = Message::new(vec![instruction]);
@ -155,14 +152,10 @@ mod tests {
bank.transfer(42, &mint_keypair, &system_pubkey).unwrap();
let (bank_client, from_keypair, config_keypair) = create_config_client(&bank, mint_keypair);
let move_instruction =
SystemInstruction::new_transfer(&system_pubkey, &Pubkey::default(), 42);
let move_instruction = system_instruction::transfer(&system_pubkey, &Pubkey::default(), 42);
let my_config = MyConfig::new(42);
let mut store_instruction = ConfigInstruction::new_store(
&from_keypair.pubkey(),
&config_keypair.pubkey(),
&my_config,
);
let mut store_instruction =
config_instruction::store(&from_keypair.pubkey(), &config_keypair.pubkey(), &my_config);
store_instruction.accounts[0].is_signer = false;
store_instruction.accounts[1].is_signer = false;

View File

@ -1,11 +1,9 @@
use serde::Serialize;
use solana_sdk::pubkey::Pubkey;
mod config_instruction;
pub mod config_instruction;
pub mod config_processor;
pub use config_instruction::ConfigInstruction;
const CONFIG_PROGRAM_ID: [u8; 32] = [
133, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0,