diff --git a/src/bank.rs b/src/bank.rs index bdb97e9370a716..2bdfe15249ccca 100644 --- a/src/bank.rs +++ b/src/bank.rs @@ -484,7 +484,7 @@ impl Bank { for (i, tx) in txs.iter().enumerate() { Self::update_signature_status_with_last_id( &mut last_ids.entries, - &tx.signature, + &tx.signatures[0], &res[i], &tx.last_id, ); @@ -498,7 +498,7 @@ impl Bank { Err(_) => RpcSignatureStatus::GenericFailure, }; if status != RpcSignatureStatus::SignatureNotFound { - self.check_signature_subscriptions(&tx.signature, status); + self.check_signature_subscriptions(&tx.signatures[0], status); } } } @@ -651,7 +651,8 @@ impl Bank { // There is no way to predict what contract will execute without an error // If a fee can pay for execution then the contract will be scheduled - let err = Self::reserve_signature_with_last_id(last_ids, &tx.last_id, &tx.signature); + let err = + Self::reserve_signature_with_last_id(last_ids, &tx.last_id, &tx.signatures[0]); if let Err(BankError::LastIdNotFound) = err { error_counters.reserve_last_id += 1; } else if let Err(BankError::DuplicateSignature) = err { @@ -880,6 +881,10 @@ impl Bank { /// The accounts are committed back to the bank only if every instruction succeeds fn execute_transaction(&self, tx: &Transaction, tx_accounts: &mut [Account]) -> Result<()> { for (instruction_index, instruction) in tx.instructions.iter().enumerate() { + info!( + "tx_accounts: {:?} ix_accounts: {:?}", + tx_accounts, instruction.accounts + ); Self::with_subset(tx_accounts, &instruction.accounts, |program_accounts| { self.execute_instruction(tx, instruction_index, program_accounts) })?; @@ -1294,7 +1299,8 @@ impl Bank { last_id: Hash, ) -> Result { let tx = Transaction::system_new(keypair, to, n, last_id); - let signature = tx.signature; + info!("tx: {:?}", tx); + let signature = tx.signatures[0]; self.process_transaction(&tx).map(|_| signature) } @@ -1552,10 +1558,13 @@ mod tests { assert_eq!(bank.get_balance(&mint.pubkey()), 0); assert_eq!(bank.get_balance(&key1), 1); assert_eq!(bank.get_balance(&key2), 0); - assert_eq!(bank.get_signature(&t1.last_id, &t1.signature), Some(Ok(()))); + assert_eq!( + bank.get_signature(&t1.last_id, &t1.signatures[0]), + Some(Ok(())) + ); // TODO: Transactions that fail to pay a fee could be dropped silently assert_eq!( - bank.get_signature(&t2.last_id, &t2.signature), + bank.get_signature(&t2.last_id, &t2.signatures[0]), Some(Err(BankError::AccountInUse)) ); } @@ -1581,7 +1590,7 @@ mod tests { ]; let t1 = Transaction::new_with_instructions( - &mint.keypair(), + &[&mint.keypair()], &[key1, key2], mint.last_id(), 0, @@ -1595,7 +1604,7 @@ mod tests { assert_eq!(bank.get_balance(&key1), 0); assert_eq!(bank.get_balance(&key2), 0); assert_eq!( - bank.get_signature(&t1.last_id, &t1.signature), + bank.get_signature(&t1.last_id, &t1.signatures[0]), Some(Err(BankError::ResultWithNegativeTokens(1))) ); } @@ -1618,7 +1627,10 @@ mod tests { assert_eq!(bank.get_balance(&mint.pubkey()), 0); assert_eq!(bank.get_balance(&key1), 1); assert_eq!(bank.get_balance(&key2), 1); - assert_eq!(bank.get_signature(&t1.last_id, &t1.signature), Some(Ok(()))); + assert_eq!( + bank.get_signature(&t1.last_id, &t1.signatures[0]), + Some(Ok(())) + ); } // TODO: This test demonstrates that fees are not paid when a program fails. @@ -1639,7 +1651,7 @@ mod tests { Pubkey::default(), 1, ); - let signature = tx.signature; + let signature = tx.signatures[0]; assert!(!bank.has_signature(&signature)); let res = bank.process_transaction(&tx); @@ -2046,7 +2058,7 @@ mod tests { let bank_sub_id = Keypair::new().pubkey(); let last_id = bank.last_id(); let tx = Transaction::system_move(&mint.keypair(), alice.pubkey(), 20, last_id, 0); - let signature = tx.signature; + let signature = tx.signatures[0]; bank.process_transaction(&tx).unwrap(); let (subscriber, _id_receiver, mut transport_receiver) = diff --git a/src/bin/bench-tps.rs b/src/bin/bench-tps.rs index 8b5e72b844b139..ffbe58d7a4440b 100644 --- a/src/bin/bench-tps.rs +++ b/src/bin/bench-tps.rs @@ -293,7 +293,7 @@ fn do_tx_transfers( const MAX_SPENDS_PER_TX: usize = 5; fn verify_transfer(client: &mut ThinClient, tx: &Transaction) -> bool { - if client.poll_for_signature(&tx.signature).is_err() { + if client.poll_for_signature(&tx.signatures[0]).is_err() { println!("no signature"); return false; } diff --git a/src/budget_transaction.rs b/src/budget_transaction.rs index 0d6e78181456e8..09a925657afd61 100644 --- a/src/budget_transaction.rs +++ b/src/budget_transaction.rs @@ -92,7 +92,14 @@ impl BudgetTransaction for Transaction { transaction::Instruction::new(1, &budget_instruction, vec![1]), ]; - Self::new_with_instructions(from_keypair, &keys, last_id, fee, program_ids, instructions) + Self::new_with_instructions( + &[from_keypair], + &keys, + last_id, + fee, + program_ids, + instructions, + ) } /// Create and sign a new Transaction. Used for unit-testing. @@ -258,9 +265,10 @@ mod tests { let instruction = Instruction::NewBudget(expr); let instructions = vec![transaction::Instruction::new(0, &instruction, vec![])]; let claim0 = Transaction { + signed_keys: vec![], account_keys: vec![], last_id: Default::default(), - signature: Default::default(), + signatures: vec![], program_ids: vec![], instructions, fee: 0, diff --git a/src/rpc.rs b/src/rpc.rs index 820d9779c71501..a71ffc7f357777 100644 --- a/src/rpc.rs +++ b/src/rpc.rs @@ -251,7 +251,7 @@ impl RpcSol for RpcSolImpl { info!("send_transaction: send_to error: {:?}", err); Error::internal_error() })?; - let signature = bs58::encode(tx.signature).into_string(); + let signature = bs58::encode(tx.signatures[0]).into_string(); trace!( "send_transaction: sent {} bytes, signature={}", data.len(), @@ -507,7 +507,7 @@ mod tests { let req = format!( r#"{{"jsonrpc":"2.0","id":1,"method":"confirmTransaction","params":["{}"]}}"#, - tx.signature + tx.signatures[0] ); let res = io.handle_request_sync(&req, meta); let expected = format!(r#"{{"jsonrpc":"2.0","result":true,"id":1}}"#); @@ -526,7 +526,7 @@ mod tests { let req = format!( r#"{{"jsonrpc":"2.0","id":1,"method":"getSignatureStatus","params":["{}"]}}"#, - tx.signature + tx.signatures[0] ); let res = io.handle_request_sync(&req, meta.clone()); let expected = format!(r#"{{"jsonrpc":"2.0","result":"Confirmed","id":1}}"#); @@ -540,7 +540,7 @@ mod tests { let tx = Transaction::system_move(&alice_keypair, bob_pubkey, 10, last_id, 0); let req = format!( r#"{{"jsonrpc":"2.0","id":1,"method":"getSignatureStatus","params":["{}"]}}"#, - tx.signature + tx.signatures[0] ); let res = io.handle_request_sync(&req, meta); let expected = format!(r#"{{"jsonrpc":"2.0","result":"SignatureNotFound","id":1}}"#); @@ -745,8 +745,8 @@ mod tests { let tx = Transaction::system_move(&Keypair::new(), Keypair::new().pubkey(), 20, hash(&[0]), 0); assert_eq!( - verify_signature(&tx.signature.to_string()).unwrap(), - tx.signature + verify_signature(&tx.signatures[0].to_string()).unwrap(), + tx.signatures[0] ); let bad_signature = "a1b2c3d4"; assert_eq!( diff --git a/src/rpc_pubsub.rs b/src/rpc_pubsub.rs index fd270570e7e93f..1bf4bfc86624ef 100644 --- a/src/rpc_pubsub.rs +++ b/src/rpc_pubsub.rs @@ -290,7 +290,7 @@ mod tests { let req = format!( r#"{{"jsonrpc":"2.0","id":1,"method":"signatureSubscribe","params":["{}"]}}"#, - tx.signature.to_string() + tx.signatures[0].to_string() ); let res = io.handle_request_sync(&req, session.clone()); let expected = format!(r#"{{"jsonrpc":"2.0","result":0,"id":1}}"#); @@ -331,7 +331,7 @@ mod tests { let tx = Transaction::system_move(&alice.keypair(), bob_pubkey, 10, last_id, 0); let req = format!( r#"{{"jsonrpc":"2.0","id":1,"method":"signatureSubscribe","params":["{}"]}}"#, - tx.signature.to_string() + tx.signatures[0].to_string() ); let res = io.handle_request_sync(&req, session.clone()); let expected = format!(r#"{{"jsonrpc":"2.0","result":1,"id":1}}"#); @@ -364,7 +364,7 @@ mod tests { let tx = Transaction::system_move(&alice.keypair(), bob_pubkey, 20, last_id, 0); let req = format!( r#"{{"jsonrpc":"2.0","id":1,"method":"signatureSubscribe","params":["{}"]}}"#, - tx.signature.to_string() + tx.signatures[0].to_string() ); let _res = io.handle_request_sync(&req, session.clone()); diff --git a/src/sigverify.rs b/src/sigverify.rs index 4cd1e4464069d2..90b1d52fc277ba 100644 --- a/src/sigverify.rs +++ b/src/sigverify.rs @@ -4,12 +4,16 @@ //! offloaded to the GPU. //! +use byteorder::{LittleEndian, ReadBytesExt}; use counter::Counter; use log::Level; use packet::{Packet, SharedPackets}; +use result::Result; +use signature::Signature; +use solana_sdk::pubkey::Pubkey; +use std::io; use std::mem::size_of; use std::sync::atomic::AtomicUsize; -use transaction::{PUB_KEY_OFFSET, SIGNED_DATA_OFFSET, SIG_OFFSET}; pub const TX_OFFSET: usize = 0; @@ -29,10 +33,12 @@ extern "C" { vecs: *const Elems, num: u32, //number of vecs message_size: u32, //size of each element inside the elems field of the vec - pubkey_offset: u32, - signature_offset: u32, - signed_message_offset: u32, - signed_message_len_offset: u32, + total_packets: u32, + total_signatures: u32, + message_lens: *const u32, + pubkey_offsets: *const u32, + signature_offsets: *const u32, + signed_message_offsets: *const u32, out: *mut u8, //combined length of all the items in vecs ) -> u32; @@ -62,22 +68,35 @@ fn verify_packet(packet: &Packet) -> u8 { use signature::Signature; use solana_sdk::pubkey::Pubkey; - let msg_start = TX_OFFSET + SIGNED_DATA_OFFSET; - let sig_start = TX_OFFSET + SIG_OFFSET; - let sig_end = sig_start + size_of::(); - let pubkey_start = TX_OFFSET + PUB_KEY_OFFSET; - let pubkey_end = pubkey_start + size_of::(); + let (sig_len, sig_start, msg_start, pubkey_start) = get_packet_offsets(packet, 0); + let mut sig_start = sig_start as usize; + let mut pubkey_start = pubkey_start as usize; + let msg_start = msg_start as usize; if packet.meta.size <= msg_start { return 0; } let msg_end = packet.meta.size; - let signature = Signature::new(&packet.data[sig_start..sig_end]); - signature.verify( - &packet.data[pubkey_start..pubkey_end], - &packet.data[msg_start..msg_end], - ) as u8 + for _ in 0..sig_len { + let pubkey_end = pubkey_start as usize + size_of::(); + let sig_end = sig_start as usize + size_of::(); + + if pubkey_end >= packet.meta.size || sig_end >= packet.meta.size { + return 0; + } + + let signature = Signature::new(&packet.data[sig_start..sig_end]); + if !signature.verify( + &packet.data[pubkey_start..pubkey_end], + &packet.data[msg_start..msg_end], + ) { + return 0; + } + pubkey_start += size_of::(); + sig_start += size_of::(); + } + 1 } fn verify_packet_disabled(_packet: &Packet) -> u8 { @@ -97,10 +116,65 @@ pub fn ed25519_verify(batches: &[SharedPackets]) -> Vec> { ed25519_verify_cpu(batches) } +pub fn get_packet_offsets(packet: &Packet, current_offset: u32) -> (u32, u32, u32, u32) { + // Read in u64 as the size of signatures array + let mut rdr = io::Cursor::new(&packet.data[0..size_of::()]); + let sig_len = rdr.read_u64::().unwrap() as u32; + + let msg_start_offset = + current_offset + size_of::() as u32 + sig_len * size_of::() as u32; + let pubkey_offset = msg_start_offset + size_of::() as u32; + + let sig_start = size_of::() as u32; + + (sig_len, sig_start, msg_start_offset, pubkey_offset) +} + +pub fn generate_offsets( + batches: &[SharedPackets], +) -> Result<(Vec, Vec, Vec, Vec)> { + let mut signature_offsets: Vec = Vec::new(); + let mut pubkey_offsets: Vec = Vec::new(); + let mut msg_start_offsets: Vec = Vec::new(); + let mut msg_sizes: Vec = Vec::new(); + let mut current_packet = 0; + batches.into_iter().for_each(|p| { + p.read().unwrap().packets.iter().for_each(|packet| { + let current_offset = current_packet as u32 * size_of::() as u32; + + let (sig_len, _sig_start, msg_start_offset, pubkey_offset) = + get_packet_offsets(packet, current_offset); + let mut pubkey_offset = pubkey_offset; + + info!("pubkey_offset: {}", pubkey_offset); + let mut sig_offset = current_offset + size_of::() as u32; + for _ in 0..sig_len { + signature_offsets.push(sig_offset); + sig_offset += size_of::() as u32; + + pubkey_offsets.push(pubkey_offset); + pubkey_offset += size_of::() as u32; + + msg_start_offsets.push(msg_start_offset); + + msg_sizes.push(current_offset + (packet.meta.size as u32) - msg_start_offset); + } + current_packet += 1; + }); + }); + Ok(( + signature_offsets, + pubkey_offsets, + msg_start_offsets, + msg_sizes, + )) +} + pub fn ed25519_verify_cpu(batches: &[SharedPackets]) -> Vec> { use rayon::prelude::*; let count = batch_size(batches); info!("CPU ECDSA for {}", batch_size(batches)); + let rv = batches .into_par_iter() .map(|p| { @@ -108,8 +182,11 @@ pub fn ed25519_verify_cpu(batches: &[SharedPackets]) -> Vec> { .unwrap() .packets .par_iter() - .map(verify_packet) - .collect() + .map(|packet| { + let ret = verify_packet(packet); + info!("verify! {}", ret); + ret + }).collect() }).collect(); inc_new_counter_info!("ed25519_verify_cpu", count); rv @@ -158,6 +235,13 @@ pub fn ed25519_verify(batches: &[SharedPackets]) -> Vec> { return ed25519_verify_cpu(batches); } + let (signature_offsets, pubkey_offsets, msg_start_offsets, msg_sizes) = + generate_offsets(batches).unwrap(); + + info!("sig offsets: {:?}", signature_offsets); + info!("pubkey offsets: {:?}", pubkey_offsets); + info!("msg offsets: {:?}", msg_start_offsets); + info!("CUDA ECDSA for {}", batch_size(batches)); let mut out = Vec::new(); let mut elems = Vec::new(); @@ -167,7 +251,7 @@ pub fn ed25519_verify(batches: &[SharedPackets]) -> Vec> { for packets in batches { locks.push(packets.read().unwrap()); } - let mut num = 0; + let mut num_packets = 0; for p in locks { elems.push(Elems { elems: p.packets.as_ptr(), @@ -176,25 +260,24 @@ pub fn ed25519_verify(batches: &[SharedPackets]) -> Vec> { let mut v = Vec::new(); v.resize(p.packets.len(), 0); rvs.push(v); - num += p.packets.len(); + num_packets += p.packets.len(); } - out.resize(num, 0); - trace!("Starting verify num packets: {}", num); + out.resize(num_packets, 0); + trace!("Starting verify num packets: {}", num_packets); trace!("elem len: {}", elems.len() as u32); trace!("packet sizeof: {}", size_of::() as u32); - trace!("pubkey: {}", (TX_OFFSET + PUB_KEY_OFFSET) as u32); - trace!("signature offset: {}", (TX_OFFSET + SIG_OFFSET) as u32); - trace!("sign data: {}", (TX_OFFSET + SIGNED_DATA_OFFSET) as u32); trace!("len offset: {}", PACKET_DATA_SIZE as u32); unsafe { let res = ed25519_verify_many( elems.as_ptr(), elems.len() as u32, size_of::() as u32, - (TX_OFFSET + PUB_KEY_OFFSET) as u32, - (TX_OFFSET + SIG_OFFSET) as u32, - (TX_OFFSET + SIGNED_DATA_OFFSET) as u32, - PACKET_DATA_SIZE as u32, + num_packets as u32, + signature_offsets.len() as u32, + msg_sizes.as_ptr(), + pubkey_offsets.as_ptr(), + signature_offsets.as_ptr(), + msg_start_offsets.as_ptr(), out.as_mut_ptr(), ); if res != 0 { @@ -236,6 +319,7 @@ mod tests { fn make_packet_from_transaction(tx: Transaction) -> Packet { let tx_bytes = serialize(&tx).unwrap(); let mut packet = Packet::default(); + info!("tx: {:?} len: {}", tx, tx_bytes.len()); packet.meta.size = tx_bytes.len(); packet.data[..packet.meta.size].copy_from_slice(&tx_bytes); return packet; @@ -282,7 +366,9 @@ mod tests { #[test] fn test_verify_one() { - test_verify_n(1, false); + use logger; + logger::setup(); + test_verify_n(2, false); } #[test] diff --git a/src/storage_stage.rs b/src/storage_stage.rs index 25a4c3fc7f4e52..f2e92e731d9491 100644 --- a/src/storage_stage.rs +++ b/src/storage_stage.rs @@ -223,7 +223,7 @@ impl StorageStage { ); let mut storage_keys = storage_keys.write().unwrap(); storage_keys[*current_key_idx..*current_key_idx + size_of::()] - .copy_from_slice(tx.signature.as_ref()); + .copy_from_slice(tx.signatures[0].as_ref()); *current_key_idx += size_of::(); *current_key_idx %= storage_keys.len(); } diff --git a/src/system_transaction.rs b/src/system_transaction.rs index fdb3ec3a6dee24..8d44e55a7942b8 100644 --- a/src/system_transaction.rs +++ b/src/system_transaction.rs @@ -110,7 +110,7 @@ impl SystemTransaction for Transaction { let to_keys: Vec<_> = moves.iter().map(|(to_key, _)| *to_key).collect(); Transaction::new_with_instructions( - from, + &[from], &to_keys, last_id, fee, @@ -149,18 +149,15 @@ mod tests { use super::*; use bincode::{deserialize, serialize}; use packet::PACKET_DATA_SIZE; - use transaction::{PUB_KEY_OFFSET, SIGNED_DATA_OFFSET, SIG_OFFSET}; + use transaction::SIG_OFFSET; #[test] fn test_layout() { let tx = test_tx(); - let sign_data = tx.get_sign_data(); let tx_bytes = serialize(&tx).unwrap(); - assert_eq!(memfind(&tx_bytes, &sign_data), Some(SIGNED_DATA_OFFSET)); - assert_eq!(memfind(&tx_bytes, &tx.signature.as_ref()), Some(SIG_OFFSET)); assert_eq!( - memfind(&tx_bytes, &tx.account_keys[0].as_ref()), - Some(PUB_KEY_OFFSET) + memfind(&tx_bytes, &tx.signatures[0].as_ref()), + Some(SIG_OFFSET) ); assert!(tx.verify_signature()); } @@ -172,15 +169,10 @@ mod tests { let sign_data0a = tx0.get_sign_data(); let tx_bytes = serialize(&tx0).unwrap(); assert!(tx_bytes.len() < PACKET_DATA_SIZE); - assert_eq!(memfind(&tx_bytes, &sign_data0a), Some(SIGNED_DATA_OFFSET)); assert_eq!( - memfind(&tx_bytes, &tx0.signature.as_ref()), + memfind(&tx_bytes, &tx0.signatures[0].as_ref()), Some(SIG_OFFSET) ); - assert_eq!( - memfind(&tx_bytes, &tx0.account_keys[0].as_ref()), - Some(PUB_KEY_OFFSET) - ); let tx1 = deserialize(&tx_bytes).unwrap(); assert_eq!(tx0, tx1); assert_eq!(tx1.instructions[0].userdata, vec![1, 2, 3]); diff --git a/src/thin_client.rs b/src/thin_client.rs index ee903b80ee8fdf..ebca5fbddb2584 100644 --- a/src/thin_client.rs +++ b/src/thin_client.rs @@ -71,7 +71,7 @@ impl ThinClient { let data = serialize(&tx).expect("serialize Transaction in pub fn transfer_signed"); self.transactions_socket .send_to(&data, &self.transactions_addr)?; - Ok(tx.signature) + Ok(tx.signatures[0]) } /// Retry a sending a signed Transaction to the server for processing. @@ -82,12 +82,12 @@ impl ThinClient { tries: usize, ) -> io::Result { for x in 0..tries { - tx.sign(&keypair, self.get_last_id()); + tx.sign(&[&keypair], self.get_last_id()); let data = serialize(&tx).expect("serialize Transaction in pub fn transfer_signed"); self.transactions_socket .send_to(&data, &self.transactions_addr)?; - if self.poll_for_signature(&tx.signature).is_ok() { - return Ok(tx.signature); + if self.poll_for_signature(&tx.signatures[0]).is_ok() { + return Ok(tx.signatures[0]); } info!("{} tries failed transfer to {}", x, self.transactions_addr); } diff --git a/src/transaction.rs b/src/transaction.rs index da19a5cc382619..4dba437875ee34 100644 --- a/src/transaction.rs +++ b/src/transaction.rs @@ -8,9 +8,7 @@ use signature::{Keypair, KeypairUtil, Signature}; use solana_sdk::pubkey::Pubkey; use std::mem::size_of; -pub const SIGNED_DATA_OFFSET: usize = size_of::(); -pub const SIG_OFFSET: usize = 0; -pub const PUB_KEY_OFFSET: usize = size_of::() + size_of::(); +pub const SIG_OFFSET: usize = size_of::(); /// An instruction to execute a program under the `program_id` of `program_ids_index` with the /// specified accounts and userdata @@ -39,8 +37,11 @@ impl Instruction { /// An atomic transaction #[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)] pub struct Transaction { - /// A digital signature of `account_keys`, `program_ids`, `last_id`, `fee` and `instructions`, signed by `Pubkey`. - pub signature: Signature, + /// A set of digital signature of `account_keys`, `program_ids`, `last_id`, `fee` and `instructions`, signed by `signed_keys`. + pub signatures: Vec, + + /// The `Pubkeys` that correspond to signature in `signatures` + pub signed_keys: Vec, /// The `Pubkeys` that are executing this transaction userdata. The meaning of each key is /// program-specific. @@ -75,7 +76,7 @@ impl Transaction { let accounts = (0..=transaction_keys.len() as u8).collect(); let instructions = vec![Instruction::new(0, userdata, accounts)]; Self::new_with_instructions( - from_keypair, + &[from_keypair], transaction_keys, last_id, fee, @@ -92,25 +93,31 @@ impl Transaction { /// * `program_ids` - The keys that identify programs used in the `instruction` vector. /// * `instructions` - The programs and their arguments that the transaction will execute atomically pub fn new_with_instructions( - from_keypair: &Keypair, + from_keypairs: &[&Keypair], keys: &[Pubkey], last_id: Hash, fee: u64, program_ids: Vec, instructions: Vec, ) -> Self { - let from = from_keypair.pubkey(); - let mut account_keys = vec![from]; + let from = from_keypairs[0].pubkey(); + let mut account_keys = Vec::with_capacity(keys.len() + 1); + account_keys.push(from); account_keys.extend_from_slice(keys); + let signed_keys = from_keypairs + .iter() + .map(|keypair| keypair.pubkey()) + .collect(); let mut tx = Transaction { - signature: Signature::default(), + signatures: vec![], + signed_keys, account_keys, last_id: Hash::default(), fee, program_ids, instructions, }; - tx.sign(from_keypair, last_id); + tx.sign(from_keypairs, last_id); tx } pub fn userdata(&self, instruction_index: usize) -> &[u8] { @@ -139,7 +146,10 @@ impl Transaction { } /// Get the transaction data to sign. pub fn get_sign_data(&self) -> Vec { - let mut data = serialize(&self.account_keys).expect("serialize account_keys"); + let mut data = serialize(&self.signed_keys).expect("serialize signed keys"); + + let account_keys_data = serialize(&self.account_keys).expect("serialize account_keys"); + data.extend_from_slice(&account_keys_data); let last_id_data = serialize(&self.last_id).expect("serialize last_id"); data.extend_from_slice(&last_id_data); @@ -156,17 +166,21 @@ impl Transaction { } /// Sign this transaction. - pub fn sign(&mut self, keypair: &Keypair, last_id: Hash) { + pub fn sign(&mut self, keypairs: &[&Keypair], last_id: Hash) { self.last_id = last_id; let sign_data = self.get_sign_data(); - self.signature = Signature::new(&keypair.sign::(&sign_data).to_bytes()); + self.signatures = keypairs + .iter() + .map(|keypair| Signature::new(&keypair.sign::(&sign_data).to_bytes())) + .collect(); } /// Verify only the transaction signature. pub fn verify_signature(&self) -> bool { warn!("transaction signature verification called"); - self.signature - .verify(&self.from().as_ref(), &self.get_sign_data()) + self.signatures + .iter() + .all(|s| s.verify(&self.from().as_ref(), &self.get_sign_data())) } /// Verify that references in the instructions are valid @@ -193,7 +207,7 @@ impl Transaction { let mut hasher = Hasher::default(); transactions .iter() - .for_each(|tx| hasher.hash(&tx.signature.as_ref())); + .for_each(|tx| hasher.hash(&tx.signatures[0].as_ref())); hasher.result() } } @@ -216,7 +230,7 @@ mod tests { Instruction::new(1, &(), vec![0, 2]), ]; let tx = Transaction::new_with_instructions( - &key, + &[&key], &[key1, key2], Default::default(), 0, @@ -251,7 +265,7 @@ mod tests { let key = Keypair::new(); let instructions = vec![Instruction::new(1, &(), vec![])]; let tx = Transaction::new_with_instructions( - &key, + &[&key], &[], Default::default(), 0, @@ -265,7 +279,7 @@ mod tests { let key = Keypair::new(); let instructions = vec![Instruction::new(0, &(), vec![1])]; let tx = Transaction::new_with_instructions( - &key, + &[&key], &[], Default::default(), 0, @@ -302,19 +316,21 @@ mod tests { assert_eq!( serialize(&tx).unwrap(), vec![ - 238, 228, 120, 18, 14, 44, 44, 74, 186, 124, 104, 174, 137, 227, 237, 157, 147, 37, - 230, 74, 20, 48, 234, 36, 170, 60, 68, 184, 171, 240, 203, 18, 255, 110, 164, 67, - 212, 206, 115, 182, 13, 90, 38, 215, 191, 51, 79, 183, 57, 102, 248, 221, 114, 72, - 120, 66, 113, 146, 251, 102, 69, 187, 25, 8, 3, 0, 0, 0, 0, 0, 0, 0, 218, 65, 89, - 124, 81, 87, 72, 141, 119, 36, 224, 63, 184, 216, 74, 55, 106, 67, 184, 244, 21, - 24, 161, 28, 195, 135, 182, 105, 178, 238, 101, 134, 218, 65, 89, 124, 81, 87, 72, - 141, 119, 36, 224, 63, 184, 216, 74, 55, 106, 67, 184, 244, 21, 24, 161, 28, 195, - 135, 182, 105, 178, 238, 101, 134, 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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 0, 0, 0, 0, 0, 0, - 0, 1, 0, 0, 0, 0, 0, 0, 0, 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, 1, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, - 0, 0, 0, 1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3 + 1, 0, 0, 0, 0, 0, 0, 0, 138, 181, 204, 254, 187, 152, 126, 209, 179, 112, 152, 165, + 238, 27, 77, 189, 74, 127, 75, 229, 231, 221, 151, 206, 37, 37, 193, 84, 176, 236, + 124, 245, 246, 248, 160, 176, 19, 120, 13, 241, 248, 250, 169, 138, 235, 113, 19, + 46, 187, 48, 210, 178, 232, 251, 101, 148, 178, 239, 141, 140, 193, 155, 26, 14, 1, + 0, 0, 0, 0, 0, 0, 0, 218, 65, 89, 124, 81, 87, 72, 141, 119, 36, 224, 63, 184, 216, + 74, 55, 106, 67, 184, 244, 21, 24, 161, 28, 195, 135, 182, 105, 178, 238, 101, 134, + 3, 0, 0, 0, 0, 0, 0, 0, 218, 65, 89, 124, 81, 87, 72, 141, 119, 36, 224, 63, 184, + 216, 74, 55, 106, 67, 184, 244, 21, 24, 161, 28, 195, 135, 182, 105, 178, 238, 101, + 134, 218, 65, 89, 124, 81, 87, 72, 141, 119, 36, 224, 63, 184, 216, 74, 55, 106, + 67, 184, 244, 21, 24, 161, 28, 195, 135, 182, 105, 178, 238, 101, 134, 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, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 99, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 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, 1, 0, 0, + 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3 ], ); } diff --git a/tests/programs.rs b/tests/programs.rs index 0d6a8593bf3fe6..75e96d066b6a0b 100644 --- a/tests/programs.rs +++ b/tests/programs.rs @@ -46,7 +46,10 @@ fn create_bpf_path(name: &str) -> PathBuf { fn check_tx_results(bank: &Bank, tx: &Transaction, result: Vec>) { assert_eq!(result.len(), 1); assert_eq!(result[0], Ok(())); - assert_eq!(bank.get_signature(&tx.last_id, &tx.signature), Some(Ok(()))); + assert_eq!( + bank.get_signature(&tx.last_id, &tx.signatures[0]), + Some(Ok(())) + ); } struct Loader {