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

Protobuf Introduction + Types #5491

Merged
merged 36 commits into from
Jan 24, 2020
Merged
Show file tree
Hide file tree
Changes from 35 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
adfae21
Migrate types to use proto
alexanderbez Jan 7, 2020
72f70c0
Add newline
alexanderbez Jan 7, 2020
80ab9d6
Fix rand_util
alexanderbez Jan 7, 2020
bf49ef2
Add nolint
alexanderbez Jan 7, 2020
d9c08cf
Implement amino marshal interface for int, uint, and dec
alexanderbez Jan 7, 2020
c5e5c6b
Update encoding doc
alexanderbez Jan 8, 2020
c1b17a5
Update CircleCI config
alexanderbez Jan 8, 2020
28b2da6
Remove tools target from proto job
alexanderbez Jan 8, 2020
f039b99
Rename to CustomProtobufType
alexanderbez Jan 8, 2020
49aa60d
Merge branch 'master' into bez/5444-types-proto-enc
alexanderbez Jan 8, 2020
cd092c0
Merge branch 'master' into bez/5444-types-proto-enc
alexanderbez Jan 8, 2020
b3463f7
changed proto docker image
Jan 8, 2020
f05929c
changed proto job to skip the binary installs
Jan 8, 2020
52e0a49
removed extra files and update proto CI
Jan 8, 2020
161f23b
Use third_party over vendor
alexanderbez Jan 9, 2020
5098419
Merge branch 'master' into bez/5444-types-proto-enc
alexanderbez Jan 9, 2020
7501af3
Merge PR #5505: Convert Int and Dec to string's on Coin and DecCoin p…
aaronc Jan 13, 2020
5c6c08c
Update go.sum
alexanderbez Jan 13, 2020
2e80b77
Fix protoc-gen-gocosmos target by allowing versioning
alexanderbez Jan 13, 2020
9e6a906
Lint
alexanderbez Jan 13, 2020
33400ef
Update go.{mod, sum} and install
alexanderbez Jan 13, 2020
241eafb
wildcard import
alexanderbez Jan 13, 2020
489d5e0
Merge branch 'master' into bez/5444-types-proto-enc
alexanderbez Jan 13, 2020
01f7766
go mod tidy
alexanderbez Jan 13, 2020
bb4ae4e
Bump cosmos-proto to v0.1.0
alexanderbez Jan 13, 2020
d6182fb
Update go.{mod, sum}
alexanderbez Jan 13, 2020
ba12a87
Update third party proto deps
alexanderbez Jan 15, 2020
153dadd
Define Marshaler and ProtoMarshaler interfaces along with BaseCodec type
alexanderbez Jan 16, 2020
3504709
Merge branch 'master' into bez/5444-types-proto-enc
alexanderbez Jan 17, 2020
8bf9d72
Refactor marshaler interface
alexanderbez Jan 21, 2020
790802b
Add ProtoCodec tests
alexanderbez Jan 21, 2020
63ce937
Add HybridCodec tests
alexanderbez Jan 21, 2020
c123a18
Linting
alexanderbez Jan 21, 2020
b35d36d
Merge branch 'master' into bez/5444-types-proto-enc
alexanderbez Jan 21, 2020
cd6de70
Update encoding docs
alexanderbez Jan 21, 2020
51b5f35
Merge branch 'master' into bez/5444-types-proto-enc
alexanderbez Jan 24, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ executors:
- image: tendermintdev/docker-website-deployment
environment:
AWS_REGION: us-east-1
protoc:
docker:
- image: tendermintdev/docker-protoc

commands:
make:
Expand Down Expand Up @@ -73,6 +76,15 @@ jobs:
key: go-src-v1-{{ .Revision }}
paths:
- ".git"
proto:
executor: protoc
steps:
- make:
tac0turtle marked this conversation as resolved.
Show resolved Hide resolved
target: protoc-gen-gocosmos
description: "Generate go plugin for protoc"
- make:
target: proto-gen proto-lint proto-check-breaking
description: "Lint and verify Protocol Buffer definitions"

test-sim-nondeterminism:
executor: golang
Expand Down Expand Up @@ -182,6 +194,9 @@ workflows:
tags:
only:
- /^v.*/
- proto:
requires:
- setup-dependencies
- test-sim-nondeterminism:
requires:
- setup-dependencies
Expand Down
92 changes: 59 additions & 33 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,37 +11,37 @@ MOCKS_DIR = $(CURDIR)/tests/mocks

export GO111MODULE = on

all: tools build lint test

# The below include contains the tools and runsim targets.
include contrib/devtools/Makefile

########################################
### Build
all: tools build lint test

###############################################################################
### Build ###
###############################################################################

