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

add macros to generate clap commands and args for modules in runtime #423

Merged
merged 49 commits into from
Jul 26, 2023

Conversation

dubbelosix
Copy link
Contributor

@dubbelosix dubbelosix commented Jun 21, 2023

Description

We currently let the users use sov-cli by passing in a json file that contains a json representation of the CallMessage

cargo run --bin sov-cli serialize-call src/sov-cli/test_data/minter_private_key.json Bank src/sov-cli/test_data/create_token.json 1

where src/sov-cli/test_data/create_token.json is

{
    "CreateToken": {
      "salt": 11,
      "token_name": "sov-test-token",
      "initial_balance": 1000,
      "minter_address": "sov15vspj48hpttzyvxu8kzq5klhvaczcpyxn6z6k0hwpwtzs4a6wkvqmlyjd6",
      "authorized_minters": ["sov15vspj48hpttzyvxu8kzq5klhvaczcpyxn6z6k0hwpwtzs4a6wkvqmlyjd6"]
    }
}

This PR adds an additional command to the sov-cli called generate-transaction, which allows the arguments to be specified on the command line.

cargo run --bin sov-cli generate-transaction bank create-token 11 sov-test-token 1000 sov15vspj48hpttzyvxu8kzq5klhvaczcpyxn6z6k0hwpwtzs4a6wkvqmlyjd6

The generated cli will have documentation based on doc comments in the CallMessage enum for each module:

% cargo run --features="cli" --bin sov-cli generate-transaction bank
    Finished dev [unoptimized + debuginfo] target(s) in 0.30s
     Running `/Users/dubbelosix/sovereign/target/debug/sov-cli module bank`
Usage: sov-cli generate-transaction bank <COMMAND>

Commands:
  create-token  Creates a new token with the specified name and initial balance
  transfer      Transfers a specified amount of tokens to the specified address
  burn          Burns a specified amount of tokens
  mint          Mints a specified amount of tokens
  freeze        Freeze a token so that the supply is frozen
  help          Print this message or the help of the given subcommand(s)

Options:
  -h, --help  Print help

Updates July 20 (by @preston-evans98)

This PR has been significantly refactored to address a number of limitations. Now, the cli_parser macro no longer requires the Context type to be provided as part of the annotation. Instead, the cli methods are generated generically so that any context can be configured in the resulting binary.

Remaining work:

  • Hide clap dependency behind sov_modules_api
  • Refactor cli_parser as a derive macro instead of an attribute
  • Refactor cli_skip to be an attribute instead of an arg to cli_parser
  • Wrap clap::Parser for structs and standard enums behind our CliWalletArg macro

Linked Issues

Testing

Tested using try_build

Docs

Doc comments have been added as appropriate.

@dubbelosix dubbelosix marked this pull request as ready for review June 22, 2023 06:10
@bkolad
Copy link
Member

bkolad commented Jun 22, 2023

Thanks for the work! The module system now makes clap firs class citizen which is a bit unfortunate. Would it be possible to convert the module CallMessages to some intermediate structures (which we can even serialize to a file, something like abi in ethereum) and generate the clap options from this structures? I looks like we could achieve this by using the clap builder pattern. This way we decouple the dependency + we can use the structures to generate clis in other programming languages or even web clients.

I understand this might be bigger chunk of work (and a will require some research) so I am fine with merging the PR, but I would like to discuss the intermediate structures approach on the daily.

@preston-evans98 preston-evans98 marked this pull request as ready for review July 24, 2023 19:48
@codecov
Copy link

codecov bot commented Jul 25, 2023

Codecov Report

Merging #423 (738b122) into nightly (fd58400) will decrease coverage by 0.3%.
The diff coverage is 77.5%.

Files Changed Coverage Δ
adapters/celestia/src/celestia.rs 56.6% <0.0%> (-1.7%) ⬇️
...em/module-implementations/sov-accounts/src/call.rs 75.0% <0.0%> (-5.8%) ⬇️
...system/module-implementations/sov-bank/src/call.rs 87.5% <0.0%> (-7.7%) ⬇️
...ystem/module-implementations/sov-bank/src/token.rs 81.9% <0.0%> (-16.7%) ⬇️
...implementations/sov-sequencer-registry/src/call.rs 87.2% <0.0%> (-6.0%) ⬇️
...dule-system/sov-modules-api/src/default_context.rs 84.2% <0.0%> (-4.7%) ⬇️
...le-system/sov-modules-api/src/default_signature.rs 52.2% <0.0%> (-3.4%) ⬇️
module-system/sov-modules-api/src/lib.rs 61.4% <0.0%> (-6.0%) ⬇️
rollup-interface/src/state_machine/mocks.rs 62.0% <0.0%> (-5.5%) ⬇️
module-system/sov-modules-macros/src/cli_parser.rs 84.7% <83.6%> (-13.8%) ⬇️
... and 2 more

... and 1 file with indirect coverage changes

Copy link
Member

@bkolad bkolad left a comment

Choose a reason for hiding this comment

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

Thanks a lot, Preston! I had a couple of old comments about introducing thiserror: https://github.com/Sovereign-Labs/sovereign-sdk/pull/423/files#r1238171744. I think you have mentioned there were some problems with doing this way, right? If this is the case, feel free to close them.

@preston-evans98 preston-evans98 enabled auto-merge (squash) July 26, 2023 15:27
@preston-evans98 preston-evans98 merged commit 824bfa6 into nightly Jul 26, 2023
@preston-evans98 preston-evans98 deleted the dub/module_call_clap branch July 26, 2023 16:00
preston-evans98 added a commit that referenced this pull request Sep 14, 2023
…423)

* module clap cli draft pr

* temp commit working state

* add impls for other fields of Runtime

* working code

* fix broken test

* fix cargo hack

* missing file

* fix linting

* lint fix

* WIP: continue cleanup

* Use anyhow::Error as FromStr error

* Rustc panics here

* Fix bug in cli_parser bounds extraction

* It works

* Fix warnings/lints

* Defer serde bounds enforcement

* Remove even more deserialize bounds

* Update CLI with inline tx generation

* Fix doc test; improve hygiene

* cleanup; make hex optional

* remove duplicate Signature defn

* Fix unused import

* remove evm cli for now

* fix warnings without default features

* Fix unused import

* Fix evm skipping

* Fix formatting

* Fix bug in cli make batch

* Get cli_skip working

* Convert to derive macro

* derive CliWalletArg for structs

* Fix doc tests

* test cli_wallet_arg derive

* revert lockfile

* Remove lockfile from git

* lint

* Add missing cfg_attr

* Remove commented code

* fix doc test

* Address code review feedback

---------

Co-authored-by: dubbelosix <[email protected]>
Co-authored-by: Preston Evans <[email protected]>
@citizen-stig citizen-stig linked an issue May 2, 2024 that may be closed by this pull request
4 tasks
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.

Tracking Issue: sov-cli improvements
4 participants