diff --git a/Cargo.lock b/Cargo.lock index a50ce5b6e..ca5845eea 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4051,11 +4051,13 @@ dependencies = [ "borsh", "serde", "serde_derive", + "solana-address", "solana-example-mocks", "solana-frozen-abi", "solana-frozen-abi-macro", "solana-hash", "solana-instruction", + "solana-instruction-error", "solana-keypair", "solana-logger", "solana-message", diff --git a/transaction/Cargo.toml b/transaction/Cargo.toml index 54105bbd4..3d688836f 100644 --- a/transaction/Cargo.toml +++ b/transaction/Cargo.toml @@ -41,13 +41,14 @@ verify = ["blake3", "solana-signature/verify"] bincode = { workspace = true, optional = true } serde = { workspace = true, optional = true } serde_derive = { workspace = true, optional = true } +solana-address = { workspace = true } solana-frozen-abi = { workspace = true, optional = true } solana-frozen-abi-macro = { workspace = true, optional = true } solana-hash = { workspace = true } solana-instruction = { workspace = true } +solana-instruction-error = { workspace = true } solana-logger = { workspace = true, optional = true } solana-message = { workspace = true } -solana-pubkey = { workspace = true } solana-sanitize = { workspace = true } solana-sdk-ids = { workspace = true } solana-short-vec = { workspace = true, optional = true } diff --git a/transaction/src/lib.rs b/transaction/src/lib.rs index 0bc14bbf5..b9d409daa 100644 --- a/transaction/src/lib.rs +++ b/transaction/src/lib.rs @@ -115,22 +115,23 @@ use { serde_derive::{Deserialize, Serialize}, solana_short_vec as short_vec, }; +pub use { + solana_address::Address, + solana_instruction::{AccountMeta, Instruction}, + solana_instruction_error::InstructionError, + solana_message::{compiled_instruction::CompiledInstruction, Message, VersionedMessage}, + solana_signature::Signature, + solana_transaction_error::{TransactionError, TransactionResult}, +}; #[cfg(feature = "bincode")] -use { +pub use { solana_hash::Hash, solana_signer::{signers::Signers, SignerError}, }; use { - solana_instruction::Instruction, - solana_message::{ - compiled_instruction::CompiledInstruction, inline_nonce::is_advance_nonce_instruction_data, - Message, - }, - solana_pubkey::Pubkey, + solana_message::inline_nonce::is_advance_nonce_instruction_data, solana_sanitize::{Sanitize, SanitizeError}, solana_sdk_ids::system_program, - solana_signature::Signature, - solana_transaction_error::{TransactionError, TransactionResult as Result}, std::result, }; @@ -427,7 +428,7 @@ impl Transaction { /// # /// # Ok::<(), anyhow::Error>(()) /// ``` - pub fn new_with_payer(instructions: &[Instruction], payer: Option<&Pubkey>) -> Self { + pub fn new_with_payer(instructions: &[Instruction], payer: Option<&Address>) -> Self { let message = Message::new(instructions, payer); Self::new_unsigned(message) } @@ -509,7 +510,7 @@ impl Transaction { #[cfg(feature = "bincode")] pub fn new_signed_with_payer( instructions: &[Instruction], - payer: Option<&Pubkey>, + payer: Option<&Address>, signing_keypairs: &T, recent_blockhash: Hash, ) -> Self { @@ -535,9 +536,9 @@ impl Transaction { #[cfg(feature = "bincode")] pub fn new_with_compiled_instructions( from_keypairs: &T, - keys: &[Pubkey], + keys: &[Address], recent_blockhash: Hash, - program_ids: Vec, + program_ids: Vec
, instructions: Vec, ) -> Self { let mut account_keys = from_keypairs.pubkeys(); @@ -592,7 +593,7 @@ impl Transaction { /// Returns `None` if `instruction_index` is greater than or equal to the /// number of instructions in the transaction; or if `accounts_index` is /// greater than or equal to the number of accounts in the instruction. - pub fn key(&self, instruction_index: usize, accounts_index: usize) -> Option<&Pubkey> { + pub fn key(&self, instruction_index: usize, accounts_index: usize) -> Option<&Address> { self.key_index(instruction_index, accounts_index) .and_then(|account_keys_index| self.message.account_keys.get(account_keys_index)) } @@ -613,7 +614,7 @@ impl Transaction { /// Returns `None` if `instruction_index` is greater than or equal to the /// number of instructions in the transaction; or if `accounts_index` is /// greater than or equal to the number of accounts in the instruction. - pub fn signer_key(&self, instruction_index: usize, accounts_index: usize) -> Option<&Pubkey> { + pub fn signer_key(&self, instruction_index: usize, accounts_index: usize) -> Option<&Address> { match self.key_index(instruction_index, accounts_index) { None => None, Some(signature_index) => { @@ -976,7 +977,7 @@ impl Transaction { /// # Errors /// /// Returns [`TransactionError::SignatureFailure`] on error. - pub fn verify(&self) -> Result<()> { + pub fn verify(&self) -> TransactionResult<()> { let message_bytes = self.message_data(); if !self ._verify_with_results(&message_bytes) @@ -995,7 +996,7 @@ impl Transaction { /// # Errors /// /// Returns [`TransactionError::SignatureFailure`] on error. - pub fn verify_and_hash_message(&self) -> Result { + pub fn verify_and_hash_message(&self) -> TransactionResult { let message_bytes = self.message_data(); if !self ._verify_with_results(&message_bytes) @@ -1029,7 +1030,10 @@ impl Transaction { /// Get the positions of the pubkeys in `account_keys` associated with signing keypairs. /// /// [`account_keys`]: Message::account_keys - pub fn get_signing_keypair_positions(&self, pubkeys: &[Pubkey]) -> Result>> { + pub fn get_signing_keypair_positions( + &self, + pubkeys: &[Address], + ) -> TransactionResult>> { if self.message.account_keys.len() < self.message.header.num_required_signatures as usize { return Err(TransactionError::InvalidAccountIndex); } @@ -1044,7 +1048,10 @@ impl Transaction { #[cfg(feature = "verify")] /// Replace all the signatures and pubkeys. - pub fn replace_signatures(&mut self, signers: &[(Pubkey, Signature)]) -> Result<()> { + pub fn replace_signatures( + &mut self, + signers: &[(Address, Signature)], + ) -> TransactionResult<()> { let num_required_signatures = self.message.header.num_required_signatures as usize; if signers.len() != num_required_signatures || self.signatures.len() != num_required_signatures @@ -1110,7 +1117,7 @@ mod tests { std::mem::size_of, }; - fn get_program_id(tx: &Transaction, instruction_index: usize) -> &Pubkey { + fn get_program_id(tx: &Transaction, instruction_index: usize) -> &Address { let message = tx.message(); let instruction = &message.instructions[instruction_index]; instruction.program_id(&message.account_keys) @@ -1179,17 +1186,17 @@ mod tests { &[&key], &[], Hash::default(), - vec![Pubkey::default()], + vec![Address::default()], instructions, ); - assert_eq!(*get_program_id(&tx, 0), Pubkey::default()); + assert_eq!(*get_program_id(&tx, 0), Address::default()); assert_eq!(tx.sanitize(), Err(SanitizeError::IndexOutOfBounds)); } #[test] fn test_sanitize_txs() { let key = Keypair::new(); - let id0 = Pubkey::default(); + let id0 = Address::default(); let program_id = solana_pubkey::new_rand(); let ix = Instruction::new_with_bincode( program_id, @@ -1238,7 +1245,7 @@ mod tests { tx = o.clone(); tx.message.header.num_readonly_signed_accounts = 2; tx.message.header.num_readonly_unsigned_accounts = 3; - tx.message.account_keys.resize(4, Pubkey::default()); + tx.message.account_keys.resize(4, Address::default()); assert_eq!(tx.sanitize(), Err(SanitizeError::IndexOutOfBounds)); tx = o; @@ -1258,12 +1265,12 @@ mod tests { .as_ref(), ) .unwrap(); - let to = Pubkey::from([ + let to = Address::from([ 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::from([ + let program_id = Address::from([ 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, ]); @@ -1324,7 +1331,7 @@ mod tests { + num_required_sigs_size + num_readonly_accounts_size + len_size - + (tx.message.account_keys.len() * size_of::()) + + (tx.message.account_keys.len() * size_of::
()) + blockhash_size + len_size + expected_instruction_size; @@ -1372,7 +1379,7 @@ mod tests { let keypair = Keypair::new(); let fee_payer = solana_pubkey::new_rand(); let ix = Instruction::new_with_bincode( - Pubkey::default(), + Address::default(), &0, vec![AccountMeta::new(fee_payer, true)], ); @@ -1386,7 +1393,7 @@ mod tests { let keypair1 = Keypair::new(); let keypair2 = Keypair::new(); let ix = Instruction::new_with_bincode( - Pubkey::default(), + Address::default(), &0, vec![ AccountMeta::new(keypair0.pubkey(), true), @@ -1412,7 +1419,7 @@ mod tests { #[test] #[should_panic] fn test_transaction_missing_keypair() { - let program_id = Pubkey::default(); + let program_id = Address::default(); let keypair0 = Keypair::new(); let id0 = keypair0.pubkey(); let ix = Instruction::new_with_bincode(program_id, &0, vec![AccountMeta::new(id0, true)]); @@ -1423,9 +1430,9 @@ mod tests { #[test] #[should_panic] fn test_transaction_wrong_key() { - let program_id = Pubkey::default(); + let program_id = Address::default(); let keypair0 = Keypair::new(); - let wrong_id = Pubkey::default(); + let wrong_id = Address::default(); let ix = Instruction::new_with_bincode(program_id, &0, vec![AccountMeta::new(wrong_id, true)]); let message = Message::new(&[ix], Some(&wrong_id)); @@ -1434,7 +1441,7 @@ mod tests { #[test] fn test_transaction_correct_key() { - let program_id = Pubkey::default(); + let program_id = Address::default(); let keypair0 = Keypair::new(); let id0 = keypair0.pubkey(); let ix = Instruction::new_with_bincode(program_id, &0, vec![AccountMeta::new(id0, true)]); @@ -1450,7 +1457,7 @@ mod tests { #[test] fn test_transaction_instruction_with_duplicate_keys() { - let program_id = Pubkey::default(); + let program_id = Address::default(); let keypair0 = Keypair::new(); let id0 = keypair0.pubkey(); let id1 = solana_pubkey::new_rand(); @@ -1476,7 +1483,7 @@ mod tests { #[test] fn test_try_sign_dyn_keypairs() { - let program_id = Pubkey::default(); + let program_id = Address::default(); let keypair = Keypair::new(); let pubkey = keypair.pubkey(); let presigner_keypair = Keypair::new(); @@ -1524,7 +1531,7 @@ mod tests { ); } - fn nonced_transfer_tx() -> (Pubkey, Pubkey, Transaction) { + fn nonced_transfer_tx() -> (Address, Address, Transaction) { let from_keypair = Keypair::new(); let from_pubkey = from_keypair.pubkey(); let nonce_keypair = Keypair::new(); @@ -1595,7 +1602,7 @@ mod tests { fn tx_keypair_pubkey_mismatch() { let from_keypair = Keypair::new(); let from_pubkey = from_keypair.pubkey(); - let to_pubkey = Pubkey::new_unique(); + let to_pubkey = Address::new_unique(); let instructions = [system_instruction::transfer(&from_pubkey, &to_pubkey, 42)]; let mut tx = Transaction::new_with_payer(&instructions, Some(&from_pubkey)); let unused_keypair = Keypair::new(); @@ -1625,7 +1632,7 @@ mod tests { #[test] fn test_replace_signatures() { - let program_id = Pubkey::default(); + let program_id = Address::default(); let keypair0 = Keypair::new(); let keypair1 = Keypair::new(); let pubkey0 = keypair0.pubkey(); diff --git a/transaction/src/sanitized.rs b/transaction/src/sanitized.rs index 638885be3..66375c47e 100644 --- a/transaction/src/sanitized.rs +++ b/transaction/src/sanitized.rs @@ -1,5 +1,6 @@ use { crate::versioned::{sanitized::SanitizedVersionedTransaction, VersionedTransaction}, + solana_address::Address, solana_hash::Hash, solana_message::{ legacy, @@ -7,9 +8,8 @@ use { AddressLoader, LegacyMessage, SanitizedMessage, SanitizedVersionedMessage, VersionedMessage, }, - solana_pubkey::Pubkey, solana_signature::Signature, - solana_transaction_error::{TransactionError, TransactionResult as Result}, + solana_transaction_error::{TransactionError, TransactionResult}, std::collections::HashSet, }; #[cfg(feature = "blake3")] @@ -33,9 +33,9 @@ pub struct SanitizedTransaction { #[derive(Debug, Clone, Default, Eq, PartialEq)] pub struct TransactionAccountLocks<'a> { /// List of readonly account key locks - pub readonly: Vec<&'a Pubkey>, + pub readonly: Vec<&'a Address>, /// List of writable account key locks - pub writable: Vec<&'a Pubkey>, + pub writable: Vec<&'a Address>, } /// Type that represents whether the transaction message has been precomputed or @@ -60,8 +60,8 @@ impl SanitizedTransaction { message_hash: Hash, is_simple_vote_tx: bool, address_loader: impl AddressLoader, - reserved_account_keys: &HashSet, - ) -> Result { + reserved_account_keys: &HashSet
, + ) -> TransactionResult { let signatures = tx.signatures; let SanitizedVersionedMessage { message } = tx.message; let message = match message { @@ -96,8 +96,8 @@ impl SanitizedTransaction { message_hash: impl Into, is_simple_vote_tx: Option, address_loader: impl AddressLoader, - reserved_account_keys: &HashSet, - ) -> Result { + reserved_account_keys: &HashSet
, + ) -> TransactionResult { let sanitized_versioned_tx = SanitizedVersionedTransaction::try_from(tx)?; let is_simple_vote_tx = is_simple_vote_tx.unwrap_or_else(|| { crate::simple_vote_transaction_checker::is_simple_vote_transaction( @@ -121,8 +121,8 @@ impl SanitizedTransaction { #[cfg(feature = "blake3")] pub fn try_from_legacy_transaction( tx: Transaction, - reserved_account_keys: &HashSet, - ) -> Result { + reserved_account_keys: &HashSet
, + ) -> TransactionResult { tx.sanitize()?; Ok(Self { @@ -150,7 +150,7 @@ impl SanitizedTransaction { message_hash: Hash, is_simple_vote_tx: bool, signatures: Vec, - ) -> Result { + ) -> TransactionResult { VersionedTransaction::sanitize_signatures_inner( usize::from(message.header().num_required_signatures), message.static_account_keys().len(), @@ -216,7 +216,7 @@ impl SanitizedTransaction { pub fn get_account_locks( &self, tx_account_lock_limit: usize, - ) -> Result> { + ) -> TransactionResult> { Self::validate_account_locks(self.message(), tx_account_lock_limit)?; Ok(self.get_account_locks_unchecked()) } @@ -254,7 +254,7 @@ impl SanitizedTransaction { /// If the transaction uses a durable nonce, return the pubkey of the nonce account #[cfg(feature = "bincode")] - pub fn get_durable_nonce(&self) -> Option<&Pubkey> { + pub fn get_durable_nonce(&self) -> Option<&Address> { self.message.get_durable_nonce() } @@ -269,7 +269,7 @@ impl SanitizedTransaction { #[cfg(feature = "verify")] /// Verify the transaction signatures - pub fn verify(&self) -> Result<()> { + pub fn verify(&self) -> TransactionResult<()> { let message_bytes = self.message_data(); if self .signatures @@ -288,7 +288,7 @@ impl SanitizedTransaction { pub fn validate_account_locks( message: &SanitizedMessage, tx_account_lock_limit: usize, - ) -> Result<()> { + ) -> TransactionResult<()> { if message.has_duplicates() { Err(TransactionError::AccountLoadedTwice) } else if message.account_keys().len() > tx_account_lock_limit { @@ -403,9 +403,9 @@ mod tests { num_readonly_unsigned_accounts: 1, }, account_keys: vec![ - Pubkey::new_unique(), - Pubkey::new_unique(), - Pubkey::new_unique(), + Address::new_unique(), + Address::new_unique(), + Address::new_unique(), ], ..legacy::Message::default() }, diff --git a/transaction/src/simple_vote_transaction_checker.rs b/transaction/src/simple_vote_transaction_checker.rs index bece18367..96b4cb9a6 100644 --- a/transaction/src/simple_vote_transaction_checker.rs +++ b/transaction/src/simple_vote_transaction_checker.rs @@ -1,6 +1,6 @@ use { - crate::versioned::sanitized::SanitizedVersionedTransaction, solana_message::VersionedMessage, - solana_pubkey::Pubkey, solana_signature::Signature, + crate::versioned::sanitized::SanitizedVersionedTransaction, solana_address::Address, + solana_message::VersionedMessage, solana_signature::Signature, }; /// Simple vote transaction meets these conditions: @@ -36,7 +36,7 @@ pub fn is_simple_vote_transaction( pub fn is_simple_vote_transaction_impl<'a>( signatures: &[Signature], is_legacy_message: bool, - mut instruction_programs: impl Iterator, + mut instruction_programs: impl Iterator, ) -> bool { signatures.len() < 3 && is_legacy_message