build: go.sum
@go build -mod=readonly ./...
.PHONY: build

update-swagger-docs: statik
$(BINDIR)/statik -src=client/lcd/swagger-ui -dest=client/lcd -f -m
@if [ -n "$(git status --porcelain)" ]; then \
echo "\033[91mSwagger docs are out of sync!!!\033[0m";\
exit 1;\
else \
echo "\033[92mSwagger docs are in sync\033[0m";\
fi
.PHONY: update-swagger-docs

mocks: $(MOCKS_DIR)
mockgen -source=x/auth/types/account_retriever.go -package mocks -destination tests/mocks/account_retriever.go
.PHONY: mocks

$(MOCKS_DIR):
mkdir -p $(MOCKS_DIR)

########################################
### Tools & dependencies
distclean:
rm -rf \
gitian-build-darwin/ \
gitian-build-linux/ \
gitian-build-windows/ \
.gitian-builder-cache/
.PHONY: distclean

###############################################################################
### Tools & Dependencies ###
###############################################################################

go-mod-cache: go.sum
@echo "--> Download go modules to local cache"
Expand All @@ -53,16 +53,19 @@ go.sum: go.mod
@go mod verify
@go mod tidy

distclean:
rm -rf \
gitian-build-darwin/ \
gitian-build-linux/ \
gitian-build-windows/ \
.gitian-builder-cache/
.PHONY: distclean
###############################################################################
### Documentation ###
###############################################################################

########################################
### Documentation
update-swagger-docs: statik
$(BINDIR)/statik -src=client/lcd/swagger-ui -dest=client/lcd -f -m
@if [ -n "$(git status --porcelain)" ]; then \
echo "\033[91mSwagger docs are out of sync!!!\033[0m";\
exit 1;\
else \
echo "\033[92mSwagger docs are in sync\033[0m";\
fi
.PHONY: update-swagger-docs

godocs:
@echo "--> Wait a few seconds and visit http://localhost:6060/pkg/github.com/cosmos/cosmos-sdk/types"
Expand All @@ -85,8 +88,9 @@ sync-docs:
aws cloudfront create-invalidation --distribution-id ${CF_DISTRIBUTION_ID} --profile terraform --path "/*" ;
.PHONY: sync-docs

########################################
### Testing
###############################################################################
### Tests & Simulation ###
###############################################################################

test: test-unit
test-all: test-unit test-ledger-mock test-race test-cover
Expand Down Expand Up @@ -173,6 +177,14 @@ test-cover:
@export VERSION=$(VERSION); bash -x tests/test_cover.sh
.PHONY: test-cover

benchmark:
@go test -mod=readonly -bench=. $(PACKAGES_NOSIMULATION)
.PHONY: benchmark

###############################################################################
### Linting ###
###############################################################################

lint: golangci-lint
$(BINDIR)/golangci-lint run
find . -name '*.go' -type f -not -path "./vendor*" -not -path "*.git*" | xargs gofmt -d -s
Expand All @@ -185,12 +197,9 @@ format: tools
find . -name '*.go' -type f -not -path "./vendor*" -not -path "*.git*" -not -path "./client/lcd/statik/statik.go" | xargs goimports -w -local github.com/cosmos/cosmos-sdk
.PHONY: format

benchmark:
@go test -mod=readonly -bench=. $(PACKAGES_NOSIMULATION)
.PHONY: benchmark

########################################
### Devdoc
###############################################################################
### Devdoc ###
###############################################################################

DEVDOC_SAVE = docker commit `docker ps -a -n 1 -q` devdoc:local

Expand All @@ -213,3 +222,20 @@ devdoc-update:
docker pull tendermint/devdoc

.PHONY: devdoc devdoc-clean devdoc-init devdoc-save devdoc-update

###############################################################################
### Protobuf ###
###############################################################################

proto-all: proto-gen proto-lint proto-check-breaking

proto-gen:
@./scripts/protocgen.sh

proto-lint:
@buf check lint --error-format=json

proto-check-breaking:
@buf check breaking --against-input '.git#branch=master'

.PHONY: proto-all proto-gen proto-lint proto-check-breaking
19 changes: 19 additions & 0 deletions buf.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
build:
roots:
- .
lint:
use:
- DEFAULT
- COMMENTS
- FILE_LOWER_SNAKE_CASE
except:
- UNARY_RPC
Copy link
Member

Choose a reason for hiding this comment

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

Do we have any use case for streaming RPC?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, we don't atm.

- COMMENT_FIELD
Copy link
Member

Choose a reason for hiding this comment

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

I would actually lean towards requiring field comments, but maybe it will make migration slower...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We can, but it'll be super tedious. We don't even do this for standard go types.

