lssa/common/src/test_utils.rs

86 lines
2.3 KiB
Rust
Raw Normal View History

use nssa::AccountId;
2025-09-03 10:29:51 +03:00
use crate::{
HashType,
2025-09-03 10:29:51 +03:00
block::{Block, HashableBlockData},
transaction::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
2026-03-03 23:21:08 +03:00
#[must_use]
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
2026-03-10 00:17:43 +03:00
/// Produce dummy block with.
2025-08-05 14:59:20 +03:00
///
2026-03-10 00:17:43 +03:00
/// `id` - block id, provide zero for genesis.
2025-08-05 14:59:20 +03:00
///
2026-03-10 00:17:43 +03:00
/// `prev_hash` - hash of previous block, provide None for genesis.
2025-08-05 14:59:20 +03:00
///
2026-03-10 00:17:43 +03:00
/// `transactions` - vector of `EncodedTransaction` objects.
2026-03-03 23:21:08 +03:00
#[must_use]
2025-08-05 14:59:20 +03:00
pub fn produce_dummy_block(
id: u64,
prev_hash: Option<HashType>,
transactions: Vec<NSSATransaction>,
2025-08-05 14:59:20 +03:00
) -> Block {
let block_data = HashableBlockData {
block_id: id,
prev_block_hash: prev_hash.unwrap_or_default(),
2026-03-04 18:42:33 +03:00
timestamp: id.saturating_mul(100),
2025-08-05 14:59:20 +03:00
transactions,
};
2026-01-29 15:21:15 -03:00
block_data.into_pending_block(&sequencer_sign_key_for_testing(), [0; 32])
2025-08-05 14:59:20 +03:00
}
2026-03-03 23:21:08 +03:00
#[must_use]
pub fn produce_dummy_empty_transaction() -> NSSATransaction {
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);
NSSATransaction::Public(nssa_tx)
2025-08-05 14:59:20 +03:00
}
2026-03-03 23:21:08 +03:00
#[must_use]
2025-08-09 20:25:58 -03:00
pub fn create_transaction_native_token_transfer(
from: AccountId,
nonce: u128,
to: AccountId,
balance_to_move: u128,
2026-03-03 23:21:08 +03:00
signing_key: &nssa::PrivateKey,
) -> NSSATransaction {
let account_ids = vec![from, to];
2026-03-02 11:54:41 -05:00
let nonces = vec![nonce.into()];
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();
2026-03-03 23:21:08 +03:00
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);
NSSATransaction::Public(nssa_tx)
2025-08-05 15:44:52 +03:00
}