From c3323c0c36384130103506818dc752ecec559f2d Mon Sep 17 00:00:00 2001 From: Tao Zhu <82401714+taozhu-chicago@users.noreply.github.com> Date: Wed, 29 Nov 2023 18:30:59 -0600 Subject: [PATCH] Add test module to runtime-transaction (#34273) * basic tests --- Cargo.lock | 1 + runtime-transaction/Cargo.toml | 1 + .../src/runtime_transaction.rs | 110 ++++++++++++++++++ 3 files changed, 112 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 00dd4d1397..f00743fa29 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7074,6 +7074,7 @@ dependencies = [ "log", "rand 0.8.5", "rustc_version 0.4.0", + "solana-program", "solana-program-runtime", "solana-sdk", "thiserror", diff --git a/runtime-transaction/Cargo.toml b/runtime-transaction/Cargo.toml index 6503740d4d..947da05cc1 100644 --- a/runtime-transaction/Cargo.toml +++ b/runtime-transaction/Cargo.toml @@ -22,6 +22,7 @@ name = "solana_runtime_transaction" [dev-dependencies] bincode = { workspace = true } rand = { workspace = true } +solana-program ={ workspace = true } [package.metadata.docs.rs] targets = ["x86_64-unknown-linux-gnu"] diff --git a/runtime-transaction/src/runtime_transaction.rs b/runtime-transaction/src/runtime_transaction.rs index 8ddf004f8b..ef8bbbe1e7 100644 --- a/runtime-transaction/src/runtime_transaction.rs +++ b/runtime-transaction/src/runtime_transaction.rs @@ -106,3 +106,113 @@ impl RuntimeTransactionDynamic { Ok(()) } } + +#[cfg(test)] +mod tests { + use { + super::*, + solana_program::{ + system_instruction, + vote::{self, state::Vote}, + }, + solana_sdk::{ + compute_budget::ComputeBudgetInstruction, + message::Message, + signer::{keypair::Keypair, Signer}, + transaction::{Transaction, VersionedTransaction}, + }, + }; + + fn vote_sanitized_versioned_transaction() -> SanitizedVersionedTransaction { + let bank_hash = Hash::new_unique(); + let block_hash = Hash::new_unique(); + let vote_keypair = Keypair::new(); + let node_keypair = Keypair::new(); + let auth_keypair = Keypair::new(); + let votes = Vote::new(vec![1, 2, 3], bank_hash); + let vote_ix = + vote::instruction::vote(&vote_keypair.pubkey(), &auth_keypair.pubkey(), votes); + let mut vote_tx = Transaction::new_with_payer(&[vote_ix], Some(&node_keypair.pubkey())); + vote_tx.partial_sign(&[&node_keypair], block_hash); + vote_tx.partial_sign(&[&auth_keypair], block_hash); + + SanitizedVersionedTransaction::try_from(VersionedTransaction::from(vote_tx)).unwrap() + } + + fn non_vote_sanitized_versioned_transaction( + compute_unit_price: u64, + ) -> SanitizedVersionedTransaction { + let from_keypair = Keypair::new(); + let ixs = vec![ + system_instruction::transfer( + &from_keypair.pubkey(), + &solana_sdk::pubkey::new_rand(), + 1, + ), + ComputeBudgetInstruction::set_compute_unit_price(compute_unit_price), + ]; + let message = Message::new(&ixs, Some(&from_keypair.pubkey())); + let tx = Transaction::new(&[&from_keypair], message, Hash::new_unique()); + SanitizedVersionedTransaction::try_from(VersionedTransaction::from(tx)).unwrap() + } + + fn get_transaction_meta( + svt: SanitizedVersionedTransaction, + hash: Option, + is_simple_vote: Option, + ) -> TransactionMeta { + RuntimeTransactionStatic::try_from(svt, hash, is_simple_vote) + .unwrap() + .meta + } + + #[test] + fn test_new_runtime_transaction_static() { + let hash = Hash::new_unique(); + let compute_unit_price = 1_000; + + assert_eq!( + TransactionMeta { + message_hash: hash, + is_simple_vote_tx: false, + }, + get_transaction_meta( + non_vote_sanitized_versioned_transaction(compute_unit_price), + Some(hash), + None + ) + ); + + assert_eq!( + TransactionMeta { + message_hash: hash, + is_simple_vote_tx: true, + }, + get_transaction_meta( + non_vote_sanitized_versioned_transaction(compute_unit_price), + Some(hash), + Some(true), // override + ) + ); + + assert_eq!( + TransactionMeta { + message_hash: hash, + is_simple_vote_tx: true, + }, + get_transaction_meta(vote_sanitized_versioned_transaction(), Some(hash), None) + ); + + assert_eq!( + TransactionMeta { + message_hash: hash, + is_simple_vote_tx: false, + }, + get_transaction_meta( + vote_sanitized_versioned_transaction(), + Some(hash), + Some(false), // override + ) + ); + } +}