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

Remove module-level ModuleCdc, use the global legacy.Cdc #11232

Closed
4 tasks
amaury1093 opened this issue Feb 18, 2022 · 3 comments · Fixed by #11240
Closed
4 tasks

Remove module-level ModuleCdc, use the global legacy.Cdc #11232

amaury1093 opened this issue Feb 18, 2022 · 3 comments · Fixed by #11240

Comments

@amaury1093
Copy link
Contributor

Summary

Replace all modules' individual ModuleCdc with the global legacy.Cdc

Problem Definition

Each module currently define it's own amino ModuleCdc, which is used to serialize Msgs for amino signing. Each ModuleCdc registers the module's own Msg types, via the RegisterLegacyAminoCodec method.

We have a problem when using nested Msgs. An example is authz's MsgExec. Authz's RegisterLegacyAminoCodec registers MsgExec, but it doesn't register e.g. a nested bank MsgSend. So authz's ModuleCdc is not aware of the bank msg types, leading to weird Amino JSON encoding: cosmos/cosmjs#1026.

We'll have the same problem for group, gov, and any other external module that uses nested Msgs.

Proposal

@RiccardoM proposed an elegant solution in #11224. Basically, instead of each module defining its own ModuleCdc, they all rely on the global legacy.Cdc from "github.com/cosmos/cosmos-sdk/codec/legacy".

Each module actually then call RegisterLegacyAminoCodec(legacy.Cdc) on that single global legacy.Cdc.

All module developers ideally change their code too

We've been using authz in production since v0.43, and people were able to sign with Ledger. So this proposal is actually optional security-wise (I think) or functioanlity-wise.

It's just that without this proposal, it's a PITA for clients like CosmJS to decode. So if we decide to do this change, it's highly recommended for all module developers to follow. It's consensus-breaking though.

All modue developers need to do the following 2 changes.

  1. Remove ModuleCdc, register using legacy.Cdc
// x/mymodule/codec.go

+ import "github.com/cosmos/cosmos-sdk/codec/legacy"

func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) { ... }

- var (
-	amino = codec.NewLegacyAmino()

-	// ModuleCdc references the global x/bank module codec. Note, the codec should
-	// ONLY be used in certain instances of tests and for JSON encoding as Amino is
-	// still used for that purpose.
-	//
-	// The actual codec used for serialization should be provided to x/staking and
-	// defined at the application level.
-	ModuleCdc = codec.NewAminoCodec(amino)
)

func init() {
-	RegisterLegacyAminoCodec(amino)
+	RegisterLegacyAminoCodec(legacy.Cdc)
-	cryptocodec.RegisterCrypto(amino)
}
  1. For each Msg in the module, change GetSignBytes to use the global legacy.Cdc
// x/mymodule/codec.go

+ import "github.com/cosmos/cosmos-sdk/codec/legacy"

// GetSignBytes Implements Msg.
func (msg MsgWhatever) GetSignBytes() []byte {
-	return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg))
+	return sdk.MustSortJSON(legacy.Cdc.MustMarshalJSON(&msg))
}

Proposal

Perform the 2 changes described above for all SDK modules. Note that this has already been done in authz in #11224


For Admin Use

  • Not duplicate issue
  • Appropriate labels applied
  • Appropriate contributors tagged
  • Contributor assigned/self-assigned
@tac0turtle
Copy link
Member

should this live within the framework eg or are we fine with it on its own.

@robert-zaremba
Copy link
Collaborator

I think the only long term solution is to use top-down approach as we do with proto codec.

@RiccardoM
Copy link
Contributor

I think the only long term solution is to use top-down approach as we do with proto codec.

I agree with you, but at the same time I think we can use this approach to properly support Ledger signing for the x/authz module until a better fix is found since this solution has no particular drawback

@mergify mergify bot closed this as completed in #11240 Feb 23, 2022
mergify bot pushed a commit that referenced this issue Feb 23, 2022
## Description
Replaces all the various `ModuleCdc` instances with the global `legacy.Cdc` to properly support Amino serialization for the `x/authz` module. This is needed in order to correctly support Ledger signing for `MsgExec` and messages.

Closes: #11232



---

### 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...

- [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [x] provided a link to the relevant issue or specification
- [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules)
- [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing)
- [ ] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [x] 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](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) 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)
RiccardoM added a commit to desmos-labs/cosmos-sdk that referenced this issue Feb 24, 2022
## Description
Replaces all the various `ModuleCdc` instances with the global `legacy.Cdc` to properly support Amino serialization for the `x/authz` module. This is needed in order to correctly support Ledger signing for `MsgExec` and messages.

Closes: cosmos#11232

---

### 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...

- [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [x] provided a link to the relevant issue or specification
- [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules)
- [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing)
- [ ] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [x] 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](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) 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)

(cherry picked from commit ff31490)
leobragaz pushed a commit to desmos-labs/cosmos-sdk that referenced this issue Apr 5, 2022
## Description
Replaces all the various `ModuleCdc` instances with the global `legacy.Cdc` to properly support Amino serialization for the `x/authz` module. This is needed in order to correctly support Ledger signing for `MsgExec` and messages.

Closes: cosmos#11232

---

### 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...

- [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [x] provided a link to the relevant issue or specification
- [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules)
- [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing)
- [ ] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [x] 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](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) 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)

(cherry picked from commit ff31490)
RiccardoM added a commit to desmos-labs/cosmos-sdk that referenced this issue Oct 25, 2022
## Description
Replaces all the various `ModuleCdc` instances with the global `legacy.Cdc` to properly support Amino serialization for the `x/authz` module. This is needed in order to correctly support Ledger signing for `MsgExec` and messages.

Closes: cosmos#11232

---

### 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...

- [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [x] provided a link to the relevant issue or specification
- [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules)
- [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing)
- [ ] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [x] 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](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) 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)

(cherry picked from commit ff31490)
RiccardoM added a commit to desmos-labs/cosmos-sdk that referenced this issue Nov 14, 2022
## Description
Replaces all the various `ModuleCdc` instances with the global `legacy.Cdc` to properly support Amino serialization for the `x/authz` module. This is needed in order to correctly support Ledger signing for `MsgExec` and messages.

Closes: cosmos#11232

---

### 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...

- [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [x] provided a link to the relevant issue or specification
- [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules)
- [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing)
- [ ] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [x] 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](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) 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)

(cherry picked from commit ff31490)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants