Repository: stellar/rs-soroban-env
Pull request #1181 (2023-11-07 20:25:54)

Remove vnext test wasms
### What

Remove vnext test wasms to allow core to test multiple protocol versions. This only works at the moment because V20 test wasms are compatible with V21, but this may not be the case in the future. At some point we may need test wasms organized by protocol version (not just curr vs vnext).


Patch:
diff --git a/soroban-env-host/Cargo.toml b/soroban-env-host/Cargo.toml
index a3b747f1..26611e3c 100644
--- a/soroban-env-host/Cargo.toml
+++ b/soroban-env-host/Cargo.toml
@@ -57,7 +57,7 @@ pretty_assertions = "1.4.0"
 
 [features]
 testutils = ["soroban-env-common/testutils", "soroban-synth-wasm/testutils", "recording_auth"]
-next = ["soroban-env-common/next", "soroban-test-wasms/next", "soroban-synth-wasm/next", "soroban-bench-utils/next"]
+next = ["soroban-env-common/next", "soroban-synth-wasm/next", "soroban-bench-utils/next"]
 tracy = ["dep:tracy-client", "soroban-env-common/tracy"]
 recording_auth = []
 
diff --git a/soroban-env-host/src/vm.rs b/soroban-env-host/src/vm.rs
index 0c065252..e4d96b66 100644
--- a/soroban-env-host/src/vm.rs
+++ b/soroban-env-host/src/vm.rs
@@ -28,7 +28,7 @@ use super::{xdr::Hash, Host, Symbol, Val};
 use fuel_refillable::FuelRefillable;
 use func_info::HOST_FUNCTIONS;
 use soroban_env_common::{
-    meta::{self, get_ledger_protocol_version, get_pre_release_version},
+    meta::{self, get_ledger_protocol_version},
     xdr::{
         DepthLimitedRead, ReadXdr, ScEnvMetaEntry, ScErrorCode, ScErrorType,
         DEFAULT_XDR_RW_DEPTH_LIMIT,
@@ -96,12 +96,22 @@ impl Vm {
             }
         };
 
-        let got_pre = get_pre_release_version(interface_version);
+        // Not used when "next" is enabled
+        #[cfg(not(feature = "next"))]
+        let got_pre = meta::get_pre_release_version(interface_version);
+
         let got_proto = get_ledger_protocol_version(interface_version);
 
         if got_proto < want_proto {
             // Old protocols are finalized, we only support contracts
             // with similarly finalized (zero) prerelease numbers.
+            //
+            // Note that we only enable this check if the "next" feature isn't enabled
+            // because a "next" stellar-core can still run a "curr" test using non-finalized
+            // test wasms. The "next" feature isn't safe for production and is meant to
+            // simulate the protocol version after the one currently supported in
+            // stellar-core, so bypassing this check for "next" is safe.
+            #[cfg(not(feature = "next"))]
             if got_pre != 0 {
                 return Err(err!(
                     host,
@@ -111,17 +121,23 @@ impl Vm {
                 ));
             }
         } else if got_proto == want_proto {
-            // Current protocol might have a nonzero prerelease number; we will
-            // allow it only if it matches the current prerelease exactly.
-            let want_pre = get_pre_release_version(meta::INTERFACE_VERSION);
-            if want_pre != got_pre {
-                return Err(err!(
-                    host,
-                    (ScErrorType::WasmVm, ScErrorCode::InvalidInput),
-                    "contract pre-release number for current protocol does not match host",
-                    got_pre,
-                    want_pre
-                ));
+            // Relax this check as well for the "next" feature to allow for flexibility while testing.
+            // stellar-core can pass in an older protocol version, in which case the pre-release version
+            // will not match up with the "next" feature (The "next" pre-release version is always 1).
+            #[cfg(not(feature = "next"))]
+            {
+                // Current protocol might have a nonzero prerelease number; we will
+                // allow it only if it matches the current prerelease exactly.
+                let want_pre = meta::get_pre_release_version(meta::INTERFACE_VERSION);
+                if want_pre != got_pre {
+                    return Err(err!(
+                        host,
+                        (ScErrorType::WasmVm, ScErrorCode::InvalidInput),
+                        "contract pre-release number for current protocol does not match host",
+                        got_pre,
+                        want_pre
+                    ));
+                }
             }
         } else {
             // Future protocols we don't allow. It might be nice (in the sense


Test patch:
diff --git a/soroban-env-host/src/test/lifecycle.rs b/soroban-env-host/src/test/lifecycle.rs
index 7569f4a3..8bdc2037 100644
--- a/soroban-env-host/src/test/lifecycle.rs
+++ b/soroban-env-host/src/test/lifecycle.rs
@@ -32,7 +32,7 @@ fn get_contract_wasm_ref(host: &Host, contract_id: Hash) -> Hash {
             LedgerEntryData::ContractData(e) => match &e.val {
                 ScVal::ContractInstance(i) => match &i.executable {
                     ContractExecutable::Wasm(h) => Ok(h.clone()),
-                    _ => panic!("expectecd Wasm executable"),
+                    _ => panic!("expected Wasm executable"),
                 },
                 _ => panic!("expected ContractInstance"),
             },
@@ -412,3 +412,16 @@ fn test_create_contract_from_source_account_recording_auth() {
         }]
     );
 }
+
+#[test]
+fn test_invalid_contract() {
+    let host = Host::test_host_with_recording_footprint();
+
+    let bytes = [0u8, 32];
+
+    assert!(host
+        .invoke_function(HostFunction::UploadContractWasm(
+            bytes.to_vec().try_into().unwrap(),
+        ))
+        .is_err());
+}
diff --git a/soroban-test-wasms/Cargo.toml b/soroban-test-wasms/Cargo.toml
index d581a24b..3149338b 100644
--- a/soroban-test-wasms/Cargo.toml
+++ b/soroban-test-wasms/Cargo.toml
@@ -4,7 +4,4 @@ version.workspace = true
 description = "crate full of precompiled test WASM binaries for soroban -- for use in other tests, do not publish"
 edition = "2021"
 publish = false
-rust-version = "1.71"
-
-[features]
-next = []
\ No newline at end of file
+rust-version = "1.71"
\ No newline at end of file
diff --git a/soroban-test-wasms/src/lib.rs b/soroban-test-wasms/src/lib.rs
index 241745ed..9e5cb71f 100644
--- a/soroban-test-wasms/src/lib.rs
+++ b/soroban-test-wasms/src/lib.rs
@@ -42,10 +42,8 @@
 //!      documentation purpopses without having to worry about breaking tests in
 //!      the host here.
 
-#[cfg(not(feature = "next"))]
 pub use curr::*;
 
-#[cfg(not(feature = "next"))]
 mod curr {
     pub const ADD_I32: &[u8] =
         include_bytes!("../wasm-workspace/opt/curr/example_add_i32.wasm").as_slice();
@@ -86,49 +84,3 @@ mod curr {
     pub const SAC_REENTRY_TEST_CONTRACT: &[u8] =
         include_bytes!("../wasm-workspace/opt/curr/sac_reentry_account.wasm").as_slice();
 }
-
-#[cfg(feature = "next")]
-pub use next::*;
-
-#[cfg(feature = "next")]
-mod next {
-    pub const ADD_I32: &[u8] =
-        include_bytes!("../wasm-workspace/opt/next/example_add_i32.wasm").as_slice();
-    pub const ADD_F32: &[u8] =
-        include_bytes!("../wasm-workspace/opt/next/example_add_f32.wasm").as_slice();
-    pub const ALLOC: &[u8] =
-        include_bytes!("../wasm-workspace/opt/next/example_alloc.wasm").as_slice();
-    pub const CREATE_CONTRACT: &[u8] =
-        include_bytes!("../wasm-workspace/opt/next/example_create_contract.wasm").as_slice();
-    pub const CONTRACT_STORAGE: &[u8] =
-        include_bytes!("../wasm-workspace/opt/next/example_contract_data.wasm").as_slice();
-    pub const LINEAR_MEMORY: &[u8] =
-        include_bytes!("../wasm-workspace/opt/next/example_linear_memory.wasm").as_slice();
-    pub const VEC: &[u8] = include_bytes!("../wasm-workspace/opt/next/example_vec.wasm").as_slice();
-    pub const INVOKE_CONTRACT: &[u8] =
-        include_bytes!("../wasm-workspace/opt/next/example_invoke_contract.wasm").as_slice();
-    pub const HOSTILE: &[u8] =
-        include_bytes!("../wasm-workspace/opt/next/example_hostile.wasm").as_slice();
-    pub const FIB: &[u8] = include_bytes!("../wasm-workspace/opt/next/example_fib.wasm").as_slice();
-    pub const FANNKUCH: &[u8] =
-        include_bytes!("../wasm-workspace/opt/next/example_fannkuch.wasm").as_slice();
-    pub const COMPLEX: &[u8] =
-        include_bytes!("../wasm-workspace/opt/next/example_complex.wasm").as_slice();
-    pub const SIMPLE_ACCOUNT_CONTRACT: &[u8] =
-        include_bytes!("../wasm-workspace/opt/next/example_simple_account.wasm").as_slice();
-    pub const AUTH_TEST_CONTRACT: &[u8] =
-        include_bytes!("../wasm-workspace/opt/next/auth_test_contract.wasm").as_slice();
-    pub const UPDATEABLE_CONTRACT: &[u8] =
-        include_bytes!("../wasm-workspace/opt/next/example_updateable_contract.wasm").as_slice();
-    pub const DELEGATED_ACCOUNT_TEST_CONTRACT: &[u8] =
-        include_bytes!("../wasm-workspace/opt/next/test_delegated_account.wasm").as_slice();
-    pub const ERR: &[u8] = include_bytes!("../wasm-workspace/opt/next/example_err.wasm").as_slice();
-    pub const WRITE_BYTES: &[u8] =
-        include_bytes!("../wasm-workspace/opt/next/soroban_write_upgrade_bytes_contract.wasm")
-            .as_slice();
-    pub const CONDITIONAL_ACCOUNT_TEST_CONTRACT: &[u8] =
-        include_bytes!("../wasm-workspace/opt/next/test_conditional_account.wasm").as_slice();
-
-    pub const SAC_REENTRY_TEST_CONTRACT: &[u8] =
-        include_bytes!("../wasm-workspace/opt/next/sac_reentry_account.wasm").as_slice();
-}
diff --git a/soroban-test-wasms/wasm-workspace/Makefile b/soroban-test-wasms/wasm-workspace/Makefile
index 9a0a6278..8a716bfd 100644
--- a/soroban-test-wasms/wasm-workspace/Makefile
+++ b/soroban-test-wasms/wasm-workspace/Makefile
@@ -11,15 +11,4 @@ regenerate-test-wasms:
 	for i in target/wasm32-unknown-unknown/release/*.wasm ; do \
 		wasm-opt -Oz "$$i" -o "opt/curr/$$(basename $$i)"; \
 		ls -l "opt/curr/$$(basename $$i)"; \
-	done
-	cargo clean
-	cargo +nightly build \
-		--target wasm32-unknown-unknown \
-		--release \
-		--features next \
-		-Z build-std=std,panic_abort \
-		-Z build-std-features=panic_immediate_abort
-	for i in target/wasm32-unknown-unknown/release/*.wasm ; do \
-		wasm-opt -Oz "$$i" -o "opt/next/$$(basename $$i)"; \
-		ls -l "opt/next/$$(basename $$i)"; \
-	done
+	done
\ No newline at end of file