contracts: Remove wat support from test fixtures (#3588)

In order to prepare for PolkaVM support I removed the wat support from
our test fixture crate.

- Removed redundant tests (invalid module checks are already inside the
prepare module where they belong
- Converted the gas_sync tests to Rust
- Moved the start function test to the `wasm` module
This commit is contained in:
Alexander Theißen
2024-03-07 09:05:04 +08:00
committed by GitHub
parent 117a9433da
commit 4ae7398818
12 changed files with 49 additions and 283 deletions
+5 -36
View File
@@ -16,34 +16,7 @@
// limitations under the License.
use sp_runtime::traits::Hash;
use std::{env::var, fs, path::PathBuf};
fn wat_root_dir() -> PathBuf {
match (var("CARGO_MANIFEST_DIR"), var("CARGO_PKG_NAME")) {
// When `CARGO_MANIFEST_DIR` is not set, Rust resolves relative paths from the root folder
(Err(_), _) => "substrate/frame/contracts/fixtures/data".into(),
(Ok(path), Ok(s)) if s == "pallet-contracts" => PathBuf::from(path).join("fixtures/data"),
(Ok(path), Ok(s)) if s == "pallet-contracts-mock-network" =>
PathBuf::from(path).parent().unwrap().join("fixtures/data"),
(Ok(_), pkg_name) => panic!("Failed to resolve fixture dir for tests from {pkg_name:?}."),
}
}
/// Load a given wasm module represented by a .wat file and returns a wasm binary contents along
/// with it's hash.
///
/// The fixture files are located under the `fixtures/` directory.
fn legacy_compile_module<T>(
fixture_name: &str,
) -> anyhow::Result<(Vec<u8>, <T::Hashing as Hash>::Output)>
where
T: frame_system::Config,
{
let fixture_path = wat_root_dir().join(format!("{fixture_name}.wat"));
let wasm_binary = wat::parse_file(fixture_path)?;
let code_hash = T::Hashing::hash(&wasm_binary);
Ok((wasm_binary, code_hash))
}
use std::{fs, path::PathBuf};
/// Load a given wasm module and returns a wasm binary contents along with it's hash.
/// Use the legacy compile_module as fallback, if the rust fixture does not exist yet.
@@ -53,15 +26,11 @@ pub fn compile_module<T>(
where
T: frame_system::Config,
{
let out_dir: std::path::PathBuf = env!("OUT_DIR").into();
let out_dir: PathBuf = env!("OUT_DIR").into();
let fixture_path = out_dir.join(format!("{fixture_name}.wasm"));
match fs::read(fixture_path) {
Ok(wasm_binary) => {
let code_hash = T::Hashing::hash(&wasm_binary);
Ok((wasm_binary, code_hash))
},
Err(_) => legacy_compile_module::<T>(fixture_name),
}
let binary = fs::read(fixture_path)?;
let code_hash = T::Hashing::hash(&binary);
Ok((binary, code_hash))
}
#[cfg(test)]