From 9b6b8f89e25f605e686d43dc46d73c7fe49d3e1d Mon Sep 17 00:00:00 2001 From: Filippo Costa Date: Mon, 22 Nov 2021 14:03:45 +0100 Subject: [PATCH] Transition away from conflicting feature flags in favor of separate crates (#417) * Safer cursor * Create two separate svm-sdk crates * Remove ffi and mock features from svm-sdk-mock * Make svm-sdk and svm-sdk-mock two separate crates * Separate svm-sdk-storage * Separate host and storage in separate crates as well * Use default crate features for simplicity * Remove commented out dependencies * Remove some leftover dbg! calls --- Cargo.lock | 68 ++++++++++++++- crates/runtime-ffi/Cargo.toml | 2 +- crates/runtime-ffi/tests/api_tests.rs | 2 + .../runtime-ffi/tests/wasm/counter/Cargo.toml | 9 +- .../runtime-ffi/tests/wasm/counter/build.sh | 2 +- .../runtime-ffi/tests/wasm/failure/Cargo.toml | 9 +- .../runtime-ffi/tests/wasm/failure/build.sh | 2 +- crates/runtime/Cargo.toml | 2 +- crates/runtime/tests/runtime_tests.rs | 2 + .../wasm/calldata/CallDataTemplate-meta.json | 2 +- crates/runtime/tests/wasm/calldata/Cargo.lock | 73 +++++----------- crates/runtime/tests/wasm/calldata/Cargo.toml | 10 +-- crates/runtime/tests/wasm/calldata/build.sh | 2 +- .../runtime/tests/wasm/runtime_calldata.wasm | Bin 10805 -> 10817 bytes crates/sdk-host-ffi/Cargo.toml | 38 +++++++++ crates/{sdk-host => sdk-host-ffi}/src/ext.rs | 3 +- crates/sdk-host-ffi/src/lib.rs | 17 ++++ crates/sdk-host-mock/Cargo.toml | 39 +++++++++ .../src/mock.rs => sdk-host-mock/src/lib.rs} | 15 +++- crates/sdk-host/Cargo.toml | 26 ------ crates/sdk-host/src/lib.rs | 34 ++++---- crates/sdk-host/src/traits.rs | 21 ----- crates/sdk-macros/Cargo.toml | 8 +- crates/sdk-macros/src/function/endpoint.rs | 12 +-- crates/sdk-macros/src/function/fundable.rs | 6 +- .../sdk-macros/src/function/fundable_hook.rs | 4 +- crates/sdk-macros/src/function/mod.rs | 22 ++--- crates/sdk-macros/src/lib.rs | 16 +++- crates/sdk-macros/src/struct/mod.rs | 4 +- crates/sdk-macros/src/struct/storage.rs | 46 +++++----- crates/sdk-macros/src/template.rs | 34 ++++++-- crates/sdk-mock/Cargo.toml | 48 +++++++++++ crates/sdk-mock/src/lib.rs | 80 ++++++++++++++++++ crates/sdk-storage-ffi/Cargo.toml | 30 +++++++ .../src/ext.rs | 2 +- crates/sdk-storage-ffi/src/lib.rs | 17 ++++ crates/sdk-storage-mock/Cargo.toml | 30 +++++++ .../mock.rs => sdk-storage-mock/src/lib.rs} | 19 +++-- crates/sdk-storage/Cargo.toml | 16 ---- crates/sdk-storage/src/lib.rs | 21 +++-- crates/sdk-storage/src/traits.rs | 13 --- crates/sdk/Cargo.toml | 15 ++-- crates/sdk/src/lib.rs | 38 +-------- tests/sdk/Cargo.toml | 4 +- tests/sdk/src/lib.rs | 2 + tests/sdk/tests/alloc.rs | 4 +- tests/sdk/tests/alloc/alloc.rs | 2 +- tests/sdk/tests/ctor.rs | 6 +- tests/sdk/tests/ctor/bool_params.rs | 2 +- .../ctor_and_fundable_attrs_wrong_order.rs | 2 +- tests/sdk/tests/ctor/ctor_used_twice_fails.rs | 2 +- tests/sdk/tests/endpoint.rs | 6 +- tests/sdk/tests/endpoint/address_params.rs | 2 +- tests/sdk/tests/endpoint/amount_params.rs | 2 +- tests/sdk/tests/endpoint/bool_params.rs | 2 +- .../tests/endpoint/endpoint_and_ctor_fails.rs | 2 +- ...endpoint_and_fundable_attrs_wrong_order.rs | 2 +- .../endpoint/endpoint_used_twice_fails.rs | 2 +- .../endpoint/endpoint_with_fundable_hook.rs | 2 +- tests/sdk/tests/endpoint/integers_params.rs | 2 +- tests/sdk/tests/funding.rs | 6 +- .../fundable_hook_and_fundable_not_allowed.rs | 2 +- .../fundable_hook_func_with_args_fails.rs | 2 +- ...ndable_hook_func_with_return_type_fails.rs | 2 +- .../funding/fundable_hook_used_twice_fails.rs | 2 +- .../funding/fundable_used_twice_fails.rs | 2 +- tests/sdk/tests/funding/funding.rs | 4 +- tests/sdk/tests/meta.rs | 6 +- tests/sdk/tests/meta/ctor_fundable_meta.rs | 2 +- tests/sdk/tests/meta/ctor_meta.rs | 2 +- tests/sdk/tests/meta/ctor_with_doc.rs | 2 +- .../sdk/tests/meta/endpoint_fundable_meta.rs | 2 +- tests/sdk/tests/meta/endpoint_with_doc.rs | 2 +- .../meta/endpoint_with_params_array_meta.rs | 3 +- .../tests/meta/endpoint_with_params_meta.rs | 3 +- .../meta/endpoint_with_returns_path_meta.rs | 3 +- .../meta/endpoint_with_returns_tuple_meta.rs | 3 +- tests/sdk/tests/meta/storage_meta.rs | 2 +- tests/sdk/tests/storage.rs | 6 +- tests/sdk/tests/storage/address_field.rs | 2 +- tests/sdk/tests/storage/address_fqn_field.rs | 4 +- tests/sdk/tests/storage/amount_field.rs | 2 +- tests/sdk/tests/storage/amount_fqn_field.rs | 4 +- tests/sdk/tests/storage/bool_field.rs | 2 +- tests/sdk/tests/storage/i16_field.rs | 2 +- tests/sdk/tests/storage/i32_field.rs | 2 +- tests/sdk/tests/storage/i64_field.rs | 2 +- tests/sdk/tests/storage/i8_field.rs | 2 +- tests/sdk/tests/storage/invalid_field.rs | 2 +- tests/sdk/tests/storage/singleton.rs | 2 +- tests/sdk/tests/storage/u16_field.rs | 2 +- tests/sdk/tests/storage/u32_field.rs | 2 +- tests/sdk/tests/storage/u64_field.rs | 2 +- tests/sdk/tests/storage/u8_field.rs | 2 +- tests/sdk/tests/template.rs | 6 +- .../template/declaring_const_not_allowed.rs | 2 +- .../template/declaring_enum_not_allowed.rs | 2 +- .../template/declaring_static_not_allowed.rs | 2 +- .../template/declaring_traits_not_allowed.rs | 2 +- .../template/declaring_union_not_allowed.rs | 2 +- tests/sdk/tests/template/empty.rs | 2 +- ...h_two_default_fundable_hook_not_allowed.rs | 2 +- .../using_extern_crate_not_allowed.rs | 2 +- .../tests/template/using_ffi_not_allowed.rs | 2 +- .../tests/template/using_impl_not_allowed.rs | 2 +- .../template/using_macro_rules_not_allowed.rs | 2 +- 106 files changed, 636 insertions(+), 387 deletions(-) create mode 100644 crates/sdk-host-ffi/Cargo.toml rename crates/{sdk-host => sdk-host-ffi}/src/ext.rs (99%) create mode 100644 crates/sdk-host-ffi/src/lib.rs create mode 100644 crates/sdk-host-mock/Cargo.toml rename crates/{sdk-host/src/mock.rs => sdk-host-mock/src/lib.rs} (95%) delete mode 100644 crates/sdk-host/src/traits.rs create mode 100644 crates/sdk-mock/Cargo.toml create mode 100644 crates/sdk-mock/src/lib.rs create mode 100644 crates/sdk-storage-ffi/Cargo.toml rename crates/{sdk-storage => sdk-storage-ffi}/src/ext.rs (96%) create mode 100644 crates/sdk-storage-ffi/src/lib.rs create mode 100644 crates/sdk-storage-mock/Cargo.toml rename crates/{sdk-storage/src/mock.rs => sdk-storage-mock/src/lib.rs} (94%) delete mode 100644 crates/sdk-storage/src/traits.rs diff --git a/Cargo.lock b/Cargo.lock index 878db3215..36916eefc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1838,7 +1838,7 @@ dependencies = [ "svm-hash", "svm-layout", "svm-program", - "svm-sdk", + "svm-sdk-mock", "svm-state", "svm-types", "thiserror", @@ -1860,7 +1860,7 @@ dependencies = [ "svm-gas", "svm-layout", "svm-runtime", - "svm-sdk", + "svm-sdk-mock", "svm-state", "svm-types", "wasmer", @@ -1874,11 +1874,14 @@ dependencies = [ "lazy_static", "svm-abi-decoder", "svm-abi-encoder", + "svm-sdk", "svm-sdk-alloc", "svm-sdk-host", + "svm-sdk-host-ffi", "svm-sdk-macros", "svm-sdk-std", "svm-sdk-storage", + "svm-sdk-storage-ffi", "svm-sdk-types", ] @@ -1892,11 +1895,32 @@ dependencies = [ [[package]] name = "svm-sdk-host" version = "0.0.0" +dependencies = [ + "svm-sdk-types", +] + +[[package]] +name = "svm-sdk-host-ffi" +version = "0.0.0" +dependencies = [ + "lazy_static", + "svm-abi-decoder", + "svm-abi-encoder", + "svm-sdk-alloc", + "svm-sdk-host", + "svm-sdk-std", + "svm-sdk-types", +] + +[[package]] +name = "svm-sdk-host-mock" +version = "0.0.0" dependencies = [ "lazy_static", "svm-abi-decoder", "svm-abi-encoder", "svm-sdk-alloc", + "svm-sdk-host", "svm-sdk-std", "svm-sdk-types", ] @@ -1920,6 +1944,23 @@ dependencies = [ "trybuild", ] +[[package]] +name = "svm-sdk-mock" +version = "0.0.0" +dependencies = [ + "lazy_static", + "svm-abi-decoder", + "svm-abi-encoder", + "svm-sdk-alloc", + "svm-sdk-host", + "svm-sdk-host-mock", + "svm-sdk-macros", + "svm-sdk-std", + "svm-sdk-storage", + "svm-sdk-storage-mock", + "svm-sdk-types", +] + [[package]] name = "svm-sdk-std" version = "0.0.0" @@ -1931,10 +1972,31 @@ dependencies = [ [[package]] name = "svm-sdk-storage" version = "0.0.0" +dependencies = [ + "svm-sdk-alloc", + "svm-sdk-std", + "svm-sdk-types", +] + +[[package]] +name = "svm-sdk-storage-ffi" +version = "0.0.0" +dependencies = [ + "lazy_static", + "svm-sdk-alloc", + "svm-sdk-std", + "svm-sdk-storage", + "svm-sdk-types", +] + +[[package]] +name = "svm-sdk-storage-mock" +version = "0.0.0" dependencies = [ "lazy_static", "svm-sdk-alloc", "svm-sdk-std", + "svm-sdk-storage", "svm-sdk-types", ] @@ -1943,7 +2005,7 @@ name = "svm-sdk-tests" version = "0.0.0" dependencies = [ "serde_json", - "svm-sdk", + "svm-sdk-mock", "svm-sdk-types", "trybuild", ] diff --git a/crates/runtime-ffi/Cargo.toml b/crates/runtime-ffi/Cargo.toml index 8456cf6d5..1fe20977d 100644 --- a/crates/runtime-ffi/Cargo.toml +++ b/crates/runtime-ffi/Cargo.toml @@ -28,7 +28,7 @@ wasmer = { version = "2.0.0", default-features = false } wat = "1.0.40" serde_json = "1.0.69" maplit = "1.0.2" -svm-sdk = { path="../sdk", features = ["mock", "dynamic-alloc"], default-features=false } +svm-sdk-mock = { path="../sdk-mock", features = ["dynamic-alloc"], default-features=false } svm-abi-encoder = { path="../abi-encoder", features = ["dynamic-alloc"], default-features=false } svm-types = { path="../types", default-features=false } diff --git a/crates/runtime-ffi/tests/api_tests.rs b/crates/runtime-ffi/tests/api_tests.rs index 2cf6db8a3..cda2fb0c8 100644 --- a/crates/runtime-ffi/tests/api_tests.rs +++ b/crates/runtime-ffi/tests/api_tests.rs @@ -1,3 +1,5 @@ +extern crate svm_sdk_mock as svm_sdk; + use svm_codec::Codec; use svm_layout::FixedLayout; use svm_runtime::testing; diff --git a/crates/runtime-ffi/tests/wasm/counter/Cargo.toml b/crates/runtime-ffi/tests/wasm/counter/Cargo.toml index f1698d42c..af95ee37c 100644 --- a/crates/runtime-ffi/tests/wasm/counter/Cargo.toml +++ b/crates/runtime-ffi/tests/wasm/counter/Cargo.toml @@ -21,13 +21,6 @@ incremental = false rpath = false [dependencies] -svm-sdk = { path = "../../../../sdk", default-features = false } - -[features] -default = [] -ffi = ["svm-sdk/ffi", "static-alloc"] -mock = ["svm-sdk/mock"] -static-alloc = ["svm-sdk/static-alloc"] -dynamic-alloc = ["svm-sdk/dynamic-alloc"] +svm-sdk = { path = "../../../../sdk", features = ["ffi", "static-alloc"] } [workspace] diff --git a/crates/runtime-ffi/tests/wasm/counter/build.sh b/crates/runtime-ffi/tests/wasm/counter/build.sh index 42f726d28..7b754fa92 100755 --- a/crates/runtime-ffi/tests/wasm/counter/build.sh +++ b/crates/runtime-ffi/tests/wasm/counter/build.sh @@ -1,4 +1,4 @@ -cargo +nightly build --features=ffi,static-alloc --no-default-features --release --target wasm32-unknown-unknown +cargo +nightly build --release --target wasm32-unknown-unknown if [ -f ./../counter.wasm ]; then rm ./../counter.wasm diff --git a/crates/runtime-ffi/tests/wasm/failure/Cargo.toml b/crates/runtime-ffi/tests/wasm/failure/Cargo.toml index 20fd9dc18..74ac823fa 100644 --- a/crates/runtime-ffi/tests/wasm/failure/Cargo.toml +++ b/crates/runtime-ffi/tests/wasm/failure/Cargo.toml @@ -21,13 +21,6 @@ incremental = false rpath = false [dependencies] -svm-sdk = { path = "../../../../sdk", default-features = false } - -[features] -default = [] -ffi = ["svm-sdk/ffi", "static-alloc"] -mock = ["svm-sdk/mock"] -static-alloc = ["svm-sdk/static-alloc"] -dynamic-alloc = ["svm-sdk/dynamic-alloc"] +svm-sdk = { path = "../../../../sdk", features = ["ffi", "static-alloc"] } [workspace] diff --git a/crates/runtime-ffi/tests/wasm/failure/build.sh b/crates/runtime-ffi/tests/wasm/failure/build.sh index bb652e787..3fe1f4b6d 100755 --- a/crates/runtime-ffi/tests/wasm/failure/build.sh +++ b/crates/runtime-ffi/tests/wasm/failure/build.sh @@ -1,4 +1,4 @@ -cargo +nightly build --features=ffi,static-alloc --no-default-features --release --target wasm32-unknown-unknown +cargo +nightly build --release --target wasm32-unknown-unknown if [ -f ./../failure.wasm ]; then rm ./../failure.wasm diff --git a/crates/runtime/Cargo.toml b/crates/runtime/Cargo.toml index 3d61e2215..e01c9f452 100644 --- a/crates/runtime/Cargo.toml +++ b/crates/runtime/Cargo.toml @@ -28,7 +28,7 @@ svm-program = { path = "../program" } [dev-dependencies] maplit = "1.0.2" -svm-sdk = { path = "../sdk", features = ["mock", "dynamic-alloc"], default-features = false } +svm-sdk-mock = { path = "../sdk-mock", features = ["dynamic-alloc"], default-features = false } [features] default = [] diff --git a/crates/runtime/tests/runtime_tests.rs b/crates/runtime/tests/runtime_tests.rs index 5c60b99ba..f210f9d3d 100644 --- a/crates/runtime/tests/runtime_tests.rs +++ b/crates/runtime/tests/runtime_tests.rs @@ -1,3 +1,5 @@ +extern crate svm_sdk_mock as svm_sdk; + use svm_sdk as sdk; use svm_sdk::traits::Encoder; diff --git a/crates/runtime/tests/wasm/calldata/CallDataTemplate-meta.json b/crates/runtime/tests/wasm/calldata/CallDataTemplate-meta.json index d3b46f1fb..f11acb2ce 100644 --- a/crates/runtime/tests/wasm/calldata/CallDataTemplate-meta.json +++ b/crates/runtime/tests/wasm/calldata/CallDataTemplate-meta.json @@ -1 +1 @@ -{"api":[{"doc":"","is_ctor":false,"is_fundable":false,"name":"load_addr","signature":{"params":[],"returns":{"type":"Address"}},"wasm_name":"load_addr"},{"doc":"","is_ctor":true,"is_fundable":false,"name":"initialize","signature":{"params":[{"name":"init","type":"Address"},{"name":"should_store","type":"bool"}],"returns":{"type":"bool"}},"wasm_name":"initialize"},{"doc":"","is_ctor":false,"is_fundable":false,"name":"store_addr","signature":{"params":[{"name":"addr","type":"Address"}],"returns":{}},"wasm_name":"store_addr"}],"schema":[{"byte_count":20,"id":0,"name":"addr","offset":0,"type":"Address"}]} \ No newline at end of file +{"api":[{"doc":"","is_ctor":false,"is_fundable":false,"name":"store_addr","signature":{"params":[{"name":"addr","type":"Address"}],"returns":{}},"wasm_name":"store_addr"},{"doc":"","is_ctor":false,"is_fundable":false,"name":"load_addr","signature":{"params":[],"returns":{"type":"Address"}},"wasm_name":"load_addr"},{"doc":"","is_ctor":true,"is_fundable":false,"name":"initialize","signature":{"params":[{"name":"init","type":"Address"},{"name":"should_store","type":"bool"}],"returns":{"type":"bool"}},"wasm_name":"initialize"}],"schema":[{"byte_count":20,"id":0,"name":"addr","offset":0,"type":"Address"}]} \ No newline at end of file diff --git a/crates/runtime/tests/wasm/calldata/Cargo.lock b/crates/runtime/tests/wasm/calldata/Cargo.lock index 7b05608f5..0f03a8c68 100644 --- a/crates/runtime/tests/wasm/calldata/Cargo.lock +++ b/crates/runtime/tests/wasm/calldata/Cargo.lock @@ -8,12 +8,6 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" -[[package]] -name = "cfg-if" -version = "0.1.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" - [[package]] name = "convert_case" version = "0.4.0" @@ -67,18 +61,6 @@ version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" -[[package]] -name = "libc" -version = "0.2.93" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9385f66bf6105b241aa65a61cb923ef20efc665cb9f9bb50ac2f0c4b7f378d41" - -[[package]] -name = "memory_units" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8452105ba047068f40ff7093dd1d9da90898e63dd61736462e9cdda6a90ad3c3" - [[package]] name = "num-traits" version = "0.2.14" @@ -216,26 +198,33 @@ dependencies = [ "svm-abi-encoder", "svm-sdk-alloc", "svm-sdk-host", + "svm-sdk-host-ffi", "svm-sdk-macros", "svm-sdk-std", "svm-sdk-storage", + "svm-sdk-storage-ffi", "svm-sdk-types", ] [[package]] name = "svm-sdk-alloc" version = "0.0.0" + +[[package]] +name = "svm-sdk-host" +version = "0.0.0" dependencies = [ - "wee_alloc", + "svm-sdk-types", ] [[package]] -name = "svm-sdk-host" +name = "svm-sdk-host-ffi" version = "0.0.0" dependencies = [ "svm-abi-decoder", "svm-abi-encoder", "svm-sdk-alloc", + "svm-sdk-host", "svm-sdk-std", "svm-sdk-types", ] @@ -268,10 +257,20 @@ dependencies = [ [[package]] name = "svm-sdk-storage" version = "0.0.0" +dependencies = [ + "svm-sdk-alloc", + "svm-sdk-std", + "svm-sdk-types", +] + +[[package]] +name = "svm-sdk-storage-ffi" +version = "0.0.0" dependencies = [ "lazy_static", "svm-sdk-alloc", "svm-sdk-std", + "svm-sdk-storage", "svm-sdk-types", ] @@ -336,37 +335,3 @@ name = "unicode-xid" version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564" - -[[package]] -name = "wee_alloc" -version = "0.4.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dbb3b5a6b2bb17cb6ad44a2e68a43e8d2722c997da10e928665c72ec6c0a0b8e" -dependencies = [ - "cfg-if", - "libc", - "memory_units", - "winapi", -] - -[[package]] -name = "winapi" -version = "0.3.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" -dependencies = [ - "winapi-i686-pc-windows-gnu", - "winapi-x86_64-pc-windows-gnu", -] - -[[package]] -name = "winapi-i686-pc-windows-gnu" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" - -[[package]] -name = "winapi-x86_64-pc-windows-gnu" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/crates/runtime/tests/wasm/calldata/Cargo.toml b/crates/runtime/tests/wasm/calldata/Cargo.toml index ca92acfb5..518122a41 100644 --- a/crates/runtime/tests/wasm/calldata/Cargo.toml +++ b/crates/runtime/tests/wasm/calldata/Cargo.toml @@ -21,14 +21,6 @@ incremental = false rpath = false [dependencies] -svm-sdk = { path = "../../../../sdk", default-features = false } - -[features] -default = ["mock", "static-alloc"] -meta = ["svm-sdk/meta"] -ffi = ["svm-sdk/ffi"] -mock = ["svm-sdk/mock"] -static-alloc = ["svm-sdk/static-alloc"] -dynamic-alloc = ["svm-sdk/dynamic-alloc"] +svm-sdk = { path = "../../../../sdk", features = ["static-alloc", "meta"] } [workspace] diff --git a/crates/runtime/tests/wasm/calldata/build.sh b/crates/runtime/tests/wasm/calldata/build.sh index 698ba18be..2f14c10b8 100755 --- a/crates/runtime/tests/wasm/calldata/build.sh +++ b/crates/runtime/tests/wasm/calldata/build.sh @@ -1,4 +1,4 @@ -cargo +nightly build --features=ffi,static-alloc,meta --no-default-features --release --target wasm32-unknown-unknown +cargo +nightly build --release --target wasm32-unknown-unknown if [ -f ./../runtime_calldata.wasm ]; then rm ./../runtime_calldata.wasm diff --git a/crates/runtime/tests/wasm/runtime_calldata.wasm b/crates/runtime/tests/wasm/runtime_calldata.wasm index 5376fe2f645d41a2d0c8220a0889551a66eac638..8873713e50b6c5b39b8c1d186ca7aad3a5c30af1 100755 GIT binary patch delta 543 zcmaKo&q~8U5QnW;J=(jc+8jK|PWMlCK`jcw9z+lWf|szHowQ<_KoiAVA3-E%-$4)` zz^Cvac=s*bL|XLF&T-)P&HTQ%@I8F_=+2%x8=0P9>wCMqb{t)wgDCDNQDJYRabA>B zlH5>yC_y$`ETbuwchk)3QiDGR!wrLkC5%wC+yt$YbNC> zAP%9x2rFYpF(gJ%LHlrJ9_oCWYx^5FR2oT1!hFlXS8=eplexT5k#`n)&8~gu<}9d+-TAmZGX=B&N=^E{1QLC*LLr{m5~^l(nqDXbd+42A)MU|lT7!M-Z*pl zXKfom#*LPSS*|1zm_?=IMPa+X0~imG#u6zQQqF~pmJb(&cl9lg=#U^tt?{|AFi`np zy*+77wZS|+bK_K88e(qUtWt3>h(g4AkV;D^l^AhF81kbs|JRGAz`bB_j$aY?DBf>`Ox`~h^THXzKG ZTq}$O;+940o!o0|=C5Gmxw_vhe*ve8n34bh diff --git a/crates/sdk-host-ffi/Cargo.toml b/crates/sdk-host-ffi/Cargo.toml new file mode 100644 index 000000000..eaa9cc5f4 --- /dev/null +++ b/crates/sdk-host-ffi/Cargo.toml @@ -0,0 +1,38 @@ +[package] +name = "svm-sdk-host-ffi" +version = "0.0.0" +authors = ["Spacemesh SVM Team"] +license = "MIT" +edition = "2018" +readme = "README.md" +repository = "https://github.com/spacemeshos/svm" +homepage = "https://github.com/spacemeshos/svm" +description = "Spacemesh Virtual Machine" +publish = false + +[dependencies] +svm-sdk-alloc = { path = "../sdk-alloc", default-features = false } +svm-sdk-host = { path = "../sdk-host" } +svm-sdk-types = { path = "../sdk-types", default-features = false } +svm-sdk-std = { path = "../sdk-std", default-features = false } +svm-abi-encoder = { path = "../abi-encoder", default-features = false } +svm-abi-decoder = { path = "../abi-decoder", default-features = false } + +[dev-dependencies] +lazy_static = "1.4.0" + +[features] +static-alloc = [ + "svm-sdk-alloc/static-alloc", + "svm-sdk-types/static-alloc", + "svm-sdk-std/static-alloc", + "svm-abi-encoder/static-alloc", + "svm-abi-decoder/static-alloc", +] +dynamic-alloc = [ + "svm-sdk-alloc/dynamic-alloc", + "svm-sdk-types/dynamic-alloc", + "svm-sdk-std/dynamic-alloc", + "svm-abi-encoder/dynamic-alloc", + "svm-abi-decoder/dynamic-alloc", +] diff --git a/crates/sdk-host/src/ext.rs b/crates/sdk-host-ffi/src/ext.rs similarity index 99% rename from crates/sdk-host/src/ext.rs rename to crates/sdk-host-ffi/src/ext.rs index 321e2e530..43a47381d 100644 --- a/crates/sdk-host/src/ext.rs +++ b/crates/sdk-host-ffi/src/ext.rs @@ -1,6 +1,5 @@ -use crate::traits::Host; - use svm_sdk_alloc::Ptr; +use svm_sdk_host::Host; use svm_sdk_types::{Address, Amount, LayerId}; use core::mem::MaybeUninit; diff --git a/crates/sdk-host-ffi/src/lib.rs b/crates/sdk-host-ffi/src/lib.rs new file mode 100644 index 000000000..3bf0f2c87 --- /dev/null +++ b/crates/sdk-host-ffi/src/lib.rs @@ -0,0 +1,17 @@ +//! This crate implements SDK for SVM. +//! Using this crate when writing SVM Templates in Rust isn't mandatory but should be very useful. +//! +//! The crate is compiled with `![no_std]` (no Rust standard-library) annotation in order to reduce the compiled WASM size. + +#![no_std] +#![allow(missing_docs)] +#![allow(unused)] +#![allow(dead_code)] +#![allow(unreachable_code)] +#![deny(rustdoc::broken_intra_doc_links)] +#![feature(maybe_uninit_uninit_array)] +#![feature(once_cell)] + +mod ext; + +pub use ext::ExtHost; diff --git a/crates/sdk-host-mock/Cargo.toml b/crates/sdk-host-mock/Cargo.toml new file mode 100644 index 000000000..6c582b5cd --- /dev/null +++ b/crates/sdk-host-mock/Cargo.toml @@ -0,0 +1,39 @@ +[package] +name = "svm-sdk-host-mock" +version = "0.0.0" +authors = ["Spacemesh SVM Team"] +license = "MIT" +edition = "2018" +readme = "README.md" +repository = "https://github.com/spacemeshos/svm" +homepage = "https://github.com/spacemeshos/svm" +description = "Spacemesh Virtual Machine" +publish = false + +[dependencies] +svm-sdk-alloc = { path = "../sdk-alloc", default-features = false } +svm-sdk-host = { path = "../sdk-host" } +svm-sdk-types = { path = "../sdk-types", default-features = false } +svm-sdk-std = { path = "../sdk-std", default-features = false } +svm-abi-encoder = { path = "../abi-encoder", default-features = false } +svm-abi-decoder = { path = "../abi-decoder", default-features = false } + +[dev-dependencies] +lazy_static = "1.4.0" + +[features] +mock = [] +static-alloc = [ + "svm-sdk-alloc/static-alloc", + "svm-sdk-types/static-alloc", + "svm-sdk-std/static-alloc", + "svm-abi-encoder/static-alloc", + "svm-abi-decoder/static-alloc", +] +dynamic-alloc = [ + "svm-sdk-alloc/dynamic-alloc", + "svm-sdk-types/dynamic-alloc", + "svm-sdk-std/dynamic-alloc", + "svm-abi-encoder/dynamic-alloc", + "svm-abi-decoder/dynamic-alloc", +] diff --git a/crates/sdk-host/src/mock.rs b/crates/sdk-host-mock/src/lib.rs similarity index 95% rename from crates/sdk-host/src/mock.rs rename to crates/sdk-host-mock/src/lib.rs index 6e7aac0fc..132535115 100644 --- a/crates/sdk-host/src/mock.rs +++ b/crates/sdk-host-mock/src/lib.rs @@ -1,6 +1,19 @@ -use crate::traits::Host; +//! This crate implements SDK for SVM. +//! Using this crate when writing SVM Templates in Rust isn't mandatory but should be very useful. +//! +//! The crate is compiled with `![no_std]` (no Rust standard-library) annotation in order to reduce the compiled WASM size. + +#![no_std] +#![allow(missing_docs)] +#![allow(unused)] +#![allow(dead_code)] +#![allow(unreachable_code)] +#![deny(rustdoc::broken_intra_doc_links)] +#![feature(maybe_uninit_uninit_array)] +#![feature(once_cell)] use svm_abi_encoder::{ByteSize, Encoder}; +use svm_sdk_host::Host; use svm_sdk_std::Vec; use svm_sdk_types::{Address, Amount, LayerId}; diff --git a/crates/sdk-host/Cargo.toml b/crates/sdk-host/Cargo.toml index cbde7b0fd..62cd87591 100644 --- a/crates/sdk-host/Cargo.toml +++ b/crates/sdk-host/Cargo.toml @@ -11,30 +11,4 @@ description = "Spacemesh Virtual Machine" publish = false [dependencies] -svm-sdk-alloc = { path = "../sdk-alloc", default-features = false } svm-sdk-types = { path = "../sdk-types", default-features = false } -svm-sdk-std = { path = "../sdk-std", default-features = false } -svm-abi-encoder = { path = "../abi-encoder", default-features = false } -svm-abi-decoder = { path = "../abi-decoder", default-features = false } - -[dev-dependencies] -lazy_static = "1.4.0" - -[features] -default = ["mock"] -ffi = [] -mock = [] -static-alloc = [ - "svm-sdk-alloc/static-alloc", - "svm-sdk-types/static-alloc", - "svm-sdk-std/static-alloc", - "svm-abi-encoder/static-alloc", - "svm-abi-decoder/static-alloc", -] -dynamic-alloc = [ - "svm-sdk-alloc/dynamic-alloc", - "svm-sdk-types/dynamic-alloc", - "svm-sdk-std/dynamic-alloc", - "svm-abi-encoder/dynamic-alloc", - "svm-abi-decoder/dynamic-alloc", -] diff --git a/crates/sdk-host/src/lib.rs b/crates/sdk-host/src/lib.rs index 699768bc5..8ac120166 100644 --- a/crates/sdk-host/src/lib.rs +++ b/crates/sdk-host/src/lib.rs @@ -5,26 +5,26 @@ #![no_std] #![allow(missing_docs)] -#![allow(unused)] -#![allow(dead_code)] -#![allow(unreachable_code)] -#![deny(rustdoc::broken_intra_doc_links)] -#![feature(maybe_uninit_uninit_array)] -#![feature(once_cell)] +#![allow(missing_docs)] + +use svm_sdk_types::{Address, Amount, LayerId}; + +pub trait Host { + fn calldata(&self) -> &'static [u8]; + + fn set_returndata(&mut self, bytes: &[u8]); + + fn principal(&self) -> Address; -pub mod traits; + fn target(&self) -> Address; -mod ext; -mod mock; + fn value(&self) -> Amount; -#[cfg(all(feature = "ffi", feature = "mock"))] -compile_error!("can't have both `ffi` and `mock` features turned-on"); + fn layer_id(&self) -> LayerId; -#[cfg(not(any(feature = "ffi", feature = "mock")))] -compile_error!("must have at least one feature flag turned-on (`ffi` or `mock`)"); + fn balance(&self) -> Amount; -#[cfg(feature = "ffi")] -pub use ext::ExtHost; + fn transfer(&mut self, dst: &Address, amount: Amount); -#[cfg(feature = "mock")] -pub use mock::MockHost; + fn log(&mut self, msg: &str, code: u8); +} diff --git a/crates/sdk-host/src/traits.rs b/crates/sdk-host/src/traits.rs deleted file mode 100644 index 8aa50a23d..000000000 --- a/crates/sdk-host/src/traits.rs +++ /dev/null @@ -1,21 +0,0 @@ -use svm_sdk_types::{Address, Amount, LayerId}; - -pub trait Host { - fn calldata(&self) -> &'static [u8]; - - fn set_returndata(&mut self, bytes: &[u8]); - - fn principal(&self) -> Address; - - fn target(&self) -> Address; - - fn value(&self) -> Amount; - - fn layer_id(&self) -> LayerId; - - fn balance(&self) -> Amount; - - fn transfer(&mut self, dst: &Address, amount: Amount); - - fn log(&mut self, msg: &str, code: u8); -} diff --git a/crates/sdk-macros/Cargo.toml b/crates/sdk-macros/Cargo.toml index 1940cb14c..cabe0e826 100644 --- a/crates/sdk-macros/Cargo.toml +++ b/crates/sdk-macros/Cargo.toml @@ -31,23 +31,17 @@ lazy_static = "1.4.0" trybuild = { version = "1.0.52", features = ["diff"] } [features] -default = ["mock", "meta", "dynamic-alloc"] +default = ["meta", "dynamic-alloc"] meta = [] -ffi = ["svm-sdk-host/ffi", "svm-sdk-storage/ffi"] -mock = ["svm-sdk-host/mock", "svm-sdk-storage/mock"] static-alloc = [ "svm-sdk-types/static-alloc", "svm-abi-encoder/static-alloc", "svm-abi-decoder/static-alloc", "svm-sdk-std/static-alloc", - "svm-sdk-host/static-alloc", - "svm-sdk-storage/static-alloc", ] dynamic-alloc = [ "svm-sdk-types/dynamic-alloc", "svm-abi-encoder/dynamic-alloc", "svm-abi-decoder/dynamic-alloc", "svm-sdk-std/dynamic-alloc", - "svm-sdk-host/dynamic-alloc", - "svm-sdk-storage/dynamic-alloc", ] diff --git a/crates/sdk-macros/src/function/endpoint.rs b/crates/sdk-macros/src/function/endpoint.rs index 9e7a4a021..42fd594d0 100644 --- a/crates/sdk-macros/src/function/endpoint.rs +++ b/crates/sdk-macros/src/function/endpoint.rs @@ -13,8 +13,8 @@ pub fn expand(func: &Function, attrs: &[FuncAttr], template: &Template) -> Resul validate_sig(func)?; let name = func.raw_name(); - let prologue = expand_prologue(func)?; - let epilogue = expand_epilogue(func)?; + let prologue = expand_prologue(func, template.must_mock())?; + let epilogue = expand_epilogue(func, template.must_mock())?; let returns = expand_returns(func)?; let body = func.raw_body(); @@ -54,7 +54,7 @@ pub fn expand(func: &Function, attrs: &[FuncAttr], template: &Template) -> Resul Ok(ast) } -fn expand_prologue(func: &Function) -> Result { +fn expand_prologue(func: &Function, must_mock: bool) -> Result { let sig = func.raw_sig(); if sig.inputs.is_empty() { @@ -81,7 +81,7 @@ fn expand_prologue(func: &Function) -> Result { } } - let includes = function::host_includes(); + let includes = function::host_includes(must_mock); let ast = quote! { #includes @@ -117,9 +117,9 @@ fn expand_returns_size(func: &Function) -> Result { Ok(ast) } -fn expand_epilogue(func: &Function) -> Result { +fn expand_epilogue(func: &Function, must_mock: bool) -> Result { let ast = if func.has_returns() { - let includes = function::host_includes(); + let includes = function::host_includes(must_mock); let returns_size = expand_returns_size(func)?; quote! { diff --git a/crates/sdk-macros/src/function/fundable.rs b/crates/sdk-macros/src/function/fundable.rs index 0029b6936..875f32845 100644 --- a/crates/sdk-macros/src/function/fundable.rs +++ b/crates/sdk-macros/src/function/fundable.rs @@ -20,11 +20,11 @@ pub fn expand(attrs: &[FuncAttr], template: &Template) -> Result { _ => unreachable!(), }; - call_fundable_hook_ast(fundable_hook) + call_fundable_hook_ast(fundable_hook, template.must_mock()) } -pub fn call_fundable_hook_ast(fundable_hook: Ident) -> Result { - let includes = function::host_includes(); +pub fn call_fundable_hook_ast(fundable_hook: Ident, must_mock: bool) -> Result { + let includes = function::host_includes(must_mock); let ast = quote! { { diff --git a/crates/sdk-macros/src/function/fundable_hook.rs b/crates/sdk-macros/src/function/fundable_hook.rs index 8f76cf2e4..e6d6f58a8 100644 --- a/crates/sdk-macros/src/function/fundable_hook.rs +++ b/crates/sdk-macros/src/function/fundable_hook.rs @@ -7,7 +7,7 @@ use attr::{has_fundable_hook_attr, FuncAttr}; use crate::{function, Function}; -pub fn expand(func: &Function, attrs: &[FuncAttr]) -> Result { +pub fn expand(func: &Function, attrs: &[FuncAttr], must_mock: bool) -> Result { debug_assert!(has_fundable_hook_attr(attrs)); validate_fundable_hook_func_sig(func)?; @@ -15,7 +15,7 @@ pub fn expand(func: &Function, attrs: &[FuncAttr]) -> Result { let sig = func.raw_sig(); let body = func.raw_body(); - let includes = function::host_includes(); + let includes = function::host_includes(must_mock); let ast = quote! { #[inline] diff --git a/crates/sdk-macros/src/function/mod.rs b/crates/sdk-macros/src/function/mod.rs index 92f94d0bd..cd21a0883 100644 --- a/crates/sdk-macros/src/function/mod.rs +++ b/crates/sdk-macros/src/function/mod.rs @@ -74,7 +74,7 @@ pub fn expand(func: &Function, template: &Template) -> Result { } else if has_endpoint_attr(&attrs) { endpoint::expand(func, &attrs, template)? } else if has_fundable_hook_attr(&attrs) { - fundable_hook::expand(func, &attrs)? + fundable_hook::expand(func, &attrs, template.must_mock())? } else { expand_func(func, &attrs)? }; @@ -92,15 +92,17 @@ fn validate_attrs(attrs: &[FuncAttr]) -> Result<()> { Ok(()) } -pub fn host_includes() -> TokenStream { - quote! { - use svm_sdk::traits::Host; - - #[cfg(feature = "mock")] - use svm_sdk::host::MockHost as Node; - - #[cfg(feature = "ffi")] - use svm_sdk::host::ExtHost as Node; +pub fn host_includes(must_mock: bool) -> TokenStream { + if must_mock { + quote! { + use svm_sdk::traits::Host; + use svm_sdk::host::MockHost as Node; + } + } else { + quote! { + use svm_sdk::traits::Host; + use svm_sdk::host::ExtHost as Node; + } } } diff --git a/crates/sdk-macros/src/lib.rs b/crates/sdk-macros/src/lib.rs index 74e67e87f..1922c957c 100644 --- a/crates/sdk-macros/src/lib.rs +++ b/crates/sdk-macros/src/lib.rs @@ -23,7 +23,18 @@ pub fn template( args: proc_macro::TokenStream, input: proc_macro::TokenStream, ) -> proc_macro::TokenStream { - match template::expand(args.into(), input.into()) { + match template::expand(args.into(), input.into(), false) { + Ok((ast, meta)) => finalize_ast(ast, &meta), + Err(err) => err.to_compile_error().into(), + } +} + +#[proc_macro_attribute] +pub fn template_mock( + args: proc_macro::TokenStream, + input: proc_macro::TokenStream, +) -> proc_macro::TokenStream { + match template::expand(args.into(), input.into(), true) { Ok((ast, meta)) => finalize_ast(ast, &meta), Err(err) => err.to_compile_error().into(), } @@ -32,9 +43,6 @@ pub fn template( #[cfg(feature = "meta")] fn finalize_ast(ast: proc_macro2::TokenStream, meta: &TemplateMeta) -> proc_macro::TokenStream { let path = format!("{}-meta.json", meta.name()); - dbg!(&path); - dbg!(&path); - dbg!(&path); let meta_json = json::meta(&meta); json::json_write(&path, &meta_json); diff --git a/crates/sdk-macros/src/struct/mod.rs b/crates/sdk-macros/src/struct/mod.rs index 980e1069e..f7dc9fcc0 100644 --- a/crates/sdk-macros/src/struct/mod.rs +++ b/crates/sdk-macros/src/struct/mod.rs @@ -34,11 +34,11 @@ impl Struct { } } -pub fn expand(strukt: &Struct) -> Result { +pub fn expand(strukt: &Struct, must_mock: bool) -> Result { match strukt.attrs() { Ok(attrs) => { if has_storage_attr(attrs) { - storage::expand(strukt, attrs) + storage::expand(strukt, attrs, must_mock) } else { todo!() } diff --git a/crates/sdk-macros/src/struct/storage.rs b/crates/sdk-macros/src/struct/storage.rs index 9c4c48ed6..b7e6f8ee0 100644 --- a/crates/sdk-macros/src/struct/storage.rs +++ b/crates/sdk-macros/src/struct/storage.rs @@ -8,14 +8,14 @@ use attr::{has_storage_attr, StructAttr}; use crate::{PrimType, Struct, Type}; -pub fn expand(strukt: &Struct, attrs: &[StructAttr]) -> Result { +pub fn expand(strukt: &Struct, attrs: &[StructAttr], must_mock: bool) -> Result { debug_assert!(has_storage_attr(attrs)); let vars = storage_vars(strukt)?; let name = strukt.raw_name(); - let getters = getters_ast(&vars); - let setters = setters_ast(&vars); + let getters = getters_ast(&vars, must_mock); + let setters = setters_ast(&vars, must_mock); let ast = quote! { struct #name; @@ -121,11 +121,11 @@ fn ensure_named_fields(fields: &Fields) -> Result<()> { } } -fn getters_ast(vars: &[Var]) -> TokenStream { +fn getters_ast(vars: &[Var], must_mock: bool) -> TokenStream { let mut getters = Vec::new(); for var in vars.iter() { - let ast = getter_ast(var); + let ast = getter_ast(var, must_mock); getters.push(ast); } @@ -135,11 +135,11 @@ fn getters_ast(vars: &[Var]) -> TokenStream { } } -fn setters_ast(vars: &[Var]) -> TokenStream { +fn setters_ast(vars: &[Var], must_mock: bool) -> TokenStream { let mut setters = Vec::new(); for var in vars.iter() { - let ast = setter_ast(var); + let ast = setter_ast(var, must_mock); setters.push(ast); } @@ -149,8 +149,8 @@ fn setters_ast(vars: &[Var]) -> TokenStream { } } -fn getter_ast(var: &Var) -> TokenStream { - let includes = include_storage_ast(); +fn getter_ast(var: &Var, must_mock: bool) -> TokenStream { + let includes = include_storage_ast(must_mock); match var { Var::Primitive { id, name, ty, .. } => { @@ -264,8 +264,8 @@ fn getter_ast(var: &Var) -> TokenStream { } } -fn setter_ast(var: &Var) -> TokenStream { - let includes = include_storage_ast(); +fn setter_ast(var: &Var, must_mock: bool) -> TokenStream { + let includes = include_storage_ast(must_mock); match var { Var::Primitive { id, name, ty, .. } => { @@ -383,21 +383,19 @@ fn setter_ident(var_name: &Ident) -> Ident { Ident::new(&format!("set_{}", var_name), Span::call_site()) } -fn include_storage_ast() -> TokenStream { - quote! { - use svm_sdk::traits::Storage; - - #[cfg(all(feature = "mock", feature = "ffi"))] - compile_error!("`svm_sdk` must be compiled with feature \"mock\" or \"ffi\" but not both!"); - - #[cfg(all(not(feature = "mock"), not(feature = "ffi")))] - compile_error!("`svm_sdk` must be compiled with feature \"mock\" or \"ffi\""); +fn include_storage_ast(must_mock: bool) -> TokenStream { + if must_mock { + quote! { + use svm_sdk::traits::Storage; - #[cfg(feature = "mock")] - use svm_sdk::storage::MockStorage as StorageImpl; + use svm_sdk::storage::MockStorage as StorageImpl; + } + } else { + quote! { + use svm_sdk::traits::Storage; - #[cfg(feature = "ffi")] - use svm_sdk::storage::ExtStorage as StorageImpl; + use svm_sdk::storage::ExtStorage as StorageImpl; + } } } diff --git a/crates/sdk-macros/src/template.rs b/crates/sdk-macros/src/template.rs index c3792edf5..095796813 100644 --- a/crates/sdk-macros/src/template.rs +++ b/crates/sdk-macros/src/template.rs @@ -10,6 +10,7 @@ use r#struct::has_storage_attr; pub struct Template { name: Ident, + must_mock: bool, functions: Vec, structs: Vec, imports: Vec, @@ -22,6 +23,10 @@ impl Template { self.name.to_string() } + pub fn must_mock(&self) -> bool { + self.must_mock + } + pub fn functions(&self) -> &[Function] { &self.functions } @@ -47,9 +52,13 @@ impl Template { } } -pub fn expand(_args: TokenStream, input: TokenStream) -> Result<(TokenStream, TemplateMeta)> { +pub fn expand( + args: TokenStream, + input: TokenStream, + must_mock: bool, +) -> Result<(TokenStream, TemplateMeta)> { let module = syn::parse2(input)?; - let template = parse_template(module)?; + let template = parse_template(module, must_mock)?; let meta = meta::template_meta(&template)?; let _imports = template.imports(); @@ -58,7 +67,7 @@ pub fn expand(_args: TokenStream, input: TokenStream) -> Result<(TokenStream, Te let structs = expand_structs(&template)?; let functions = expand_functions(&template)?; let verify_export = export_verify_ast(); - let alloc_export = export_alloc_ast(); + let alloc_export = export_alloc_ast(&template); let ast = quote! { #verify_export @@ -73,7 +82,7 @@ pub fn expand(_args: TokenStream, input: TokenStream) -> Result<(TokenStream, Te Ok((ast, meta)) } -pub fn parse_template(mut raw_template: ItemMod) -> Result