lssa/common/src/test_utils.rs

79 lines
2.3 KiB
Rust
Raw Normal View History

2025-09-03 10:29:51 +03:00
use crate::{
block::{Block, HashableBlockData},
2025-09-08 10:11:04 +03:00
transaction::{EncodedTransaction, NSSATransaction},
2025-09-03 10:29:51 +03:00
};
2025-11-26 00:27:20 +03:00
// Helpers
2025-09-03 10:29:51 +03:00
pub fn sequencer_sign_key_for_testing() -> nssa::PrivateKey {
nssa::PrivateKey::try_new([37; 32]).unwrap()
}
2025-08-05 14:59:20 +03:00
2025-11-26 00:27:20 +03:00
// Dummy producers
2025-08-05 14:59:20 +03:00
2025-11-26 00:27:20 +03:00
/// Produce dummy block with
2025-08-05 14:59:20 +03:00
///
/// `id` - block id, provide zero for genesis
///
/// `prev_hash` - hash of previous block, provide None for genesis
///
2025-09-08 10:11:04 +03:00
/// `transactions` - vector of `EncodedTransaction` objects
2025-08-05 14:59:20 +03:00
pub fn produce_dummy_block(
id: u64,
prev_hash: Option<[u8; 32]>,
2025-09-08 10:11:04 +03:00
transactions: Vec<EncodedTransaction>,
2025-08-05 14:59:20 +03:00
) -> Block {
let block_data = HashableBlockData {
block_id: id,
prev_block_hash: prev_hash.unwrap_or_default(),
2025-09-03 10:29:51 +03:00
timestamp: id * 100,
2025-08-05 14:59:20 +03:00
transactions,
};
2025-09-03 10:29:51 +03:00
block_data.into_block(&sequencer_sign_key_for_testing())
2025-08-05 14:59:20 +03:00
}
2025-09-08 10:11:04 +03:00
pub fn produce_dummy_empty_transaction() -> EncodedTransaction {
2025-08-10 00:53:53 -03:00
let program_id = nssa::program::Program::authenticated_transfer_program().id();
let account_ids = vec![];
let nonces = vec![];
let instruction_data: u128 = 0;
let message = nssa::public_transaction::Message::try_new(
program_id,
account_ids,
nonces,
instruction_data,
)
.unwrap();
2025-08-11 20:38:29 -03:00
let private_key = nssa::PrivateKey::try_new([1; 32]).unwrap();
let witness_set = nssa::public_transaction::WitnessSet::for_message(&message, &[&private_key]);
2025-09-03 10:29:51 +03:00
let nssa_tx = nssa::PublicTransaction::new(message, witness_set);
2025-09-08 10:11:04 +03:00
EncodedTransaction::from(NSSATransaction::Public(nssa_tx))
2025-08-05 14:59:20 +03:00
}
2025-08-09 20:25:58 -03:00
pub fn create_transaction_native_token_transfer(
2025-08-05 15:44:52 +03:00
from: [u8; 32],
nonce: u128,
2025-08-05 15:44:52 +03:00
to: [u8; 32],
balance_to_move: u128,
signing_key: nssa::PrivateKey,
2025-09-08 10:11:04 +03:00
) -> EncodedTransaction {
let account_ids = vec![nssa::AccountId::new(from), nssa::AccountId::new(to)];
let nonces = vec![nonce];
2025-08-10 00:53:53 -03:00
let program_id = nssa::program::Program::authenticated_transfer_program().id();
let message = nssa::public_transaction::Message::try_new(
program_id,
account_ids,
nonces,
balance_to_move,
)
.unwrap();
let witness_set = nssa::public_transaction::WitnessSet::for_message(&message, &[&signing_key]);
2025-09-03 10:29:51 +03:00
let nssa_tx = nssa::PublicTransaction::new(message, witness_set);
2025-09-08 10:11:04 +03:00
EncodedTransaction::from(NSSATransaction::Public(nssa_tx))
2025-08-05 15:44:52 +03:00
}