2019-03-14 07:55:41 -07:00
|
|
|
//! Config program
|
|
|
|
|
|
|
|
use log::*;
|
|
|
|
use solana_config_api::check_id;
|
|
|
|
use solana_sdk::account::KeyedAccount;
|
|
|
|
use solana_sdk::native_program::ProgramError;
|
|
|
|
use solana_sdk::pubkey::Pubkey;
|
|
|
|
use solana_sdk::solana_entrypoint;
|
|
|
|
|
|
|
|
fn process_instruction(
|
|
|
|
_program_id: &Pubkey,
|
|
|
|
keyed_accounts: &mut [KeyedAccount],
|
|
|
|
data: &[u8],
|
2019-03-16 22:06:53 -06:00
|
|
|
_tick_height: u64,
|
2019-03-14 07:55:41 -07:00
|
|
|
) -> Result<(), ProgramError> {
|
|
|
|
if !check_id(&keyed_accounts[0].account.owner) {
|
|
|
|
error!("account[0] is not assigned to the config program");
|
|
|
|
Err(ProgramError::IncorrectProgramId)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
if keyed_accounts[0].signer_key().is_none() {
|
|
|
|
error!("account[0] should sign the transaction");
|
|
|
|
Err(ProgramError::MissingRequiredSignature)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
if keyed_accounts[0].account.data.len() < data.len() {
|
|
|
|
error!("instruction data too large");
|
|
|
|
Err(ProgramError::InvalidInstructionData)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
keyed_accounts[0].account.data[0..data.len()].copy_from_slice(data);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
solana_entrypoint!(entrypoint);
|
|
|
|
fn entrypoint(
|
|
|
|
program_id: &Pubkey,
|
|
|
|
keyed_accounts: &mut [KeyedAccount],
|
|
|
|
data: &[u8],
|
2019-03-16 22:06:53 -06:00
|
|
|
tick_height: u64,
|
2019-03-14 07:55:41 -07:00
|
|
|
) -> Result<(), ProgramError> {
|
|
|
|
solana_logger::setup();
|
|
|
|
|
|
|
|
trace!("process_instruction: {:?}", data);
|
|
|
|
trace!("keyed_accounts: {:?}", keyed_accounts);
|
2019-03-16 22:06:53 -06:00
|
|
|
process_instruction(program_id, keyed_accounts, data, tick_height)
|
2019-03-14 07:55:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use bincode::{deserialize, serialized_size};
|
|
|
|
use serde_derive::{Deserialize, Serialize};
|
2019-03-16 22:06:53 -06:00
|
|
|
use solana_config_api::{id, ConfigInstruction, ConfigState};
|
|
|
|
use solana_runtime::bank::Bank;
|
|
|
|
use solana_runtime::bank_client::BankClient;
|
|
|
|
use solana_sdk::genesis_block::GenesisBlock;
|
2019-03-17 09:55:42 -06:00
|
|
|
use solana_sdk::script::Script;
|
2019-03-14 07:55:41 -07:00
|
|
|
use solana_sdk::signature::{Keypair, KeypairUtil};
|
|
|
|
use solana_sdk::system_instruction::SystemInstruction;
|
2019-03-17 09:55:42 -06:00
|
|
|
use solana_sdk::transaction::Instruction;
|
2019-03-14 07:55:41 -07: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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-16 22:06:53 -06:00
|
|
|
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_account_instruction(from_pubkey: &Pubkey, config_pubkey: &Pubkey) -> Instruction {
|
|
|
|
ConfigInstruction::new_account::<MyConfig>(&from_pubkey, &config_pubkey, 1)
|
2019-03-14 07:55:41 -07:00
|
|
|
}
|
|
|
|
|
2019-03-16 22:06:53 -06:00
|
|
|
fn create_config_client(bank: &Bank, from_keypair: Keypair) -> BankClient {
|
|
|
|
let from_client = BankClient::new(&bank, from_keypair);
|
|
|
|
let from_pubkey = from_client.pubkey();
|
|
|
|
let config_client = BankClient::new(&bank, Keypair::new());
|
|
|
|
let config_pubkey = config_client.pubkey();
|
|
|
|
|
|
|
|
let instruction = create_account_instruction(&from_pubkey, &config_pubkey);
|
2019-03-17 09:55:42 -06:00
|
|
|
from_client.process_instruction(instruction).unwrap();
|
2019-03-16 22:06:53 -06:00
|
|
|
|
|
|
|
config_client
|
2019-03-14 07:55:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_process_create_ok() {
|
|
|
|
solana_logger::setup();
|
2019-03-16 22:06:53 -06:00
|
|
|
let (bank, from_keypair) = create_bank(10_000);
|
|
|
|
let config_client = create_config_client(&bank, from_keypair);
|
|
|
|
let config_account = bank.get_account(&config_client.pubkey()).unwrap();
|
|
|
|
assert_eq!(id(), config_account.owner);
|
2019-03-14 07:55:41 -07:00
|
|
|
assert_eq!(
|
|
|
|
MyConfig::default(),
|
2019-03-16 22:06:53 -06:00
|
|
|
MyConfig::deserialize(&config_account.data).unwrap()
|
2019-03-14 07:55:41 -07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_process_store_ok() {
|
|
|
|
solana_logger::setup();
|
2019-03-16 22:06:53 -06:00
|
|
|
let (bank, from_keypair) = create_bank(10_000);
|
|
|
|
let config_client = create_config_client(&bank, from_keypair);
|
|
|
|
let config_pubkey = config_client.pubkey();
|
2019-03-14 07:55:41 -07:00
|
|
|
|
2019-03-16 22:06:53 -06:00
|
|
|
let my_config = MyConfig::new(42);
|
|
|
|
let instruction = ConfigInstruction::new_store(&config_pubkey, &my_config);
|
2019-03-17 09:55:42 -06:00
|
|
|
config_client.process_instruction(instruction).unwrap();
|
2019-03-14 07:55:41 -07:00
|
|
|
|
2019-03-16 22:06:53 -06:00
|
|
|
let config_account = bank.get_account(&config_pubkey).unwrap();
|
2019-03-14 07:55:41 -07:00
|
|
|
assert_eq!(
|
2019-03-16 22:06:53 -06:00
|
|
|
my_config,
|
|
|
|
MyConfig::deserialize(&config_account.data).unwrap()
|
2019-03-14 07:55:41 -07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_process_store_fail_instruction_data_too_large() {
|
|
|
|
solana_logger::setup();
|
2019-03-16 22:06:53 -06:00
|
|
|
let (bank, from_keypair) = create_bank(10_000);
|
|
|
|
let config_client = create_config_client(&bank, from_keypair);
|
|
|
|
let config_pubkey = config_client.pubkey();
|
2019-03-14 07:55:41 -07:00
|
|
|
|
2019-03-16 22:06:53 -06:00
|
|
|
let my_config = MyConfig::new(42);
|
|
|
|
let instruction = ConfigInstruction::new_store(&config_pubkey, &my_config);
|
2019-03-14 07:55:41 -07:00
|
|
|
|
|
|
|
// Replace instruction data with a vector that's too large
|
2019-03-17 09:55:42 -06:00
|
|
|
let script = Script::new(vec![instruction]);
|
|
|
|
let mut transaction = script.compile();
|
2019-03-14 07:55:41 -07:00
|
|
|
transaction.instructions[0].data = vec![0; 123];
|
2019-03-16 22:06:53 -06:00
|
|
|
config_client.process_transaction(transaction).unwrap_err();
|
2019-03-14 07:55:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_process_store_fail_account0_not_signer() {
|
|
|
|
solana_logger::setup();
|
2019-03-16 22:06:53 -06:00
|
|
|
let (bank, from_keypair) = create_bank(10_000);
|
|
|
|
let system_keypair = Keypair::new();
|
|
|
|
let system_pubkey = system_keypair.pubkey();
|
|
|
|
bank.transfer(42, &from_keypair, &system_pubkey, bank.last_blockhash())
|
|
|
|
.unwrap();
|
|
|
|
let config_client = create_config_client(&bank, from_keypair);
|
|
|
|
let config_pubkey = config_client.pubkey();
|
|
|
|
|
|
|
|
let move_instruction = SystemInstruction::new_move(&system_pubkey, &Pubkey::default(), 42);
|
|
|
|
let my_config = MyConfig::new(42);
|
|
|
|
let store_instruction = ConfigInstruction::new_store(&config_pubkey, &my_config);
|
|
|
|
|
|
|
|
// Don't sign the transaction with `config_client`
|
2019-03-17 09:55:42 -06:00
|
|
|
let script = Script::new(vec![move_instruction, store_instruction]);
|
|
|
|
let mut transaction = script.compile();
|
2019-03-16 22:06:53 -06:00
|
|
|
transaction.sign_unchecked(&[&system_keypair], bank.last_blockhash());
|
|
|
|
let system_client = BankClient::new(&bank, system_keypair);
|
|
|
|
system_client.process_transaction(transaction).unwrap_err();
|
2019-03-14 07:55:41 -07:00
|
|
|
}
|
|
|
|
}
|