Remove Transaction::new_signed
This commit is contained in:
@ -12,11 +12,11 @@ use serde_derive::{Deserialize, Serialize};
|
|||||||
use solana_metrics;
|
use solana_metrics;
|
||||||
use solana_metrics::influxdb;
|
use solana_metrics::influxdb;
|
||||||
use solana_sdk::hash::Hash;
|
use solana_sdk::hash::Hash;
|
||||||
|
use solana_sdk::message::Message;
|
||||||
use solana_sdk::packet::PACKET_DATA_SIZE;
|
use solana_sdk::packet::PACKET_DATA_SIZE;
|
||||||
use solana_sdk::pubkey::Pubkey;
|
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_instruction::SystemInstruction;
|
||||||
use solana_sdk::system_program;
|
|
||||||
use solana_sdk::transaction::Transaction;
|
use solana_sdk::transaction::Transaction;
|
||||||
use std::io;
|
use std::io;
|
||||||
use std::io::{Error, ErrorKind};
|
use std::io::{Error, ErrorKind};
|
||||||
@ -127,22 +127,10 @@ impl Drone {
|
|||||||
|
|
||||||
info!("Requesting airdrop of {} to {:?}", lamports, to);
|
info!("Requesting airdrop of {} to {:?}", lamports, to);
|
||||||
|
|
||||||
let create_instruction = SystemInstruction::CreateAccount {
|
let create_instruction =
|
||||||
lamports,
|
SystemInstruction::new_account(&self.mint_keypair.pubkey(), &to, lamports);
|
||||||
space: 0,
|
let message = Message::new(vec![create_instruction]);
|
||||||
program_id: system_program::id(),
|
Ok(Transaction::new(&[&self.mint_keypair], message, blockhash))
|
||||||
};
|
|
||||||
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)
|
|
||||||
} else {
|
} else {
|
||||||
Err(Error::new(ErrorKind::Other, "token limit reached"))
|
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 {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use bytes::BufMut;
|
use bytes::BufMut;
|
||||||
use solana_sdk::signature::{Keypair, KeypairUtil};
|
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -369,7 +356,6 @@ mod tests {
|
|||||||
assert_eq!(tx.signatures.len(), 1);
|
assert_eq!(tx.signatures.len(), 1);
|
||||||
assert_eq!(tx.account_keys, vec![mint_pubkey, to]);
|
assert_eq!(tx.account_keys, vec![mint_pubkey, to]);
|
||||||
assert_eq!(tx.recent_blockhash, blockhash);
|
assert_eq!(tx.recent_blockhash, blockhash);
|
||||||
assert_eq!(tx.program_ids, vec![system_program::id()]);
|
|
||||||
|
|
||||||
assert_eq!(tx.instructions.len(), 1);
|
assert_eq!(tx.instructions.len(), 1);
|
||||||
let instruction: SystemInstruction = deserialize(&tx.instructions[0].data).unwrap();
|
let instruction: SystemInstruction = deserialize(&tx.instructions[0].data).unwrap();
|
||||||
@ -403,20 +389,9 @@ mod tests {
|
|||||||
bytes.put(&req[..]);
|
bytes.put(&req[..]);
|
||||||
|
|
||||||
let keypair = Keypair::new();
|
let keypair = Keypair::new();
|
||||||
let expected_instruction = SystemInstruction::CreateAccount {
|
let expected_instruction = SystemInstruction::new_account(&keypair.pubkey(), &to, lamports);
|
||||||
lamports,
|
let message = Message::new(vec![expected_instruction]);
|
||||||
space: 0,
|
let expected_tx = Transaction::new(&[&keypair], message, blockhash);
|
||||||
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_bytes = serialize(&expected_tx).unwrap();
|
let expected_bytes = serialize(&expected_tx).unwrap();
|
||||||
let mut expected_vec_with_length = vec![0; 2];
|
let mut expected_vec_with_length = vec![0; 2];
|
||||||
LittleEndian::write_u16(&mut expected_vec_with_length, expected_bytes.len() as u16);
|
LittleEndian::write_u16(&mut expected_vec_with_length, expected_bytes.len() as u16);
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
use solana_drone::drone::{request_airdrop_transaction, run_local_drone};
|
use solana_drone::drone::{request_airdrop_transaction, run_local_drone};
|
||||||
use solana_sdk::hash::Hash;
|
use solana_sdk::hash::Hash;
|
||||||
|
use solana_sdk::message::Message;
|
||||||
use solana_sdk::signature::{Keypair, KeypairUtil};
|
use solana_sdk::signature::{Keypair, KeypairUtil};
|
||||||
use solana_sdk::system_instruction::SystemInstruction;
|
use solana_sdk::system_instruction::SystemInstruction;
|
||||||
use solana_sdk::system_program;
|
|
||||||
use solana_sdk::transaction::Transaction;
|
use solana_sdk::transaction::Transaction;
|
||||||
use std::sync::mpsc::channel;
|
use std::sync::mpsc::channel;
|
||||||
|
|
||||||
@ -12,20 +12,9 @@ fn test_local_drone() {
|
|||||||
let to = Keypair::new().pubkey();
|
let to = Keypair::new().pubkey();
|
||||||
let lamports = 50;
|
let lamports = 50;
|
||||||
let blockhash = Hash::new(&to.as_ref());
|
let blockhash = Hash::new(&to.as_ref());
|
||||||
let expected_instruction = SystemInstruction::CreateAccount {
|
let create_instruction = SystemInstruction::new_account(&keypair.pubkey(), &to, lamports);
|
||||||
lamports,
|
let message = Message::new(vec![create_instruction]);
|
||||||
space: 0,
|
let expected_tx = Transaction::new(&[&keypair], message, blockhash);
|
||||||
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 (sender, receiver) = channel();
|
let (sender, receiver) = channel();
|
||||||
run_local_drone(keypair, sender);
|
run_local_drone(keypair, sender);
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
//! Defines a Transaction type to package an atomic sequence of instructions.
|
//! Defines a Transaction type to package an atomic sequence of instructions.
|
||||||
|
|
||||||
use crate::hash::{Hash, Hasher};
|
use crate::hash::{Hash, Hasher};
|
||||||
use crate::instruction::{AccountMeta, CompiledInstruction, Instruction, InstructionError};
|
use crate::instruction::{CompiledInstruction, Instruction, InstructionError};
|
||||||
use crate::message::Message;
|
use crate::message::Message;
|
||||||
use crate::packet::PACKET_DATA_SIZE;
|
use crate::packet::PACKET_DATA_SIZE;
|
||||||
use crate::pubkey::Pubkey;
|
use crate::pubkey::Pubkey;
|
||||||
@ -109,22 +109,6 @@ impl Transaction {
|
|||||||
Self::new(from_keypairs, message, recent_blockhash)
|
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
|
/// Create a signed transaction
|
||||||
/// * `from_keypairs` - The keys used to sign the transaction.
|
/// * `from_keypairs` - The keys used to sign the transaction.
|
||||||
/// * `account_keys` - The keys for the transaction. These are the program state
|
/// * `account_keys` - The keys for the transaction. These are the program state
|
||||||
@ -407,6 +391,7 @@ impl<'de> Deserialize<'de> for Transaction {
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
use crate::instruction::AccountMeta;
|
||||||
use crate::signature::Keypair;
|
use crate::signature::Keypair;
|
||||||
use bincode::{deserialize, serialize};
|
use bincode::{deserialize, serialize};
|
||||||
|
|
||||||
@ -482,20 +467,38 @@ mod tests {
|
|||||||
assert!(!tx.verify_refs());
|
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]
|
#[test]
|
||||||
fn test_transaction_serialize() {
|
fn test_transaction_serialize() {
|
||||||
let keypair = Keypair::new();
|
let tx = create_sample_transaction();
|
||||||
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 ser = serialize(&tx).unwrap();
|
let ser = serialize(&tx).unwrap();
|
||||||
let deser = deserialize(&ser).unwrap();
|
let deser = deserialize(&ser).unwrap();
|
||||||
assert_eq!(tx, deser);
|
assert_eq!(tx, deser);
|
||||||
@ -503,17 +506,7 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_transaction_serialized_size() {
|
fn test_transaction_serialized_size() {
|
||||||
let keypair = Keypair::new();
|
let tx = create_sample_transaction();
|
||||||
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 req_size = size_of::<u64>()
|
let req_size = size_of::<u64>()
|
||||||
+ 1
|
+ 1
|
||||||
+ (tx.signatures.len() * size_of::<Signature>())
|
+ (tx.signatures.len() * size_of::<Signature>())
|
||||||
@ -533,35 +526,8 @@ mod tests {
|
|||||||
/// affect on SDKs and DApps
|
/// affect on SDKs and DApps
|
||||||
#[test]
|
#[test]
|
||||||
fn test_sdk_serialize() {
|
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!(
|
assert_eq!(
|
||||||
serialize(&tx).unwrap(),
|
serialize(&create_sample_transaction()).unwrap(),
|
||||||
vec![
|
vec![
|
||||||
212, 0, 0, 0, 0, 0, 0, 0, 1, 107, 231, 179, 42, 11, 220, 153, 173, 229, 29, 51,
|
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,
|
218, 98, 26, 46, 164, 248, 228, 118, 244, 191, 192, 198, 228, 190, 119, 21, 52, 66,
|
||||||
|
Reference in New Issue
Block a user