Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement canisterSubnet primitive #4857

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/codegen/compile_classical.ml
Original file line number Diff line number Diff line change
Expand Up @@ -5065,6 +5065,8 @@ module IC = struct
E.add_func_import env "ic0" "canister_status" [] [I32Type];
E.add_func_import env "ic0" "canister_version" [] [I64Type];
E.add_func_import env "ic0" "is_controller" (i32s 2) [I32Type];
E.add_func_import env "ic0" "subnet_self_copy" (i32s 3) [];
E.add_func_import env "ic0" "subnet_self_size" [] [I32Type];
E.add_func_import env "ic0" "debug_print" (i32s 2) [];
E.add_func_import env "ic0" "msg_arg_data_copy" (i32s 3) [];
E.add_func_import env "ic0" "msg_arg_data_size" [] [I32Type];
Expand Down Expand Up @@ -5337,6 +5339,18 @@ module IC = struct
| _ ->
E.trap_with env "cannot get self-actor-reference when running locally"

let get_subnet_reference env =
match E.mode env with
| Flags.(ICMode | RefMode) ->
Func.share_code0 Func.Never env "canister_subnet" [I32Type] (fun env ->
Blob.of_size_copy env Tagged.A
(fun env -> system_call env "subnet_self_size")
(fun env -> system_call env "subnet_self_copy")
(fun env -> compile_unboxed_const 0l)
)
| _ ->
E.trap_with env "cannot get actor-subnet-reference when running locally"

let get_system_time env =
match E.mode env with
| Flags.ICMode | Flags.RefMode ->
Expand Down Expand Up @@ -12015,7 +12029,10 @@ and compile_prim_invocation (env : E.t) ae p es at =
IC.get_self_reference env ^^
IC.actor_public_field env Type.(motoko_stable_var_info_fld.lab)

(* Other prims, binary*)
| OtherPrim "canister_subnet", [] ->
SR.Vanilla, IC.get_subnet_reference env

(* Other prims, binary *)
| OtherPrim "Array.init", [_;_] ->
const_sr SR.Vanilla (Arr.init env)
| OtherPrim "Array.tabulate", [_;_] ->
Expand Down
19 changes: 18 additions & 1 deletion src/codegen/compile_enhanced.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4723,6 +4723,8 @@ module IC = struct
E.add_func_import env "ic0" "canister_status" [] [I32Type];
E.add_func_import env "ic0" "canister_version" [] [I64Type];
E.add_func_import env "ic0" "is_controller" (i64s 2) [I32Type];
E.add_func_import env "ic0" "subnet_self_copy" (i64s 3) [];
E.add_func_import env "ic0" "subnet_self_size" [] [I64Type];
E.add_func_import env "ic0" "debug_print" (i64s 2) [];
E.add_func_import env "ic0" "msg_arg_data_copy" (i64s 3) [];
E.add_func_import env "ic0" "msg_arg_data_size" [] [I64Type];
Expand Down Expand Up @@ -5055,6 +5057,18 @@ module IC = struct
| _ ->
E.trap_with env "cannot get self-actor-reference when running locally"

let get_subnet_reference env =
match E.mode env with
| Flags.(ICMode | RefMode) ->
Func.share_code0 Func.Never env "canister_subnet" [I64Type] (fun env ->
Blob.of_size_copy env Tagged.A
(fun env -> system_call env "subnet_self_size")
(fun env -> system_call env "subnet_self_copy")
(fun env -> compile_unboxed_const 0L)
)
| _ ->
E.trap_with env "cannot get actor-subnet-reference when running locally"

let get_system_time env =
match E.mode env with
| Flags.ICMode | Flags.RefMode ->
Expand Down Expand Up @@ -12104,7 +12118,10 @@ and compile_prim_invocation (env : E.t) ae p es at =
IC.get_self_reference env ^^
IC.actor_public_field env Type.(motoko_stable_var_info_fld.lab)

(* Other prims, binary*)
| OtherPrim "canister_subnet", [] ->
SR.Vanilla, IC.get_subnet_reference env

(* Other prims, binary *)
| OtherPrim "Array.init", [_;_] ->
const_sr SR.Vanilla (Arr.init env)
| OtherPrim "Array.tabulate", [_;_] ->
Expand Down
3 changes: 3 additions & 0 deletions src/lowering/desugar.ml
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ and exp' at note = function
| S.CallE ({it=S.AnnotE ({it=S.PrimE "deadline";_},_);_}, _, {it=S.TupE es;_}) ->
assert (es = []);
I.PrimE (I.ICReplyDeadlinePrim, [])
| S.CallE ({it=S.AnnotE ({it=S.PrimE "canister_subnet";_},_);_}, _, {it=S.TupE es;_}) ->
assert (es = []);
I.PrimE (I.OtherPrim "canister_subnet", [])
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not covered in line 202, we could think about introducing a non-OtherPrim one.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, maybe it is covered in line 200?

| S.CallE ({it=S.AnnotE ({it=S.PrimE "time";_},_);_}, _, {it=S.TupE es;_}) ->
assert (es = []);
I.PrimE (I.SystemTimePrim, [])
Expand Down
3 changes: 3 additions & 0 deletions src/mo_values/prim.ml
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,9 @@ let prim trap =
| "canister_version" ->
fun _ v k -> as_unit v; k (Nat64 (Numerics.Nat64.of_int 42))

| "canister_subnet" ->
fun _ v k -> as_unit v; k (Blob "")

(* fake *)
| "setCandidLimits" ->
fun _ v k -> k unit
Expand Down
1 change: 1 addition & 0 deletions src/prelude/prim.mo
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ func principalOfBlob(act : Blob) : Principal {
func principalOfActor(act : actor {}) : Principal = (prim "principalOfActor" : (actor {}) -> Principal) act;
func isController(p : Principal) : Bool = (prim "is_controller" : Principal -> Bool) p;
func canisterVersion() : Nat64 = (prim "canister_version" : () -> Nat64)();
func canisterSubnet() : Principal = (prim "canister_subnet" : () -> Principal)();

// Untyped dynamic actor creation from blobs
let createActor : (wasm : Blob, argument : Blob) -> async Principal = @create_actor_helper;
Expand Down
1 change: 1 addition & 0 deletions test/run-drun/upgrades/upgrade0.mo
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Prim "mo:⛔";
actor {
Prim.debugPrint ("init'ed 0");
Prim.debugPrint ("initial subnet: " # debug_show Prim.canisterSubnet());
Prim.debugPrint ("initial version: " # debug_show Prim.canisterVersion());
stable var c = "a";
public func inc() { c #= "a"; };
Expand Down
Loading