Skip to content

Commit ec6a768

Browse files
authored
fix(crypto): encrypted_notes example: use persistent actor (#235)
Adapts the encrypted_notes_dapp example so that it works again with ICP Ninja, which recently upgraded to DFX 0.29.1, which ships with a new motoko compiler (0.16.1). The necessary changes for this were * Declaring the actor as `persistent` * Marking all transient variables explicitly as transient, and removing the redundant stable keywords. Note that the example still uses the old way to persist data across updates, which is serialize/deserialize data in the preupgrade and postupgrade methods. The Github workflow sets the `DFX_VERSION` environment variable, so that only this example uses the new DFX version 0.29.1. This also required to change the bind host from `localhost` to `127.0.0.1` as otherwise this new DFX version hits the following error: ``` Error: The replica returned an HTTP Error: Http Error: status 400 Bad Request, content type "text/plain; charset=utf-8", content: error: no_authority details: The request is missing the required authority information (e.g. 'Host' header). ``` The changes were tested in ICP Ninja with the following link: [![](https://icp.ninja/assets/open.svg)](http://icp.ninja/editor?g=https://github.com/dfinity/vetkeys/tree/franzstefan/CRP-2924-enc-notes/examples/encrypted_notes_dapp_vetkd/motoko)
1 parent 1ce1f5f commit ec6a768

File tree

4 files changed

+17
-15
lines changed

4 files changed

+17
-15
lines changed

.github/workflows/examples-encrypted-notes-dapp.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ on:
1212
concurrency:
1313
group: ${{ github.workflow }}-${{ github.ref }}
1414
cancel-in-progress: true
15+
env:
16+
DFX_VERSION: 0.29.1
1517
jobs:
1618
examples-encrypted-notes-dapp-rust-darwin:
1719
runs-on: macos-15

examples/encrypted_notes_dapp_vetkd/motoko/backend/main.mo

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import Hex "./utils/Hex";
1616

1717
// Declare a shared actor class
1818
// Bind the caller and the initializer
19-
shared ({ caller = initializer }) actor class (keyName: Text) {
19+
shared ({ caller = initializer }) persistent actor class (keyName: Text) {
2020

2121
// Currently, a single canister smart contract is limited to 4 GB of heap size.
2222
// For the current limits see https://internetcomputer.org/docs/current/developer-docs/production/resource-limits.
@@ -27,10 +27,10 @@ shared ({ caller = initializer }) actor class (keyName: Text) {
2727
// memory usage must be calculated or monitored and the various restrictions adapted accordingly.
2828

2929
// Define dapp limits - important for security assurance
30-
private let MAX_USERS = 500;
31-
private let MAX_NOTES_PER_USER = 200;
32-
private let MAX_NOTE_CHARS = 1000;
33-
private let MAX_SHARES_PER_NOTE = 50;
30+
private transient let MAX_USERS = 500;
31+
private transient let MAX_NOTES_PER_USER = 200;
32+
private transient let MAX_NOTE_CHARS = 1000;
33+
private transient let MAX_SHARES_PER_NOTE = 50;
3434

3535
private type PrincipalName = Text;
3636
private type NoteId = Nat;
@@ -58,23 +58,23 @@ shared ({ caller = initializer }) actor class (keyName: Text) {
5858
// The keyword `stable` makes this (scalar) variable keep its value across canister upgrades.
5959
//
6060
// See https://internetcomputer.org/docs/current/developer-docs/setup/manage-canisters#upgrade-a-canister
61-
private stable var nextNoteId : Nat = 1;
61+
private var nextNoteId : Nat = 1;
6262

6363
// Store notes by their ID, so that note-specific encryption keys can be derived.
64-
private var notesById = Map.HashMap<NoteId, EncryptedNote>(0, Nat.equal, Hash.hash);
64+
private transient var notesById = Map.HashMap<NoteId, EncryptedNote>(0, Nat.equal, Hash.hash);
6565
// Store which note IDs are owned by a particular principal
66-
private var noteIdsByOwner = Map.HashMap<PrincipalName, List.List<NoteId>>(0, Text.equal, Text.hash);
66+
private transient var noteIdsByOwner = Map.HashMap<PrincipalName, List.List<NoteId>>(0, Text.equal, Text.hash);
6767
// Store which notes are shared with a particular principal. Does not include the owner, as this is tracked by `noteIdsByOwner`.
68-
private var noteIdsByUser = Map.HashMap<PrincipalName, List.List<NoteId>>(0, Text.equal, Text.hash);
68+
private transient var noteIdsByUser = Map.HashMap<PrincipalName, List.List<NoteId>>(0, Text.equal, Text.hash);
6969

7070
// While accessing _heap_ data is more efficient, we use the following _stable memory_
7171
// as a buffer to preserve data across canister upgrades.
7272
// Stable memory is currently 96GB. For the current limits see
7373
// https://internetcomputer.org/docs/current/developer-docs/production/resource-limits.
7474
// See also: [preupgrade], [postupgrade]
75-
private stable var stable_notesById : [(NoteId, EncryptedNote)] = [];
76-
private stable var stable_noteIdsByOwner : [(PrincipalName, List.List<NoteId>)] = [];
77-
private stable var stable_noteIdsByUser : [(PrincipalName, List.List<NoteId>)] = [];
75+
private var stable_notesById : [(NoteId, EncryptedNote)] = [];
76+
private var stable_noteIdsByOwner : [(PrincipalName, List.List<NoteId>)] = [];
77+
private var stable_noteIdsByUser : [(PrincipalName, List.List<NoteId>)] = [];
7878

7979
// Utility function that helps writing assertion-driven code more concisely.
8080
private func expect<T>(opt : ?T, violation_msg : Text) : T {
@@ -322,7 +322,7 @@ shared ({ caller = initializer }) actor class (keyName: Text) {
322322
}) -> async ({ encrypted_key : Blob });
323323
};
324324

325-
let management_canister : VETKD_API = actor ("aaaaa-aa");
325+
transient let management_canister : VETKD_API = actor ("aaaaa-aa");
326326

327327
public shared func symmetric_key_verification_key_for_note() : async Text {
328328
let { public_key } = await management_canister.vetkd_public_key({

examples/encrypted_notes_dapp_vetkd/motoko/dfx.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
},
3333
"networks": {
3434
"local": {
35-
"bind": "localhost:8000",
35+
"bind": "127.0.0.1:8000",
3636
"type": "ephemeral"
3737
}
3838
}

examples/encrypted_notes_dapp_vetkd/rust/dfx.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
},
3333
"networks": {
3434
"local": {
35-
"bind": "localhost:8000",
35+
"bind": "127.0.0.1:8000",
3636
"type": "ephemeral"
3737
}
3838
}

0 commit comments

Comments
 (0)