-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: wallet_call_with_max_cycles #170
Merged
Merged
Changes from 12 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
c74346a
feat: wallet_call_with_max_cycles
sesi200 53788d7
bump rust
sesi200 fa76057
add test, use margin
sesi200 e9f0c11
shellcheck
sesi200 775d703
update dfx
sesi200 202d3a3
use --storage-mode
sesi200 be1defc
update dfx in other places
sesi200 ce23cea
gzipped wallet.wasm?
sesi200 b6c6cdf
update dfx.json to use .gz
sesi200 b137ba6
revert dfx upgrade
sesi200 e0f9cfc
report how many cycles were attached
sesi200 eed2dab
revert whitespace change
sesi200 d96b5f9
I'm not a time traveller, honest!
sesi200 3237503
rm explicit rust version install
sesi200 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#!/usr/bin/env bats | ||
|
||
# shellcheck source=/dev/null | ||
source "$BATS_SUPPORT/load.bash" | ||
|
||
load util/assertions | ||
|
||
setup() { | ||
# We want to work from a temporary directory, different for every test. | ||
x=$(mktemp -d -t dfx-usage-env-home-XXXXXXXX) | ||
cd "$x" || exit | ||
export DFX_CONFIG_ROOT=$x | ||
|
||
dfx new --no-frontend e2e_project | ||
cd e2e_project || exit 1 | ||
dfx start --background --clean | ||
} | ||
|
||
teardown() { | ||
dfx stop | ||
rm -rf "$DFX_CONFIG_ROOT" | ||
} | ||
|
||
@test "wallet_call_with_max_cycles" { | ||
dfx identity new alice | ||
dfx identity new bob | ||
WALLET_ALICE=$(dfx --identity alice identity get-wallet) | ||
WALLET_BOB=$(dfx --identity bob identity get-wallet) | ||
|
||
ALICE_CYCLES_BEFORE_SEND=$(dfx --identity alice wallet balance | sed 's/[^0-9]//g') | ||
if (( ALICE_CYCLES_BEFORE_SEND < 2000000000000 )); then | ||
echo "alice has unexpectedly few cycles before sending: ${ALICE_CYCLES_BEFORE_SEND}" | ||
exit 1 | ||
fi | ||
|
||
# non-controller can't make the call | ||
assert_command_fail dfx --identity bob canister call "${WALLET_ALICE}" wallet_call_with_max_cycles "(record { canister = principal \"${WALLET_BOB}\"; method_name = \"wallet_receive\"; args = blob \"\44\49\44\4c\00\00\"; })" | ||
|
||
assert_command dfx --identity alice canister call "${WALLET_ALICE}" wallet_call_with_max_cycles "(record { canister = principal \"${WALLET_BOB}\"; method_name = \"wallet_receive\"; args = blob \"\44\49\44\4c\00\00\"; })" | ||
|
||
# has less than 0.2T cycles afterwards | ||
ALICE_CYCLES_AFTER_SEND=$(dfx --identity alice wallet balance | sed 's/[^0-9]//g') | ||
if (( ALICE_CYCLES_AFTER_SEND > 200000000000 )); then | ||
echo "expected alice to have <1TC after wallet_call_with_max_cycles, actually has ${ALICE_CYCLES_AFTER_SEND}, before was ${ALICE_CYCLES_BEFORE_SEND}" | ||
exit 1 | ||
fi | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
[toolchain] | ||
channel = "1.70.0" | ||
channel = "1.74.1" | ||
components = ["clippy", "rustfmt"] | ||
targets = ["wasm32-unknown-unknown"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -892,6 +892,13 @@ mod wallet { | |
r#return: Vec<u8>, | ||
} | ||
|
||
#[derive(CandidType, Deserialize)] | ||
struct CallResultWithMaxCycles { | ||
#[serde(with = "serde_bytes")] | ||
r#return: Vec<u8>, | ||
attached_cycles: u128, | ||
} | ||
|
||
/// Forward a call to another canister. | ||
#[update(guard = "is_custodian_or_controller", name = "wallet_call")] | ||
async fn call( | ||
|
@@ -935,6 +942,39 @@ mod wallet { | |
)), | ||
} | ||
} | ||
|
||
#[derive(CandidType, Deserialize)] | ||
struct CallWithMaxCyclesArgs { | ||
canister: Principal, | ||
method_name: String, | ||
args: Vec<u8>, | ||
} | ||
|
||
#[update( | ||
guard = "is_custodian_or_controller", | ||
name = "wallet_call_with_max_cycles" | ||
)] | ||
async fn call_with_max_cycles( | ||
args: CallWithMaxCyclesArgs, | ||
) -> Result<CallResultWithMaxCycles, String> { | ||
let available_cycles = ic_cdk::api::canister_balance128(); | ||
// If no margin is used then the call either fails locally with `Couldn't send message` or processing the response traps with `Canister out of cycles`. | ||
// On the local network the margin needs to be ~1.7B cycles. (Experimentally determined in August 2024) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Time travel ¯_(ツ)_/¯ |
||
// Extrapolating, a margin of 100B should work up to a subnet of ~60 nodes. | ||
const MARGIN: u128 = 100_000_000_000; | ||
let cycles_to_attach = available_cycles.saturating_sub(MARGIN); | ||
let result = call128(CallCanisterArgs { | ||
canister: args.canister, | ||
method_name: args.method_name, | ||
args: args.args, | ||
cycles: cycles_to_attach, | ||
}) | ||
.await?; | ||
Ok(CallResultWithMaxCycles { | ||
r#return: result.r#return, | ||
attached_cycles: cycles_to_attach, | ||
}) | ||
} | ||
} | ||
|
||
/*************************************************************************************************** | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can remove the
rust: ['1.74.1']
and the following "Install Rust" step from workflow files.GitHub Action runners default to have Rust. And
rust-toolchain.toml
will automatically take effect.Therefore, we will only need the Rust version in
rust-toolchain.toml
which is more maintainable.