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

@ -21,7 +21,7 @@ impl FeeCalculator {
mod tests {
use super::*;
use crate::pubkey::Pubkey;
use crate::system_instruction::SystemInstruction;
use crate::system_instruction;
#[test]
fn test_fee_calculator_calculate_fee() {
@ -35,13 +35,13 @@ mod tests {
// One signature, a fee.
let pubkey0 = Pubkey::new(&[0; 32]);
let pubkey1 = Pubkey::new(&[1; 32]);
let ix0 = SystemInstruction::new_transfer(&pubkey0, &pubkey1, 1);
let ix0 = system_instruction::transfer(&pubkey0, &pubkey1, 1);
let message = Message::new(vec![ix0]);
assert_eq!(FeeCalculator::new(2).calculate_fee(&message), 2);
// Two signatures, double the fee.
let ix0 = SystemInstruction::new_transfer(&pubkey0, &pubkey1, 1);
let ix1 = SystemInstruction::new_transfer(&pubkey1, &pubkey0, 1);
let ix0 = system_instruction::transfer(&pubkey0, &pubkey1, 1);
let ix1 = system_instruction::transfer(&pubkey1, &pubkey0, 1);
let message = Message::new(vec![ix0, ix1]);
assert_eq!(FeeCalculator::new(2).calculate_fee(&message), 4);
}

View File

@ -20,23 +20,16 @@ pub enum LoaderInstruction {
Finalize,
}
impl LoaderInstruction {
pub fn new_write(
account_id: &Pubkey,
program_id: &Pubkey,
offset: u32,
bytes: Vec<u8>,
) -> Instruction {
let account_metas = vec![AccountMeta::new(*account_id, true)];
Instruction::new(
*program_id,
&LoaderInstruction::Write { offset, bytes },
account_metas,
)
}
pub fn new_finalize(account_id: &Pubkey, program_id: &Pubkey) -> Instruction {
let account_metas = vec![AccountMeta::new(*account_id, true)];
Instruction::new(*program_id, &LoaderInstruction::Finalize, account_metas)
}
pub fn write(account_id: &Pubkey, program_id: &Pubkey, offset: u32, bytes: Vec<u8>) -> Instruction {
let account_metas = vec![AccountMeta::new(*account_id, true)];
Instruction::new(
*program_id,
&LoaderInstruction::Write { offset, bytes },
account_metas,
)
}
pub fn finalize(account_id: &Pubkey, program_id: &Pubkey) -> Instruction {
let account_metas = vec![AccountMeta::new(*account_id, true)];
Instruction::new(*program_id, &LoaderInstruction::Finalize, account_metas)
}

View File

@ -31,65 +31,63 @@ pub enum SystemInstruction {
Transfer { lamports: u64 },
}
impl SystemInstruction {
pub fn new_account(
from_id: &Pubkey,
to_id: &Pubkey,
lamports: u64,
space: u64,
program_id: &Pubkey,
) -> Instruction {
let account_metas = vec![
AccountMeta::new(*from_id, true),
AccountMeta::new(*to_id, false),
];
Instruction::new(
system_program::id(),
&SystemInstruction::CreateAccount {
lamports,
space,
program_id: *program_id,
},
account_metas,
)
}
pub fn create_account(
from_id: &Pubkey,
to_id: &Pubkey,
lamports: u64,
space: u64,
program_id: &Pubkey,
) -> Instruction {
let account_metas = vec![
AccountMeta::new(*from_id, true),
AccountMeta::new(*to_id, false),
];
Instruction::new(
system_program::id(),
&SystemInstruction::CreateAccount {
lamports,
space,
program_id: *program_id,
},
account_metas,
)
}
/// Create and sign a transaction to create a system account
pub fn new_user_account(from_id: &Pubkey, to_id: &Pubkey, lamports: u64) -> Instruction {
let program_id = system_program::id();
Self::new_account(from_id, to_id, lamports, 0, &program_id)
}
/// Create and sign a transaction to create a system account
pub fn create_user_account(from_id: &Pubkey, to_id: &Pubkey, lamports: u64) -> Instruction {
let program_id = system_program::id();
create_account(from_id, to_id, lamports, 0, &program_id)
}
pub fn new_assign(from_id: &Pubkey, program_id: &Pubkey) -> Instruction {
let account_metas = vec![AccountMeta::new(*from_id, true)];
Instruction::new(
system_program::id(),
&SystemInstruction::Assign {
program_id: *program_id,
},
account_metas,
)
}
pub fn assign(from_id: &Pubkey, program_id: &Pubkey) -> Instruction {
let account_metas = vec![AccountMeta::new(*from_id, true)];
Instruction::new(
system_program::id(),
&SystemInstruction::Assign {
program_id: *program_id,
},
account_metas,
)
}
pub fn new_transfer(from_id: &Pubkey, to_id: &Pubkey, lamports: u64) -> Instruction {
let account_metas = vec![
AccountMeta::new(*from_id, true),
AccountMeta::new(*to_id, false),
];
Instruction::new(
system_program::id(),
&SystemInstruction::Transfer { lamports },
account_metas,
)
}
pub fn transfer(from_id: &Pubkey, to_id: &Pubkey, lamports: u64) -> Instruction {
let account_metas = vec![
AccountMeta::new(*from_id, true),
AccountMeta::new(*to_id, false),
];
Instruction::new(
system_program::id(),
&SystemInstruction::Transfer { lamports },
account_metas,
)
}
/// Create and sign new SystemInstruction::Transfer transaction to many destinations
pub fn new_transfer_many(from_id: &Pubkey, to_lamports: &[(Pubkey, u64)]) -> Vec<Instruction> {
to_lamports
.iter()
.map(|(to_id, lamports)| SystemInstruction::new_transfer(from_id, to_id, *lamports))
.collect()
}
/// Create and sign new SystemInstruction::Transfer transaction to many destinations
pub fn transfer_many(from_id: &Pubkey, to_lamports: &[(Pubkey, u64)]) -> Vec<Instruction> {
to_lamports
.iter()
.map(|(to_id, lamports)| transfer(from_id, to_id, *lamports))
.collect()
}
#[cfg(test)]
@ -107,7 +105,7 @@ mod tests {
let carol_pubkey = Pubkey::new_rand();
let to_lamports = vec![(bob_pubkey, 1), (carol_pubkey, 2)];
let instructions = SystemInstruction::new_transfer_many(&alice_pubkey, &to_lamports);
let instructions = transfer_many(&alice_pubkey, &to_lamports);
assert_eq!(instructions.len(), 2);
assert_eq!(get_keys(&instructions[0]), vec![alice_pubkey, bob_pubkey]);
assert_eq!(get_keys(&instructions[1]), vec![alice_pubkey, carol_pubkey]);

View File

@ -3,74 +3,70 @@
use crate::hash::Hash;
use crate::pubkey::Pubkey;
use crate::signature::{Keypair, KeypairUtil};
use crate::system_instruction::SystemInstruction;
use crate::system_instruction;
use crate::system_program;
use crate::transaction::Transaction;
pub struct SystemTransaction {}
impl SystemTransaction {
/// Create and sign new SystemInstruction::CreateAccount transaction
pub fn new_account(
from_keypair: &Keypair,
to: &Pubkey,
recent_blockhash: Hash,
lamports: u64,
space: u64,
program_id: &Pubkey,
_fee: u64,
) -> Transaction {
let from_pubkey = from_keypair.pubkey();
let create_instruction =
SystemInstruction::new_account(&from_pubkey, to, lamports, space, program_id);
let instructions = vec![create_instruction];
Transaction::new_signed_instructions(&[from_keypair], instructions, recent_blockhash)
}
/// Create and sign a transaction to create a system account
pub fn new_user_account(
from_keypair: &Keypair,
to: &Pubkey,
lamports: u64,
recent_blockhash: Hash,
fee: u64,
) -> Transaction {
let program_id = system_program::id();
Self::new_account(
from_keypair,
to,
recent_blockhash,
lamports,
0,
&program_id,
fee,
)
}
/// Create and sign new SystemInstruction::Assign transaction
pub fn new_assign(
from_keypair: &Keypair,
recent_blockhash: Hash,
program_id: &Pubkey,
_fee: u64,
) -> Transaction {
let from_pubkey = from_keypair.pubkey();
let assign_instruction = SystemInstruction::new_assign(&from_pubkey, program_id);
let instructions = vec![assign_instruction];
Transaction::new_signed_instructions(&[from_keypair], instructions, recent_blockhash)
}
/// Create and sign new SystemInstruction::Transfer transaction
pub fn new_transfer(
from_keypair: &Keypair,
to: &Pubkey,
lamports: u64,
recent_blockhash: Hash,
_fee: u64,
) -> Transaction {
let from_pubkey = from_keypair.pubkey();
let move_instruction = SystemInstruction::new_transfer(&from_pubkey, to, lamports);
let instructions = vec![move_instruction];
Transaction::new_signed_instructions(&[from_keypair], instructions, recent_blockhash)
}
/// Create and sign new SystemInstruction::CreateAccount transaction
pub fn create_account(
from_keypair: &Keypair,
to: &Pubkey,
recent_blockhash: Hash,
lamports: u64,
space: u64,
program_id: &Pubkey,
_fee: u64,
) -> Transaction {
let from_pubkey = from_keypair.pubkey();
let create_instruction =
system_instruction::create_account(&from_pubkey, to, lamports, space, program_id);
let instructions = vec![create_instruction];
Transaction::new_signed_instructions(&[from_keypair], instructions, recent_blockhash)
}
/// Create and sign a transaction to create a system account
pub fn create_user_account(
from_keypair: &Keypair,
to: &Pubkey,
lamports: u64,
recent_blockhash: Hash,
fee: u64,
) -> Transaction {
let program_id = system_program::id();
create_account(
from_keypair,
to,
recent_blockhash,
lamports,
0,
&program_id,
fee,
)
}
/// Create and sign new system_instruction::Assign transaction
pub fn assign(
from_keypair: &Keypair,
recent_blockhash: Hash,
program_id: &Pubkey,
_fee: u64,
) -> Transaction {
let from_pubkey = from_keypair.pubkey();
let assign_instruction = system_instruction::assign(&from_pubkey, program_id);
let instructions = vec![assign_instruction];
Transaction::new_signed_instructions(&[from_keypair], instructions, recent_blockhash)
}
/// Create and sign new system_instruction::Transfer transaction
pub fn transfer(
from_keypair: &Keypair,
to: &Pubkey,
lamports: u64,
recent_blockhash: Hash,
_fee: u64,
) -> Transaction {
let from_pubkey = from_keypair.pubkey();
let move_instruction = system_instruction::transfer(&from_pubkey, to, lamports);
let instructions = vec![move_instruction];
Transaction::new_signed_instructions(&[from_keypair], instructions, recent_blockhash)
}

View File

@ -200,7 +200,7 @@ mod tests {
use super::*;
use crate::instruction::AccountMeta;
use crate::signature::Keypair;
use crate::system_instruction::SystemInstruction;
use crate::system_instruction;
use bincode::{deserialize, serialize, serialized_size};
use std::mem::size_of;
@ -321,7 +321,7 @@ mod tests {
let alice_keypair = Keypair::new();
let alice_pubkey = alice_keypair.pubkey();
let bob_pubkey = Pubkey::new_rand();
let ix = SystemInstruction::new_transfer(&alice_pubkey, &bob_pubkey, 42);
let ix = system_instruction::transfer(&alice_pubkey, &bob_pubkey, 42);
let expected_data_size = size_of::<u32>() + size_of::<u64>();
assert_eq!(expected_data_size, 12);