Revert "Rename programs to instruction_processors (#3789)" (#3824)

This reverts commit 34344982a9.
This commit is contained in:
Greg Fitzgerald
2019-04-17 15:05:49 -06:00
committed by GitHub
parent 083090817a
commit 51a2988bb2
93 changed files with 44 additions and 44 deletions

View File

@ -0,0 +1,25 @@
[package]
name = "solana-config-api"
version = "0.14.0"
description = "config program API"
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.1.3"
log = "0.4.2"
serde = "1.0.90"
serde_derive = "1.0.90"
solana-logger = { path = "../../logger", version = "0.14.0" }
solana-sdk = { path = "../../sdk", version = "0.14.0" }
[dev-dependencies]
solana-runtime = { path = "../../runtime", version = "0.14.0" }
[lib]
name = "solana_config_api"
crate-type = ["lib"]

View File

@ -0,0 +1,33 @@
use crate::id;
use crate::ConfigState;
use solana_sdk::instruction::{AccountMeta, Instruction};
use solana_sdk::pubkey::Pubkey;
use solana_sdk::system_instruction;
/// 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

@ -0,0 +1,174 @@
//! Config program
use log::*;
use solana_sdk::account::KeyedAccount;
use solana_sdk::instruction::InstructionError;
use solana_sdk::pubkey::Pubkey;
pub fn process_instruction(
_program_id: &Pubkey,
keyed_accounts: &mut [KeyedAccount],
data: &[u8],
_tick_height: u64,
) -> Result<(), InstructionError> {
if keyed_accounts[1].signer_key().is_none() {
error!("account[1] should sign the transaction");
Err(InstructionError::MissingRequiredSignature)?;
}
if keyed_accounts[1].account.data.len() < data.len() {
error!("instruction data too large");
Err(InstructionError::InvalidInstructionData)?;
}
keyed_accounts[1].account.data[0..data.len()].copy_from_slice(data);
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{config_instruction, id, ConfigState};
use bincode::{deserialize, serialized_size};
use serde_derive::{Deserialize, Serialize};
use solana_runtime::bank::Bank;
use solana_runtime::bank_client::BankClient;
use solana_sdk::client::SyncClient;
use solana_sdk::genesis_block::GenesisBlock;
use solana_sdk::message::Message;
use solana_sdk::signature::{Keypair, KeypairUtil};
use solana_sdk::system_instruction;
#[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) {
let (genesis_block, mint_keypair) = GenesisBlock::new(lamports);
let mut bank = Bank::new(&genesis_block);
bank.add_instruction_processor(id(), process_instruction);
(bank, mint_keypair)
}
fn create_config_client(bank: Bank, mint_keypair: Keypair) -> (BankClient, Keypair, Keypair) {
let from_keypair = Keypair::new();
let from_pubkey = from_keypair.pubkey();
let config_keypair = Keypair::new();
let config_pubkey = config_keypair.pubkey();
let bank_client = BankClient::new(bank);
bank_client
.transfer(42, &mint_keypair, &from_pubkey)
.unwrap();
bank_client
.send_instruction(
&mint_keypair,
config_instruction::create_account::<MyConfig>(
&mint_keypair.pubkey(),
&config_pubkey,
1,
),
)
.expect("new_account");
(bank_client, from_keypair, config_keypair)
}
#[test]
fn test_process_create_ok() {
solana_logger::setup();
let (bank, from_keypair) = create_bank(10_000);
let (bank_client, _, config_keypair) = create_config_client(bank, from_keypair);
let config_account_data = bank_client
.get_account_data(&config_keypair.pubkey())
.unwrap()
.unwrap();
assert_eq!(
MyConfig::default(),
MyConfig::deserialize(&config_account_data).unwrap()
);
}
#[test]
fn test_process_store_ok() {
solana_logger::setup();
let (bank, mint_keypair) = create_bank(10_000);
let (bank_client, from_keypair, config_keypair) = create_config_client(bank, mint_keypair);
let config_pubkey = config_keypair.pubkey();
let my_config = MyConfig::new(42);
let instruction =
config_instruction::store(&from_keypair.pubkey(), &config_pubkey, &my_config);
let message = Message::new(vec![instruction]);
bank_client
.send_message(&[&from_keypair, &config_keypair], message)
.unwrap();
let config_account_data = bank_client
.get_account_data(&config_pubkey)
.unwrap()
.unwrap();
assert_eq!(
my_config,
MyConfig::deserialize(&config_account_data).unwrap()
);
}
#[test]
fn test_process_store_fail_instruction_data_too_large() {
solana_logger::setup();
let (bank, mint_keypair) = create_bank(10_000);
let (bank_client, from_keypair, config_keypair) = create_config_client(bank, mint_keypair);
let my_config = MyConfig::new(42);
// Replace instruction data with a vector that's too large
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]);
bank_client
.send_message(&[&from_keypair, &config_keypair], message)
.unwrap_err();
}
#[test]
fn test_process_store_fail_account1_not_signer() {
solana_logger::setup();
let (bank, mint_keypair) = create_bank(10_000);
let system_keypair = Keypair::new();
let system_pubkey = system_keypair.pubkey();
bank.transfer(42, &mint_keypair, &system_pubkey).unwrap();
let (bank_client, from_keypair, config_keypair) = create_config_client(bank, mint_keypair);
let move_instruction = system_instruction::transfer(&system_pubkey, &Pubkey::default(), 42);
let my_config = MyConfig::new(42);
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;
// Don't sign the transaction with `config_client`
let message = Message::new(vec![move_instruction, store_instruction]);
bank_client
.send_message(&[&system_keypair], message)
.unwrap_err();
}
}

View File

@ -0,0 +1,23 @@
use serde::Serialize;
use solana_sdk::pubkey::Pubkey;
pub mod config_instruction;
pub mod config_processor;
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,
];
pub fn check_id(program_id: &Pubkey) -> bool {
program_id.as_ref() == CONFIG_PROGRAM_ID
}
pub fn id() -> Pubkey {
Pubkey::new(&CONFIG_PROGRAM_ID)
}
pub trait ConfigState: Serialize {
/// Maximum space that the serialized representation will require
fn max_space() -> u64;
}