Remove Transaction::new_signed

This commit is contained in:
Greg Fitzgerald 2019-03-26 20:31:14 -06:00 committed by Grimes
parent b9ff70c8ab
commit 5ce31168ef
3 changed files with 47 additions and 117 deletions

View File

@ -12,11 +12,11 @@ use serde_derive::{Deserialize, Serialize};
use solana_metrics;
use solana_metrics::influxdb;
use solana_sdk::hash::Hash;
use solana_sdk::message::Message;
use solana_sdk::packet::PACKET_DATA_SIZE;
use solana_sdk::pubkey::Pubkey;
use solana_sdk::signature::Keypair;
use solana_sdk::signature::{Keypair, KeypairUtil};
use solana_sdk::system_instruction::SystemInstruction;
use solana_sdk::system_program;
use solana_sdk::transaction::Transaction;
use std::io;
use std::io::{Error, ErrorKind};
@ -127,22 +127,10 @@ impl Drone {
info!("Requesting airdrop of {} to {:?}", lamports, to);
let create_instruction = SystemInstruction::CreateAccount {
lamports,
space: 0,
program_id: system_program::id(),
};
let mut transaction = Transaction::new_signed(
&self.mint_keypair,
&[to],
&system_program::id(),
&create_instruction,
blockhash,
0, /*fee*/
);
transaction.sign(&[&self.mint_keypair], blockhash);
Ok(transaction)
let create_instruction =
SystemInstruction::new_account(&self.mint_keypair.pubkey(), &to, lamports);
let message = Message::new(vec![create_instruction]);
Ok(Transaction::new(&[&self.mint_keypair], message, blockhash))
} else {
Err(Error::new(ErrorKind::Other, "token limit reached"))
}
@ -294,7 +282,6 @@ pub fn run_local_drone(mint_keypair: Keypair, sender: Sender<SocketAddr>) {
mod tests {
use super::*;
use bytes::BufMut;
use solana_sdk::signature::{Keypair, KeypairUtil};
use std::time::Duration;
#[test]
@ -369,7 +356,6 @@ mod tests {
assert_eq!(tx.signatures.len(), 1);
assert_eq!(tx.account_keys, vec![mint_pubkey, to]);
assert_eq!(tx.recent_blockhash, blockhash);
assert_eq!(tx.program_ids, vec![system_program::id()]);
assert_eq!(tx.instructions.len(), 1);
let instruction: SystemInstruction = deserialize(&tx.instructions[0].data).unwrap();
@ -403,20 +389,9 @@ mod tests {
bytes.put(&req[..]);
let keypair = Keypair::new();
let expected_instruction = SystemInstruction::CreateAccount {
lamports,
space: 0,
program_id: system_program::id(),
};
let mut expected_tx = Transaction::new_signed(
&keypair,
&[to],
&system_program::id(),
&expected_instruction,
blockhash,
0,
);
expected_tx.sign(&[&keypair], blockhash);
let expected_instruction = SystemInstruction::new_account(&keypair.pubkey(), &to, lamports);
let message = Message::new(vec![expected_instruction]);
let expected_tx = Transaction::new(&[&keypair], message, blockhash);
let expected_bytes = serialize(&expected_tx).unwrap();
let mut expected_vec_with_length = vec![0; 2];
LittleEndian::write_u16(&mut expected_vec_with_length, expected_bytes.len() as u16);

View File

@ -1,8 +1,8 @@
use solana_drone::drone::{request_airdrop_transaction, run_local_drone};
use solana_sdk::hash::Hash;
use solana_sdk::message::Message;
use solana_sdk::signature::{Keypair, KeypairUtil};
use solana_sdk::system_instruction::SystemInstruction;
use solana_sdk::system_program;
use solana_sdk::transaction::Transaction;
use std::sync::mpsc::channel;
@ -12,20 +12,9 @@ fn test_local_drone() {
let to = Keypair::new().pubkey();
let lamports = 50;
let blockhash = Hash::new(&to.as_ref());
let expected_instruction = SystemInstruction::CreateAccount {
lamports,
space: 0,
program_id: system_program::id(),
};
let mut expected_tx = Transaction::new_signed(
&keypair,
&[to],
&system_program::id(),
&expected_instruction,
blockhash,
0,
);
expected_tx.sign(&[&keypair], blockhash);
let create_instruction = SystemInstruction::new_account(&keypair.pubkey(), &to, lamports);
let message = Message::new(vec![create_instruction]);
let expected_tx = Transaction::new(&[&keypair], message, blockhash);
let (sender, receiver) = channel();
run_local_drone(keypair, sender);

View File

@ -1,7 +1,7 @@
//! Defines a Transaction type to package an atomic sequence of instructions.
use crate::hash::{Hash, Hasher};
use crate::instruction::{AccountMeta, CompiledInstruction, Instruction, InstructionError};
use crate::instruction::{CompiledInstruction, Instruction, InstructionError};
use crate::message::Message;
use crate::packet::PACKET_DATA_SIZE;
use crate::pubkey::Pubkey;
@ -109,22 +109,6 @@ impl Transaction {
Self::new(from_keypairs, message, recent_blockhash)
}
pub fn new_signed<S: Serialize, T: KeypairUtil>(
from_keypair: &T,
transaction_keys: &[Pubkey],
program_id: &Pubkey,
data: &S,
recent_blockhash: Hash,
fee: u64,
) -> Self {
let mut account_metas = vec![AccountMeta::new(from_keypair.pubkey(), true)];
for pubkey in transaction_keys {
account_metas.push(AccountMeta::new(*pubkey, false));
}
let instruction = Instruction::new(*program_id, data, account_metas);
Self::new_signed_instructions(&[from_keypair], vec![instruction], recent_blockhash, fee)
}
/// Create a signed transaction
/// * `from_keypairs` - The keys used to sign the transaction.
/// * `account_keys` - The keys for the transaction. These are the program state
@ -407,6 +391,7 @@ impl<'de> Deserialize<'de> for Transaction {
#[cfg(test)]
mod tests {
use super::*;
use crate::instruction::AccountMeta;
use crate::signature::Keypair;
use bincode::{deserialize, serialize};
@ -482,20 +467,38 @@ mod tests {
assert!(!tx.verify_refs());
}
fn create_sample_transaction() -> Transaction {
use untrusted::Input;
let keypair = Keypair::from_pkcs8(Input::from(&[
48, 83, 2, 1, 1, 48, 5, 6, 3, 43, 101, 112, 4, 34, 4, 32, 255, 101, 36, 24, 124, 23,
167, 21, 132, 204, 155, 5, 185, 58, 121, 75, 156, 227, 116, 193, 215, 38, 142, 22, 8,
14, 229, 239, 119, 93, 5, 218, 161, 35, 3, 33, 0, 36, 100, 158, 252, 33, 161, 97, 185,
62, 89, 99, 195, 250, 249, 187, 189, 171, 118, 241, 90, 248, 14, 68, 219, 231, 62, 157,
5, 142, 27, 210, 117,
]))
.unwrap();
let to = Pubkey::new(&[
1, 1, 1, 4, 5, 6, 7, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 8, 7, 6, 5, 4,
1, 1, 1,
]);
let program_id = Pubkey::new(&[
2, 2, 2, 4, 5, 6, 7, 8, 9, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 9, 8, 7, 6, 5, 4,
2, 2, 2,
]);
let account_metas = vec![
AccountMeta::new(keypair.pubkey(), true),
AccountMeta::new(to, false),
];
let instruction = Instruction::new(program_id, &(1u8, 2u8, 3u8), account_metas);
let mut message = Message::new(vec![instruction]);
message.fee = 99;
Transaction::new(&[&keypair], message, Hash::default())
}
#[test]
fn test_transaction_serialize() {
let keypair = Keypair::new();
let program_id = Pubkey::new(&[4; 32]);
let to = Pubkey::new(&[5; 32]);
let tx = Transaction::new_signed(
&keypair,
&[keypair.pubkey(), to],
&program_id,
&(1u8, 2u8, 3u8),
Hash::default(),
99,
);
let tx = create_sample_transaction();
let ser = serialize(&tx).unwrap();
let deser = deserialize(&ser).unwrap();
assert_eq!(tx, deser);
@ -503,17 +506,7 @@ mod tests {
#[test]
fn test_transaction_serialized_size() {
let keypair = Keypair::new();
let program_id = Pubkey::new(&[4; 32]);
let to = Pubkey::new(&[5; 32]);
let tx = Transaction::new_signed(
&keypair,
&[keypair.pubkey(), to],
&program_id,
&(1u8, 2u8, 3u8),
Hash::default(),
99,
);
let tx = create_sample_transaction();
let req_size = size_of::<u64>()
+ 1
+ (tx.signatures.len() * size_of::<Signature>())
@ -533,35 +526,8 @@ mod tests {
/// affect on SDKs and DApps
#[test]
fn test_sdk_serialize() {
use untrusted::Input;
let keypair = Keypair::from_pkcs8(Input::from(&[
48, 83, 2, 1, 1, 48, 5, 6, 3, 43, 101, 112, 4, 34, 4, 32, 255, 101, 36, 24, 124, 23,
167, 21, 132, 204, 155, 5, 185, 58, 121, 75, 156, 227, 116, 193, 215, 38, 142, 22, 8,
14, 229, 239, 119, 93, 5, 218, 161, 35, 3, 33, 0, 36, 100, 158, 252, 33, 161, 97, 185,
62, 89, 99, 195, 250, 249, 187, 189, 171, 118, 241, 90, 248, 14, 68, 219, 231, 62, 157,
5, 142, 27, 210, 117,
]))
.unwrap();
let to = Pubkey::new(&[
1, 1, 1, 4, 5, 6, 7, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 8, 7, 6, 5, 4,
1, 1, 1,
]);
let program_id = Pubkey::new(&[
2, 2, 2, 4, 5, 6, 7, 8, 9, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 9, 8, 7, 6, 5, 4,
2, 2, 2,
]);
let tx = Transaction::new_signed(
&keypair,
&[to],
&program_id,
&(1u8, 2u8, 3u8),
Hash::default(),
99,
);
assert_eq!(
serialize(&tx).unwrap(),
serialize(&create_sample_transaction()).unwrap(),
vec![
212, 0, 0, 0, 0, 0, 0, 0, 1, 107, 231, 179, 42, 11, 220, 153, 173, 229, 29, 51,
218, 98, 26, 46, 164, 248, 228, 118, 244, 191, 192, 198, 228, 190, 119, 21, 52, 66,