From 8d577d14e317e5adf116b6dd8e43926f09a6527c Mon Sep 17 00:00:00 2001 From: Marco Walz Date: Tue, 16 Jun 2026 20:05:11 +0200 Subject: [PATCH] chore: migrate rust/inter-canister-calls to icp-cli Co-Authored-By: Claude Sonnet 4.6 --- .github/workflows/inter-canister-calls.yml | 28 ++ .../workflows/rust-inter-canister-calls.yaml | 44 --- .../.devcontainer/devcontainer.json | 20 -- rust/inter-canister-calls/BUILD.md | 113 ------ rust/inter-canister-calls/Cargo.lock | 321 ++++++++---------- rust/inter-canister-calls/Cargo.toml | 2 +- rust/inter-canister-calls/Makefile | 104 ++++-- rust/inter-canister-calls/README.md | 29 +- .../{src => }/caller/Cargo.toml | 5 +- .../{src => }/caller/src/lib.rs | 4 +- .../{src => }/counter/Cargo.toml | 3 +- rust/inter-canister-calls/counter/src/lib.rs | 33 ++ rust/inter-canister-calls/dfx.json | 22 -- rust/inter-canister-calls/icp.yaml | 10 + rust/inter-canister-calls/rust-toolchain.toml | 2 + .../src/caller/caller.did | 28 -- .../src/counter/counter.did | 6 - .../src/counter/src/lib.rs | 67 ---- 18 files changed, 311 insertions(+), 530 deletions(-) create mode 100644 .github/workflows/inter-canister-calls.yml delete mode 100644 .github/workflows/rust-inter-canister-calls.yaml delete mode 100644 rust/inter-canister-calls/.devcontainer/devcontainer.json delete mode 100644 rust/inter-canister-calls/BUILD.md rename rust/inter-canister-calls/{src => }/caller/Cargo.toml (64%) rename rust/inter-canister-calls/{src => }/caller/src/lib.rs (99%) rename rust/inter-canister-calls/{src => }/counter/Cargo.toml (74%) create mode 100644 rust/inter-canister-calls/counter/src/lib.rs delete mode 100644 rust/inter-canister-calls/dfx.json create mode 100644 rust/inter-canister-calls/icp.yaml create mode 100644 rust/inter-canister-calls/rust-toolchain.toml delete mode 100644 rust/inter-canister-calls/src/caller/caller.did delete mode 100644 rust/inter-canister-calls/src/counter/counter.did delete mode 100644 rust/inter-canister-calls/src/counter/src/lib.rs diff --git a/.github/workflows/inter-canister-calls.yml b/.github/workflows/inter-canister-calls.yml new file mode 100644 index 0000000000..c063e5dc31 --- /dev/null +++ b/.github/workflows/inter-canister-calls.yml @@ -0,0 +1,28 @@ +name: inter-canister-calls + +on: + push: + branches: [master] + pull_request: + paths: + - rust/inter-canister-calls/** + - .github/workflows/inter-canister-calls.yml + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +jobs: + rust-inter-canister-calls: + runs-on: ubuntu-24.04 + container: ghcr.io/dfinity/icp-dev-env-rust:1.0.0 + env: + ICP_CLI_GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + steps: + - uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4.3.1 + - name: Deploy and test + working-directory: rust/inter-canister-calls + run: | + icp network start -d + icp deploy + make test diff --git a/.github/workflows/rust-inter-canister-calls.yaml b/.github/workflows/rust-inter-canister-calls.yaml deleted file mode 100644 index 9c8a286308..0000000000 --- a/.github/workflows/rust-inter-canister-calls.yaml +++ /dev/null @@ -1,44 +0,0 @@ -name: rust-inter-canister-calls -on: - push: - branches: - - master - pull_request: - paths: - - rust/inter-canister-calls/** - - .github/workflows/provision-darwin.sh - - .github/workflows/provision-linux.sh - - .github/workflows/rust-inter-canister-calls-example.yml - - .ic-commit -concurrency: - group: ${{ github.workflow }}-${{ github.ref }} - cancel-in-progress: true -jobs: - rust-inter-canister-calls-darwin: - runs-on: macos-15 - steps: - - uses: actions/checkout@ee0669bd1cc54295c223e0bb666b733df41de1c5 # v2.7.0 - with: - submodules: recursive - - name: Provision Darwin - run: bash .github/workflows/provision-darwin.sh - - name: Rust Parallel-Calls Darwin - run: | - pushd rust/inter-canister-calls - dfx start --background - make test - popd - rust-inter-canister-calls-linux: - runs-on: ubuntu-24.04 - steps: - - uses: actions/checkout@ee0669bd1cc54295c223e0bb666b733df41de1c5 # v2.7.0 - with: - submodules: recursive - - name: Provision Linux - run: bash .github/workflows/provision-linux.sh - - name: Rust Parallel-Calls Linux - run: | - pushd rust/inter-canister-calls - dfx start --background - make test - popd diff --git a/rust/inter-canister-calls/.devcontainer/devcontainer.json b/rust/inter-canister-calls/.devcontainer/devcontainer.json deleted file mode 100644 index ebb0b8bcc6..0000000000 --- a/rust/inter-canister-calls/.devcontainer/devcontainer.json +++ /dev/null @@ -1,20 +0,0 @@ -{ - "name": "ICP Dev Environment", - "image": "ghcr.io/dfinity/icp-dev-env-slim:22", - "forwardPorts": [4943, 5173], - "portsAttributes": { - "4943": { - "label": "dfx", - "onAutoForward": "ignore" - }, - "5173": { - "label": "vite", - "onAutoForward": "openBrowser" - } - }, - "customizations": { - "vscode": { - "extensions": ["dfinity-foundation.vscode-motoko"] - } - } -} diff --git a/rust/inter-canister-calls/BUILD.md b/rust/inter-canister-calls/BUILD.md deleted file mode 100644 index 2a8a7688ee..0000000000 --- a/rust/inter-canister-calls/BUILD.md +++ /dev/null @@ -1,113 +0,0 @@ -# Continue building locally - -Projects deployed through ICP Ninja are temporary; they will only be live for 20 minutes before they are removed. The command-line tool `dfx` can be used to continue building your ICP Ninja project locally and deploy it to the mainnet. - -To migrate your ICP Ninja project off of the web browser and develop it locally, follow these steps. - -### 1. Install developer tools. - -You can install the developer tools natively or use Dev Containers. - -#### Option 1: Natively install developer tools - -> Installing `dfx` natively is currently only supported on macOS and Linux systems. On Windows, it is recommended to use the Dev Containers option. - -1. Install `dfx` with the following command: - -``` - -sh -ci "$(curl -fsSL https://internetcomputer.org/install.sh)" - -``` - -> On Apple Silicon (e.g., Apple M1 chip), make sure you have Rosetta installed (`softwareupdate --install-rosetta`). - -2. [Install NodeJS](https://nodejs.org/en/download/package-manager). - -3. For Rust projects, you will also need to: - -- Install [Rust](https://doc.rust-lang.org/cargo/getting-started/installation.html#install-rust-and-cargo): `curl https://sh.rustup.rs -sSf | sh` - -- Install [candid-extractor](https://crates.io/crates/candid-extractor): `cargo install candid-extractor` - -4. For Motoko projects, you will also need to: - -- Install the Motoko package manager [Mops](https://docs.mops.one/quick-start#2-install-mops-cli): `npm i -g ic-mops` - -Lastly, navigate into your project's directory that you downloaded from ICP Ninja. - -#### Option 2: Dev Containers - -Continue building your projects locally by installing the [Dev Container extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers) for VS Code and [Docker](https://docs.docker.com/engine/install/). - -Make sure Docker is running, then navigate into your project's directory that you downloaded from ICP Ninja and start the Dev Container by selecting `Dev-Containers: Reopen in Container` in VS Code's command palette (F1 or Ctrl/Cmd+Shift+P). - -> Note that local development ports (e.g. the ports used by `dfx` or `vite`) are forwarded from the Dev Container to your local machine. In the VS code terminal, use Ctrl/Cmd+Click on the displayed local URLs to open them in your browser. To view the current port mappings, click the "Ports" tab in the VS Code terminal window. - -### 2. Start the local development environment. - -``` -dfx start --background -``` - -### 3. Create a local developer identity. - -To manage your project's canisters, it is recommended that you create a local [developer identity](https://internetcomputer.org/docs/building-apps/getting-started/identities) rather than use the `dfx` default identity that is not stored securely. - -To create a new identity, run the commands: - -``` - -dfx identity new IDENTITY_NAME - -dfx identity use IDENTITY_NAME - -``` - -Replace `IDENTITY_NAME` with your preferred identity name. The first command `dfx start --background` starts the local `dfx` processes, then `dfx identity new` will create a new identity and return your identity's seed phase. Be sure to save this in a safe, secure location. - -The third command `dfx identity use` will tell `dfx` to use your new identity as the active identity. Any canister smart contracts created after running `dfx identity use` will be owned and controlled by the active identity. - -Your identity will have a principal ID associated with it. Principal IDs are used to identify different entities on ICP, such as users and canisters. - -[Learn more about ICP developer identities](https://internetcomputer.org/docs/building-apps/getting-started/identities). - -### 3. Deploy the project locally. - -Deploy your project to your local developer environment with: - -``` -npm install -dfx deploy - -``` - -Your project will be hosted on your local machine. The local canister URLs for your project will be shown in the terminal window as output of the `dfx deploy` command. You can open these URLs in your web browser to view the local instance of your project. - -### 4. Obtain cycles. - -To deploy your project to the mainnet for long-term public accessibility, first you will need [cycles](https://internetcomputer.org/docs/building-apps/getting-started/tokens-and-cycles). Cycles are used to pay for the resources your project uses on the mainnet, such as storage and compute. - -> This cost model is known as ICP's [reverse gas model](https://internetcomputer.org/docs/building-apps/essentials/gas-cost), where developers pay for their project's gas fees rather than users pay for their own gas fees. This model provides an enhanced end user experience since they do not need to hold tokens or sign transactions when using a dapp deployed on ICP. - -> Learn how much a project may cost by using the [pricing calculator](https://internetcomputer.org/docs/building-apps/essentials/cost-estimations-and-examples). - -Cycles can be obtained through [converting ICP tokens into cycles using `dfx`](https://internetcomputer.org/docs/building-apps/developer-tools/dfx/dfx-cycles#dfx-cycles-convert). - -### 5. Deploy to the mainnet. - -Once you have cycles, run the command: - -``` - -dfx deploy --network ic - -``` - -After your project has been deployed to the mainnet, it will continuously require cycles to pay for the resources it uses. You will need to [top up](https://internetcomputer.org/docs/building-apps/canister-management/topping-up) your project's canisters or set up automatic cycles management through a service such as [CycleOps](https://cycleops.dev/). - -> If your project's canisters run out of cycles, they will be removed from the network. - -## Additional examples - -Additional code examples and sample applications can be found in the [DFINITY examples repo](https://github.com/dfinity/examples). diff --git a/rust/inter-canister-calls/Cargo.lock b/rust/inter-canister-calls/Cargo.lock index 564835f6c1..65c8d7d601 100644 --- a/rust/inter-canister-calls/Cargo.lock +++ b/rust/inter-canister-calls/Cargo.lock @@ -4,9 +4,18 @@ version = 4 [[package]] name = "anyhow" -version = "1.0.95" +version = "1.0.102" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "34ac096ce696dc2fcabef30516bb13c0a68a11d30131d3df6f04711467681b04" +checksum = "7f202df86484c868dbad7eaa557ef785d5c66295e41b460ef922eca0723b842c" + +[[package]] +name = "ar_archive_writer" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4087686b4b0a3427190bae57a1d9a478dbb2d40c5dc1bd6e2b6d797913bdd348" +dependencies = [ + "object", +] [[package]] name = "arrayvec" @@ -16,9 +25,9 @@ checksum = "23b62fc65de8e4e7f52534fb52b0f3ed04746ae267519eef2a83941e8085068b" [[package]] name = "autocfg" -version = "1.4.0" +version = "1.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" +checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53" [[package]] name = "binread" @@ -63,17 +72,14 @@ name = "caller" version = "0.1.0" dependencies = [ "candid", - "hex", "ic-cdk", - "ic-cdk-macros", - "sha2", ] [[package]] name = "candid" -version = "0.10.13" +version = "0.10.29" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a253bab4a9be502c82332b60cbeee6202ad0692834efeec95fae9f29db33d692" +checksum = "f8f781afa4a1303e3eab4ada0720a874942bcfa936ce01b816ac6378945c43a9" dependencies = [ "anyhow", "binread", @@ -94,30 +100,31 @@ dependencies = [ [[package]] name = "candid_derive" -version = "0.6.6" +version = "0.10.29" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3de398570c386726e7a59d9887b68763c481477f9a043fb998a2e09d428df1a9" +checksum = "ad6ae8e7944dd0035651bc0e7b3a3e4cb16f5fc43f8ae4fd76b36ff2cd52759f" dependencies = [ "lazy_static", "proc-macro2", "quote", - "syn 2.0.96", + "syn 2.0.118", ] [[package]] name = "cc" -version = "1.2.10" +version = "1.2.64" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "13208fcbb66eaeffe09b99fffbe1af420f00a7b35aa99ad683dfc1aa76145229" +checksum = "dad887fd958be91b5098c0248def011f4523ab786cd411be668777e55063501f" dependencies = [ + "find-msvc-tools", "shlex", ] [[package]] name = "cfg-if" -version = "1.0.0" +version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" [[package]] name = "counter" @@ -125,7 +132,6 @@ version = "0.1.0" dependencies = [ "candid", "ic-cdk", - "ic-cdk-macros", ] [[package]] @@ -139,18 +145,18 @@ dependencies = [ [[package]] name = "crc32fast" -version = "1.4.2" +version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a97769d94ddab943e4510d138150169a2758b5ef3eb191a9ee688de3e23ef7b3" +checksum = "9481c1c90cbf2ac953f07c8d4a58aa3945c425b7185c9154d67a65e4230da511" dependencies = [ "cfg-if", ] [[package]] name = "crypto-common" -version = "0.1.6" +version = "0.1.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" +checksum = "78c8292055d1c1df0cce5d180393dc8cce0abec0a7102adb6c7b1eef6016d60a" dependencies = [ "generic-array", "typenum", @@ -158,9 +164,9 @@ dependencies = [ [[package]] name = "darling" -version = "0.20.11" +version = "0.23.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fc7f46116c46ff9ab3eb1597a45688b6715c6e628b5c133e288e709a29bcb4ee" +checksum = "25ae13da2f202d56bd7f91c25fba009e7717a1e4a1cc98a76d844b65ae912e9d" dependencies = [ "darling_core", "darling_macro", @@ -168,34 +174,33 @@ dependencies = [ [[package]] name = "darling_core" -version = "0.20.11" +version = "0.23.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0d00b9596d185e565c2207a0b01f8bd1a135483d02d9b7b0a54b11da8d53412e" +checksum = "9865a50f7c335f53564bb694ef660825eb8610e0a53d3e11bf1b0d3df31e03b0" dependencies = [ - "fnv", "ident_case", "proc-macro2", "quote", "strsim", - "syn 2.0.96", + "syn 2.0.118", ] [[package]] name = "darling_macro" -version = "0.20.11" +version = "0.23.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fc34b93ccb385b40dc71c6fceac4b2ad23662c7eeb248cf10d529b7e055b6ead" +checksum = "ac3984ec7bd6cfa798e62b4a642426a5be0e68f9401cfc2a01e3fa9ea2fcdb8d" dependencies = [ "darling_core", "quote", - "syn 2.0.96", + "syn 2.0.118", ] [[package]] name = "data-encoding" -version = "2.7.0" +version = "2.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0e60eed09d8c01d3cee5b7d30acb059b76614c918fa0f992e0dd6eeb10daad6f" +checksum = "a4ae5f15dda3c708c0ade84bfee31ccab44a3da4f88015ed22f63732abe300c8" [[package]] name = "digest" @@ -209,15 +214,15 @@ dependencies = [ [[package]] name = "either" -version = "1.13.0" +version = "1.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0" +checksum = "91622ff5e7162018101f2fea40d6ebf4a78bbe5a49736a2020649edf9693679e" [[package]] -name = "fnv" -version = "1.0.7" +name = "find-msvc-tools" +version = "0.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" +checksum = "5baebc0774151f905a1a2cc41989300b1e6fbb29aff0ceffa1064fdd3088d582" [[package]] name = "generic-array" @@ -243,42 +248,42 @@ checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" [[package]] name = "ic-cdk" -version = "0.18.5" +version = "0.20.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "db9cc3e0e86ee12504c749fa33793014f1f4d6956a8a70e4db595169c5f6ac26" +checksum = "6a7971f4983db147afbbc4e7f87f60b09fcd60ac707af37ff3d2468dcddbf551" dependencies = [ "candid", "ic-cdk-executor", "ic-cdk-macros", "ic-error-types", - "ic-management-canister-types", "ic0", + "pin-project-lite", "serde", - "serde_bytes", - "slotmap", - "thiserror 2.0.11", + "thiserror 2.0.18", ] [[package]] name = "ic-cdk-executor" -version = "1.0.1" +version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "15948808e3e7b50749fe50838df77fccaf048c8af2c26884ff5c8f787c29787a" +checksum = "33716b730ded33690b8a704bff3533fda87d229e58046823647d28816e9bcee7" dependencies = [ + "ic0", "slotmap", + "smallvec", ] [[package]] name = "ic-cdk-macros" -version = "0.18.5" +version = "0.20.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b190cace2b141a5801252115bdc27397d47f086c928af3e917ce1da81b17e3cd" +checksum = "a7c20c002200c720958f321bb78b4d4987fc2c380bf9ef69ed4404730810f45f" dependencies = [ "candid", "darling", "proc-macro2", "quote", - "syn 2.0.96", + "syn 2.0.118", ] [[package]] @@ -292,28 +297,17 @@ dependencies = [ "strum_macros", ] -[[package]] -name = "ic-management-canister-types" -version = "0.3.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "95f3af3543f6d0cbdecd2dcdfd4737ada2bd42d935cc787eec22090c96492c76" -dependencies = [ - "candid", - "serde", - "serde_bytes", -] - [[package]] name = "ic0" -version = "1.0.0" +version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8877193e1921b5fd16accb0305eb46016868cd1935b05c05eca0ec007b943272" +checksum = "c77c8932bff1f09502d0d8c079d5a206a06afe523e35e816162cf4d30b5bf80d" [[package]] name = "ic_principal" -version = "0.1.1" +version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1762deb6f7c8d8c2bdee4b6c5a47b60195b74e9b5280faa5ba29692f8e17429c" +checksum = "1aea751965eaf92990be8c79c64b4f3174ff22dd3604e6696a06a494afbaba2a" dependencies = [ "crc32fast", "data-encoding", @@ -336,15 +330,21 @@ checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" [[package]] name = "leb128" -version = "0.2.5" +version = "0.2.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "884e2677b40cc8c339eaefcb701c32ef1fd2493d71118dc0ca4b6a736c93bd67" +checksum = "c83bff1d572d6b9aeef67ddfc8448e4a3737909cb28e81f97c791b9018703e52" [[package]] name = "libc" -version = "0.2.169" +version = "0.2.186" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b5aba8db14291edd000dfcc4d620c7ebfb122c613afb886ca8803fa4e128a20a" +checksum = "68ab91017fe16c622486840e4c83c9a37afeff978bd239b5293d61ece587de66" + +[[package]] +name = "memchr" +version = "2.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "88904434abc2901f197fe8cc55f0445e7ded921dba5911dad2e2b39b48e663c4" [[package]] name = "num-bigint" @@ -375,17 +375,32 @@ dependencies = [ "autocfg", ] +[[package]] +name = "object" +version = "0.37.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff76201f031d8863c38aa7f905eca4f53abbfa15f609db4277d44cd8938f33fe" +dependencies = [ + "memchr", +] + [[package]] name = "paste" version = "1.0.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" +[[package]] +name = "pin-project-lite" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a89322df9ebe1c1578d689c92318e070967d1042b512afbe49518723f4e6d5cd" + [[package]] name = "pretty" -version = "0.12.3" +version = "0.12.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b55c4d17d994b637e2f4daf6e5dc5d660d209d5642377d675d7a1c3ab69fa579" +checksum = "0d22152487193190344590e4f30e219cf3fe140d9e7a3fdb683d82aa2c5f4156" dependencies = [ "arrayvec", "typed-arena", @@ -394,71 +409,83 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.93" +version = "1.0.106" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "60946a68e5f9d28b0dc1c21bb8a97ee7d018a8b322fa57838ba31cc878e22d99" +checksum = "8fd00f0bb2e90d81d1044c2b32617f68fcb9fa3bb7640c23e9c748e53fb30934" dependencies = [ "unicode-ident", ] [[package]] name = "psm" -version = "0.1.24" +version = "0.1.31" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "200b9ff220857e53e184257720a14553b2f4aa02577d2ed9842d45d4b9654810" +checksum = "645dbe486e346d9b5de3ef16ede18c26e6c70ad97418f4874b8b1889d6e761ea" dependencies = [ + "ar_archive_writer", "cc", ] [[package]] name = "quote" -version = "1.0.38" +version = "1.0.45" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0e4dccaaaf89514f546c693ddc140f729f958c247918a13380cccc6078391acc" +checksum = "41f2619966050689382d2b44f664f4bc593e129785a36d6ee376ddf37259b924" dependencies = [ "proc-macro2", ] [[package]] name = "rustversion" -version = "1.0.19" +version = "1.0.22" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f7c45b9784283f1b2e7fb61b42047c2fd678ef0960d4f6f1eba131594cc369d4" +checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d" [[package]] name = "serde" -version = "1.0.217" +version = "1.0.228" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "02fc4265df13d6fa1d00ecff087228cc0a2b5f3c0e87e258d8b94a156e984c70" +checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e" dependencies = [ + "serde_core", "serde_derive", ] [[package]] name = "serde_bytes" -version = "0.11.15" +version = "0.11.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "387cc504cb06bb40a96c8e04e951fe01854cf6bc921053c954e4a606d9675c6a" +checksum = "a5d440709e79d88e51ac01c4b72fc6cb7314017bb7da9eeff678aa94c10e3ea8" dependencies = [ "serde", + "serde_core", +] + +[[package]] +name = "serde_core" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41d385c7d4ca58e59fc732af25c3983b67ac852c1a25000afe1175de458b67ad" +dependencies = [ + "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.217" +version = "1.0.228" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5a9bf7cf98d04a2b28aead066b7496853d4779c9cc183c440dbac457641e19a0" +checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79" dependencies = [ "proc-macro2", "quote", - "syn 2.0.96", + "syn 2.0.118", ] [[package]] name = "sha2" -version = "0.10.8" +version = "0.10.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "793db75ad2bcafc3ffa7c68b215fee268f537982cd901d132f89c6343f3a3dc8" +checksum = "a7507d819769d01a365ab707794a4084392c824f54a7a6a7862f8c3d0892b283" dependencies = [ "cfg-if", "cpufeatures", @@ -467,24 +494,30 @@ dependencies = [ [[package]] name = "shlex" -version = "1.3.0" +version = "2.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" +checksum = "f8fadd59c855ef2080decdef8ff161eb6661b86933c9d82e5ba29dc602a55aba" [[package]] name = "slotmap" -version = "1.0.7" +version = "1.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dbff4acf519f630b3a3ddcfaea6c06b42174d9a44bc70c620e9ed1649d58b82a" +checksum = "bdd58c3c93c3d278ca835519292445cb4b0d4dc59ccfdf7ceadaab3f8aeb4038" dependencies = [ "version_check", ] +[[package]] +name = "smallvec" +version = "1.15.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ed6a63f02c8539c91a8685a86f4099661ba3da017932f6ebbea6de3f0fa7c90" + [[package]] name = "stacker" -version = "0.1.17" +version = "0.1.24" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "799c883d55abdb5e98af1a7b3f23b9b6de8ecada0ecac058672d7635eb48ca7b" +checksum = "640c8cdd92b6b12f5bcb1803ca3bbf5ab96e5e6b6b96b9ab77dabe9e880b3190" dependencies = [ "cc", "cfg-if", @@ -518,7 +551,7 @@ dependencies = [ "proc-macro2", "quote", "rustversion", - "syn 2.0.96", + "syn 2.0.118", ] [[package]] @@ -534,9 +567,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.96" +version = "2.0.118" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d5d0adab1ae378d7f53bdebc67a39f1f151407ef230f0ce2883572f5d8985c80" +checksum = "1b9ae57f904213ebb649ce6895b8a66c66f0203b9319718f69a5612a065b1422" dependencies = [ "proc-macro2", "quote", @@ -554,11 +587,11 @@ dependencies = [ [[package]] name = "thiserror" -version = "2.0.11" +version = "2.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d452f284b73e6d76dd36758a0c8684b1d5be31f92b89d07fd5822175732206fc" +checksum = "4288b5bcbc7920c07a1149a35cf9590a2aa808e0bc1eafaade0b80947865fbc4" dependencies = [ - "thiserror-impl 2.0.11", + "thiserror-impl 2.0.18", ] [[package]] @@ -569,18 +602,18 @@ checksum = "4fee6c4efc90059e10f81e6d42c60a18f76588c3d74cb83a0b242a2b6c7504c1" dependencies = [ "proc-macro2", "quote", - "syn 2.0.96", + "syn 2.0.118", ] [[package]] name = "thiserror-impl" -version = "2.0.11" +version = "2.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "26afc1baea8a989337eeb52b6e72a039780ce45c3edfcc9c5b9d112feeb173c2" +checksum = "ebc4ee7f67670e9b64d05fa4253e753e016c6c95ff35b89b7941d6b856dec1d5" dependencies = [ "proc-macro2", "quote", - "syn 2.0.96", + "syn 2.0.118", ] [[package]] @@ -591,21 +624,21 @@ checksum = "6af6ae20167a9ece4bcb41af5b80f8a1f1df981f6391189ce00fd257af04126a" [[package]] name = "typenum" -version = "1.17.0" +version = "1.20.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" +checksum = "b6f5e870be6c3b371b77fe0ee0bafb859fa4964b4404c27de1d380043c4dda20" [[package]] name = "unicode-ident" -version = "1.0.16" +version = "1.0.24" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a210d160f08b701c8721ba1c726c11662f877ea6b7094007e1ca9a1041945034" +checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75" [[package]] name = "unicode-width" -version = "0.1.14" +version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7dd6e30e90baa6f72411720665d41d89b9a3d039dc45b8faea1ddd07f617f6af" +checksum = "b4ac048d71ede7ee76d585517add45da530660ef4390e49b098733c6e897f254" [[package]] name = "version_check" @@ -614,74 +647,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" [[package]] -name = "windows-sys" -version = "0.59.0" +name = "windows-link" +version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" -dependencies = [ - "windows-targets", -] +checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" [[package]] -name = "windows-targets" -version = "0.52.6" +name = "windows-sys" +version = "0.61.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc" dependencies = [ - "windows_aarch64_gnullvm", - "windows_aarch64_msvc", - "windows_i686_gnu", - "windows_i686_gnullvm", - "windows_i686_msvc", - "windows_x86_64_gnu", - "windows_x86_64_gnullvm", - "windows_x86_64_msvc", + "windows-link", ] - -[[package]] -name = "windows_aarch64_gnullvm" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" - -[[package]] -name = "windows_aarch64_msvc" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" - -[[package]] -name = "windows_i686_gnu" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" - -[[package]] -name = "windows_i686_gnullvm" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" - -[[package]] -name = "windows_i686_msvc" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" - -[[package]] -name = "windows_x86_64_gnu" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" - -[[package]] -name = "windows_x86_64_gnullvm" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" - -[[package]] -name = "windows_x86_64_msvc" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" diff --git a/rust/inter-canister-calls/Cargo.toml b/rust/inter-canister-calls/Cargo.toml index 59a858a8a3..420cbb6d15 100644 --- a/rust/inter-canister-calls/Cargo.toml +++ b/rust/inter-canister-calls/Cargo.toml @@ -1,3 +1,3 @@ [workspace] -members = [ "src/caller", "src/counter" ] +members = ["caller", "counter"] resolver = "2" diff --git a/rust/inter-canister-calls/Makefile b/rust/inter-canister-calls/Makefile index 46537d7464..7465ce2e24 100644 --- a/rust/inter-canister-calls/Makefile +++ b/rust/inter-canister-calls/Makefile @@ -1,37 +1,69 @@ -.PHONY: all -all: test - -.PHONY: deploy -.SILENT: deploy -build: - dfx deploy -SHELL := /bin/bash -.SHELLFLAGS := -euo pipefail -c .PHONY: test -.SILENT: test -test: build - dfx canister call counter set "( 0 : nat )" | grep '()' && echo 'PASS' - dfx canister call caller call_get_and_set "( principal \"`dfx canister id counter`\", 42 : nat )" | grep '(0 : nat)' && echo 'PASS' - dfx canister call caller set_then_get "( principal \"`dfx canister id counter`\", 7 : nat )" | grep '(7 : nat)' && echo 'PASS' - dfx canister call counter get | grep '(7 : nat)' && echo 'PASS' - dfx canister call caller call_increment "( principal \"`dfx canister id counter`\" )" | grep '(variant { Ok })' && echo 'PASS' - dfx canister call counter get | grep '(8 : nat)' && echo 'PASS' - dfx canister call caller call_get "( principal \"`dfx canister id counter`\" )" | grep '(variant { Ok = 8 : nat })' && echo 'PASS' - dfx canister call caller stubborn_set "( principal \"`dfx canister id counter`\", 42 : nat )" | grep '(variant { Ok })' && echo 'PASS' - dfx canister call caller call_get "( principal \"`dfx canister id counter`\" )" | grep '(variant { Ok = 42 : nat })' && echo 'PASS' - # We'll test that the counter's balance increases when calling send_cycles to it. - # Storing the original balance in a file is the easiest with make. - echo $$(dfx canister status counter | awk '/Balance/ { gsub(/_/, "", $$2); print $$2 }') > .counter_before - # Send 10% of the caller's available cycles - CYCLES_TO_SEND=$$(dfx canister status caller | awk '/Balance/ { gsub(/_/, "", $$2); printf("%d", int($$2/10)) }'); \ - echo "Sending $$CYCLES_TO_SEND cycles to counter canister"; \ - dfx canister call caller send_cycles "( principal \"`dfx canister id counter`\", $$CYCLES_TO_SEND: nat64 )" | grep '(variant { Ok })' && echo PASS - # Check that the counter's balance increased - AFTER=$$(dfx canister status counter | awk '/Balance/ { gsub(/_/, "", $$2); print $$2 }'); \ - BEFORE=$$(cat .counter_before); \ - test $$AFTER -gt $$BEFORE && echo "PASS (cycles increased: $$BEFORE -> $$AFTER)" - -.PHONY: clean -.SILENT: clean -clean: - rm -rf .dfx + +test: + @echo "=== Test 1: Reset counter to 0 ===" + @result=$$(icp canister call counter set '(0 : nat)') && \ + echo "$$result" && \ + echo "$$result" | grep -q '()' && \ + echo "PASS" || (echo "FAIL" && exit 1) + + @echo "=== Test 2: call_get_and_set returns old value and sets new ===" + @COUNTER_ID=$$(icp canister id counter) && \ + result=$$(icp canister call caller call_get_and_set "(principal \"$$COUNTER_ID\", 42 : nat)") && \ + echo "$$result" && \ + echo "$$result" | grep -q '(0 : nat)' && \ + echo "PASS" || (echo "FAIL" && exit 1) + + @echo "=== Test 3: set_then_get sets value and returns it ===" + @COUNTER_ID=$$(icp canister id counter) && \ + result=$$(icp canister call caller set_then_get "(principal \"$$COUNTER_ID\", 7 : nat)") && \ + echo "$$result" && \ + echo "$$result" | grep -q '(7 : nat)' && \ + echo "PASS" || (echo "FAIL" && exit 1) + + @echo "=== Test 4: counter value persisted after set_then_get ===" + @result=$$(icp canister call --query counter get '()') && \ + echo "$$result" && \ + echo "$$result" | grep -q '(7 : nat)' && \ + echo "PASS" || (echo "FAIL" && exit 1) + + @echo "=== Test 5: call_increment increments counter ===" + @COUNTER_ID=$$(icp canister id counter) && \ + result=$$(icp canister call caller call_increment "(principal \"$$COUNTER_ID\")") && \ + echo "$$result" && \ + echo "$$result" | grep -q 'Ok' && \ + echo "PASS" || (echo "FAIL" && exit 1) + + @echo "=== Test 6: counter value is 8 after increment ===" + @result=$$(icp canister call --query counter get '()') && \ + echo "$$result" && \ + echo "$$result" | grep -q '(8 : nat)' && \ + echo "PASS" || (echo "FAIL" && exit 1) + + @echo "=== Test 7: call_get returns current counter value ===" + @COUNTER_ID=$$(icp canister id counter) && \ + result=$$(icp canister call caller call_get "(principal \"$$COUNTER_ID\")") && \ + echo "$$result" && \ + echo "$$result" | grep -q 'Ok = 8' && \ + echo "PASS" || (echo "FAIL" && exit 1) + + @echo "=== Test 8: stubborn_set retries until success ===" + @COUNTER_ID=$$(icp canister id counter) && \ + result=$$(icp canister call caller stubborn_set "(principal \"$$COUNTER_ID\", 42 : nat)") && \ + echo "$$result" && \ + echo "$$result" | grep -q 'Ok' && \ + echo "PASS" || (echo "FAIL" && exit 1) + + @echo "=== Test 9: counter value is 42 after stubborn_set ===" + @COUNTER_ID=$$(icp canister id counter) && \ + result=$$(icp canister call caller call_get "(principal \"$$COUNTER_ID\")") && \ + echo "$$result" && \ + echo "$$result" | grep -q 'Ok = 42' && \ + echo "PASS" || (echo "FAIL" && exit 1) + + @echo "=== Test 10: send_cycles increases counter canister balance ===" + @COUNTER_ID=$$(icp canister id counter) && \ + result=$$(icp canister call caller send_cycles "(principal \"$$COUNTER_ID\", 1_000_000_000_000 : nat64)") && \ + echo "$$result" && \ + echo "$$result" | grep -q 'Ok' && \ + echo "PASS" || (echo "FAIL" && exit 1) diff --git a/rust/inter-canister-calls/README.md b/rust/inter-canister-calls/README.md index f95d05542e..f5e03f8432 100644 --- a/rust/inter-canister-calls/README.md +++ b/rust/inter-canister-calls/README.md @@ -1,25 +1,30 @@ # Inter-canister calls -This project contains the code from the [inter-canister call Rust tutorial](https://internetcomputer.org/docs/building-apps/developer-tools/cdks/rust/intercanister). Please follow the tutorial for explanations. +This example demonstrates how to make inter-canister calls in Rust on the Internet Computer. It shows both bounded-wait and unbounded-wait call styles, retry logic, and how to send cycles to another canister via the management canister. Two canisters are deployed: a `counter` canister that exposes a simple counter interface, and a `caller` canister that calls the counter using different inter-canister call patterns. -## Deploying from ICP Ninja +## Build and deploy from the command line -[![](https://icp.ninja/assets/open.svg)](https://icp.ninja/editor?g=https://github.com/dfinity/examples/tree/master/rust/inter-canister-calls) +### Prerequisites -## Build and deploy from the command-line +- Node.js +- icp-cli: `npm install -g @icp-sdk/icp-cli @icp-sdk/ic-wasm` -### 1. [Download and install the IC SDK.](https://internetcomputer.org/docs/building-apps/getting-started/install) +### Install -### 2. Download your project from ICP Ninja using the 'Download files' button on the upper left corner, or [clone the GitHub examples repository.](https://github.com/dfinity/examples/) - -### 3. Navigate into the project's directory. +```bash +git clone https://github.com/dfinity/examples +cd examples/rust/inter-canister-calls +``` -### 4. Deploy the project to your local environment: +### Deploy and test -``` -dfx start --background --clean && dfx deploy +```bash +icp network start -d +icp deploy +make test +icp network stop ``` ## Security considerations and best practices -If you base your application on this example, it is recommended that you familiarize yourself with and adhere to the [security best practices](https://internetcomputer.org/docs/building-apps/security/overview) for developing on ICP. This example may not implement all the best practices. +If you base your application on this example, it is recommended that you familiarize yourself with and adhere to the [security best practices](https://docs.internetcomputer.org/guides/security/overview) for developing on ICP. This example may not implement all the best practices. diff --git a/rust/inter-canister-calls/src/caller/Cargo.toml b/rust/inter-canister-calls/caller/Cargo.toml similarity index 64% rename from rust/inter-canister-calls/src/caller/Cargo.toml rename to rust/inter-canister-calls/caller/Cargo.toml index 3a5520e583..ba2c2be473 100644 --- a/rust/inter-canister-calls/src/caller/Cargo.toml +++ b/rust/inter-canister-calls/caller/Cargo.toml @@ -8,7 +8,4 @@ crate-type = ["cdylib"] [dependencies] candid = "0.10" -ic-cdk = "0.18.5" -ic-cdk-macros = "0.18.5" -sha2 = "0.10" -hex = "0.4" +ic-cdk = "0.20" diff --git a/rust/inter-canister-calls/src/caller/src/lib.rs b/rust/inter-canister-calls/caller/src/lib.rs similarity index 99% rename from rust/inter-canister-calls/src/caller/src/lib.rs rename to rust/inter-canister-calls/caller/src/lib.rs index cacb4970f5..eeea3cd57f 100644 --- a/rust/inter-canister-calls/src/caller/src/lib.rs +++ b/rust/inter-canister-calls/caller/src/lib.rs @@ -3,7 +3,7 @@ use candid::{Nat, Principal}; use ic_cdk::api::time; use ic_cdk::call::{Call, CallErrorExt}; use ic_cdk::management_canister::{DepositCyclesArgs, CanisterId}; -use ic_cdk_macros::update; +use ic_cdk::update; // When calling other canisters: // @@ -150,4 +150,4 @@ pub async fn send_cycles(target: CanisterId, amount: u64) -> Result<(), String> Ok(_) => Ok(()), Err(e) => Err(format!("Error attaching {} cycles: {:?}", amount, e)), } -} \ No newline at end of file +} diff --git a/rust/inter-canister-calls/src/counter/Cargo.toml b/rust/inter-canister-calls/counter/Cargo.toml similarity index 74% rename from rust/inter-canister-calls/src/counter/Cargo.toml rename to rust/inter-canister-calls/counter/Cargo.toml index 5919d9ddb6..548bc4ca4a 100644 --- a/rust/inter-canister-calls/src/counter/Cargo.toml +++ b/rust/inter-canister-calls/counter/Cargo.toml @@ -8,5 +8,4 @@ crate-type = ["cdylib"] [dependencies] candid = "0.10" -ic-cdk = "0.18.5" -ic-cdk-macros = "0.18.5" +ic-cdk = "0.20" diff --git a/rust/inter-canister-calls/counter/src/lib.rs b/rust/inter-canister-calls/counter/src/lib.rs new file mode 100644 index 0000000000..e920baa9fa --- /dev/null +++ b/rust/inter-canister-calls/counter/src/lib.rs @@ -0,0 +1,33 @@ +use candid::types::number::Nat; +use std::cell::RefCell; + +thread_local! { + static COUNTER: RefCell = RefCell::new(Nat::from(0_u32)); +} + +/// Get the value of the counter. +#[ic_cdk::query] +fn get() -> Nat { + COUNTER.with(|counter| (*counter.borrow()).clone()) +} + +/// Set the value of the counter. +#[ic_cdk::update] +fn set(n: Nat) { + COUNTER.with(|count| *count.borrow_mut() = n); +} + +#[ic_cdk::update] +fn get_and_set(n: Nat) -> Nat { + COUNTER.with(|counter| { + let old = counter.borrow().clone(); + *counter.borrow_mut() = n; + old + }) +} + +/// Increment the value of the counter. +#[ic_cdk::update] +fn increment() { + COUNTER.with(|counter| *counter.borrow_mut() += 1_u32); +} diff --git a/rust/inter-canister-calls/dfx.json b/rust/inter-canister-calls/dfx.json deleted file mode 100644 index 3dd2e7f9f5..0000000000 --- a/rust/inter-canister-calls/dfx.json +++ /dev/null @@ -1,22 +0,0 @@ -{ - "canisters": { - "counter": { - "candid": "src/counter/counter.did", - "package": "counter", - "type": "rust" - }, - "caller": { - "candid": "src/caller/caller.did", - "package": "caller", - "type": "rust" - } - }, - "defaults": { - "build": { - "args": "", - "packtool": "" - } - }, - "output_env_file": ".env", - "version": 1 -} diff --git a/rust/inter-canister-calls/icp.yaml b/rust/inter-canister-calls/icp.yaml new file mode 100644 index 0000000000..ab478c35c6 --- /dev/null +++ b/rust/inter-canister-calls/icp.yaml @@ -0,0 +1,10 @@ +canisters: + - name: counter + recipe: + type: "@dfinity/rust@v3.3.0" + + - name: caller + recipe: + type: "@dfinity/rust@v3.3.0" + environment_variables: + - PUBLIC_CANISTER_ID:counter diff --git a/rust/inter-canister-calls/rust-toolchain.toml b/rust/inter-canister-calls/rust-toolchain.toml new file mode 100644 index 0000000000..990104f055 --- /dev/null +++ b/rust/inter-canister-calls/rust-toolchain.toml @@ -0,0 +1,2 @@ +[toolchain] +targets = ["wasm32-unknown-unknown"] diff --git a/rust/inter-canister-calls/src/caller/caller.did b/rust/inter-canister-calls/src/caller/caller.did deleted file mode 100644 index a7985092e6..0000000000 --- a/rust/inter-canister-calls/src/caller/caller.did +++ /dev/null @@ -1,28 +0,0 @@ -type StubbornSetResult = variant { - "Ok" : null; - "Err" : text; -}; - -type IncrementResult = variant { - "Ok" : null; - "Err" : text; -}; - -type GetResult = variant { - "Ok" : nat; - "Err" : text; -}; - -type SendCyclesResult = variant { - "Ok" : null; - "Err" : text; -}; - -service : { - "call_get_and_set": ( counter: principal, new_value: nat ) -> (nat); - "set_then_get": (counter: principal, new_value: nat) -> (nat); - "call_get": (counter: principal) -> (GetResult); - "call_increment": (counter: principal) -> (IncrementResult); - "stubborn_set": (counter: principal, new_value: nat) -> (StubbornSetResult); - "send_cycles": (target: principal, amount: nat64) -> (SendCyclesResult); -} \ No newline at end of file diff --git a/rust/inter-canister-calls/src/counter/counter.did b/rust/inter-canister-calls/src/counter/counter.did deleted file mode 100644 index 168034a1d4..0000000000 --- a/rust/inter-canister-calls/src/counter/counter.did +++ /dev/null @@ -1,6 +0,0 @@ -service : { - "get": () -> (nat); - "set": (nat) -> (); - "increment": () -> (); - "get_and_set": (nat) -> (nat); -} diff --git a/rust/inter-canister-calls/src/counter/src/lib.rs b/rust/inter-canister-calls/src/counter/src/lib.rs deleted file mode 100644 index 4d8e5817bc..0000000000 --- a/rust/inter-canister-calls/src/counter/src/lib.rs +++ /dev/null @@ -1,67 +0,0 @@ -use candid::types::number::Nat; -use std::cell::RefCell; - -thread_local! { - static COUNTER: RefCell = RefCell::new(Nat::from(0_u32)); -} - -/// Get the value of the counter. -#[ic_cdk_macros::query] -fn get() -> Nat { - COUNTER.with(|counter| (*counter.borrow()).clone()) -} - -/// Set the value of the counter. -#[ic_cdk_macros::update] -fn set(n: Nat) { - // COUNTER.replace(n); // requires #![feature(local_key_cell_methods)] - COUNTER.with(|count| *count.borrow_mut() = n); -} - -#[ic_cdk_macros::update] -fn get_and_set(n: Nat) -> Nat { - COUNTER.with(|counter| { - let old = counter.borrow().clone(); - *counter.borrow_mut() = n; - old - }) -} - -/// Increment the value of the counter. -#[ic_cdk_macros::update] -fn increment() { - COUNTER.with(|counter| *counter.borrow_mut() += 1_u32); -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn test_get_set() { - let expected = Nat::from(42_u32); - set(expected.clone()); - assert_eq!(get(), expected); - } - - #[test] - fn test_init() { - assert_eq!(get(), Nat::from(0_u32)); - } - - #[test] - fn test_inc() { - for i in 1..10_u32 { - inc(); - assert_eq!(get(), Nat::from(i)); - } - } - - #[test] - fn test_get_and_set() { - let old = get_and_set(Nat::from(1 as u32)); - let new = get(); - assert_eq!(old, Nat::from(0 as u32)); - assert_eq!(new, Nat::from(1 as u32)); - } -}