Add message
This commit is contained in:
@ -2,8 +2,8 @@
|
|||||||
|
|
||||||
use crate::hash::Hash;
|
use crate::hash::Hash;
|
||||||
use crate::instruction::{CompiledInstruction, Instruction};
|
use crate::instruction::{CompiledInstruction, Instruction};
|
||||||
|
use crate::message::Message;
|
||||||
use crate::pubkey::Pubkey;
|
use crate::pubkey::Pubkey;
|
||||||
use crate::transaction::Transaction;
|
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
|
|
||||||
fn position(keys: &[Pubkey], key: &Pubkey) -> u8 {
|
fn position(keys: &[Pubkey], key: &Pubkey) -> u8 {
|
||||||
@ -81,14 +81,14 @@ impl InstructionCompiler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Return an unsigned transaction with space for requires signatures.
|
/// Return an unsigned transaction with space for requires signatures.
|
||||||
pub fn compile(&self) -> Transaction {
|
pub fn compile(&self) -> Message {
|
||||||
let program_ids = self.program_ids();
|
let program_ids = self.program_ids();
|
||||||
let (mut signed_keys, unsigned_keys) = self.keys();
|
let (mut signed_keys, unsigned_keys) = self.keys();
|
||||||
let signed_len = signed_keys.len();
|
let num_signatures = signed_keys.len() as u8;
|
||||||
signed_keys.extend(&unsigned_keys);
|
signed_keys.extend(&unsigned_keys);
|
||||||
let instructions = compile_instructions(&self.instructions, &signed_keys, &program_ids);
|
let instructions = compile_instructions(&self.instructions, &signed_keys, &program_ids);
|
||||||
Transaction {
|
Message {
|
||||||
signatures: Vec::with_capacity(signed_len),
|
num_signatures,
|
||||||
account_keys: signed_keys,
|
account_keys: signed_keys,
|
||||||
recent_blockhash: Hash::default(),
|
recent_blockhash: Hash::default(),
|
||||||
fee: 0,
|
fee: 0,
|
||||||
@ -211,12 +211,12 @@ mod tests {
|
|||||||
let program_id = Pubkey::default();
|
let program_id = Pubkey::default();
|
||||||
let id0 = Pubkey::default();
|
let id0 = Pubkey::default();
|
||||||
let ix = Instruction::new(program_id, &0, vec![AccountMeta::new(id0, false)]);
|
let ix = Instruction::new(program_id, &0, vec![AccountMeta::new(id0, false)]);
|
||||||
let tx = InstructionCompiler::new(vec![ix]).compile();
|
let message = InstructionCompiler::new(vec![ix]).compile();
|
||||||
assert_eq!(tx.signatures.capacity(), 0);
|
assert_eq!(message.num_signatures, 0);
|
||||||
|
|
||||||
let ix = Instruction::new(program_id, &0, vec![AccountMeta::new(id0, true)]);
|
let ix = Instruction::new(program_id, &0, vec![AccountMeta::new(id0, true)]);
|
||||||
let tx = InstructionCompiler::new(vec![ix]).compile();
|
let message = InstructionCompiler::new(vec![ix]).compile();
|
||||||
assert_eq!(tx.signatures.capacity(), 1);
|
assert_eq!(message.num_signatures, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -5,6 +5,7 @@ pub mod hash;
|
|||||||
pub mod instruction;
|
pub mod instruction;
|
||||||
mod instruction_compiler;
|
mod instruction_compiler;
|
||||||
pub mod loader_instruction;
|
pub mod loader_instruction;
|
||||||
|
mod message;
|
||||||
pub mod native_loader;
|
pub mod native_loader;
|
||||||
pub mod native_program;
|
pub mod native_program;
|
||||||
pub mod packet;
|
pub mod packet;
|
||||||
|
13
sdk/src/message.rs
Normal file
13
sdk/src/message.rs
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
use crate::hash::Hash;
|
||||||
|
use crate::instruction::CompiledInstruction;
|
||||||
|
use crate::pubkey::Pubkey;
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq, Eq, Clone)]
|
||||||
|
pub struct Message {
|
||||||
|
pub num_signatures: u8,
|
||||||
|
pub account_keys: Vec<Pubkey>,
|
||||||
|
pub recent_blockhash: Hash,
|
||||||
|
pub fee: u64,
|
||||||
|
pub program_ids: Vec<Pubkey>,
|
||||||
|
pub instructions: Vec<CompiledInstruction>,
|
||||||
|
}
|
@ -3,6 +3,7 @@
|
|||||||
use crate::hash::{Hash, Hasher};
|
use crate::hash::{Hash, Hasher};
|
||||||
use crate::instruction::{AccountMeta, CompiledInstruction, Instruction, InstructionError};
|
use crate::instruction::{AccountMeta, CompiledInstruction, Instruction, InstructionError};
|
||||||
use crate::instruction_compiler::InstructionCompiler;
|
use crate::instruction_compiler::InstructionCompiler;
|
||||||
|
use crate::message::Message;
|
||||||
use crate::packet::PACKET_DATA_SIZE;
|
use crate::packet::PACKET_DATA_SIZE;
|
||||||
use crate::pubkey::Pubkey;
|
use crate::pubkey::Pubkey;
|
||||||
use crate::shortvec::{deserialize_vec_with, encode_len, serialize_vec_with};
|
use crate::shortvec::{deserialize_vec_with, encode_len, serialize_vec_with};
|
||||||
@ -72,8 +73,20 @@ pub struct Transaction {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Transaction {
|
impl Transaction {
|
||||||
|
pub fn new_message(message: Message) -> Self {
|
||||||
|
Self {
|
||||||
|
signatures: Vec::with_capacity(message.num_signatures as usize),
|
||||||
|
account_keys: message.account_keys,
|
||||||
|
recent_blockhash: message.recent_blockhash,
|
||||||
|
fee: message.fee,
|
||||||
|
program_ids: message.program_ids,
|
||||||
|
instructions: message.instructions,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn new(instructions: Vec<Instruction>) -> Self {
|
pub fn new(instructions: Vec<Instruction>) -> Self {
|
||||||
InstructionCompiler::new(instructions).compile()
|
let message = InstructionCompiler::new(instructions).compile();
|
||||||
|
Self::new_message(message)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new_signed_instructions<T: KeypairUtil>(
|
pub fn new_signed_instructions<T: KeypairUtil>(
|
||||||
|
Reference in New Issue
Block a user