From 5437fe27710d8209640d7ae4b243cede0e653e91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pascal=20Eng=C3=A9libert?= Date: Sat, 1 Apr 2023 16:02:42 +0200 Subject: [PATCH] Better API structure, update deps --- Cargo.lock | 199 ++++++++++++++++++----------- README.md | 28 +--- common/Cargo.toml | 2 +- common/src/ai.rs | 5 - common/src/api.rs | 23 ++-- common/src/board.rs | 9 +- common/src/entities.rs | 16 ++- common/src/entities/ferris.rs | 28 +++- common/src/lib.rs | 26 +--- common/src/sim.rs | 41 ++++-- executor/Cargo.toml | 8 +- executor/src/api.rs | 24 ++-- executor/src/main.rs | 89 +++---------- runtime-interface-macro/Cargo.toml | 2 +- runtime-interface/src/lib.rs | 2 +- runtime/Cargo.toml | 2 +- runtime/src/lib.rs | 18 +-- 17 files changed, 266 insertions(+), 256 deletions(-) delete mode 100644 common/src/ai.rs diff --git a/Cargo.lock b/Cargo.lock index f05cdbd..2ee960a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -143,6 +143,17 @@ dependencies = [ "digest 0.10.6", ] +[[package]] +name = "blake2b_simd" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c2f0dc9a68c6317d884f97cc36cf5a3d20ba14ce404227df55e1af708ab04bc" +dependencies = [ + "arrayref", + "arrayvec 0.7.2", + "constant_time_eq", +] + [[package]] name = "block-buffer" version = "0.7.3" @@ -161,7 +172,7 @@ version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4152116fd6e9dadb291ae18fc1ec3575ed6d84c29642d97890f4b4a3417297e4" dependencies = [ - "generic-array 0.14.6", + "generic-array 0.14.7", ] [[package]] @@ -170,7 +181,7 @@ version = "0.10.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" dependencies = [ - "generic-array 0.14.6", + "generic-array 0.14.7", ] [[package]] @@ -267,6 +278,12 @@ dependencies = [ "unicode-width", ] +[[package]] +name = "constant_time_eq" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "13418e745008f7349ec7e449155f419a61b92b58a99cc3616942b926825ec76b" + [[package]] name = "core-foundation-sys" version = "0.8.3" @@ -454,7 +471,7 @@ version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" dependencies = [ - "generic-array 0.14.6", + "generic-array 0.14.7", "typenum", ] @@ -464,7 +481,7 @@ version = "0.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b1d1a86f49236c215f271d40892d5fc950490551400b02ef360692c29815c714" dependencies = [ - "generic-array 0.14.6", + "generic-array 0.14.7", "subtle", ] @@ -542,7 +559,7 @@ dependencies = [ "proc-macro2", "quote", "scratch", - "syn 2.0.10", + "syn 2.0.12", ] [[package]] @@ -559,7 +576,7 @@ checksum = "2345488264226bf682893e25de0769f3360aac9957980ec49361b083ddaa5bc5" dependencies = [ "proc-macro2", "quote", - "syn 2.0.10", + "syn 2.0.12", ] [[package]] @@ -601,7 +618,7 @@ version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d3dd60d1080a57a05ab032377049e0591415d2b31afd7028356dbf3cc6dcb066" dependencies = [ - "generic-array 0.14.6", + "generic-array 0.14.7", ] [[package]] @@ -719,6 +736,17 @@ dependencies = [ "winapi", ] +[[package]] +name = "errno" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50d6a0976c999d473fe89ad888d5a284e55366d9dc9038b1ba2aa15128c4afa0" +dependencies = [ + "errno-dragonfly", + "libc", + "windows-sys 0.45.0", +] + [[package]] name = "errno-dragonfly" version = "0.1.2" @@ -795,9 +823,9 @@ checksum = "e6d5a32815ae3f33302d95fdcb2ce17862f8c65363dcfd29360480ba1001fc9c" [[package]] name = "futures" -version = "0.3.27" +version = "0.3.28" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "531ac96c6ff5fd7c62263c5e3c67a603af4fcaee2e1a0ae5565ba3a11e69e549" +checksum = "23342abe12aba583913b2e62f22225ff9c950774065e4bfb61a19cd9770fec40" dependencies = [ "futures-channel", "futures-core", @@ -810,9 +838,9 @@ dependencies = [ [[package]] name = "futures-channel" -version = "0.3.27" +version = "0.3.28" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "164713a5a0dcc3e7b4b1ed7d3b433cabc18025386f9339346e8daf15963cf7ac" +checksum = "955518d47e09b25bbebc7a18df10b81f0c766eaf4c4f1cccef2fca5f2a4fb5f2" dependencies = [ "futures-core", "futures-sink", @@ -820,15 +848,15 @@ dependencies = [ [[package]] name = "futures-core" -version = "0.3.27" +version = "0.3.28" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "86d7a0c1aa76363dac491de0ee99faf6941128376f1cf96f07db7603b7de69dd" +checksum = "4bca583b7e26f571124fe5b7561d49cb2868d79116cfa0eefce955557c6fee8c" [[package]] name = "futures-executor" -version = "0.3.27" +version = "0.3.28" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1997dd9df74cdac935c76252744c1ed5794fac083242ea4fe77ef3ed60ba0f83" +checksum = "ccecee823288125bd88b4d7f565c9e58e41858e47ab72e8ea2d64e93624386e0" dependencies = [ "futures-core", "futures-task", @@ -838,38 +866,38 @@ dependencies = [ [[package]] name = "futures-io" -version = "0.3.27" +version = "0.3.28" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "89d422fa3cbe3b40dca574ab087abb5bc98258ea57eea3fd6f1fa7162c778b91" +checksum = "4fff74096e71ed47f8e023204cfd0aa1289cd54ae5430a9523be060cdb849964" [[package]] name = "futures-macro" -version = "0.3.27" +version = "0.3.28" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3eb14ed937631bd8b8b8977f2c198443447a8355b6e3ca599f38c975e5a963b6" +checksum = "89ca545a94061b6365f2c7355b4b32bd20df3ff95f02da9329b34ccc3bd6ee72" dependencies = [ "proc-macro2", "quote", - "syn 1.0.109", + "syn 2.0.12", ] [[package]] name = "futures-sink" -version = "0.3.27" +version = "0.3.28" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ec93083a4aecafb2a80a885c9de1f0ccae9dbd32c2bb54b0c3a65690e0b8d2f2" +checksum = "f43be4fe21a13b9781a69afa4985b0f6ee0e1afab2c6f454a8cf30e2b2237b6e" [[package]] name = "futures-task" -version = "0.3.27" +version = "0.3.28" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fd65540d33b37b16542a0438c12e6aeead10d4ac5d05bd3f805b8f35ab592879" +checksum = "76d3d132be6c0e6aa1534069c705a74a5997a356c0dc2f86a47765e5617c5b65" [[package]] name = "futures-util" -version = "0.3.27" +version = "0.3.28" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3ef6b17e481503ec85211fed8f39d1970f128935ca1f814cd32ac4a6842e84ab" +checksum = "26b01e40b772d54cf6c6d721c1d1abd0647a0106a12ecaa1c186273392a69533" dependencies = [ "futures-channel", "futures-core", @@ -903,9 +931,9 @@ dependencies = [ [[package]] name = "generic-array" -version = "0.14.6" +version = "0.14.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bff49e947297f3312447abdca79f45f4738097cc82b06e72054d2223f601f1b9" +checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" dependencies = [ "typenum", "version_check", @@ -946,9 +974,9 @@ dependencies = [ [[package]] name = "hash-db" -version = "0.15.2" +version = "0.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d23bd4e7b5eda0d0f3a307e8b381fdc8ba9000f26fbe912250c0a4cc3956364a" +checksum = "8e7d7786361d7425ae2fe4f9e407eb0efaa0840f5212d109cc018c40c35c6ab4" [[package]] name = "hash256-std-hasher" @@ -1101,13 +1129,13 @@ dependencies = [ [[package]] name = "is-terminal" -version = "0.4.5" +version = "0.4.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8687c819457e979cc940d09cb16e42a1bf70aa6b60a549de6d3a62a0ee90c69e" +checksum = "256017f749ab3117e93acb91063009e1f1bb56d03965b14c2c8df4eb02c524d8" dependencies = [ "hermit-abi 0.3.1", "io-lifetimes", - "rustix", + "rustix 0.37.6", "windows-sys 0.45.0", ] @@ -1232,6 +1260,12 @@ version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f051f77a7c8e6957c0696eac88f26b0117e54f52d3fc682ab19397a8812846a4" +[[package]] +name = "linux-raw-sys" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d59d8c75012853d2e872fb56bc8a2e53718e2cafe1a4c823143141c6d90c322f" + [[package]] name = "lock_api" version = "0.4.9" @@ -1277,11 +1311,11 @@ checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" [[package]] name = "memfd" -version = "0.6.2" +version = "0.6.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b20a59d985586e4a5aef64564ac77299f8586d8be6cf9106a5a40207e8908efb" +checksum = "ffc89ccdc6e10d6907450f753537ebc5c5d3460d2e4e62ea74bd571db62c0f9e" dependencies = [ - "rustix", + "rustix 0.37.6", ] [[package]] @@ -1708,7 +1742,7 @@ checksum = "8d2275aab483050ab2a7364c1a46604865ee7d6906684e08db0f090acf74f9e7" dependencies = [ "proc-macro2", "quote", - "syn 2.0.10", + "syn 2.0.12", ] [[package]] @@ -1754,7 +1788,6 @@ name = "runtime" version = "0.1.0" dependencies = [ "cultivar-common", - "cultivar-runtime-interface", "parity-scale-codec", "wee_alloc", ] @@ -1784,10 +1817,24 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "db4165c9963ab29e422d6c26fbc1d37f15bace6b2810221f9d925023480fcf0e" dependencies = [ "bitflags", - "errno", + "errno 0.2.8", "io-lifetimes", "libc", - "linux-raw-sys", + "linux-raw-sys 0.1.4", + "windows-sys 0.45.0", +] + +[[package]] +name = "rustix" +version = "0.37.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d097081ed288dfe45699b72f5b5d648e5f15d64d900c7080273baa20c16a6849" +dependencies = [ + "bitflags", + "errno 0.3.0", + "io-lifetimes", + "libc", + "linux-raw-sys 0.3.1", "windows-sys 0.45.0", ] @@ -1799,9 +1846,9 @@ checksum = "f91339c0467de62360649f8d3e185ca8de4224ff281f66000de5eb2a77a79041" [[package]] name = "sc-allocator" -version = "14.0.0" +version = "15.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5aa8784b53aa48736a4df4c351162a63b17e7c28c77b6a2e92dfb9bc49709107" +checksum = "55d800c7dae2e3f85f2e9dabc8d34a1ca90dd4060a99bd7eac7ad6a72970de3f" dependencies = [ "log", "sp-core", @@ -1811,9 +1858,9 @@ dependencies = [ [[package]] name = "sc-executor-common" -version = "0.20.0" +version = "0.21.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0eff0b3bb1d41ca34481215297090496c35e91c6d66a71e3a26960c8fce917ea" +checksum = "4ee5b6752bb44c88e42582ae7c888932c424873c901533bb890491899df88c2d" dependencies = [ "sc-allocator", "sp-maybe-compressed-blob", @@ -1825,16 +1872,16 @@ dependencies = [ [[package]] name = "sc-executor-wasmtime" -version = "0.20.0" +version = "0.21.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7df03fdc79767bbc993cfcde07a8a09040e60fed1ca7341e3d17fc4c461e8457" +checksum = "7c021d59d365f2728e31e8340370b1a1a4cef0f1827a3484cd491c5595d0daf2" dependencies = [ "anyhow", "cfg-if 1.0.0", "libc", "log", "once_cell", - "rustix", + "rustix 0.36.11", "sc-allocator", "sc-executor-common", "sp-runtime-interface", @@ -1844,9 +1891,9 @@ dependencies = [ [[package]] name = "scale-info" -version = "2.4.0" +version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "61471dff9096de1d8b2319efed7162081e96793f5ebb147e50db10d50d648a4d" +checksum = "0cfdffd972d76b22f3d7f81c8be34b2296afd3a25e0a547bd9abe340a4dbbe97" dependencies = [ "bitvec", "cfg-if 1.0.0", @@ -1857,9 +1904,9 @@ dependencies = [ [[package]] name = "scale-info-derive" -version = "2.4.0" +version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "219580e803a66b3f05761fd06f1f879a872444e49ce23f73694d26e5a954c7e6" +checksum = "61fa974aea2d63dd18a4ec3a49d59af9f34178c73a4f56d2f18205628d00681e" dependencies = [ "proc-macro-crate", "proc-macro2", @@ -1926,29 +1973,29 @@ dependencies = [ [[package]] name = "serde" -version = "1.0.158" +version = "1.0.159" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "771d4d9c4163ee138805e12c710dd365e4f44be8be0503cb1bb9eb989425d9c9" +checksum = "3c04e8343c3daeec41f58990b9d77068df31209f2af111e059e9fe9646693065" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.158" +version = "1.0.159" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e801c1712f48475582b7696ac71e0ca34ebb30e09338425384269d9717c62cad" +checksum = "4c614d17805b093df4b147b51339e7e44bf05ef59fba1e45d83500bcfb4d8585" dependencies = [ "proc-macro2", "quote", - "syn 2.0.10", + "syn 2.0.12", ] [[package]] name = "serde_json" -version = "1.0.94" +version = "1.0.95" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1c533a59c9d8a93a09c6ab31f0fd5e5f4dd1b8fc9434804029839884765d04ea" +checksum = "d721eca97ac802aa7777b701877c8004d950fc142651367300d21c1cc0194744" dependencies = [ "itoa", "ryu", @@ -2033,9 +2080,9 @@ checksum = "a507befe795404456341dfab10cef66ead4c041f62b8b11bbb92bffe5d0953e0" [[package]] name = "sp-core" -version = "18.0.0" +version = "19.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4ea27a1d8de728306d17502ba13127a1b1149c66e0ef348f67dafad630b50c1d" +checksum = "0fb48120830bdc61a014827dbb10b6b3a6040f619e2e6aacdf57d3bac381ab06" dependencies = [ "array-bytes", "base58", @@ -2077,11 +2124,11 @@ dependencies = [ [[package]] name = "sp-core-hashing" -version = "7.0.0" +version = "8.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d607f7209b1b9571177fc3722a03312df03606bb65f89317ba686d5fa59d438f" +checksum = "27449abdfbe41b473e625bce8113745e81d65777dd1d5a8462cf24137930dad8" dependencies = [ - "blake2", + "blake2b_simd", "byteorder", "digest 0.10.6", "sha2 0.10.6", @@ -2125,9 +2172,9 @@ dependencies = [ [[package]] name = "sp-runtime-interface" -version = "15.0.0" +version = "16.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2446ea08a1ae6dac4218b26e01c7aad6dbf47eb506f4f2b1efa821aa418a07d2" +checksum = "ca5d0cd80200bf85b8b064238b2508b69b6146b13adf36066ec5d924825af737" dependencies = [ "bytes", "impl-trait-for-tuples", @@ -2190,9 +2237,9 @@ dependencies = [ [[package]] name = "sp-wasm-interface" -version = "12.0.0" +version = "13.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "510bdd9ade55508e5aa05b99ab79aaa4b74a1f7476351b6ce0f3aab3b1cb2524" +checksum = "153b7374179439e2aa783c66ed439bd86920c67bbc95d34c76390561972bc02f" dependencies = [ "anyhow", "impl-trait-for-tuples", @@ -2262,9 +2309,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.10" +version = "2.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5aad1363ed6d37b84299588d62d3a7d95b5a5c2d9aad5c85609fda12afaa1f40" +checksum = "79d9531f94112cfc3e4c8f5f02cb2b58f72c97b7efd85f70203cc6d8efda5927" dependencies = [ "proc-macro2", "quote", @@ -2309,7 +2356,7 @@ checksum = "f9456a42c5b0d803c8cd86e73dd7cc9edd429499f37a3550d286d5e86720569f" dependencies = [ "proc-macro2", "quote", - "syn 2.0.10", + "syn 2.0.12", ] [[package]] @@ -2699,7 +2746,7 @@ dependencies = [ "directories-next", "file-per-thread-logger", "log", - "rustix", + "rustix 0.36.11", "serde", "sha2 0.10.6", "toml", @@ -2779,7 +2826,7 @@ checksum = "d0245e8a9347017c7185a72e215218a802ff561545c242953c11ba00fccc930f" dependencies = [ "object", "once_cell", - "rustix", + "rustix 0.36.11", ] [[package]] @@ -2810,7 +2857,7 @@ dependencies = [ "memoffset 0.6.5", "paste", "rand 0.8.5", - "rustix", + "rustix 0.36.11", "wasmtime-asm-macros", "wasmtime-environ", "wasmtime-jit-debug", @@ -2991,13 +3038,13 @@ dependencies = [ [[package]] name = "zeroize_derive" -version = "1.4.0" +version = "1.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "57090580b8b26d9fd2288c4ac982b3b3c6446cd8e91112bcf672d5ddb2f17441" +checksum = "ce36e65b0d2999d2aafac989fb249189a141aee1f53c612c1f37d72631959f69" dependencies = [ "proc-macro2", "quote", - "syn 2.0.10", + "syn 2.0.12", ] [[package]] diff --git a/README.md b/README.md index 61e906f..45c2310 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,9 @@ +# Cultivar + +**Early development**: this is not even a prototype yet. R&D ongoing ;) + +Game where you program the behaviour of the entities. Player's program is executed in a WASM environment. + ## Idées Idées pour un jeu comme [LeekWars](https://leekwars.com) : @@ -6,28 +12,6 @@ Idées pour un jeu comme [LeekWars](https://leekwars.com) : * Ferris * autres mascottes de langages -Nom : Cultivar ou Cultivars - -## Problèmes - -* passer des objets contenant des références ou pointeurs -* sandbox sûre -* limitation de mémoire et de temps d'exécution (watchdog ?) -* isoler les instances d'IA -* comment exposer des fonctions de l'hôte avec sc-executor ? - * ne pas exposer de fonctions, seulement tout donner en argument et récupérer une liste d'actions ensuite - mais c'est gênant pour les fonctions non-déterministes du point de vue du WASM, et il faut que le WASM maintienne l'état du jeu à jour -* comment le WASM peut-il appeler des méthodes sur une struct de l'hôte ? - * forker sc-executor-wasmtime pour que `create_runtime` prenne `&mut impl HostFunctions` et non juste son type. Mais alors si on veut exposer des méthodes `&self` il faut le refléter dans `#[runtime_interface]` or le WASM ne connaît pas le pointeur vers le `HostFunctions`. - * Un wrapper de `HostFunctions` ? - * créer une variante de `#[runtime_interface]` qui ajoute qqchose comme `&mut Context` dans les fonctions côté hôte - -* https://github.com/demille/wasm-ffi -* https://demille.github.io/wasm-ffi/docs/rust/ -* http://jakegoulding.com/rust-ffi-omnibus/ -* refaire wasm-bindgen sans JS ? -* sp-runtime-interface mais virer les trucs inutiles - ## Build ### Runtime (AI) diff --git a/common/Cargo.toml b/common/Cargo.toml index 6b870ba..655e7c8 100644 --- a/common/Cargo.toml +++ b/common/Cargo.toml @@ -6,7 +6,7 @@ edition = "2021" [dependencies] num-traits = { version = "0.2.15", default-features = false } parity-scale-codec = "3.4.0" -sp-runtime-interface = { version = "15.0.0", default-features = false } +sp-runtime-interface = { version = "16.0.0", default-features = false } [features] std = ["sp-runtime-interface/std"] diff --git a/common/src/ai.rs b/common/src/ai.rs deleted file mode 100644 index 6bf1bac..0000000 --- a/common/src/ai.rs +++ /dev/null @@ -1,5 +0,0 @@ -use crate::board::*; - -pub trait Ai { - fn run(board: &mut Board); -} diff --git a/common/src/api.rs b/common/src/api.rs index 5a06900..aa07487 100644 --- a/common/src/api.rs +++ b/common/src/api.rs @@ -1,17 +1,16 @@ +use crate::prelude::*; use sp_runtime_interface::runtime_interface; #[runtime_interface] pub trait Api { - /*fn print_hello(nb: u32) -> u32 { - println!("hello {nb}"); - nb + 42 + fn say(msg: &str) { + unreachable!() } - - fn walk(sim_id: SimId, entity_id: EntityId, direction: Direction) { - if let Some(sim) = sims.read().get_mut(&sim_id) { - if let Some(entity) = sim.entities.get_mut(&entity_id) { - entity.walk(direction) - } - } - }*/ -} \ No newline at end of file + fn walk( + sim_id: SimId, + entity_id: EntityId, + direction: Direction, + ) -> Result, SimError> { + unreachable!() + } +} diff --git a/common/src/board.rs b/common/src/board.rs index 9c06227..7ad3329 100644 --- a/common/src/board.rs +++ b/common/src/board.rs @@ -1,12 +1,15 @@ use num_traits::{One, Signed, Zero}; -use parity_scale_codec::{Encode, Decode}; +use parity_scale_codec::{Decode, Encode}; + +pub type Position = (i32, i32); #[derive(Debug, Default)] pub struct Board { - pub size: (usize, usize), + pub origin: (i32, i32), + pub size: (i32, i32), } -pub trait Position { +pub trait HasPosition { fn get_position(&self) -> (i32, i32); } diff --git a/common/src/entities.rs b/common/src/entities.rs index 11ba2b7..d3f8741 100644 --- a/common/src/entities.rs +++ b/common/src/entities.rs @@ -1,7 +1,9 @@ +pub mod ferris; + use crate::prelude::*; use crate::std_bis::*; -pub mod ferris; +use parity_scale_codec::{Decode, Encode}; pub mod traits { use super::*; @@ -9,14 +11,24 @@ pub mod traits { pub trait Entity {} pub trait Walker { - fn walk(&mut self, board: &Board, direction: Direction); + fn walk( + &mut self, + board: &Board, + direction: Direction, + ) -> Result; } } +#[derive(Debug)] pub enum Entity { Ferris(Box), } +#[derive(Clone, Debug, Decode, Encode, Eq, PartialEq)] pub enum WalkError { OutOfBoard, } + +impl sp_runtime_interface::pass_by::PassBy for WalkError { + type PassBy = sp_runtime_interface::pass_by::Codec; +} diff --git a/common/src/entities/ferris.rs b/common/src/entities/ferris.rs index 74c2b25..d9dd016 100644 --- a/common/src/entities/ferris.rs +++ b/common/src/entities/ferris.rs @@ -1,22 +1,38 @@ -use super::traits::*; +use super::{traits::*, WalkError}; use crate::board::*; +#[derive(Clone, Debug)] pub struct Ferris { - position: (i32, i32), + pub position: (i32, i32), } impl Entity for Ferris {} -impl Position for Ferris { +impl HasPosition for Ferris { fn get_position(&self) -> (i32, i32) { self.position } } impl Walker for Ferris { - fn walk(&mut self, board: &Board, direction: Direction) { + fn walk( + &mut self, + board: &Board, + direction: Direction, + ) -> Result { let dpos = direction.to_position::(); - self.position.0 += dpos.0; - self.position.1 += dpos.1; + let new_x = self.position.0.saturating_add(dpos.0); + let new_y = self.position.1.saturating_add(dpos.1); + if new_x >= board.origin.0 + && new_x < board.origin.0 + board.size.0 + && new_y >= board.origin.1 + && new_y < board.origin.1 + board.size.1 + { + self.position.0 = new_x; + self.position.1 = new_y; + Ok(self.position) + } else { + Err((self.position, WalkError::OutOfBoard)) + } } } diff --git a/common/src/lib.rs b/common/src/lib.rs index bd8d7db..7ed34fc 100644 --- a/common/src/lib.rs +++ b/common/src/lib.rs @@ -1,6 +1,5 @@ #![cfg_attr(not(feature = "std"), no_std)] -pub mod ai; #[cfg(not(feature = "std"))] pub mod api; pub mod board; @@ -14,35 +13,24 @@ use std_bis::*; pub mod std_bis { #[cfg(not(feature = "std"))] - pub use alloc::{boxed::Box, vec, vec::Vec}; + pub use alloc::{boxed::Box, format, vec, vec::Vec}; } #[cfg(feature = "std")] pub mod prelude { pub use crate::{ - ai::Ai, - //api::Api, - board::{Board, Direction}, - entities::{WalkError, Entity, traits::Walker}, - sim::{Sim, SimId, EntityId}, + board::{Board, Direction, Position}, + entities::{ferris::Ferris, traits::Walker, Entity, WalkError}, + sim::{EntityId, Sim, SimError, SimId}, }; } #[cfg(not(feature = "std"))] pub mod prelude { pub use crate::{ - ai::Ai, - //api::Api, - board::{Board, Direction}, + api::api::*, + board::{Board, Direction, Position}, entities::WalkError, - sim::{SimId, EntityId}, + sim::{EntityId, SimError, SimId}, }; } - -/*#[runtime_interface] -pub trait Api { - fn foo(a: i32) -> i32 { - 1.0f32.cos(); - a.wrapping_add(42) - } -}*/ diff --git a/common/src/sim.rs b/common/src/sim.rs index 0909876..593f550 100644 --- a/common/src/sim.rs +++ b/common/src/sim.rs @@ -8,20 +8,45 @@ use std::collections::BTreeMap; #[derive(Clone, Copy, Debug, Decode, Encode, Eq, Hash, Ord, PartialEq, PartialOrd)] pub struct SimId(pub [u8; 16]); -#[derive(Clone, Copy, Debug, Decode, Encode, Eq, Hash, Ord, PartialEq, PartialOrd)] -pub struct EntityId(pub u32); - impl sp_runtime_interface::pass_by::PassBy for SimId { type PassBy = sp_runtime_interface::pass_by::Codec; } +#[derive(Clone, Copy, Debug, Decode, Encode, Eq, Hash, Ord, PartialEq, PartialOrd)] +pub struct EntityId(pub u32); + impl sp_runtime_interface::pass_by::PassBy for EntityId { type PassBy = sp_runtime_interface::pass_by::Codec; } -#[cfg(feature = "std")] -pub struct Sim { - pub board: board::Board, - pub entities: BTreeMap, - pub entity_counter: u32, +#[derive(Clone, Debug, Decode, Encode, Eq, PartialEq)] +pub enum SimError { + EntityNotFound, + SimNotFound, +} + +impl sp_runtime_interface::pass_by::PassBy for SimError { + type PassBy = sp_runtime_interface::pass_by::Codec; +} + +#[cfg(feature = "std")] +pub use sim::*; + +#[cfg(feature = "std")] +mod sim { + use super::*; + + #[derive(Debug)] + pub struct Sim { + pub board: board::Board, + pub entities: BTreeMap, + pub entity_counter: u32, + } + + impl Sim { + pub fn add_entity(&mut self, entity: entities::Entity) { + self.entities.insert(EntityId(self.entity_counter), entity); + self.entity_counter += 1; + } + } } diff --git a/executor/Cargo.toml b/executor/Cargo.toml index abf9fda..86f5055 100644 --- a/executor/Cargo.toml +++ b/executor/Cargo.toml @@ -9,10 +9,10 @@ cultivar-common = { path = "../common", features = ["std"] } once_cell = "1.17.1" parity-scale-codec = "3.4.0" parking_lot = "0.12.1" -sc-executor-common = "0.20.0" -sc-executor-wasmtime = "0.20.0" -sp-runtime-interface = { version = "15.0.0", default-features = false, features = ["std"] } -sp-wasm-interface = "12.0.0" +sc-executor-common = "0.21.0" +sc-executor-wasmtime = "0.21.0" +sp-runtime-interface = { version = "16.0.0", default-features = false, features = ["std"] } +sp-wasm-interface = "13.0.0" [features] default = ["std"] diff --git a/executor/src/api.rs b/executor/src/api.rs index ad8a239..436234e 100644 --- a/executor/src/api.rs +++ b/executor/src/api.rs @@ -1,23 +1,31 @@ -use crate::{sims}; +use crate::SIMS; use cultivar_common::prelude::*; use sp_runtime_interface::runtime_interface; +#[allow(dead_code)] #[runtime_interface] pub trait Api { - fn print_hello(nb: u32) -> u32 { - println!("hello {nb}"); - nb + 42 + fn say(msg: &str) { + println!("{msg}"); } - fn walk(sim_id: SimId, entity_id: EntityId, direction: Direction) { - if let Some(mut sim) = (*sims.write()).sims.get_mut(&sim_id) { + fn walk( + sim_id: SimId, + entity_id: EntityId, + direction: Direction, + ) -> Result, SimError> { + if let Some(mut sim) = SIMS.write().sims.get_mut(&sim_id) { let sim = sim.value_mut(); if let Some(entity) = sim.entities.get_mut(&entity_id) { match entity { - Entity::Ferris(entity) => entity.walk(&sim.board, direction) + Entity::Ferris(entity) => Ok(entity.walk(&sim.board, direction)), } + } else { + Err(SimError::EntityNotFound) } + } else { + Err(SimError::SimNotFound) } } -} \ No newline at end of file +} diff --git a/executor/src/main.rs b/executor/src/main.rs index 85be505..5e1ce20 100644 --- a/executor/src/main.rs +++ b/executor/src/main.rs @@ -7,84 +7,25 @@ use once_cell::sync::Lazy; use parity_scale_codec::Encode; use parking_lot::RwLock; use sc_executor_common::wasm_runtime::WasmModule; -use sp_wasm_interface::{Function, FunctionContext, HostFunctions, Signature}; -use std::cell::Cell; -static sims: Lazy> = Lazy::new(|| RwLock::new(sim::Sims::new())); - -/*use sp_runtime_interface::runtime_interface; - -#[runtime_interface] -pub trait Api { - /*fn print_hello(nb: u32) -> u32 { - println!("hello {nb}"); - nb + 42 - } - - fn walk(sim_id: SimId, entity_id: EntityId, direction: Direction) { - if let Some(sim) = sims.read().get_mut(&sim_id) { - if let Some(entity) = sim.entities.get_mut(&entity_id) { - entity.walk(direction) - } - } - }*/ -}*/ - -/*struct FungiHostFunctions; - -impl HostFunctions for FungiHostFunctions { - fn host_functions() -> Vec<&'static dyn Function> { - vec![ - &FooFunction - ] - } - fn register_static(registry: &mut T) -> Result<(), T::Error> - where T: HostFunctionRegistry { - - } -} - -struct FooFunction; - -impl Function for FooFunction { - fn name(&self) -> &str { - "foo" - } - - fn signature(&self) -> Signature { - Signature { - args: std::borrow::Cow::from(&vec![]), - return_value: Some(sp_wasm_interface::ValueType::I32) - } - } - - fn execute(&self, context: &mut dyn FunctionContext,args: &mut dyn Iterator - ) -> sp_wasm_interface::Result> { - Ok(None) - } -} - -fn foo() -> u8 { - 44 -}*/ +static SIMS: Lazy> = Lazy::new(|| RwLock::new(sim::Sims::new())); fn main() { - let sim_id = { - sims.read().new_sim(Sim { - board: Board { - size: (4, 4) - }, - entities: Default::default(), - entity_counter: 0, - }) + let mut sim = Sim { + board: Board { + origin: (0, 0), + size: (4, 4), + }, + entities: Default::default(), + entity_counter: 0, }; - + sim.add_entity(Entity::Ferris(Box::new(Ferris { position: (0, 0) }))); + let sim_id = SIMS.read().new_sim(sim); + let ai_code = std::fs::read("target/wasm32-unknown-unknown/release/runtime.wasm") .expect("Cannot read runtime file"); - /*let runtime = sc_executor_wasmtime::create_runtime::< - api::api::ApiHostFunctions, - >( + let runtime = sc_executor_wasmtime::create_runtime::( sc_executor_common::runtime_blob::RuntimeBlob::new(&ai_code).unwrap(), sc_executor_wasmtime::Config { allow_missing_func_imports: false, @@ -108,9 +49,9 @@ fn main() { let mut instance = runtime.new_instance().unwrap(); - dbg!(instance - .call_export("run", &sim_id.encode()) - .unwrap());*/ + dbg!(instance.call_export("run", &sim_id.encode()).unwrap()); + + dbg!(SIMS.read().sims.remove(&sim_id)); /*let mut state = (); let mut env = sp_sandbox::EnvironmentDefinitionBuilder::new(); diff --git a/runtime-interface-macro/Cargo.toml b/runtime-interface-macro/Cargo.toml index 0450b9e..7fb7cac 100644 --- a/runtime-interface-macro/Cargo.toml +++ b/runtime-interface-macro/Cargo.toml @@ -7,5 +7,5 @@ edition = "2021" proc-macro = true [dependencies] -quote = "1.0.23" +quote = "1.0.26" syn = { version = "1.0.109", features = [ "full" ] } diff --git a/runtime-interface/src/lib.rs b/runtime-interface/src/lib.rs index f93e252..10f3b79 100644 --- a/runtime-interface/src/lib.rs +++ b/runtime-interface/src/lib.rs @@ -2,7 +2,7 @@ //#![feature(core_intrinsics)] //#![feature(alloc_error_handler)] -use fungi_common::prelude::*; +use cultivar_common::prelude::*; /*#[panic_handler] #[no_mangle] diff --git a/runtime/Cargo.toml b/runtime/Cargo.toml index 55f5f36..043bb70 100644 --- a/runtime/Cargo.toml +++ b/runtime/Cargo.toml @@ -13,6 +13,6 @@ crate-type = ["cdylib", "staticlib"] [dependencies] #dlmalloc = { version = "0.2.4", features = ["global"] } cultivar-common = { path = "../common" } -cultivar-runtime-interface = { path = "../runtime-interface" } +#cultivar-runtime-interface = { path = "../runtime-interface" } parity-scale-codec = "3.4.0" wee_alloc = "0.4.5" diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index fd30d53..8e966cc 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -1,30 +1,22 @@ #![feature(vec_into_raw_parts)] #![no_std] -use core::alloc::GlobalAlloc; -use fungi_common::{prelude::*, std_bis::*}; -use fungi_runtime_interface::prelude::*; +use cultivar_common::{prelude::*, std_bis::*}; +//use cultivar_runtime_interface::prelude::*; use parity_scale_codec::Decode; -//use fungi_runtime_interface::*; #[global_allocator] static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; //static ALLOC: dlmalloc::GlobalDlmalloc = dlmalloc::GlobalDlmalloc; -struct MyAi {} - -/*impl Ai for MyAi { - fn run(_board: &mut Board) {} -}*/ - /// # Safety /// The pointer given in argument must be valid, or undefined data will be read. #[export_name = "run"] pub unsafe fn run(ptr: *mut u8, len: usize) -> u64 { - let mut data = unsafe { Vec::from_raw_parts(ptr, len, len) }; + let data = unsafe { Vec::from_raw_parts(ptr, len, len) }; let sim_id = SimId::decode(&mut &data[..]).unwrap(); - + run_inner(sim_id); let (ptr, len, _) = data.into_raw_parts(); @@ -32,5 +24,5 @@ pub unsafe fn run(ptr: *mut u8, len: usize) -> u64 { } fn run_inner(sim_id: SimId) { - fungi_common::api::walk(sim_id, EntityId(0), Direction::East); + say(&format!("{:?}", walk(sim_id, EntityId(0), Direction::East))); }