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

feat: add capability to reformat Transactions before broadcasting #12936

Merged
merged 9 commits into from
Aug 30, 2022

Conversation

0a1c
Copy link
Contributor

@0a1c 0a1c commented Aug 16, 2022

Description

Increase customized signing capabilities by:

  • Adding a hook to preprocess transactions before broadcasting

This PR introduces the ability to preprocess transactions before broadcasting, a feature that provides the ability to add extensions, or otherwise customize the payload to satisfy a chain's requirements. For adaptability, the parameters include the keytype to allow differentiation between formatting transactions for Ledger vs. software keys.

PR

This PR introduces a hook to preprocess transactions, chiefly to add extensions, from a higher-level chain. Key changes and relevant files:

  • Add PreprocessTxFn type and fields to both Context and Factory
  • Instantiate the Factory with the PreprocessTxFn provided by the Context
  • Call PreprocessTx with the appropriate parameters after signing

Closes: #XXXX


Author Checklist

All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.

I have...

  • included the correct type prefix in the PR title
  • added ! to the type prefix if API or client breaking change
  • targeted the correct branch (see PR Targeting)
  • provided a link to the relevant issue or specification
  • followed the guidelines for building modules
  • included the necessary unit and integration tests
  • added a changelog entry to CHANGELOG.md
  • included comments for documenting Go code
  • updated the relevant documentation or specification
  • reviewed "Files changed" and left comments if necessary
  • confirmed all CI checks have passed

Reviewers Checklist

All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.

I have...

  • confirmed the correct type prefix in the PR title
  • confirmed ! in the type prefix if API or client breaking change
  • confirmed all author checklist items have been addressed
  • reviewed state machine logic
  • reviewed API design and naming
  • reviewed documentation is accurate
  • reviewed tests and test coverage
  • manually tested (if applicable)

@0a1c 0a1c requested a review from a team as a code owner August 16, 2022 17:06
@github-actions github-actions bot added the C:CLI label Aug 16, 2022
@@ -18,6 +18,9 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
)

// ReformatTx allows chains to optionally reformat transactions before broadcasting
type ReformatTxFn func(chainID string, key keyring.KeyType, tx TxBuilder) error

Choose a reason for hiding this comment

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

This function signature is very non-intuitive. How does the reformatting occur? Where is the tx itself? Why is the TxBuilder called tx? How is someone who wants to reformat a tx meant to express their transformation?

A reformatter should take one version of a thing, and return the reformatted version.

type ReformatTxFn func(txBytes []byte) ([]byte, error)

If you intend to take an abstract Tx interface, then this isn't a Reformatter, it's just a Formatter, I suppose?

Copy link
Contributor

@alexanderbez alexanderbez left a comment

Choose a reason for hiding this comment

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

Do you have an example of such a function and/or it being used?

@amaury1093
Copy link
Contributor

a feature that provides the ability to add extensions, or otherwise customize the payload to satisfy a chain's requirements

The SDK's txbuilder already has a SetExtensionOptions method to add extensions. Also not sure what kind of payload cystomization you're talking about, can you give use cases?

@0a1c
Copy link
Contributor Author

0a1c commented Aug 17, 2022

@alexanderbez @AmauryM

Here is an example along the lines of what Evmos would use to reformat EIP-712-signed Ledger transactions for Cosmos. The context would use that function like this:

ctx = ctx.WithReformatTx(eip712.ReformatLedgerTx)

The main point is that higher level chains currently do not have access to the payload between generating, signing and broadcasting. With this PR, chains can apply custom logic to the TxBuilder before it is broadcasted.

@amaury1093
Copy link
Contributor

Ah I see, it's like a client-side hook on when we generate the tx. SGTM, though agreed with #12936 (comment) about naming, maybe we can add the word "Hook" in there? Would like to see a test case in the SDK as well.

@codecov
Copy link

codecov bot commented Aug 17, 2022

Codecov Report

Merging #12936 (420a025) into main (4fe7797) will decrease coverage by 2.51%.
The diff coverage is 64.06%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main   #12936      +/-   ##
==========================================
- Coverage   55.87%   53.36%   -2.52%     
==========================================
  Files         646      648       +2     
  Lines       54895    55037     +142     
==========================================
- Hits        30675    29372    -1303     
- Misses      21762    23330    +1568     
+ Partials     2458     2335     -123     
Impacted Files Coverage Δ
baseapp/abci.go 64.09% <0.00%> (-0.33%) ⬇️
client/context.go 55.37% <0.00%> (-0.91%) ⬇️
client/flags/flags.go 19.35% <0.00%> (-0.32%) ⬇️
client/rpc/status.go 66.66% <ø> (ø)
client/utils.go 34.92% <0.00%> (ø)
server/config/config.go 38.00% <0.00%> (-1.59%) ⬇️
server/rosetta/client_online.go 1.35% <0.00%> (ø)
server/swagger.go 0.00% <0.00%> (ø)
testutil/list.go 0.00% <0.00%> (ø)
types/result.go 76.74% <ø> (ø)
... and 109 more

@0a1c
Copy link
Contributor Author

0a1c commented Aug 23, 2022

@AmauryM Renamed and added test case

Copy link
Contributor

@amaury1093 amaury1093 left a comment

Choose a reason for hiding this comment

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

LGTM! Let's add a changelog and this is good to go

@0a1c 0a1c force-pushed the evmos/reformat-tx branch from 4855260 to 7099011 Compare August 23, 2022 18:11
@0a1c
Copy link
Contributor Author

0a1c commented Aug 23, 2022

@AmauryM Updated with changelog entry

Copy link
Member

@julienrbrt julienrbrt left a comment

Choose a reason for hiding this comment

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

lgtm! Can you fix the linting errors (make lint-fix might do it, otherwise manually)?

go.work.sum Outdated
@@ -1 +1,517 @@
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 h1:Ovs26xHkKqVztRpIrF/92BcuyuQ/YW4NSIpoGtfXNho=
Copy link
Member

Choose a reason for hiding this comment

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

You should rebase, this file is in the .gitignore.

@0a1c
Copy link
Contributor Author

0a1c commented Aug 29, 2022

@julienrbrt @AmauryM
Fixed and updated

@julienrbrt julienrbrt added the A:automerge Automatically merge PR once all prerequisites pass. label Aug 29, 2022
@mergify mergify bot merged commit f8b55ff into cosmos:main Aug 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A:automerge Automatically merge PR once all prerequisites pass. C:CLI
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants