Punt on the Script abstraction

Low ROI
This commit is contained in:
Greg Fitzgerald
2019-03-23 05:15:15 -06:00
parent c49e84c75b
commit b53cbdd9e6
17 changed files with 166 additions and 222 deletions

View File

@ -1,4 +1,4 @@
//! A library for building scripts and compiling them into transactions.
//! A library for compiling instructions
use crate::hash::Hash;
use crate::instruction::{CompiledInstruction, Instruction};
@ -39,20 +39,16 @@ fn compile_instructions(
}
/// A utility for constructing transactions
pub struct Script {
pub struct InstructionCompiler {
instructions: Vec<Instruction>,
}
impl Script {
impl InstructionCompiler {
/// Create a new unsigned transaction from a single instruction
pub fn new(instructions: Vec<Instruction>) -> Self {
Self { instructions }
}
pub fn push(&mut self, instruction: Instruction) {
self.instructions.push(instruction);
}
/// Return pubkeys referenced by all instructions, with the ones needing signatures first.
/// No duplicates and order is preserved.
fn keys(&self) -> (Vec<Pubkey>, Vec<Pubkey>) {
@ -111,7 +107,7 @@ mod tests {
#[test]
fn test_transaction_builder_unique_program_ids() {
let program_id0 = Pubkey::default();
let program_ids = Script::new(vec![
let program_ids = InstructionCompiler::new(vec![
Instruction::new(program_id0, &0, vec![]),
Instruction::new(program_id0, &0, vec![]),
])
@ -123,7 +119,7 @@ mod tests {
fn test_transaction_builder_unique_program_ids_not_adjacent() {
let program_id0 = Pubkey::default();
let program_id1 = Keypair::new().pubkey();
let program_ids = Script::new(vec![
let program_ids = InstructionCompiler::new(vec![
Instruction::new(program_id0, &0, vec![]),
Instruction::new(program_id1, &0, vec![]),
Instruction::new(program_id0, &0, vec![]),
@ -136,7 +132,7 @@ mod tests {
fn test_transaction_builder_unique_program_ids_order_preserved() {
let program_id0 = Keypair::new().pubkey();
let program_id1 = Pubkey::default(); // Key less than program_id0
let program_ids = Script::new(vec![
let program_ids = InstructionCompiler::new(vec![
Instruction::new(program_id0, &0, vec![]),
Instruction::new(program_id1, &0, vec![]),
Instruction::new(program_id0, &0, vec![]),
@ -149,7 +145,7 @@ mod tests {
fn test_transaction_builder_unique_keys_both_signed() {
let program_id = Pubkey::default();
let id0 = Pubkey::default();
let keys = Script::new(vec![
let keys = InstructionCompiler::new(vec![
Instruction::new(program_id, &0, vec![AccountMeta::new(id0, true)]),
Instruction::new(program_id, &0, vec![AccountMeta::new(id0, true)]),
])
@ -161,7 +157,7 @@ mod tests {
fn test_transaction_builder_unique_keys_one_signed() {
let program_id = Pubkey::default();
let id0 = Pubkey::default();
let keys = Script::new(vec![
let keys = InstructionCompiler::new(vec![
Instruction::new(program_id, &0, vec![AccountMeta::new(id0, false)]),
Instruction::new(program_id, &0, vec![AccountMeta::new(id0, true)]),
])
@ -174,7 +170,7 @@ mod tests {
let program_id = Pubkey::default();
let id0 = Keypair::new().pubkey();
let id1 = Pubkey::default(); // Key less than id0
let keys = Script::new(vec![
let keys = InstructionCompiler::new(vec![
Instruction::new(program_id, &0, vec![AccountMeta::new(id0, false)]),
Instruction::new(program_id, &0, vec![AccountMeta::new(id1, false)]),
])
@ -187,7 +183,7 @@ mod tests {
let program_id = Pubkey::default();
let id0 = Pubkey::default();
let id1 = Keypair::new().pubkey();
let keys = Script::new(vec![
let keys = InstructionCompiler::new(vec![
Instruction::new(program_id, &0, vec![AccountMeta::new(id0, false)]),
Instruction::new(program_id, &0, vec![AccountMeta::new(id1, false)]),
Instruction::new(program_id, &0, vec![AccountMeta::new(id0, true)]),
@ -201,7 +197,7 @@ mod tests {
let program_id = Pubkey::default();
let id0 = Pubkey::default();
let id1 = Keypair::new().pubkey();
let keys = Script::new(vec![
let keys = InstructionCompiler::new(vec![
Instruction::new(program_id, &0, vec![AccountMeta::new(id0, false)]),
Instruction::new(program_id, &0, vec![AccountMeta::new(id1, true)]),
])
@ -215,11 +211,11 @@ mod tests {
let program_id = Pubkey::default();
let id0 = Pubkey::default();
let ix = Instruction::new(program_id, &0, vec![AccountMeta::new(id0, false)]);
let tx = Script::new(vec![ix]).compile();
let tx = InstructionCompiler::new(vec![ix]).compile();
assert_eq!(tx.signatures.capacity(), 0);
let ix = Instruction::new(program_id, &0, vec![AccountMeta::new(id0, true)]);
let tx = Script::new(vec![ix]).compile();
let tx = InstructionCompiler::new(vec![ix]).compile();
assert_eq!(tx.signatures.capacity(), 1);
}
@ -230,7 +226,7 @@ mod tests {
let id0 = Pubkey::default();
let keypair1 = Keypair::new();
let id1 = keypair1.pubkey();
let tx = Script::new(vec![
let tx = InstructionCompiler::new(vec![
Instruction::new(program_id0, &0, vec![AccountMeta::new(id0, false)]),
Instruction::new(program_id1, &0, vec![AccountMeta::new(id1, true)]),
Instruction::new(program_id0, &0, vec![AccountMeta::new(id1, false)]),

View File

@ -3,13 +3,13 @@ pub mod bpf_loader;
pub mod genesis_block;
pub mod hash;
pub mod instruction;
mod instruction_compiler;
pub mod loader_instruction;
pub mod native_loader;
pub mod native_program;
pub mod packet;
pub mod pubkey;
pub mod rpc_port;
pub mod script;
pub mod shortvec;
pub mod signature;
pub mod system_instruction;

View File

@ -2,9 +2,9 @@
use crate::hash::{Hash, Hasher};
use crate::instruction::{AccountMeta, CompiledInstruction, Instruction, InstructionError};
use crate::instruction_compiler::InstructionCompiler;
use crate::packet::PACKET_DATA_SIZE;
use crate::pubkey::Pubkey;
use crate::script::Script;
use crate::shortvec::{deserialize_vec_with, encode_len, serialize_vec_with};
use crate::signature::{KeypairUtil, Signature};
use bincode::Error;
@ -73,7 +73,7 @@ pub struct Transaction {
impl Transaction {
pub fn new(instructions: Vec<Instruction>) -> Self {
Script::new(instructions).compile()
InstructionCompiler::new(instructions).compile()
}
pub fn new_signed_instructions<T: KeypairUtil>(