Skip to content

Commit 2671148

Browse files
authored
chore: migrate motoko/low_wasm_memory to icp-cli (#1379)
1 parent 0941f49 commit 2671148

8 files changed

Lines changed: 90 additions & 104 deletions

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: low_wasm_memory
2+
3+
on:
4+
push:
5+
branches: [master]
6+
pull_request:
7+
paths:
8+
- motoko/low_wasm_memory/**
9+
- .github/workflows/low_wasm_memory.yml
10+
11+
concurrency:
12+
group: ${{ github.workflow }}-${{ github.ref }}
13+
cancel-in-progress: true
14+
15+
jobs:
16+
motoko-low_wasm_memory:
17+
runs-on: ubuntu-24.04
18+
container: ghcr.io/dfinity/icp-dev-env-motoko:0.3.2
19+
env:
20+
ICP_CLI_GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
21+
steps:
22+
- uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4.3.1
23+
- name: Deploy and test
24+
working-directory: motoko/low_wasm_memory
25+
run: |
26+
icp network start -d
27+
icp deploy
28+
make test

‎.github/workflows/motoko-low_wasm_memory-example.yml‎

Lines changed: 0 additions & 50 deletions
This file was deleted.

‎motoko/low_wasm_memory/Makefile‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
.PHONY: configure test
2+
3+
# Set wasm_memory_limit to 5 MiB and wasm_memory_threshold to 2 MiB.
4+
# The low Wasm memory hook fires when remaining Wasm memory falls below the
5+
# threshold (i.e. when usage exceeds limit - threshold = 3 MiB).
6+
configure:
7+
icp canister settings update backend \
8+
--wasm-memory-limit 5mib \
9+
--wasm-memory-threshold 2mib \
10+
-f
11+
12+
test: configure
13+
@echo "=== Polling for onLowWasmMemory hook (up to 60s) ==="
14+
@secs=0; \
15+
while [ $$secs -lt 60 ]; do \
16+
result=$$(icp canister call --query backend getExecutedFunctionsOrder '()') || \
17+
{ echo "FAIL: canister call failed"; exit 1; }; \
18+
echo "$$result"; \
19+
echo "$$result" | grep -q 'onLowWasmMemory' && echo "PASS" && exit 0; \
20+
sleep 3; secs=$$((secs + 3)); \
21+
done; \
22+
echo "FAIL: onLowWasmMemory hook did not fire within 60s"; exit 1

‎motoko/low_wasm_memory/README.md‎

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,47 @@
11
# Low Wasm memory hook
22

3-
The Internet Computer can automatically execute a special type of function called a low Wasm memory hook, which runs when the canister's available Wasm memory falls below the `wasm_memory_threshold`.
3+
This example demonstrates the low Wasm memory hook on the Internet Computer. The ICP runtime automatically executes a special `lowmemory` system function when a canister's available Wasm memory falls below the configured `wasm_memory_threshold`. The example shows the execution order of the `heartbeat` and `lowmemory` system functions as the canister progressively allocates memory until the threshold is reached.
44

5-
This Motoko example demonstrates using the low Wasm memory hook on ICP. For more information take a look at [low Wasm memory hook specification](https://internetcomputer.org/docs/references/ic-interface-spec#on-low-wasm-memory).
5+
For more information, see the [low Wasm memory hook specification](https://docs.internetcomputer.org/references/ic-interface-spec/canister-interface/#on-low-wasm-memory).
66

7-
## Deploying from ICP Ninja
7+
## Build and deploy from the command line
88

9-
[![](https://icp.ninja/assets/open.svg)](https://icp.ninja/editor?g=https://github.com/dfinity/examples/tree/master/motoko/low_wasm_memory)
9+
### Prerequisites
1010

11-
## Build and deploy from the command-line
11+
- Node.js
12+
- icp-cli: `npm install -g @icp-sdk/icp-cli @icp-sdk/ic-wasm`
13+
- ic-mops: `npm install -g ic-mops`
1214

13-
### 1. [Download and install the IC SDK.](https://internetcomputer.org/docs/building-apps/getting-started/install)
15+
### Install
1416

15-
### 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/)
16-
17-
### 3. Navigate into the project's directory.
18-
19-
### 4. Deploy the project to your local environment:
20-
21-
```
22-
dfx start --background --clean && dfx deploy
17+
```bash
18+
git clone https://github.com/dfinity/examples
19+
cd examples/motoko/low_wasm_memory
2320
```
2421

25-
After the deployment, the memory usage periodically increases as defined in the `heartbeat` function.
22+
### Deploy and test
2623

27-
The `dfx canister update-settings` command sets the 'wasm_memory_limit' to 5MiB and 'wasm_memory_threshold' to 2MiB.
28-
Hence, whenever the Wasm memory used by the canister is above 3MiB (in other words, the remaining Wasm memory is less than 'wasm_memory_threshold'), the low Wasm memory hook will be triggered.
29-
30-
This Motoko canister starts with ~2.3MiB of memory usage after the deployment, so it will trigger the low Wasm memory hook after allocating ~0.7MiB of memory.
24+
```bash
25+
icp network start -d
26+
icp deploy
27+
make test
28+
icp network stop
29+
```
3130

32-
You can verify that the canister settings got updated and check the current 'Memory Size' with the following command:
31+
`make test` does two things:
3332

34-
```sh
35-
dfx canister status low_wasm_memory_hook
36-
```
33+
1. **Configures the canister settings**: sets `wasm_memory_limit` to 5 MiB and `wasm_memory_threshold` to 2 MiB. This means the `lowmemory` hook fires when remaining Wasm memory falls below 2 MiB (i.e. when usage exceeds 3 MiB). The canister starts with ~2.3 MiB of usage after deployment, so the hook triggers after allocating roughly 0.7 MiB more.
3734

38-
After a few seconds, observe the output of the `getExecutedFunctionsOrder` query:
35+
2. **Polls `getExecutedFunctionsOrder`** until `onLowWasmMemory` appears as the last entry (or times out after 60 s).
3936

40-
Query the canister by calling `getExecutedFunctionsOrder` to get the order of executed functions.
37+
To observe the execution order manually:
4138

42-
```sh
43-
dfx canister call low_wasm_memory_hook --query getExecutedFunctionsOrder
39+
```bash
40+
icp canister call --query backend getExecutedFunctionsOrder '()'
4441
```
4542

46-
Repeat the call until the last executed method is `onLowWasmMemory`.
43+
Repeat until the last entry is `onLowWasmMemory`.
4744

4845
## Security considerations and best practices
4946

50-
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.
47+
Refer to the [security best practices](https://docs.internetcomputer.org/guides/security/overview) for information on security and best practices for your ICP app.

motoko/low_wasm_memory/src/low_wasm_memory_hook/main.mo renamed to motoko/low_wasm_memory/backend/app.mo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import Array "mo:core/Array";
22
import Queue "mo:core/Queue";
33
import List "mo:core/List";
44

5-
persistent actor {
5+
actor {
66
// Types
77
type FnType = {
88
#heartbeat;

‎motoko/low_wasm_memory/dfx.json‎

Lines changed: 0 additions & 16 deletions
This file was deleted.

‎motoko/low_wasm_memory/icp.yaml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
canisters:
2+
- name: backend
3+
recipe:
4+
type: "@dfinity/motoko@v5.0.0"

‎motoko/low_wasm_memory/mops.toml‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
# Motoko dependencies (https://mops.one/)
2-
31
[toolchain]
4-
moc = "1.5.1"
2+
moc = "1.9.0"
53

64
[dependencies]
7-
core = "2.4.0"
5+
core = "2.5.0"
86

97
[moc]
10-
# M0236: use context dot notation (e.g. map.get(k) instead of Map.get(map, compare, k))
11-
# M0237: redundant explicit implicit arguments (e.g. Nat.compare is inferred automatically)
12-
# M0223: redundant type instantiation (e.g. Array.tabulate instead of Array.tabulate<T>)
13-
args = ["-W=M0236,M0237,M0223"]
8+
# M0236: use context dot notation
9+
# M0237: redundant explicit implicit arguments
10+
# M0223: redundant type instantiation
11+
args = ["--default-persistent-actors", "-W=M0236,M0237,M0223"]
12+
13+
[canisters.backend]
14+
main = "backend/app.mo"

0 commit comments

Comments
 (0)