From 9660889e1ccbc130823fd07b6f4bb71835a688a7 Mon Sep 17 00:00:00 2001 From: Jordan Last Date: Mon, 15 May 2023 13:30:57 -0500 Subject: [PATCH] saving my place --- Cargo.lock | 359 ++++++++++++++++++ Cargo.toml | 3 +- examples/outgoing_http_requests/dfx.json | 2 +- kybra/__main__.py | 36 +- kybra/candid.py | 4 +- kybra/cargotoml.py | 8 +- kybra/compiler/install_rust_dependencies.sh | 7 +- kybra/compiler/kybra_deployer/Cargo.toml | 11 + kybra/compiler/kybra_deployer/src/lib.rs | 39 ++ .../src/header/use_statements.rs | 1 - .../src/kybra_modules_init/mod.rs | 15 +- .../kybra_modules_init/src/Lib/uuid.py | 2 + kybra/compiler/kybra_modules_init/src/main.rs | 79 +++- 13 files changed, 536 insertions(+), 30 deletions(-) create mode 100644 kybra/compiler/kybra_deployer/Cargo.toml create mode 100644 kybra/compiler/kybra_deployer/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index d75ea90f2..0e7ea0be1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -67,6 +67,12 @@ version = "1.0.65" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "98161a4e3e2184da77bb14f02184cdd111e83bbbcc9979dfee3c44b9a85f5602" +[[package]] +name = "arrayvec" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23b62fc65de8e4e7f52534fb52b0f3ed04746ae267519eef2a83941e8085068b" + [[package]] name = "ascii" version = "1.1.0" @@ -114,6 +120,12 @@ dependencies = [ "rustc-demangle", ] +[[package]] +name = "beef" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a8241f3ebb85c056b509d4327ad0358fbbba6ffb340bf388f26350aeda225b1" + [[package]] name = "bincode" version = "1.3.3" @@ -123,6 +135,29 @@ dependencies = [ "serde", ] +[[package]] +name = "binread" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "16598dfc8e6578e9b597d9910ba2e73618385dc9f4b1d43dd92c349d6be6418f" +dependencies = [ + "binread_derive", + "lazy_static", + "rustversion", +] + +[[package]] +name = "binread_derive" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d9672209df1714ee804b1f4d4f68c8eb2a90b1f7a07acf472f88ce198ef1fed" +dependencies = [ + "either", + "proc-macro2", + "quote", + "syn 1.0.99", +] + [[package]] name = "bit-set" version = "0.5.3" @@ -144,6 +179,15 @@ version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" +[[package]] +name = "block-buffer" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" +dependencies = [ + "generic-array", +] + [[package]] name = "bstr" version = "0.2.17" @@ -161,6 +205,92 @@ version = "3.12.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3c6ed94e98ecff0c12dd1b04c15ec0d7d9458ca8fe806cea6f12954efe74c63b" +[[package]] +name = "byteorder" +version = "1.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" + +[[package]] +name = "candid" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "244005a1917bb7614cd775ca8a5d59efeb5ac74397bb14ba29a19347ebd78591" +dependencies = [ + "anyhow", + "binread", + "byteorder", + "candid_derive 0.5.0", + "codespan-reporting", + "crc32fast", + "data-encoding", + "hex", + "lalrpop", + "lalrpop-util", + "leb128", + "logos", + "num-bigint", + "num-traits", + "num_enum", + "paste", + "pretty", + "serde", + "serde_bytes", + "sha2", + "thiserror", +] + +[[package]] +name = "candid" +version = "0.9.0-beta.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2ee0d6ee2234fc18021430aad8c6125f6856407014f2c2e8baf74c236427dbfc" +dependencies = [ + "anyhow", + "binread", + "byteorder", + "candid_derive 0.6.0", + "codespan-reporting", + "crc32fast", + "data-encoding", + "hex", + "leb128", + "num-bigint", + "num-traits", + "num_enum", + "paste", + "pretty", + "serde", + "serde_bytes", + "sha2", + "stacker", + "thiserror", +] + +[[package]] +name = "candid_derive" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "58f1f4db7c7d04b87b70b3a35c5dc5c2c9dd73cef8bdf6760e2f18a0d45350dd" +dependencies = [ + "lazy_static", + "proc-macro2", + "quote", + "syn 1.0.99", +] + +[[package]] +name = "candid_derive" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34df46ac8b0a44de9b243d9dd21e608838183be20de408c934993ed6a4a8e4c1" +dependencies = [ + "lazy_static", + "proc-macro2", + "quote", + "syn 1.0.99", +] + [[package]] name = "caseless" version = "0.2.1" @@ -242,6 +372,34 @@ dependencies = [ "winapi", ] +[[package]] +name = "codespan-reporting" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3538270d33cc669650c4b093848450d380def10c331d38c768e34cac80576e6e" +dependencies = [ + "termcolor", + "unicode-width", +] + +[[package]] +name = "cpufeatures" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3e4c1eaa2012c47becbbad2ab175484c2a84d1185b566fb2cc5b8707343dfe58" +dependencies = [ + "libc", +] + +[[package]] +name = "crc32fast" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b540bd8bc810d3885c6ea91e2018302f68baba2129ab3e88f32389ee9370880d" +dependencies = [ + "cfg-if", +] + [[package]] name = "crossbeam-utils" version = "0.8.15" @@ -257,12 +415,38 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7a81dae078cea95a014a339291cec439d2f232ebe854a9d672b796c6afafa9b7" +[[package]] +name = "crypto-common" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" +dependencies = [ + "generic-array", + "typenum", +] + +[[package]] +name = "data-encoding" +version = "2.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23d8666cb01533c39dde32bcbab8e227b4ed6679b2c925eba05feabea39508fb" + [[package]] name = "diff" version = "0.1.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "56254986775e3233ffa9c4d7d3faaf6d36a2c09d30b20687e9f88bc8bafc16c8" +[[package]] +name = "digest" +version = "0.10.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8168378f4e5023e7218c89c891c0fd8ecdb5e5e4f18cb78f38cf245dd021e76f" +dependencies = [ + "block-buffer", + "crypto-common", +] + [[package]] name = "dirs-next" version = "2.0.0" @@ -379,6 +563,22 @@ version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0ce7134b9999ecaf8bcd65542e436736ef32ddca1b3e06094cb6ec5755203b80" +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + +[[package]] +name = "generic-array" +version = "0.14.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" +dependencies = [ + "typenum", + "version_check", +] + [[package]] name = "getrandom" version = "0.2.7" @@ -456,6 +656,53 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dfa686283ad6dd069f105e5ab091b04c62850d3e4cf5d67debad1933f55023df" +[[package]] +name = "ic-cdk" +version = "0.8.0-beta.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "229a2796b731794363b45ab7b3a8ae28dc4220256997709e52746887425c5135" +dependencies = [ + "candid 0.9.0-beta.3", + "ic-cdk-macros 0.8.0-beta.0", + "ic0", + "serde", + "serde_bytes", +] + +[[package]] +name = "ic-cdk-macros" +version = "0.6.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ebf50458685a0fc6b0e414cdba487610aeb199ac94db52d9fd76270565debee7" +dependencies = [ + "candid 0.8.4", + "proc-macro2", + "quote", + "serde", + "serde_tokenstream", + "syn 1.0.99", +] + +[[package]] +name = "ic-cdk-macros" +version = "0.8.0-beta.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a8ed566a9a45e97f8dd7eed3db58c95875272dbf053f0d94a965319cbffdb9e0" +dependencies = [ + "candid 0.9.0-beta.3", + "proc-macro2", + "quote", + "serde", + "serde_tokenstream", + "syn 1.0.99", +] + +[[package]] +name = "ic0" +version = "0.18.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "978b91fc78de9d2eb0144db717839cde3b35470199ea51aca362cb6310e93dfd" + [[package]] name = "indexmap" version = "1.9.3" @@ -530,6 +777,14 @@ dependencies = [ "syn 1.0.99", ] +[[package]] +name = "kybra_deployer" +version = "0.0.0" +dependencies = [ + "ic-cdk", + "ic-cdk-macros 0.6.10", +] + [[package]] name = "kybra_generate" version = "0.0.0" @@ -592,6 +847,12 @@ version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" +[[package]] +name = "leb128" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "884e2677b40cc8c339eaefcb701c32ef1fd2493d71118dc0ca4b6a736c93bd67" + [[package]] name = "lexical-parse-float" version = "0.8.5" @@ -653,6 +914,29 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "logos" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf8b031682c67a8e3d5446840f9573eb7fe26efe7ec8d195c9ac4c0647c502f1" +dependencies = [ + "logos-derive", +] + +[[package]] +name = "logos-derive" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1d849148dbaf9661a6151d1ca82b13bb4c4c128146a88d05253b38d4e2f496c" +dependencies = [ + "beef", + "fnv", + "proc-macro2", + "quote", + "regex-syntax", + "syn 1.0.99", +] + [[package]] name = "lz4_flex" version = "0.9.5" @@ -991,6 +1275,16 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "925383efa346730478fb4838dbe9137d2a47675ad789c546d150a6e1dd4ab31c" +[[package]] +name = "pretty" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ad9940b913ee56ddd94aec2d3cd179dd47068236f42a1a6415ccf9d880ce2a61" +dependencies = [ + "arrayvec", + "typed-arena", +] + [[package]] name = "proc-macro-crate" version = "1.3.1" @@ -1010,6 +1304,15 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "psm" +version = "0.1.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5787f7cda34e3033a72192c018bc5883100330f362ef279a8cbccfce8bb4e874" +dependencies = [ + "cc", +] + [[package]] name = "quote" version = "1.0.26" @@ -1506,6 +1809,15 @@ dependencies = [ "serde_derive", ] +[[package]] +name = "serde_bytes" +version = "0.11.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "416bda436f9aab92e02c8e10d49a15ddd339cea90b6e340fe51ed97abb548294" +dependencies = [ + "serde", +] + [[package]] name = "serde_derive" version = "1.0.160" @@ -1517,6 +1829,28 @@ dependencies = [ "syn 2.0.15", ] +[[package]] +name = "serde_tokenstream" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "797ba1d80299b264f3aac68ab5d12e5825a561749db4df7cd7c8083900c5d4e9" +dependencies = [ + "proc-macro2", + "serde", + "syn 1.0.99", +] + +[[package]] +name = "sha2" +version = "0.10.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "82e6b795fe2e3b1e845bafcb27aa35405c4d47cdfc92af5fc8d3002f76cebdc0" +dependencies = [ + "cfg-if", + "cpufeatures", + "digest", +] + [[package]] name = "siphasher" version = "0.3.10" @@ -1540,6 +1874,19 @@ dependencies = [ "optional", ] +[[package]] +name = "stacker" +version = "0.1.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c886bd4480155fd3ef527d45e9ac8dd7118a898a46530b7b94c3e21866259fce" +dependencies = [ + "cc", + "cfg-if", + "libc", + "psm", + "winapi", +] + [[package]] name = "static_assertions" version = "1.1.0" @@ -1767,6 +2114,18 @@ dependencies = [ "static_assertions", ] +[[package]] +name = "typed-arena" +version = "2.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6af6ae20167a9ece4bcb41af5b80f8a1f1df981f6391189ce00fd257af04126a" + +[[package]] +name = "typenum" +version = "1.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "497961ef93d974e23eb6f433eb5fe1b7930b659f06d12dec6fc44a8f554c0bba" + [[package]] name = "uname" version = "0.1.1" diff --git a/Cargo.toml b/Cargo.toml index b120fd655..b513a9a0d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,8 @@ members = [ "kybra/compiler/kybra_generate", "kybra/compiler/kybra_vm_value_derive", - "kybra/compiler/kybra_modules_init" + "kybra/compiler/kybra_modules_init", + "kybra/compiler/kybra_deployer" ] exclude = ["examples"] edition = "2018" diff --git a/examples/outgoing_http_requests/dfx.json b/examples/outgoing_http_requests/dfx.json index cc9118047..e6529d5bc 100644 --- a/examples/outgoing_http_requests/dfx.json +++ b/examples/outgoing_http_requests/dfx.json @@ -2,7 +2,7 @@ "canisters": { "outgoing_http_requests": { "type": "custom", - "build": "python -m kybra outgoing_http_requests src/main.py src/main.did", + "build": "python -m kybra outgoing_http_requests src/main.py src/main.did --verbose", "post_install": ".kybra/outgoing_http_requests/post_install.sh", "candid": "src/main.did", "wasm": ".kybra/outgoing_http_requests/outgoing_http_requests.wasm.gz", diff --git a/kybra/__main__.py b/kybra/__main__.py index 45a100f77..1a7379830 100644 --- a/kybra/__main__.py +++ b/kybra/__main__.py @@ -51,7 +51,7 @@ def main(): shutil.rmtree(paths["canister"]) shutil.copytree(paths["compiler"], paths["canister"], dirs_exist_ok=True) create_file(f"{paths['canister']}/Cargo.toml", generate_cargo_toml(canister_name)) - create_file(f"{paths['canister']}/Cargo.lock", generate_cargo_lock()) + # create_file(f"{paths['canister']}/Cargo.lock", generate_cargo_lock()) create_file( f"{paths['canister']}/post_install.sh", generate_post_install_script(canister_name, kybra.__rust_version__), @@ -389,14 +389,18 @@ def build_wasm_binary_or_exit( shutil.copy( f"{paths['global_kybra_target_dir']}/wasm32-wasi/release/{canister_name}.wasm", - paths["wasm"], + f"{paths['canister']}/{canister_name}_final.wasm", ) - candid_file = generate_candid_file(paths) + candid_file = generate_candid_file(paths, canister_name) create_file(paths["did"], candid_file) wasi2ic_result = subprocess.run( - [f"{paths['global_kybra_bin_dir']}/wasi2ic", paths["wasm"], paths["wasm"]], + [ + f"{paths['global_kybra_bin_dir']}/wasi2ic", + f"{paths['canister']}/{canister_name}_final.wasm", + f"{paths['canister']}/{canister_name}_final.wasm", + ], capture_output=not verbose, env=cargo_env, ) @@ -407,6 +411,30 @@ def build_wasm_binary_or_exit( print("šŸ’€ Build failed") sys.exit(1) + kybra_deployer_build_result = subprocess.run( + [ + f"{paths['global_kybra_bin_dir']}/cargo", + "build", + f"--manifest-path={paths['canister']}/kybra_deployer/Cargo.toml", + "--target=wasm32-unknown-unknown", + f"--package=kybra_deployer", + "--release", + ], + capture_output=not verbose, + env=cargo_env, + ) + + if kybra_deployer_build_result.returncode != 0: + print(red("\nšŸ’£ Error building Wasm binary:")) + print(cargo_build_result.stderr.decode("utf-8")) + print("šŸ’€ Build failed") + sys.exit(1) + + shutil.copy( + f"{paths['global_kybra_target_dir']}/wasm32-unknown-unknown/release/kybra_deployer.wasm", + f"{paths['canister']}/{canister_name}.wasm", + ) + @timed_inline def optimize_wasm_binary_or_exit( diff --git a/kybra/candid.py b/kybra/candid.py index cfe9e7b1b..f3fb041e3 100644 --- a/kybra/candid.py +++ b/kybra/candid.py @@ -11,8 +11,8 @@ ) -def generate_candid_file(paths) -> str: - file = open(paths["wasm"], "rb") +def generate_candid_file(paths, canister_name) -> str: + file = open(f"{paths['canister']}/{canister_name}_final.wasm", "rb") wasm_buffer = file.read() file.close() diff --git a/kybra/cargotoml.py b/kybra/cargotoml.py index 91eb588b5..96a21b147 100644 --- a/kybra/cargotoml.py +++ b/kybra/cargotoml.py @@ -9,6 +9,8 @@ def generate_cargo_toml(canister_name: str) -> str: opt-level = 'z' codegen-units = 1 lto = "fat" +# strip = true +panic = "abort" [lib] crate-type = ["cdylib"] @@ -19,8 +21,6 @@ def generate_cargo_toml(canister_name: str) -> str: ic-cdk-timers = "0.1.2" candid = {{ version = "0.9.0-beta.2", features = ["parser"] }} kybra-vm-value-derive = {{ path = "./kybra_vm_value_derive" }} -# TODO add this back once we support the full stdlib: https://github.com/demergent-labs/kybra/issues/12 -# rustpython = {{ git = "https://github.com/demergent-labs/RustPython", rev = "b7b0a4994d7871bf1e21fedb6bd0f0e5639fa874", default-features = false, features = ["stdlib", "freeze-stdlib"] }} rustpython = {{ git = "https://github.com/demergent-labs/RustPython", rev = "b7b0a4994d7871bf1e21fedb6bd0f0e5639fa874", default-features = false, features = ["stdlib", "encodings"] }} rustpython-vm = {{ git = "https://github.com/demergent-labs/RustPython", rev = "b7b0a4994d7871bf1e21fedb6bd0f0e5639fa874", default-features = false, features = ["ic"] }} @@ -41,12 +41,10 @@ def generate_cargo_toml(canister_name: str) -> str: # TODO add this back once we support the full stdlib: https://github.com/demergent-labs/kybra/issues/12 # rustpython-pylib = {{ git = "https://github.com/demergent-labs/RustPython", rev = "b7b0a4994d7871bf1e21fedb6bd0f0e5639fa874", default-features = false, features = ["freeze-stdlib"] }} # rustpython = {{ path = "../../../../../../RustPython", default-features = false, features = [] }} -getrandom = {{ version = "0.2.3", features = ["custom"] }} -serde = "1.0.137" +serde = {{ version = "1.0.137", default-features = false, features = [] }} async-recursion = "1.0.0" ic-stable-structures = "0.5.2" slotmap = "1.0.6" -rand = "0.8.5" ic-wasi-polyfill = {{ git = "https://github.com/wasm-forge/ic-wasi-polyfill", rev = "01dc79c84dc1de37bba104ac0a87f4faf7308937" }} [patch.crates-io] diff --git a/kybra/compiler/install_rust_dependencies.sh b/kybra/compiler/install_rust_dependencies.sh index 1b4331f54..cc614ea83 100755 --- a/kybra/compiler/install_rust_dependencies.sh +++ b/kybra/compiler/install_rust_dependencies.sh @@ -28,7 +28,7 @@ function run() { mkdir -p "$global_kybra_logs_dir" install_rustup - install_wasm32_wasi + install_wasm32 install_wasi2ic install_ic_wasm "$ic_wasm_already_installed" else @@ -46,10 +46,11 @@ function update_rustup() { "$global_kybra_rustup_bin" update "$rust_version" &> "$global_kybra_logs_dir"/rustup_update } -function install_wasm32_wasi() { - echo -e "2/4) Installing wasm32-wasi" +function install_wasm32() { + echo -e "2/4) Installing wasm32" "$global_kybra_rustup_bin" target add wasm32-wasi &> "$global_kybra_logs_dir"/install_wasm32_wasi + "$global_kybra_rustup_bin" target add wasm32-unknown-unknown &> "$global_kybra_logs_dir"/install_wasm32_unknown_unknown } function install_wasi2ic() { diff --git a/kybra/compiler/kybra_deployer/Cargo.toml b/kybra/compiler/kybra_deployer/Cargo.toml new file mode 100644 index 000000000..1f3d7d3ad --- /dev/null +++ b/kybra/compiler/kybra_deployer/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "kybra_deployer" +version = "0.0.0" +edition = "2018" + +[lib] +crate-type = ["cdylib"] + +[dependencies] +ic-cdk = "0.8.0-beta" +ic-cdk-macros = "0.6.10" diff --git a/kybra/compiler/kybra_deployer/src/lib.rs b/kybra/compiler/kybra_deployer/src/lib.rs new file mode 100644 index 000000000..0afbd90a6 --- /dev/null +++ b/kybra/compiler/kybra_deployer/src/lib.rs @@ -0,0 +1,39 @@ +thread_local! { + static WASM_REF_CELL: std::cell::RefCell> = std::cell::RefCell::new(vec![]); + // static STDLIB_REF_CELL +} + +#[ic_cdk_macros::update] +pub fn upload_wasm_chunk(bytes: Vec) { + WASM_REF_CELL.with(|wasm_ref_cell| { + let mut wasm_ref_mut = wasm_ref_cell.borrow_mut(); + wasm_ref_mut.extend(bytes); + }); +} + +// TODO we want to use notify +// TODO figure out args +// TODO it will probably be similar to kybra_modules_init +// TODO I think for the init stuff...I wonder if I can just store the args as binary +#[ic_cdk_macros::update] +pub async fn install_wasm() { + let wasm_module = WASM_REF_CELL.with(|wasm_ref_cell| wasm_ref_cell.borrow().clone()); + + // TODO figure out init/post_upgrade + let result = ic_cdk::api::management_canister::main::install_code( + ic_cdk::api::management_canister::main::InstallCodeArgument { + mode: ic_cdk::api::management_canister::main::CanisterInstallMode::Upgrade, + canister_id: ic_cdk::api::id(), + wasm_module, + arg: vec![], + }, + ) + .await; + + match result { + Ok(_) => {} + Err(err) => { + panic!("{:#?}", err); + } + } +} diff --git a/kybra/compiler/kybra_generate/src/header/use_statements.rs b/kybra/compiler/kybra_generate/src/header/use_statements.rs index 6f673c995..f382ffd8c 100644 --- a/kybra/compiler/kybra_generate/src/header/use_statements.rs +++ b/kybra/compiler/kybra_generate/src/header/use_statements.rs @@ -4,7 +4,6 @@ pub fn generate() -> TokenStream { quote::quote! { use candid::{Decode, Encode}; use kybra_vm_value_derive::{CdkActTryIntoVmValue, CdkActTryFromVmValue}; - use rand::Rng as _KybraTraitRng; use rustpython_vm::{ class::PyClassImpl as _KybraTraitPyClassImpl, convert::ToPyObject as _KybraTraitToPyObject, diff --git a/kybra/compiler/kybra_generate/src/kybra_modules_init/mod.rs b/kybra/compiler/kybra_generate/src/kybra_modules_init/mod.rs index a9a7b830b..894e34ee9 100644 --- a/kybra/compiler/kybra_generate/src/kybra_modules_init/mod.rs +++ b/kybra/compiler/kybra_generate/src/kybra_modules_init/mod.rs @@ -68,16 +68,19 @@ pub fn generate( static INSTALLER_PRINCIPAL_REF_CELL: std::cell::RefCell> = std::cell::RefCell::new(None); } + // TODO I think I can just check who the controllers are here, that's probably better actually // TODO make sure to actually manually test this authorization // TODO we might want an automated test for this as well + // TODO this just got more difficult, as we need to figure out the installing identity + // TODO as separate from the calling identity now, since the canister itself now installs fn authorize_python_module_init() { - INSTALLER_PRINCIPAL_REF_CELL.with(|installer_principal_ref_cell| { - let installer_principal = installer_principal_ref_cell.borrow().expect("Installer Principal must be set"); + // INSTALLER_PRINCIPAL_REF_CELL.with(|installer_principal_ref_cell| { + // let installer_principal = installer_principal_ref_cell.borrow().expect("Installer Principal must be set"); - if ic_cdk::api::caller().to_text() != installer_principal.to_text() { - panic!("Not authorized to initialize Python modules"); - } - }); + // if ic_cdk::api::caller().to_text() != installer_principal.to_text() { + // panic!("Not authorized to initialize Python modules"); + // } + // }); } #[ic_cdk_macros::update] diff --git a/kybra/compiler/kybra_modules_init/src/Lib/uuid.py b/kybra/compiler/kybra_modules_init/src/Lib/uuid.py index 5ae0a3e5f..e4298253c 100644 --- a/kybra/compiler/kybra_modules_init/src/Lib/uuid.py +++ b/kybra/compiler/kybra_modules_init/src/Lib/uuid.py @@ -55,6 +55,8 @@ # The recognized platforms - known behaviors if sys.platform in ('win32', 'darwin'): _AIX = _LINUX = False +elif sys.platform in ('emscripten', 'wasi'): # XXX: RUSTPYTHON; patched to support those platforms + _AIX = _LINUX = False else: import platform _platform_system = platform.system() diff --git a/kybra/compiler/kybra_modules_init/src/main.rs b/kybra/compiler/kybra_modules_init/src/main.rs index b142ab6fd..a08ce0690 100644 --- a/kybra/compiler/kybra_modules_init/src/main.rs +++ b/kybra/compiler/kybra_modules_init/src/main.rs @@ -2,6 +2,7 @@ use std::io::Write; use std::process::Command; use tempfile::NamedTempFile; +// TODO share with DFINITY how I'm doing this, maybe they can build that into dfx to overcome some of the Wasm binary limitations temporarily fn main() { let args: Vec = std::env::args().collect(); @@ -9,6 +10,74 @@ fn main() { let max_chunk_size = 2 * 1_000 * 1_000; // 2 MB + match std::env::current_dir() { + Ok(current_dir) => println!("Current working directory: {}", current_dir.display()), + Err(e) => println!("Error getting current directory: {}", e), + }; + + let wasm = std::fs::read(format!(".kybra/{canister_name}/{canister_name}_final.wasm")).unwrap(); + let wasm_chunks = split_into_chunks(wasm, max_chunk_size); + + for wasm_chunk in wasm_chunks { + upload_chunk(canister_name, wasm_chunk, "upload_wasm_chunk"); + } + + let canister_id_output = Command::new("dfx") + .arg("canister") + .arg("id") + .arg(canister_name) + .output() + .expect("Failed to execute the dfx canister id command"); + + let canister_id = String::from_utf8_lossy(&canister_id_output.stdout) + .trim() + .to_string(); + + // TODO we shouldn't just leave this... + // TODO we should check if it is already the controller + // TODO if it is, don't remove it. If it isn't add it and then remove it + let add_controller_output = Command::new("dfx") + .arg("canister") + .arg("update-settings") + .arg("--add-controller") + .arg(canister_id) + .arg(canister_name) + .output() + .expect("Failed to execute the dfx command"); + + if add_controller_output.status.success() { + println!( + "Output: {:?}", + String::from_utf8_lossy(&add_controller_output.stdout) + ); + } else { + eprintln!( + "Error: {:?}", + String::from_utf8_lossy(&add_controller_output.stderr) + ); + } + + let install_output = Command::new("dfx") + .arg("canister") + .arg("call") + .arg(canister_name) + .arg("install_wasm") + .output() + .expect("Failed to execute the dfx command"); + + // TODO this will error out until we use notify + if install_output.status.success() { + println!( + "Output: {:?}", + String::from_utf8_lossy(&install_output.stdout) + ); + } else { + eprintln!( + "Error: {:?}", + String::from_utf8_lossy(&install_output.stderr) + ); + } + let python_source_modules = rustpython_vm::py_freeze!(dir = "../python_source"); let python_source_bytecode = python_source_modules.bytes.to_vec(); let python_source_bytecode_chunks = split_into_chunks(python_source_bytecode, max_chunk_size); @@ -36,7 +105,7 @@ fn main() { // Upload Python source bytecode for python_source_bytecode_chunk in python_source_bytecode_chunks { - upload_python_bytecode_chunk( + upload_chunk( canister_name, python_source_bytecode_chunk, "upload_python_source_bytecode_chunk", @@ -45,7 +114,7 @@ fn main() { // Upload Python stdlib bytecode for python_stdlib_bytecode_chunk in python_stdlib_bytecode_chunks { - upload_python_bytecode_chunk( + upload_chunk( canister_name, python_stdlib_bytecode_chunk, "upload_python_stdlib_bytecode_chunk", @@ -73,11 +142,7 @@ fn main() { } } -fn upload_python_bytecode_chunk( - canister_name: &str, - bytecode_chunk: Vec, - canister_method_name: &str, -) { +fn upload_chunk(canister_name: &str, bytecode_chunk: Vec, canister_method_name: &str) { let blob_string = vec_u8_to_blob_string(&bytecode_chunk); let mut temp_file = NamedTempFile::new().expect("Failed to create temporary file");