- PACKAGE_DIRECTORY_MATCH
ignore:
- third_party
breaking:
use:
- FILE
ignore:
- third_party
70 changes: 70 additions & 0 deletions codec/amino.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package codec

import (
"bytes"
"encoding/json"
"fmt"

amino "github.com/tendermint/go-amino"
cryptoamino "github.com/tendermint/tendermint/crypto/encoding/amino"
tmtypes "github.com/tendermint/tendermint/types"
)

// Cdc defines a global generic sealed Amino codec to be used throughout sdk. It
// has all Tendermint crypto and evidence types registered.
//
// TODO: Consider removing this global.
var Cdc *Codec

func init() {
cdc := New()
RegisterCrypto(cdc)
RegisterEvidences(cdc)
Cdc = cdc.Seal()
}

// Codec defines a type alias for an Amino codec.
type Codec = amino.Codec

func New() *Codec {
return amino.NewCodec()
}

// RegisterCrypto registers all crypto dependency types with the provided Amino
// codec.
func RegisterCrypto(cdc *Codec) {
cryptoamino.RegisterAmino(cdc)
}

// RegisterEvidences registers Tendermint evidence types with the provided Amino
// codec.
func RegisterEvidences(cdc *Codec) {
tmtypes.RegisterEvidences(cdc)
}

// MarshalJSONIndent provides a utility for indented JSON encoding of an object
// via an Amino codec. It returns an error if it cannot serialize or indent as
// JSON.
func MarshalJSONIndent(cdc *Codec, obj interface{}) ([]byte, error) {
bz, err := cdc.MarshalJSON(obj)
if err != nil {
return nil, err
}

var out bytes.Buffer
if err = json.Indent(&out, bz, "", " "); err != nil {
return nil, err
}

return out.Bytes(), nil
}

// MustMarshalJSONIndent executes MarshalJSONIndent except it panics upon failure.
func MustMarshalJSONIndent(cdc *Codec, obj interface{}) []byte {
bz, err := MarshalJSONIndent(cdc, obj)
if err != nil {
panic(fmt.Sprintf("failed to marshal JSON: %s", err))
}

return bz
}
59 changes: 59 additions & 0 deletions codec/amino_codec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package codec

// AminoCodec defines a codec that utilizes Amino for both binary and JSON
// encoding.
type AminoCodec struct {
amino *Codec
}

func NewAminoCodec(amino *Codec) Marshaler {
return &AminoCodec{amino}
}

func (ac *AminoCodec) MarshalBinaryBare(o ProtoMarshaler) ([]byte, error) {
return ac.amino.MarshalBinaryBare(o)
}

func (ac *AminoCodec) MustMarshalBinaryBare(o ProtoMarshaler) []byte {
return ac.amino.MustMarshalBinaryBare(o)
}

func (ac *AminoCodec) MarshalBinaryLengthPrefixed(o ProtoMarshaler) ([]byte, error) {
return ac.amino.MarshalBinaryLengthPrefixed(o)
}

func (ac *AminoCodec) MustMarshalBinaryLengthPrefixed(o ProtoMarshaler) []byte {
return ac.amino.MustMarshalBinaryLengthPrefixed(o)
}

func (ac *AminoCodec) UnmarshalBinaryBare(bz []byte, ptr ProtoMarshaler) error {
return ac.amino.UnmarshalBinaryBare(bz, ptr)
}

func (ac *AminoCodec) MustUnmarshalBinaryBare(bz []byte, ptr ProtoMarshaler) {
ac.amino.MustUnmarshalBinaryBare(bz, ptr)
}

func (ac *AminoCodec) UnmarshalBinaryLengthPrefixed(bz []byte, ptr ProtoMarshaler) error {
return ac.amino.UnmarshalBinaryLengthPrefixed(bz, ptr)
}

func (ac *AminoCodec) MustUnmarshalBinaryLengthPrefixed(bz []byte, ptr ProtoMarshaler) {
ac.amino.MustUnmarshalBinaryLengthPrefixed(bz, ptr)
}

func (ac *AminoCodec) MarshalJSON(o interface{}) ([]byte, error) { // nolint: stdmethods
return ac.amino.MarshalJSON(o)
}

func (ac *AminoCodec) MustMarshalJSON(o interface{}) []byte {
return ac.amino.MustMarshalJSON(o)
}

func (ac *AminoCodec) UnmarshalJSON(bz []byte, ptr interface{}) error { // nolint: stdmethods
return ac.amino.UnmarshalJSON(bz, ptr)
}

func (ac *AminoCodec) MustUnmarshalJSON(bz []byte, ptr interface{}) {
ac.amino.MustUnmarshalJSON(bz, ptr)
}
Loading