From ba788ca5eac57da7161fd974349d3da880eac1b9 Mon Sep 17 00:00:00 2001 From: alenmestrov Date: Mon, 2 Dec 2024 12:51:47 +0100 Subject: [PATCH 1/4] fix: resolved building of contract for CI --- contracts/icp/context-config/build.sh | 26 ++++++++----------- contracts/icp/context-config/deploy_devnet.sh | 16 ++++++++++++ .../icp/context-config/tests/integration.rs | 2 +- scripts/test.sh | 1 + 4 files changed, 29 insertions(+), 16 deletions(-) create mode 100644 contracts/icp/context-config/deploy_devnet.sh diff --git a/contracts/icp/context-config/build.sh b/contracts/icp/context-config/build.sh index 8e9f2f742..e3cd26f5d 100755 --- a/contracts/icp/context-config/build.sh +++ b/contracts/icp/context-config/build.sh @@ -1,22 +1,18 @@ -#!/bin/bash - -# Exit on error +#!/bin/sh set -e -# Ensure we have the wasm32 target -rustup target add wasm32-unknown-unknown +cd "$(dirname $0)" -# Build the contract -cargo build --target wasm32-unknown-unknown --release +TARGET="${CARGO_TARGET_DIR:-./target}" + +rustup target add wasm32-unknown-unknown -# Generate the candid interface -candid-extractor target/wasm32-unknown-unknown/release/context_contract.wasm > context_contract.did +cargo build --target wasm32-unknown-unknown --profile app-release -# Stop the replica -dfx stop +mkdir -p res -# Start the replica -dfx start --background +cp $TARGET/wasm32-unknown-unknown/app-release/context_contract.wasm ./res/context_contract.wasm -# Deploy the contract -dfx deploy \ No newline at end of file +if command -v wasm-opt > /dev/null; then + wasm-opt -Oz ./res/context_contract.wasm -o ./res/context_contract.wasm +fi diff --git a/contracts/icp/context-config/deploy_devnet.sh b/contracts/icp/context-config/deploy_devnet.sh new file mode 100644 index 000000000..4174ccade --- /dev/null +++ b/contracts/icp/context-config/deploy_devnet.sh @@ -0,0 +1,16 @@ +#!/bin/bash + +# Build the contract +bash ./build.sh + +# Generate the candid interface +candid-extractor res/context_contract.wasm > context_contract.did + +# Stop the replica +dfx stop + +# Start the replica +dfx start --background + +# Deploy the contract +dfx deploy \ No newline at end of file diff --git a/contracts/icp/context-config/tests/integration.rs b/contracts/icp/context-config/tests/integration.rs index d85fb79bb..d90740236 100644 --- a/contracts/icp/context-config/tests/integration.rs +++ b/contracts/icp/context-config/tests/integration.rs @@ -12,7 +12,7 @@ use rand::Rng; fn setup() -> (PocketIc, Principal) { let pic = PocketIc::new(); - let wasm = std::fs::read("target/wasm32-unknown-unknown/release/context_contract.wasm") + let wasm = std::fs::read("res/context_contract.wasm") .expect("failed to read wasm"); let canister = pic.create_canister(); pic.add_cycles(canister, 2_000_000_000_000); diff --git a/scripts/test.sh b/scripts/test.sh index 35438717e..7da681894 100755 --- a/scripts/test.sh +++ b/scripts/test.sh @@ -12,6 +12,7 @@ cd "$(dirname $0)" ../contracts/registry/build.sh ../contracts/context-config/build.sh ../contracts/proxy-lib/build-test-deps.sh +../contracts/icp/context-config/build.sh # Run cargo test cargo test From f1213c011646df76b07eab8aac94f250f0ff1495 Mon Sep 17 00:00:00 2001 From: alenmestrov Date: Mon, 2 Dec 2024 12:52:03 +0100 Subject: [PATCH 2/4] fix: lint --- contracts/icp/context-config/tests/integration.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/contracts/icp/context-config/tests/integration.rs b/contracts/icp/context-config/tests/integration.rs index d90740236..07090e510 100644 --- a/contracts/icp/context-config/tests/integration.rs +++ b/contracts/icp/context-config/tests/integration.rs @@ -12,8 +12,7 @@ use rand::Rng; fn setup() -> (PocketIc, Principal) { let pic = PocketIc::new(); - let wasm = std::fs::read("res/context_contract.wasm") - .expect("failed to read wasm"); + let wasm = std::fs::read("res/context_contract.wasm").expect("failed to read wasm"); let canister = pic.create_canister(); pic.add_cycles(canister, 2_000_000_000_000); pic.install_canister( From 849979cdc0d0a8a12d2295bbb1adf6a680d313a7 Mon Sep 17 00:00:00 2001 From: alenmestrov Date: Wed, 4 Dec 2024 11:20:26 +0100 Subject: [PATCH 3/4] feat: introduced build scripts for proxy contract and its dependecies --- Cargo.lock | 20 +++++++++++ Cargo.toml | 2 ++ contracts/icp/proxy-contract/build.sh | 23 +++++++------ .../proxy-contract/build_mock_contracts.sh | 33 +++++++++++++++---- .../icp/proxy-contract/mock/external/build.sh | 18 ++++++++++ .../icp/proxy-contract/mock/ledger/build.sh | 18 ++++++++++ contracts/icp/proxy-contract/src/lib.rs | 3 +- .../icp/proxy-contract/tests/integration.rs | 6 ++-- 8 files changed, 99 insertions(+), 24 deletions(-) create mode 100755 contracts/icp/proxy-contract/mock/external/build.sh create mode 100755 contracts/icp/proxy-contract/mock/ledger/build.sh diff --git a/Cargo.lock b/Cargo.lock index dc907d647..c3a5b1202 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4695,6 +4695,26 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "mock_external" +version = "0.1.0" +dependencies = [ + "candid", + "ic-cdk 0.16.0", + "ic-cdk-macros 0.16.0", +] + +[[package]] +name = "mock_ledger" +version = "0.1.0" +dependencies = [ + "candid", + "ic-cdk 0.16.0", + "ic-cdk-macros 0.16.0", + "ic-ledger-types", + "serde", +] + [[package]] name = "more-asserts" version = "0.2.2" diff --git a/Cargo.toml b/Cargo.toml index 998fa4182..8537a3438 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -40,6 +40,8 @@ members = [ "./contracts/test-counter", "./contracts/icp/context-config", "./contracts/icp/proxy-contract", + "./contracts/icp/proxy-contract/mock/ledger", + "./contracts/icp/proxy-contract/mock/external", "./e2e-tests", ] diff --git a/contracts/icp/proxy-contract/build.sh b/contracts/icp/proxy-contract/build.sh index 4b09537c7..32fda01a6 100755 --- a/contracts/icp/proxy-contract/build.sh +++ b/contracts/icp/proxy-contract/build.sh @@ -1,19 +1,18 @@ -#!/bin/bash - -# Exit on error +#!/bin/sh set -e -# Ensure we have the wasm32 target +cd "$(dirname $0)" + +TARGET="${CARGO_TARGET_DIR:-./target}" + rustup target add wasm32-unknown-unknown -# Build the contract -cargo build --target wasm32-unknown-unknown --release +cargo build --target wasm32-unknown-unknown --profile app-release -# Generate the candid interface -candid-extractor target/wasm32-unknown-unknown/release/proxy_contract.wasm > proxy_contract.did +mkdir -p res -# Stop the replica -dfx stop +cp $TARGET/wasm32-unknown-unknown/app-release/proxy_contract.wasm ./res/proxy_contract.wasm -# Start the replica -dfx start --background \ No newline at end of file +if command -v wasm-opt > /dev/null; then + wasm-opt -Oz ./res/proxy_contract.wasm -o ./res/proxy_contract.wasm +fi diff --git a/contracts/icp/proxy-contract/build_mock_contracts.sh b/contracts/icp/proxy-contract/build_mock_contracts.sh index 3fa07632d..a6fdf7ec1 100755 --- a/contracts/icp/proxy-contract/build_mock_contracts.sh +++ b/contracts/icp/proxy-contract/build_mock_contracts.sh @@ -1,10 +1,29 @@ -cd "mock/ledger" -cargo build --target wasm32-unknown-unknown --release -candid-extractor target/wasm32-unknown-unknown/release/mock_ledger.wasm > mock_ledger.did +#!/bin/sh +set -e -cd ../.. +# Get the absolute path to the workspace root +WORKSPACE_ROOT="$(cd "$(dirname "$0")/../../../" && pwd)" -cd "mock/external" -cargo build --target wasm32-unknown-unknown --release -candid-extractor target/wasm32-unknown-unknown/release/mock_external.wasm > mock_external.did +cd "$(dirname $0)" +echo "Building proxy contract..." +./build.sh + +echo "Building test-ledger contract..." +(cd "$WORKSPACE_ROOT" && cargo build \ + --target wasm32-unknown-unknown \ + --profile app-release \ + -p mock_ledger) +mkdir -p mock/ledger/res +cp "$WORKSPACE_ROOT/target/wasm32-unknown-unknown/app-release/mock_ledger.wasm" mock/ledger/res/ + +echo "Building test-external contract..." +(cd "$WORKSPACE_ROOT" && cargo build \ + --target wasm32-unknown-unknown \ + --profile app-release \ + -p mock_external) +mkdir -p mock/external/res +cp "$WORKSPACE_ROOT/target/wasm32-unknown-unknown/app-release/mock_external.wasm" mock/external/res/ + +echo "Building context-config contract..." +(cd ../context-config && ./build.sh) diff --git a/contracts/icp/proxy-contract/mock/external/build.sh b/contracts/icp/proxy-contract/mock/external/build.sh new file mode 100755 index 000000000..d8d751e2c --- /dev/null +++ b/contracts/icp/proxy-contract/mock/external/build.sh @@ -0,0 +1,18 @@ +#!/bin/sh +set -e + +cd "$(dirname $0)" + +TARGET="${CARGO_TARGET_DIR:-../../target}" + +rustup target add wasm32-unknown-unknown + +cargo build --target wasm32-unknown-unknown --profile app-release + +mkdir -p res + +cp $TARGET/wasm32-unknown-unknown/app-release/mock_external.wasm ./res/ + +if command -v wasm-opt > /dev/null; then + wasm-opt -Oz ./res/mock_external.wasm -o ./res/mock_external.wasm +fi diff --git a/contracts/icp/proxy-contract/mock/ledger/build.sh b/contracts/icp/proxy-contract/mock/ledger/build.sh new file mode 100755 index 000000000..a69571178 --- /dev/null +++ b/contracts/icp/proxy-contract/mock/ledger/build.sh @@ -0,0 +1,18 @@ +#!/bin/sh +set -e + +cd "$(dirname $0)" + +TARGET="${CARGO_TARGET_DIR:-./target}" + +rustup target add wasm32-unknown-unknown + +cargo build --target wasm32-unknown-unknown --profile app-release + +mkdir -p res + +cp $TARGET/wasm32-unknown-unknown/app-release/mock_ledger.wasm ./res/mock_ledger.wasm + +if command -v wasm-opt > /dev/null; then + wasm-opt -Oz ./res/mock_ledger.wasm -o ./res/mock_ledger.wasm +fi diff --git a/contracts/icp/proxy-contract/src/lib.rs b/contracts/icp/proxy-contract/src/lib.rs index f8001bcef..fa5541125 100644 --- a/contracts/icp/proxy-contract/src/lib.rs +++ b/contracts/icp/proxy-contract/src/lib.rs @@ -2,7 +2,6 @@ use std::cell::RefCell; use std::collections::{BTreeMap, BTreeSet, HashMap}; use candid::Principal; -use serde::{Deserialize, Serialize}; use types::{ICContextId, LedgerId}; use crate::types::{ @@ -14,7 +13,7 @@ pub mod mutate; pub mod query; pub mod types; -#[derive(Serialize, Deserialize, Default)] +#[derive(Default)] pub struct ICProxyContract { pub context_id: ICContextId, pub context_config_id: String, diff --git a/contracts/icp/proxy-contract/tests/integration.rs b/contracts/icp/proxy-contract/tests/integration.rs index c5d9ccfbe..824f0c875 100644 --- a/contracts/icp/proxy-contract/tests/integration.rs +++ b/contracts/icp/proxy-contract/tests/integration.rs @@ -33,12 +33,12 @@ mod tests { let mock_ledger = pic.create_canister(); pic.add_cycles(mock_ledger, 100_000_000_000_000); let mock_ledger_wasm = - std::fs::read("mock/ledger/target/wasm32-unknown-unknown/release/mock_ledger.wasm") + std::fs::read("mock/ledger/res/mock_ledger.wasm") .expect("failed to read mock ledger wasm"); pic.install_canister(mock_ledger, mock_ledger_wasm, vec![], None); // Setup proxy contract - let wasm = std::fs::read("target/wasm32-unknown-unknown/release/proxy_contract.wasm") + let wasm = std::fs::read("res/proxy_contract.wasm") .expect("failed to read wasm"); let proxy_canister = pic.create_canister(); pic.add_cycles(proxy_canister, 100_000_000_000_000); @@ -53,7 +53,7 @@ mod tests { let mock_external = pic.create_canister(); pic.add_cycles(mock_external, 100_000_000_000_000); let mock_external_wasm = - std::fs::read("mock/external/target/wasm32-unknown-unknown/release/mock_external.wasm") + std::fs::read("mock/external/res/mock_external.wasm") .expect("failed to read mock external wasm"); pic.install_canister(mock_external, mock_external_wasm, vec![], None); From 601293ae7ed80066e533990e6555cec76346ab70 Mon Sep 17 00:00:00 2001 From: alenmestrov Date: Wed, 4 Dec 2024 11:21:55 +0100 Subject: [PATCH 4/4] feat: added scripts to test.sh script for CI and lint --- .../proxy-contract/mock/external/src/lib.rs | 2 +- .../icp/proxy-contract/mock/ledger/src/lib.rs | 36 ++++++++++--------- .../icp/proxy-contract/tests/integration.rs | 13 +++---- scripts/test.sh | 3 +- 4 files changed, 28 insertions(+), 26 deletions(-) diff --git a/contracts/icp/proxy-contract/mock/external/src/lib.rs b/contracts/icp/proxy-contract/mock/external/src/lib.rs index 230e4423a..0386ddead 100644 --- a/contracts/icp/proxy-contract/mock/external/src/lib.rs +++ b/contracts/icp/proxy-contract/mock/external/src/lib.rs @@ -16,4 +16,4 @@ fn get_calls() -> Vec> { CALLS.with(|calls| calls.borrow().clone()) } -ic_cdk::export_candid!(); \ No newline at end of file +ic_cdk::export_candid!(); diff --git a/contracts/icp/proxy-contract/mock/ledger/src/lib.rs b/contracts/icp/proxy-contract/mock/ledger/src/lib.rs index b0452d3b0..2c60efed8 100644 --- a/contracts/icp/proxy-contract/mock/ledger/src/lib.rs +++ b/contracts/icp/proxy-contract/mock/ledger/src/lib.rs @@ -1,6 +1,9 @@ use std::cell::RefCell; + use candid::{CandidType, Deserialize, Principal}; -use ic_ledger_types::{Tokens, AccountIdentifier, Memo, TransferArgs, Timestamp, BlockIndex, TransferError}; +use ic_ledger_types::{ + AccountIdentifier, BlockIndex, Memo, Timestamp, Tokens, TransferArgs, TransferError, +}; thread_local! { static BALANCE: RefCell = RefCell::new(1_000_000_000); @@ -10,34 +13,37 @@ type TransferResult = Result; #[ic_cdk::update] fn transfer(args: TransferArgs) -> TransferResult { - ic_cdk::println!("Mock ledger received transfer: to={:?}, amount={}", - args.to, args.amount); - + ic_cdk::println!( + "Mock ledger received transfer: to={:?}, amount={}", + args.to, + args.amount + ); + // Verify fee if args.fee.e8s() != 10_000 { - return Err(TransferError::BadFee { - expected_fee: Tokens::from_e8s(10_000) + return Err(TransferError::BadFee { + expected_fee: Tokens::from_e8s(10_000), }); } let amount_e8s = args.amount.e8s(); - + BALANCE.with(|balance| { let mut bal = balance.borrow_mut(); - + // Check if we have enough balance if amount_e8s > *bal { - return Err(TransferError::InsufficientFunds { - balance: Tokens::from_e8s(*bal) + return Err(TransferError::InsufficientFunds { + balance: Tokens::from_e8s(*bal), }); } - + // Subtract amount and fee *bal = bal.saturating_sub(amount_e8s); *bal = bal.saturating_sub(args.fee.e8s()); - + ic_cdk::println!("New balance: {}", *bal); - + // Return mock block index Ok(1) }) @@ -45,9 +51,7 @@ fn transfer(args: TransferArgs) -> TransferResult { #[ic_cdk::query] fn account_balance(args: AccountBalanceArgs) -> Tokens { - BALANCE.with(|balance| { - Tokens::from_e8s(*balance.borrow()) - }) + BALANCE.with(|balance| Tokens::from_e8s(*balance.borrow())) } #[derive(CandidType, Deserialize)] diff --git a/contracts/icp/proxy-contract/tests/integration.rs b/contracts/icp/proxy-contract/tests/integration.rs index 824f0c875..cf7bad043 100644 --- a/contracts/icp/proxy-contract/tests/integration.rs +++ b/contracts/icp/proxy-contract/tests/integration.rs @@ -32,14 +32,12 @@ mod tests { // Setup mock ledger first let mock_ledger = pic.create_canister(); pic.add_cycles(mock_ledger, 100_000_000_000_000); - let mock_ledger_wasm = - std::fs::read("mock/ledger/res/mock_ledger.wasm") - .expect("failed to read mock ledger wasm"); + let mock_ledger_wasm = std::fs::read("mock/ledger/res/mock_ledger.wasm") + .expect("failed to read mock ledger wasm"); pic.install_canister(mock_ledger, mock_ledger_wasm, vec![], None); // Setup proxy contract - let wasm = std::fs::read("res/proxy_contract.wasm") - .expect("failed to read wasm"); + let wasm = std::fs::read("res/proxy_contract.wasm").expect("failed to read wasm"); let proxy_canister = pic.create_canister(); pic.add_cycles(proxy_canister, 100_000_000_000_000); @@ -52,9 +50,8 @@ mod tests { // Setup mock external let mock_external = pic.create_canister(); pic.add_cycles(mock_external, 100_000_000_000_000); - let mock_external_wasm = - std::fs::read("mock/external/res/mock_external.wasm") - .expect("failed to read mock external wasm"); + let mock_external_wasm = std::fs::read("mock/external/res/mock_external.wasm") + .expect("failed to read mock external wasm"); pic.install_canister(mock_external, mock_external_wasm, vec![], None); ProxyTestContext { diff --git a/scripts/test.sh b/scripts/test.sh index 7da681894..1dd1cda2b 100755 --- a/scripts/test.sh +++ b/scripts/test.sh @@ -13,6 +13,7 @@ cd "$(dirname $0)" ../contracts/context-config/build.sh ../contracts/proxy-lib/build-test-deps.sh ../contracts/icp/context-config/build.sh - +../contracts/icp/proxy-contract/build_mock_contracts.sh +../contracts/icp/proxy-contract/build.sh # Run cargo test cargo test