From 6de0417c4a6846d79ece5ddd1192d85fb17713d0 Mon Sep 17 00:00:00 2001 From: Nikolas Haimerl Date: Fri, 18 Aug 2023 14:20:30 +0000 Subject: [PATCH] split up transfer tests --- cycles-ledger/src/storage.rs | 4 +-- cycles-ledger/tests/tests.rs | 51 +++++++++++++++++++++++++++++++++++- 2 files changed, 52 insertions(+), 3 deletions(-) diff --git a/cycles-ledger/src/storage.rs b/cycles-ledger/src/storage.rs index 61f44a9..62e4b58 100644 --- a/cycles-ledger/src/storage.rs +++ b/cycles-ledger/src/storage.rs @@ -17,7 +17,7 @@ use crate::{ciborium_to_generic_value, compact_account, config, endpoints::Dedup const BLOCK_LOG_INDEX_MEMORY_ID: MemoryId = MemoryId::new(1); const BLOCK_LOG_DATA_MEMORY_ID: MemoryId = MemoryId::new(2); const BALANCES_MEMORY_ID: MemoryId = MemoryId::new(3); -const TRANSACTIONS_MEMORY_ID: MemoryId = MemoryId::new(4); +const TRANSACTION_MEMORY_ID: MemoryId = MemoryId::new(4); type VMem = VirtualMemory; @@ -166,7 +166,7 @@ thread_local! { }, blocks, balances: Balances::init(mm.get(BALANCES_MEMORY_ID)), - transactions: TransactionsLog::init(mm.get(TRANSACTIONS_MEMORY_ID)), + transactions: TransactionsLog::init(mm.get(TRANSACTION_MEMORY_ID)), }) }); } diff --git a/cycles-ledger/tests/tests.rs b/cycles-ledger/tests/tests.rs index 15b1710..222c973 100644 --- a/cycles-ledger/tests/tests.rs +++ b/cycles-ledger/tests/tests.rs @@ -472,7 +472,7 @@ fn test_send_fails() { } #[test] -fn test_transfer() { +fn test_basic_transfer() { let env = &new_state_machine(); let ledger_id = install_ledger(env); let depositor_id = install_depositor(env, ledger_id); @@ -551,6 +551,55 @@ fn test_transfer() { ) .unwrap_err() ); +} + +#[test] +fn test_deduplication() { + let env = &new_state_machine(); + let ledger_id = install_ledger(env); + let depositor_id = install_depositor(env, ledger_id); + let user1 = Account { + owner: Principal::from_slice(&[1]), + subaccount: None, + }; + let user2: Account = Account { + owner: Principal::from_slice(&[2]), + subaccount: None, + }; + let deposit_amount = 1_000_000_000; + deposit(env, depositor_id, user1, deposit_amount); + let transfer_amount = Nat::from(100_000); + + // If created_at_time is not set, the same transaction should be able to be sent multiple times + transfer( + env, + ledger_id, + user1, + TransferArg { + from_subaccount: None, + to: user2, + fee: None, + created_at_time: None, + memo: None, + amount: transfer_amount.clone(), + }, + ) + .unwrap(); + + transfer( + env, + ledger_id, + user1, + TransferArg { + from_subaccount: None, + to: user2, + fee: None, + created_at_time: None, + memo: None, + amount: transfer_amount.clone(), + }, + ) + .unwrap(); // Should not be able commit a transaction that was created in the future let mut now = env