Skip to content

Commit

Permalink
Merge PR cosmos#5491: Protobuf Introduction + Types
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderbez authored Jan 24, 2020
1 parent f091204 commit 26d6e49
Show file tree
Hide file tree
Showing 34 changed files with 2,826 additions and 398 deletions.
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:
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
- COMMENT_FIELD
- 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

0 comments on commit 26d6e49

Please sign in to comment.