From 91ad497b7a50019e6736013f7e875f73a384fe47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lasse=20M=C3=B8ldrup?= Date: Tue, 14 May 2024 16:06:14 +0200 Subject: [PATCH 1/6] Add support for loading the contract module under test --- contract-testing/CHANGELOG.md | 1 + contract-testing/src/constants.rs | 6 ++++++ contract-testing/src/impls.rs | 8 ++++++++ contract-testing/src/lib.rs | 4 ++-- contract-testing/src/types.rs | 12 ++++++++++++ 5 files changed, 29 insertions(+), 2 deletions(-) diff --git a/contract-testing/CHANGELOG.md b/contract-testing/CHANGELOG.md index 84e022fa..02f57882 100644 --- a/contract-testing/CHANGELOG.md +++ b/contract-testing/CHANGELOG.md @@ -5,6 +5,7 @@ - Integrate protocol version 7 cost semantics. - The `ContractInvokeSuccess` and `ContractInvokeError` have additional fields that record where parts of the energy was allocated during execution. +- Add support for loading the contract under test with the `module_load_contract` function. The module path is exposed by `cargo-concordium` through the `CARGO_CONCORDIUM_TEST_MODULE_OUTPUT_PATH` environment variable. ## 4.2.0 diff --git a/contract-testing/src/constants.rs b/contract-testing/src/constants.rs index f6fb7cd8..83187e39 100644 --- a/contract-testing/src/constants.rs +++ b/contract-testing/src/constants.rs @@ -75,3 +75,9 @@ pub(crate) const INITIALIZE_CONTRACT_INSTANCE_CREATE_COST: Energy = Energy { pub(crate) const UPDATE_CONTRACT_INSTANCE_BASE_COST: Energy = Energy { energy: 300, }; + +/// The name of the environment variable that holds the path to the contract +/// module file. To load the module, use the +/// [`module_load_contract`](crate::module_load_contract) function. +pub const CONTRACT_MODULE_OUTPUT_PATH_ENV_VAR: &'static str = + "CARGO_CONCORDIUM_TEST_MODULE_OUTPUT_PATH"; diff --git a/contract-testing/src/impls.rs b/contract-testing/src/impls.rs index 827c6d80..be8923f9 100644 --- a/contract-testing/src/impls.rs +++ b/contract-testing/src/impls.rs @@ -2,6 +2,7 @@ use crate::{ constants, invocation::{ChangeSet, EntrypointInvocationHandler, TestConfigurationError}, types::*, + CONTRACT_MODULE_OUTPUT_PATH_ENV_VAR, }; use anyhow::anyhow; use concordium_rust_sdk::{ @@ -37,6 +38,7 @@ use sdk::{ }; use std::{ collections::{BTreeMap, BTreeSet}, + env, future::Future, path::Path, sync::Arc, @@ -2058,6 +2060,12 @@ pub fn module_load_v1(module_path: impl AsRef) -> Result Result { + let module_path = env::var(CONTRACT_MODULE_OUTPUT_PATH_ENV_VAR)?; + let module = module_load_v1(module_path)?; + Ok(module) +} + impl Signer { /// Create a signer which always signs with one key. pub const fn with_one_key() -> Self { diff --git a/contract-testing/src/lib.rs b/contract-testing/src/lib.rs index cd4d4fb1..a51cfc87 100644 --- a/contract-testing/src/lib.rs +++ b/contract-testing/src/lib.rs @@ -83,13 +83,13 @@ //! - deployment.transaction_fee //! - initialization.transaction_fee //! - update.transaction_fee)); -//! //! ``` mod constants; mod impls; mod invocation; mod types; -pub use impls::{is_debug_enabled, module_load_v1, module_load_v1_raw}; +pub use constants::CONTRACT_MODULE_OUTPUT_PATH_ENV_VAR; +pub use impls::{is_debug_enabled, module_load_contract, module_load_v1, module_load_v1_raw}; pub use types::*; // Re-export types. diff --git a/contract-testing/src/types.rs b/contract-testing/src/types.rs index 327cc7ce..c9cd76ab 100644 --- a/contract-testing/src/types.rs +++ b/contract-testing/src/types.rs @@ -27,6 +27,7 @@ use concordium_rust_sdk::{ }; use std::{ collections::{BTreeMap, BTreeSet}, + env, path::PathBuf, sync::Arc, }; @@ -325,6 +326,17 @@ pub enum ModuleLoadErrorKind { UnsupportedModuleVersion(WasmVersion), } +#[derive(Debug, Error)] +pub enum ContractModuleLoadError { + #[error( + "Module path environment variable was not set or invalid. Please ensure that \ + cargo-concordium is up-to-date. Details: {0}" + )] + MissingEnvVar(#[from] env::VarError), + #[error("{0}")] + ModuleLoad(#[from] ModuleLoadError), +} + /// The error produced when trying to read a smart contract /// module from a file. #[derive(Debug, Error)] From 0f60feab8c3f5cd8e5e4c710a2c930cb111a94b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lasse=20M=C3=B8ldrup?= Date: Tue, 14 May 2024 16:30:03 +0200 Subject: [PATCH 2/6] Fix clippy warning --- contract-testing/src/constants.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/contract-testing/src/constants.rs b/contract-testing/src/constants.rs index 83187e39..579aa5bb 100644 --- a/contract-testing/src/constants.rs +++ b/contract-testing/src/constants.rs @@ -79,5 +79,4 @@ pub(crate) const UPDATE_CONTRACT_INSTANCE_BASE_COST: Energy = Energy { /// The name of the environment variable that holds the path to the contract /// module file. To load the module, use the /// [`module_load_contract`](crate::module_load_contract) function. -pub const CONTRACT_MODULE_OUTPUT_PATH_ENV_VAR: &'static str = - "CARGO_CONCORDIUM_TEST_MODULE_OUTPUT_PATH"; +pub const CONTRACT_MODULE_OUTPUT_PATH_ENV_VAR: &str = "CARGO_CONCORDIUM_TEST_MODULE_OUTPUT_PATH"; From 50e02a404e6fadc86645f3eeed373e54263ec0ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lasse=20M=C3=B8ldrup?= Date: Tue, 21 May 2024 13:09:19 +0200 Subject: [PATCH 3/6] Improve function name and add comment --- contract-testing/src/impls.rs | 3 ++- contract-testing/src/lib.rs | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/contract-testing/src/impls.rs b/contract-testing/src/impls.rs index be8923f9..fb7db7ad 100644 --- a/contract-testing/src/impls.rs +++ b/contract-testing/src/impls.rs @@ -2060,7 +2060,8 @@ pub fn module_load_v1(module_path: impl AsRef) -> Result Result { +/// Load the wasm module being tested output by `cargo concordium test`. +pub fn module_load_output() -> Result { let module_path = env::var(CONTRACT_MODULE_OUTPUT_PATH_ENV_VAR)?; let module = module_load_v1(module_path)?; Ok(module) diff --git a/contract-testing/src/lib.rs b/contract-testing/src/lib.rs index a51cfc87..4673c11a 100644 --- a/contract-testing/src/lib.rs +++ b/contract-testing/src/lib.rs @@ -89,7 +89,7 @@ mod impls; mod invocation; mod types; pub use constants::CONTRACT_MODULE_OUTPUT_PATH_ENV_VAR; -pub use impls::{is_debug_enabled, module_load_contract, module_load_v1, module_load_v1_raw}; +pub use impls::{is_debug_enabled, module_load_output, module_load_v1, module_load_v1_raw}; pub use types::*; // Re-export types. From 59dad591be1a6e0b434cfeadc3fc07ef576d78ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lasse=20M=C3=B8ldrup?= Date: Tue, 21 May 2024 13:22:39 +0200 Subject: [PATCH 4/6] Rename error type --- contract-testing/src/impls.rs | 2 +- contract-testing/src/types.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/contract-testing/src/impls.rs b/contract-testing/src/impls.rs index fb7db7ad..1b72959f 100644 --- a/contract-testing/src/impls.rs +++ b/contract-testing/src/impls.rs @@ -2061,7 +2061,7 @@ pub fn module_load_v1(module_path: impl AsRef) -> Result Result { +pub fn module_load_output() -> Result { let module_path = env::var(CONTRACT_MODULE_OUTPUT_PATH_ENV_VAR)?; let module = module_load_v1(module_path)?; Ok(module) diff --git a/contract-testing/src/types.rs b/contract-testing/src/types.rs index c9cd76ab..1c2c7d34 100644 --- a/contract-testing/src/types.rs +++ b/contract-testing/src/types.rs @@ -327,7 +327,7 @@ pub enum ModuleLoadErrorKind { } #[derive(Debug, Error)] -pub enum ContractModuleLoadError { +pub enum OutputModuleLoadError { #[error( "Module path environment variable was not set or invalid. Please ensure that \ cargo-concordium is up-to-date. Details: {0}" From 19987c4c298c8c8d17c6be31561368f9664ac19a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lasse=20M=C3=B8ldrup?= Date: Tue, 21 May 2024 14:30:16 +0200 Subject: [PATCH 5/6] Apply suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Emil Holm Gjørup --- contract-testing/CHANGELOG.md | 2 +- contract-testing/src/constants.rs | 2 +- contract-testing/src/impls.rs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/contract-testing/CHANGELOG.md b/contract-testing/CHANGELOG.md index 02f57882..46364be3 100644 --- a/contract-testing/CHANGELOG.md +++ b/contract-testing/CHANGELOG.md @@ -5,7 +5,7 @@ - Integrate protocol version 7 cost semantics. - The `ContractInvokeSuccess` and `ContractInvokeError` have additional fields that record where parts of the energy was allocated during execution. -- Add support for loading the contract under test with the `module_load_contract` function. The module path is exposed by `cargo-concordium` through the `CARGO_CONCORDIUM_TEST_MODULE_OUTPUT_PATH` environment variable. +- Add support for loading the contract under test with the `module_load_output` function. The module path is exposed by `cargo-concordium` through the `CARGO_CONCORDIUM_TEST_MODULE_OUTPUT_PATH` environment variable. ## 4.2.0 diff --git a/contract-testing/src/constants.rs b/contract-testing/src/constants.rs index 579aa5bb..568a1ba1 100644 --- a/contract-testing/src/constants.rs +++ b/contract-testing/src/constants.rs @@ -78,5 +78,5 @@ pub(crate) const UPDATE_CONTRACT_INSTANCE_BASE_COST: Energy = Energy { /// The name of the environment variable that holds the path to the contract /// module file. To load the module, use the -/// [`module_load_contract`](crate::module_load_contract) function. +/// [`module_load_output`](crate::module_load_output) function. pub const CONTRACT_MODULE_OUTPUT_PATH_ENV_VAR: &str = "CARGO_CONCORDIUM_TEST_MODULE_OUTPUT_PATH"; diff --git a/contract-testing/src/impls.rs b/contract-testing/src/impls.rs index 1b72959f..aeedfea9 100644 --- a/contract-testing/src/impls.rs +++ b/contract-testing/src/impls.rs @@ -2060,7 +2060,7 @@ pub fn module_load_v1(module_path: impl AsRef) -> Result Result { let module_path = env::var(CONTRACT_MODULE_OUTPUT_PATH_ENV_VAR)?; let module = module_load_v1(module_path)?; From 9cf2385c5a58726c207edb3955e5dc2f8d662ef2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lasse=20M=C3=B8ldrup?= Date: Tue, 21 May 2024 16:01:10 +0200 Subject: [PATCH 6/6] Fix cargo fmt --- contract-testing/src/impls.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/contract-testing/src/impls.rs b/contract-testing/src/impls.rs index aeedfea9..8b318895 100644 --- a/contract-testing/src/impls.rs +++ b/contract-testing/src/impls.rs @@ -2060,7 +2060,9 @@ pub fn module_load_v1(module_path: impl AsRef) -> Result Result { let module_path = env::var(CONTRACT_MODULE_OUTPUT_PATH_ENV_VAR)?; let module = module_load_v1(module_path)?;