Rename TransactionCompiler to Script and use it to replace the type alias
This commit is contained in:
@ -9,6 +9,7 @@ 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;
|
||||
@ -16,7 +17,6 @@ pub mod system_program;
|
||||
pub mod system_transaction;
|
||||
pub mod timing;
|
||||
pub mod transaction;
|
||||
mod transaction_compiler;
|
||||
|
||||
#[macro_use]
|
||||
extern crate serde_derive;
|
||||
|
@ -1,4 +1,4 @@
|
||||
//! A library for composing transactions.
|
||||
//! A library for building scripts and compiling them into transactions.
|
||||
|
||||
use crate::hash::Hash;
|
||||
use crate::pubkey::Pubkey;
|
||||
@ -33,11 +33,11 @@ fn compile_instructions(
|
||||
}
|
||||
|
||||
/// A utility for constructing transactions
|
||||
pub struct TransactionCompiler {
|
||||
pub struct Script {
|
||||
instructions: Vec<Instruction>,
|
||||
}
|
||||
|
||||
impl TransactionCompiler {
|
||||
impl Script {
|
||||
/// Create a new unsigned transaction from a single instruction
|
||||
pub fn new(instructions: Vec<Instruction>) -> Self {
|
||||
Self { instructions }
|
||||
@ -100,7 +100,7 @@ mod tests {
|
||||
#[test]
|
||||
fn test_transaction_builder_unique_program_ids() {
|
||||
let program_id0 = Pubkey::default();
|
||||
let program_ids = TransactionCompiler::new(vec![
|
||||
let program_ids = Script::new(vec![
|
||||
Instruction::new(program_id0, &0, vec![]),
|
||||
Instruction::new(program_id0, &0, vec![]),
|
||||
])
|
||||
@ -112,7 +112,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 = TransactionCompiler::new(vec![
|
||||
let program_ids = Script::new(vec![
|
||||
Instruction::new(program_id0, &0, vec![]),
|
||||
Instruction::new(program_id1, &0, vec![]),
|
||||
Instruction::new(program_id0, &0, vec![]),
|
||||
@ -125,7 +125,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 = TransactionCompiler::new(vec![
|
||||
let program_ids = Script::new(vec![
|
||||
Instruction::new(program_id0, &0, vec![]),
|
||||
Instruction::new(program_id1, &0, vec![]),
|
||||
Instruction::new(program_id0, &0, vec![]),
|
||||
@ -138,7 +138,7 @@ mod tests {
|
||||
fn test_transaction_builder_unique_keys_both_signed() {
|
||||
let program_id = Pubkey::default();
|
||||
let id0 = Pubkey::default();
|
||||
let keys = TransactionCompiler::new(vec![
|
||||
let keys = Script::new(vec![
|
||||
Instruction::new(program_id, &0, vec![(id0, true)]),
|
||||
Instruction::new(program_id, &0, vec![(id0, true)]),
|
||||
])
|
||||
@ -150,7 +150,7 @@ mod tests {
|
||||
fn test_transaction_builder_unique_keys_one_signed() {
|
||||
let program_id = Pubkey::default();
|
||||
let id0 = Pubkey::default();
|
||||
let keys = TransactionCompiler::new(vec![
|
||||
let keys = Script::new(vec![
|
||||
Instruction::new(program_id, &0, vec![(id0, false)]),
|
||||
Instruction::new(program_id, &0, vec![(id0, true)]),
|
||||
])
|
||||
@ -163,7 +163,7 @@ mod tests {
|
||||
let program_id = Pubkey::default();
|
||||
let id0 = Keypair::new().pubkey();
|
||||
let id1 = Pubkey::default(); // Key less than id0
|
||||
let keys = TransactionCompiler::new(vec![
|
||||
let keys = Script::new(vec![
|
||||
Instruction::new(program_id, &0, vec![(id0, false)]),
|
||||
Instruction::new(program_id, &0, vec![(id1, false)]),
|
||||
])
|
||||
@ -176,7 +176,7 @@ mod tests {
|
||||
let program_id = Pubkey::default();
|
||||
let id0 = Pubkey::default();
|
||||
let id1 = Keypair::new().pubkey();
|
||||
let keys = TransactionCompiler::new(vec![
|
||||
let keys = Script::new(vec![
|
||||
Instruction::new(program_id, &0, vec![(id0, false)]),
|
||||
Instruction::new(program_id, &0, vec![(id1, false)]),
|
||||
Instruction::new(program_id, &0, vec![(id0, true)]),
|
||||
@ -190,7 +190,7 @@ mod tests {
|
||||
let program_id = Pubkey::default();
|
||||
let id0 = Pubkey::default();
|
||||
let id1 = Keypair::new().pubkey();
|
||||
let keys = TransactionCompiler::new(vec![
|
||||
let keys = Script::new(vec![
|
||||
Instruction::new(program_id, &0, vec![(id0, false)]),
|
||||
Instruction::new(program_id, &0, vec![(id1, true)]),
|
||||
])
|
||||
@ -203,14 +203,10 @@ mod tests {
|
||||
fn test_transaction_builder_signed_keys_len() {
|
||||
let program_id = Pubkey::default();
|
||||
let id0 = Pubkey::default();
|
||||
let tx =
|
||||
TransactionCompiler::new(vec![Instruction::new(program_id, &0, vec![(id0, false)])])
|
||||
.compile();
|
||||
let tx = Script::new(vec![Instruction::new(program_id, &0, vec![(id0, false)])]).compile();
|
||||
assert_eq!(tx.signatures.capacity(), 0);
|
||||
|
||||
let tx =
|
||||
TransactionCompiler::new(vec![Instruction::new(program_id, &0, vec![(id0, true)])])
|
||||
.compile();
|
||||
let tx = Script::new(vec![Instruction::new(program_id, &0, vec![(id0, true)])]).compile();
|
||||
assert_eq!(tx.signatures.capacity(), 1);
|
||||
}
|
||||
|
||||
@ -221,7 +217,7 @@ mod tests {
|
||||
let id0 = Pubkey::default();
|
||||
let keypair1 = Keypair::new();
|
||||
let id1 = keypair1.pubkey();
|
||||
let tx = TransactionCompiler::new(vec![
|
||||
let tx = Script::new(vec![
|
||||
Instruction::new(program_id0, &0, vec![(id0, false)]),
|
||||
Instruction::new(program_id1, &0, vec![(id1, true)]),
|
||||
Instruction::new(program_id0, &0, vec![(id1, false)]),
|
@ -4,13 +4,13 @@ use crate::hash::{Hash, Hasher};
|
||||
use crate::native_program::ProgramError;
|
||||
use crate::packet::PACKET_DATA_SIZE;
|
||||
use crate::pubkey::Pubkey;
|
||||
use crate::script::Script;
|
||||
use crate::shortvec::{
|
||||
deserialize_vec_bytes, deserialize_vec_with, encode_len, serialize_vec_bytes,
|
||||
serialize_vec_with,
|
||||
};
|
||||
use crate::signature::{KeypairUtil, Signature};
|
||||
use crate::system_instruction::SystemError;
|
||||
use crate::transaction_compiler::TransactionCompiler;
|
||||
use bincode::{serialize, Error};
|
||||
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
|
||||
use serde::{Deserialize, Serialize, Serializer};
|
||||
@ -71,10 +71,7 @@ impl<P, Q> GenericInstruction<P, Q> {
|
||||
}
|
||||
|
||||
pub type Instruction = GenericInstruction<Pubkey, (Pubkey, bool)>;
|
||||
pub type Script = Vec<Instruction>;
|
||||
|
||||
pub type CompiledInstruction = GenericInstruction<u8, u8>;
|
||||
pub type CompiledScript = Vec<CompiledInstruction>;
|
||||
|
||||
impl CompiledInstruction {
|
||||
pub fn serialize_with(mut writer: &mut Cursor<&mut [u8]>, ix: &Self) -> Result<(), Error> {
|
||||
@ -166,12 +163,12 @@ pub struct Transaction {
|
||||
pub program_ids: Vec<Pubkey>,
|
||||
/// Programs that will be executed in sequence and committed in one atomic transaction if all
|
||||
/// succeed.
|
||||
pub instructions: CompiledScript,
|
||||
pub instructions: Vec<CompiledInstruction>,
|
||||
}
|
||||
|
||||
impl Transaction {
|
||||
pub fn new(script: Script) -> Self {
|
||||
TransactionCompiler::new(script).compile()
|
||||
pub fn new(instructions: Vec<Instruction>) -> Self {
|
||||
Script::new(instructions).compile()
|
||||
}
|
||||
|
||||
pub fn new_with_blockhash_and_fee<T: Serialize>(
|
||||
@ -227,7 +224,7 @@ impl Transaction {
|
||||
recent_blockhash: Hash,
|
||||
fee: u64,
|
||||
program_ids: Vec<Pubkey>,
|
||||
instructions: CompiledScript,
|
||||
instructions: Vec<CompiledInstruction>,
|
||||
) -> Self {
|
||||
let mut account_keys: Vec<_> = from_keypairs
|
||||
.iter()
|
||||
@ -469,7 +466,7 @@ impl<'a> serde::de::Visitor<'a> for TransactionVisitor {
|
||||
let program_ids: Vec<Pubkey> =
|
||||
deserialize_vec_with(&mut rd, Transaction::deserialize_pubkey)
|
||||
.map_err(Error::custom)?;
|
||||
let instructions: CompiledScript =
|
||||
let instructions: Vec<CompiledInstruction> =
|
||||
deserialize_vec_with(&mut rd, CompiledInstruction::deserialize_from)
|
||||
.map_err(Error::custom)?;
|
||||
Ok(Transaction {
|
||||
|
Reference in New Issue
Block a user