From 64c9857fcd298d43746938f9a7b2d04ed97c1b13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrzej=20Bro=C5=84ski?= Date: Mon, 8 Jul 2024 12:54:07 +0200 Subject: [PATCH 1/4] Compile contracts with super focus on code size. --- kairos-contracts/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kairos-contracts/Cargo.toml b/kairos-contracts/Cargo.toml index 845d0efb..2be85db2 100644 --- a/kairos-contracts/Cargo.toml +++ b/kairos-contracts/Cargo.toml @@ -17,7 +17,7 @@ codegen-units = 1 lto = true debug = false strip = true -opt-level = 3 +opt-level = 'z' panic = "abort" [workspace.dependencies] From ba2bff7f46c27ae5b53e4ba27826e37c3f673274 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrzej=20Bro=C5=84ski?= Date: Mon, 8 Jul 2024 12:54:39 +0200 Subject: [PATCH 2/4] Set WASM optimization to super focus on code size. --- flake.nix | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/flake.nix b/flake.nix index a5de7fed..4a5884bd 100644 --- a/flake.nix +++ b/flake.nix @@ -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 ''; @@ -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 ''; From 071cc23f3404cecb0f457b40a9035a64138dd1d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrzej=20Bro=C5=84ski?= Date: Mon, 8 Jul 2024 16:09:43 +0200 Subject: [PATCH 3/4] Introduce `contract_call_by_hash()` that allows custom payment. --- demo-contract-tests/tests/test_fixture/mod.rs | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/demo-contract-tests/tests/test_fixture/mod.rs b/demo-contract-tests/tests/test_fixture/mod.rs index 103ab93d..cb952675 100644 --- a/demo-contract-tests/tests/test_fixture/mod.rs +++ b/demo-contract-tests/tests/test_fixture/mod.rs @@ -201,3 +201,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) +} From da9896f17cedac07317d71bd817bde19ce8a60e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrzej=20Bro=C5=84ski?= Date: Mon, 8 Jul 2024 16:10:47 +0200 Subject: [PATCH 4/4] Use 3000 CSPR for verifying batches. Previously it was using 2500 (default), but it seems to be more costly after code-space optimization. --- demo-contract-tests/tests/test_fixture/mod.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/demo-contract-tests/tests/test_fixture/mod.rs b/demo-contract-tests/tests/test_fixture/mod.rs index cb952675..23c88559 100644 --- a/demo-contract-tests/tests/test_fixture/mod.rs +++ b/demo-contract-tests/tests/test_fixture/mod.rs @@ -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; @@ -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}; @@ -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