chevdor / subwasm

Subwasm is a cli utility to look inside a Substrate WASM Runtime. It can inspect and compare the metadata of Substrate based runtimes such as Polkadot or Kusama.
MIT License
62 stars 25 forks source link

feat: add option to output metadata to a file #61

Closed trusch closed 1 year ago

trusch commented 1 year ago

This adds a --output flag to the metadata subcommand. If specified, the prefixed scale-encoded metadata is written to the file as expected by for example subxt or the polkadotjs/api augmentation scripts. If the global --json flag is set, the json of the prefixed metadata is written to the file.

The normal json output is not changed to not break backward compability.

We need this because we want to expose the metadata of our chain directly from CI and don't want to spin up a local parachain setup just to query the metadata from a live node.

Ps: I really like this tool, getting the metadata directly from the wasm is super nice!

edit: Fixes https://github.com/chevdor/subwasm/issues/58

trusch commented 1 year ago

Currently (in this PR) subwasm meta --output <output-file> --json <wasm-blob> would write the json encoded version of the metadata into and would not store the metadata as hex encoded scale within a json object..

But actually I like the idea of combining --json and --scale to produce something like {"result": "0xdeadbeaf..."}, thats exactly the format that polkadotjs expects.

Ok, let summarize:

If we agree on that I'll restructure the main to reflect that :)

chevdor commented 1 year ago
  • subwasm meta <wasm>

    • outputs json formatted RuntimeMetadata to stdout

No this is not the current behavior, currently it outputs a human friendly list of pallets and the user can dig in using an extra -m (for module)

  • subwasm meta --json <wasm>

    • same as above

Not same as above, this one shows the raw json version

  • subwasm meta --scale <wasm>

    • outputs hex(scale(prefixed_metadata)), i.e. 0xdeadbeaf...

yep, that would be great, I guess you mean to stdout here

  • subwasm meta --scale --json <wasm>

    • outputs a json object with result key set to hex(scale(prefixed_metadata)), i.e. {"result": "0xdeadbeaf..."}

fine by me

  • subwasm meta --output <file> <wasm>

    • outputs json formatted RuntimeMetadata to

We may drop --output totally I think, and we let users do subwasm meta ... > output.ext.

  • subwasm meta --output <file> --json <wasm>

    • same as above

any "same than above" is likley not required

  • subwasm meta --output <file> --scale <wasm>

    • outputs scale(prefixed_metadata) to

see above

  • this is the format expected by subxt
    • subwasm meta --output <file> --scale --json <wasm>

can we not drop --output and simply use:

subwasm meta --scale --json <wasm> > <file>
  • outputs a json object with result key set to hex(scale(prefixed_metadata)) to , i.e. {"result": "0xdeadbeaf..."}
  • this is the format expected by polkadotjs

If we have several "format" for --scale we may be better off using: -- scale <format> with romat one of: subxt | polkadotjs (or whatever names...)

trusch commented 1 year ago

I just discovered that there are even emojis in the human readable output when specifying a module, lfg! Ok, new UX proposal:

We have a flag --format <human | json | scale | json+scale | hex+scale > that defaults to human and we always print to stdout (eventually refusing to write raw scale to a terminal). Thats the least confusing in my opinion.

Does that sound better?

chevdor commented 1 year ago

Yes that sounds very good actually 👍

I indeed would like to keep human as default. Automation can do the effort of adding flags but humans are usually lazy :)

I am not against some --output file, it was so far just not required but I see it could benefit some use case and prevent a few subwasm.... | tee <file>. If we introduce it, it should however just do what users expect: send the output (whaetver it is) to the provided file, and not make any assumption on the format.

In theory, you may want to even do stuff like --format human --output /tmp/foo.bar

Having such a new output flag, we could make it mandatory for raw scale and set a default filename such as metadata_scale_raw.bin to keep it simple for instance.

Cherry on top, for the default file, we may want to keep track of what is the appropriate extension for a given format. For instance:

Those will be user overridable anyway to we help making it easy without getting in the way.

trusch commented 1 year ago

@chevdor that took a little more effort than anticipated, but besides a few minor warnings (error handling in those macros is hard) it should now do exactly what we want.

Only thing I don't know what to do is with the --json flag. Should we respect it for backward compability and translate it to "--format json"?

chevdor commented 1 year ago

@chevdor that took a little more effort than anticipated, but besides a few minor warnings (error handling in those macros is hard) it should now do exactly what we want.

Yes, I do see the extra work, thanks a lot for that !

Only thing I don't know what to do is with the --json flag. Should we respect it for backward compability and translate it to "--format json"?

Yes I want to be extra cautious with that since parity and probably other users may be counting on that. We could either:

I think the first option is cleaner.

trusch commented 1 year ago

@chevdor Broken Pipe errors are now ignored again + there is now a warning about the deprecation of --json when you use it in the context of retrieving metadata. Error handling is also much better now.

Hope you like it now :)

chevdor commented 1 year ago

Nice work on that PR @trusch, thank you!

dzmitry-lahoda commented 1 year ago

tried to use output of subwasm as input to subxt. not worked.

chevdor commented 1 year ago

Can you please open a new issue and provide more details ? I just found out that this PR introduces a regression that I am fixing now. If this is the issue you are running into, I may have a workaround for you for now.

dzmitry-lahoda commented 1 year ago

https://github.com/chevdor/subwasm/issues/69