From a46910f546079d3a8be9c1e729ae581830339362 Mon Sep 17 00:00:00 2001 From: Max Date: Wed, 18 Sep 2024 09:58:34 +0200 Subject: [PATCH 1/2] fix(rust): Address clippy warnings (#14) # Motivation The code is failing the rust linter in CI. # Changes - Address clippy lints. # Tests See CI --------- Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- scripts/did.sh | 38 +++++++++++++-------------- scripts/pic-install | 2 +- scripts/test-rs | 9 +++++++ src/api/src/lib.rs | 1 + src/example/paid_service/src/lib.rs | 2 +- src/example/paid_service/src/state.rs | 2 +- src/guard/src/guards/icrc2_cycles.rs | 15 +++++++++-- 7 files changed, 45 insertions(+), 24 deletions(-) create mode 100755 scripts/test-rs diff --git a/scripts/did.sh b/scripts/did.sh index 7197fa0..458f77f 100755 --- a/scripts/did.sh +++ b/scripts/did.sh @@ -1,37 +1,37 @@ #!/usr/bin/env bash did_file_location_from_cargo() { - # Warning: Does no _- conversion. - cargo metadata --format-version 1 | jq -r --arg v "$1" '.packages[] | select(.name==$v) | .manifest_path | sub("Cargo.toml";"\($v).did")' + # Warning: Does no _- conversion. + cargo metadata --format-version 1 | jq -r --arg v "$1" '.packages[] | select(.name==$v) | .manifest_path | sub("Cargo.toml";"\($v).did")' } cargo_manifest_path() { - # Warning: Does no _- conversion. - cargo metadata --format-version 1 | jq -r --arg v "$1" '.packages[] | select(.name==$v) | .manifest_path")' + # Warning: Does no _- conversion. + cargo metadata --format-version 1 | jq -r --arg v "$1" '.packages[] | select(.name==$v) | .manifest_path")' } did_file_location_from_dfx_json() { - # Warning: Does no _- conversion. - jq -r --arg v "$1" '.canisters[$v].candid' dfx.json + # Warning: Does no _- conversion. + jq -r --arg v "$1" '.canisters[$v].candid' dfx.json } function generate_did() { - local canister=$1 - echo "Deriving candid file from Rust for $canister" - #local manifest_path="$(cargo_manifest_path "$canister")" - #local candid_file="${manifest_path%Cargo.toml}$canister.did" - local candid_file="$(did_file_location_from_dfx_json "$canister")" + local canister=$1 + echo "Deriving candid file from Rust for $canister" + #local manifest_path="$(cargo_manifest_path "$canister")" + #local candid_file="${manifest_path%Cargo.toml}$canister.did" + local candid_file="$(did_file_location_from_dfx_json "$canister")" - test -e "target/wasm32-unknown-unknown/release/$canister.wasm" || - cargo build -p "$canister" \ - --target wasm32-unknown-unknown \ - --release --package "$canister" + test -e "target/wasm32-unknown-unknown/release/$canister.wasm" || + cargo build -p "$canister" \ + --target wasm32-unknown-unknown \ + --release --package "$canister" - # cargo install candid-extractor - candid-extractor "target/wasm32-unknown-unknown/release/$canister.wasm" >"$candid_file" - echo "Written: $candid_file" + # cargo install candid-extractor + candid-extractor "target/wasm32-unknown-unknown/release/$canister.wasm" >"$candid_file" + echo "Written: $candid_file" } CANISTERS=(example_app_backend example_paid_service) for canister in ${CANISTERS[@]}; do - generate_did "$canister" + generate_did "$canister" done diff --git a/scripts/pic-install b/scripts/pic-install index b796a0c..0edf992 100755 --- a/scripts/pic-install +++ b/scripts/pic-install @@ -13,7 +13,7 @@ else exit 1 fi -if "$POCKET_IC_SERVER_PATH" --version &>/dev/null | grep -wq "$POCKET_IC_SERVER_VERSION" ; then +if "$POCKET_IC_SERVER_PATH" --version &>/dev/null | grep -wq "$POCKET_IC_SERVER_VERSION"; then echo "PocketIC server already exists at: $POCKET_IC_SERVER_PATH" echo "Skipping download." else diff --git a/scripts/test-rs b/scripts/test-rs new file mode 100755 index 0000000..2bd8931 --- /dev/null +++ b/scripts/test-rs @@ -0,0 +1,9 @@ +#!/usr/bin/env bash + +print_help() { + echo "Runs rust unit tests." +} + +[[ "${1:-}" != "--help" ]] || print_help + +cargo test --lib --bins "${@}" diff --git a/src/api/src/lib.rs b/src/api/src/lib.rs index a5bff2f..aacde2e 100644 --- a/src/api/src/lib.rs +++ b/src/api/src/lib.rs @@ -9,6 +9,7 @@ pub use caller::PaymentType; pub use error::PaymentError; pub use vendor::Icrc2Payer; +#[must_use] pub fn principal2account(principal: &Principal) -> ByteBuf { // TODO: This is NOT the right way. let mut ans = principal.as_slice().to_vec(); diff --git a/src/example/paid_service/src/lib.rs b/src/example/paid_service/src/lib.rs index 9dda2a5..e21ad2b 100644 --- a/src/example/paid_service/src/lib.rs +++ b/src/example/paid_service/src/lib.rs @@ -18,7 +18,7 @@ fn init(init_args: Option) { } #[update()] -async fn free() -> String { +fn free() -> String { "Yes, I am free!".to_string() } diff --git a/src/example/paid_service/src/state.rs b/src/example/paid_service/src/state.rs index aca6cad..e027b6d 100644 --- a/src/example/paid_service/src/state.rs +++ b/src/example/paid_service/src/state.rs @@ -14,7 +14,7 @@ where INIT_ARGS.with(|init_args| f(init_args.borrow().as_ref().expect("No init args provided"))) } -/// Provides the canister_id of the ledger used for payments. +/// Provides the canister id of the ledger used for payments. pub fn payment_ledger() -> Principal { init_element(|init_args| init_args.ledger.expect("Init args specify no ledger")) } diff --git a/src/guard/src/guards/icrc2_cycles.rs b/src/guard/src/guards/icrc2_cycles.rs index 243e097..c34b23e 100644 --- a/src/guard/src/guards/icrc2_cycles.rs +++ b/src/guard/src/guards/icrc2_cycles.rs @@ -16,17 +16,28 @@ pub struct Icrc2CyclesPaymentGuard { pub own_canister_id: Principal, } impl Icrc2CyclesPaymentGuard { + #[must_use] pub fn default_account() -> Account { Account { owner: ic_cdk::caller(), subaccount: None, } } + /// The normal cycles ledger canister ID. + /// + /// - If the cycles ledger is listed in `dfx.json`, a normal `dfx build` will set the + /// environment variable `CANISTER_ID_CYCLES_LEDGER` and we use this to obtain the canister ID. + /// - Otherwise, we use the mainnet cycled ledger canister ID, which is `um5iw-rqaaa-aaaaq-qaaba-cai`. + /// + /// # Panics + /// - If the `CANISTER_ID_CYCLES_LEDGER` environment variable is not a valid canister ID at + /// build time. + #[must_use] pub fn default_cycles_ledger() -> Principal { Principal::from_text( - option_env!("DFX_CYCLES_LEDGER_CANISTER_ID").unwrap_or("um5iw-rqaaa-aaaaq-qaaba-cai"), + option_env!("CANISTER_ID_CYCLES_LEDGER").unwrap_or("um5iw-rqaaa-aaaaq-qaaba-cai"), ) - .expect("Failed to parse cycles ledger canister ID") + .expect("Compile error: Failed to parse build env var 'CANISTER_ID_CYCLES_LEDGER' as a canister ID.") } } From e0871e5938b2374cf1c91e0d4cbec69a215e614e Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Wed, 18 Sep 2024 08:03:17 +0000 Subject: [PATCH 2/2] chore(deps): Bump ic-cdk from 0.15.1 to 0.16.0 (#11) Bumps [ic-cdk](https://github.com/dfinity/cdk-rs) from 0.15.1 to 0.16.0.
Commits

[![Dependabot compatibility score](https://dependabot-badges.githubapp.com/badges/compatibility_score?dependency-name=ic-cdk&package-manager=cargo&previous-version=0.15.1&new-version=0.16.0)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores) Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting `@dependabot rebase`. [//]: # (dependabot-automerge-start) [//]: # (dependabot-automerge-end) ---
Dependabot commands and options
You can trigger Dependabot actions by commenting on this PR: - `@dependabot rebase` will rebase this PR - `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it - `@dependabot merge` will merge this PR after your CI passes on it - `@dependabot squash and merge` will squash and merge this PR after your CI passes on it - `@dependabot cancel merge` will cancel a previously requested merge and block automerging - `@dependabot reopen` will reopen this PR if it is closed - `@dependabot close` will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually - `@dependabot show ignore conditions` will show all of the ignore conditions of the specified dependency - `@dependabot ignore this major version` will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself) - `@dependabot ignore this minor version` will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself) - `@dependabot ignore this dependency` will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Max --- Cargo.lock | 28 +++++++++++++++++++++------- Cargo.toml | 2 +- src/guard/Cargo.toml | 2 +- 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a188fc3..90f5295 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -244,7 +244,7 @@ name = "cycles-ledger-client" version = "0.1.0" dependencies = [ "candid", - "ic-cdk 0.15.1", + "ic-cdk 0.16.0", "serde", "serde_bytes", ] @@ -306,7 +306,7 @@ name = "example_app_backend" version = "0.1.0" dependencies = [ "candid", - "ic-cdk 0.15.1", + "ic-cdk 0.16.0", "ic-cdk-macros 0.15.0", "ic-papi-api", "pocket-ic", @@ -319,7 +319,7 @@ dependencies = [ "candid", "cycles-ledger-client", "example-paid-service-api", - "ic-cdk 0.15.1", + "ic-cdk 0.16.0", "ic-cdk-macros 0.15.0", "ic-papi-api", "ic-papi-guard", @@ -587,12 +587,12 @@ dependencies = [ [[package]] name = "ic-cdk" -version = "0.15.1" +version = "0.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "038ff230bf0fc8630943e3c52e989d248a7c89834ccb65da408fabc5817a475b" +checksum = "dd8ecacd682fa05a985253592963306cb9799622d7b1cce4b1edb89c6ec85be1" dependencies = [ "candid", - "ic-cdk-macros 0.15.0", + "ic-cdk-macros 0.16.0", "ic0 0.23.0", "serde", "serde_bytes", @@ -626,6 +626,20 @@ dependencies = [ "syn 2.0.58", ] +[[package]] +name = "ic-cdk-macros" +version = "0.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d4d857135deef20cc7ea8f3869a30cd9cfeb1392b3a81043790b2cd82adc3e0" +dependencies = [ + "candid", + "proc-macro2", + "quote", + "serde", + "serde_tokenstream 0.2.1", + "syn 2.0.58", +] + [[package]] name = "ic-papi-api" version = "0.1.0" @@ -642,7 +656,7 @@ version = "0.1.0" dependencies = [ "candid", "cycles-ledger-client", - "ic-cdk 0.15.1", + "ic-cdk 0.16.0", "ic-papi-api", "serde_bytes", ] diff --git a/Cargo.toml b/Cargo.toml index e39c984..d395e8a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,7 +5,7 @@ resolver = "2" [workspace.dependencies] pocket-ic = "4.0.0" candid = "0.10.10" -ic-cdk = "0.15.1" +ic-cdk = "0.16.0" ic-cdk-macros = "0.15.0" serde = "1" serde_bytes = "0.11" diff --git a/src/guard/Cargo.toml b/src/guard/Cargo.toml index ac3f330..11fa568 100644 --- a/src/guard/Cargo.toml +++ b/src/guard/Cargo.toml @@ -4,7 +4,7 @@ version = "0.1.0" edition = "2021" [dependencies] -ic-cdk = "0.15.0" +ic-cdk = "0.16.0" ic-papi-api = { workspace = true } candid = { workspace = true } cycles-ledger-client = { workspace = true }