-
Notifications
You must be signed in to change notification settings - Fork 97
feat: add fabricate-cycles command #2135
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
Merged
mergify
merged 6 commits into
master
from
SDK-426-provide-a-command-to-top-up-canisters-locally
Apr 21, 2022
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b4c2e00
feat: add local-top-up command
viviveevee 3273e51
move to dfx ledger fabricate-cycles
viviveevee 1dc13e3
Merge branch 'master' into SDK-426-provide-a-command-to-top-up-canist…
viviveevee 0d208bf
canister is a flag, not an argument
viviveevee 35616ea
add t flag
viviveevee 90ff97b
Merge branch 'master' into SDK-426-provide-a-command-to-top-up-canist…
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 hidden or 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 hidden or 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,44 @@ | ||
| #!/usr/bin/env bats | ||
|
|
||
| load ../utils/_ | ||
|
|
||
| setup() { | ||
| standard_setup | ||
|
|
||
| dfx_new hello | ||
| } | ||
|
|
||
| teardown() { | ||
| dfx_stop | ||
|
|
||
| standard_teardown | ||
| } | ||
|
|
||
| @test "ledger fabricate-cycles works with default amount" { | ||
| install_asset greet | ||
| dfx_start | ||
| dfx deploy | ||
| # default amount is 10 trillion cycles, which results in an amount like 13_899_071_239_420 | ||
| assert_command dfx ledger fabricate-cycles --canister "$(dfx canister id hello)" | ||
| # bash does not accept \d, use [0-9] instead | ||
| assert_match 'updated balance: [0-9]{2}(_[0-9]{3}){4} cycles' | ||
| assert_command dfx ledger fabricate-cycles --all | ||
| assert_match 'updated balance: [0-9]{2}(_[0-9]{3}){4} cycles' | ||
| } | ||
|
|
||
| @test "ledger fabricate-cycles works with specific amount" { | ||
| install_asset greet | ||
| dfx_start | ||
| dfx deploy | ||
| # adding 100 trillion cycles, which results in an amount like 103_899_071_239_420 | ||
| assert_command dfx ledger fabricate-cycles --canister "$(dfx canister id hello)" --amount 100000000000000 | ||
| assert_match 'updated balance: [0-9]{3}(_[0-9]{3}){4} cycles' | ||
| assert_command dfx ledger fabricate-cycles --canister hello --t 100 | ||
| assert_match 'updated balance: [0-9]{3}(_[0-9]{3}){4} cycles' | ||
| } | ||
|
|
||
| @test "ledger fabricate-cycles fails on real IC" { | ||
| install_asset greet | ||
| assert_command_fail dfx ledger --network ic fabricate-cycles --all | ||
| assert_match "Cannot run this on the real IC." | ||
| } |
This file contains hidden or 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,104 @@ | ||
| use crate::lib::environment::Environment; | ||
| use crate::lib::error::DfxResult; | ||
| use crate::lib::identity::identity_utils::CallSender; | ||
| use crate::lib::models::canister_id_store::CanisterIdStore; | ||
| use crate::lib::operations::canister; | ||
| use crate::lib::root_key::fetch_root_key_or_anyhow; | ||
| use crate::util::clap::validators::{cycle_amount_validator, trillion_cycle_amount_validator}; | ||
| use crate::util::expiry_duration; | ||
|
|
||
| use clap::Parser; | ||
| use ic_types::Principal; | ||
| use slog::info; | ||
| use std::time::Duration; | ||
|
|
||
| const DEFAULT_CYCLES_TO_FABRICATE: u128 = 10_000_000_000_000_u128; | ||
|
|
||
| /// Local development only: Fabricate cycles out of thin air and deposit them into the specified canister(s). | ||
| #[derive(Parser)] | ||
| pub struct FabricateCyclesOpts { | ||
| /// Specifies the amount of cycles to fabricate. Defaults to 10T cycles. | ||
| #[clap(long, validator(cycle_amount_validator), conflicts_with("t"))] | ||
| amount: Option<String>, | ||
|
|
||
| /// Specifies the amount of trillion cycles to fabricate. Defaults to 10T cycles. | ||
| #[clap( | ||
| long, | ||
| validator(trillion_cycle_amount_validator), | ||
| conflicts_with("amount") | ||
| )] | ||
| t: Option<String>, | ||
|
|
||
| /// Specifies the name or id of the canister to receive the cycles deposit. | ||
| /// You must specify either a canister name/id or the --all option. | ||
| #[clap(long)] | ||
| canister: Option<String>, | ||
|
|
||
| /// Deposit cycles to all of the canisters configured in the dfx.json file. | ||
| #[clap(long, required_unless_present("canister"))] | ||
| all: bool, | ||
| } | ||
|
|
||
| async fn deposit_minted_cycles( | ||
| env: &dyn Environment, | ||
| canister: &str, | ||
| timeout: Duration, | ||
| call_sender: &CallSender, | ||
| cycles: u128, | ||
| ) -> DfxResult { | ||
| let log = env.get_logger(); | ||
| let canister_id_store = CanisterIdStore::for_env(env)?; | ||
| let canister_id = | ||
| Principal::from_text(canister).or_else(|_| canister_id_store.get(canister))?; | ||
|
|
||
| info!(log, "Fabricating {} cycles onto {}", cycles, canister,); | ||
|
|
||
| canister::provisional_deposit_cycles(env, canister_id, timeout, call_sender, cycles).await?; | ||
|
|
||
| let status = canister::get_canister_status(env, canister_id, timeout, call_sender).await?; | ||
|
|
||
| info!( | ||
| log, | ||
| "Fabricated {} cycles, updated balance: {} cycles", cycles, status.cycles | ||
| ); | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| pub async fn exec(env: &dyn Environment, opts: FabricateCyclesOpts) -> DfxResult { | ||
| // amount has been validated by cycle_amount_validator | ||
| let cycles = cycles_to_fabricate(&opts); | ||
|
|
||
| fetch_root_key_or_anyhow(env).await?; | ||
|
|
||
| let timeout = expiry_duration(); | ||
|
|
||
| if let Some(canister) = opts.canister.as_deref() { | ||
| deposit_minted_cycles(env, canister, timeout, &CallSender::SelectedId, cycles).await | ||
| } else if opts.all { | ||
| let config = env.get_config_or_anyhow()?; | ||
| if let Some(canisters) = &config.get_config().canisters { | ||
| for canister in canisters.keys() { | ||
| deposit_minted_cycles(env, canister, timeout, &CallSender::SelectedId, cycles) | ||
| .await?; | ||
| } | ||
| } | ||
| Ok(()) | ||
| } else { | ||
| unreachable!() | ||
| } | ||
| } | ||
|
|
||
| fn cycles_to_fabricate(opts: &FabricateCyclesOpts) -> u128 { | ||
| if let Some(cycles_str) = &opts.amount { | ||
| //cycles_str is validated by cycle_amount_validator | ||
| cycles_str.parse::<u128>().unwrap() | ||
| } else if let Some(t_cycles_str) = &opts.t { | ||
| //cycles_str is validated by trillion_cycle_amount_validator | ||
| format!("{}000000000000", t_cycles_str) | ||
| .parse::<u128>() | ||
| .unwrap() | ||
| } else { | ||
| DEFAULT_CYCLES_TO_FABRICATE | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.