From eaeafe5ae48c04a5412a24f6e240632052dd5e2d Mon Sep 17 00:00:00 2001 From: Michael Birch Date: Wed, 19 May 2021 16:40:54 -0400 Subject: [PATCH 1/6] Add state_migration function and call it during upgrade --- src/lib.rs | 9 +++++++++ src/sdk.rs | 34 ++++++++++++++++++++++++++++++---- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 5132b9856..e759f614f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -146,6 +146,15 @@ mod contract { sdk::self_deploy(&bytes_to_key(KeyPrefix::Config, CODE_KEY)); } + /// Called as part of the upgrade process (see `sdk::self_deploy`). This function is meant + /// to make any necessary changes to the state such that it aligns with the newly deployed + /// code. + #[no_mangle] + pub extern "C" fn state_migration() { + // This function is purposely left empty because we do not have any state migration + // to do. + } + /// /// MUTATIVE METHODS /// diff --git a/src/sdk.rs b/src/sdk.rs index fc4faa4a9..eef0384af 100644 --- a/src/sdk.rs +++ b/src/sdk.rs @@ -4,6 +4,7 @@ use borsh::{BorshDeserialize, BorshSerialize}; const READ_STORAGE_REGISTER_ID: u64 = 0; const INPUT_REGISTER_ID: u64 = 0; +const GAS_FOR_STATE_MIGRATION: u64 = 100_000_000_000_000; mod exports { @@ -85,7 +86,7 @@ mod exports { code_len: u64, code_ptr: u64, ); - fn promise_batch_action_function_call( + pub(crate) fn promise_batch_action_function_call( promise_index: u64, method_name_len: u64, method_name_ptr: u64, @@ -351,9 +352,14 @@ pub fn self_deploy(code_key: &[u8]) { let promise_id = exports::promise_batch_create(u64::MAX as _, 0); // Remove code from storage and store it in register 1. exports::storage_remove(code_key.len() as _, code_key.as_ptr() as _, 1); - exports::promise_batch_action_deploy_contract(promise_id, u64::MAX as _, 1); - // TODO: Call upgrade on the same promise to make sure the state has migrated successfully. - // Otherwise, you may have to handle non-latest state in every other method call, which might be inefficient. + exports::promise_batch_action_deploy_contract(promise_id, u64::MAX, 1); + promise_batch_action_function_call( + promise_id, + b"state_migration", + &[], + 0, + GAS_FOR_STATE_MIGRATION, + ) } } @@ -502,6 +508,26 @@ pub fn promise_batch_create(account_id: String) -> u64 { unsafe { exports::promise_batch_create(account_id.len() as _, account_id.as_ptr() as _) } } +pub fn promise_batch_action_function_call( + promise_idx: u64, + method_name: &[u8], + arguments: &[u8], + amount: u128, + gas: u64, +) { + unsafe { + exports::promise_batch_action_function_call( + promise_idx, + method_name.len() as _, + method_name.as_ptr() as _, + arguments.len() as _, + arguments.as_ptr() as _, + &amount as *const u128 as _, + gas, + ) + } +} + #[allow(dead_code)] pub fn storage_has_key(key: &[u8]) -> bool { unsafe { exports::storage_has_key(key.len() as u64, key.as_ptr() as u64) == 1 } From c18e0d9d97f59805a7530cc0be3b30287acfef8b Mon Sep 17 00:00:00 2001 From: Michael Birch Date: Thu, 20 May 2021 10:31:13 -0400 Subject: [PATCH 2/6] Use a separate compile-time feature for engine code so that EngineState can be imported into a test contract without importing all the public exports in lib.rs that are hidden by the contract compile-time feature --- Cargo.toml | 3 ++- src/lib.rs | 43 ++++++++++++++++++++++--------------------- 2 files changed, 24 insertions(+), 22 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e0d79ee30..a77602742 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -74,5 +74,6 @@ git2 = "0.13" default = ["sha2", "std"] std = ["borsh/std", "evm/std", "primitive-types/std", "rlp/std", "sha3/std", "ethabi/std", "lunarity-lexer/std", "bn/std"] testnet = [] -contract = [] +engine = [] +contract = ["engine"] evm_bully = [] diff --git a/src/lib.rs b/src/lib.rs index e759f614f..dfecbcd7f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,7 +7,7 @@ extern crate alloc; #[cfg(not(feature = "std"))] extern crate core; -#[cfg(feature = "contract")] +#[cfg(feature = "engine")] mod map; pub mod meta_parsing; pub mod parameters; @@ -16,14 +16,14 @@ pub mod storage; pub mod transaction; pub mod types; -#[cfg(feature = "contract")] -mod engine; +#[cfg(feature = "engine")] +pub mod engine; #[cfg(feature = "contract")] mod json; #[cfg(feature = "contract")] mod log_entry; mod precompiles; -#[cfg(feature = "contract")] +#[cfg(feature = "engine")] mod sdk; #[cfg(test)] @@ -46,26 +46,9 @@ mod contract { use crate::storage::{bytes_to_key, KeyPrefix}; use crate::types::{near_account_to_evm_address, u256_to_arr}; - #[global_allocator] - static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; - const CODE_KEY: &[u8; 4] = b"CODE"; const CODE_STAGE_KEY: &[u8; 10] = b"CODE_STAGE"; - #[cfg(target_arch = "wasm32")] - #[panic_handler] - #[no_mangle] - pub unsafe fn on_panic(_info: &::core::panic::PanicInfo) -> ! { - ::core::intrinsics::abort(); - } - - #[cfg(target_arch = "wasm32")] - #[alloc_error_handler] - #[no_mangle] - pub unsafe fn on_alloc_error(_: core::alloc::Layout) -> ! { - ::core::intrinsics::abort(); - } - /// /// ADMINISTRATIVE METHODS /// @@ -432,3 +415,21 @@ mod contract { } } } + +#[cfg(target_arch = "wasm32")] +#[global_allocator] +static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; + +#[cfg(target_arch = "wasm32")] +#[panic_handler] +#[no_mangle] +pub unsafe fn on_panic(_info: &::core::panic::PanicInfo) -> ! { + ::core::intrinsics::abort(); +} + +#[cfg(target_arch = "wasm32")] +#[alloc_error_handler] +#[no_mangle] +pub unsafe fn on_alloc_error(_: core::alloc::Layout) -> ! { + ::core::intrinsics::abort(); +} From 841c92e38b48138598623542a166937871b84a8a Mon Sep 17 00:00:00 2001 From: Michael Birch Date: Thu, 20 May 2021 16:07:14 +0000 Subject: [PATCH 3/6] Add contract to use for testing state migrations --- .gitignore | 1 + etc/state-migration-test/Cargo.lock | 990 ++++++++++++++++++++++++++++ etc/state-migration-test/Cargo.toml | 41 ++ etc/state-migration-test/src/lib.rs | 42 ++ src/lib.rs | 2 +- 5 files changed, 1075 insertions(+), 1 deletion(-) create mode 100644 etc/state-migration-test/Cargo.lock create mode 100644 etc/state-migration-test/Cargo.toml create mode 100644 etc/state-migration-test/src/lib.rs diff --git a/.gitignore b/.gitignore index 4c577a1ed..d108eec41 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,4 @@ # Rust artifacts /*.wasm /target/ +etc/state-migration-test/target/ diff --git a/etc/state-migration-test/Cargo.lock b/etc/state-migration-test/Cargo.lock new file mode 100644 index 000000000..0ebfde6e3 --- /dev/null +++ b/etc/state-migration-test/Cargo.lock @@ -0,0 +1,990 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "ahash" +version = "0.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "739f4a8db6605981345c5654f3a85b056ce52f37a39d34da03f25bf2151ea16e" + +[[package]] +name = "anyhow" +version = "1.0.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "28b2cd92db5cbd74e8e5028f7e27dd7aa3090e89e4f2a197cc7c8dfb69c7063b" + +[[package]] +name = "arrayref" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4c527152e37cf757a3f78aae5a06fbeefdb07ccc535c980a3208ee3060dd544" + +[[package]] +name = "aurora-bn" +version = "0.1.0" +source = "git+https://github.com/aurora-is-near/aurora-bn.git#8f1743884061981cac84388862e2763b2aa09307" +dependencies = [ + "byteorder", + "getrandom", +] + +[[package]] +name = "aurora-engine" +version = "1.0.0" +dependencies = [ + "aurora-bn", + "blake2", + "borsh", + "ethabi", + "evm", + "evm-core", + "hex", + "libsecp256k1", + "lunarity-lexer", + "num", + "primitive-types", + "ripemd160", + "rjson", + "rlp", + "sha2", + "sha3 0.9.1", + "wee_alloc", +] + +[[package]] +name = "aurora-engine-state-migration-test" +version = "1.0.0" +dependencies = [ + "aurora-engine", + "borsh", +] + +[[package]] +name = "autocfg" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" + +[[package]] +name = "blake2" +version = "0.9.1" +source = "git+https://github.com/near/near-blake2.git#736ff607cc8160af87ffa697c14ebef85050138f" +dependencies = [ + "crypto-mac", + "digest 0.9.0", + "opaque-debug 0.3.0", +] + +[[package]] +name = "block-buffer" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0940dc441f31689269e10ac70eb1002a3a1d3ad1390e030043662eb7fe4688b" +dependencies = [ + "block-padding 0.1.5", + "byte-tools", + "byteorder", + "generic-array 0.12.4", +] + +[[package]] +name = "block-buffer" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4152116fd6e9dadb291ae18fc1ec3575ed6d84c29642d97890f4b4a3417297e4" +dependencies = [ + "block-padding 0.2.1", + "generic-array 0.14.4", +] + +[[package]] +name = "block-padding" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa79dedbb091f449f1f39e53edf88d5dbe95f895dae6135a8d7b881fb5af73f5" +dependencies = [ + "byte-tools", +] + +[[package]] +name = "block-padding" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d696c370c750c948ada61c69a0ee2cbbb9c50b1019ddb86d9317157a99c2cae" + +[[package]] +name = "borsh" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09a7111f797cc721407885a323fb071636aee57f750b1a4ddc27397eba168a74" +dependencies = [ + "borsh-derive", + "hashbrown", +] + +[[package]] +name = "borsh-derive" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "307f3740906bac2c118a8122fe22681232b244f1369273e45f1156b45c43d2dd" +dependencies = [ + "borsh-derive-internal", + "borsh-schema-derive-internal", + "proc-macro-crate", + "proc-macro2 1.0.26", + "syn 1.0.72", +] + +[[package]] +name = "borsh-derive-internal" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2104c73179359431cc98e016998f2f23bc7a05bc53e79741bcba705f30047bc" +dependencies = [ + "proc-macro2 1.0.26", + "quote 1.0.9", + "syn 1.0.72", +] + +[[package]] +name = "borsh-schema-derive-internal" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae29eb8418fcd46f723f8691a2ac06857d31179d33d2f2d91eb13967de97c728" +dependencies = [ + "proc-macro2 1.0.26", + "quote 1.0.9", + "syn 1.0.72", +] + +[[package]] +name = "bumpalo" +version = "3.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "63396b8a4b9de3f4fdfb320ab6080762242f66a8ef174c49d8e19b674db4cdbe" + +[[package]] +name = "byte-tools" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3b5ca7a04898ad4bcd41c90c5285445ff5b791899bb1b0abdd2a2aa791211d7" + +[[package]] +name = "byteorder" +version = "1.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" + +[[package]] +name = "bytes" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b700ce4376041dcd0a327fd0097c41095743c4c8af8887265942faf1100bd040" + +[[package]] +name = "cfg-if" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "cpufeatures" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed00c67cb5d0a7d64a44f6ad2668db7e7530311dd53ea79bcd4fb022c64911c8" +dependencies = [ + "libc", +] + +[[package]] +name = "crunchy" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a81dae078cea95a014a339291cec439d2f232ebe854a9d672b796c6afafa9b7" + +[[package]] +name = "crypto-mac" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b584a330336237c1eecd3e94266efb216c56ed91225d634cb2991c5f3fd1aeab" +dependencies = [ + "generic-array 0.14.4", + "subtle", +] + +[[package]] +name = "digest" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f3d0c8c8752312f9713efd397ff63acb9f85585afbf179282e720e7704954dd5" +dependencies = [ + "generic-array 0.12.4", +] + +[[package]] +name = "digest" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3dd60d1080a57a05ab032377049e0591415d2b31afd7028356dbf3cc6dcb066" +dependencies = [ + "generic-array 0.14.4", +] + +[[package]] +name = "ethabi" +version = "13.0.0" +source = "git+https://github.com/darwinia-network/ethabi?branch=xavier-no-std#baacf174d5c2f12122c4ee3fe31506fb52069983" +dependencies = [ + "anyhow", + "ethereum-types", + "hex", + "sha3 0.9.1", + "uint", +] + +[[package]] +name = "ethbloom" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "779864b9c7f7ead1f092972c3257496c6a84b46dba2ce131dd8a282cb2cc5972" +dependencies = [ + "crunchy", + "fixed-hash", + "impl-rlp", + "tiny-keccak", +] + +[[package]] +name = "ethereum" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "567ce064a8232c16e2b2c2173a936b91fbe35c2f2c5278871f5a1a31688b42e9" +dependencies = [ + "ethereum-types", + "funty", + "hash-db", + "hash256-std-hasher", + "rlp", + "rlp-derive", + "sha3 0.9.1", + "triehash", +] + +[[package]] +name = "ethereum-types" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f64b5df66a228d85e4b17e5d6c6aa43b0310898ffe8a85988c4c032357aaabfd" +dependencies = [ + "ethbloom", + "fixed-hash", + "impl-rlp", + "primitive-types", + "uint", +] + +[[package]] +name = "evm" +version = "0.26.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1202e8dfa45bea73ee003c4be2f3549d60cb2e2ac5b0c1db563bd4653213efde" +dependencies = [ + "ethereum", + "evm-core", + "evm-gasometer", + "evm-runtime", + "log", + "primitive-types", + "rlp", + "sha3 0.8.2", +] + +[[package]] +name = "evm-core" +version = "0.26.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0f235e93b84fccc1ebdffad226dc56caf833de2fb2f3395f933d95fbf66b254e" +dependencies = [ + "funty", + "primitive-types", +] + +[[package]] +name = "evm-gasometer" +version = "0.26.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "463412356790c5e34e8a13cd23ba06284d9afa999e82e8c64497aed2ee625375" +dependencies = [ + "evm-core", + "evm-runtime", + "primitive-types", +] + +[[package]] +name = "evm-runtime" +version = "0.26.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c08f510e5535cee2352adb9b93ff24dc80f8ca1bad445a9aa1292ce144b45a1" +dependencies = [ + "evm-core", + "primitive-types", + "sha3 0.8.2", +] + +[[package]] +name = "fixed-hash" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cfcf0ed7fe52a17a03854ec54a9f76d6d84508d1c0e66bc1793301c73fc8493c" +dependencies = [ + "byteorder", + "rustc-hex", + "static_assertions", +] + +[[package]] +name = "funty" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fed34cd105917e91daa4da6b3728c47b068749d6a62c59811f06ed2ac71d9da7" + +[[package]] +name = "generic-array" +version = "0.12.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ffdf9f34f1447443d37393cc6c2b8313aebddcd96906caf34e54c68d8e57d7bd" +dependencies = [ + "typenum", +] + +[[package]] +name = "generic-array" +version = "0.14.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "501466ecc8a30d1d3b7fc9229b122b2ce8ed6e9d9223f1138d4babb253e51817" +dependencies = [ + "typenum", + "version_check", +] + +[[package]] +name = "getrandom" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c9495705279e7140bf035dde1f6e750c162df8b625267cd52cc44e0b156732c8" +dependencies = [ + "cfg-if 1.0.0", + "js-sys", + "libc", + "wasi", + "wasm-bindgen", +] + +[[package]] +name = "hash-db" +version = "0.15.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d23bd4e7b5eda0d0f3a307e8b381fdc8ba9000f26fbe912250c0a4cc3956364a" + +[[package]] +name = "hash256-std-hasher" +version = "0.15.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92c171d55b98633f4ed3860808f004099b36c1cc29c42cfc53aa8591b21efcf2" +dependencies = [ + "crunchy", +] + +[[package]] +name = "hashbrown" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d7afe4a420e3fe79967a00898cc1f4db7c8a49a9333a29f8a4bd76a253d5cd04" +dependencies = [ + "ahash", +] + +[[package]] +name = "hex" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" + +[[package]] +name = "impl-rlp" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f28220f89297a075ddc7245cd538076ee98b01f2a9c23a53a4f1105d5a322808" +dependencies = [ + "rlp", +] + +[[package]] +name = "js-sys" +version = "0.3.51" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "83bdfbace3a0e81a4253f73b49e960b053e396a11012cbd49b9b74d6a2b67062" +dependencies = [ + "wasm-bindgen", +] + +[[package]] +name = "keccak" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67c21572b4949434e4fc1e1978b99c5f77064153c59d998bf13ecd96fb5ecba7" + +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + +[[package]] +name = "libc" +version = "0.2.94" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "18794a8ad5b29321f790b55d93dfba91e125cb1a9edbd4f8e3150acc771c1a5e" + +[[package]] +name = "libsecp256k1" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fc1e2c808481a63dc6da2074752fdd4336a3c8fcc68b83db6f1fd5224ae7962" +dependencies = [ + "arrayref", + "crunchy", + "digest 0.8.1", + "rand", + "subtle", +] + +[[package]] +name = "log" +version = "0.4.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "51b9bbe6c47d51fc3e1a9b945965946b4c44142ab8792c50835a980d362c2710" +dependencies = [ + "cfg-if 1.0.0", +] + +[[package]] +name = "logos" +version = "0.7.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60ca690691528b32832c7e8aaae8ae1edcdee4e9ffde55b2d31a4795bc7a12d0" +dependencies = [ + "logos-derive", +] + +[[package]] +name = "logos-derive" +version = "0.7.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "917dccdd529d5681f3d28b26bcfdafd2ed67fe4f26d15b5ac679f67b55279f3d" +dependencies = [ + "proc-macro2 0.4.30", + "quote 0.6.13", + "regex-syntax", + "syn 0.15.44", + "utf8-ranges", +] + +[[package]] +name = "lunarity-lexer" +version = "0.2.1" +source = "git+https://github.com/ilblackdragon/lunarity?rev=5201d9a76f7e491082b7f74af7e64049271e387f#5201d9a76f7e491082b7f74af7e64049271e387f" +dependencies = [ + "logos", +] + +[[package]] +name = "memory_units" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8452105ba047068f40ff7093dd1d9da90898e63dd61736462e9cdda6a90ad3c3" + +[[package]] +name = "num" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43db66d1170d347f9a065114077f7dccb00c1b9478c89384490a3425279a4606" +dependencies = [ + "num-bigint", + "num-complex", + "num-integer", + "num-iter", + "num-rational", + "num-traits", +] + +[[package]] +name = "num-bigint" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e0d047c1062aa51e256408c560894e5251f08925980e53cf1aa5bd00eec6512" +dependencies = [ + "autocfg", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-complex" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26873667bbbb7c5182d4a37c1add32cdf09f841af72da53318fdb81543c15085" +dependencies = [ + "num-traits", +] + +[[package]] +name = "num-integer" +version = "0.1.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2cc698a63b549a70bc047073d2949cce27cd1c7b0a4a862d08a8031bc2801db" +dependencies = [ + "autocfg", + "num-traits", +] + +[[package]] +name = "num-iter" +version = "0.1.42" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b2021c8337a54d21aca0d59a92577a029af9431cb59b909b03252b9c164fad59" +dependencies = [ + "autocfg", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-rational" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d41702bd167c2df5520b384281bc111a4b5efcf7fbc4c9c222c815b07e0a6a6a" +dependencies = [ + "autocfg", + "num-bigint", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a64b1ec5cda2586e284722486d802acf1f7dbdc623e2bfc57e65ca1cd099290" +dependencies = [ + "autocfg", +] + +[[package]] +name = "opaque-debug" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2839e79665f131bdb5782e51f2c6c9599c133c6098982a54c794358bf432529c" + +[[package]] +name = "opaque-debug" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5" + +[[package]] +name = "ppv-lite86" +version = "0.2.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857" + +[[package]] +name = "primitive-types" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2415937401cb030a2a0a4d922483f945fa068f52a7dbb22ce0fe5f2b6f6adace" +dependencies = [ + "fixed-hash", + "impl-rlp", + "uint", +] + +[[package]] +name = "proc-macro-crate" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d6ea3c4595b96363c13943497db34af4460fb474a95c43f4446ad341b8c9785" +dependencies = [ + "toml", +] + +[[package]] +name = "proc-macro2" +version = "0.4.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf3d2011ab5c909338f7887f4fc896d35932e29146c12c8d01da6b22a80ba759" +dependencies = [ + "unicode-xid 0.1.0", +] + +[[package]] +name = "proc-macro2" +version = "1.0.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a152013215dca273577e18d2bf00fa862b89b24169fb78c4c95aeb07992c9cec" +dependencies = [ + "unicode-xid 0.2.2", +] + +[[package]] +name = "quote" +version = "0.6.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ce23b6b870e8f94f81fb0a363d65d86675884b34a09043c81e5562f11c1f8e1" +dependencies = [ + "proc-macro2 0.4.30", +] + +[[package]] +name = "quote" +version = "1.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3d0b9745dc2debf507c8422de05d7226cc1f0644216dfdfead988f9b1ab32a7" +dependencies = [ + "proc-macro2 1.0.26", +] + +[[package]] +name = "rand" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03" +dependencies = [ + "rand_chacha", + "rand_core", + "rand_hc", +] + +[[package]] +name = "rand_chacha" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f4c8ed856279c9737206bf725bf36935d8666ead7aa69b52be55af369d193402" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19" + +[[package]] +name = "rand_hc" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c" +dependencies = [ + "rand_core", +] + +[[package]] +name = "regex-syntax" +version = "0.6.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b" + +[[package]] +name = "ripemd160" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2eca4ecc81b7f313189bf73ce724400a07da2a6dac19588b03c8bd76a2dcc251" +dependencies = [ + "block-buffer 0.9.0", + "digest 0.9.0", + "opaque-debug 0.3.0", +] + +[[package]] +name = "rjson" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5510dbde48c4c37bf69123b1f636b6dd5f8dffe1f4e358af03c46a4947dca219" + +[[package]] +name = "rlp" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e54369147e3e7796c9b885c7304db87ca3d09a0a98f72843d532868675bbfba8" +dependencies = [ + "bytes", + "rustc-hex", +] + +[[package]] +name = "rlp-derive" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e33d7b2abe0c340d8797fe2907d3f20d3b5ea5908683618bfe80df7f621f672a" +dependencies = [ + "proc-macro2 1.0.26", + "quote 1.0.9", + "syn 1.0.72", +] + +[[package]] +name = "rustc-hex" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3e75f6a532d0fd9f7f13144f392b6ad56a32696bfcd9c78f797f16bbb6f072d6" + +[[package]] +name = "serde" +version = "1.0.126" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec7505abeacaec74ae4778d9d9328fe5a5d04253220a85c4ee022239fc996d03" + +[[package]] +name = "sha2" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b362ae5752fd2137731f9fa25fd4d9058af34666ca1966fb969119cc35719f12" +dependencies = [ + "block-buffer 0.9.0", + "cfg-if 1.0.0", + "cpufeatures", + "digest 0.9.0", + "opaque-debug 0.3.0", +] + +[[package]] +name = "sha3" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd26bc0e7a2e3a7c959bc494caf58b72ee0c71d67704e9520f736ca7e4853ecf" +dependencies = [ + "block-buffer 0.7.3", + "byte-tools", + "digest 0.8.1", + "keccak", + "opaque-debug 0.2.3", +] + +[[package]] +name = "sha3" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f81199417d4e5de3f04b1e871023acea7389672c4135918f05aa9cbf2f2fa809" +dependencies = [ + "block-buffer 0.9.0", + "digest 0.9.0", + "keccak", + "opaque-debug 0.3.0", +] + +[[package]] +name = "static_assertions" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" + +[[package]] +name = "subtle" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e81da0851ada1f3e9d4312c704aa4f8806f0f9d69faaf8df2f3464b4a9437c2" + +[[package]] +name = "syn" +version = "0.15.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ca4b3b69a77cbe1ffc9e198781b7acb0c7365a883670e8f1c1bc66fba79a5c5" +dependencies = [ + "proc-macro2 0.4.30", + "quote 0.6.13", + "unicode-xid 0.1.0", +] + +[[package]] +name = "syn" +version = "1.0.72" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1e8cdbefb79a9a5a65e0db8b47b723ee907b7c7f8496c76a1770b5c310bab82" +dependencies = [ + "proc-macro2 1.0.26", + "quote 1.0.9", + "unicode-xid 0.2.2", +] + +[[package]] +name = "tiny-keccak" +version = "2.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2c9d3793400a45f954c52e73d068316d76b6f4e36977e3fcebb13a2721e80237" +dependencies = [ + "crunchy", +] + +[[package]] +name = "toml" +version = "0.5.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a31142970826733df8241ef35dc040ef98c679ab14d7c3e54d827099b3acecaa" +dependencies = [ + "serde", +] + +[[package]] +name = "triehash" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1631b201eb031b563d2e85ca18ec8092508e262a3196ce9bd10a67ec87b9f5c" +dependencies = [ + "hash-db", + "rlp", +] + +[[package]] +name = "typenum" +version = "1.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "879f6906492a7cd215bfa4cf595b600146ccfac0c79bcbd1f3000162af5e8b06" + +[[package]] +name = "uint" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e11fe9a9348741cf134085ad57c249508345fe16411b3d7fb4ff2da2f1d6382e" +dependencies = [ + "byteorder", + "crunchy", + "hex", + "static_assertions", +] + +[[package]] +name = "unicode-xid" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc" + +[[package]] +name = "unicode-xid" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" + +[[package]] +name = "utf8-ranges" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b4ae116fef2b7fea257ed6440d3cfcff7f190865f170cdad00bb6465bf18ecba" + +[[package]] +name = "version_check" +version = "0.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5fecdca9a5291cc2b8dcf7dc02453fee791a280f3743cb0905f8822ae463b3fe" + +[[package]] +name = "wasi" +version = "0.10.2+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6" + +[[package]] +name = "wasm-bindgen" +version = "0.2.74" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d54ee1d4ed486f78874278e63e4069fc1ab9f6a18ca492076ffb90c5eb2997fd" +dependencies = [ + "cfg-if 1.0.0", + "wasm-bindgen-macro", +] + +[[package]] +name = "wasm-bindgen-backend" +version = "0.2.74" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b33f6a0694ccfea53d94db8b2ed1c3a8a4c86dd936b13b9f0a15ec4a451b900" +dependencies = [ + "bumpalo", + "lazy_static", + "log", + "proc-macro2 1.0.26", + "quote 1.0.9", + "syn 1.0.72", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.74" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "088169ca61430fe1e58b8096c24975251700e7b1f6fd91cc9d59b04fb9b18bd4" +dependencies = [ + "quote 1.0.9", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.74" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "be2241542ff3d9f241f5e2cb6dd09b37efe786df8851c54957683a49f0987a97" +dependencies = [ + "proc-macro2 1.0.26", + "quote 1.0.9", + "syn 1.0.72", + "wasm-bindgen-backend", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.74" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d7cff876b8f18eed75a66cf49b65e7f967cb354a7aa16003fb55dbfd25b44b4f" + +[[package]] +name = "wee_alloc" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dbb3b5a6b2bb17cb6ad44a2e68a43e8d2722c997da10e928665c72ec6c0a0b8e" +dependencies = [ + "cfg-if 0.1.10", + "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/etc/state-migration-test/Cargo.toml b/etc/state-migration-test/Cargo.toml new file mode 100644 index 000000000..f3a8cf82a --- /dev/null +++ b/etc/state-migration-test/Cargo.toml @@ -0,0 +1,41 @@ +[package] +name = "aurora-engine-state-migration-test" +version = "1.0.0" +authors = ["NEAR "] +edition = "2018" +description = "" +documentation = "" +readme = true +homepage = "https://github.com/aurora-is-near/aurora-engine" +repository = "https://github.com/aurora-is-near/aurora-engine" +license = "CC0-1.0" +publish = false + +[lib] +crate-type = ["cdylib", "rlib"] + +[profile.release] +opt-level = "z" +debug = false +debug-assertions = false +overflow-checks = true +lto = true +panic = "abort" +incremental = false +codegen-units = 1 +rpath = false + +[profile.dev] +opt-level = "z" +debug = false +debug-assertions = true +overflow-checks = true +lto = true +panic = "abort" +incremental = false +codegen-units = 1 +rpath = false + +[dependencies] +borsh = { version = "0.8.2", default-features = false } +aurora-engine = { path = "../../", default-features = false, features = ["engine", "sha2"] } diff --git a/etc/state-migration-test/src/lib.rs b/etc/state-migration-test/src/lib.rs new file mode 100644 index 000000000..a0e0a8609 --- /dev/null +++ b/etc/state-migration-test/src/lib.rs @@ -0,0 +1,42 @@ +#![no_std] + +extern crate alloc; + +use alloc::vec::Vec; +use aurora_engine::engine::{Engine, EngineState}; +use aurora_engine::{sdk, storage}; +use borsh::{BorshDeserialize, BorshSerialize}; + +#[derive(BorshDeserialize, BorshSerialize)] +struct NewFancyState { + old_state: EngineState, + some_other_numbers: [u32; 7], +} + +#[no_mangle] +pub extern "C" fn state_migration() { + let old_state = match Engine::get_state() { + Ok(state) => state, + Err(e) => sdk::panic_utf8(e.as_ref()), + }; + + let new_state = NewFancyState { + old_state, + some_other_numbers: [3, 1, 4, 1, 5, 9, 2], + }; + + sdk::write_storage(&state_key(), &new_state.try_to_vec().expect("ERR_SER")); +} + +#[no_mangle] +pub extern "C" fn some_new_facy_function() { + let state = sdk::read_storage(&state_key()) + .and_then(|bytes| NewFancyState::try_from_slice(&bytes).ok()) + .unwrap(); + + sdk::return_output(&state.some_other_numbers.try_to_vec().unwrap()); +} + +fn state_key() -> Vec { + storage::bytes_to_key(storage::KeyPrefix::Config, b"STATE") +} diff --git a/src/lib.rs b/src/lib.rs index dfecbcd7f..ae1232d33 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -24,7 +24,7 @@ mod json; mod log_entry; mod precompiles; #[cfg(feature = "engine")] -mod sdk; +pub mod sdk; #[cfg(test)] mod benches; From db606ef045a779717cce034d87cc06f293d9f399 Mon Sep 17 00:00:00 2001 From: Michael Birch Date: Thu, 20 May 2021 14:21:27 -0400 Subject: [PATCH 4/6] Add test for state migration --- etc/state-migration-test/src/lib.rs | 2 +- tests/test_state_migration.rs | 115 ++++++++++++++++++++++++++++ tests/test_upgrade.rs | 55 ------------- 3 files changed, 116 insertions(+), 56 deletions(-) create mode 100644 tests/test_state_migration.rs delete mode 100644 tests/test_upgrade.rs diff --git a/etc/state-migration-test/src/lib.rs b/etc/state-migration-test/src/lib.rs index a0e0a8609..89782d496 100644 --- a/etc/state-migration-test/src/lib.rs +++ b/etc/state-migration-test/src/lib.rs @@ -29,7 +29,7 @@ pub extern "C" fn state_migration() { } #[no_mangle] -pub extern "C" fn some_new_facy_function() { +pub extern "C" fn some_new_fancy_function() { let state = sdk::read_storage(&state_key()) .and_then(|bytes| NewFancyState::try_from_slice(&bytes).ok()) .unwrap(); diff --git a/tests/test_state_migration.rs b/tests/test_state_migration.rs new file mode 100644 index 000000000..ecbdae253 --- /dev/null +++ b/tests/test_state_migration.rs @@ -0,0 +1,115 @@ +use aurora_engine::parameters::NewCallArgs; +use aurora_engine::prelude::U256; +use aurora_engine::types; +use borsh::BorshSerialize; +use near_sdk_sim::{ExecutionResult, UserAccount}; +use std::fs; +use std::path::Path; +use std::process::Command; + +// TODO: it would be nice to include this under src/tests but right now this is not possible. +// The issue is a linker error (arising from multiple dependencies on near-vm-logic I think). + +near_sdk_sim::lazy_static_include::lazy_static_include_bytes! { + EVM_WASM_BYTES => "release.wasm" +} + +#[test] +fn test_state_migration() { + let aurora = deploy_evm(); + + // do upgrade + let upgraded_contract_bytes = contract_bytes(); + aurora + .call("stage_upgrade", &upgraded_contract_bytes) + .assert_success(); + aurora.call("deploy_upgrade", &[]).assert_success(); + + // upgraded contract as some_new_fancy_function + let result = aurora.call("some_new_fancy_function", &[]); + result.assert_success(); + let some_numbers: [u32; 7] = result.unwrap_borsh(); + assert_eq!(some_numbers, [3, 1, 4, 1, 5, 9, 2]); +} + +fn deploy_evm() -> AuroraAccount { + let aurora_config = AuroraConfig::default(); + let main_account = near_sdk_sim::init_simulator(None); + let contract_account = main_account.deploy( + &aurora_config.code, + aurora_config.account_id.clone(), + 5 * near_sdk_sim::STORAGE_AMOUNT, + ); + let new_args = NewCallArgs { + chain_id: types::u256_to_arr(&U256::from(aurora_config.chain_id)), + owner_id: main_account.account_id.clone(), + bridge_prover_id: "prover.near".to_string(), + upgrade_delay_blocks: 1, + }; + main_account + .call( + contract_account.account_id.clone(), + "new", + &new_args.try_to_vec().unwrap(), + near_sdk_sim::DEFAULT_GAS, + 0, + ) + .assert_success(); + AuroraAccount { + user: main_account, + contract: contract_account, + } +} + +struct AuroraAccount { + user: UserAccount, + contract: UserAccount, +} + +impl AuroraAccount { + fn call(&self, method: &str, args: &[u8]) -> ExecutionResult { + self.user.call( + self.contract.account_id.clone(), + method, + args, + near_sdk_sim::DEFAULT_GAS, + 0, + ) + } +} + +struct AuroraConfig { + code: Vec, + chain_id: u64, + account_id: String, +} + +impl Default for AuroraConfig { + fn default() -> Self { + Self { + code: EVM_WASM_BYTES.to_vec(), + chain_id: 1313161556, // NEAR betanet + account_id: "aurora".to_string(), + } + } +} + +fn contract_bytes() -> Vec { + let base_path = Path::new("etc").join("state-migration-test"); + let output_path = base_path + .join("target/wasm32-unknown-unknown/release/aurora_engine_state_migration_test.wasm"); + compile(base_path); + fs::read(output_path).unwrap() +} + +fn compile>(source_path: P) { + let output = Command::new("cargo") + .current_dir(source_path) + .args(&["build", "--target", "wasm32-unknown-unknown", "--release"]) + .output() + .unwrap(); + + if !output.status.success() { + panic!("{}", String::from_utf8(output.stderr).unwrap()); + } +} diff --git a/tests/test_upgrade.rs b/tests/test_upgrade.rs deleted file mode 100644 index ac536143a..000000000 --- a/tests/test_upgrade.rs +++ /dev/null @@ -1,55 +0,0 @@ -use near_sdk::borsh::BorshSerialize; -use near_sdk::test_utils::accounts; -use near_sdk_sim::{to_yocto, UserAccount, DEFAULT_GAS, STORAGE_AMOUNT}; - -use aurora_engine::parameters::NewCallArgs; - -near_sdk_sim::lazy_static_include::lazy_static_include_bytes! { - EVM_WASM_BYTES => "release.wasm" -} - -fn init() -> (UserAccount, UserAccount) { - let master_account = near_sdk_sim::init_simulator(None); - let contract_account = - master_account.deploy(*EVM_WASM_BYTES, accounts(0).to_string(), to_yocto("1000")); - contract_account - .call( - accounts(0).to_string(), - "new", - &NewCallArgs { - chain_id: [0u8; 32], - owner_id: master_account.account_id.clone(), - bridge_prover_id: accounts(0).to_string(), - upgrade_delay_blocks: 1, - } - .try_to_vec() - .unwrap(), - DEFAULT_GAS, - STORAGE_AMOUNT, - ) - .assert_success(); - (master_account, contract_account) -} - -#[test] -fn test_contract_upgrade() { - let (master_account, _contract_account) = init(); - master_account - .call( - accounts(0).to_string(), - "stage_upgrade", - &EVM_WASM_BYTES, - DEFAULT_GAS, - 0, - ) - .assert_success(); - master_account - .call( - accounts(0).to_string(), - "deploy_upgrade", - &[], - DEFAULT_GAS, - 0, - ) - .assert_success(); -} From 208de2d0297e0658c680feea697bf5ff953bc25b Mon Sep 17 00:00:00 2001 From: Michael Birch Date: Tue, 25 May 2021 13:54:34 +0000 Subject: [PATCH 5/6] Increase GAS_FOR_STATE_MIGRATION to 256 Tgas --- src/sdk.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sdk.rs b/src/sdk.rs index 117ca99b0..eed9cfcd3 100644 --- a/src/sdk.rs +++ b/src/sdk.rs @@ -4,7 +4,7 @@ use borsh::{BorshDeserialize, BorshSerialize}; const READ_STORAGE_REGISTER_ID: u64 = 0; const INPUT_REGISTER_ID: u64 = 0; -const GAS_FOR_STATE_MIGRATION: u64 = 100_000_000_000_000; +const GAS_FOR_STATE_MIGRATION: u64 = 250_000_000_000_000; mod exports { From d14fe517cef05e41b22b85d32c6ff150fbeb7237 Mon Sep 17 00:00:00 2001 From: Michael Birch Date: Tue, 25 May 2021 14:01:29 +0000 Subject: [PATCH 6/6] Move some code --- src/lib.rs | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index aa414a347..27b734a2d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -32,6 +32,24 @@ mod test_utils; #[cfg(test)] mod tests; +#[cfg(target_arch = "wasm32")] +#[global_allocator] +static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; + +#[cfg(target_arch = "wasm32")] +#[panic_handler] +#[no_mangle] +pub unsafe fn on_panic(_info: &::core::panic::PanicInfo) -> ! { + ::core::arch::wasm32::unreachable(); +} + +#[cfg(target_arch = "wasm32")] +#[alloc_error_handler] +#[no_mangle] +pub unsafe fn on_alloc_error(_: core::alloc::Layout) -> ! { + ::core::arch::wasm32::unreachable(); +} + #[cfg(feature = "contract")] mod contract { use borsh::BorshSerialize; @@ -414,21 +432,3 @@ mod contract { } } } - -#[cfg(target_arch = "wasm32")] -#[global_allocator] -static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; - -#[cfg(target_arch = "wasm32")] -#[panic_handler] -#[no_mangle] -pub unsafe fn on_panic(_info: &::core::panic::PanicInfo) -> ! { - ::core::arch::wasm32::unreachable(); -} - -#[cfg(target_arch = "wasm32")] -#[alloc_error_handler] -#[no_mangle] -pub unsafe fn on_alloc_error(_: core::alloc::Layout) -> ! { - ::core::arch::wasm32::unreachable(); -}