diff --git a/Cargo.lock b/Cargo.lock index b1e7f048e72..89d1c6bb2a3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3771,7 +3771,9 @@ version = "2.0.0-rc.1.0" dependencies = [ "iroha_crypto", "iroha_data_model", + "iroha_wasm_builder", "serde", + "toml", ] [[package]] diff --git a/crates/iroha/tests/upgrade.rs b/crates/iroha/tests/upgrade.rs index c6f2c04f995..bbf940e73ff 100644 --- a/crates/iroha/tests/upgrade.rs +++ b/crates/iroha/tests/upgrade.rs @@ -10,7 +10,7 @@ use iroha::{ }; use iroha_executor_data_model::permission::{domain::CanUnregisterDomain, Permission as _}; use iroha_test_network::*; -use iroha_test_samples::{load_sample_wasm, ALICE_ID, BOB_ID}; +use iroha_test_samples::{load_sample_wasm, load_wasm_build_profile, ALICE_ID, BOB_ID}; use nonzero_ext::nonzero; const ADMIN_PUBLIC_KEY_MULTIHASH: &str = @@ -389,6 +389,18 @@ fn define_custom_parameter() -> Result<()> { fn upgrade_executor(client: &Client, executor: impl AsRef) -> Result<()> { let upgrade_executor = Upgrade::new(Executor::new(load_sample_wasm(executor))); + let profile = load_wasm_build_profile(); + + if !profile.is_optimized() { + client.submit_all_blocking::([ + InstructionBox::SetParameter(SetParameter::new( + Parameter::Executor(SmartContractParameter::Fuel( + std::num::NonZeroU64::new(80_000_000_u64).expect("Fuel must be positive."), + )) + )) + ])?; + } + client.submit_blocking(upgrade_executor)?; Ok(()) diff --git a/crates/iroha_core/src/sumeragi/main_loop.rs b/crates/iroha_core/src/sumeragi/main_loop.rs index e5b88bfa076..593ca69ef9a 100644 --- a/crates/iroha_core/src/sumeragi/main_loop.rs +++ b/crates/iroha_core/src/sumeragi/main_loop.rs @@ -262,8 +262,7 @@ impl Sumeragi { } }; - let mut errors = block.as_ref().errors().peekable(); - if errors.peek().is_some() { + if block.as_ref().errors().next().is_some() { error!("Genesis contains invalid transactions"); for error in block.as_ref().errors() { diff --git a/crates/iroha_test_network/src/lib.rs b/crates/iroha_test_network/src/lib.rs index 07cb412ffcd..107920ca55f 100644 --- a/crates/iroha_test_network/src/lib.rs +++ b/crates/iroha_test_network/src/lib.rs @@ -27,7 +27,7 @@ use iroha_crypto::{ExposedPrivateKey, KeyPair, PrivateKey}; use iroha_data_model::{ events::pipeline::BlockEventFilter, isi::InstructionBox, - parameter::{SmartContractParameter, SumeragiParameter, SumeragiParameters}, + parameter::{SumeragiParameter, SumeragiParameters}, ChainId, }; use iroha_genesis::GenesisBlock; @@ -340,13 +340,13 @@ impl NetworkBuilder { } // Unoptimized profile requires additional resources. - if cfg!(debug_assertions) { - extra_isi.push(InstructionBox::SetParameter(SetParameter::new( - Parameter::Executor(SmartContractParameter::Fuel( - std::num::NonZeroU64::new(80000000).expect("Fuel must be positive."), - )), - ))); - } + // if cfg!(debug_assertions) { + // extra_isi.push(InstructionBox::SetParameter(SetParameter::new( + // Parameter::Executor(SmartContractParameter::Fuel( + // std::num::NonZeroU64::new(80000000).expect("Fuel must be positive."), + // )), + // ))); + // } let genesis = config::genesis( [ diff --git a/crates/iroha_test_samples/Cargo.toml b/crates/iroha_test_samples/Cargo.toml index c8d0d37192f..da4291c840f 100644 --- a/crates/iroha_test_samples/Cargo.toml +++ b/crates/iroha_test_samples/Cargo.toml @@ -16,8 +16,10 @@ categories.workspace = true [dependencies] iroha_crypto = { workspace = true } iroha_data_model = { workspace = true } +iroha_wasm_builder = { workspace = true } serde = { workspace = true, features = ["derive"] } +toml = { workspace = true } [lints] workspace = true diff --git a/crates/iroha_test_samples/src/lib.rs b/crates/iroha_test_samples/src/lib.rs index ed1d29e45b2..c781285be4c 100644 --- a/crates/iroha_test_samples/src/lib.rs +++ b/crates/iroha_test_samples/src/lib.rs @@ -8,6 +8,8 @@ use std::{ use iroha_crypto::KeyPair; use iroha_data_model::prelude::{AccountId, WasmSmartContract}; +use iroha_wasm_builder::Profile; +use std::fs; /// Generate [`AccountId`](iroha_data_model::account::AccountId) in the given `domain`. /// @@ -96,6 +98,7 @@ fn read_file(path: impl AsRef) -> std::io::Result> { } const WASM_SAMPLES_PREBUILT_DIR: &str = "wasm/target/prebuilt/samples"; +const WASM_BUILD_CONFIG_PATH: &str = "wasm/target/prebuilt/build_config.toml"; /// Load WASM smart contract from `wasm/samples` by the name of smart contract, /// e.g. `default_executor`. @@ -126,3 +129,32 @@ pub fn load_sample_wasm(name: impl AsRef) -> WasmSmartContract { Ok(blob) => WasmSmartContract::from_compiled(blob), } } + +/// Load WASM smart contract build profile +/// +/// WASMs must be pre-built with the `build_wasm.sh` script +pub fn load_wasm_build_profile() -> Profile { + let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")) + .join("../../") + .canonicalize() + .expect("invoking from crates/iroha_test_samples, should be fine") + .join(WASM_BUILD_CONFIG_PATH); + + match fs::read_to_string(&path) { + Err(err) => { + eprintln!( + "ERROR: Could not load WASM build configuration file from `{}`: {err}\n\ + Make sure to run `build_wasm.sh` first.", + path.display(), + ); + panic!("could not read WASM build profile"); + } + Ok(content) => { + let parsed: toml::Value = toml::from_str(content.as_str()).expect("must parse config file"); + let profile_str = parsed.get("profile") + .and_then(toml::Value::as_str) + .expect("profile must be present in config"); + profile_str.parse().expect(format!("unrecognized profile {profile_str}").as_str()) + } + } +} \ No newline at end of file diff --git a/crates/iroha_wasm_builder/src/lib.rs b/crates/iroha_wasm_builder/src/lib.rs index 38fc62acb63..498272c04c5 100644 --- a/crates/iroha_wasm_builder/src/lib.rs +++ b/crates/iroha_wasm_builder/src/lib.rs @@ -29,8 +29,8 @@ pub enum Profile { impl Profile { /// Checks whether profile uses optimizations from wasm-opt - pub fn is_optimized(profile: Self) -> bool { - return profile == Profile::Deploy; + pub fn is_optimized(self) -> bool { + return self == Profile::Deploy; } } @@ -120,7 +120,7 @@ impl<'path, 'out_dir> Builder<'path, 'out_dir> { /// /// Will also return error if ran on workspace and not on the concrete package. pub fn build(self) -> Result { - let optimize = Profile::is_optimized(self.profile); + let optimize = self.profile.is_optimized(); let output = self.into_internal()?.build()?; if optimize { output.optimize() diff --git a/crates/iroha_wasm_builder/src/main.rs b/crates/iroha_wasm_builder/src/main.rs index 3267d780176..4b4f97adfbd 100644 --- a/crates/iroha_wasm_builder/src/main.rs +++ b/crates/iroha_wasm_builder/src/main.rs @@ -60,7 +60,7 @@ fn main() -> color_eyre::Result<()> { } }; - let output = if Profile::is_optimized(profile) { + let output = if profile.is_optimized() { let sp = if std::env::var("CI").is_err() { Some(spinoff::Spinner::new_with_stream( spinoff::spinners::Binary, diff --git a/scripts/build_wasm.sh b/scripts/build_wasm.sh index 45ce0702639..9ffee8a9c67 100755 --- a/scripts/build_wasm.sh +++ b/scripts/build_wasm.sh @@ -72,6 +72,9 @@ build() { out_file="$TARGET_DIR/$1/$name.wasm" cargo run --bin iroha_wasm_builder -- build "$CARGO_DIR/$1/$name" --profile=$PROFILE --out-file "$out_file" done + + echo "profile = \"${PROFILE}\"" > "$TARGET_DIR/build_config.toml" + echo "info: WASM $1 build complete" echo "artifacts written to $TARGET_DIR/$1/" }