Skip to content
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

[cli] Add automatic gas estimation, and move all CLI client commands to use it #16503

Closed

Conversation

stefan-mysten
Copy link
Contributor

@stefan-mysten stefan-mysten commented Mar 2, 2024

Description

This PR adds automatic gas estimation support for all CLI client commands by default. Providing a specific gas budget is still possible through the --gas-budget option.

Note this also fixes a problem with the way CLI PTB tests are executed, which led to issues (cannot find test files).

Test Plan

Added new tests + existing tests.
cargo nextest run --test cli_tests


If your changes are not user-facing and do not break anything, you can skip the following section. Otherwise, please briefly describe what has changed under the Release Notes section.

Type of Change (Check all that apply)

  • protocol change
  • user-visible impact
  • breaking change for a client SDKs
  • breaking change for FNs (FN binary must upgrade)
  • breaking change for validators or node operators (must upgrade binaries)
  • breaking change for on-chain data layout
  • necessitate either a data wipe or data migration

Release notes

The CLI client commands do not require to provide a --gas-budget value anymore, as it is automatically inferred through a dry run. It is still possible to provide one, if the user wants.

@stefan-mysten stefan-mysten requested a review from a team March 2, 2024 19:32
Copy link

vercel bot commented Mar 2, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
explorer ✅ Ready (Inspect) Visit Preview 💬 Add feedback Mar 14, 2024 10:10pm
multisig-toolkit ✅ Ready (Inspect) Visit Preview 💬 Add feedback Mar 14, 2024 10:10pm
mysten-ui ✅ Ready (Inspect) Visit Preview 💬 Add feedback Mar 14, 2024 10:10pm
sui-core ✅ Ready (Inspect) Visit Preview 💬 Add feedback Mar 14, 2024 10:10pm
sui-kiosk ✅ Ready (Inspect) Visit Preview 💬 Add feedback Mar 14, 2024 10:10pm
sui-typescript-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Mar 14, 2024 10:10pm

@stefan-mysten stefan-mysten changed the title [cli] Moved all CLI commands to automatically infer gas budget. [cli] Add automatic gas budget, and move all CLI commands to using it Mar 3, 2024
@stefan-mysten stefan-mysten changed the title [cli] Add automatic gas budget, and move all CLI commands to using it [cli] Add automatic gas estimation, and move all CLI commands to using it Mar 3, 2024
@stefan-mysten stefan-mysten changed the title [cli] Add automatic gas estimation, and move all CLI commands to using it [cli] Add automatic gas estimation, and move all CLI commands to use it Mar 3, 2024
@stefan-mysten stefan-mysten changed the title [cli] Add automatic gas estimation, and move all CLI commands to use it [cli] Add automatic gas estimation, and move all CLI client commands to use it Mar 3, 2024
Copy link
Contributor

@amnn amnn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the change overall! Main thing I'd love to change before we land this is how the transactions are created.

Today, you are duplicating the logic involved in creating the TransactionData, once for the gas estimation and another time to actually run it. This is not great from a code repetition perspective, but it's also not good from an efficiency or consistency perspective. Some of these transaction building commands do a fair bit of work, talking to RPC to resolve ObjectIDs into ObjectRefs etc, you don't want to redo that work because it's not cheap, and in general you may get a different answer from run to run, and while that's usually going to mean the transaction execution will fail, we should still return a consistent response in that case (e.g. the transaction you dry ran is the same as the one that you executed).

The fix is to split the logic for generating transactions into three phases:

  • Build the TransactionKind -- this is usually where most of the effort and consistency concerns go, and we can always do this once because the gas budget is not relevant for this part.
  • Build the TransactionData for dry-run, from a clone of the TransactionKind.
  • Build the TransactionData for execution, using the gas budget you got from dry-run, and execute it.

crates/sui/src/client_commands.rs Show resolved Hide resolved
crates/sui/src/client_commands.rs Outdated Show resolved Hide resolved
) -> Result<u64, anyhow::Error> {
let client = context.get_client().await?;
let dry_run = client.read_api().dry_run_transaction_block(tx).await?;
Ok(dry_run.effects.gas_cost_summary().gas_used())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we like to add a "fudge factor" to the estimated gas budget to account for differences due to shared object usage (or Clock, or Randomness, etc). I believe it's about 10%, but the source of truth is the equivalent logic in TS SDK.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added, let me know if you have any feedback. Here's the TypeScript SDK code for this.

@stefan-mysten
Copy link
Contributor Author

Superseded by #17322

@stefan-mysten stefan-mysten deleted the cli_gas_management branch April 24, 2024 22:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants