Skip to content

Commit

Permalink
Merge pull request #143 from cspr-rad/feature/heavily-optimized-wasm
Browse files Browse the repository at this point in the history
Reduce WASM size to fit into production chainspec
  • Loading branch information
Avi-D-coder authored Jul 8, 2024
2 parents 99b3e60 + da9896f commit 54c0b1e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
31 changes: 29 additions & 2 deletions demo-contract-tests/tests/test_fixture/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
mod wasm_helper;

use casper_engine_test_support::{
ExecuteRequestBuilder, WasmTestBuilder, ARG_AMOUNT, DEFAULT_ACCOUNT_ADDR,
DeployItemBuilder, ExecuteRequestBuilder, WasmTestBuilder, ARG_AMOUNT, DEFAULT_ACCOUNT_ADDR,
DEFAULT_ACCOUNT_INITIAL_BALANCE,
};
use casper_execution_engine::storage::global_state::in_memory::InMemoryGlobalState;
Expand All @@ -13,6 +13,7 @@ use casper_types::{
system::{handle_payment::ARG_TARGET, mint::ARG_ID},
RuntimeArgs, U512,
};
use rand::Rng;
use std::path::Path;

use casper_engine_test_support::{InMemoryWasmTestBuilder, PRODUCTION_RUN_GENESIS_REQUEST};
Expand Down Expand Up @@ -154,11 +155,13 @@ impl TestContext {
let session_args = runtime_args! {
"risc0_receipt" => Bytes::from(proof_serialized),
};
let submit_batch_request = ExecuteRequestBuilder::contract_call_by_hash(
let payment = U512::from(3_000_000_000_000u64); // 3000 CSPR
let submit_batch_request = contract_call_by_hash(
sender,
self.contract_hash,
"submit_batch",
session_args,
payment,
)
.build();
self.builder
Expand Down Expand Up @@ -201,3 +204,27 @@ pub fn create_funded_account_for_secret_key_bytes(
builder.exec(transfer).expect_success().commit();
account_public_key
}

/// Returns an [`ExecuteRequest`] that will call a stored contract by hash.
/// NOTE: This utiliity is a custom version of `ExecuteRequest::contract_call_by_hash`
/// that allows to specify custom payment.
pub fn contract_call_by_hash(
sender: AccountHash,
contract_hash: ContractHash,
entry_point: &str,
args: RuntimeArgs,
payment: U512,
) -> ExecuteRequestBuilder {
let mut rng = rand::thread_rng();
let deploy_hash = rng.gen();

let deploy = DeployItemBuilder::new()
.with_address(sender)
.with_stored_session_hash(contract_hash, entry_point, args)
.with_empty_payment_bytes(runtime_args! { ARG_AMOUNT => payment, })
.with_authorization_keys(&[sender])
.with_deploy_hash(deploy_hash)
.build();

ExecuteRequestBuilder::new().push_deploy(deploy)
}
4 changes: 2 additions & 2 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
filename=$(basename "$file" .wasm)
# Append "-optimized" to the filename and add back the .wasm extension
new_filename="$directory$filename-optimized.wasm"
wasm-opt --strip-debug --signext-lowering "$file" -o "$new_filename"
wasm-opt -Oz --strip-debug --signext-lowering "$file" -o "$new_filename"
fi
done
'';
Expand Down Expand Up @@ -121,7 +121,7 @@
filename=$(basename "$file" .wasm)
# Append "-optimized" to the filename and add back the .wasm extension
new_filename="$directory$filename-optimized.wasm"
wasm-opt --strip-debug --signext-lowering "$file" -o "$new_filename"
wasm-opt -Oz --strip-debug --signext-lowering "$file" -o "$new_filename"
fi
done
'';
Expand Down
2 changes: 1 addition & 1 deletion kairos-contracts/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ codegen-units = 1
lto = true
debug = false
strip = true
opt-level = 3
opt-level = 'z'
panic = "abort"

[workspace.dependencies]
Expand Down

0 comments on commit 54c0b1e

Please sign in to comment.