diff --git a/e2e/bats/assetscanister.bash b/e2e/bats/assetscanister.bash index 7221c11de7..99d0775538 100644 --- a/e2e/bats/assetscanister.bash +++ b/e2e/bats/assetscanister.bash @@ -42,5 +42,6 @@ teardown() { assert_eq '(vec { 88; 87; 86; })' assert_command_fail dfx canister call --query e2e_project_assets retrieve '("C")' -} + HOME=. assert_command_fail dfx canister call --update e2e_project_assets store '("index.js", vec { 1; 2; 3; })' +} diff --git a/nix/sources.json b/nix/sources.json index 5d65c7bda7..6d559c9876 100644 --- a/nix/sources.json +++ b/nix/sources.json @@ -46,7 +46,7 @@ "motoko": { "ref": "master", "repo": "ssh://git@github.com/dfinity-lab/motoko", - "rev": "719ea2b3f54de4c4c867bd34a8284e52b49999a7", + "rev": "ea4585c4b3606689e224c89ae809cf50ab07df30", "type": "git" }, "napalm": { diff --git a/src/distributed/assetstorage/assetstorage.mo b/src/distributed/assetstorage/assetstorage.mo index 04d0af480b..eb6fa747e3 100644 --- a/src/distributed/assetstorage/assetstorage.mo +++ b/src/distributed/assetstorage/assetstorage.mo @@ -1,31 +1,37 @@ -import A "mo:base/AssocList"; -import L "mo:base/List"; +import AssocList "mo:base/AssocList"; +import Error "mo:base/Error"; +import List "mo:base/List"; import Prim "mo:prim"; actor { + public type Path = Text; + public type Contents = Blob; - var stored: A.AssocList = L.nil<(Path, Contents)>(); + private let initializer : Principal = Prim.caller(); + + private stable var db: AssocList.AssocList = List.nil(); - func pathEq(a: Path, b: Path): Bool { + func eq(a: Path, b: Path): Bool { return a == b; }; - public func store(path : Path, contents: Contents) : async () { - let (newStored, _) = A.replace(stored, path, pathEq, ?contents); - stored := newStored; + public shared { caller } func store(path : Path, contents : Contents) : async () { + if (caller != initializer) { + throw Error.reject("not authorized"); + } else { + db := AssocList.replace(db, path, eq, ?contents).0; + }; }; - public query func retrieve(path: Path): async Contents { - let result = A.find(stored, path, pathEq); + public query func retrieve(path : Path) : async Contents { + let result = AssocList.find(db, path, eq); switch result { case null { - // more than 8 chars treated as invalid UTF-8 - // TODO: https://github.com/dfinity-lab/sdk/issues/701 - throw Prim.error("notfound") + throw Error.reject("not found"); }; - case (?contents) { contents }; - } + case (?contents) contents; + }; }; };