Skip to content

Commit

Permalink
fix: use profiles for custom executors
Browse files Browse the repository at this point in the history
Signed-off-by: Lohachov Mykhailo <[email protected]>
  • Loading branch information
aoyako committed Dec 7, 2024
1 parent 0cba0b8 commit a10f446
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 15 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 13 additions & 1 deletion crates/iroha/tests/upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -389,6 +389,18 @@ fn define_custom_parameter() -> Result<()> {

fn upgrade_executor(client: &Client, executor: impl AsRef<str>) -> 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>([
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(())
Expand Down
3 changes: 1 addition & 2 deletions crates/iroha_core/src/sumeragi/main_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
16 changes: 8 additions & 8 deletions crates/iroha_test_network/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
[
Expand Down
2 changes: 2 additions & 0 deletions crates/iroha_test_samples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 32 additions & 0 deletions crates/iroha_test_samples/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
///
Expand Down Expand Up @@ -96,6 +98,7 @@ fn read_file(path: impl AsRef<Path>) -> std::io::Result<Vec<u8>> {
}

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`.
Expand Down Expand Up @@ -126,3 +129,32 @@ pub fn load_sample_wasm(name: impl AsRef<str>) -> 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())
}
}
}
6 changes: 3 additions & 3 deletions crates/iroha_wasm_builder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -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<Output> {
let optimize = Profile::is_optimized(self.profile);
let optimize = self.profile.is_optimized();
let output = self.into_internal()?.build()?;
if optimize {
output.optimize()
Expand Down
2 changes: 1 addition & 1 deletion crates/iroha_wasm_builder/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions scripts/build_wasm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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/"
}
Expand Down

0 comments on commit a10f446

Please sign in to comment.