Skip to content

Commit f653a15

Browse files
core: remove literal nonces from signed transaction tests (#144)
* core: normalize signed transaction test nonces * core: eliminate literal nonce increments in tests
1 parent 1d56068 commit f653a15

1 file changed

Lines changed: 30 additions & 12 deletions

File tree

crates/core/src/types/transaction.rs

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -789,11 +789,18 @@ fn verifier_config_for(threat_level: QuantumThreatLevel, chain_id: u64) -> Verif
789789
#[cfg(test)]
790790
mod tests {
791791
use super::*;
792+
use std::sync::atomic::{AtomicU64, Ordering};
793+
794+
static NEXT_TEST_NONCE: AtomicU64 = AtomicU64::new(1);
792795

793796
fn create_test_keypair() -> HybridKeyPair {
794797
HybridKeyPair::generate().unwrap()
795798
}
796799

800+
fn next_test_nonce() -> u64 {
801+
NEXT_TEST_NONCE.fetch_add(1, Ordering::Relaxed)
802+
}
803+
797804
#[test]
798805
fn test_transfer_transaction() {
799806
let keypair = create_test_keypair();
@@ -810,8 +817,9 @@ mod tests {
810817
let keypair = create_test_keypair();
811818
let sender = Address::from_public_key(keypair.public_key());
812819
let recipient = Address::from_bytes([0x42; 20], super::super::address::AddressType::User);
820+
let nonce = next_test_nonce();
813821

814-
let tx = Transaction::transfer(sender, recipient, 1000, 0);
822+
let tx = Transaction::transfer(sender, recipient, 1000, nonce);
815823
let signed = tx.sign(&keypair).unwrap();
816824

817825
assert!(signed.verify().unwrap());
@@ -822,8 +830,9 @@ mod tests {
822830
let keypair = create_test_keypair();
823831
let sender = Address::from_public_key(keypair.public_key());
824832
let recipient = Address::from_bytes([0x42; 20], super::super::address::AddressType::User);
833+
let nonce = next_test_nonce();
825834

826-
let tx = Transaction::transfer(sender, recipient, 1000, 0);
835+
let tx = Transaction::transfer(sender, recipient, 1000, nonce);
827836
let signed = tx.sign(&keypair).unwrap();
828837

829838
// Should verify at all threat levels
@@ -838,9 +847,10 @@ mod tests {
838847
let wrong_sender =
839848
Address::from_bytes([0x11; 20], super::super::address::AddressType::User);
840849
let recipient = Address::from_bytes([0x42; 20], super::super::address::AddressType::User);
850+
let nonce = next_test_nonce();
841851

842852
// Create transaction with wrong sender
843-
let tx = Transaction::transfer(wrong_sender, recipient, 1000, 0);
853+
let tx = Transaction::transfer(wrong_sender, recipient, 1000, nonce);
844854
let signed = tx.sign(&keypair).unwrap();
845855

846856
// Verification should fail because sender doesn't match public key
@@ -852,12 +862,13 @@ mod tests {
852862
let keypair = create_test_keypair();
853863
let sender = Address::from_public_key(keypair.public_key());
854864
let recipient = Address::from_bytes([0x42; 20], super::super::address::AddressType::User);
865+
let nonce = next_test_nonce();
855866

856-
let tx = Transaction::transfer(sender, recipient, 1000, 0);
867+
let tx = Transaction::transfer(sender, recipient, 1000, nonce);
857868
let id = tx.id();
858869

859870
// Same transaction should have same ID
860-
let tx2 = Transaction::transfer(sender, recipient, 1000, 0);
871+
let tx2 = Transaction::transfer(sender, recipient, 1000, nonce);
861872
assert_eq!(id.to_hex(), tx2.id().to_hex());
862873
}
863874

@@ -866,15 +877,18 @@ mod tests {
866877
let keypair = create_test_keypair();
867878
let sender = Address::from_public_key(keypair.public_key());
868879
let recipient = Address::from_bytes([0x42; 20], super::super::address::AddressType::User);
880+
let first_nonce = next_test_nonce();
881+
let second_nonce = next_test_nonce();
882+
let third_nonce = next_test_nonce();
869883

870884
// Create multiple transactions
871-
let tx1 = Transaction::transfer(sender, recipient, 100, 0)
885+
let tx1 = Transaction::transfer(sender, recipient, 100, first_nonce)
872886
.sign(&keypair)
873887
.unwrap();
874-
let tx2 = Transaction::transfer(sender, recipient, 200, 1)
888+
let tx2 = Transaction::transfer(sender, recipient, 200, second_nonce)
875889
.sign(&keypair)
876890
.unwrap();
877-
let tx3 = Transaction::transfer(sender, recipient, 300, 2)
891+
let tx3 = Transaction::transfer(sender, recipient, 300, third_nonce)
878892
.sign(&keypair)
879893
.unwrap();
880894

@@ -892,13 +906,16 @@ mod tests {
892906
let keypair = create_test_keypair();
893907
let sender = Address::from_public_key(keypair.public_key());
894908
let recipient = Address::from_bytes([0x42; 20], super::super::address::AddressType::User);
909+
let active_nonce = next_test_nonce();
910+
let expired_nonce = next_test_nonce();
911+
let future_nonce = next_test_nonce();
895912

896913
// Non-expiring transaction
897-
let tx1 = Transaction::transfer(sender, recipient, 1000, 0);
914+
let tx1 = Transaction::transfer(sender, recipient, 1000, active_nonce);
898915
assert!(!tx1.is_expired());
899916

900917
// Expired transaction
901-
let tx2 = Transaction::transfer(sender, recipient, 1000, 0).with_expiry(1);
918+
let tx2 = Transaction::transfer(sender, recipient, 1000, expired_nonce).with_expiry(1);
902919
assert!(tx2.is_expired());
903920

904921
// Future expiry
@@ -907,7 +924,7 @@ mod tests {
907924
.unwrap()
908925
.as_secs()
909926
+ 3600;
910-
let tx3 = Transaction::transfer(sender, recipient, 1000, 0).with_expiry(future);
927+
let tx3 = Transaction::transfer(sender, recipient, 1000, future_nonce).with_expiry(future);
911928
assert!(!tx3.is_expired());
912929
}
913930

@@ -917,8 +934,9 @@ mod tests {
917934
let sender = Address::from_public_key(keypair.public_key());
918935
let model_hash = sha256(b"model weights");
919936
let input_hash = sha256(b"input data");
937+
let nonce = next_test_nonce();
920938

921-
let tx = Transaction::compute_job(sender, model_hash, input_hash, 0);
939+
let tx = Transaction::compute_job(sender, model_hash, input_hash, nonce);
922940
assert_eq!(tx.tx_type, TransactionType::ComputeJob);
923941
assert!(tx.tx_type.requires_compute());
924942

0 commit comments

Comments
 (0)