Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 21 additions & 37 deletions minter/src/consolidate/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@ use crate::{
},
};
use assert_matches::assert_matches;
use sol_rpc_types::{ConfirmedBlock, MultiRpcResult, RpcError, Signature, Slot};

type SlotResult = MultiRpcResult<Slot>;
type BlockResult = MultiRpcResult<ConfirmedBlock>;
type SendTransactionResult = MultiRpcResult<Signature>;
use sol_rpc_types::{MultiRpcResult, RpcError, Signature};

#[tokio::test]
async fn should_return_early_if_no_deposits_to_consolidate() {
Expand Down Expand Up @@ -55,11 +51,8 @@ async fn should_return_early_if_fetching_blockhash_fails() {

add_funds_to_consolidate(&[(deposit_id(0), 1_000_000_000)]);

let error = SlotResult::Consistent(Err(RpcError::ValidationError("Error".to_string())));
let runtime = TestCanisterRuntime::new()
.add_stub_response(error.clone())
.add_stub_response(error.clone())
.add_stub_response(error);
.add_n_get_slot_error(RpcError::ValidationError("Error".to_string()), 3);

consolidate_deposits(runtime).await;

Expand All @@ -81,11 +74,9 @@ async fn should_submit_single_consolidation_request() {
let runtime = TestCanisterRuntime::new()
.with_increasing_time()
// get_recent_slot_and_blockhash calls (get_recent_block internally calls getSlot then getBlock)
.add_stub_response(SlotResult::Consistent(Ok(slot)))
.add_stub_response(BlockResult::Consistent(Ok(confirmed_block())))
.add_stub_response(SendTransactionResult::Consistent(Ok(
fee_payer_signature.into()
)))
.add_get_slot_response(slot)
.add_get_block_response(confirmed_block())
.add_send_transaction_response(fee_payer_signature)
.add_signature(fee_payer_signature.into());

consolidate_deposits(runtime).await;
Expand Down Expand Up @@ -118,10 +109,10 @@ async fn should_record_events_even_if_transaction_submission_fails() {
let runtime = TestCanisterRuntime::new()
.with_increasing_time()
// get_recent_slot_and_blockhash calls
.add_stub_response(SlotResult::Consistent(Ok(slot)))
.add_stub_response(BlockResult::Consistent(Ok(confirmed_block())))
.add_get_slot_response(slot)
.add_get_block_response(confirmed_block())
// Transaction submission fails
.add_stub_response(SendTransactionResult::Inconsistent(vec![]))
.add_stub_response(MultiRpcResult::<Signature>::Inconsistent(vec![]))
.add_signature(fee_payer_signature.into());

consolidate_deposits(runtime).await;
Expand Down Expand Up @@ -158,14 +149,10 @@ async fn should_submit_multiple_consolidation_batches() {
let mut runtime = TestCanisterRuntime::new()
.with_increasing_time()
// get_recent_slot_and_blockhash calls
.add_stub_response(SlotResult::Consistent(Ok(slot)))
.add_stub_response(BlockResult::Consistent(Ok(confirmed_block())))
.add_stub_response(SendTransactionResult::Consistent(Ok(
fee_payer_signature_1.into()
)))
.add_stub_response(SendTransactionResult::Consistent(Ok(
fee_payer_signature_2.into()
)));
.add_get_slot_response(slot)
.add_get_block_response(confirmed_block())
.add_send_transaction_response(fee_payer_signature_1)
.add_send_transaction_response(fee_payer_signature_2);

for i in 0..(2 + NUM_DEPOSITS) {
runtime = runtime.add_signature(signature(i).into());
Expand Down Expand Up @@ -229,11 +216,9 @@ async fn should_consolidate_multiple_deposits_to_same_account_in_single_transfer
let slot = 100;
let runtime = TestCanisterRuntime::new()
.with_increasing_time()
.add_stub_response(SlotResult::Consistent(Ok(slot)))
.add_stub_response(BlockResult::Consistent(Ok(confirmed_block())))
.add_stub_response(SendTransactionResult::Consistent(Ok(
fee_payer_signature.into()
)))
.add_get_slot_response(slot)
.add_get_block_response(confirmed_block())
.add_send_transaction_response(fee_payer_signature)
.add_signature(fee_payer_signature.into());

consolidate_deposits(runtime).await;
Expand Down Expand Up @@ -271,11 +256,10 @@ async fn should_reschedule_until_all_deposits_consolidated() {
// Round 1: processes MAX_CONCURRENT_RPC_CALLS batches, 1 deposit remains → reschedule
let mut runtime = TestCanisterRuntime::new()
.with_increasing_time()
.add_stub_response(SlotResult::Consistent(Ok(slot)))
.add_stub_response(BlockResult::Consistent(Ok(confirmed_block())));
.add_get_slot_response(slot)
.add_get_block_response(confirmed_block());
for i in 0..MAX_CONCURRENT_RPC_CALLS {
runtime =
runtime.add_stub_response(SendTransactionResult::Consistent(Ok(signature(i).into())));
runtime = runtime.add_send_transaction_response(signature(i));
}
for i in 0..(MAX_CONCURRENT_RPC_CALLS + num_deposits) {
runtime = runtime.add_signature(signature(i).into());
Expand All @@ -293,9 +277,9 @@ async fn should_reschedule_until_all_deposits_consolidated() {
let last_sig = signature(num_deposits);
let runtime = TestCanisterRuntime::new()
.with_increasing_time()
.add_stub_response(SlotResult::Consistent(Ok(slot)))
.add_stub_response(BlockResult::Consistent(Ok(confirmed_block())))
.add_stub_response(SendTransactionResult::Consistent(Ok(last_sig.into())))
.add_get_slot_response(slot)
.add_get_block_response(confirmed_block())
.add_send_transaction_response(last_sig)
.add_signature(last_sig.into());

consolidate_deposits(runtime.clone()).await;
Expand Down
57 changes: 10 additions & 47 deletions minter/src/deposit/manual/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,8 @@ use candid_parser::Principal;
use cksol_types::{DepositStatus, InsufficientCyclesError, ProcessDepositError};
use cksol_types_internal::InitArgs;
use ic_canister_runtime::IcError;
use icrc_ledger_types::icrc1::{
account::Account,
transfer::{BlockIndex, TransferError},
};
use sol_rpc_types::{EncodedConfirmedTransactionWithStatusMeta, Lamport, MultiRpcResult};

type GetTransactionResult = MultiRpcResult<Option<EncodedConfirmedTransactionWithStatusMeta>>;
type MintResult = Result<BlockIndex, TransferError>;
use icrc_ledger_types::icrc1::{account::Account, transfer::TransferError};
use sol_rpc_types::{EncodedConfirmedTransactionWithStatusMeta, Lamport};

mod process_deposit_tests {
use super::*;
Expand Down Expand Up @@ -89,7 +83,7 @@ mod process_deposit_tests {
init_state();
init_schnorr_master_key();

let runtime = rejected_runtime().add_get_transaction_not_found_response();
let runtime = rejected_runtime().add_get_transaction_not_found();

let result = process_deposit(
runtime,
Expand Down Expand Up @@ -158,7 +152,7 @@ mod process_deposit_tests {
init_schnorr_master_key();

let runtime = runtime(legacy_deposit_transaction())
.add_mint_response(Err(TransferError::TemporarilyUnavailable));
.add_icrc1_transfer_response(Err(TransferError::TemporarilyUnavailable));

let result = process_deposit(
runtime,
Expand All @@ -181,7 +175,7 @@ mod process_deposit_tests {

// First call: makes JSON-RPC call and attempts to mint
let runtime = runtime(legacy_deposit_transaction())
.add_mint_response(Err(TransferError::TemporarilyUnavailable));
.add_icrc1_transfer_response(Err(TransferError::TemporarilyUnavailable));
let result = process_deposit(
runtime,
DEPOSITOR_ACCOUNT,
Expand All @@ -194,7 +188,7 @@ mod process_deposit_tests {
// additional JSON-RPC calls
let runtime = TestCanisterRuntime::new()
.with_increasing_time()
.add_mint_response(Ok(BLOCK_INDEX.into()));
.add_icrc1_transfer_response(Ok(BLOCK_INDEX.into()));
let result = process_deposit(
runtime,
DEPOSITOR_ACCOUNT,
Expand Down Expand Up @@ -228,7 +222,7 @@ mod process_deposit_tests {
] {
reset_events();

let runtime = runtime(transaction).add_mint_response(Ok(block_index.into()));
let runtime = runtime(transaction).add_icrc1_transfer_response(Ok(block_index.into()));

let result = process_deposit(runtime, DEPOSITOR_ACCOUNT, signature).await;

Expand Down Expand Up @@ -270,8 +264,8 @@ mod process_deposit_tests {
init_schnorr_master_key();

// Successful mint
let runtime =
runtime(legacy_deposit_transaction()).add_mint_response(Ok(BLOCK_INDEX.into()));
let runtime = runtime(legacy_deposit_transaction())
.add_icrc1_transfer_response(Ok(BLOCK_INDEX.into()));
let result = process_deposit(
runtime,
DEPOSITOR_ACCOUNT,
Expand Down Expand Up @@ -370,7 +364,7 @@ mod process_deposit_tests {

for i in 0..3 {
let runtime = runtime(deposit_transaction_to_multiple_accounts())
.add_mint_response(Ok(BLOCK_INDEXES[i].into()));
.add_icrc1_transfer_response(Ok(BLOCK_INDEXES[i].into()));
let result = process_deposit(
runtime,
ACCOUNTS[i],
Expand Down Expand Up @@ -435,35 +429,4 @@ mod process_deposit_tests {
.add_msg_cycles_refunded(GET_TRANSACTION_CYCLES - RPC_COST)
.add_msg_cycles_accept(RPC_COST + consolidation_fee)
}

trait DepositRuntimeExt: Sized {
fn add_get_transaction_response(
self,
tx: impl TryInto<EncodedConfirmedTransactionWithStatusMeta>,
) -> Self;
fn add_get_transaction_not_found_response(self) -> Self;
fn add_mint_response(self, result: MintResult) -> Self;
}

impl DepositRuntimeExt for TestCanisterRuntime {
fn add_get_transaction_response(
self,
response: impl TryInto<EncodedConfirmedTransactionWithStatusMeta>,
) -> Self {
self.add_stub_response(GetTransactionResult::Consistent(Ok(Some(
response
.try_into()
.ok()
.expect("failed to convert transaction"),
))))
}

fn add_get_transaction_not_found_response(self) -> Self {
self.add_stub_response(GetTransactionResult::Consistent(Ok(None)))
}

fn add_mint_response(self, result: MintResult) -> Self {
self.add_stub_response(result)
}
}
}
Loading
Loading