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

Cosmos Scalars #9694

Closed
4 tasks
amaury1093 opened this issue Jul 13, 2021 · 5 comments · Fixed by #9933
Closed
4 tasks

Cosmos Scalars #9694

amaury1093 opened this issue Jul 13, 2021 · 5 comments · Fixed by #9933
Assignees
Labels

Comments

@amaury1093
Copy link
Contributor

amaury1093 commented Jul 13, 2021

Summary

Introduce the notion of cosmos scalars.

Part of #7091.

Problem Definition

We decided to use strings to represent addresses in proto definitions. A string can mean anything, and it sometimes be hard to guess that the string actually represents an address.

This issue introduces the notion of a Cosmos "Scalar" type (similar to the notion of Scalar type in GraphQL), which would just be simple aliases that give human-understable meaning to the underlying type, both in Go code and in Proto definitions.

How it looks like

Introduce the following Cosmos scalar types (naming to be confirmed):

  • cosmos.AddressString: the string representation of an address.
  • cosmos.AddressBytes: address as bytes,
  • cosmos.Int: integers
  • cosmos.Dec: decimals

Other scalars can be introduced in the future.

Usage in Protobuf

message MyMsgSend {
  // annotations either using string
  string sender = 1 [(cosmos_proto.scalar) = "cosmos.AddressString"]; // so that clients know that `sender` holds the string repr of an address
  // or using enum
  bytes  rcpt = 2   [(cosmos_proto.scalar) = cosmos_proto.ADDRESS_BYTES];
}

Usage in Go Code

The above code would generate in Go (with cosmos-proto):

// somewhere in the SDK
type AddressString string
type AddressBytes bytes

// in the .pb.go file
type MyMsgSend struct {
  Sender sdk.AddressString
  Rcpt sdk.AddressBytes
}

Proposal

Go through all the proto files, and add annotations to all relevant fields.

Types we need add annotation for:

  • AddressString
  • AddressBytes
  • Int
  • Dec

Open questions:

  • Names to be confirmed
  • cosmos_proto.scalar = : string or enum (do we want people to extend scalars)?

cc @aaronc @cyberbono3


For Admin Use

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

ACK 👍

@fdymylja
Copy link
Contributor

fdymylja commented Jul 14, 2021

I'm not a fan of changing internal structure of protobuf objects as it's done by gogoproto "customtype", it means that every protobuf generator used needs to be aware of those definitions and how they change structures internally.

First pain-point it's gonna be in ORM codegen, which will need to also understand to cast the type too for example when someone is wishing to index every balance by address which should be a protobuf string but needs to be casted to the custom type.

I think it would be fine to mark how certain types should be decoded, but I wouldn't go as far as mangling with protobuf object structures, for the sake of more mantainable and losely coupled code-bases.

I'm bringing this up because I'm currently working on the ORM codegen and I'm constantly hitting walls due to gogoproto, incompatibility with the protobuf plugin, and the need to be aware of gogoproto options to parse objects and generate clients in the correct way.

@aaronc
Copy link
Member

aaronc commented Jul 14, 2021

So the idea with the new codegen would be to properly implement protoreflect.Message so that other tools can use that rather than reflect over golang fields like you'd need to do with gogo. That is the correct way to do things with v2 afaik so it shouldn't matter what happens with the go fields as long as protoreflect works.

Also I'd like to coordinate the ORM codegen work with the codegen work we're doing, but still if protoreflect is correct it shouldn't matter.

@fdymylja
Copy link
Contributor

Also I'd like to coordinate the ORM codegen work with the codegen work we're doing, but still if protoreflect is correct it shouldn't matter.

Absolutely. The problem we'd hit, for example, would be in Client generator, in which you'd need to check for scalar/customtype option and modify the arguments of the client functions, alongside protoreflect.Value for secondary key value to []byte encoding. I'll sync here #9156 with the API I have come up with (maybe it's a non-issue).

@amaury1093
Copy link
Contributor Author

amaury1093 commented Aug 11, 2021

We need to decide in which repo to put these annotations (e.g. in cosmos-proto or here)?

related cosmos/cosmos-proto#4

@mergify mergify bot closed this as completed in #9933 Sep 21, 2021
mergify bot pushed a commit that referenced this issue Sep 21, 2021
<!--
The default pull request template is for types feat, fix, or refactor.
For other templates, add one of the following parameters to the url:
- template=docs.md
- template=other.md
-->

## Description

Closes: #9694

goes hand-in-hand with cosmos/cosmos-proto#17

<!-- Add a description of the changes that this PR introduces and the files that
are the most critical to review. -->

---

### 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
- [ ] 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
- [x] 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`
- [x] 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
- [x] 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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants