From c6ea557a48c9127e20234d515b7ed634b902ff6f Mon Sep 17 00:00:00 2001 From: Nikolas Haimerl Date: Wed, 9 Aug 2023 07:47:31 +0000 Subject: [PATCH 1/2] fix icrc1 add balance --- Cargo.lock | 170 +++++++++++++++++++++++++++++++++- cycles-ledger/Cargo.toml | 3 + cycles-ledger/src/storage.rs | 4 +- cycles-ledger/tests/client.rs | 4 +- cycles-ledger/tests/tests.rs | 32 ++++++- depositor/src/main.rs | 2 +- 6 files changed, 205 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 738c282..af68b6c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -32,6 +32,17 @@ dependencies = [ "term", ] +[[package]] +name = "async-trait" +version = "0.1.72" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc6dde6e4ed435a4c1ee4e73592f5ba9da2151af10076cc04858746af9352d09" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.28", +] + [[package]] name = "autocfg" version = "1.1.0" @@ -244,11 +255,14 @@ dependencies = [ "ciborium", "depositor", "escargot", + "futures", "ic-cdk 0.6.10", "ic-cdk-macros", "ic-stable-structures", "ic-test-state-machine-client", "icrc-ledger-types", + "icrc1-test-env-state-machine", + "icrc1-test-suite", "minicbor", "num-bigint", "num-traits", @@ -378,6 +392,95 @@ version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" +[[package]] +name = "futures" +version = "0.3.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23342abe12aba583913b2e62f22225ff9c950774065e4bfb61a19cd9770fec40" +dependencies = [ + "futures-channel", + "futures-core", + "futures-executor", + "futures-io", + "futures-sink", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-channel" +version = "0.3.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "955518d47e09b25bbebc7a18df10b81f0c766eaf4c4f1cccef2fca5f2a4fb5f2" +dependencies = [ + "futures-core", + "futures-sink", +] + +[[package]] +name = "futures-core" +version = "0.3.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4bca583b7e26f571124fe5b7561d49cb2868d79116cfa0eefce955557c6fee8c" + +[[package]] +name = "futures-executor" +version = "0.3.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ccecee823288125bd88b4d7f565c9e58e41858e47ab72e8ea2d64e93624386e0" +dependencies = [ + "futures-core", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-io" +version = "0.3.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4fff74096e71ed47f8e023204cfd0aa1289cd54ae5430a9523be060cdb849964" + +[[package]] +name = "futures-macro" +version = "0.3.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "89ca545a94061b6365f2c7355b4b32bd20df3ff95f02da9329b34ccc3bd6ee72" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.28", +] + +[[package]] +name = "futures-sink" +version = "0.3.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f43be4fe21a13b9781a69afa4985b0f6ee0e1afab2c6f454a8cf30e2b2237b6e" + +[[package]] +name = "futures-task" +version = "0.3.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "76d3d132be6c0e6aa1534069c705a74a5997a356c0dc2f86a47765e5617c5b65" + +[[package]] +name = "futures-util" +version = "0.3.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26b01e40b772d54cf6c6d721c1d1abd0647a0106a12ecaa1c186273392a69533" +dependencies = [ + "futures-channel", + "futures-core", + "futures-io", + "futures-macro", + "futures-sink", + "futures-task", + "memchr", + "pin-project-lite", + "pin-utils", + "slab", +] + [[package]] name = "generic-array" version = "0.14.7" @@ -509,6 +612,44 @@ dependencies = [ "sha2", ] +[[package]] +name = "icrc1-test-env" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "742ac065f9340e129b1e2497036aafed4937d66d7ab225c4989a1405496b9b18" +dependencies = [ + "anyhow", + "async-trait", + "candid", + "serde", +] + +[[package]] +name = "icrc1-test-env-state-machine" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5bb1a0d52daac39df2db65b08e5abb1bd07ab0ce4969adcf2194cfb03098b7d4" +dependencies = [ + "anyhow", + "async-trait", + "candid", + "hex", + "ic-test-state-machine-client", + "icrc1-test-env", +] + +[[package]] +name = "icrc1-test-suite" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e65c993be0c4fc3fa8f2209834ec39674fb327bde0ec07b5c0b2f40fd0dd8a9c" +dependencies = [ + "anyhow", + "candid", + "futures", + "icrc1-test-env", +] + [[package]] name = "indexmap" version = "1.9.3" @@ -787,6 +928,18 @@ dependencies = [ "siphasher", ] +[[package]] +name = "pin-project-lite" +version = "0.2.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2c516611246607d0c04186886dbb3a754368ef82c79e9827a802c6d836dd111c" + +[[package]] +name = "pin-utils" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" + [[package]] name = "precomputed-hash" version = "0.1.1" @@ -928,9 +1081,9 @@ checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" [[package]] name = "serde" -version = "1.0.181" +version = "1.0.171" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6d3e73c93c3240c0bda063c239298e633114c69a888c3e37ca8bb33f343e9890" +checksum = "30e27d1e4fd7659406c492fd6cfaf2066ba8773de45ca75e855590f856dc34a9" dependencies = [ "serde_derive", ] @@ -946,9 +1099,9 @@ dependencies = [ [[package]] name = "serde_derive" -version = "1.0.181" +version = "1.0.171" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "be02f6cb0cd3a5ec20bbcfbcbd749f57daddb1a0882dc2e46a6c236c90b977ed" +checksum = "389894603bd18c46fa56231694f8d827779c0951a667087194cf9de94ed24682" dependencies = [ "proc-macro2", "quote", @@ -994,6 +1147,15 @@ version = "0.3.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7bd3e3206899af3f8b12af284fafc038cc1dc2b41d1b89dd17297221c5d225de" +[[package]] +name = "slab" +version = "0.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6528351c9bc8ab22353f9d776db39a20288e8d6c37ef8cfe3317cf875eecfc2d" +dependencies = [ + "autocfg", +] + [[package]] name = "smallvec" version = "1.11.0" diff --git a/cycles-ledger/Cargo.toml b/cycles-ledger/Cargo.toml index 7b54378..c0e458f 100644 --- a/cycles-ledger/Cargo.toml +++ b/cycles-ledger/Cargo.toml @@ -31,3 +31,6 @@ thiserror = "1.0" depositor = { path = "../depositor" } escargot = { version = "0.5.7", features = ["print"] } ic-test-state-machine-client = "2.2.1" +icrc1-test-env-state-machine = "0.1.0" +icrc1-test-suite = "0.1.0" +futures = "0.3.28" \ No newline at end of file diff --git a/cycles-ledger/src/storage.rs b/cycles-ledger/src/storage.rs index bf24e09..9d62244 100644 --- a/cycles-ledger/src/storage.rs +++ b/cycles-ledger/src/storage.rs @@ -243,9 +243,7 @@ pub fn transfer( .expect("failed to update 'from' balance"); let to_balance = s.balances.get(&to_key).unwrap_or_default(); - s.balances - .insert(to_key, to_balance + amount) - .expect("failed to update 'to' balance"); + s.balances.insert(to_key, to_balance.saturating_add(amount)); let phash = s.last_block_hash(); let block_hash = s.emit_block(Block { diff --git a/cycles-ledger/tests/client.rs b/cycles-ledger/tests/client.rs index 1bc2ef3..6b4d2cd 100644 --- a/cycles-ledger/tests/client.rs +++ b/cycles-ledger/tests/client.rs @@ -2,7 +2,9 @@ use candid::{Decode, Encode, Nat, Principal}; use cycles_ledger::endpoints::{self, DepositResult, SendArg}; use depositor::endpoints::DepositArg; use ic_test_state_machine_client::{StateMachine, WasmResult}; -use icrc_ledger_types::icrc1::account::Account; +use icrc_ledger_types::icrc1::{ + account::Account, +}; use num_traits::ToPrimitive; pub fn deposit( diff --git a/cycles-ledger/tests/tests.rs b/cycles-ledger/tests/tests.rs index 24e91ba..df4f2e8 100644 --- a/cycles-ledger/tests/tests.rs +++ b/cycles-ledger/tests/tests.rs @@ -1,4 +1,5 @@ -use std::path::PathBuf; +use futures::future::FutureExt; +use std::{path::PathBuf, sync::Arc}; use candid::{Encode, Nat, Principal}; use client::deposit; @@ -459,3 +460,32 @@ fn test_send_fails() { .unwrap_err(); assert_eq!(0, balance_of(env, ledger_id, user_2)); } + +#[test] +fn test_icrc1_test_suite() { + let env = new_state_machine(); + let ledger_id = install_ledger(&env); + let depositor_id = install_depositor(&env, ledger_id); + let user = Account { + owner: Principal::from_slice(&[10]), + subaccount: Some([0; 32]), + }; + + // make the first deposit to the user and check the result + let deposit_res = deposit(&env, depositor_id, user, 1_000_000_000_000_000); + assert_eq!(deposit_res.txid, Nat::from(0)); + assert_eq!(deposit_res.balance, 1_000_000_000_000_000_u128); + assert_eq!(1_000_000_000_000_000, balance_of(&env, ledger_id, user)); + + let ledger_env = + icrc1_test_env_state_machine::SMLedger::new(Arc::new(env), ledger_id, user.owner); + let tests = icrc1_test_suite::test_suite(ledger_env) + .now_or_never() + .unwrap(); + if !icrc1_test_suite::execute_tests(tests) + .now_or_never() + .unwrap() + { + panic!("The ICRC-1 test suite failed"); + } +} diff --git a/depositor/src/main.rs b/depositor/src/main.rs index 366c008..2986096 100644 --- a/depositor/src/main.rs +++ b/depositor/src/main.rs @@ -38,6 +38,6 @@ async fn deposit(arg: DepositArg) -> DepositResult { }; let (result,): (DepositResult,) = call_with_payment128(ledger_id, "deposit", (arg,), cycles) .await - .expect("Unable to call deposit"); + .expect(&format!("Unable to call deposit with {}",cycles)); result } From d7b1ae8407a7eaad4caa739561367f88b6724187 Mon Sep 17 00:00:00 2001 From: Nikolas Haimerl Date: Mon, 28 Aug 2023 07:34:49 +0000 Subject: [PATCH 2/2] clippy --- depositor/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/depositor/src/main.rs b/depositor/src/main.rs index a7febc2..366c008 100644 --- a/depositor/src/main.rs +++ b/depositor/src/main.rs @@ -38,6 +38,6 @@ async fn deposit(arg: DepositArg) -> DepositResult { }; let (result,): (DepositResult,) = call_with_payment128(ledger_id, "deposit", (arg,), cycles) .await - .expect(&format!("Unable to call deposit with {}", cycles)); + .expect("Unable to call deposit"); result }