From 3f8778e68a68dca177aee66c20034110a4b67036 Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Mon, 30 Dec 2019 10:28:11 -0500 Subject: [PATCH 01/40] Makefile cleanup --- Makefile | 75 ++++++++++++++++++++++----------------- contrib/devtools/Makefile | 40 ++++++++++----------- 2 files changed, 62 insertions(+), 53 deletions(-) diff --git a/Makefile b/Makefile index 8d947640f75a..161a6a9cfb23 100644 --- a/Makefile +++ b/Makefile @@ -11,28 +11,19 @@ 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 @@ -40,8 +31,17 @@ mocks: $(MOCKS_DIR) $(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" @@ -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" @@ -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 @@ -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 @@ -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 diff --git a/contrib/devtools/Makefile b/contrib/devtools/Makefile index 735ab395178c..b7c4d8225c9e 100644 --- a/contrib/devtools/Makefile +++ b/contrib/devtools/Makefile @@ -1,3 +1,5 @@ +.PHONY: all tools tools-clean + ### # Find OS and Go environment # GO contains the Go binary @@ -15,13 +17,11 @@ ifeq ($(GO),) $(error could not find go. Is it in PATH? $(GO)) endif -GOPATH ?= $(shell $(GO) env GOPATH) -GITHUBDIR := $(GOPATH)$(FS)src$(FS)github.com -GOLANGCI_LINT_HASHSUM := 8d21cc95da8d3daf8321ac40091456fc26123c964d7c2281d339d431f2f4c840 +all: tools -### -# Functions -### +############################################################################### +### Functions ### +############################################################################### go_get = $(if $(findstring Windows_NT,$(OS)),\ IF NOT EXIST $(GITHUBDIR)$(FS)$(1)$(FS) ( mkdir $(GITHUBDIR)$(FS)$(1) ) else (cd .) &\ @@ -35,17 +35,19 @@ cd $(GITHUBDIR)$(FS)$(1)$(FS)$(2) && git fetch origin && git checkout -q $(3) mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST))) mkfile_dir := $(shell cd $(shell dirname $(mkfile_path)); pwd) -### -# tools -### -TOOLS_DESTDIR ?= $(GOPATH)/bin +############################################################################### +### Tools ### +############################################################################### -GOLANGCI_LINT = $(TOOLS_DESTDIR)/golangci-lint -STATIK = $(TOOLS_DESTDIR)/statik -RUNSIM = $(TOOLS_DESTDIR)/runsim +GOPATH ?= $(shell $(GO) env GOPATH) +GITHUBDIR := $(GOPATH)$(FS)src$(FS)github.com +GOLANGCI_LINT_HASHSUM := 8d21cc95da8d3daf8321ac40091456fc26123c964d7c2281d339d431f2f4c840 -all: tools +TOOLS_DESTDIR ?= $(GOPATH)/bin +GOLANGCI_LINT = $(TOOLS_DESTDIR)/golangci-lint +STATIK = $(TOOLS_DESTDIR)/statik +RUNSIM = $(TOOLS_DESTDIR)/runsim tools: statik runsim golangci-lint @@ -64,6 +66,10 @@ $(STATIK): @echo "Installing statik..." @(cd /tmp && go get github.com/rakyll/statik@v0.1.6) +tools-clean: + rm -f $(STATIK) $(GOLANGCI_LINT) $(RUNSIM) + rm -f tools-stamp + # Install the runsim binary with a temporary workaround of entering an outside # directory as the "go get" command ignores the -mod option and will polute the # go.{mod, sum} files. @@ -73,9 +79,3 @@ runsim: $(RUNSIM) $(RUNSIM): @echo "Installing runsim..." @(cd /tmp && go get github.com/cosmos/tools/cmd/runsim@v1.0.0) - -tools-clean: - rm -f $(STATIK) $(GOLANGCI_LINT) $(RUNSIM) - rm -f tools-stamp - -.PHONY: all tools tools-clean From f55bdf3ba311cfe4e9f9d2b27bc881d665a796dd Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Mon, 30 Dec 2019 10:52:26 -0500 Subject: [PATCH 02/40] Add buf targets to makefile --- contrib/devtools/Makefile | 38 +++++++++++++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/contrib/devtools/Makefile b/contrib/devtools/Makefile index b7c4d8225c9e..3777a34ed253 100644 --- a/contrib/devtools/Makefile +++ b/contrib/devtools/Makefile @@ -1,4 +1,5 @@ -.PHONY: all tools tools-clean +.PHONY: all tools tools-clean statik runsim golangci-lint \ +buf protoc-gen-buf-check-breaking protoc-gen-buf-check-lint ### # Find OS and Go environment @@ -40,10 +41,16 @@ mkfile_dir := $(shell cd $(shell dirname $(mkfile_path)); pwd) ### Tools ### ############################################################################### +BIN ?= /usr/local/bin +OS ?= $(shell uname -s) +HD ?= $(shell uname -m) + GOPATH ?= $(shell $(GO) env GOPATH) GITHUBDIR := $(GOPATH)$(FS)src$(FS)github.com GOLANGCI_LINT_HASHSUM := 8d21cc95da8d3daf8321ac40091456fc26123c964d7c2281d339d431f2f4c840 +BUF_VERSION = 0.4.0 + TOOLS_DESTDIR ?= $(GOPATH)/bin GOLANGCI_LINT = $(TOOLS_DESTDIR)/golangci-lint STATIK = $(TOOLS_DESTDIR)/statik @@ -66,10 +73,6 @@ $(STATIK): @echo "Installing statik..." @(cd /tmp && go get github.com/rakyll/statik@v0.1.6) -tools-clean: - rm -f $(STATIK) $(GOLANGCI_LINT) $(RUNSIM) - rm -f tools-stamp - # Install the runsim binary with a temporary workaround of entering an outside # directory as the "go get" command ignores the -mod option and will polute the # go.{mod, sum} files. @@ -79,3 +82,28 @@ runsim: $(RUNSIM) $(RUNSIM): @echo "Installing runsim..." @(cd /tmp && go get github.com/cosmos/tools/cmd/runsim@v1.0.0) + +buf: protoc-gen-buf-check-breaking protoc-gen-buf-check-lint + @echo "Installing buf..." + @curl -sSL \ + "https://github.com/bufbuild/buf/releases/download/v${BUF_VERSION}/buf-${OS}-${HD}" \ + -o "${BIN}/buf" && \ + chmod +x "${BIN}/buf" + +protoc-gen-buf-check-breaking: + @echo "Installing protoc-gen-buf-check-breaking..." + @curl -sSL \ + "https://github.com/bufbuild/buf/releases/download/v${BUF_VERSION}/protoc-gen-buf-check-breaking-${OS}-${HD}" \ + -o "${BIN}/protoc-gen-buf-check-breaking" && \ + chmod +x "${BIN}/protoc-gen-buf-check-breaking" + +protoc-gen-buf-check-lint: + @echo "Installing protoc-gen-buf-check-lint..." + @curl -sSL \ + "https://github.com/bufbuild/buf/releases/download/v${BUF_VERSION}/protoc-gen-buf-check-lint-${OS}-${HD}" \ + -o "${BIN}/protoc-gen-buf-check-lint" && \ + chmod +x "${BIN}/protoc-gen-buf-check-lint" + +tools-clean: + rm -f $(STATIK) $(GOLANGCI_LINT) $(RUNSIM) + rm -f tools-stamp From f0da7fce8da3d1917cb850f5a93391e7172368c3 Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Mon, 30 Dec 2019 10:54:48 -0500 Subject: [PATCH 03/40] Fix buf version var --- contrib/devtools/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contrib/devtools/Makefile b/contrib/devtools/Makefile index 3777a34ed253..561b2389a0ec 100644 --- a/contrib/devtools/Makefile +++ b/contrib/devtools/Makefile @@ -49,7 +49,7 @@ GOPATH ?= $(shell $(GO) env GOPATH) GITHUBDIR := $(GOPATH)$(FS)src$(FS)github.com GOLANGCI_LINT_HASHSUM := 8d21cc95da8d3daf8321ac40091456fc26123c964d7c2281d339d431f2f4c840 -BUF_VERSION = 0.4.0 +BUF_VERSION ?= 0.4.0 TOOLS_DESTDIR ?= $(GOPATH)/bin GOLANGCI_LINT = $(TOOLS_DESTDIR)/golangci-lint From 3f6fd6de1d56f14b4c13ca6c8813baa31e8b1ae2 Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Mon, 30 Dec 2019 11:00:28 -0500 Subject: [PATCH 04/40] GOLANGCI_LINT_HASHSUM --- contrib/devtools/Makefile | 2 +- contrib/devtools/install-golangci-lint.sh | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/contrib/devtools/Makefile b/contrib/devtools/Makefile index 561b2389a0ec..dda055299f22 100644 --- a/contrib/devtools/Makefile +++ b/contrib/devtools/Makefile @@ -47,7 +47,7 @@ HD ?= $(shell uname -m) GOPATH ?= $(shell $(GO) env GOPATH) GITHUBDIR := $(GOPATH)$(FS)src$(FS)github.com -GOLANGCI_LINT_HASHSUM := 8d21cc95da8d3daf8321ac40091456fc26123c964d7c2281d339d431f2f4c840 +GOLANGCI_LINT_HASHSUM := f11179f445385a4a6d5079d67de63fe48a9113cee8e9ee0ced19327a83a4394b BUF_VERSION ?= 0.4.0 diff --git a/contrib/devtools/install-golangci-lint.sh b/contrib/devtools/install-golangci-lint.sh index 3b0842e59cf3..32412fbb55dd 100644 --- a/contrib/devtools/install-golangci-lint.sh +++ b/contrib/devtools/install-golangci-lint.sh @@ -22,10 +22,10 @@ get_latest_release() { VERSION="$(get_latest_release golangci/golangci-lint)" -echo "Downloading golangci-lint ${VERSION} installer ..." >&2 +echo "Downloading golangci-lint ${VERSION} installer..." >&2 "${CURL}" -sfL "https://raw.githubusercontent.com/golangci/golangci-lint/${VERSION}/install.sh" > "${installer}" -echo "Checking hashsum ..." >&2 +echo "Checking hashsum..." >&2 [ "${HASHSUM}" = "$(f_sha256 ${installer})" ] chmod +x "${installer}" From 5be064560cd3d3ed0f1698f91232ab799bf9e1a8 Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Tue, 31 Dec 2019 10:41:27 -0500 Subject: [PATCH 05/40] Update go.{mod,sum} --- go.mod | 1 + go.sum | 8 ++++---- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/go.mod b/go.mod index 93c6f3b209e9..bb0a000b84a3 100644 --- a/go.mod +++ b/go.mod @@ -9,6 +9,7 @@ require ( github.com/cosmos/ledger-cosmos-go v0.11.1 github.com/gogo/protobuf v1.3.1 github.com/golang/mock v1.3.1-0.20190508161146-9fa652df1129 + github.com/golang/protobuf v1.3.2 github.com/gorilla/mux v1.7.3 github.com/hashicorp/golang-lru v0.5.3 github.com/mattn/go-isatty v0.0.11 diff --git a/go.sum b/go.sum index e83a49d31f82..cc5d9b873f77 100644 --- a/go.sum +++ b/go.sum @@ -107,6 +107,7 @@ github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5a github.com/google/gofuzz v0.0.0-20170612174753-24818f796faf/go.mod h1:HP5RmnzzSNb993RKQDq4+1A4ia9nllfqcQFTQJedwGI= github.com/google/gofuzz v1.0.0 h1:A8PeW59pxE9IoFRqBp37U+mSNaQoZ46F1f0f863XSXw= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8= github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= github.com/gorilla/mux v1.7.3 h1:gnP5JzjVOuiZD07fKKToCAOjS0yOpj/qPETTXCCS6hw= github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs= @@ -132,6 +133,7 @@ github.com/jmhodges/levigo v1.0.0 h1:q5EC36kV79HWeTBWsod3mG11EgStG3qArTKcvlksN1U github.com/jmhodges/levigo v1.0.0/go.mod h1:Q6Qx+uH3RAqyK4rFQroq9RL7mdkABMcfhEI+nNuzMJQ= github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo= github.com/jrick/logrotate v1.0.0/go.mod h1:LNinyqDIJnpAur+b8yyulnQw/wDuN1+BYKlTRt3OuAQ= +github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo= github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= github.com/keybase/go-keychain v0.0.0-20190712205309-48d3d31d256d h1:Z+RDyXzjKE0i2sTjZ/b1uxiGtPhFy34Ou/Tk0qwN0kM= @@ -154,8 +156,6 @@ github.com/magiconair/properties v1.8.0 h1:LLgXmsheXeRoUOBOjtwPQCWIYqM/LU1ayDtDe github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= github.com/magiconair/properties v1.8.1 h1:ZC2Vc7/ZFkGmsVC9KvOjumD+G5lXy2RtTKyzRKO2BQ4= github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= -github.com/mattn/go-isatty v0.0.10 h1:qxFzApOv4WsAL965uUPIsXzAKCZxN2p9UqdhFS4ZW10= -github.com/mattn/go-isatty v0.0.10/go.mod h1:qgIWMr58cqv1PHHyhnkY9lrL7etaEgOFcMEpPG5Rm84= github.com/mattn/go-isatty v0.0.11 h1:FxPOTFNqGkuDUGi3H/qkUbQO4ZiBa2brKq5r0l8TGeM= github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOAqxQCu2WE= github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU= @@ -206,7 +206,9 @@ github.com/rs/cors v1.7.0 h1:+88SsELBHx5r+hZ8TCkggzSstaWNbDvThkVK8H6f9ik= github.com/rs/cors v1.7.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU= github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g= github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= +github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM= github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= +github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s= github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= github.com/snikch/goodman v0.0.0-20171125024755-10e37e294daa/go.mod h1:oJyF+mSPHbB5mVY2iO9KV3pTt/QbIkGaO8gQ2WrDbP4= github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM= @@ -316,8 +318,6 @@ golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5h golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190712062909-fae7ac547cb7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20191008105621-543471e840be h1:QAcqgptGM8IQBC9K/RC4o+O9YmqEm0diQn9QmZw/0mU= -golang.org/x/sys v0.0.0-20191008105621-543471e840be/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4= golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= From 75fbebc4244a9122d548fdd029285c80ef58fa24 Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Tue, 31 Dec 2019 10:41:47 -0500 Subject: [PATCH 06/40] Add buf config file --- buf.yaml | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 buf.yaml diff --git a/buf.yaml b/buf.yaml new file mode 100644 index 000000000000..ff2065dbfd2d --- /dev/null +++ b/buf.yaml @@ -0,0 +1,19 @@ +build: + roots: + - . + excludes: + - vendor/github.com/tendermint +lint: + use: + - DEFAULT + - COMMENTS + - FILE_LOWER_SNAKE_CASE + except: + - UNARY_RPC + - PACKAGE_VERSION_SUFFIX + - COMMENT_FIELD + ignore: + - vendor +breaking: + use: + - FILE From ff570d91a50c1c260579e8bfe4a28d278704a435 Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Tue, 31 Dec 2019 10:47:47 -0500 Subject: [PATCH 07/40] Add proto targets and script --- Makefile | 18 ++++++++++++++++++ scripts/protocgen.sh | 13 +++++++++++++ 2 files changed, 31 insertions(+) create mode 100755 scripts/protocgen.sh diff --git a/Makefile b/Makefile index 161a6a9cfb23..b0a34bf0bc4a 100644 --- a/Makefile +++ b/Makefile @@ -222,3 +222,21 @@ devdoc-update: docker pull tendermint/devdoc .PHONY: devdoc devdoc-clean devdoc-init devdoc-save devdoc-update + +############################################################################### +### Protobuf ### +############################################################################### + +proto-all: proto-lint proto-check-breaking proto-gen + +proto-gen: + @go mod vendor + @./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 diff --git a/scripts/protocgen.sh b/scripts/protocgen.sh new file mode 100755 index 000000000000..3d7e51b5d964 --- /dev/null +++ b/scripts/protocgen.sh @@ -0,0 +1,13 @@ +#!/usr/bin/env bash + +set -eo pipefail + +proto_dirs=$(find . -path ./vendor -prune -o -name '*.proto' -print0 | xargs -0 -n1 dirname | sort | uniq) +for dir in $proto_dirs; do + protoc -I/usr/local/include \ + -I. \ + -I${GOPATH}/src \ + -Ivendor/github.com/gogo/protobuf/gogoproto \ + --gofast_out=. \ + $(find "${dir}" -name '*.proto') +done \ No newline at end of file From 04d1834210c2f2f7bffc611f1ac834acdd67191e Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Tue, 31 Dec 2019 10:48:04 -0500 Subject: [PATCH 08/40] Update types/ --- types/coin.go | 21 +- types/coin_test.go | 218 ++++++------ types/dec_coin_test.go | 8 +- types/int.go | 86 +++-- types/int_test.go | 85 +++-- types/types.pb.go | 784 +++++++++++++++++++++++++++++++++++++++++ types/types.proto | 37 ++ types/uint.go | 71 +++- 8 files changed, 1098 insertions(+), 212 deletions(-) create mode 100644 types/types.pb.go create mode 100644 types/types.proto diff --git a/types/coin.go b/types/coin.go index 94b9dc901c77..e9912835c328 100644 --- a/types/coin.go +++ b/types/coin.go @@ -8,23 +8,6 @@ import ( "strings" ) -//----------------------------------------------------------------------------- -// Coin - -// Coin hold some amount of one currency. -// -// CONTRACT: A coin will never hold a negative amount of any denomination. -// -// TODO: Make field members private for further safety. -type Coin struct { - Denom string `json:"denom"` - - // To allow the use of unsigned integers (see: #1273) a larger refactor will - // need to be made. So we use signed integers for now with safety measures in - // place preventing negative values being used. - Amount Int `json:"amount"` -} - // NewCoin returns a new coin with a denomination and amount. It will panic if // the amount is negative. func NewCoin(denom string, amount Int) Coin { @@ -112,7 +95,7 @@ func (coin Coin) Add(coinB Coin) Coin { panic(fmt.Sprintf("invalid coin denominations; %s, %s", coin.Denom, coinB.Denom)) } - return Coin{coin.Denom, coin.Amount.Add(coinB.Amount)} + return Coin{Denom: coin.Denom, Amount: coin.Amount.Add(coinB.Amount)} } // Subtracts amounts of two coins with same denom. If the coins differ in denom @@ -122,7 +105,7 @@ func (coin Coin) Sub(coinB Coin) Coin { panic(fmt.Sprintf("invalid coin denominations; %s, %s", coin.Denom, coinB.Denom)) } - res := Coin{coin.Denom, coin.Amount.Sub(coinB.Amount)} + res := Coin{Denom: coin.Denom, Amount: coin.Amount.Sub(coinB.Amount)} if res.IsNegative() { panic("negative coin amount") } diff --git a/types/coin_test.go b/types/coin_test.go index f18ff036500a..a3b96f4fb2ca 100644 --- a/types/coin_test.go +++ b/types/coin_test.go @@ -55,14 +55,14 @@ func TestCoinIsValid(t *testing.T) { coin Coin expectPass bool }{ - {Coin{testDenom1, NewInt(-1)}, false}, - {Coin{testDenom1, NewInt(0)}, true}, - {Coin{testDenom1, NewInt(1)}, true}, - {Coin{"Atom", NewInt(1)}, false}, - {Coin{"a", NewInt(1)}, false}, - {Coin{"a very long coin denom", NewInt(1)}, false}, - {Coin{"atOm", NewInt(1)}, false}, - {Coin{" ", NewInt(1)}, false}, + {Coin{Denom: testDenom1, Amount: NewInt(-1)}, false}, + {Coin{Denom: testDenom1, Amount: NewInt(0)}, true}, + {Coin{Denom: testDenom1, Amount: NewInt(1)}, true}, + {Coin{Denom: "Atom", Amount: NewInt(1)}, false}, + {Coin{Denom: "a", Amount: NewInt(1)}, false}, + {Coin{Denom: "a very long coin denom", Amount: NewInt(1)}, false}, + {Coin{Denom: "atOm", Amount: NewInt(1)}, false}, + {Coin{Denom: " ", Amount: NewInt(1)}, false}, } for i, tc := range cases { @@ -243,11 +243,11 @@ func TestAddCoins(t *testing.T) { inputTwo Coins expected Coins }{ - {Coins{{testDenom1, one}, {testDenom2, one}}, Coins{{testDenom1, one}, {testDenom2, one}}, Coins{{testDenom1, two}, {testDenom2, two}}}, - {Coins{{testDenom1, zero}, {testDenom2, one}}, Coins{{testDenom1, zero}, {testDenom2, zero}}, Coins{{testDenom2, one}}}, - {Coins{{testDenom1, two}}, Coins{{testDenom2, zero}}, Coins{{testDenom1, two}}}, - {Coins{{testDenom1, one}}, Coins{{testDenom1, one}, {testDenom2, two}}, Coins{{testDenom1, two}, {testDenom2, two}}}, - {Coins{{testDenom1, zero}, {testDenom2, zero}}, Coins{{testDenom1, zero}, {testDenom2, zero}}, Coins(nil)}, + {Coins{{Denom: testDenom1, Amount: one}, {Denom: testDenom2, Amount: one}}, Coins{{Denom: testDenom1, Amount: one}, {Denom: testDenom2, Amount: one}}, Coins{{Denom: testDenom1, Amount: two}, {Denom: testDenom2, Amount: two}}}, + {Coins{{Denom: testDenom1, Amount: zero}, {Denom: testDenom2, Amount: one}}, Coins{{Denom: testDenom1, Amount: zero}, {Denom: testDenom2, Amount: zero}}, Coins{{Denom: testDenom2, Amount: one}}}, + {Coins{{Denom: testDenom1, Amount: two}}, Coins{{Denom: testDenom2, Amount: zero}}, Coins{{Denom: testDenom1, Amount: two}}}, + {Coins{{Denom: testDenom1, Amount: one}}, Coins{{Denom: testDenom1, Amount: one}, {Denom: testDenom2, Amount: two}}, Coins{{Denom: testDenom1, Amount: two}, {Denom: testDenom2, Amount: two}}}, + {Coins{{Denom: testDenom1, Amount: zero}, {Denom: testDenom2, Amount: zero}}, Coins{{Denom: testDenom1, Amount: zero}, {Denom: testDenom2, Amount: zero}}, Coins(nil)}, } for tcIndex, tc := range cases { @@ -268,11 +268,11 @@ func TestSubCoins(t *testing.T) { expected Coins shouldPanic bool }{ - {Coins{{testDenom1, two}}, Coins{{testDenom1, one}, {testDenom2, two}}, Coins{{testDenom1, one}, {testDenom2, two}}, true}, - {Coins{{testDenom1, two}}, Coins{{testDenom2, zero}}, Coins{{testDenom1, two}}, false}, - {Coins{{testDenom1, one}}, Coins{{testDenom2, zero}}, Coins{{testDenom1, one}}, false}, - {Coins{{testDenom1, one}, {testDenom2, one}}, Coins{{testDenom1, one}}, Coins{{testDenom2, one}}, false}, - {Coins{{testDenom1, one}, {testDenom2, one}}, Coins{{testDenom1, two}}, Coins{}, true}, + {Coins{{Denom: testDenom1, Amount: two}}, Coins{{Denom: testDenom1, Amount: one}, {Denom: testDenom2, Amount: two}}, Coins{{Denom: testDenom1, Amount: one}, {Denom: testDenom2, Amount: two}}, true}, + {Coins{{Denom: testDenom1, Amount: two}}, Coins{{Denom: testDenom2, Amount: zero}}, Coins{{Denom: testDenom1, Amount: two}}, false}, + {Coins{{Denom: testDenom1, Amount: one}}, Coins{{Denom: testDenom2, Amount: zero}}, Coins{{Denom: testDenom1, Amount: one}}, false}, + {Coins{{Denom: testDenom1, Amount: one}, {Denom: testDenom2, Amount: one}}, Coins{{Denom: testDenom1, Amount: one}}, Coins{{Denom: testDenom2, Amount: one}}, false}, + {Coins{{Denom: testDenom1, Amount: one}, {Denom: testDenom2, Amount: one}}, Coins{{Denom: testDenom1, Amount: two}}, Coins{}, true}, } for i, tc := range testCases { @@ -289,48 +289,48 @@ func TestSubCoins(t *testing.T) { func TestCoins(t *testing.T) { good := Coins{ - {"gas", NewInt(1)}, - {"mineral", NewInt(1)}, - {"tree", NewInt(1)}, + {Denom: "gas", Amount: NewInt(1)}, + {Denom: "mineral", Amount: NewInt(1)}, + {Denom: "tree", Amount: NewInt(1)}, } mixedCase1 := Coins{ - {"gAs", NewInt(1)}, - {"MineraL", NewInt(1)}, - {"TREE", NewInt(1)}, + {Denom: "gAs", Amount: NewInt(1)}, + {Denom: "MineraL", Amount: NewInt(1)}, + {Denom: "TREE", Amount: NewInt(1)}, } mixedCase2 := Coins{ - {"gAs", NewInt(1)}, - {"mineral", NewInt(1)}, + {Denom: "gAs", Amount: NewInt(1)}, + {Denom: "mineral", Amount: NewInt(1)}, } mixedCase3 := Coins{ - {"gAs", NewInt(1)}, + {Denom: "gAs", Amount: NewInt(1)}, } empty := NewCoins() badSort1 := Coins{ - {"tree", NewInt(1)}, - {"gas", NewInt(1)}, - {"mineral", NewInt(1)}, + {Denom: "tree", Amount: NewInt(1)}, + {Denom: "gas", Amount: NewInt(1)}, + {Denom: "mineral", Amount: NewInt(1)}, } // both are after the first one, but the second and third are in the wrong order badSort2 := Coins{ - {"gas", NewInt(1)}, - {"tree", NewInt(1)}, - {"mineral", NewInt(1)}, + {Denom: "gas", Amount: NewInt(1)}, + {Denom: "tree", Amount: NewInt(1)}, + {Denom: "mineral", Amount: NewInt(1)}, } badAmt := Coins{ - {"gas", NewInt(1)}, - {"tree", NewInt(0)}, - {"mineral", NewInt(1)}, + {Denom: "gas", Amount: NewInt(1)}, + {Denom: "tree", Amount: NewInt(0)}, + {Denom: "mineral", Amount: NewInt(1)}, } dup := Coins{ - {"gas", NewInt(1)}, - {"gas", NewInt(1)}, - {"mineral", NewInt(1)}, + {Denom: "gas", Amount: NewInt(1)}, + {Denom: "gas", Amount: NewInt(1)}, + {Denom: "mineral", Amount: NewInt(1)}, } neg := Coins{ - {"gas", NewInt(-1)}, - {"mineral", NewInt(1)}, + {Denom: "gas", Amount: NewInt(-1)}, + {Denom: "mineral", Amount: NewInt(1)}, } assert.True(t, good.IsValid(), "Coins are valid") @@ -354,11 +354,11 @@ func TestCoinsGT(t *testing.T) { two := NewInt(2) assert.False(t, Coins{}.IsAllGT(Coins{})) - assert.True(t, Coins{{testDenom1, one}}.IsAllGT(Coins{})) - assert.False(t, Coins{{testDenom1, one}}.IsAllGT(Coins{{testDenom1, one}})) - assert.False(t, Coins{{testDenom1, one}}.IsAllGT(Coins{{testDenom2, one}})) - assert.True(t, Coins{{testDenom1, one}, {testDenom2, two}}.IsAllGT(Coins{{testDenom2, one}})) - assert.False(t, Coins{{testDenom1, one}, {testDenom2, one}}.IsAllGT(Coins{{testDenom2, two}})) + assert.True(t, Coins{{Denom: testDenom1, Amount: one}}.IsAllGT(Coins{})) + assert.False(t, Coins{{Denom: testDenom1, Amount: one}}.IsAllGT(Coins{{Denom: testDenom1, Amount: one}})) + assert.False(t, Coins{{Denom: testDenom1, Amount: one}}.IsAllGT(Coins{{Denom: testDenom2, Amount: one}})) + assert.True(t, Coins{{Denom: testDenom1, Amount: one}, {Denom: testDenom2, Amount: two}}.IsAllGT(Coins{{Denom: testDenom2, Amount: one}})) + assert.False(t, Coins{{Denom: testDenom1, Amount: one}, {Denom: testDenom2, Amount: one}}.IsAllGT(Coins{{Denom: testDenom2, Amount: two}})) } func TestCoinsLT(t *testing.T) { @@ -366,14 +366,14 @@ func TestCoinsLT(t *testing.T) { two := NewInt(2) assert.False(t, Coins{}.IsAllLT(Coins{})) - assert.False(t, Coins{{testDenom1, one}}.IsAllLT(Coins{})) - assert.False(t, Coins{{testDenom1, one}}.IsAllLT(Coins{{testDenom1, one}})) - assert.False(t, Coins{{testDenom1, one}}.IsAllLT(Coins{{testDenom2, one}})) - assert.False(t, Coins{{testDenom1, one}, {testDenom2, one}}.IsAllLT(Coins{{testDenom2, one}})) - assert.False(t, Coins{{testDenom1, one}, {testDenom2, one}}.IsAllLT(Coins{{testDenom2, two}})) - assert.False(t, Coins{{testDenom1, one}, {testDenom2, one}}.IsAllLT(Coins{{testDenom1, one}, {testDenom2, one}})) - assert.True(t, Coins{{testDenom1, one}, {testDenom2, one}}.IsAllLT(Coins{{testDenom1, two}, {testDenom2, two}})) - assert.True(t, Coins{}.IsAllLT(Coins{{testDenom1, one}})) + assert.False(t, Coins{{Denom: testDenom1, Amount: one}}.IsAllLT(Coins{})) + assert.False(t, Coins{{Denom: testDenom1, Amount: one}}.IsAllLT(Coins{{Denom: testDenom1, Amount: one}})) + assert.False(t, Coins{{Denom: testDenom1, Amount: one}}.IsAllLT(Coins{{Denom: testDenom2, Amount: one}})) + assert.False(t, Coins{{Denom: testDenom1, Amount: one}, {Denom: testDenom2, Amount: one}}.IsAllLT(Coins{{Denom: testDenom2, Amount: one}})) + assert.False(t, Coins{{Denom: testDenom1, Amount: one}, {Denom: testDenom2, Amount: one}}.IsAllLT(Coins{{Denom: testDenom2, Amount: two}})) + assert.False(t, Coins{{Denom: testDenom1, Amount: one}, {Denom: testDenom2, Amount: one}}.IsAllLT(Coins{{Denom: testDenom1, Amount: one}, {Denom: testDenom2, Amount: one}})) + assert.True(t, Coins{{Denom: testDenom1, Amount: one}, {Denom: testDenom2, Amount: one}}.IsAllLT(Coins{{Denom: testDenom1, Amount: two}, {Denom: testDenom2, Amount: two}})) + assert.True(t, Coins{}.IsAllLT(Coins{{Denom: testDenom1, Amount: one}})) } func TestCoinsLTE(t *testing.T) { @@ -381,14 +381,14 @@ func TestCoinsLTE(t *testing.T) { two := NewInt(2) assert.True(t, Coins{}.IsAllLTE(Coins{})) - assert.False(t, Coins{{testDenom1, one}}.IsAllLTE(Coins{})) - assert.True(t, Coins{{testDenom1, one}}.IsAllLTE(Coins{{testDenom1, one}})) - assert.False(t, Coins{{testDenom1, one}}.IsAllLTE(Coins{{testDenom2, one}})) - assert.False(t, Coins{{testDenom1, one}, {testDenom2, one}}.IsAllLTE(Coins{{testDenom2, one}})) - assert.False(t, Coins{{testDenom1, one}, {testDenom2, one}}.IsAllLTE(Coins{{testDenom2, two}})) - assert.True(t, Coins{{testDenom1, one}, {testDenom2, one}}.IsAllLTE(Coins{{testDenom1, one}, {testDenom2, one}})) - assert.True(t, Coins{{testDenom1, one}, {testDenom2, one}}.IsAllLTE(Coins{{testDenom1, one}, {testDenom2, two}})) - assert.True(t, Coins{}.IsAllLTE(Coins{{testDenom1, one}})) + assert.False(t, Coins{{Denom: testDenom1, Amount: one}}.IsAllLTE(Coins{})) + assert.True(t, Coins{{Denom: testDenom1, Amount: one}}.IsAllLTE(Coins{{Denom: testDenom1, Amount: one}})) + assert.False(t, Coins{{Denom: testDenom1, Amount: one}}.IsAllLTE(Coins{{Denom: testDenom2, Amount: one}})) + assert.False(t, Coins{{Denom: testDenom1, Amount: one}, {Denom: testDenom2, Amount: one}}.IsAllLTE(Coins{{Denom: testDenom2, Amount: one}})) + assert.False(t, Coins{{Denom: testDenom1, Amount: one}, {Denom: testDenom2, Amount: one}}.IsAllLTE(Coins{{Denom: testDenom2, Amount: two}})) + assert.True(t, Coins{{Denom: testDenom1, Amount: one}, {Denom: testDenom2, Amount: one}}.IsAllLTE(Coins{{Denom: testDenom1, Amount: one}, {Denom: testDenom2, Amount: one}})) + assert.True(t, Coins{{Denom: testDenom1, Amount: one}, {Denom: testDenom2, Amount: one}}.IsAllLTE(Coins{{Denom: testDenom1, Amount: one}, {Denom: testDenom2, Amount: two}})) + assert.True(t, Coins{}.IsAllLTE(Coins{{Denom: testDenom1, Amount: one}})) } func TestParse(t *testing.T) { @@ -400,12 +400,12 @@ func TestParse(t *testing.T) { expected Coins // if valid is true, make sure this is returned }{ {"", true, nil}, - {"1foo", true, Coins{{"foo", one}}}, - {"10bar", true, Coins{{"bar", NewInt(10)}}}, - {"99bar,1foo", true, Coins{{"bar", NewInt(99)}, {"foo", one}}}, - {"98 bar , 1 foo ", true, Coins{{"bar", NewInt(98)}, {"foo", one}}}, - {" 55\t \t bling\n", true, Coins{{"bling", NewInt(55)}}}, - {"2foo, 97 bar", true, Coins{{"bar", NewInt(97)}, {"foo", NewInt(2)}}}, + {"1foo", true, Coins{{Denom: "foo", Amount: one}}}, + {"10bar", true, Coins{{Denom: "bar", Amount: NewInt(10)}}}, + {"99bar,1foo", true, Coins{{Denom: "bar", Amount: NewInt(99)}, {Denom: "foo", Amount: one}}}, + {"98 bar , 1 foo ", true, Coins{{Denom: "bar", Amount: NewInt(98)}, {Denom: "foo", Amount: one}}}, + {" 55\t \t bling\n", true, Coins{{Denom: "bling", Amount: NewInt(55)}}}, + {"2foo, 97 bar", true, Coins{{Denom: "bar", Amount: NewInt(97)}, {Denom: "foo", Amount: NewInt(2)}}}, {"5 mycoin,", false, nil}, // no empty coins in a list {"2 3foo, 97 bar", false, nil}, // 3foo is invalid coin name {"11me coin, 12you coin", false, nil}, // no spaces in coin names @@ -519,19 +519,19 @@ func TestCoinsIsAnyGTE(t *testing.T) { two := NewInt(2) assert.False(t, Coins{}.IsAnyGTE(Coins{})) - assert.False(t, Coins{{testDenom1, one}}.IsAnyGTE(Coins{})) - assert.False(t, Coins{}.IsAnyGTE(Coins{{testDenom1, one}})) - assert.False(t, Coins{{testDenom1, one}}.IsAnyGTE(Coins{{testDenom1, two}})) - assert.False(t, Coins{{testDenom1, one}}.IsAnyGTE(Coins{{testDenom2, one}})) - assert.True(t, Coins{{testDenom1, one}, {testDenom2, two}}.IsAnyGTE(Coins{{testDenom1, two}, {testDenom2, one}})) - assert.True(t, Coins{{testDenom1, one}}.IsAnyGTE(Coins{{testDenom1, one}})) - assert.True(t, Coins{{testDenom1, two}}.IsAnyGTE(Coins{{testDenom1, one}})) - assert.True(t, Coins{{testDenom1, one}}.IsAnyGTE(Coins{{testDenom1, one}, {testDenom2, two}})) - assert.True(t, Coins{{testDenom2, two}}.IsAnyGTE(Coins{{testDenom1, one}, {testDenom2, two}})) - assert.False(t, Coins{{testDenom2, one}}.IsAnyGTE(Coins{{testDenom1, one}, {testDenom2, two}})) - assert.True(t, Coins{{testDenom1, one}, {testDenom2, two}}.IsAnyGTE(Coins{{testDenom1, one}, {testDenom2, one}})) - assert.True(t, Coins{{testDenom1, one}, {testDenom2, one}}.IsAnyGTE(Coins{{testDenom1, one}, {testDenom2, two}})) - assert.True(t, Coins{{"xxx", one}, {"yyy", one}}.IsAnyGTE(Coins{{testDenom2, one}, {"ccc", one}, {"yyy", one}, {"zzz", one}})) + assert.False(t, Coins{{Denom: testDenom1, Amount: one}}.IsAnyGTE(Coins{})) + assert.False(t, Coins{}.IsAnyGTE(Coins{{Denom: testDenom1, Amount: one}})) + assert.False(t, Coins{{Denom: testDenom1, Amount: one}}.IsAnyGTE(Coins{{Denom: testDenom1, Amount: two}})) + assert.False(t, Coins{{Denom: testDenom1, Amount: one}}.IsAnyGTE(Coins{{Denom: testDenom2, Amount: one}})) + assert.True(t, Coins{{Denom: testDenom1, Amount: one}, {Denom: testDenom2, Amount: two}}.IsAnyGTE(Coins{{Denom: testDenom1, Amount: two}, {Denom: testDenom2, Amount: one}})) + assert.True(t, Coins{{Denom: testDenom1, Amount: one}}.IsAnyGTE(Coins{{Denom: testDenom1, Amount: one}})) + assert.True(t, Coins{{Denom: testDenom1, Amount: two}}.IsAnyGTE(Coins{{Denom: testDenom1, Amount: one}})) + assert.True(t, Coins{{Denom: testDenom1, Amount: one}}.IsAnyGTE(Coins{{Denom: testDenom1, Amount: one}, {Denom: testDenom2, Amount: two}})) + assert.True(t, Coins{{Denom: testDenom2, Amount: two}}.IsAnyGTE(Coins{{Denom: testDenom1, Amount: one}, {Denom: testDenom2, Amount: two}})) + assert.False(t, Coins{{Denom: testDenom2, Amount: one}}.IsAnyGTE(Coins{{Denom: testDenom1, Amount: one}, {Denom: testDenom2, Amount: two}})) + assert.True(t, Coins{{Denom: testDenom1, Amount: one}, {Denom: testDenom2, Amount: two}}.IsAnyGTE(Coins{{Denom: testDenom1, Amount: one}, {Denom: testDenom2, Amount: one}})) + assert.True(t, Coins{{Denom: testDenom1, Amount: one}, {Denom: testDenom2, Amount: one}}.IsAnyGTE(Coins{{Denom: testDenom1, Amount: one}, {Denom: testDenom2, Amount: two}})) + assert.True(t, Coins{{Denom: "xxx", Amount: one}, {Denom: "yyy", Amount: one}}.IsAnyGTE(Coins{{Denom: testDenom2, Amount: one}, {Denom: "ccc", Amount: one}, {Denom: "yyy", Amount: one}, {Denom: "zzz", Amount: one}})) } func TestCoinsIsAllGT(t *testing.T) { @@ -539,19 +539,19 @@ func TestCoinsIsAllGT(t *testing.T) { two := NewInt(2) assert.False(t, Coins{}.IsAllGT(Coins{})) - assert.True(t, Coins{{testDenom1, one}}.IsAllGT(Coins{})) - assert.False(t, Coins{}.IsAllGT(Coins{{testDenom1, one}})) - assert.False(t, Coins{{testDenom1, one}}.IsAllGT(Coins{{testDenom1, two}})) - assert.False(t, Coins{{testDenom1, one}}.IsAllGT(Coins{{testDenom2, one}})) - assert.False(t, Coins{{testDenom1, one}, {testDenom2, two}}.IsAllGT(Coins{{testDenom1, two}, {testDenom2, one}})) - assert.False(t, Coins{{testDenom1, one}}.IsAllGT(Coins{{testDenom1, one}})) - assert.True(t, Coins{{testDenom1, two}}.IsAllGT(Coins{{testDenom1, one}})) - assert.False(t, Coins{{testDenom1, one}}.IsAllGT(Coins{{testDenom1, one}, {testDenom2, two}})) - assert.False(t, Coins{{testDenom2, two}}.IsAllGT(Coins{{testDenom1, one}, {testDenom2, two}})) - assert.False(t, Coins{{testDenom2, one}}.IsAllGT(Coins{{testDenom1, one}, {testDenom2, two}})) - assert.False(t, Coins{{testDenom1, one}, {testDenom2, two}}.IsAllGT(Coins{{testDenom1, one}, {testDenom2, one}})) - assert.False(t, Coins{{testDenom1, one}, {testDenom2, one}}.IsAllGT(Coins{{testDenom1, one}, {testDenom2, two}})) - assert.False(t, Coins{{"xxx", one}, {"yyy", one}}.IsAllGT(Coins{{testDenom2, one}, {"ccc", one}, {"yyy", one}, {"zzz", one}})) + assert.True(t, Coins{{Denom: testDenom1, Amount: one}}.IsAllGT(Coins{})) + assert.False(t, Coins{}.IsAllGT(Coins{{Denom: testDenom1, Amount: one}})) + assert.False(t, Coins{{Denom: testDenom1, Amount: one}}.IsAllGT(Coins{{Denom: testDenom1, Amount: two}})) + assert.False(t, Coins{{Denom: testDenom1, Amount: one}}.IsAllGT(Coins{{Denom: testDenom2, Amount: one}})) + assert.False(t, Coins{{Denom: testDenom1, Amount: one}, {Denom: testDenom2, Amount: two}}.IsAllGT(Coins{{Denom: testDenom1, Amount: two}, {Denom: testDenom2, Amount: one}})) + assert.False(t, Coins{{Denom: testDenom1, Amount: one}}.IsAllGT(Coins{{Denom: testDenom1, Amount: one}})) + assert.True(t, Coins{{Denom: testDenom1, Amount: two}}.IsAllGT(Coins{{Denom: testDenom1, Amount: one}})) + assert.False(t, Coins{{Denom: testDenom1, Amount: one}}.IsAllGT(Coins{{Denom: testDenom1, Amount: one}, {Denom: testDenom2, Amount: two}})) + assert.False(t, Coins{{Denom: testDenom2, Amount: two}}.IsAllGT(Coins{{Denom: testDenom1, Amount: one}, {Denom: testDenom2, Amount: two}})) + assert.False(t, Coins{{Denom: testDenom2, Amount: one}}.IsAllGT(Coins{{Denom: testDenom1, Amount: one}, {Denom: testDenom2, Amount: two}})) + assert.False(t, Coins{{Denom: testDenom1, Amount: one}, {Denom: testDenom2, Amount: two}}.IsAllGT(Coins{{Denom: testDenom1, Amount: one}, {Denom: testDenom2, Amount: one}})) + assert.False(t, Coins{{Denom: testDenom1, Amount: one}, {Denom: testDenom2, Amount: one}}.IsAllGT(Coins{{Denom: testDenom1, Amount: one}, {Denom: testDenom2, Amount: two}})) + assert.False(t, Coins{{Denom: "xxx", Amount: one}, {Denom: "yyy", Amount: one}}.IsAllGT(Coins{{Denom: testDenom2, Amount: one}, {Denom: "ccc", Amount: one}, {Denom: "yyy", Amount: one}, {Denom: "zzz", Amount: one}})) } func TestCoinsIsAllGTE(t *testing.T) { @@ -559,21 +559,21 @@ func TestCoinsIsAllGTE(t *testing.T) { two := NewInt(2) assert.True(t, Coins{}.IsAllGTE(Coins{})) - assert.True(t, Coins{{testDenom1, one}}.IsAllGTE(Coins{})) - assert.True(t, Coins{{testDenom1, one}, {testDenom2, one}}.IsAllGTE(Coins{{testDenom2, one}})) - assert.False(t, Coins{{testDenom1, one}, {testDenom2, one}}.IsAllGTE(Coins{{testDenom2, two}})) - assert.False(t, Coins{}.IsAllGTE(Coins{{testDenom1, one}})) - assert.False(t, Coins{{testDenom1, one}}.IsAllGTE(Coins{{testDenom1, two}})) - assert.False(t, Coins{{testDenom1, one}}.IsAllGTE(Coins{{testDenom2, one}})) - assert.False(t, Coins{{testDenom1, one}, {testDenom2, two}}.IsAllGTE(Coins{{testDenom1, two}, {testDenom2, one}})) - assert.True(t, Coins{{testDenom1, one}}.IsAllGTE(Coins{{testDenom1, one}})) - assert.True(t, Coins{{testDenom1, two}}.IsAllGTE(Coins{{testDenom1, one}})) - assert.False(t, Coins{{testDenom1, one}}.IsAllGTE(Coins{{testDenom1, one}, {testDenom2, two}})) - assert.False(t, Coins{{testDenom2, two}}.IsAllGTE(Coins{{testDenom1, one}, {testDenom2, two}})) - assert.False(t, Coins{{testDenom2, one}}.IsAllGTE(Coins{{testDenom1, one}, {testDenom2, two}})) - assert.True(t, Coins{{testDenom1, one}, {testDenom2, two}}.IsAllGTE(Coins{{testDenom1, one}, {testDenom2, one}})) - assert.False(t, Coins{{testDenom1, one}, {testDenom2, one}}.IsAllGTE(Coins{{testDenom1, one}, {testDenom2, two}})) - assert.False(t, Coins{{"xxx", one}, {"yyy", one}}.IsAllGTE(Coins{{testDenom2, one}, {"ccc", one}, {"yyy", one}, {"zzz", one}})) + assert.True(t, Coins{{Denom: testDenom1, Amount: one}}.IsAllGTE(Coins{})) + assert.True(t, Coins{{Denom: testDenom1, Amount: one}, {Denom: testDenom2, Amount: one}}.IsAllGTE(Coins{{Denom: testDenom2, Amount: one}})) + assert.False(t, Coins{{Denom: testDenom1, Amount: one}, {Denom: testDenom2, Amount: one}}.IsAllGTE(Coins{{Denom: testDenom2, Amount: two}})) + assert.False(t, Coins{}.IsAllGTE(Coins{{Denom: testDenom1, Amount: one}})) + assert.False(t, Coins{{Denom: testDenom1, Amount: one}}.IsAllGTE(Coins{{Denom: testDenom1, Amount: two}})) + assert.False(t, Coins{{Denom: testDenom1, Amount: one}}.IsAllGTE(Coins{{Denom: testDenom2, Amount: one}})) + assert.False(t, Coins{{Denom: testDenom1, Amount: one}, {Denom: testDenom2, Amount: two}}.IsAllGTE(Coins{{Denom: testDenom1, Amount: two}, {Denom: testDenom2, Amount: one}})) + assert.True(t, Coins{{Denom: testDenom1, Amount: one}}.IsAllGTE(Coins{{Denom: testDenom1, Amount: one}})) + assert.True(t, Coins{{Denom: testDenom1, Amount: two}}.IsAllGTE(Coins{{Denom: testDenom1, Amount: one}})) + assert.False(t, Coins{{Denom: testDenom1, Amount: one}}.IsAllGTE(Coins{{Denom: testDenom1, Amount: one}, {Denom: testDenom2, Amount: two}})) + assert.False(t, Coins{{Denom: testDenom2, Amount: two}}.IsAllGTE(Coins{{Denom: testDenom1, Amount: one}, {Denom: testDenom2, Amount: two}})) + assert.False(t, Coins{{Denom: testDenom2, Amount: one}}.IsAllGTE(Coins{{Denom: testDenom1, Amount: one}, {Denom: testDenom2, Amount: two}})) + assert.True(t, Coins{{Denom: testDenom1, Amount: one}, {Denom: testDenom2, Amount: two}}.IsAllGTE(Coins{{Denom: testDenom1, Amount: one}, {Denom: testDenom2, Amount: one}})) + assert.False(t, Coins{{Denom: testDenom1, Amount: one}, {Denom: testDenom2, Amount: one}}.IsAllGTE(Coins{{Denom: testDenom1, Amount: one}, {Denom: testDenom2, Amount: two}})) + assert.False(t, Coins{{Denom: "xxx", Amount: one}, {Denom: "yyy", Amount: one}}.IsAllGTE(Coins{{Denom: testDenom2, Amount: one}, {Denom: "ccc", Amount: one}, {Denom: "yyy", Amount: one}, {Denom: "zzz", Amount: one}})) } func TestNewCoins(t *testing.T) { diff --git a/types/dec_coin_test.go b/types/dec_coin_test.go index 65408a0b3ab3..9ee113effeeb 100644 --- a/types/dec_coin_test.go +++ b/types/dec_coin_test.go @@ -39,16 +39,16 @@ func TestNewDecCoinFromDec(t *testing.T) { func TestNewDecCoinFromCoin(t *testing.T) { require.NotPanics(t, func() { - NewDecCoinFromCoin(Coin{testDenom1, NewInt(5)}) + NewDecCoinFromCoin(Coin{Denom: testDenom1, Amount: NewInt(5)}) }) require.NotPanics(t, func() { - NewDecCoinFromCoin(Coin{testDenom1, NewInt(0)}) + NewDecCoinFromCoin(Coin{Denom: testDenom1, Amount: NewInt(0)}) }) require.Panics(t, func() { - NewDecCoinFromCoin(Coin{strings.ToUpper(testDenom1), NewInt(5)}) + NewDecCoinFromCoin(Coin{Denom: strings.ToUpper(testDenom1), Amount: NewInt(5)}) }) require.Panics(t, func() { - NewDecCoinFromCoin(Coin{testDenom1, NewInt(-5)}) + NewDecCoinFromCoin(Coin{Denom: testDenom1, Amount: NewInt(-5)}) }) } diff --git a/types/int.go b/types/int.go index 41a6efef74c7..814ec5d70b92 100644 --- a/types/int.go +++ b/types/int.go @@ -52,12 +52,6 @@ func max(i *big.Int, i2 *big.Int) *big.Int { return new(big.Int).Set(i) } -// MarshalAmino for custom encoding scheme -func marshalAmino(i *big.Int) (string, error) { - bz, err := i.MarshalText() - return string(bz), err -} - func unmarshalText(i *big.Int, text string) error { if err := i.UnmarshalText([]byte(text)); err != nil { return err @@ -70,11 +64,6 @@ func unmarshalText(i *big.Int, text string) error { return nil } -// UnmarshalAmino for custom decoding scheme -func unmarshalAmino(i *big.Int, text string) (err error) { - return unmarshalText(i, text) -} - // MarshalJSON for custom encoding scheme // Must be encoded as a string for JSON precision func marshalJSON(i *big.Int) ([]byte, error) { @@ -320,22 +309,6 @@ func (i Int) String() string { return i.i.String() } -// MarshalAmino defines custom encoding scheme -func (i Int) MarshalAmino() (string, error) { - if i.i == nil { // Necessary since default Uint initialization has i.i as nil - i.i = new(big.Int) - } - return marshalAmino(i.i) -} - -// UnmarshalAmino defines custom decoding scheme -func (i *Int) UnmarshalAmino(text string) error { - if i.i == nil { // Necessary since default Int initialization has i.i as nil - i.i = new(big.Int) - } - return unmarshalAmino(i.i, text) -} - // MarshalJSON defines custom encoding scheme func (i Int) MarshalJSON() ([]byte, error) { if i.i == nil { // Necessary since default Uint initialization has i.i as nil @@ -359,3 +332,62 @@ func (i Int) MarshalYAML() (interface{}, error) { return i.String(), nil } func IntEq(t *testing.T, exp, got Int) (*testing.T, bool, string, string, string) { return t, exp.Equal(got), "expected:\t%v\ngot:\t\t%v", exp.String(), got.String() } + +// Marshal implements the gogo proto custom type interface. +func (i Int) Marshal() ([]byte, error) { + if i.i == nil { + i.i = new(big.Int) + } + return i.i.MarshalText() +} + +// MarshalTo implements the gogo proto custom type interface. +func (i *Int) MarshalTo(data []byte) (n int, err error) { + if i.i == nil { + i.i = new(big.Int) + } + if len(i.i.Bytes()) == 0 { + return 0, nil + } + + bz, err := i.Marshal() + if err != nil { + return 0, err + } + + copy(data, bz) + return len(bz), nil +} + +// Unmarshal implements the gogo proto custom type interface. +func (i *Int) Unmarshal(data []byte) error { + if len(data) == 0 { + i = nil + return nil + } + + if i.i == nil { + i.i = new(big.Int) + } + + if err := i.i.UnmarshalText(data); err != nil { + return err + } + + if i.i.BitLen() > maxBitLen { + return fmt.Errorf("integer out of range; got: %d, max: %d", i.i.BitLen(), maxBitLen) + } + + return nil +} + +// Size implements the gogo proto custom type interface. +func (i *Int) Size() int { + bz, _ := i.Marshal() + return len(bz) +} + +// Compare implements the gogo proto custom type interface. +func (i Int) Compare(other Int) int { + return i.i.Cmp(other.i) +} diff --git a/types/int_test.go b/types/int_test.go index f6dbc1b407e9..0b3d7a1df17a 100644 --- a/types/int_test.go +++ b/types/int_test.go @@ -274,21 +274,21 @@ func TestEncodingRandom(t *testing.T) { ni := NewInt(n) var ri Int - str, err := ni.MarshalAmino() + str, err := ni.Marshal() require.Nil(t, err) - err = (&ri).UnmarshalAmino(str) + err = (&ri).Unmarshal(str) require.Nil(t, err) - require.Equal(t, ni, ri, "MarshalAmino * UnmarshalAmino is not identity. tc #%d, Expected %s, Actual %s", i, ni.String(), ri.String()) - require.True(t, ni.i != ri.i, "Pointer addresses are equal. tc #%d", i) + require.Equal(t, ni, ri, "binary mismatch; tc #%d, expected %s, actual %s", i, ni.String(), ri.String()) + require.True(t, ni.i != ri.i, "pointer addresses are equal; tc #%d", i) bz, err := ni.MarshalJSON() require.Nil(t, err) err = (&ri).UnmarshalJSON(bz) require.Nil(t, err) - require.Equal(t, ni, ri, "MarshalJSON * UnmarshalJSON is not identity. tc #%d, Expected %s, Actual %s", i, ni.String(), ri.String()) - require.True(t, ni.i != ri.i, "Pointer addresses are equal. tc #%d", i) + require.Equal(t, ni, ri, "json mismatch; tc #%d, expected %s, actual %s", i, ni.String(), ri.String()) + require.True(t, ni.i != ri.i, "pointer addresses are equal; tc #%d", i) } for i := 0; i < 1000; i++ { @@ -296,21 +296,21 @@ func TestEncodingRandom(t *testing.T) { ni := NewUint(n) var ri Uint - str, err := ni.MarshalAmino() + str, err := ni.Marshal() require.Nil(t, err) - err = (&ri).UnmarshalAmino(str) + err = (&ri).Unmarshal(str) require.Nil(t, err) - require.Equal(t, ni, ri, "MarshalAmino * UnmarshalAmino is not identity. tc #%d, Expected %s, Actual %s", i, ni.String(), ri.String()) - require.True(t, ni.i != ri.i, "Pointer addresses are equal. tc #%d", i) + require.Equal(t, ni, ri, "binary mismatch; tc #%d, expected %s, actual %s", i, ni.String(), ri.String()) + require.True(t, ni.i != ri.i, "pointer addresses are equal; tc #%d", i) bz, err := ni.MarshalJSON() require.Nil(t, err) err = (&ri).UnmarshalJSON(bz) require.Nil(t, err) - require.Equal(t, ni, ri, "MarshalJSON * UnmarshalJSON is not identity. tc #%d, Expected %s, Actual %s", i, ni.String(), ri.String()) - require.True(t, ni.i != ri.i, "Pointer addresses are equal. tc #%d", i) + require.Equal(t, ni, ri, "json mismatch; tc #%d, expected %s, actual %s", i, ni.String(), ri.String()) + require.True(t, ni.i != ri.i, "pointer addresses are equal; tc #%d", i) } } @@ -318,29 +318,35 @@ func TestEncodingTableInt(t *testing.T) { var i Int cases := []struct { - i Int - bz []byte - str string + i Int + jsonBz []byte + rawBz []byte }{ - {NewInt(0), []byte("\"0\""), "0"}, - {NewInt(100), []byte("\"100\""), "100"}, - {NewInt(51842), []byte("\"51842\""), "51842"}, - {NewInt(19513368), []byte("\"19513368\""), "19513368"}, - {NewInt(999999999999), []byte("\"999999999999\""), "999999999999"}, + {NewInt(0), []byte("\"0\""), []byte{0x30}}, + {NewInt(100), []byte("\"100\""), []byte{0x31, 0x30, 0x30}}, + {NewInt(-100), []byte("\"-100\""), []byte{0x2d, 0x31, 0x30, 0x30}}, + {NewInt(51842), []byte("\"51842\""), []byte{0x35, 0x31, 0x38, 0x34, 0x32}}, + {NewInt(-51842), []byte("\"-51842\""), []byte{0x2d, 0x35, 0x31, 0x38, 0x34, 0x32}}, + {NewInt(19513368), []byte("\"19513368\""), []byte{0x31, 0x39, 0x35, 0x31, 0x33, 0x33, 0x36, 0x38}}, + {NewInt(-19513368), []byte("\"-19513368\""), []byte{0x2d, 0x31, 0x39, 0x35, 0x31, 0x33, 0x33, 0x36, 0x38}}, + {NewInt(999999999999), []byte("\"999999999999\""), []byte{0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39}}, + {NewInt(-999999999999), []byte("\"-999999999999\""), []byte{0x2d, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39}}, } for tcnum, tc := range cases { bz, err := tc.i.MarshalJSON() require.Nil(t, err, "Error marshaling Int. tc #%d, err %s", tcnum, err) - require.Equal(t, tc.bz, bz, "Marshaled value is different from exported. tc #%d", tcnum) + require.Equal(t, tc.jsonBz, bz, "Marshaled value is different from exported. tc #%d", tcnum) + err = (&i).UnmarshalJSON(bz) require.Nil(t, err, "Error unmarshaling Int. tc #%d, err %s", tcnum, err) require.Equal(t, tc.i, i, "Unmarshaled value is different from exported. tc #%d", tcnum) - str, err := tc.i.MarshalAmino() + bz, err = tc.i.Marshal() require.Nil(t, err, "Error marshaling Int. tc #%d, err %s", tcnum, err) - require.Equal(t, tc.str, str, "Marshaled value is different from exported. tc #%d", tcnum) - err = (&i).UnmarshalAmino(str) + require.Equal(t, tc.rawBz, bz, "Marshaled value is different from exported. tc #%d", tcnum) + + err = (&i).Unmarshal(bz) require.Nil(t, err, "Error unmarshaling Int. tc #%d, err %s", tcnum, err) require.Equal(t, tc.i, i, "Unmarshaled value is different from exported. tc #%d", tcnum) } @@ -350,29 +356,30 @@ func TestEncodingTableUint(t *testing.T) { var i Uint cases := []struct { - i Uint - bz []byte - str string + i Uint + jsonBz []byte + rawBz []byte }{ - {NewUint(0), []byte("\"0\""), "0"}, - {NewUint(100), []byte("\"100\""), "100"}, - {NewUint(51842), []byte("\"51842\""), "51842"}, - {NewUint(19513368), []byte("\"19513368\""), "19513368"}, - {NewUint(999999999999), []byte("\"999999999999\""), "999999999999"}, + {NewUint(0), []byte("\"0\""), []byte{0x30}}, + {NewUint(100), []byte("\"100\""), []byte{0x31, 0x30, 0x30}}, + {NewUint(51842), []byte("\"51842\""), []byte{0x35, 0x31, 0x38, 0x34, 0x32}}, + {NewUint(19513368), []byte("\"19513368\""), []byte{0x31, 0x39, 0x35, 0x31, 0x33, 0x33, 0x36, 0x38}}, + {NewUint(999999999999), []byte("\"999999999999\""), []byte{0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39}}, } for tcnum, tc := range cases { bz, err := tc.i.MarshalJSON() require.Nil(t, err, "Error marshaling Int. tc #%d, err %s", tcnum, err) - require.Equal(t, tc.bz, bz, "Marshaled value is different from exported. tc #%d", tcnum) + require.Equal(t, tc.jsonBz, bz, "Marshaled value is different from exported. tc #%d", tcnum) err = (&i).UnmarshalJSON(bz) require.Nil(t, err, "Error unmarshaling Int. tc #%d, err %s", tcnum, err) require.Equal(t, tc.i, i, "Unmarshaled value is different from exported. tc #%d", tcnum) - str, err := tc.i.MarshalAmino() + bz, err = tc.i.Marshal() require.Nil(t, err, "Error marshaling Int. tc #%d, err %s", tcnum, err) - require.Equal(t, tc.str, str, "Marshaled value is different from exported. tc #%d", tcnum) - err = (&i).UnmarshalAmino(str) + require.Equal(t, tc.rawBz, bz, "Marshaled value is different from exported. tc #%d", tcnum) + + err = (&i).Unmarshal(bz) require.Nil(t, err, "Error unmarshaling Int. tc #%d, err %s", tcnum, err) require.Equal(t, tc.i, i, "Unmarshaled value is different from exported. tc #%d", tcnum) } @@ -383,11 +390,11 @@ func TestSerializationOverflow(t *testing.T) { x := Int{bx} y := new(Int) - // require amino deserialization to fail due to overflow - xStr, err := x.MarshalAmino() + xStr, err := x.Marshal() require.NoError(t, err) - err = y.UnmarshalAmino(xStr) + // require deserialization to fail due to overflow + err = y.Unmarshal(xStr) require.Error(t, err) // require JSON deserialization to fail due to overflow diff --git a/types/types.pb.go b/types/types.pb.go new file mode 100644 index 000000000000..8759c70cda02 --- /dev/null +++ b/types/types.pb.go @@ -0,0 +1,784 @@ +// Code generated by protoc-gen-gogo. DO NOT EDIT. +// source: types/types.proto + +package types + +import ( + fmt "fmt" + _ "github.com/gogo/protobuf/gogoproto" + proto "github.com/golang/protobuf/proto" + io "io" + math "math" + math_bits "math/bits" + reflect "reflect" + strings "strings" +) + +// Reference imports to suppress errors if they are not otherwise used. +var _ = proto.Marshal +var _ = fmt.Errorf +var _ = math.Inf + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the proto package it is being compiled against. +// A compilation error at this line likely means your copy of the +// proto package needs to be updated. +const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package + +// ProtoInt defines a big integer that is represented as a string and decorated +// by a custom type Int. +type ProtoInt struct { + I string `protobuf:"bytes,1,opt,name=i,proto3" json:"i,omitempty"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` +} + +func (m *ProtoInt) Reset() { *m = ProtoInt{} } +func (*ProtoInt) ProtoMessage() {} +func (*ProtoInt) Descriptor() ([]byte, []int) { + return fileDescriptor_2c0f90c600ad7e2e, []int{0} +} +func (m *ProtoInt) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *ProtoInt) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_ProtoInt.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalToSizedBuffer(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (m *ProtoInt) XXX_Merge(src proto.Message) { + xxx_messageInfo_ProtoInt.Merge(m, src) +} +func (m *ProtoInt) XXX_Size() int { + return m.Size() +} +func (m *ProtoInt) XXX_DiscardUnknown() { + xxx_messageInfo_ProtoInt.DiscardUnknown(m) +} + +var xxx_messageInfo_ProtoInt proto.InternalMessageInfo + +func (m *ProtoInt) GetI() string { + if m != nil { + return m.I + } + return "" +} + +// ProtoUint defines a big unsigned integer that is represented as a string and +// decorated by a custom type Uint. +type ProtoUint struct { + I string `protobuf:"bytes,1,opt,name=i,proto3" json:"i,omitempty"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` +} + +func (m *ProtoUint) Reset() { *m = ProtoUint{} } +func (*ProtoUint) ProtoMessage() {} +func (*ProtoUint) Descriptor() ([]byte, []int) { + return fileDescriptor_2c0f90c600ad7e2e, []int{1} +} +func (m *ProtoUint) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *ProtoUint) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_ProtoUint.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalToSizedBuffer(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (m *ProtoUint) XXX_Merge(src proto.Message) { + xxx_messageInfo_ProtoUint.Merge(m, src) +} +func (m *ProtoUint) XXX_Size() int { + return m.Size() +} +func (m *ProtoUint) XXX_DiscardUnknown() { + xxx_messageInfo_ProtoUint.DiscardUnknown(m) +} + +var xxx_messageInfo_ProtoUint proto.InternalMessageInfo + +func (m *ProtoUint) GetI() string { + if m != nil { + return m.I + } + return "" +} + +// Coin defines a token with a denomination and an amount. +// +// NOTE: The amount field is an Int which implements the custom method +// signatures required by gogoproto. +type Coin struct { + Denom string `protobuf:"bytes,1,opt,name=denom,proto3" json:"denom,omitempty"` + Amount Int `protobuf:"bytes,2,opt,name=amount,proto3,customtype=Int" json:"amount"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` +} + +func (m *Coin) Reset() { *m = Coin{} } +func (*Coin) ProtoMessage() {} +func (*Coin) Descriptor() ([]byte, []int) { + return fileDescriptor_2c0f90c600ad7e2e, []int{2} +} +func (m *Coin) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *Coin) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_Coin.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalToSizedBuffer(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (m *Coin) XXX_Merge(src proto.Message) { + xxx_messageInfo_Coin.Merge(m, src) +} +func (m *Coin) XXX_Size() int { + return m.Size() +} +func (m *Coin) XXX_DiscardUnknown() { + xxx_messageInfo_Coin.DiscardUnknown(m) +} + +var xxx_messageInfo_Coin proto.InternalMessageInfo + +func (m *Coin) GetDenom() string { + if m != nil { + return m.Denom + } + return "" +} + +func init() { + proto.RegisterType((*ProtoInt)(nil), "types.ProtoInt") + proto.RegisterType((*ProtoUint)(nil), "types.ProtoUint") + proto.RegisterType((*Coin)(nil), "types.Coin") +} + +func init() { proto.RegisterFile("types/types.proto", fileDescriptor_2c0f90c600ad7e2e) } + +var fileDescriptor_2c0f90c600ad7e2e = []byte{ + // 216 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x12, 0x2c, 0xa9, 0x2c, 0x48, + 0x2d, 0xd6, 0x07, 0x93, 0x7a, 0x05, 0x45, 0xf9, 0x25, 0xf9, 0x42, 0xac, 0x60, 0x8e, 0x94, 0x49, + 0x59, 0x6a, 0x5e, 0x4a, 0x7e, 0x91, 0x7e, 0x7a, 0x66, 0x49, 0x46, 0x69, 0x92, 0x5e, 0x72, 0x7e, + 0xae, 0x7e, 0x7a, 0x7e, 0x7a, 0xbe, 0x3e, 0x58, 0x51, 0x52, 0x69, 0x1a, 0x98, 0x07, 0xe6, 0x80, + 0x59, 0x10, 0xcd, 0x4a, 0x72, 0x5c, 0x1c, 0x01, 0x20, 0x86, 0x67, 0x5e, 0x89, 0x10, 0x0f, 0x17, + 0x63, 0xa6, 0x04, 0xa3, 0x02, 0xa3, 0x06, 0x67, 0x10, 0x63, 0xa6, 0x15, 0x4b, 0xc3, 0x1d, 0x05, + 0x46, 0x25, 0x79, 0x2e, 0x4e, 0xb0, 0x7c, 0x68, 0x26, 0x0e, 0x05, 0x41, 0x5c, 0x2c, 0xce, 0xf9, + 0x99, 0x79, 0x42, 0x22, 0x5c, 0xac, 0x29, 0xa9, 0x79, 0xf9, 0xb9, 0x50, 0x79, 0x08, 0x47, 0xc8, + 0x84, 0x8b, 0x2d, 0x31, 0x37, 0xbf, 0x34, 0xaf, 0x44, 0x82, 0x49, 0x81, 0x51, 0x83, 0xdb, 0x88, + 0x5f, 0x0f, 0xe2, 0x72, 0x98, 0x9d, 0x4e, 0xdc, 0x27, 0xee, 0xc9, 0x33, 0xdc, 0xba, 0x27, 0xcf, + 0xec, 0x99, 0x57, 0x12, 0x04, 0x55, 0xeb, 0x24, 0x7f, 0xe3, 0xa1, 0x1c, 0x43, 0xc3, 0x23, 0x39, + 0x86, 0x13, 0x8f, 0xe4, 0x18, 0x2f, 0x3c, 0x92, 0x63, 0x7c, 0xf0, 0x48, 0x8e, 0x71, 0xc6, 0x63, + 0x39, 0x86, 0x28, 0x88, 0x5f, 0x93, 0xd8, 0xc0, 0x8e, 0x37, 0x06, 0x04, 0x00, 0x00, 0xff, 0xff, + 0xe2, 0x07, 0x0a, 0xee, 0x0e, 0x01, 0x00, 0x00, +} + +func (m *ProtoInt) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalToSizedBuffer(dAtA[:size]) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *ProtoInt) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *ProtoInt) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + if m.XXX_unrecognized != nil { + i -= len(m.XXX_unrecognized) + copy(dAtA[i:], m.XXX_unrecognized) + } + if len(m.I) > 0 { + i -= len(m.I) + copy(dAtA[i:], m.I) + i = encodeVarintTypes(dAtA, i, uint64(len(m.I))) + i-- + dAtA[i] = 0xa + } + return len(dAtA) - i, nil +} + +func (m *ProtoUint) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalToSizedBuffer(dAtA[:size]) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *ProtoUint) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *ProtoUint) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + if m.XXX_unrecognized != nil { + i -= len(m.XXX_unrecognized) + copy(dAtA[i:], m.XXX_unrecognized) + } + if len(m.I) > 0 { + i -= len(m.I) + copy(dAtA[i:], m.I) + i = encodeVarintTypes(dAtA, i, uint64(len(m.I))) + i-- + dAtA[i] = 0xa + } + return len(dAtA) - i, nil +} + +func (m *Coin) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalToSizedBuffer(dAtA[:size]) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *Coin) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *Coin) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + if m.XXX_unrecognized != nil { + i -= len(m.XXX_unrecognized) + copy(dAtA[i:], m.XXX_unrecognized) + } + { + size := m.Amount.Size() + i -= size + if _, err := m.Amount.MarshalTo(dAtA[i:]); err != nil { + return 0, err + } + i = encodeVarintTypes(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0x12 + if len(m.Denom) > 0 { + i -= len(m.Denom) + copy(dAtA[i:], m.Denom) + i = encodeVarintTypes(dAtA, i, uint64(len(m.Denom))) + i-- + dAtA[i] = 0xa + } + return len(dAtA) - i, nil +} + +func encodeVarintTypes(dAtA []byte, offset int, v uint64) int { + offset -= sovTypes(v) + base := offset + for v >= 1<<7 { + dAtA[offset] = uint8(v&0x7f | 0x80) + v >>= 7 + offset++ + } + dAtA[offset] = uint8(v) + return base +} +func (m *ProtoInt) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + l = len(m.I) + if l > 0 { + n += 1 + l + sovTypes(uint64(l)) + } + if m.XXX_unrecognized != nil { + n += len(m.XXX_unrecognized) + } + return n +} + +func (m *ProtoUint) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + l = len(m.I) + if l > 0 { + n += 1 + l + sovTypes(uint64(l)) + } + if m.XXX_unrecognized != nil { + n += len(m.XXX_unrecognized) + } + return n +} + +func (m *Coin) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + l = len(m.Denom) + if l > 0 { + n += 1 + l + sovTypes(uint64(l)) + } + l = m.Amount.Size() + n += 1 + l + sovTypes(uint64(l)) + if m.XXX_unrecognized != nil { + n += len(m.XXX_unrecognized) + } + return n +} + +func sovTypes(x uint64) (n int) { + return (math_bits.Len64(x|1) + 6) / 7 +} +func sozTypes(x uint64) (n int) { + return sovTypes(uint64((x << 1) ^ uint64((int64(x) >> 63)))) +} +func (this *ProtoInt) String() string { + if this == nil { + return "nil" + } + s := strings.Join([]string{`&ProtoInt{`, + `I:` + fmt.Sprintf("%v", this.I) + `,`, + `XXX_unrecognized:` + fmt.Sprintf("%v", this.XXX_unrecognized) + `,`, + `}`, + }, "") + return s +} +func (this *ProtoUint) String() string { + if this == nil { + return "nil" + } + s := strings.Join([]string{`&ProtoUint{`, + `I:` + fmt.Sprintf("%v", this.I) + `,`, + `XXX_unrecognized:` + fmt.Sprintf("%v", this.XXX_unrecognized) + `,`, + `}`, + }, "") + return s +} +func valueToStringTypes(v interface{}) string { + rv := reflect.ValueOf(v) + if rv.IsNil() { + return "nil" + } + pv := reflect.Indirect(rv).Interface() + return fmt.Sprintf("*%v", pv) +} +func (m *ProtoInt) Unmarshal(dAtA []byte) error { + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTypes + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: ProtoInt: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: ProtoInt: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field I", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTypes + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthTypes + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthTypes + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.I = string(dAtA[iNdEx:postIndex]) + iNdEx = postIndex + default: + iNdEx = preIndex + skippy, err := skipTypes(dAtA[iNdEx:]) + if err != nil { + return err + } + if skippy < 0 { + return ErrInvalidLengthTypes + } + if (iNdEx + skippy) < 0 { + return ErrInvalidLengthTypes + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + m.XXX_unrecognized = append(m.XXX_unrecognized, dAtA[iNdEx:iNdEx+skippy]...) + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} +func (m *ProtoUint) Unmarshal(dAtA []byte) error { + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTypes + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: ProtoUint: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: ProtoUint: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field I", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTypes + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthTypes + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthTypes + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.I = string(dAtA[iNdEx:postIndex]) + iNdEx = postIndex + default: + iNdEx = preIndex + skippy, err := skipTypes(dAtA[iNdEx:]) + if err != nil { + return err + } + if skippy < 0 { + return ErrInvalidLengthTypes + } + if (iNdEx + skippy) < 0 { + return ErrInvalidLengthTypes + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + m.XXX_unrecognized = append(m.XXX_unrecognized, dAtA[iNdEx:iNdEx+skippy]...) + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} +func (m *Coin) Unmarshal(dAtA []byte) error { + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTypes + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: Coin: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: Coin: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Denom", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTypes + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthTypes + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthTypes + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.Denom = string(dAtA[iNdEx:postIndex]) + iNdEx = postIndex + case 2: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Amount", wireType) + } + var msglen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTypes + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + msglen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if msglen < 0 { + return ErrInvalidLengthTypes + } + postIndex := iNdEx + msglen + if postIndex < 0 { + return ErrInvalidLengthTypes + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + if err := m.Amount.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + default: + iNdEx = preIndex + skippy, err := skipTypes(dAtA[iNdEx:]) + if err != nil { + return err + } + if skippy < 0 { + return ErrInvalidLengthTypes + } + if (iNdEx + skippy) < 0 { + return ErrInvalidLengthTypes + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + m.XXX_unrecognized = append(m.XXX_unrecognized, dAtA[iNdEx:iNdEx+skippy]...) + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} +func skipTypes(dAtA []byte) (n int, err error) { + l := len(dAtA) + iNdEx := 0 + depth := 0 + for iNdEx < l { + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowTypes + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= (uint64(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + wireType := int(wire & 0x7) + switch wireType { + case 0: + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowTypes + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + iNdEx++ + if dAtA[iNdEx-1] < 0x80 { + break + } + } + case 1: + iNdEx += 8 + case 2: + var length int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowTypes + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + length |= (int(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + if length < 0 { + return 0, ErrInvalidLengthTypes + } + iNdEx += length + case 3: + depth++ + case 4: + if depth == 0 { + return 0, ErrUnexpectedEndOfGroupTypes + } + depth-- + case 5: + iNdEx += 4 + default: + return 0, fmt.Errorf("proto: illegal wireType %d", wireType) + } + if iNdEx < 0 { + return 0, ErrInvalidLengthTypes + } + if depth == 0 { + return iNdEx, nil + } + } + return 0, io.ErrUnexpectedEOF +} + +var ( + ErrInvalidLengthTypes = fmt.Errorf("proto: negative length found during unmarshaling") + ErrIntOverflowTypes = fmt.Errorf("proto: integer overflow") + ErrUnexpectedEndOfGroupTypes = fmt.Errorf("proto: unexpected end of group") +) diff --git a/types/types.proto b/types/types.proto new file mode 100644 index 000000000000..3dae0ce9c00b --- /dev/null +++ b/types/types.proto @@ -0,0 +1,37 @@ +syntax = "proto3"; +package types; + +option go_package = "types"; +option (gogoproto.goproto_stringer_all) = false; +option (gogoproto.stringer_all) = false; +option (gogoproto.marshaler_all) = true; +option (gogoproto.unmarshaler_all) = true; +option (gogoproto.sizer_all) = true; + +import "vendor/github.com/gogo/protobuf/gogoproto/gogo.proto"; + +// ProtoInt defines a big integer that is represented as a string and decorated +// by a custom type Int. +message ProtoInt { + option (gogoproto.stringer) = true; + + string i = 1; +} + +// ProtoUint defines a big unsigned integer that is represented as a string and +// decorated by a custom type Uint. +message ProtoUint { + option (gogoproto.stringer) = true; + + string i = 1; +} + +// Coin defines a token with a denomination and an amount. +// +// NOTE: The amount field is an Int which implements the custom method +// signatures required by gogoproto. +message Coin { + string denom = 1; + ProtoInt amount = 2 + [ (gogoproto.customtype) = "Int", (gogoproto.nullable) = false ]; +} \ No newline at end of file diff --git a/types/uint.go b/types/uint.go index 1cc289b900dc..4d02387270e8 100644 --- a/types/uint.go +++ b/types/uint.go @@ -119,36 +119,79 @@ func MaxUint(u1, u2 Uint) Uint { return NewUintFromBigInt(max(u1.i, u2.i)) } // Human readable string func (u Uint) String() string { return u.i.String() } -// MarshalAmino defines custom encoding scheme -func (u Uint) MarshalAmino() (string, error) { +// MarshalJSON defines custom encoding scheme +func (u Uint) MarshalJSON() ([]byte, error) { if u.i == nil { // Necessary since default Uint initialization has i.i as nil u.i = new(big.Int) } - return marshalAmino(u.i) + return marshalJSON(u.i) } -// UnmarshalAmino defines custom decoding scheme -func (u *Uint) UnmarshalAmino(text string) error { +// UnmarshalJSON defines custom decoding scheme +func (u *Uint) UnmarshalJSON(bz []byte) error { if u.i == nil { // Necessary since default Uint initialization has i.i as nil u.i = new(big.Int) } - return unmarshalAmino(u.i, text) + return unmarshalJSON(u.i, bz) } -// MarshalJSON defines custom encoding scheme -func (u Uint) MarshalJSON() ([]byte, error) { - if u.i == nil { // Necessary since default Uint initialization has i.i as nil +// Marshal implements the gogo proto custom type interface. +func (u Uint) Marshal() ([]byte, error) { + if u.i == nil { u.i = new(big.Int) } - return marshalJSON(u.i) + return u.i.MarshalText() } -// UnmarshalJSON defines custom decoding scheme -func (u *Uint) UnmarshalJSON(bz []byte) error { - if u.i == nil { // Necessary since default Uint initialization has i.i as nil +// MarshalTo implements the gogo proto custom type interface. +func (u *Uint) MarshalTo(data []byte) (n int, err error) { + if u.i == nil { u.i = new(big.Int) } - return unmarshalJSON(u.i, bz) + if len(u.i.Bytes()) == 0 { + return 0, nil + } + + bz, err := u.Marshal() + if err != nil { + return 0, err + } + + copy(data, bz) + return len(bz), nil +} + +// Unmarshal implements the gogo proto custom type interface. +func (u *Uint) Unmarshal(data []byte) error { + if len(data) == 0 { + u = nil + return nil + } + + if u.i == nil { + u.i = new(big.Int) + } + + if err := u.i.UnmarshalText(data); err != nil { + return err + } + + if u.i.BitLen() > maxBitLen { + return fmt.Errorf("integer out of range; got: %d, max: %d", u.i.BitLen(), maxBitLen) + } + + return nil +} + +// Size implements the gogo proto custom type interface. +func (u *Uint) Size() int { + bz, _ := u.Marshal() + return len(bz) +} + +// Compare implements the gogo proto custom type interface. +func (u Uint) Compare(other Int) int { + return u.i.Cmp(other.i) } //__________________________________________________________________________ From 2c58592059977caa525ea878368cd793378c853f Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Tue, 31 Dec 2019 10:50:18 -0500 Subject: [PATCH 09/40] Add newlines --- scripts/protocgen.sh | 2 +- types/types.proto | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/protocgen.sh b/scripts/protocgen.sh index 3d7e51b5d964..3e90d5d6eefb 100755 --- a/scripts/protocgen.sh +++ b/scripts/protocgen.sh @@ -10,4 +10,4 @@ for dir in $proto_dirs; do -Ivendor/github.com/gogo/protobuf/gogoproto \ --gofast_out=. \ $(find "${dir}" -name '*.proto') -done \ No newline at end of file +done diff --git a/types/types.proto b/types/types.proto index 3dae0ce9c00b..9b8ce130d49b 100644 --- a/types/types.proto +++ b/types/types.proto @@ -34,4 +34,4 @@ message Coin { string denom = 1; ProtoInt amount = 2 [ (gogoproto.customtype) = "Int", (gogoproto.nullable) = false ]; -} \ No newline at end of file +} From dac3b5ef1b99d9b3ad4ca569e83afeaa3d82c0ff Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Tue, 31 Dec 2019 16:42:51 -0500 Subject: [PATCH 10/40] Fix MarshalTo for nil case --- types/int.go | 5 +++-- types/int_test.go | 7 ++++--- types/uint.go | 3 ++- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/types/int.go b/types/int.go index 814ec5d70b92..78c00b98607c 100644 --- a/types/int.go +++ b/types/int.go @@ -325,7 +325,7 @@ func (i *Int) UnmarshalJSON(bz []byte) error { return unmarshalJSON(i.i, bz) } -// MarshalYAML returns Ythe AML representation. +// MarshalYAML returns the YAML representation. func (i Int) MarshalYAML() (interface{}, error) { return i.String(), nil } // intended to be used with require/assert: require.True(IntEq(...)) @@ -347,7 +347,8 @@ func (i *Int) MarshalTo(data []byte) (n int, err error) { i.i = new(big.Int) } if len(i.i.Bytes()) == 0 { - return 0, nil + copy(data, []byte{0x30}) + return 1, nil } bz, err := i.Marshal() diff --git a/types/int_test.go b/types/int_test.go index 0b3d7a1df17a..01ec6ed7d590 100644 --- a/types/int_test.go +++ b/types/int_test.go @@ -371,6 +371,7 @@ func TestEncodingTableUint(t *testing.T) { bz, err := tc.i.MarshalJSON() require.Nil(t, err, "Error marshaling Int. tc #%d, err %s", tcnum, err) require.Equal(t, tc.jsonBz, bz, "Marshaled value is different from exported. tc #%d", tcnum) + err = (&i).UnmarshalJSON(bz) require.Nil(t, err, "Error unmarshaling Int. tc #%d, err %s", tcnum, err) require.Equal(t, tc.i, i, "Unmarshaled value is different from exported. tc #%d", tcnum) @@ -390,15 +391,15 @@ func TestSerializationOverflow(t *testing.T) { x := Int{bx} y := new(Int) - xStr, err := x.Marshal() + bz, err := x.Marshal() require.NoError(t, err) // require deserialization to fail due to overflow - err = y.Unmarshal(xStr) + err = y.Unmarshal(bz) require.Error(t, err) // require JSON deserialization to fail due to overflow - bz, err := x.MarshalJSON() + bz, err = x.MarshalJSON() require.NoError(t, err) err = y.UnmarshalJSON(bz) diff --git a/types/uint.go b/types/uint.go index 4d02387270e8..50f0163d2162 100644 --- a/types/uint.go +++ b/types/uint.go @@ -149,7 +149,8 @@ func (u *Uint) MarshalTo(data []byte) (n int, err error) { u.i = new(big.Int) } if len(u.i.Bytes()) == 0 { - return 0, nil + copy(data, []byte{0x30}) + return 1, nil } bz, err := u.Marshal() From 1623ac5b8a2b69db47d0af703bf44d4f176192f8 Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Tue, 31 Dec 2019 16:44:08 -0500 Subject: [PATCH 11/40] Update proto types --- types/coin.go | 13 ++ types/coin_test.go | 35 ++++ types/types.pb.go | 431 ++------------------------------------------- types/types.proto | 19 +- 4 files changed, 62 insertions(+), 436 deletions(-) diff --git a/types/coin.go b/types/coin.go index e9912835c328..9dfb73cb20f8 100644 --- a/types/coin.go +++ b/types/coin.go @@ -6,6 +6,8 @@ import ( "regexp" "sort" "strings" + + yaml "gopkg.in/yaml.v2" ) // NewCoin returns a new coin with a denomination and amount. It will panic if @@ -127,6 +129,17 @@ func (coin Coin) IsNegative() bool { return coin.Amount.Sign() == -1 } +// MarshalYAML implements custom YAML marshaling for the Coin type. It omits all +// proto fields. +func (coin Coin) MarshalYAML() (interface{}, error) { + bz, err := yaml.Marshal(struct { + Denom string `json:"denom,omitempty" yaml:"denom,omitempty"` + Amount Int `json:"amount" yaml:"amount"` + }{coin.Denom, coin.Amount}) + + return string(bz), err +} + //----------------------------------------------------------------------------- // Coins diff --git a/types/coin_test.go b/types/coin_test.go index a3b96f4fb2ca..67cfc2b76a2f 100644 --- a/types/coin_test.go +++ b/types/coin_test.go @@ -1,11 +1,15 @@ package types import ( + "encoding/json" + "fmt" "strings" "testing" + proto "github.com/golang/protobuf/proto" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + yaml "gopkg.in/yaml.v2" "github.com/cosmos/cosmos-sdk/codec" ) @@ -685,3 +689,34 @@ func TestMarshalJSONCoins(t *testing.T) { }) } } + +func TestCoinEncoding(t *testing.T) { + testCases := []struct { + input Coin + rawBz string + jsonStr string + yamlStr string + }{ + {Coin{}, "120130", "{\"amount\":\"0\"}", "|\n amount: \"0\"\n"}, + {Coin{Denom: "test", Amount: NewInt(0)}, "0A0474657374120130", "{\"denom\":\"test\",\"amount\":\"0\"}", "|\n denom: test\n amount: \"0\"\n"}, + {NewCoin("test", NewInt(777)), "0A04746573741203373737", "{\"denom\":\"test\",\"amount\":\"777\"}", "|\n denom: test\n amount: \"777\"\n"}, + } + + for _, tc := range testCases { + bz, err := proto.Marshal(&tc.input) + require.NoError(t, err) + require.Equal(t, tc.rawBz, fmt.Sprintf("%X", bz)) + + var other Coin + require.NoError(t, proto.Unmarshal(bz, &other)) + require.True(t, tc.input.IsEqual(other)) + + bz, err = json.Marshal(tc.input) + require.NoError(t, err) + require.Equal(t, tc.jsonStr, string(bz)) + + bz, err = yaml.Marshal(tc.input) + require.NoError(t, err) + require.Equal(t, tc.yamlStr, string(bz)) + } +} diff --git a/types/types.pb.go b/types/types.pb.go index 8759c70cda02..078dd7c95772 100644 --- a/types/types.pb.go +++ b/types/types.pb.go @@ -10,8 +10,6 @@ import ( io "io" math "math" math_bits "math/bits" - reflect "reflect" - strings "strings" ) // Reference imports to suppress errors if they are not otherwise used. @@ -25,102 +23,6 @@ var _ = math.Inf // proto package needs to be updated. const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package -// ProtoInt defines a big integer that is represented as a string and decorated -// by a custom type Int. -type ProtoInt struct { - I string `protobuf:"bytes,1,opt,name=i,proto3" json:"i,omitempty"` - XXX_NoUnkeyedLiteral struct{} `json:"-"` - XXX_unrecognized []byte `json:"-"` - XXX_sizecache int32 `json:"-"` -} - -func (m *ProtoInt) Reset() { *m = ProtoInt{} } -func (*ProtoInt) ProtoMessage() {} -func (*ProtoInt) Descriptor() ([]byte, []int) { - return fileDescriptor_2c0f90c600ad7e2e, []int{0} -} -func (m *ProtoInt) XXX_Unmarshal(b []byte) error { - return m.Unmarshal(b) -} -func (m *ProtoInt) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { - if deterministic { - return xxx_messageInfo_ProtoInt.Marshal(b, m, deterministic) - } else { - b = b[:cap(b)] - n, err := m.MarshalToSizedBuffer(b) - if err != nil { - return nil, err - } - return b[:n], nil - } -} -func (m *ProtoInt) XXX_Merge(src proto.Message) { - xxx_messageInfo_ProtoInt.Merge(m, src) -} -func (m *ProtoInt) XXX_Size() int { - return m.Size() -} -func (m *ProtoInt) XXX_DiscardUnknown() { - xxx_messageInfo_ProtoInt.DiscardUnknown(m) -} - -var xxx_messageInfo_ProtoInt proto.InternalMessageInfo - -func (m *ProtoInt) GetI() string { - if m != nil { - return m.I - } - return "" -} - -// ProtoUint defines a big unsigned integer that is represented as a string and -// decorated by a custom type Uint. -type ProtoUint struct { - I string `protobuf:"bytes,1,opt,name=i,proto3" json:"i,omitempty"` - XXX_NoUnkeyedLiteral struct{} `json:"-"` - XXX_unrecognized []byte `json:"-"` - XXX_sizecache int32 `json:"-"` -} - -func (m *ProtoUint) Reset() { *m = ProtoUint{} } -func (*ProtoUint) ProtoMessage() {} -func (*ProtoUint) Descriptor() ([]byte, []int) { - return fileDescriptor_2c0f90c600ad7e2e, []int{1} -} -func (m *ProtoUint) XXX_Unmarshal(b []byte) error { - return m.Unmarshal(b) -} -func (m *ProtoUint) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { - if deterministic { - return xxx_messageInfo_ProtoUint.Marshal(b, m, deterministic) - } else { - b = b[:cap(b)] - n, err := m.MarshalToSizedBuffer(b) - if err != nil { - return nil, err - } - return b[:n], nil - } -} -func (m *ProtoUint) XXX_Merge(src proto.Message) { - xxx_messageInfo_ProtoUint.Merge(m, src) -} -func (m *ProtoUint) XXX_Size() int { - return m.Size() -} -func (m *ProtoUint) XXX_DiscardUnknown() { - xxx_messageInfo_ProtoUint.DiscardUnknown(m) -} - -var xxx_messageInfo_ProtoUint proto.InternalMessageInfo - -func (m *ProtoUint) GetI() string { - if m != nil { - return m.I - } - return "" -} - // Coin defines a token with a denomination and an amount. // // NOTE: The amount field is an Int which implements the custom method @@ -136,7 +38,7 @@ type Coin struct { func (m *Coin) Reset() { *m = Coin{} } func (*Coin) ProtoMessage() {} func (*Coin) Descriptor() ([]byte, []int) { - return fileDescriptor_2c0f90c600ad7e2e, []int{2} + return fileDescriptor_2c0f90c600ad7e2e, []int{0} } func (m *Coin) XXX_Unmarshal(b []byte) error { return m.Unmarshal(b) @@ -173,97 +75,24 @@ func (m *Coin) GetDenom() string { } func init() { - proto.RegisterType((*ProtoInt)(nil), "types.ProtoInt") - proto.RegisterType((*ProtoUint)(nil), "types.ProtoUint") proto.RegisterType((*Coin)(nil), "types.Coin") } func init() { proto.RegisterFile("types/types.proto", fileDescriptor_2c0f90c600ad7e2e) } var fileDescriptor_2c0f90c600ad7e2e = []byte{ - // 216 bytes of a gzipped FileDescriptorProto + // 175 bytes of a gzipped FileDescriptorProto 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x12, 0x2c, 0xa9, 0x2c, 0x48, 0x2d, 0xd6, 0x07, 0x93, 0x7a, 0x05, 0x45, 0xf9, 0x25, 0xf9, 0x42, 0xac, 0x60, 0x8e, 0x94, 0x49, 0x59, 0x6a, 0x5e, 0x4a, 0x7e, 0x91, 0x7e, 0x7a, 0x66, 0x49, 0x46, 0x69, 0x92, 0x5e, 0x72, 0x7e, 0xae, 0x7e, 0x7a, 0x7e, 0x7a, 0xbe, 0x3e, 0x58, 0x51, 0x52, 0x69, 0x1a, 0x98, 0x07, 0xe6, 0x80, - 0x59, 0x10, 0xcd, 0x4a, 0x72, 0x5c, 0x1c, 0x01, 0x20, 0x86, 0x67, 0x5e, 0x89, 0x10, 0x0f, 0x17, - 0x63, 0xa6, 0x04, 0xa3, 0x02, 0xa3, 0x06, 0x67, 0x10, 0x63, 0xa6, 0x15, 0x4b, 0xc3, 0x1d, 0x05, - 0x46, 0x25, 0x79, 0x2e, 0x4e, 0xb0, 0x7c, 0x68, 0x26, 0x0e, 0x05, 0x41, 0x5c, 0x2c, 0xce, 0xf9, - 0x99, 0x79, 0x42, 0x22, 0x5c, 0xac, 0x29, 0xa9, 0x79, 0xf9, 0xb9, 0x50, 0x79, 0x08, 0x47, 0xc8, - 0x84, 0x8b, 0x2d, 0x31, 0x37, 0xbf, 0x34, 0xaf, 0x44, 0x82, 0x49, 0x81, 0x51, 0x83, 0xdb, 0x88, - 0x5f, 0x0f, 0xe2, 0x72, 0x98, 0x9d, 0x4e, 0xdc, 0x27, 0xee, 0xc9, 0x33, 0xdc, 0xba, 0x27, 0xcf, - 0xec, 0x99, 0x57, 0x12, 0x04, 0x55, 0xeb, 0x24, 0x7f, 0xe3, 0xa1, 0x1c, 0x43, 0xc3, 0x23, 0x39, - 0x86, 0x13, 0x8f, 0xe4, 0x18, 0x2f, 0x3c, 0x92, 0x63, 0x7c, 0xf0, 0x48, 0x8e, 0x71, 0xc6, 0x63, - 0x39, 0x86, 0x28, 0x88, 0x5f, 0x93, 0xd8, 0xc0, 0x8e, 0x37, 0x06, 0x04, 0x00, 0x00, 0xff, 0xff, - 0xe2, 0x07, 0x0a, 0xee, 0x0e, 0x01, 0x00, 0x00, -} - -func (m *ProtoInt) Marshal() (dAtA []byte, err error) { - size := m.Size() - dAtA = make([]byte, size) - n, err := m.MarshalToSizedBuffer(dAtA[:size]) - if err != nil { - return nil, err - } - return dAtA[:n], nil -} - -func (m *ProtoInt) MarshalTo(dAtA []byte) (int, error) { - size := m.Size() - return m.MarshalToSizedBuffer(dAtA[:size]) -} - -func (m *ProtoInt) MarshalToSizedBuffer(dAtA []byte) (int, error) { - i := len(dAtA) - _ = i - var l int - _ = l - if m.XXX_unrecognized != nil { - i -= len(m.XXX_unrecognized) - copy(dAtA[i:], m.XXX_unrecognized) - } - if len(m.I) > 0 { - i -= len(m.I) - copy(dAtA[i:], m.I) - i = encodeVarintTypes(dAtA, i, uint64(len(m.I))) - i-- - dAtA[i] = 0xa - } - return len(dAtA) - i, nil -} - -func (m *ProtoUint) Marshal() (dAtA []byte, err error) { - size := m.Size() - dAtA = make([]byte, size) - n, err := m.MarshalToSizedBuffer(dAtA[:size]) - if err != nil { - return nil, err - } - return dAtA[:n], nil -} - -func (m *ProtoUint) MarshalTo(dAtA []byte) (int, error) { - size := m.Size() - return m.MarshalToSizedBuffer(dAtA[:size]) -} - -func (m *ProtoUint) MarshalToSizedBuffer(dAtA []byte) (int, error) { - i := len(dAtA) - _ = i - var l int - _ = l - if m.XXX_unrecognized != nil { - i -= len(m.XXX_unrecognized) - copy(dAtA[i:], m.XXX_unrecognized) - } - if len(m.I) > 0 { - i -= len(m.I) - copy(dAtA[i:], m.I) - i = encodeVarintTypes(dAtA, i, uint64(len(m.I))) - i-- - dAtA[i] = 0xa - } - return len(dAtA) - i, nil + 0x59, 0x10, 0xcd, 0x4a, 0x8e, 0x5c, 0x2c, 0xce, 0xf9, 0x99, 0x79, 0x42, 0x22, 0x5c, 0xac, 0x29, + 0xa9, 0x79, 0xf9, 0xb9, 0x12, 0x8c, 0x0a, 0x8c, 0x1a, 0x9c, 0x41, 0x10, 0x8e, 0x90, 0x32, 0x17, + 0x5b, 0x62, 0x6e, 0x7e, 0x69, 0x5e, 0x89, 0x04, 0x93, 0x02, 0xa3, 0x06, 0x8f, 0x13, 0xf7, 0x89, + 0x7b, 0xf2, 0x0c, 0xb7, 0xee, 0xc9, 0x33, 0x7b, 0xe6, 0x95, 0x04, 0x41, 0xa5, 0x9c, 0xe4, 0x6f, + 0x3c, 0x94, 0x63, 0x68, 0x78, 0x24, 0xc7, 0x70, 0xe2, 0x91, 0x1c, 0xe3, 0x85, 0x47, 0x72, 0x8c, + 0x0f, 0x1e, 0xc9, 0x31, 0xce, 0x78, 0x2c, 0xc7, 0x10, 0x05, 0x71, 0x59, 0x12, 0x1b, 0xd8, 0x2a, + 0x63, 0x40, 0x00, 0x00, 0x00, 0xff, 0xff, 0xbd, 0x23, 0x63, 0xd4, 0xbc, 0x00, 0x00, 0x00, } func (m *Coin) Marshal() (dAtA []byte, err error) { @@ -321,38 +150,6 @@ func encodeVarintTypes(dAtA []byte, offset int, v uint64) int { dAtA[offset] = uint8(v) return base } -func (m *ProtoInt) Size() (n int) { - if m == nil { - return 0 - } - var l int - _ = l - l = len(m.I) - if l > 0 { - n += 1 + l + sovTypes(uint64(l)) - } - if m.XXX_unrecognized != nil { - n += len(m.XXX_unrecognized) - } - return n -} - -func (m *ProtoUint) Size() (n int) { - if m == nil { - return 0 - } - var l int - _ = l - l = len(m.I) - if l > 0 { - n += 1 + l + sovTypes(uint64(l)) - } - if m.XXX_unrecognized != nil { - n += len(m.XXX_unrecognized) - } - return n -} - func (m *Coin) Size() (n int) { if m == nil { return 0 @@ -377,208 +174,6 @@ func sovTypes(x uint64) (n int) { func sozTypes(x uint64) (n int) { return sovTypes(uint64((x << 1) ^ uint64((int64(x) >> 63)))) } -func (this *ProtoInt) String() string { - if this == nil { - return "nil" - } - s := strings.Join([]string{`&ProtoInt{`, - `I:` + fmt.Sprintf("%v", this.I) + `,`, - `XXX_unrecognized:` + fmt.Sprintf("%v", this.XXX_unrecognized) + `,`, - `}`, - }, "") - return s -} -func (this *ProtoUint) String() string { - if this == nil { - return "nil" - } - s := strings.Join([]string{`&ProtoUint{`, - `I:` + fmt.Sprintf("%v", this.I) + `,`, - `XXX_unrecognized:` + fmt.Sprintf("%v", this.XXX_unrecognized) + `,`, - `}`, - }, "") - return s -} -func valueToStringTypes(v interface{}) string { - rv := reflect.ValueOf(v) - if rv.IsNil() { - return "nil" - } - pv := reflect.Indirect(rv).Interface() - return fmt.Sprintf("*%v", pv) -} -func (m *ProtoInt) Unmarshal(dAtA []byte) error { - l := len(dAtA) - iNdEx := 0 - for iNdEx < l { - preIndex := iNdEx - var wire uint64 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowTypes - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - wire |= uint64(b&0x7F) << shift - if b < 0x80 { - break - } - } - fieldNum := int32(wire >> 3) - wireType := int(wire & 0x7) - if wireType == 4 { - return fmt.Errorf("proto: ProtoInt: wiretype end group for non-group") - } - if fieldNum <= 0 { - return fmt.Errorf("proto: ProtoInt: illegal tag %d (wire type %d)", fieldNum, wire) - } - switch fieldNum { - case 1: - if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field I", wireType) - } - var stringLen uint64 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowTypes - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - stringLen |= uint64(b&0x7F) << shift - if b < 0x80 { - break - } - } - intStringLen := int(stringLen) - if intStringLen < 0 { - return ErrInvalidLengthTypes - } - postIndex := iNdEx + intStringLen - if postIndex < 0 { - return ErrInvalidLengthTypes - } - if postIndex > l { - return io.ErrUnexpectedEOF - } - m.I = string(dAtA[iNdEx:postIndex]) - iNdEx = postIndex - default: - iNdEx = preIndex - skippy, err := skipTypes(dAtA[iNdEx:]) - if err != nil { - return err - } - if skippy < 0 { - return ErrInvalidLengthTypes - } - if (iNdEx + skippy) < 0 { - return ErrInvalidLengthTypes - } - if (iNdEx + skippy) > l { - return io.ErrUnexpectedEOF - } - m.XXX_unrecognized = append(m.XXX_unrecognized, dAtA[iNdEx:iNdEx+skippy]...) - iNdEx += skippy - } - } - - if iNdEx > l { - return io.ErrUnexpectedEOF - } - return nil -} -func (m *ProtoUint) Unmarshal(dAtA []byte) error { - l := len(dAtA) - iNdEx := 0 - for iNdEx < l { - preIndex := iNdEx - var wire uint64 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowTypes - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - wire |= uint64(b&0x7F) << shift - if b < 0x80 { - break - } - } - fieldNum := int32(wire >> 3) - wireType := int(wire & 0x7) - if wireType == 4 { - return fmt.Errorf("proto: ProtoUint: wiretype end group for non-group") - } - if fieldNum <= 0 { - return fmt.Errorf("proto: ProtoUint: illegal tag %d (wire type %d)", fieldNum, wire) - } - switch fieldNum { - case 1: - if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field I", wireType) - } - var stringLen uint64 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowTypes - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - stringLen |= uint64(b&0x7F) << shift - if b < 0x80 { - break - } - } - intStringLen := int(stringLen) - if intStringLen < 0 { - return ErrInvalidLengthTypes - } - postIndex := iNdEx + intStringLen - if postIndex < 0 { - return ErrInvalidLengthTypes - } - if postIndex > l { - return io.ErrUnexpectedEOF - } - m.I = string(dAtA[iNdEx:postIndex]) - iNdEx = postIndex - default: - iNdEx = preIndex - skippy, err := skipTypes(dAtA[iNdEx:]) - if err != nil { - return err - } - if skippy < 0 { - return ErrInvalidLengthTypes - } - if (iNdEx + skippy) < 0 { - return ErrInvalidLengthTypes - } - if (iNdEx + skippy) > l { - return io.ErrUnexpectedEOF - } - m.XXX_unrecognized = append(m.XXX_unrecognized, dAtA[iNdEx:iNdEx+skippy]...) - iNdEx += skippy - } - } - - if iNdEx > l { - return io.ErrUnexpectedEOF - } - return nil -} func (m *Coin) Unmarshal(dAtA []byte) error { l := len(dAtA) iNdEx := 0 @@ -644,7 +239,7 @@ func (m *Coin) Unmarshal(dAtA []byte) error { if wireType != 2 { return fmt.Errorf("proto: wrong wireType = %d for field Amount", wireType) } - var msglen int + var byteLen int for shift := uint(0); ; shift += 7 { if shift >= 64 { return ErrIntOverflowTypes @@ -654,15 +249,15 @@ func (m *Coin) Unmarshal(dAtA []byte) error { } b := dAtA[iNdEx] iNdEx++ - msglen |= int(b&0x7F) << shift + byteLen |= int(b&0x7F) << shift if b < 0x80 { break } } - if msglen < 0 { + if byteLen < 0 { return ErrInvalidLengthTypes } - postIndex := iNdEx + msglen + postIndex := iNdEx + byteLen if postIndex < 0 { return ErrInvalidLengthTypes } diff --git a/types/types.proto b/types/types.proto index 9b8ce130d49b..81b4dec97d0d 100644 --- a/types/types.proto +++ b/types/types.proto @@ -10,28 +10,11 @@ option (gogoproto.sizer_all) = true; import "vendor/github.com/gogo/protobuf/gogoproto/gogo.proto"; -// ProtoInt defines a big integer that is represented as a string and decorated -// by a custom type Int. -message ProtoInt { - option (gogoproto.stringer) = true; - - string i = 1; -} - -// ProtoUint defines a big unsigned integer that is represented as a string and -// decorated by a custom type Uint. -message ProtoUint { - option (gogoproto.stringer) = true; - - string i = 1; -} - // Coin defines a token with a denomination and an amount. // // NOTE: The amount field is an Int which implements the custom method // signatures required by gogoproto. message Coin { string denom = 1; - ProtoInt amount = 2 - [ (gogoproto.customtype) = "Int", (gogoproto.nullable) = false ]; + bytes amount = 2 [ (gogoproto.customtype) = "Int", (gogoproto.nullable) = false ]; } From 1dca2761db65ec4ef89f8738f19bd23a769acaf9 Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Tue, 31 Dec 2019 17:54:59 -0500 Subject: [PATCH 12/40] Define and enforce custom type interface --- types/int.go | 2 ++ types/proto.go | 15 +++++++++++++++ types/uint.go | 2 ++ 3 files changed, 19 insertions(+) create mode 100644 types/proto.go diff --git a/types/int.go b/types/int.go index 78c00b98607c..790d21383937 100644 --- a/types/int.go +++ b/types/int.go @@ -86,6 +86,8 @@ func unmarshalJSON(i *big.Int, bz []byte) error { return unmarshalText(i, text) } +var _ CustomProtoType = (*Int)(nil) + // Int wraps integer with 256 bit range bound // Checks overflow, underflow and division by zero // Exists in range from -(2^maxBitLen-1) to 2^maxBitLen-1 diff --git a/types/proto.go b/types/proto.go new file mode 100644 index 000000000000..145aa410aa17 --- /dev/null +++ b/types/proto.go @@ -0,0 +1,15 @@ +package types + +// CustomProtoType defines the interface custom gogo proto types must implement +// in order to be used as a "customtype" extension. +// +// ref: https://github.com/gogo/protobuf/blob/master/custom_types.md +type CustomProtoType interface { + Marshal() ([]byte, error) + MarshalTo(data []byte) (n int, err error) + Unmarshal(data []byte) error + Size() int + + MarshalJSON() ([]byte, error) + UnmarshalJSON(data []byte) error +} diff --git a/types/uint.go b/types/uint.go index 50f0163d2162..e5f97cab2667 100644 --- a/types/uint.go +++ b/types/uint.go @@ -49,6 +49,8 @@ func ZeroUint() Uint { return Uint{big.NewInt(0)} } // OneUint returns Uint value with one. func OneUint() Uint { return Uint{big.NewInt(1)} } +var _ CustomProtoType = (*Uint)(nil) + // Uint64 converts Uint to uint64 // Panics if the value is out of range func (u Uint) Uint64() uint64 { From fc5b04903cfebe5dccc65f7c126652431e37775e Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Wed, 1 Jan 2020 16:54:53 -0500 Subject: [PATCH 13/40] Migrate Dec type to proto --- types/coin_test.go | 23 +++- types/dec_coin.go | 27 ++--- types/dec_coin_test.go | 74 +++++++++++-- types/decimal.go | 183 ++++++++++++++++++------------- types/decimal_test.go | 76 ++++++++++--- types/int.go | 56 +++++----- types/int_test.go | 84 ++++++++++++--- types/types.pb.go | 240 ++++++++++++++++++++++++++++++++++++++++- types/types.proto | 5 + 9 files changed, 606 insertions(+), 162 deletions(-) diff --git a/types/coin_test.go b/types/coin_test.go index 67cfc2b76a2f..cb17af6ffa93 100644 --- a/types/coin_test.go +++ b/types/coin_test.go @@ -697,9 +697,24 @@ func TestCoinEncoding(t *testing.T) { jsonStr string yamlStr string }{ - {Coin{}, "120130", "{\"amount\":\"0\"}", "|\n amount: \"0\"\n"}, - {Coin{Denom: "test", Amount: NewInt(0)}, "0A0474657374120130", "{\"denom\":\"test\",\"amount\":\"0\"}", "|\n denom: test\n amount: \"0\"\n"}, - {NewCoin("test", NewInt(777)), "0A04746573741203373737", "{\"denom\":\"test\",\"amount\":\"777\"}", "|\n denom: test\n amount: \"777\"\n"}, + { + Coin{}, + "120130", + "{\"amount\":\"0\"}", + "|\n amount: \"0\"\n", + }, + { + Coin{Denom: "test", Amount: NewInt(0)}, + "0A0474657374120130", + "{\"denom\":\"test\",\"amount\":\"0\"}", + "|\n denom: test\n amount: \"0\"\n", + }, + { + NewCoin("test", NewInt(777)), + "0A04746573741203373737", + "{\"denom\":\"test\",\"amount\":\"777\"}", + "|\n denom: test\n amount: \"777\"\n", + }, } for _, tc := range testCases { @@ -714,6 +729,8 @@ func TestCoinEncoding(t *testing.T) { bz, err = json.Marshal(tc.input) require.NoError(t, err) require.Equal(t, tc.jsonStr, string(bz)) + require.NoError(t, json.Unmarshal(bz, &other)) + require.True(t, tc.input.IsEqual(other)) bz, err = yaml.Marshal(tc.input) require.NoError(t, err) diff --git a/types/dec_coin.go b/types/dec_coin.go index e26a88f8755e..0464c67c0f31 100644 --- a/types/dec_coin.go +++ b/types/dec_coin.go @@ -6,17 +6,9 @@ import ( "strings" "github.com/pkg/errors" + yaml "gopkg.in/yaml.v2" ) -// ---------------------------------------------------------------------------- -// Decimal Coin - -// DecCoin defines a coin which can have additional decimal points -type DecCoin struct { - Denom string `json:"denom"` - Amount Dec `json:"amount"` -} - // NewDecCoin creates a new DecCoin instance from an Int. func NewDecCoin(denom string, amount Int) DecCoin { if err := validate(denom, amount); err != nil { @@ -100,7 +92,7 @@ func (coin DecCoin) Add(coinB DecCoin) DecCoin { if coin.Denom != coinB.Denom { panic(fmt.Sprintf("coin denom different: %v %v\n", coin.Denom, coinB.Denom)) } - return DecCoin{coin.Denom, coin.Amount.Add(coinB.Amount)} + return DecCoin{Denom: coin.Denom, Amount: coin.Amount.Add(coinB.Amount)} } // Sub subtracts amounts of two decimal coins with same denom. @@ -108,7 +100,7 @@ func (coin DecCoin) Sub(coinB DecCoin) DecCoin { if coin.Denom != coinB.Denom { panic(fmt.Sprintf("coin denom different: %v %v\n", coin.Denom, coinB.Denom)) } - res := DecCoin{coin.Denom, coin.Amount.Sub(coinB.Amount)} + res := DecCoin{Denom: coin.Denom, Amount: coin.Amount.Sub(coinB.Amount)} if res.IsNegative() { panic("negative decimal coin amount") } @@ -134,7 +126,7 @@ func (coin DecCoin) IsPositive() bool { // // TODO: Remove once unsigned integers are used. func (coin DecCoin) IsNegative() bool { - return coin.Amount.Sign() == -1 + return coin.Amount.i.Sign() == -1 } // String implements the Stringer interface for DecCoin. It returns a @@ -151,6 +143,17 @@ func (coin DecCoin) IsValid() bool { return !coin.IsNegative() } +// MarshalYAML implements custom YAML marshaling for the DecCoin type. It omits +// all proto fields. +func (coin DecCoin) MarshalYAML() (interface{}, error) { + bz, err := yaml.Marshal(struct { + Denom string `json:"denom,omitempty" yaml:"denom,omitempty"` + Amount Dec `json:"amount" yaml:"amount"` + }{coin.Denom, coin.Amount}) + + return string(bz), err +} + // ---------------------------------------------------------------------------- // Decimal Coins diff --git a/types/dec_coin_test.go b/types/dec_coin_test.go index 9ee113effeeb..145c9f74dc75 100644 --- a/types/dec_coin_test.go +++ b/types/dec_coin_test.go @@ -1,10 +1,14 @@ package types import ( + "encoding/json" + "fmt" "strings" "testing" + proto "github.com/golang/protobuf/proto" "github.com/stretchr/testify/require" + yaml "gopkg.in/yaml.v2" ) func TestNewDecCoin(t *testing.T) { @@ -85,9 +89,9 @@ func TestAddDecCoins(t *testing.T) { inputTwo DecCoins expected DecCoins }{ - {DecCoins{{testDenom1, one}, {testDenom2, one}}, DecCoins{{testDenom1, one}, {testDenom2, one}}, DecCoins{{testDenom1, two}, {testDenom2, two}}}, - {DecCoins{{testDenom1, zero}, {testDenom2, one}}, DecCoins{{testDenom1, zero}, {testDenom2, zero}}, DecCoins{{testDenom2, one}}}, - {DecCoins{{testDenom1, zero}, {testDenom2, zero}}, DecCoins{{testDenom1, zero}, {testDenom2, zero}}, DecCoins(nil)}, + {DecCoins{{Denom: testDenom1, Amount: one}, {Denom: testDenom2, Amount: one}}, DecCoins{{Denom: testDenom1, Amount: one}, {Denom: testDenom2, Amount: one}}, DecCoins{{Denom: testDenom1, Amount: two}, {Denom: testDenom2, Amount: two}}}, + {DecCoins{{Denom: testDenom1, Amount: zero}, {Denom: testDenom2, Amount: one}}, DecCoins{{Denom: testDenom1, Amount: zero}, {Denom: testDenom2, Amount: zero}}, DecCoins{{Denom: testDenom2, Amount: one}}}, + {DecCoins{{Denom: testDenom1, Amount: zero}, {Denom: testDenom2, Amount: zero}}, DecCoins{{Denom: testDenom1, Amount: zero}, {Denom: testDenom2, Amount: zero}}, DecCoins(nil)}, } for tcIndex, tc := range cases { @@ -256,14 +260,14 @@ func TestDecCoinsIsValid(t *testing.T) { expected bool }{ {DecCoins{}, true}, - {DecCoins{DecCoin{testDenom1, NewDec(5)}}, true}, - {DecCoins{DecCoin{testDenom1, NewDec(5)}, DecCoin{testDenom2, NewDec(100000)}}, true}, - {DecCoins{DecCoin{testDenom1, NewDec(-5)}}, false}, - {DecCoins{DecCoin{"AAA", NewDec(5)}}, false}, - {DecCoins{DecCoin{testDenom1, NewDec(5)}, DecCoin{"B", NewDec(100000)}}, false}, - {DecCoins{DecCoin{testDenom1, NewDec(5)}, DecCoin{testDenom2, NewDec(-100000)}}, false}, - {DecCoins{DecCoin{testDenom1, NewDec(-5)}, DecCoin{testDenom2, NewDec(100000)}}, false}, - {DecCoins{DecCoin{"AAA", NewDec(5)}, DecCoin{testDenom2, NewDec(100000)}}, false}, + {DecCoins{DecCoin{Denom: testDenom1, Amount: NewDec(5)}}, true}, + {DecCoins{DecCoin{Denom: testDenom1, Amount: NewDec(5)}, DecCoin{Denom: testDenom2, Amount: NewDec(100000)}}, true}, + {DecCoins{DecCoin{Denom: testDenom1, Amount: NewDec(-5)}}, false}, + {DecCoins{DecCoin{Denom: "AAA", Amount: NewDec(5)}}, false}, + {DecCoins{DecCoin{Denom: testDenom1, Amount: NewDec(5)}, DecCoin{Denom: "B", Amount: NewDec(100000)}}, false}, + {DecCoins{DecCoin{Denom: testDenom1, Amount: NewDec(5)}, DecCoin{Denom: testDenom2, Amount: NewDec(-100000)}}, false}, + {DecCoins{DecCoin{Denom: testDenom1, Amount: NewDec(-5)}, DecCoin{Denom: testDenom2, Amount: NewDec(100000)}}, false}, + {DecCoins{DecCoin{Denom: "AAA", Amount: NewDec(5)}, DecCoin{Denom: testDenom2, Amount: NewDec(100000)}}, false}, } for i, tc := range testCases { @@ -421,3 +425,51 @@ func TestDecCoinsQuoDecTruncate(t *testing.T) { } } } + +func TestDecCoinEncoding(t *testing.T) { + testCases := []struct { + input DecCoin + rawBz string + jsonStr string + yamlStr string + }{ + { + DecCoin{}, + "120130", + "{\"amount\":\"0.000000000000000000\"}", + "|\n amount: \"0.000000000000000000\"\n", + }, + { + DecCoin{Denom: "test", Amount: NewDec(0)}, + "0A0474657374120130", + "{\"denom\":\"test\",\"amount\":\"0.000000000000000000\"}", + "|\n denom: test\n amount: \"0.000000000000000000\"\n", + }, + { + NewDecCoinFromDec("test", NewDecWithPrec(4, 2)), + "0A047465737412113430303030303030303030303030303030", + "{\"denom\":\"test\",\"amount\":\"0.040000000000000000\"}", + "|\n denom: test\n amount: \"0.040000000000000000\"\n", + }, + } + + for _, tc := range testCases { + bz, err := proto.Marshal(&tc.input) + require.NoError(t, err) + require.Equal(t, tc.rawBz, fmt.Sprintf("%X", bz)) + + var other DecCoin + require.NoError(t, proto.Unmarshal(bz, &other)) + require.True(t, tc.input.IsEqual(other)) + + bz, err = json.Marshal(tc.input) + require.NoError(t, err) + require.Equal(t, tc.jsonStr, string(bz)) + require.NoError(t, json.Unmarshal(bz, &other)) + require.True(t, tc.input.IsEqual(other)) + + bz, err = yaml.Marshal(tc.input) + require.NoError(t, err) + require.Equal(t, tc.yamlStr, string(bz)) + } +} diff --git a/types/decimal.go b/types/decimal.go index 63aecf6cab18..4939c3ff67b6 100644 --- a/types/decimal.go +++ b/types/decimal.go @@ -10,10 +10,12 @@ import ( "testing" ) +var _ CustomProtoType = (*Dec)(nil) + // NOTE: never use new(Dec) or else we will panic unmarshalling into the // nil embedded big.Int type Dec struct { - *big.Int `json:"int"` + i *big.Int `json:"int"` } // number of decimal places @@ -193,21 +195,21 @@ func MustNewDecFromStr(s string) Dec { //______________________________________________________________________________________________ //nolint -func (d Dec) IsNil() bool { return d.Int == nil } // is decimal nil -func (d Dec) IsZero() bool { return (d.Int).Sign() == 0 } // is equal to zero -func (d Dec) IsNegative() bool { return (d.Int).Sign() == -1 } // is negative -func (d Dec) IsPositive() bool { return (d.Int).Sign() == 1 } // is positive -func (d Dec) Equal(d2 Dec) bool { return (d.Int).Cmp(d2.Int) == 0 } // equal decimals -func (d Dec) GT(d2 Dec) bool { return (d.Int).Cmp(d2.Int) > 0 } // greater than -func (d Dec) GTE(d2 Dec) bool { return (d.Int).Cmp(d2.Int) >= 0 } // greater than or equal -func (d Dec) LT(d2 Dec) bool { return (d.Int).Cmp(d2.Int) < 0 } // less than -func (d Dec) LTE(d2 Dec) bool { return (d.Int).Cmp(d2.Int) <= 0 } // less than or equal -func (d Dec) Neg() Dec { return Dec{new(big.Int).Neg(d.Int)} } // reverse the decimal sign -func (d Dec) Abs() Dec { return Dec{new(big.Int).Abs(d.Int)} } // absolute value +func (d Dec) IsNil() bool { return d.i == nil } // is decimal nil +func (d Dec) IsZero() bool { return (d.i).Sign() == 0 } // is equal to zero +func (d Dec) IsNegative() bool { return (d.i).Sign() == -1 } // is negative +func (d Dec) IsPositive() bool { return (d.i).Sign() == 1 } // is positive +func (d Dec) Equal(d2 Dec) bool { return (d.i).Cmp(d2.i) == 0 } // equal decimals +func (d Dec) GT(d2 Dec) bool { return (d.i).Cmp(d2.i) > 0 } // greater than +func (d Dec) GTE(d2 Dec) bool { return (d.i).Cmp(d2.i) >= 0 } // greater than or equal +func (d Dec) LT(d2 Dec) bool { return (d.i).Cmp(d2.i) < 0 } // less than +func (d Dec) LTE(d2 Dec) bool { return (d.i).Cmp(d2.i) <= 0 } // less than or equal +func (d Dec) Neg() Dec { return Dec{new(big.Int).Neg(d.i)} } // reverse the decimal sign +func (d Dec) Abs() Dec { return Dec{new(big.Int).Abs(d.i)} } // absolute value // addition func (d Dec) Add(d2 Dec) Dec { - res := new(big.Int).Add(d.Int, d2.Int) + res := new(big.Int).Add(d.i, d2.i) if res.BitLen() > 255+DecimalPrecisionBits { panic("Int overflow") @@ -217,7 +219,7 @@ func (d Dec) Add(d2 Dec) Dec { // subtraction func (d Dec) Sub(d2 Dec) Dec { - res := new(big.Int).Sub(d.Int, d2.Int) + res := new(big.Int).Sub(d.i, d2.i) if res.BitLen() > 255+DecimalPrecisionBits { panic("Int overflow") @@ -227,7 +229,7 @@ func (d Dec) Sub(d2 Dec) Dec { // multiplication func (d Dec) Mul(d2 Dec) Dec { - mul := new(big.Int).Mul(d.Int, d2.Int) + mul := new(big.Int).Mul(d.i, d2.i) chopped := chopPrecisionAndRound(mul) if chopped.BitLen() > 255+DecimalPrecisionBits { @@ -238,7 +240,7 @@ func (d Dec) Mul(d2 Dec) Dec { // multiplication truncate func (d Dec) MulTruncate(d2 Dec) Dec { - mul := new(big.Int).Mul(d.Int, d2.Int) + mul := new(big.Int).Mul(d.i, d2.i) chopped := chopPrecisionAndTruncate(mul) if chopped.BitLen() > 255+DecimalPrecisionBits { @@ -249,7 +251,7 @@ func (d Dec) MulTruncate(d2 Dec) Dec { // multiplication func (d Dec) MulInt(i Int) Dec { - mul := new(big.Int).Mul(d.Int, i.i) + mul := new(big.Int).Mul(d.i, i.i) if mul.BitLen() > 255+DecimalPrecisionBits { panic("Int overflow") @@ -259,7 +261,7 @@ func (d Dec) MulInt(i Int) Dec { // MulInt64 - multiplication with int64 func (d Dec) MulInt64(i int64) Dec { - mul := new(big.Int).Mul(d.Int, big.NewInt(i)) + mul := new(big.Int).Mul(d.i, big.NewInt(i)) if mul.BitLen() > 255+DecimalPrecisionBits { panic("Int overflow") @@ -271,10 +273,10 @@ func (d Dec) MulInt64(i int64) Dec { func (d Dec) Quo(d2 Dec) Dec { // multiply precision twice - mul := new(big.Int).Mul(d.Int, precisionReuse) + mul := new(big.Int).Mul(d.i, precisionReuse) mul.Mul(mul, precisionReuse) - quo := new(big.Int).Quo(mul, d2.Int) + quo := new(big.Int).Quo(mul, d2.i) chopped := chopPrecisionAndRound(quo) if chopped.BitLen() > 255+DecimalPrecisionBits { @@ -287,10 +289,10 @@ func (d Dec) Quo(d2 Dec) Dec { func (d Dec) QuoTruncate(d2 Dec) Dec { // multiply precision twice - mul := new(big.Int).Mul(d.Int, precisionReuse) + mul := new(big.Int).Mul(d.i, precisionReuse) mul.Mul(mul, precisionReuse) - quo := new(big.Int).Quo(mul, d2.Int) + quo := new(big.Int).Quo(mul, d2.i) chopped := chopPrecisionAndTruncate(quo) if chopped.BitLen() > 255+DecimalPrecisionBits { @@ -302,10 +304,10 @@ func (d Dec) QuoTruncate(d2 Dec) Dec { // quotient, round up func (d Dec) QuoRoundUp(d2 Dec) Dec { // multiply precision twice - mul := new(big.Int).Mul(d.Int, precisionReuse) + mul := new(big.Int).Mul(d.i, precisionReuse) mul.Mul(mul, precisionReuse) - quo := new(big.Int).Quo(mul, d2.Int) + quo := new(big.Int).Quo(mul, d2.i) chopped := chopPrecisionAndRoundUp(quo) if chopped.BitLen() > 255+DecimalPrecisionBits { @@ -316,13 +318,13 @@ func (d Dec) QuoRoundUp(d2 Dec) Dec { // quotient func (d Dec) QuoInt(i Int) Dec { - mul := new(big.Int).Quo(d.Int, i.i) + mul := new(big.Int).Quo(d.i, i.i) return Dec{mul} } // QuoInt64 - quotient with int64 func (d Dec) QuoInt64(i int64) Dec { - mul := new(big.Int).Quo(d.Int, big.NewInt(i)) + mul := new(big.Int).Quo(d.i, big.NewInt(i)) return Dec{mul} } @@ -354,7 +356,7 @@ func (d Dec) ApproxSqrt() Dec { // is integer, e.g. decimals are zero func (d Dec) IsInteger() bool { - return new(big.Int).Rem(d.Int, precisionReuse).Sign() == 0 + return new(big.Int).Rem(d.i, precisionReuse).Sign() == 0 } // format decimal state @@ -366,8 +368,8 @@ func (d Dec) Format(s fmt.State, verb rune) { } func (d Dec) String() string { - if d.Int == nil { - return d.Int.String() + if d.i == nil { + return d.i.String() } isNeg := d.IsNegative() @@ -375,7 +377,7 @@ func (d Dec) String() string { d = d.Neg() } - bzInt, err := d.Int.MarshalText() + bzInt, err := d.i.MarshalText() if err != nil { return "" } @@ -494,7 +496,7 @@ func chopPrecisionAndRoundNonMutative(d *big.Int) *big.Int { // RoundInt64 rounds the decimal using bankers rounding func (d Dec) RoundInt64() int64 { - chopped := chopPrecisionAndRoundNonMutative(d.Int) + chopped := chopPrecisionAndRoundNonMutative(d.i) if !chopped.IsInt64() { panic("Int64() out of bound") } @@ -503,7 +505,7 @@ func (d Dec) RoundInt64() int64 { // RoundInt round the decimal using bankers rounding func (d Dec) RoundInt() Int { - return NewIntFromBigInt(chopPrecisionAndRoundNonMutative(d.Int)) + return NewIntFromBigInt(chopPrecisionAndRoundNonMutative(d.i)) } //___________________________________________________________________________________ @@ -520,7 +522,7 @@ func chopPrecisionAndTruncateNonMutative(d *big.Int) *big.Int { // TruncateInt64 truncates the decimals from the number and returns an int64 func (d Dec) TruncateInt64() int64 { - chopped := chopPrecisionAndTruncateNonMutative(d.Int) + chopped := chopPrecisionAndTruncateNonMutative(d.i) if !chopped.IsInt64() { panic("Int64() out of bound") } @@ -529,18 +531,18 @@ func (d Dec) TruncateInt64() int64 { // TruncateInt truncates the decimals from the number and returns an Int func (d Dec) TruncateInt() Int { - return NewIntFromBigInt(chopPrecisionAndTruncateNonMutative(d.Int)) + return NewIntFromBigInt(chopPrecisionAndTruncateNonMutative(d.i)) } // TruncateDec truncates the decimals from the number and returns a Dec func (d Dec) TruncateDec() Dec { - return NewDecFromBigInt(chopPrecisionAndTruncateNonMutative(d.Int)) + return NewDecFromBigInt(chopPrecisionAndTruncateNonMutative(d.i)) } // Ceil returns the smallest interger value (as a decimal) that is greater than // or equal to the given decimal. func (d Dec) Ceil() Dec { - tmp := new(big.Int).Set(d.Int) + tmp := new(big.Int).Set(d.i) quo, rem := tmp, big.NewInt(0) quo, rem = quo.QuoRem(tmp, precisionReuse, rem) @@ -596,58 +598,26 @@ func SortableDecBytes(dec Dec) []byte { //___________________________________________________________________________________ // reuse nil values -var ( - nilAmino string - nilJSON []byte -) +var nilJSON []byte func init() { empty := new(big.Int) - bz, err := empty.MarshalText() - if err != nil { - panic("bad nil amino init") - } - nilAmino = string(bz) - - nilJSON, err = json.Marshal(string(bz)) - if err != nil { - panic("bad nil json init") - } -} - -// wraps d.MarshalText() -func (d Dec) MarshalAmino() (string, error) { - if d.Int == nil { - return nilAmino, nil - } - bz, err := d.Int.MarshalText() - return string(bz), err -} - -// requires a valid JSON string - strings quotes and calls UnmarshalText -func (d *Dec) UnmarshalAmino(text string) (err error) { - tempInt := new(big.Int) - err = tempInt.UnmarshalText([]byte(text)) - if err != nil { - return err - } - d.Int = tempInt - return nil + bz, _ := empty.MarshalText() + nilJSON, _ = json.Marshal(string(bz)) } // MarshalJSON marshals the decimal func (d Dec) MarshalJSON() ([]byte, error) { - if d.Int == nil { + if d.i == nil { return nilJSON, nil } - return json.Marshal(d.String()) } // UnmarshalJSON defines custom decoding scheme func (d *Dec) UnmarshalJSON(bz []byte) error { - if d.Int == nil { - d.Int = new(big.Int) + if d.i == nil { + d.i = new(big.Int) } var text string @@ -655,17 +625,76 @@ func (d *Dec) UnmarshalJSON(bz []byte) error { if err != nil { return err } + // TODO: Reuse dec allocation newDec, err := NewDecFromStr(text) if err != nil { return err } - d.Int = newDec.Int + + d.i = newDec.i + return nil +} + +// MarshalYAML returns the YAML representation. +func (d Dec) MarshalYAML() (interface{}, error) { + return d.String(), nil +} + +// Marshal implements the gogo proto custom type interface. +func (d Dec) Marshal() ([]byte, error) { + if d.i == nil { + d.i = new(big.Int) + } + return d.i.MarshalText() +} + +// MarshalTo implements the gogo proto custom type interface. +func (d *Dec) MarshalTo(data []byte) (n int, err error) { + if d.i == nil { + d.i = new(big.Int) + } + if len(d.i.Bytes()) == 0 { + copy(data, []byte{0x30}) + return 1, nil + } + + bz, err := d.Marshal() + if err != nil { + return 0, err + } + + copy(data, bz) + return len(bz), nil +} + +// Unmarshal implements the gogo proto custom type interface. +func (d *Dec) Unmarshal(data []byte) error { + if len(data) == 0 { + d = nil + return nil + } + + if d.i == nil { + d.i = new(big.Int) + } + + if err := d.i.UnmarshalText(data); err != nil { + return err + } + + if d.i.BitLen() > maxBitLen { + return fmt.Errorf("decimal out of range; got: %d, max: %d", d.i.BitLen(), maxBitLen) + } + return nil } -// MarshalYAML returns Ythe AML representation. -func (d Dec) MarshalYAML() (interface{}, error) { return d.String(), nil } +// Size implements the gogo proto custom type interface. +func (d *Dec) Size() int { + bz, _ := d.Marshal() + return len(bz) +} //___________________________________________________________________________________ // helpers diff --git a/types/decimal_test.go b/types/decimal_test.go index ea97a1029a5a..7e5ed319e8c3 100644 --- a/types/decimal_test.go +++ b/types/decimal_test.go @@ -1,10 +1,13 @@ package types import ( + "encoding/json" + "fmt" "math/big" "testing" "github.com/stretchr/testify/assert" + yaml "gopkg.in/yaml.v2" "github.com/stretchr/testify/require" @@ -273,7 +276,7 @@ var cdc = codec.New() func TestDecMarshalJSON(t *testing.T) { decimal := func(i int64) Dec { d := NewDec(0) - d.Int = new(big.Int).SetInt64(i) + d.i = new(big.Int).SetInt64(i) return d } tests := []struct { @@ -317,18 +320,6 @@ func TestZeroDeserializationJSON(t *testing.T) { require.NotNil(t, err) } -func TestSerializationText(t *testing.T) { - d := mustNewDecFromStr(t, "0.333") - - bz, err := d.MarshalText() - require.NoError(t, err) - - d2 := Dec{new(big.Int)} - err = d2.UnmarshalText(bz) - require.NoError(t, err) - require.True(t, d.Equal(d2), "original: %v, unmarshalled: %v", d, d2) -} - func TestSerializationGocodecJSON(t *testing.T) { d := mustNewDecFromStr(t, "0.333") @@ -468,3 +459,62 @@ func TestDecSortableBytes(t *testing.T) { assert.Panics(t, func() { SortableDecBytes(NewDec(1000000000000000001)) }) assert.Panics(t, func() { SortableDecBytes(NewDec(-1000000000000000001)) }) } + +func TestDecEncoding(t *testing.T) { + testCases := []struct { + input Dec + rawBz string + jsonStr string + yamlStr string + }{ + { + NewDec(0), "30", + "\"0.000000000000000000\"", + "\"0.000000000000000000\"\n", + }, + { + NewDecWithPrec(4, 2), + "3430303030303030303030303030303030", + "\"0.040000000000000000\"", + "\"0.040000000000000000\"\n", + }, + { + NewDecWithPrec(-4, 2), + "2D3430303030303030303030303030303030", + "\"-0.040000000000000000\"", + "\"-0.040000000000000000\"\n", + }, + { + NewDecWithPrec(1414213562373095049, 18), + "31343134323133353632333733303935303439", + "\"1.414213562373095049\"", + "\"1.414213562373095049\"\n", + }, + { + NewDecWithPrec(-1414213562373095049, 18), + "2D31343134323133353632333733303935303439", + "\"-1.414213562373095049\"", + "\"-1.414213562373095049\"\n", + }, + } + + for _, tc := range testCases { + bz, err := tc.input.Marshal() + require.NoError(t, err) + require.Equal(t, tc.rawBz, fmt.Sprintf("%X", bz)) + + var other Dec + require.NoError(t, (&other).Unmarshal(bz)) + require.True(t, tc.input.Equal(other)) + + bz, err = json.Marshal(tc.input) + require.NoError(t, err) + require.Equal(t, tc.jsonStr, string(bz)) + require.NoError(t, json.Unmarshal(bz, &other)) + require.True(t, tc.input.Equal(other)) + + bz, err = yaml.Marshal(tc.input) + require.NoError(t, err) + require.Equal(t, tc.yamlStr, string(bz)) + } +} diff --git a/types/int.go b/types/int.go index 790d21383937..91dc039f7ff6 100644 --- a/types/int.go +++ b/types/int.go @@ -64,28 +64,6 @@ func unmarshalText(i *big.Int, text string) error { return nil } -// MarshalJSON for custom encoding scheme -// Must be encoded as a string for JSON precision -func marshalJSON(i *big.Int) ([]byte, error) { - text, err := i.MarshalText() - if err != nil { - return nil, err - } - return json.Marshal(string(text)) -} - -// UnmarshalJSON for custom decoding scheme -// Must be encoded as a string for JSON precision -func unmarshalJSON(i *big.Int, bz []byte) error { - var text string - err := json.Unmarshal(bz, &text) - if err != nil { - return err - } - - return unmarshalText(i, text) -} - var _ CustomProtoType = (*Int)(nil) // Int wraps integer with 256 bit range bound @@ -327,12 +305,31 @@ func (i *Int) UnmarshalJSON(bz []byte) error { return unmarshalJSON(i.i, bz) } -// MarshalYAML returns the YAML representation. -func (i Int) MarshalYAML() (interface{}, error) { return i.String(), nil } +// MarshalJSON for custom encoding scheme +// Must be encoded as a string for JSON precision +func marshalJSON(i *big.Int) ([]byte, error) { + text, err := i.MarshalText() + if err != nil { + return nil, err + } -// intended to be used with require/assert: require.True(IntEq(...)) -func IntEq(t *testing.T, exp, got Int) (*testing.T, bool, string, string, string) { - return t, exp.Equal(got), "expected:\t%v\ngot:\t\t%v", exp.String(), got.String() + return json.Marshal(string(text)) +} + +// UnmarshalJSON for custom decoding scheme +// Must be encoded as a string for JSON precision +func unmarshalJSON(i *big.Int, bz []byte) error { + var text string + if err := json.Unmarshal(bz, &text); err != nil { + return err + } + + return unmarshalText(i, text) +} + +// MarshalYAML returns the YAML representation. +func (i Int) MarshalYAML() (interface{}, error) { + return i.String(), nil } // Marshal implements the gogo proto custom type interface. @@ -394,3 +391,8 @@ func (i *Int) Size() int { func (i Int) Compare(other Int) int { return i.i.Cmp(other.i) } + +// intended to be used with require/assert: require.True(IntEq(...)) +func IntEq(t *testing.T, exp, got Int) (*testing.T, bool, string, string, string) { + return t, exp.Equal(got), "expected:\t%v\ngot:\t\t%v", exp.String(), got.String() +} diff --git a/types/int_test.go b/types/int_test.go index 01ec6ed7d590..91f138022027 100644 --- a/types/int_test.go +++ b/types/int_test.go @@ -322,15 +322,51 @@ func TestEncodingTableInt(t *testing.T) { jsonBz []byte rawBz []byte }{ - {NewInt(0), []byte("\"0\""), []byte{0x30}}, - {NewInt(100), []byte("\"100\""), []byte{0x31, 0x30, 0x30}}, - {NewInt(-100), []byte("\"-100\""), []byte{0x2d, 0x31, 0x30, 0x30}}, - {NewInt(51842), []byte("\"51842\""), []byte{0x35, 0x31, 0x38, 0x34, 0x32}}, - {NewInt(-51842), []byte("\"-51842\""), []byte{0x2d, 0x35, 0x31, 0x38, 0x34, 0x32}}, - {NewInt(19513368), []byte("\"19513368\""), []byte{0x31, 0x39, 0x35, 0x31, 0x33, 0x33, 0x36, 0x38}}, - {NewInt(-19513368), []byte("\"-19513368\""), []byte{0x2d, 0x31, 0x39, 0x35, 0x31, 0x33, 0x33, 0x36, 0x38}}, - {NewInt(999999999999), []byte("\"999999999999\""), []byte{0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39}}, - {NewInt(-999999999999), []byte("\"-999999999999\""), []byte{0x2d, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39}}, + { + NewInt(0), + []byte("\"0\""), + []byte{0x30}, + }, + { + NewInt(100), + []byte("\"100\""), + []byte{0x31, 0x30, 0x30}, + }, + { + NewInt(-100), + []byte("\"-100\""), + []byte{0x2d, 0x31, 0x30, 0x30}, + }, + { + NewInt(51842), + []byte("\"51842\""), + []byte{0x35, 0x31, 0x38, 0x34, 0x32}, + }, + { + NewInt(-51842), + []byte("\"-51842\""), + []byte{0x2d, 0x35, 0x31, 0x38, 0x34, 0x32}, + }, + { + NewInt(19513368), + []byte("\"19513368\""), + []byte{0x31, 0x39, 0x35, 0x31, 0x33, 0x33, 0x36, 0x38}, + }, + { + NewInt(-19513368), + []byte("\"-19513368\""), + []byte{0x2d, 0x31, 0x39, 0x35, 0x31, 0x33, 0x33, 0x36, 0x38}, + }, + { + NewInt(999999999999), + []byte("\"999999999999\""), + []byte{0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39}, + }, + { + NewInt(-999999999999), + []byte("\"-999999999999\""), + []byte{0x2d, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39}, + }, } for tcnum, tc := range cases { @@ -360,11 +396,31 @@ func TestEncodingTableUint(t *testing.T) { jsonBz []byte rawBz []byte }{ - {NewUint(0), []byte("\"0\""), []byte{0x30}}, - {NewUint(100), []byte("\"100\""), []byte{0x31, 0x30, 0x30}}, - {NewUint(51842), []byte("\"51842\""), []byte{0x35, 0x31, 0x38, 0x34, 0x32}}, - {NewUint(19513368), []byte("\"19513368\""), []byte{0x31, 0x39, 0x35, 0x31, 0x33, 0x33, 0x36, 0x38}}, - {NewUint(999999999999), []byte("\"999999999999\""), []byte{0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39}}, + { + NewUint(0), + []byte("\"0\""), + []byte{0x30}, + }, + { + NewUint(100), + []byte("\"100\""), + []byte{0x31, 0x30, 0x30}, + }, + { + NewUint(51842), + []byte("\"51842\""), + []byte{0x35, 0x31, 0x38, 0x34, 0x32}, + }, + { + NewUint(19513368), + []byte("\"19513368\""), + []byte{0x31, 0x39, 0x35, 0x31, 0x33, 0x33, 0x36, 0x38}, + }, + { + NewUint(999999999999), + []byte("\"999999999999\""), + []byte{0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39}, + }, } for tcnum, tc := range cases { diff --git a/types/types.pb.go b/types/types.pb.go index 078dd7c95772..c9003365ee86 100644 --- a/types/types.pb.go +++ b/types/types.pb.go @@ -74,14 +74,62 @@ func (m *Coin) GetDenom() string { return "" } +type DecCoin struct { + Denom string `protobuf:"bytes,1,opt,name=denom,proto3" json:"denom,omitempty"` + Amount Dec `protobuf:"bytes,2,opt,name=amount,proto3,customtype=Dec" json:"amount"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` +} + +func (m *DecCoin) Reset() { *m = DecCoin{} } +func (*DecCoin) ProtoMessage() {} +func (*DecCoin) Descriptor() ([]byte, []int) { + return fileDescriptor_2c0f90c600ad7e2e, []int{1} +} +func (m *DecCoin) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *DecCoin) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_DecCoin.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalToSizedBuffer(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (m *DecCoin) XXX_Merge(src proto.Message) { + xxx_messageInfo_DecCoin.Merge(m, src) +} +func (m *DecCoin) XXX_Size() int { + return m.Size() +} +func (m *DecCoin) XXX_DiscardUnknown() { + xxx_messageInfo_DecCoin.DiscardUnknown(m) +} + +var xxx_messageInfo_DecCoin proto.InternalMessageInfo + +func (m *DecCoin) GetDenom() string { + if m != nil { + return m.Denom + } + return "" +} + func init() { proto.RegisterType((*Coin)(nil), "types.Coin") + proto.RegisterType((*DecCoin)(nil), "types.DecCoin") } func init() { proto.RegisterFile("types/types.proto", fileDescriptor_2c0f90c600ad7e2e) } var fileDescriptor_2c0f90c600ad7e2e = []byte{ - // 175 bytes of a gzipped FileDescriptorProto + // 189 bytes of a gzipped FileDescriptorProto 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x12, 0x2c, 0xa9, 0x2c, 0x48, 0x2d, 0xd6, 0x07, 0x93, 0x7a, 0x05, 0x45, 0xf9, 0x25, 0xf9, 0x42, 0xac, 0x60, 0x8e, 0x94, 0x49, 0x59, 0x6a, 0x5e, 0x4a, 0x7e, 0x91, 0x7e, 0x7a, 0x66, 0x49, 0x46, 0x69, 0x92, 0x5e, 0x72, 0x7e, @@ -89,10 +137,11 @@ var fileDescriptor_2c0f90c600ad7e2e = []byte{ 0x59, 0x10, 0xcd, 0x4a, 0x8e, 0x5c, 0x2c, 0xce, 0xf9, 0x99, 0x79, 0x42, 0x22, 0x5c, 0xac, 0x29, 0xa9, 0x79, 0xf9, 0xb9, 0x12, 0x8c, 0x0a, 0x8c, 0x1a, 0x9c, 0x41, 0x10, 0x8e, 0x90, 0x32, 0x17, 0x5b, 0x62, 0x6e, 0x7e, 0x69, 0x5e, 0x89, 0x04, 0x93, 0x02, 0xa3, 0x06, 0x8f, 0x13, 0xf7, 0x89, - 0x7b, 0xf2, 0x0c, 0xb7, 0xee, 0xc9, 0x33, 0x7b, 0xe6, 0x95, 0x04, 0x41, 0xa5, 0x9c, 0xe4, 0x6f, - 0x3c, 0x94, 0x63, 0x68, 0x78, 0x24, 0xc7, 0x70, 0xe2, 0x91, 0x1c, 0xe3, 0x85, 0x47, 0x72, 0x8c, - 0x0f, 0x1e, 0xc9, 0x31, 0xce, 0x78, 0x2c, 0xc7, 0x10, 0x05, 0x71, 0x59, 0x12, 0x1b, 0xd8, 0x2a, - 0x63, 0x40, 0x00, 0x00, 0x00, 0xff, 0xff, 0xbd, 0x23, 0x63, 0xd4, 0xbc, 0x00, 0x00, 0x00, + 0x7b, 0xf2, 0x0c, 0xb7, 0xee, 0xc9, 0x33, 0x7b, 0xe6, 0x95, 0x04, 0x41, 0xa5, 0x94, 0x5c, 0xb8, + 0xd8, 0x5d, 0x52, 0x93, 0xc9, 0x31, 0xc5, 0x25, 0x35, 0x19, 0x66, 0x8a, 0x93, 0xfc, 0x8d, 0x87, + 0x72, 0x0c, 0x0d, 0x8f, 0xe4, 0x18, 0x4e, 0x3c, 0x92, 0x63, 0xbc, 0xf0, 0x48, 0x8e, 0xf1, 0xc1, + 0x23, 0x39, 0xc6, 0x19, 0x8f, 0xe5, 0x18, 0xa2, 0x20, 0xfe, 0x4b, 0x62, 0x03, 0x3b, 0xd8, 0x18, + 0x10, 0x00, 0x00, 0xff, 0xff, 0x30, 0x54, 0xad, 0x0c, 0x02, 0x01, 0x00, 0x00, } func (m *Coin) Marshal() (dAtA []byte, err error) { @@ -139,6 +188,50 @@ func (m *Coin) MarshalToSizedBuffer(dAtA []byte) (int, error) { return len(dAtA) - i, nil } +func (m *DecCoin) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalToSizedBuffer(dAtA[:size]) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *DecCoin) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *DecCoin) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + if m.XXX_unrecognized != nil { + i -= len(m.XXX_unrecognized) + copy(dAtA[i:], m.XXX_unrecognized) + } + { + size := m.Amount.Size() + i -= size + if _, err := m.Amount.MarshalTo(dAtA[i:]); err != nil { + return 0, err + } + i = encodeVarintTypes(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0x12 + if len(m.Denom) > 0 { + i -= len(m.Denom) + copy(dAtA[i:], m.Denom) + i = encodeVarintTypes(dAtA, i, uint64(len(m.Denom))) + i-- + dAtA[i] = 0xa + } + return len(dAtA) - i, nil +} + func encodeVarintTypes(dAtA []byte, offset int, v uint64) int { offset -= sovTypes(v) base := offset @@ -168,6 +261,24 @@ func (m *Coin) Size() (n int) { return n } +func (m *DecCoin) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + l = len(m.Denom) + if l > 0 { + n += 1 + l + sovTypes(uint64(l)) + } + l = m.Amount.Size() + n += 1 + l + sovTypes(uint64(l)) + if m.XXX_unrecognized != nil { + n += len(m.XXX_unrecognized) + } + return n +} + func sovTypes(x uint64) (n int) { return (math_bits.Len64(x|1) + 6) / 7 } @@ -293,6 +404,125 @@ func (m *Coin) Unmarshal(dAtA []byte) error { } return nil } +func (m *DecCoin) Unmarshal(dAtA []byte) error { + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTypes + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: DecCoin: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: DecCoin: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Denom", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTypes + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthTypes + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthTypes + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.Denom = string(dAtA[iNdEx:postIndex]) + iNdEx = postIndex + case 2: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Amount", wireType) + } + var byteLen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTypes + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + byteLen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if byteLen < 0 { + return ErrInvalidLengthTypes + } + postIndex := iNdEx + byteLen + if postIndex < 0 { + return ErrInvalidLengthTypes + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + if err := m.Amount.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + default: + iNdEx = preIndex + skippy, err := skipTypes(dAtA[iNdEx:]) + if err != nil { + return err + } + if skippy < 0 { + return ErrInvalidLengthTypes + } + if (iNdEx + skippy) < 0 { + return ErrInvalidLengthTypes + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + m.XXX_unrecognized = append(m.XXX_unrecognized, dAtA[iNdEx:iNdEx+skippy]...) + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} func skipTypes(dAtA []byte) (n int, err error) { l := len(dAtA) iNdEx := 0 diff --git a/types/types.proto b/types/types.proto index 81b4dec97d0d..629c10ffad3f 100644 --- a/types/types.proto +++ b/types/types.proto @@ -18,3 +18,8 @@ message Coin { string denom = 1; bytes amount = 2 [ (gogoproto.customtype) = "Int", (gogoproto.nullable) = false ]; } + +message DecCoin { + string denom = 1; + bytes amount = 2 [ (gogoproto.customtype) = "Dec", (gogoproto.nullable) = false ]; +} From 3bc73b2088dc6ff4a8b7468733c622f10b5c7d89 Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Wed, 1 Jan 2020 16:59:20 -0500 Subject: [PATCH 14/40] Proto lint --- types/types.pb.go | 4 ++++ types/types.proto | 9 +++++++++ 2 files changed, 13 insertions(+) diff --git a/types/types.pb.go b/types/types.pb.go index c9003365ee86..5899333b0f8d 100644 --- a/types/types.pb.go +++ b/types/types.pb.go @@ -74,6 +74,10 @@ func (m *Coin) GetDenom() string { return "" } +// DecCoin defines a token with a denomination and a decimal amount. +// +// NOTE: The amount field is an Dec which implements the custom method +// signatures required by gogoproto. type DecCoin struct { Denom string `protobuf:"bytes,1,opt,name=denom,proto3" json:"denom,omitempty"` Amount Dec `protobuf:"bytes,2,opt,name=amount,proto3,customtype=Dec" json:"amount"` diff --git a/types/types.proto b/types/types.proto index 629c10ffad3f..71f7dbb7c67b 100644 --- a/types/types.proto +++ b/types/types.proto @@ -10,6 +10,11 @@ option (gogoproto.sizer_all) = true; import "vendor/github.com/gogo/protobuf/gogoproto/gogo.proto"; +// TODO: +// +// 2. Msg +// 3. Tx + // Coin defines a token with a denomination and an amount. // // NOTE: The amount field is an Int which implements the custom method @@ -19,6 +24,10 @@ message Coin { bytes amount = 2 [ (gogoproto.customtype) = "Int", (gogoproto.nullable) = false ]; } +// DecCoin defines a token with a denomination and a decimal amount. +// +// NOTE: The amount field is an Dec which implements the custom method +// signatures required by gogoproto. message DecCoin { string denom = 1; bytes amount = 2 [ (gogoproto.customtype) = "Dec", (gogoproto.nullable) = false ]; From cafabf6469a8bcff5435402c70e5124f9dec8463 Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Wed, 1 Jan 2020 17:32:20 -0500 Subject: [PATCH 15/40] Remove old decimal amino tests --- types/decimal_test.go | 38 ++++---------------------------------- 1 file changed, 4 insertions(+), 34 deletions(-) diff --git a/types/decimal_test.go b/types/decimal_test.go index 7e5ed319e8c3..8f714c1be696 100644 --- a/types/decimal_test.go +++ b/types/decimal_test.go @@ -3,15 +3,12 @@ package types import ( "encoding/json" "fmt" - "math/big" - "testing" - + "github.com/cosmos/cosmos-sdk/codec" "github.com/stretchr/testify/assert" - yaml "gopkg.in/yaml.v2" - "github.com/stretchr/testify/require" - - "github.com/cosmos/cosmos-sdk/codec" + yaml "gopkg.in/yaml.v2" + "math/big" + "testing" ) // create a decimal from a decimal string (ex. "1234.5678") @@ -332,39 +329,12 @@ func TestSerializationGocodecJSON(t *testing.T) { require.True(t, d.Equal(d2), "original: %v, unmarshalled: %v", d, d2) } -func TestSerializationGocodecBinary(t *testing.T) { - d := mustNewDecFromStr(t, "0.333") - - bz, err := cdc.MarshalBinaryLengthPrefixed(d) - require.NoError(t, err) - - var d2 Dec - err = cdc.UnmarshalBinaryLengthPrefixed(bz, &d2) - require.NoError(t, err) - require.True(t, d.Equal(d2), "original: %v, unmarshalled: %v", d, d2) -} - type testDEmbedStruct struct { Field1 string `json:"f1"` Field2 int `json:"f2"` Field3 Dec `json:"f3"` } -// TODO make work for UnmarshalJSON -func TestEmbeddedStructSerializationGocodec(t *testing.T) { - obj := testDEmbedStruct{"foo", 10, NewDecWithPrec(1, 3)} - bz, err := cdc.MarshalBinaryLengthPrefixed(obj) - require.Nil(t, err) - - var obj2 testDEmbedStruct - err = cdc.UnmarshalBinaryLengthPrefixed(bz, &obj2) - require.Nil(t, err) - - require.Equal(t, obj.Field1, obj2.Field1) - require.Equal(t, obj.Field2, obj2.Field2) - require.True(t, obj.Field3.Equal(obj2.Field3), "original: %v, unmarshalled: %v", obj, obj2) -} - func TestStringOverflow(t *testing.T) { // two random 64 bit primes dec1, err := NewDecFromStr("51643150036226787134389711697696177267") From f48e8fd91c80295c11b82d0e6ba76deb94d41b36 Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Wed, 1 Jan 2020 18:11:19 -0500 Subject: [PATCH 16/40] Remove compare methods --- types/decimal.go | 2 +- types/int.go | 5 ----- types/uint.go | 5 ----- 3 files changed, 1 insertion(+), 11 deletions(-) diff --git a/types/decimal.go b/types/decimal.go index 4939c3ff67b6..97ebfd799892 100644 --- a/types/decimal.go +++ b/types/decimal.go @@ -15,7 +15,7 @@ var _ CustomProtoType = (*Dec)(nil) // NOTE: never use new(Dec) or else we will panic unmarshalling into the // nil embedded big.Int type Dec struct { - i *big.Int `json:"int"` + i *big.Int } // number of decimal places diff --git a/types/int.go b/types/int.go index 91dc039f7ff6..fe6d57d1e91d 100644 --- a/types/int.go +++ b/types/int.go @@ -387,11 +387,6 @@ func (i *Int) Size() int { return len(bz) } -// Compare implements the gogo proto custom type interface. -func (i Int) Compare(other Int) int { - return i.i.Cmp(other.i) -} - // intended to be used with require/assert: require.True(IntEq(...)) func IntEq(t *testing.T, exp, got Int) (*testing.T, bool, string, string, string) { return t, exp.Equal(got), "expected:\t%v\ngot:\t\t%v", exp.String(), got.String() diff --git a/types/uint.go b/types/uint.go index e5f97cab2667..c1a78d68931f 100644 --- a/types/uint.go +++ b/types/uint.go @@ -192,11 +192,6 @@ func (u *Uint) Size() int { return len(bz) } -// Compare implements the gogo proto custom type interface. -func (u Uint) Compare(other Int) int { - return u.i.Cmp(other.i) -} - //__________________________________________________________________________ // UintOverflow returns true if a given unsigned integer overflows and false From b723ac58202ddcc878d0500cc8375457bb566770 Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Thu, 2 Jan 2020 09:11:20 -0500 Subject: [PATCH 17/40] Fix sim --- types/decimal.go | 6 ++++++ x/simulation/rand_util.go | 4 ++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/types/decimal.go b/types/decimal.go index 97ebfd799892..fecc2f25bfd6 100644 --- a/types/decimal.go +++ b/types/decimal.go @@ -207,6 +207,12 @@ func (d Dec) LTE(d2 Dec) bool { return (d.i).Cmp(d2.i) <= 0 } // less th func (d Dec) Neg() Dec { return Dec{new(big.Int).Neg(d.i)} } // reverse the decimal sign func (d Dec) Abs() Dec { return Dec{new(big.Int).Abs(d.i)} } // absolute value +// BigInt returns a copy of the underlying big.Int. +func (d Dec) BigInt() *big.Int { + copy := new(big.Int) + return copy.Set(d.i) +} + // addition func (d Dec) Add(d2 Dec) Dec { res := new(big.Int).Add(d.i, d2.i) diff --git a/x/simulation/rand_util.go b/x/simulation/rand_util.go index b494b0fb8aab..7ef09100d545 100644 --- a/x/simulation/rand_util.go +++ b/x/simulation/rand_util.go @@ -69,9 +69,9 @@ func RandomDecAmount(r *rand.Rand, max sdk.Dec) sdk.Dec { case 0: // randInt = big.NewInt(0) case 1: - randInt = max.Int // the underlying big int with all precision bits. + randInt = max.BigInt() // the underlying big int with all precision bits. default: // NOTE: there are 10 total cases. - randInt = big.NewInt(0).Rand(r, max.Int) + randInt = big.NewInt(0).Rand(r, max.BigInt()) } return sdk.NewDecFromBigIntWithPrec(randInt, sdk.Precision) } From 2c92e06f1d3aa37a58c92e2b4db22f657410688b Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Thu, 2 Jan 2020 09:15:30 -0500 Subject: [PATCH 18/40] Remove TODO --- types/types.proto | 5 ----- 1 file changed, 5 deletions(-) diff --git a/types/types.proto b/types/types.proto index 71f7dbb7c67b..6fc4604b9ace 100644 --- a/types/types.proto +++ b/types/types.proto @@ -10,11 +10,6 @@ option (gogoproto.sizer_all) = true; import "vendor/github.com/gogo/protobuf/gogoproto/gogo.proto"; -// TODO: -// -// 2. Msg -// 3. Tx - // Coin defines a token with a denomination and an amount. // // NOTE: The amount field is an Int which implements the custom method From fa841fcb9fa1b583754b0462928137b0cb1d9f05 Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Thu, 2 Jan 2020 10:35:39 -0500 Subject: [PATCH 19/40] Rename types.proto to codec.proto --- types/{types.pb.go => codec.pb.go} | 121 +++++++++++++++-------------- types/{types.proto => codec.proto} | 0 2 files changed, 61 insertions(+), 60 deletions(-) rename types/{types.pb.go => codec.pb.go} (79%) rename types/{types.proto => codec.proto} (100%) diff --git a/types/types.pb.go b/types/codec.pb.go similarity index 79% rename from types/types.pb.go rename to types/codec.pb.go index 5899333b0f8d..96dc36da19c1 100644 --- a/types/types.pb.go +++ b/types/codec.pb.go @@ -1,5 +1,5 @@ // Code generated by protoc-gen-gogo. DO NOT EDIT. -// source: types/types.proto +// source: types/codec.proto package types @@ -38,7 +38,7 @@ type Coin struct { func (m *Coin) Reset() { *m = Coin{} } func (*Coin) ProtoMessage() {} func (*Coin) Descriptor() ([]byte, []int) { - return fileDescriptor_2c0f90c600ad7e2e, []int{0} + return fileDescriptor_97e5f89da6f0daf0, []int{0} } func (m *Coin) XXX_Unmarshal(b []byte) error { return m.Unmarshal(b) @@ -89,7 +89,7 @@ type DecCoin struct { func (m *DecCoin) Reset() { *m = DecCoin{} } func (*DecCoin) ProtoMessage() {} func (*DecCoin) Descriptor() ([]byte, []int) { - return fileDescriptor_2c0f90c600ad7e2e, []int{1} + return fileDescriptor_97e5f89da6f0daf0, []int{1} } func (m *DecCoin) XXX_Unmarshal(b []byte) error { return m.Unmarshal(b) @@ -130,22 +130,23 @@ func init() { proto.RegisterType((*DecCoin)(nil), "types.DecCoin") } -func init() { proto.RegisterFile("types/types.proto", fileDescriptor_2c0f90c600ad7e2e) } +func init() { proto.RegisterFile("types/codec.proto", fileDescriptor_97e5f89da6f0daf0) } -var fileDescriptor_2c0f90c600ad7e2e = []byte{ - // 189 bytes of a gzipped FileDescriptorProto +var fileDescriptor_97e5f89da6f0daf0 = []byte{ + // 193 bytes of a gzipped FileDescriptorProto 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x12, 0x2c, 0xa9, 0x2c, 0x48, - 0x2d, 0xd6, 0x07, 0x93, 0x7a, 0x05, 0x45, 0xf9, 0x25, 0xf9, 0x42, 0xac, 0x60, 0x8e, 0x94, 0x49, - 0x59, 0x6a, 0x5e, 0x4a, 0x7e, 0x91, 0x7e, 0x7a, 0x66, 0x49, 0x46, 0x69, 0x92, 0x5e, 0x72, 0x7e, - 0xae, 0x7e, 0x7a, 0x7e, 0x7a, 0xbe, 0x3e, 0x58, 0x51, 0x52, 0x69, 0x1a, 0x98, 0x07, 0xe6, 0x80, - 0x59, 0x10, 0xcd, 0x4a, 0x8e, 0x5c, 0x2c, 0xce, 0xf9, 0x99, 0x79, 0x42, 0x22, 0x5c, 0xac, 0x29, - 0xa9, 0x79, 0xf9, 0xb9, 0x12, 0x8c, 0x0a, 0x8c, 0x1a, 0x9c, 0x41, 0x10, 0x8e, 0x90, 0x32, 0x17, - 0x5b, 0x62, 0x6e, 0x7e, 0x69, 0x5e, 0x89, 0x04, 0x93, 0x02, 0xa3, 0x06, 0x8f, 0x13, 0xf7, 0x89, - 0x7b, 0xf2, 0x0c, 0xb7, 0xee, 0xc9, 0x33, 0x7b, 0xe6, 0x95, 0x04, 0x41, 0xa5, 0x94, 0x5c, 0xb8, - 0xd8, 0x5d, 0x52, 0x93, 0xc9, 0x31, 0xc5, 0x25, 0x35, 0x19, 0x66, 0x8a, 0x93, 0xfc, 0x8d, 0x87, - 0x72, 0x0c, 0x0d, 0x8f, 0xe4, 0x18, 0x4e, 0x3c, 0x92, 0x63, 0xbc, 0xf0, 0x48, 0x8e, 0xf1, 0xc1, - 0x23, 0x39, 0xc6, 0x19, 0x8f, 0xe5, 0x18, 0xa2, 0x20, 0xfe, 0x4b, 0x62, 0x03, 0x3b, 0xd8, 0x18, - 0x10, 0x00, 0x00, 0xff, 0xff, 0x30, 0x54, 0xad, 0x0c, 0x02, 0x01, 0x00, 0x00, + 0x2d, 0xd6, 0x4f, 0xce, 0x4f, 0x49, 0x4d, 0xd6, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x62, 0x05, + 0x0b, 0x49, 0x99, 0x94, 0xa5, 0xe6, 0xa5, 0xe4, 0x17, 0xe9, 0xa7, 0x67, 0x96, 0x64, 0x94, 0x26, + 0xe9, 0x25, 0xe7, 0xe7, 0xea, 0xa7, 0xe7, 0xa7, 0xe7, 0xeb, 0x83, 0x15, 0x25, 0x95, 0xa6, 0x81, + 0x79, 0x60, 0x0e, 0x98, 0x05, 0xd1, 0xac, 0xe4, 0xc8, 0xc5, 0xe2, 0x9c, 0x9f, 0x99, 0x27, 0x24, + 0xc2, 0xc5, 0x9a, 0x92, 0x9a, 0x97, 0x9f, 0x2b, 0xc1, 0xa8, 0xc0, 0xa8, 0xc1, 0x19, 0x04, 0xe1, + 0x08, 0x29, 0x73, 0xb1, 0x25, 0xe6, 0xe6, 0x97, 0xe6, 0x95, 0x48, 0x30, 0x29, 0x30, 0x6a, 0xf0, + 0x38, 0x71, 0x9f, 0xb8, 0x27, 0xcf, 0x70, 0xeb, 0x9e, 0x3c, 0xb3, 0x67, 0x5e, 0x49, 0x10, 0x54, + 0x4a, 0xc9, 0x85, 0x8b, 0xdd, 0x25, 0x35, 0x99, 0x1c, 0x53, 0x5c, 0x52, 0x93, 0x61, 0xa6, 0x38, + 0xc9, 0xdf, 0x78, 0x28, 0xc7, 0xd0, 0xf0, 0x48, 0x8e, 0xe1, 0xc4, 0x23, 0x39, 0xc6, 0x0b, 0x8f, + 0xe4, 0x18, 0x1f, 0x3c, 0x92, 0x63, 0x9c, 0xf1, 0x58, 0x8e, 0x21, 0x0a, 0xe2, 0xbf, 0x24, 0x36, + 0xb0, 0x83, 0x8d, 0x01, 0x01, 0x00, 0x00, 0xff, 0xff, 0x8c, 0x29, 0x87, 0x6c, 0x02, 0x01, 0x00, + 0x00, } func (m *Coin) Marshal() (dAtA []byte, err error) { @@ -178,14 +179,14 @@ func (m *Coin) MarshalToSizedBuffer(dAtA []byte) (int, error) { if _, err := m.Amount.MarshalTo(dAtA[i:]); err != nil { return 0, err } - i = encodeVarintTypes(dAtA, i, uint64(size)) + i = encodeVarintCodec(dAtA, i, uint64(size)) } i-- dAtA[i] = 0x12 if len(m.Denom) > 0 { i -= len(m.Denom) copy(dAtA[i:], m.Denom) - i = encodeVarintTypes(dAtA, i, uint64(len(m.Denom))) + i = encodeVarintCodec(dAtA, i, uint64(len(m.Denom))) i-- dAtA[i] = 0xa } @@ -222,22 +223,22 @@ func (m *DecCoin) MarshalToSizedBuffer(dAtA []byte) (int, error) { if _, err := m.Amount.MarshalTo(dAtA[i:]); err != nil { return 0, err } - i = encodeVarintTypes(dAtA, i, uint64(size)) + i = encodeVarintCodec(dAtA, i, uint64(size)) } i-- dAtA[i] = 0x12 if len(m.Denom) > 0 { i -= len(m.Denom) copy(dAtA[i:], m.Denom) - i = encodeVarintTypes(dAtA, i, uint64(len(m.Denom))) + i = encodeVarintCodec(dAtA, i, uint64(len(m.Denom))) i-- dAtA[i] = 0xa } return len(dAtA) - i, nil } -func encodeVarintTypes(dAtA []byte, offset int, v uint64) int { - offset -= sovTypes(v) +func encodeVarintCodec(dAtA []byte, offset int, v uint64) int { + offset -= sovCodec(v) base := offset for v >= 1<<7 { dAtA[offset] = uint8(v&0x7f | 0x80) @@ -255,10 +256,10 @@ func (m *Coin) Size() (n int) { _ = l l = len(m.Denom) if l > 0 { - n += 1 + l + sovTypes(uint64(l)) + n += 1 + l + sovCodec(uint64(l)) } l = m.Amount.Size() - n += 1 + l + sovTypes(uint64(l)) + n += 1 + l + sovCodec(uint64(l)) if m.XXX_unrecognized != nil { n += len(m.XXX_unrecognized) } @@ -273,21 +274,21 @@ func (m *DecCoin) Size() (n int) { _ = l l = len(m.Denom) if l > 0 { - n += 1 + l + sovTypes(uint64(l)) + n += 1 + l + sovCodec(uint64(l)) } l = m.Amount.Size() - n += 1 + l + sovTypes(uint64(l)) + n += 1 + l + sovCodec(uint64(l)) if m.XXX_unrecognized != nil { n += len(m.XXX_unrecognized) } return n } -func sovTypes(x uint64) (n int) { +func sovCodec(x uint64) (n int) { return (math_bits.Len64(x|1) + 6) / 7 } -func sozTypes(x uint64) (n int) { - return sovTypes(uint64((x << 1) ^ uint64((int64(x) >> 63)))) +func sozCodec(x uint64) (n int) { + return sovCodec(uint64((x << 1) ^ uint64((int64(x) >> 63)))) } func (m *Coin) Unmarshal(dAtA []byte) error { l := len(dAtA) @@ -297,7 +298,7 @@ func (m *Coin) Unmarshal(dAtA []byte) error { var wire uint64 for shift := uint(0); ; shift += 7 { if shift >= 64 { - return ErrIntOverflowTypes + return ErrIntOverflowCodec } if iNdEx >= l { return io.ErrUnexpectedEOF @@ -325,7 +326,7 @@ func (m *Coin) Unmarshal(dAtA []byte) error { var stringLen uint64 for shift := uint(0); ; shift += 7 { if shift >= 64 { - return ErrIntOverflowTypes + return ErrIntOverflowCodec } if iNdEx >= l { return io.ErrUnexpectedEOF @@ -339,11 +340,11 @@ func (m *Coin) Unmarshal(dAtA []byte) error { } intStringLen := int(stringLen) if intStringLen < 0 { - return ErrInvalidLengthTypes + return ErrInvalidLengthCodec } postIndex := iNdEx + intStringLen if postIndex < 0 { - return ErrInvalidLengthTypes + return ErrInvalidLengthCodec } if postIndex > l { return io.ErrUnexpectedEOF @@ -357,7 +358,7 @@ func (m *Coin) Unmarshal(dAtA []byte) error { var byteLen int for shift := uint(0); ; shift += 7 { if shift >= 64 { - return ErrIntOverflowTypes + return ErrIntOverflowCodec } if iNdEx >= l { return io.ErrUnexpectedEOF @@ -370,11 +371,11 @@ func (m *Coin) Unmarshal(dAtA []byte) error { } } if byteLen < 0 { - return ErrInvalidLengthTypes + return ErrInvalidLengthCodec } postIndex := iNdEx + byteLen if postIndex < 0 { - return ErrInvalidLengthTypes + return ErrInvalidLengthCodec } if postIndex > l { return io.ErrUnexpectedEOF @@ -385,15 +386,15 @@ func (m *Coin) Unmarshal(dAtA []byte) error { iNdEx = postIndex default: iNdEx = preIndex - skippy, err := skipTypes(dAtA[iNdEx:]) + skippy, err := skipCodec(dAtA[iNdEx:]) if err != nil { return err } if skippy < 0 { - return ErrInvalidLengthTypes + return ErrInvalidLengthCodec } if (iNdEx + skippy) < 0 { - return ErrInvalidLengthTypes + return ErrInvalidLengthCodec } if (iNdEx + skippy) > l { return io.ErrUnexpectedEOF @@ -416,7 +417,7 @@ func (m *DecCoin) Unmarshal(dAtA []byte) error { var wire uint64 for shift := uint(0); ; shift += 7 { if shift >= 64 { - return ErrIntOverflowTypes + return ErrIntOverflowCodec } if iNdEx >= l { return io.ErrUnexpectedEOF @@ -444,7 +445,7 @@ func (m *DecCoin) Unmarshal(dAtA []byte) error { var stringLen uint64 for shift := uint(0); ; shift += 7 { if shift >= 64 { - return ErrIntOverflowTypes + return ErrIntOverflowCodec } if iNdEx >= l { return io.ErrUnexpectedEOF @@ -458,11 +459,11 @@ func (m *DecCoin) Unmarshal(dAtA []byte) error { } intStringLen := int(stringLen) if intStringLen < 0 { - return ErrInvalidLengthTypes + return ErrInvalidLengthCodec } postIndex := iNdEx + intStringLen if postIndex < 0 { - return ErrInvalidLengthTypes + return ErrInvalidLengthCodec } if postIndex > l { return io.ErrUnexpectedEOF @@ -476,7 +477,7 @@ func (m *DecCoin) Unmarshal(dAtA []byte) error { var byteLen int for shift := uint(0); ; shift += 7 { if shift >= 64 { - return ErrIntOverflowTypes + return ErrIntOverflowCodec } if iNdEx >= l { return io.ErrUnexpectedEOF @@ -489,11 +490,11 @@ func (m *DecCoin) Unmarshal(dAtA []byte) error { } } if byteLen < 0 { - return ErrInvalidLengthTypes + return ErrInvalidLengthCodec } postIndex := iNdEx + byteLen if postIndex < 0 { - return ErrInvalidLengthTypes + return ErrInvalidLengthCodec } if postIndex > l { return io.ErrUnexpectedEOF @@ -504,15 +505,15 @@ func (m *DecCoin) Unmarshal(dAtA []byte) error { iNdEx = postIndex default: iNdEx = preIndex - skippy, err := skipTypes(dAtA[iNdEx:]) + skippy, err := skipCodec(dAtA[iNdEx:]) if err != nil { return err } if skippy < 0 { - return ErrInvalidLengthTypes + return ErrInvalidLengthCodec } if (iNdEx + skippy) < 0 { - return ErrInvalidLengthTypes + return ErrInvalidLengthCodec } if (iNdEx + skippy) > l { return io.ErrUnexpectedEOF @@ -527,7 +528,7 @@ func (m *DecCoin) Unmarshal(dAtA []byte) error { } return nil } -func skipTypes(dAtA []byte) (n int, err error) { +func skipCodec(dAtA []byte) (n int, err error) { l := len(dAtA) iNdEx := 0 depth := 0 @@ -535,7 +536,7 @@ func skipTypes(dAtA []byte) (n int, err error) { var wire uint64 for shift := uint(0); ; shift += 7 { if shift >= 64 { - return 0, ErrIntOverflowTypes + return 0, ErrIntOverflowCodec } if iNdEx >= l { return 0, io.ErrUnexpectedEOF @@ -552,7 +553,7 @@ func skipTypes(dAtA []byte) (n int, err error) { case 0: for shift := uint(0); ; shift += 7 { if shift >= 64 { - return 0, ErrIntOverflowTypes + return 0, ErrIntOverflowCodec } if iNdEx >= l { return 0, io.ErrUnexpectedEOF @@ -568,7 +569,7 @@ func skipTypes(dAtA []byte) (n int, err error) { var length int for shift := uint(0); ; shift += 7 { if shift >= 64 { - return 0, ErrIntOverflowTypes + return 0, ErrIntOverflowCodec } if iNdEx >= l { return 0, io.ErrUnexpectedEOF @@ -581,14 +582,14 @@ func skipTypes(dAtA []byte) (n int, err error) { } } if length < 0 { - return 0, ErrInvalidLengthTypes + return 0, ErrInvalidLengthCodec } iNdEx += length case 3: depth++ case 4: if depth == 0 { - return 0, ErrUnexpectedEndOfGroupTypes + return 0, ErrUnexpectedEndOfGroupCodec } depth-- case 5: @@ -597,7 +598,7 @@ func skipTypes(dAtA []byte) (n int, err error) { return 0, fmt.Errorf("proto: illegal wireType %d", wireType) } if iNdEx < 0 { - return 0, ErrInvalidLengthTypes + return 0, ErrInvalidLengthCodec } if depth == 0 { return iNdEx, nil @@ -607,7 +608,7 @@ func skipTypes(dAtA []byte) (n int, err error) { } var ( - ErrInvalidLengthTypes = fmt.Errorf("proto: negative length found during unmarshaling") - ErrIntOverflowTypes = fmt.Errorf("proto: integer overflow") - ErrUnexpectedEndOfGroupTypes = fmt.Errorf("proto: unexpected end of group") + ErrInvalidLengthCodec = fmt.Errorf("proto: negative length found during unmarshaling") + ErrIntOverflowCodec = fmt.Errorf("proto: integer overflow") + ErrUnexpectedEndOfGroupCodec = fmt.Errorf("proto: unexpected end of group") ) diff --git a/types/types.proto b/types/codec.proto similarity index 100% rename from types/types.proto rename to types/codec.proto From b990a1084c41e89cf72f03aae9c8e1d6dee13c47 Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Thu, 2 Jan 2020 10:36:17 -0500 Subject: [PATCH 20/40] remove old codec.go --- types/codec.go | 9 --------- 1 file changed, 9 deletions(-) delete mode 100644 types/codec.go diff --git a/types/codec.go b/types/codec.go deleted file mode 100644 index 3d789afe9f0a..000000000000 --- a/types/codec.go +++ /dev/null @@ -1,9 +0,0 @@ -package types - -import "github.com/cosmos/cosmos-sdk/codec" - -// Register the sdk message type -func RegisterCodec(cdc *codec.Codec) { - cdc.RegisterInterface((*Msg)(nil), nil) - cdc.RegisterInterface((*Tx)(nil), nil) -} From dae8941879d69dc361299f81dd70dc7fe16aaddd Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Thu, 2 Jan 2020 11:39:30 -0500 Subject: [PATCH 21/40] Fix linting --- types/coin_test.go | 5 ----- types/decimal_test.go | 5 +++-- types/int.go | 3 ++- 3 files changed, 5 insertions(+), 8 deletions(-) diff --git a/types/coin_test.go b/types/coin_test.go index cb17af6ffa93..d412f58eee50 100644 --- a/types/coin_test.go +++ b/types/coin_test.go @@ -10,8 +10,6 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" yaml "gopkg.in/yaml.v2" - - "github.com/cosmos/cosmos-sdk/codec" ) var ( @@ -658,9 +656,6 @@ func TestFindDup(t *testing.T) { } func TestMarshalJSONCoins(t *testing.T) { - cdc := codec.New() - RegisterCodec(cdc) - testCases := []struct { name string input Coins diff --git a/types/decimal_test.go b/types/decimal_test.go index 8f714c1be696..191f13f0128f 100644 --- a/types/decimal_test.go +++ b/types/decimal_test.go @@ -3,12 +3,13 @@ package types import ( "encoding/json" "fmt" + "math/big" + "testing" + "github.com/cosmos/cosmos-sdk/codec" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" yaml "gopkg.in/yaml.v2" - "math/big" - "testing" ) // create a decimal from a decimal string (ex. "1234.5678") diff --git a/types/int.go b/types/int.go index fe6d57d1e91d..0c2f8d008d70 100644 --- a/types/int.go +++ b/types/int.go @@ -1,6 +1,7 @@ package types import ( + "encoding" "encoding/json" "fmt" "testing" @@ -307,7 +308,7 @@ func (i *Int) UnmarshalJSON(bz []byte) error { // MarshalJSON for custom encoding scheme // Must be encoded as a string for JSON precision -func marshalJSON(i *big.Int) ([]byte, error) { +func marshalJSON(i encoding.TextMarshaler) ([]byte, error) { text, err := i.MarshalText() if err != nil { return nil, err From d87b7c632ce9ecc65a567e08f2a6b5bb751124cd Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Thu, 2 Jan 2020 13:17:03 -0500 Subject: [PATCH 22/40] Add todo --- types/codec.proto | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/types/codec.proto b/types/codec.proto index 6fc4604b9ace..668ab2d26796 100644 --- a/types/codec.proto +++ b/types/codec.proto @@ -16,6 +16,8 @@ import "vendor/github.com/gogo/protobuf/gogoproto/gogo.proto"; // signatures required by gogoproto. message Coin { string denom = 1; + // TODO: Consider using a string instead of bytes. This would require gogo proto + // to support types other than bytes for the customtype extension. bytes amount = 2 [ (gogoproto.customtype) = "Int", (gogoproto.nullable) = false ]; } @@ -25,5 +27,7 @@ message Coin { // signatures required by gogoproto. message DecCoin { string denom = 1; + // TODO: Consider using a string instead of bytes. This would require gogo proto + // to support types other than bytes for the customtype extension. bytes amount = 2 [ (gogoproto.customtype) = "Dec", (gogoproto.nullable) = false ]; } From 58441cf63fe90078f6d167289abc29fe49741012 Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Thu, 2 Jan 2020 14:29:48 -0500 Subject: [PATCH 23/40] Squash --- .github/workflows/proto.yml | 17 +++++++++++++++++ Makefile | 2 +- 2 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/proto.yml diff --git a/.github/workflows/proto.yml b/.github/workflows/proto.yml new file mode 100644 index 000000000000..72f1f7f62052 --- /dev/null +++ b/.github/workflows/proto.yml @@ -0,0 +1,17 @@ +name: Protobuf +on: [pull_request] +jobs: + lint-and-verify: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Install dependencies + run: sudo make buf + - name: test + run: sh -c "sudo chown -R circlci /home/runner/.cache/go-build" + - name: Install Protoc + uses: Arduino/actions/setup-protoc@master + with: + version: '3.11.x' + - name: Generate, lint, and verify + run: sudo make proto-all diff --git a/Makefile b/Makefile index b0a34bf0bc4a..e0b02d93e961 100644 --- a/Makefile +++ b/Makefile @@ -227,7 +227,7 @@ devdoc-update: ### Protobuf ### ############################################################################### -proto-all: proto-lint proto-check-breaking proto-gen +proto-all: proto-gen proto-lint proto-check-breaking proto-gen: @go mod vendor From 4c884c3d98a57d2bb74f0b0d06a07d1ad1cd3a19 Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Thu, 2 Jan 2020 14:30:41 -0500 Subject: [PATCH 24/40] Update GH action --- .github/workflows/proto.yml | 17 ----------------- 1 file changed, 17 deletions(-) delete mode 100644 .github/workflows/proto.yml diff --git a/.github/workflows/proto.yml b/.github/workflows/proto.yml deleted file mode 100644 index 72f1f7f62052..000000000000 --- a/.github/workflows/proto.yml +++ /dev/null @@ -1,17 +0,0 @@ -name: Protobuf -on: [pull_request] -jobs: - lint-and-verify: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - name: Install dependencies - run: sudo make buf - - name: test - run: sh -c "sudo chown -R circlci /home/runner/.cache/go-build" - - name: Install Protoc - uses: Arduino/actions/setup-protoc@master - with: - version: '3.11.x' - - name: Generate, lint, and verify - run: sudo make proto-all From 2f50122f2a1033a497b23d8a912d7bb88a847ef1 Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Thu, 2 Jan 2020 15:21:21 -0500 Subject: [PATCH 25/40] Change package name and add version suffix --- buf.yaml | 2 +- types/codec.pb.go | 38 +++++++++++++++++++++----------------- types/codec.proto | 2 +- 3 files changed, 23 insertions(+), 19 deletions(-) diff --git a/buf.yaml b/buf.yaml index ff2065dbfd2d..a1fc20cc249a 100644 --- a/buf.yaml +++ b/buf.yaml @@ -10,8 +10,8 @@ lint: - FILE_LOWER_SNAKE_CASE except: - UNARY_RPC - - PACKAGE_VERSION_SUFFIX - COMMENT_FIELD + - PACKAGE_DIRECTORY_MATCH ignore: - vendor breaking: diff --git a/types/codec.pb.go b/types/codec.pb.go index 96dc36da19c1..5983add7382a 100644 --- a/types/codec.pb.go +++ b/types/codec.pb.go @@ -28,7 +28,9 @@ const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package // NOTE: The amount field is an Int which implements the custom method // signatures required by gogoproto. type Coin struct { - Denom string `protobuf:"bytes,1,opt,name=denom,proto3" json:"denom,omitempty"` + Denom string `protobuf:"bytes,1,opt,name=denom,proto3" json:"denom,omitempty"` + // TODO: Consider using a string instead of bytes. This would require gogo proto + // to support types other than bytes for the customtype extension. Amount Int `protobuf:"bytes,2,opt,name=amount,proto3,customtype=Int" json:"amount"` XXX_NoUnkeyedLiteral struct{} `json:"-"` XXX_unrecognized []byte `json:"-"` @@ -79,7 +81,9 @@ func (m *Coin) GetDenom() string { // NOTE: The amount field is an Dec which implements the custom method // signatures required by gogoproto. type DecCoin struct { - Denom string `protobuf:"bytes,1,opt,name=denom,proto3" json:"denom,omitempty"` + Denom string `protobuf:"bytes,1,opt,name=denom,proto3" json:"denom,omitempty"` + // TODO: Consider using a string instead of bytes. This would require gogo proto + // to support types other than bytes for the customtype extension. Amount Dec `protobuf:"bytes,2,opt,name=amount,proto3,customtype=Dec" json:"amount"` XXX_NoUnkeyedLiteral struct{} `json:"-"` XXX_unrecognized []byte `json:"-"` @@ -126,27 +130,27 @@ func (m *DecCoin) GetDenom() string { } func init() { - proto.RegisterType((*Coin)(nil), "types.Coin") - proto.RegisterType((*DecCoin)(nil), "types.DecCoin") + proto.RegisterType((*Coin)(nil), "cosmos_sdk.v1.Coin") + proto.RegisterType((*DecCoin)(nil), "cosmos_sdk.v1.DecCoin") } func init() { proto.RegisterFile("types/codec.proto", fileDescriptor_97e5f89da6f0daf0) } var fileDescriptor_97e5f89da6f0daf0 = []byte{ - // 193 bytes of a gzipped FileDescriptorProto + // 205 bytes of a gzipped FileDescriptorProto 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x12, 0x2c, 0xa9, 0x2c, 0x48, - 0x2d, 0xd6, 0x4f, 0xce, 0x4f, 0x49, 0x4d, 0xd6, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x62, 0x05, - 0x0b, 0x49, 0x99, 0x94, 0xa5, 0xe6, 0xa5, 0xe4, 0x17, 0xe9, 0xa7, 0x67, 0x96, 0x64, 0x94, 0x26, - 0xe9, 0x25, 0xe7, 0xe7, 0xea, 0xa7, 0xe7, 0xa7, 0xe7, 0xeb, 0x83, 0x15, 0x25, 0x95, 0xa6, 0x81, - 0x79, 0x60, 0x0e, 0x98, 0x05, 0xd1, 0xac, 0xe4, 0xc8, 0xc5, 0xe2, 0x9c, 0x9f, 0x99, 0x27, 0x24, - 0xc2, 0xc5, 0x9a, 0x92, 0x9a, 0x97, 0x9f, 0x2b, 0xc1, 0xa8, 0xc0, 0xa8, 0xc1, 0x19, 0x04, 0xe1, - 0x08, 0x29, 0x73, 0xb1, 0x25, 0xe6, 0xe6, 0x97, 0xe6, 0x95, 0x48, 0x30, 0x29, 0x30, 0x6a, 0xf0, - 0x38, 0x71, 0x9f, 0xb8, 0x27, 0xcf, 0x70, 0xeb, 0x9e, 0x3c, 0xb3, 0x67, 0x5e, 0x49, 0x10, 0x54, - 0x4a, 0xc9, 0x85, 0x8b, 0xdd, 0x25, 0x35, 0x99, 0x1c, 0x53, 0x5c, 0x52, 0x93, 0x61, 0xa6, 0x38, - 0xc9, 0xdf, 0x78, 0x28, 0xc7, 0xd0, 0xf0, 0x48, 0x8e, 0xe1, 0xc4, 0x23, 0x39, 0xc6, 0x0b, 0x8f, - 0xe4, 0x18, 0x1f, 0x3c, 0x92, 0x63, 0x9c, 0xf1, 0x58, 0x8e, 0x21, 0x0a, 0xe2, 0xbf, 0x24, 0x36, - 0xb0, 0x83, 0x8d, 0x01, 0x01, 0x00, 0x00, 0xff, 0xff, 0x8c, 0x29, 0x87, 0x6c, 0x02, 0x01, 0x00, - 0x00, + 0x2d, 0xd6, 0x4f, 0xce, 0x4f, 0x49, 0x4d, 0xd6, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0xe2, 0x4d, + 0xce, 0x2f, 0xce, 0xcd, 0x2f, 0x8e, 0x2f, 0x4e, 0xc9, 0xd6, 0x2b, 0x33, 0x94, 0x32, 0x29, 0x4b, + 0xcd, 0x4b, 0xc9, 0x2f, 0xd2, 0x4f, 0xcf, 0x2c, 0xc9, 0x28, 0x4d, 0xd2, 0x4b, 0xce, 0xcf, 0xd5, + 0x4f, 0xcf, 0x4f, 0xcf, 0xd7, 0x07, 0x2b, 0x4e, 0x2a, 0x4d, 0x03, 0xf3, 0xc0, 0x1c, 0x30, 0x0b, + 0x62, 0x88, 0x92, 0x23, 0x17, 0x8b, 0x73, 0x7e, 0x66, 0x9e, 0x90, 0x08, 0x17, 0x6b, 0x4a, 0x6a, + 0x5e, 0x7e, 0xae, 0x04, 0xa3, 0x02, 0xa3, 0x06, 0x67, 0x10, 0x84, 0x23, 0xa4, 0xcc, 0xc5, 0x96, + 0x98, 0x9b, 0x5f, 0x9a, 0x57, 0x22, 0xc1, 0xa4, 0xc0, 0xa8, 0xc1, 0xe3, 0xc4, 0x7d, 0xe2, 0x9e, + 0x3c, 0xc3, 0xad, 0x7b, 0xf2, 0xcc, 0x9e, 0x79, 0x25, 0x41, 0x50, 0x29, 0x25, 0x17, 0x2e, 0x76, + 0x97, 0xd4, 0x64, 0x72, 0x4c, 0x71, 0x49, 0x4d, 0x86, 0x99, 0xe2, 0x24, 0x7f, 0xe3, 0xa1, 0x1c, + 0x43, 0xc3, 0x23, 0x39, 0x86, 0x13, 0x8f, 0xe4, 0x18, 0x2f, 0x3c, 0x92, 0x63, 0x7c, 0xf0, 0x48, + 0x8e, 0x71, 0xc6, 0x63, 0x39, 0x86, 0x28, 0x56, 0xb0, 0xd7, 0x93, 0xd8, 0xc0, 0x0e, 0x36, 0x06, + 0x04, 0x00, 0x00, 0xff, 0xff, 0x47, 0x8a, 0x73, 0x02, 0x0a, 0x01, 0x00, 0x00, } func (m *Coin) Marshal() (dAtA []byte, err error) { diff --git a/types/codec.proto b/types/codec.proto index 668ab2d26796..228f9270ae28 100644 --- a/types/codec.proto +++ b/types/codec.proto @@ -1,5 +1,5 @@ syntax = "proto3"; -package types; +package cosmos_sdk.v1; option go_package = "types"; option (gogoproto.goproto_stringer_all) = false; From 616a10d3675842eab4b914836fae30156b40a059 Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Fri, 3 Jan 2020 10:45:09 -0500 Subject: [PATCH 26/40] Initial commit --- crypto/codec.pb.go | 546 +++++++++++++++++++++++++++++++++++++++ crypto/codec.proto | 22 ++ scripts/protocgen.sh | 2 +- types/codec.pb.go | 9 +- types/codec.proto | 2 +- x/auth/types/account.go | 44 +--- x/auth/types/codec.go | 44 ++-- x/auth/types/codec.pb.go | 540 ++++++++++++++++++++++++++++++++++++++ x/auth/types/codec.proto | 28 ++ 9 files changed, 1169 insertions(+), 68 deletions(-) create mode 100644 crypto/codec.pb.go create mode 100644 crypto/codec.proto create mode 100644 x/auth/types/codec.pb.go create mode 100644 x/auth/types/codec.proto diff --git a/crypto/codec.pb.go b/crypto/codec.pb.go new file mode 100644 index 000000000000..06f67b5187e3 --- /dev/null +++ b/crypto/codec.pb.go @@ -0,0 +1,546 @@ +// Code generated by protoc-gen-gogo. DO NOT EDIT. +// source: crypto/codec.proto + +package crypto + +import ( + fmt "fmt" + _ "github.com/gogo/protobuf/gogoproto" + proto "github.com/golang/protobuf/proto" + io "io" + math "math" + math_bits "math/bits" +) + +// Reference imports to suppress errors if they are not otherwise used. +var _ = proto.Marshal +var _ = fmt.Errorf +var _ = math.Inf + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the proto package it is being compiled against. +// A compilation error at this line likely means your copy of the +// proto package needs to be updated. +const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package + +// PublicKey defines a proto message with a single oneof field that represents a +// public key as a byte slice. +// +// NOTE: Private keys must still be amino-encoded for backwards compatiblity. +type PublicKey struct { + // pub defines an single supported public key type as a byte slice. + // + // Types that are valid to be assigned to Pub: + // *PublicKey_Secp256K1 + // *PublicKey_Ed25519 + // *PublicKey_Multisig + Pub isPublicKey_Pub `protobuf_oneof:"pub"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` +} + +func (m *PublicKey) Reset() { *m = PublicKey{} } +func (m *PublicKey) String() string { return proto.CompactTextString(m) } +func (*PublicKey) ProtoMessage() {} +func (*PublicKey) Descriptor() ([]byte, []int) { + return fileDescriptor_0402281428f37664, []int{0} +} +func (m *PublicKey) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *PublicKey) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_PublicKey.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalToSizedBuffer(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (m *PublicKey) XXX_Merge(src proto.Message) { + xxx_messageInfo_PublicKey.Merge(m, src) +} +func (m *PublicKey) XXX_Size() int { + return m.Size() +} +func (m *PublicKey) XXX_DiscardUnknown() { + xxx_messageInfo_PublicKey.DiscardUnknown(m) +} + +var xxx_messageInfo_PublicKey proto.InternalMessageInfo + +type isPublicKey_Pub interface { + isPublicKey_Pub() + MarshalTo([]byte) (int, error) + Size() int +} + +type PublicKey_Secp256K1 struct { + Secp256K1 []byte `protobuf:"bytes,1,opt,name=secp256k1,proto3,oneof" json:"secp256k1,omitempty"` +} +type PublicKey_Ed25519 struct { + Ed25519 []byte `protobuf:"bytes,2,opt,name=ed25519,proto3,oneof" json:"ed25519,omitempty"` +} +type PublicKey_Multisig struct { + Multisig []byte `protobuf:"bytes,3,opt,name=multisig,proto3,oneof" json:"multisig,omitempty"` +} + +func (*PublicKey_Secp256K1) isPublicKey_Pub() {} +func (*PublicKey_Ed25519) isPublicKey_Pub() {} +func (*PublicKey_Multisig) isPublicKey_Pub() {} + +func (m *PublicKey) GetPub() isPublicKey_Pub { + if m != nil { + return m.Pub + } + return nil +} + +func (m *PublicKey) GetSecp256K1() []byte { + if x, ok := m.GetPub().(*PublicKey_Secp256K1); ok { + return x.Secp256K1 + } + return nil +} + +func (m *PublicKey) GetEd25519() []byte { + if x, ok := m.GetPub().(*PublicKey_Ed25519); ok { + return x.Ed25519 + } + return nil +} + +func (m *PublicKey) GetMultisig() []byte { + if x, ok := m.GetPub().(*PublicKey_Multisig); ok { + return x.Multisig + } + return nil +} + +// XXX_OneofWrappers is for the internal use of the proto package. +func (*PublicKey) XXX_OneofWrappers() []interface{} { + return []interface{}{ + (*PublicKey_Secp256K1)(nil), + (*PublicKey_Ed25519)(nil), + (*PublicKey_Multisig)(nil), + } +} + +func init() { + proto.RegisterType((*PublicKey)(nil), "cosmos_sdk.crypto.v1.PublicKey") +} + +func init() { proto.RegisterFile("crypto/codec.proto", fileDescriptor_0402281428f37664) } + +var fileDescriptor_0402281428f37664 = []byte{ + // 217 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x12, 0x4a, 0x2e, 0xaa, 0x2c, + 0x28, 0xc9, 0xd7, 0x4f, 0xce, 0x4f, 0x49, 0x4d, 0xd6, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x12, + 0x49, 0xce, 0x2f, 0xce, 0xcd, 0x2f, 0x8e, 0x2f, 0x4e, 0xc9, 0xd6, 0x83, 0x48, 0xeb, 0x95, 0x19, + 0x4a, 0x99, 0x94, 0xa5, 0xe6, 0xa5, 0xe4, 0x17, 0xe9, 0xa7, 0x67, 0x96, 0x64, 0x94, 0x26, 0xe9, + 0x25, 0xe7, 0xe7, 0xea, 0xa7, 0xe7, 0xa7, 0xe7, 0xeb, 0x83, 0xf5, 0x24, 0x95, 0xa6, 0x81, 0x79, + 0x60, 0x0e, 0x98, 0x05, 0x31, 0x4b, 0x29, 0x87, 0x8b, 0x33, 0xa0, 0x34, 0x29, 0x27, 0x33, 0xd9, + 0x3b, 0xb5, 0x52, 0x48, 0x8e, 0x8b, 0xb3, 0x38, 0x35, 0xb9, 0xc0, 0xc8, 0xd4, 0x2c, 0xdb, 0x50, + 0x82, 0x51, 0x81, 0x51, 0x83, 0xc7, 0x83, 0x21, 0x08, 0x21, 0x24, 0x24, 0xc5, 0xc5, 0x9e, 0x9a, + 0x62, 0x64, 0x6a, 0x6a, 0x68, 0x29, 0xc1, 0x04, 0x95, 0x85, 0x09, 0x08, 0xc9, 0x70, 0x71, 0xe4, + 0x96, 0xe6, 0x94, 0x64, 0x16, 0x67, 0xa6, 0x4b, 0x30, 0x43, 0x25, 0xe1, 0x22, 0x4e, 0xac, 0x5c, + 0xcc, 0x05, 0xa5, 0x49, 0x4e, 0xa6, 0x27, 0x1e, 0xc9, 0x31, 0x5e, 0x78, 0x24, 0xc7, 0xf8, 0xe0, + 0x91, 0x1c, 0xe3, 0x8c, 0xc7, 0x72, 0x0c, 0x51, 0xca, 0x48, 0xce, 0x85, 0x78, 0x0b, 0x4a, 0xe9, + 0x16, 0xa7, 0x64, 0xeb, 0x43, 0x7c, 0x97, 0xc4, 0x06, 0x76, 0xab, 0x31, 0x20, 0x00, 0x00, 0xff, + 0xff, 0x1d, 0x9c, 0xc5, 0xf1, 0x0d, 0x01, 0x00, 0x00, +} + +func (m *PublicKey) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalToSizedBuffer(dAtA[:size]) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *PublicKey) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *PublicKey) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + if m.XXX_unrecognized != nil { + i -= len(m.XXX_unrecognized) + copy(dAtA[i:], m.XXX_unrecognized) + } + if m.Pub != nil { + { + size := m.Pub.Size() + i -= size + if _, err := m.Pub.MarshalTo(dAtA[i:]); err != nil { + return 0, err + } + } + } + return len(dAtA) - i, nil +} + +func (m *PublicKey_Secp256K1) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *PublicKey_Secp256K1) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + if m.Secp256K1 != nil { + i -= len(m.Secp256K1) + copy(dAtA[i:], m.Secp256K1) + i = encodeVarintCodec(dAtA, i, uint64(len(m.Secp256K1))) + i-- + dAtA[i] = 0xa + } + return len(dAtA) - i, nil +} +func (m *PublicKey_Ed25519) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *PublicKey_Ed25519) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + if m.Ed25519 != nil { + i -= len(m.Ed25519) + copy(dAtA[i:], m.Ed25519) + i = encodeVarintCodec(dAtA, i, uint64(len(m.Ed25519))) + i-- + dAtA[i] = 0x12 + } + return len(dAtA) - i, nil +} +func (m *PublicKey_Multisig) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *PublicKey_Multisig) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + if m.Multisig != nil { + i -= len(m.Multisig) + copy(dAtA[i:], m.Multisig) + i = encodeVarintCodec(dAtA, i, uint64(len(m.Multisig))) + i-- + dAtA[i] = 0x1a + } + return len(dAtA) - i, nil +} +func encodeVarintCodec(dAtA []byte, offset int, v uint64) int { + offset -= sovCodec(v) + base := offset + for v >= 1<<7 { + dAtA[offset] = uint8(v&0x7f | 0x80) + v >>= 7 + offset++ + } + dAtA[offset] = uint8(v) + return base +} +func (m *PublicKey) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + if m.Pub != nil { + n += m.Pub.Size() + } + if m.XXX_unrecognized != nil { + n += len(m.XXX_unrecognized) + } + return n +} + +func (m *PublicKey_Secp256K1) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + if m.Secp256K1 != nil { + l = len(m.Secp256K1) + n += 1 + l + sovCodec(uint64(l)) + } + return n +} +func (m *PublicKey_Ed25519) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + if m.Ed25519 != nil { + l = len(m.Ed25519) + n += 1 + l + sovCodec(uint64(l)) + } + return n +} +func (m *PublicKey_Multisig) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + if m.Multisig != nil { + l = len(m.Multisig) + n += 1 + l + sovCodec(uint64(l)) + } + return n +} + +func sovCodec(x uint64) (n int) { + return (math_bits.Len64(x|1) + 6) / 7 +} +func sozCodec(x uint64) (n int) { + return sovCodec(uint64((x << 1) ^ uint64((int64(x) >> 63)))) +} +func (m *PublicKey) Unmarshal(dAtA []byte) error { + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowCodec + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: PublicKey: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: PublicKey: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Secp256K1", wireType) + } + var byteLen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowCodec + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + byteLen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if byteLen < 0 { + return ErrInvalidLengthCodec + } + postIndex := iNdEx + byteLen + if postIndex < 0 { + return ErrInvalidLengthCodec + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + v := make([]byte, postIndex-iNdEx) + copy(v, dAtA[iNdEx:postIndex]) + m.Pub = &PublicKey_Secp256K1{v} + iNdEx = postIndex + case 2: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Ed25519", wireType) + } + var byteLen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowCodec + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + byteLen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if byteLen < 0 { + return ErrInvalidLengthCodec + } + postIndex := iNdEx + byteLen + if postIndex < 0 { + return ErrInvalidLengthCodec + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + v := make([]byte, postIndex-iNdEx) + copy(v, dAtA[iNdEx:postIndex]) + m.Pub = &PublicKey_Ed25519{v} + iNdEx = postIndex + case 3: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Multisig", wireType) + } + var byteLen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowCodec + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + byteLen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if byteLen < 0 { + return ErrInvalidLengthCodec + } + postIndex := iNdEx + byteLen + if postIndex < 0 { + return ErrInvalidLengthCodec + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + v := make([]byte, postIndex-iNdEx) + copy(v, dAtA[iNdEx:postIndex]) + m.Pub = &PublicKey_Multisig{v} + iNdEx = postIndex + default: + iNdEx = preIndex + skippy, err := skipCodec(dAtA[iNdEx:]) + if err != nil { + return err + } + if skippy < 0 { + return ErrInvalidLengthCodec + } + if (iNdEx + skippy) < 0 { + return ErrInvalidLengthCodec + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + m.XXX_unrecognized = append(m.XXX_unrecognized, dAtA[iNdEx:iNdEx+skippy]...) + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} +func skipCodec(dAtA []byte) (n int, err error) { + l := len(dAtA) + iNdEx := 0 + depth := 0 + for iNdEx < l { + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowCodec + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= (uint64(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + wireType := int(wire & 0x7) + switch wireType { + case 0: + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowCodec + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + iNdEx++ + if dAtA[iNdEx-1] < 0x80 { + break + } + } + case 1: + iNdEx += 8 + case 2: + var length int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowCodec + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + length |= (int(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + if length < 0 { + return 0, ErrInvalidLengthCodec + } + iNdEx += length + case 3: + depth++ + case 4: + if depth == 0 { + return 0, ErrUnexpectedEndOfGroupCodec + } + depth-- + case 5: + iNdEx += 4 + default: + return 0, fmt.Errorf("proto: illegal wireType %d", wireType) + } + if iNdEx < 0 { + return 0, ErrInvalidLengthCodec + } + if depth == 0 { + return iNdEx, nil + } + } + return 0, io.ErrUnexpectedEOF +} + +var ( + ErrInvalidLengthCodec = fmt.Errorf("proto: negative length found during unmarshaling") + ErrIntOverflowCodec = fmt.Errorf("proto: integer overflow") + ErrUnexpectedEndOfGroupCodec = fmt.Errorf("proto: unexpected end of group") +) diff --git a/crypto/codec.proto b/crypto/codec.proto new file mode 100644 index 000000000000..32c26e1a0186 --- /dev/null +++ b/crypto/codec.proto @@ -0,0 +1,22 @@ +syntax = "proto3"; +package cosmos_sdk.crypto.v1; + +import "vendor/github.com/gogo/protobuf/gogoproto/gogo.proto"; + +option go_package = "github.com/cosmos/cosmos-sdk/crypto"; +option (gogoproto.marshaler_all) = true; +option (gogoproto.unmarshaler_all) = true; +option (gogoproto.sizer_all) = true; + +// PublicKey defines a proto message with a single oneof field that represents a +// public key as a byte slice. +// +// NOTE: Private keys must still be amino-encoded for backwards compatiblity. +message PublicKey { + // pub defines an single supported public key type as a byte slice. + oneof pub { + bytes secp256k1 = 1; + bytes ed25519 = 2; + bytes multisig = 3; + } +} diff --git a/scripts/protocgen.sh b/scripts/protocgen.sh index 3e90d5d6eefb..ebe4ba0b96ac 100755 --- a/scripts/protocgen.sh +++ b/scripts/protocgen.sh @@ -8,6 +8,6 @@ for dir in $proto_dirs; do -I. \ -I${GOPATH}/src \ -Ivendor/github.com/gogo/protobuf/gogoproto \ - --gofast_out=. \ + --gofast_out=$GOPATH/src \ $(find "${dir}" -name '*.proto') done diff --git a/types/codec.pb.go b/types/codec.pb.go index 5983add7382a..11f1c5a65894 100644 --- a/types/codec.pb.go +++ b/types/codec.pb.go @@ -137,7 +137,7 @@ func init() { func init() { proto.RegisterFile("types/codec.proto", fileDescriptor_97e5f89da6f0daf0) } var fileDescriptor_97e5f89da6f0daf0 = []byte{ - // 205 bytes of a gzipped FileDescriptorProto + // 216 bytes of a gzipped FileDescriptorProto 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x12, 0x2c, 0xa9, 0x2c, 0x48, 0x2d, 0xd6, 0x4f, 0xce, 0x4f, 0x49, 0x4d, 0xd6, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0xe2, 0x4d, 0xce, 0x2f, 0xce, 0xcd, 0x2f, 0x8e, 0x2f, 0x4e, 0xc9, 0xd6, 0x2b, 0x33, 0x94, 0x32, 0x29, 0x4b, @@ -147,10 +147,11 @@ var fileDescriptor_97e5f89da6f0daf0 = []byte{ 0x5e, 0x7e, 0xae, 0x04, 0xa3, 0x02, 0xa3, 0x06, 0x67, 0x10, 0x84, 0x23, 0xa4, 0xcc, 0xc5, 0x96, 0x98, 0x9b, 0x5f, 0x9a, 0x57, 0x22, 0xc1, 0xa4, 0xc0, 0xa8, 0xc1, 0xe3, 0xc4, 0x7d, 0xe2, 0x9e, 0x3c, 0xc3, 0xad, 0x7b, 0xf2, 0xcc, 0x9e, 0x79, 0x25, 0x41, 0x50, 0x29, 0x25, 0x17, 0x2e, 0x76, - 0x97, 0xd4, 0x64, 0x72, 0x4c, 0x71, 0x49, 0x4d, 0x86, 0x99, 0xe2, 0x24, 0x7f, 0xe3, 0xa1, 0x1c, + 0x97, 0xd4, 0x64, 0x72, 0x4c, 0x71, 0x49, 0x4d, 0x86, 0x99, 0xe2, 0x64, 0x73, 0xe3, 0xa1, 0x1c, 0x43, 0xc3, 0x23, 0x39, 0x86, 0x13, 0x8f, 0xe4, 0x18, 0x2f, 0x3c, 0x92, 0x63, 0x7c, 0xf0, 0x48, - 0x8e, 0x71, 0xc6, 0x63, 0x39, 0x86, 0x28, 0x56, 0xb0, 0xd7, 0x93, 0xd8, 0xc0, 0x0e, 0x36, 0x06, - 0x04, 0x00, 0x00, 0xff, 0xff, 0x47, 0x8a, 0x73, 0x02, 0x0a, 0x01, 0x00, 0x00, + 0x8e, 0x71, 0xc6, 0x63, 0x39, 0x86, 0x28, 0x25, 0x24, 0x1f, 0x41, 0xbc, 0x0c, 0xa5, 0x74, 0x8b, + 0x53, 0xb2, 0xf5, 0xc1, 0xe1, 0x92, 0xc4, 0x06, 0xf6, 0x8d, 0x31, 0x20, 0x00, 0x00, 0xff, 0xff, + 0xef, 0x4e, 0x92, 0x34, 0x27, 0x01, 0x00, 0x00, } func (m *Coin) Marshal() (dAtA []byte, err error) { diff --git a/types/codec.proto b/types/codec.proto index 228f9270ae28..a7275ed004ca 100644 --- a/types/codec.proto +++ b/types/codec.proto @@ -1,7 +1,7 @@ syntax = "proto3"; package cosmos_sdk.v1; -option go_package = "types"; +option go_package = "github.com/cosmos/cosmos-sdk/types"; option (gogoproto.goproto_stringer_all) = false; option (gogoproto.stringer_all) = false; option (gogoproto.marshaler_all) = true; diff --git a/x/auth/types/account.go b/x/auth/types/account.go index 69dc5be447d8..b3765181a569 100644 --- a/x/auth/types/account.go +++ b/x/auth/types/account.go @@ -19,26 +19,15 @@ import ( var _ exported.Account = (*BaseAccount)(nil) var _ exported.GenesisAccount = (*BaseAccount)(nil) -// BaseAccount - a base account structure. -// This can be extended by embedding within in your AppAccount. -// However one doesn't have to use BaseAccount as long as your struct -// implements Account. -type BaseAccount struct { - Address sdk.AccAddress `json:"address" yaml:"address"` - Coins sdk.Coins `json:"coins" yaml:"coins"` - PubKey crypto.PubKey `json:"public_key" yaml:"public_key"` - AccountNumber uint64 `json:"account_number" yaml:"account_number"` - Sequence uint64 `json:"sequence" yaml:"sequence"` -} - // NewBaseAccount creates a new BaseAccount object -func NewBaseAccount(address sdk.AccAddress, coins sdk.Coins, - pubKey crypto.PubKey, accountNumber uint64, sequence uint64) *BaseAccount { +func NewBaseAccount( + address sdk.AccAddress, coins sdk.Coins, pubKey crypto.PubKey, accountNumber, sequence uint64, +) *BaseAccount { return &BaseAccount{ Address: address, Coins: coins, - PubKey: pubKey, + PublicKey: pubKey.Bytes(), AccountNumber: accountNumber, Sequence: sequence, } @@ -56,11 +45,6 @@ func NewBaseAccountWithAddress(addr sdk.AccAddress) BaseAccount { } } -// GetAddress - Implements sdk.Account. -func (acc BaseAccount) GetAddress() sdk.AccAddress { - return acc.Address -} - // SetAddress - Implements sdk.Account. func (acc *BaseAccount) SetAddress(addr sdk.AccAddress) error { if len(acc.Address) != 0 { @@ -70,44 +54,24 @@ func (acc *BaseAccount) SetAddress(addr sdk.AccAddress) error { return nil } -// GetPubKey - Implements sdk.Account. -func (acc BaseAccount) GetPubKey() crypto.PubKey { - return acc.PubKey -} - // SetPubKey - Implements sdk.Account. func (acc *BaseAccount) SetPubKey(pubKey crypto.PubKey) error { acc.PubKey = pubKey return nil } -// GetCoins - Implements sdk.Account. -func (acc *BaseAccount) GetCoins() sdk.Coins { - return acc.Coins -} - // SetCoins - Implements sdk.Account. func (acc *BaseAccount) SetCoins(coins sdk.Coins) error { acc.Coins = coins return nil } -// GetAccountNumber - Implements Account -func (acc *BaseAccount) GetAccountNumber() uint64 { - return acc.AccountNumber -} - // SetAccountNumber - Implements Account func (acc *BaseAccount) SetAccountNumber(accNumber uint64) error { acc.AccountNumber = accNumber return nil } -// GetSequence - Implements sdk.Account. -func (acc *BaseAccount) GetSequence() uint64 { - return acc.Sequence -} - // SetSequence - Implements sdk.Account. func (acc *BaseAccount) SetSequence(seq uint64) error { acc.Sequence = seq diff --git a/x/auth/types/codec.go b/x/auth/types/codec.go index c6be8883f7ad..0cea551eac97 100644 --- a/x/auth/types/codec.go +++ b/x/auth/types/codec.go @@ -1,28 +1,28 @@ package types -import ( - "github.com/cosmos/cosmos-sdk/codec" - "github.com/cosmos/cosmos-sdk/x/auth/exported" -) +// import ( +// "github.com/cosmos/cosmos-sdk/codec" +// "github.com/cosmos/cosmos-sdk/x/auth/exported" +// ) -// ModuleCdc auth module wide codec -var ModuleCdc = codec.New() +// // ModuleCdc auth module wide codec +// var ModuleCdc = codec.New() -// RegisterCodec registers concrete types on the codec -func RegisterCodec(cdc *codec.Codec) { - cdc.RegisterInterface((*exported.GenesisAccount)(nil), nil) - cdc.RegisterInterface((*exported.Account)(nil), nil) - cdc.RegisterConcrete(&BaseAccount{}, "cosmos-sdk/Account", nil) - cdc.RegisterConcrete(StdTx{}, "cosmos-sdk/StdTx", nil) -} +// // RegisterCodec registers concrete types on the codec +// func RegisterCodec(cdc *codec.Codec) { +// cdc.RegisterInterface((*exported.GenesisAccount)(nil), nil) +// cdc.RegisterInterface((*exported.Account)(nil), nil) +// cdc.RegisterConcrete(&BaseAccount{}, "cosmos-sdk/Account", nil) +// cdc.RegisterConcrete(StdTx{}, "cosmos-sdk/StdTx", nil) +// } -// RegisterAccountTypeCodec registers an external account type defined in -// another module for the internal ModuleCdc. -func RegisterAccountTypeCodec(o interface{}, name string) { - ModuleCdc.RegisterConcrete(o, name, nil) -} +// // RegisterAccountTypeCodec registers an external account type defined in +// // another module for the internal ModuleCdc. +// func RegisterAccountTypeCodec(o interface{}, name string) { +// ModuleCdc.RegisterConcrete(o, name, nil) +// } -func init() { - RegisterCodec(ModuleCdc) - codec.RegisterCrypto(ModuleCdc) -} +// func init() { +// RegisterCodec(ModuleCdc) +// codec.RegisterCrypto(ModuleCdc) +// } diff --git a/x/auth/types/codec.pb.go b/x/auth/types/codec.pb.go new file mode 100644 index 000000000000..5e9d9d12f30f --- /dev/null +++ b/x/auth/types/codec.pb.go @@ -0,0 +1,540 @@ +// Code generated by protoc-gen-gogo. DO NOT EDIT. +// source: x/auth/types/codec.proto + +package types + +import ( + fmt "fmt" + crypto "github.com/cosmos/cosmos-sdk/crypto" + github_com_cosmos_cosmos_sdk_types "github.com/cosmos/cosmos-sdk/types" + types "github.com/cosmos/cosmos-sdk/types" + _ "github.com/gogo/protobuf/gogoproto" + proto "github.com/golang/protobuf/proto" + io "io" + math "math" + math_bits "math/bits" +) + +// Reference imports to suppress errors if they are not otherwise used. +var _ = proto.Marshal +var _ = fmt.Errorf +var _ = math.Inf + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the proto package it is being compiled against. +// A compilation error at this line likely means your copy of the +// proto package needs to be updated. +const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package + +// BaseAccount defines a base account message structure. This can be extended by +// embedding within another account type that extends functionality. A BaseAccount +// provides the basic functionality needed to operate an account. +type BaseAccount struct { + Address github_com_cosmos_cosmos_sdk_types.AccAddress `protobuf:"bytes,1,opt,name=address,proto3,casttype=github.com/cosmos/cosmos-sdk/types.AccAddress" json:"address,omitempty"` + Coins []types.Coin `protobuf:"bytes,2,rep,name=coins,proto3" json:"coins"` + PublicKey *crypto.PublicKey `protobuf:"bytes,3,opt,name=public_key,json=publicKey,proto3" json:"public_key,omitempty"` + AccountNumber uint64 `protobuf:"varint,4,opt,name=account_number,json=accountNumber,proto3" json:"account_number,omitempty"` + Sequence uint64 `protobuf:"varint,5,opt,name=sequence,proto3" json:"sequence,omitempty"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` +} + +func (m *BaseAccount) Reset() { *m = BaseAccount{} } +func (*BaseAccount) ProtoMessage() {} +func (*BaseAccount) Descriptor() ([]byte, []int) { + return fileDescriptor_e44d2a11716720ad, []int{0} +} +func (m *BaseAccount) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *BaseAccount) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_BaseAccount.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalToSizedBuffer(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (m *BaseAccount) XXX_Merge(src proto.Message) { + xxx_messageInfo_BaseAccount.Merge(m, src) +} +func (m *BaseAccount) XXX_Size() int { + return m.Size() +} +func (m *BaseAccount) XXX_DiscardUnknown() { + xxx_messageInfo_BaseAccount.DiscardUnknown(m) +} + +var xxx_messageInfo_BaseAccount proto.InternalMessageInfo + +func (m *BaseAccount) GetAddress() github_com_cosmos_cosmos_sdk_types.AccAddress { + if m != nil { + return m.Address + } + return nil +} + +func (m *BaseAccount) GetCoins() []types.Coin { + if m != nil { + return m.Coins + } + return nil +} + +func (m *BaseAccount) GetPublicKey() *crypto.PublicKey { + if m != nil { + return m.PublicKey + } + return nil +} + +func (m *BaseAccount) GetAccountNumber() uint64 { + if m != nil { + return m.AccountNumber + } + return 0 +} + +func (m *BaseAccount) GetSequence() uint64 { + if m != nil { + return m.Sequence + } + return 0 +} + +func init() { + proto.RegisterType((*BaseAccount)(nil), "cosmos_sdk.x.auth.v1.BaseAccount") +} + +func init() { proto.RegisterFile("x/auth/types/codec.proto", fileDescriptor_e44d2a11716720ad) } + +var fileDescriptor_e44d2a11716720ad = []byte{ + // 339 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x7c, 0x51, 0xbf, 0x4e, 0xf3, 0x30, + 0x1c, 0x8c, 0xfb, 0xe7, 0xfb, 0xc0, 0x05, 0x24, 0x0c, 0x43, 0x94, 0x21, 0x8d, 0x90, 0x90, 0xc2, + 0x50, 0x5b, 0x2d, 0xcc, 0x48, 0x4d, 0xc7, 0x4a, 0x08, 0x65, 0x64, 0xa9, 0x12, 0xc7, 0xb4, 0x51, + 0x69, 0x1c, 0xe2, 0x38, 0x6a, 0x36, 0x9e, 0x80, 0x99, 0x47, 0xea, 0xc8, 0xc8, 0x54, 0x41, 0x78, + 0x0b, 0x26, 0x54, 0xbb, 0x45, 0x91, 0x90, 0x98, 0xfc, 0xbb, 0xd3, 0xdd, 0xd9, 0xf7, 0x33, 0x34, + 0x97, 0x24, 0x90, 0xf9, 0x8c, 0xe4, 0x65, 0xca, 0x04, 0xa1, 0x3c, 0x62, 0x14, 0xa7, 0x19, 0xcf, + 0x39, 0x3a, 0xa5, 0x5c, 0x2c, 0xb8, 0x98, 0x88, 0x68, 0x8e, 0x97, 0x78, 0x23, 0xc2, 0x45, 0xdf, + 0xba, 0x2a, 0x58, 0x12, 0xf1, 0x8c, 0x4c, 0xe3, 0x7c, 0x26, 0x43, 0x4c, 0xf9, 0x82, 0x4c, 0xf9, + 0x94, 0x13, 0xe5, 0x09, 0xe5, 0xbd, 0x42, 0x0a, 0xa8, 0x49, 0x67, 0x59, 0xc7, 0xbf, 0xe2, 0x2d, + 0x44, 0xb3, 0x32, 0xcd, 0x79, 0x9d, 0x3b, 0x7b, 0x6e, 0xc0, 0x8e, 0x17, 0x08, 0x36, 0xa4, 0x94, + 0xcb, 0x24, 0x47, 0x63, 0xf8, 0x3f, 0x88, 0xa2, 0x8c, 0x09, 0x61, 0x02, 0x07, 0xb8, 0x07, 0x5e, + 0xff, 0x6b, 0xdd, 0xed, 0xd5, 0xae, 0xd6, 0x4f, 0xdc, 0x1e, 0x3d, 0x11, 0xcd, 0x75, 0x0f, 0x3c, + 0xa4, 0x74, 0xa8, 0x8d, 0xfe, 0x2e, 0x01, 0x11, 0xd8, 0xa6, 0x3c, 0x4e, 0x84, 0xd9, 0x70, 0x9a, + 0x6e, 0x67, 0x70, 0x82, 0x6b, 0xfd, 0x8a, 0x3e, 0x1e, 0xf1, 0x38, 0xf1, 0x5a, 0xab, 0x75, 0xd7, + 0xf0, 0xb5, 0x0e, 0x5d, 0x43, 0x98, 0xca, 0xf0, 0x21, 0xa6, 0x93, 0x39, 0x2b, 0xcd, 0xa6, 0x03, + 0xdc, 0xce, 0xa0, 0x5b, 0x77, 0xe9, 0x06, 0x1b, 0xf3, 0xad, 0xd2, 0x8d, 0x59, 0xe9, 0xef, 0xa7, + 0xbb, 0x11, 0x9d, 0xc3, 0xa3, 0x40, 0x17, 0x99, 0x24, 0x72, 0x11, 0xb2, 0xcc, 0x6c, 0x39, 0xc0, + 0x6d, 0xf9, 0x87, 0x5b, 0xf6, 0x46, 0x91, 0xc8, 0x82, 0x7b, 0x82, 0x3d, 0x4a, 0x96, 0x50, 0x66, + 0xb6, 0x95, 0xe0, 0x07, 0x7b, 0xa3, 0xb7, 0x0f, 0xdb, 0x78, 0xaa, 0x6c, 0x63, 0x55, 0xd9, 0xe0, + 0xb5, 0xb2, 0xc1, 0x7b, 0x65, 0x83, 0x97, 0x4f, 0xdb, 0xb8, 0xbb, 0xf8, 0x73, 0x0b, 0xf5, 0x4f, + 0x0d, 0xff, 0xa9, 0xe5, 0x5e, 0x7e, 0x07, 0x00, 0x00, 0xff, 0xff, 0x7c, 0x3d, 0x84, 0x92, 0xeb, + 0x01, 0x00, 0x00, +} + +func (m *BaseAccount) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalToSizedBuffer(dAtA[:size]) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *BaseAccount) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *BaseAccount) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + if m.XXX_unrecognized != nil { + i -= len(m.XXX_unrecognized) + copy(dAtA[i:], m.XXX_unrecognized) + } + if m.Sequence != 0 { + i = encodeVarintCodec(dAtA, i, uint64(m.Sequence)) + i-- + dAtA[i] = 0x28 + } + if m.AccountNumber != 0 { + i = encodeVarintCodec(dAtA, i, uint64(m.AccountNumber)) + i-- + dAtA[i] = 0x20 + } + if m.PublicKey != nil { + { + size, err := m.PublicKey.MarshalToSizedBuffer(dAtA[:i]) + if err != nil { + return 0, err + } + i -= size + i = encodeVarintCodec(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0x1a + } + if len(m.Coins) > 0 { + for iNdEx := len(m.Coins) - 1; iNdEx >= 0; iNdEx-- { + { + size, err := m.Coins[iNdEx].MarshalToSizedBuffer(dAtA[:i]) + if err != nil { + return 0, err + } + i -= size + i = encodeVarintCodec(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0x12 + } + } + if len(m.Address) > 0 { + i -= len(m.Address) + copy(dAtA[i:], m.Address) + i = encodeVarintCodec(dAtA, i, uint64(len(m.Address))) + i-- + dAtA[i] = 0xa + } + return len(dAtA) - i, nil +} + +func encodeVarintCodec(dAtA []byte, offset int, v uint64) int { + offset -= sovCodec(v) + base := offset + for v >= 1<<7 { + dAtA[offset] = uint8(v&0x7f | 0x80) + v >>= 7 + offset++ + } + dAtA[offset] = uint8(v) + return base +} +func (m *BaseAccount) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + l = len(m.Address) + if l > 0 { + n += 1 + l + sovCodec(uint64(l)) + } + if len(m.Coins) > 0 { + for _, e := range m.Coins { + l = e.Size() + n += 1 + l + sovCodec(uint64(l)) + } + } + if m.PublicKey != nil { + l = m.PublicKey.Size() + n += 1 + l + sovCodec(uint64(l)) + } + if m.AccountNumber != 0 { + n += 1 + sovCodec(uint64(m.AccountNumber)) + } + if m.Sequence != 0 { + n += 1 + sovCodec(uint64(m.Sequence)) + } + if m.XXX_unrecognized != nil { + n += len(m.XXX_unrecognized) + } + return n +} + +func sovCodec(x uint64) (n int) { + return (math_bits.Len64(x|1) + 6) / 7 +} +func sozCodec(x uint64) (n int) { + return sovCodec(uint64((x << 1) ^ uint64((int64(x) >> 63)))) +} +func (m *BaseAccount) Unmarshal(dAtA []byte) error { + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowCodec + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: BaseAccount: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: BaseAccount: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Address", wireType) + } + var byteLen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowCodec + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + byteLen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if byteLen < 0 { + return ErrInvalidLengthCodec + } + postIndex := iNdEx + byteLen + if postIndex < 0 { + return ErrInvalidLengthCodec + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.Address = append(m.Address[:0], dAtA[iNdEx:postIndex]...) + if m.Address == nil { + m.Address = []byte{} + } + iNdEx = postIndex + case 2: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Coins", wireType) + } + var msglen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowCodec + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + msglen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if msglen < 0 { + return ErrInvalidLengthCodec + } + postIndex := iNdEx + msglen + if postIndex < 0 { + return ErrInvalidLengthCodec + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.Coins = append(m.Coins, types.Coin{}) + if err := m.Coins[len(m.Coins)-1].Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + case 3: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field PublicKey", wireType) + } + var msglen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowCodec + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + msglen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if msglen < 0 { + return ErrInvalidLengthCodec + } + postIndex := iNdEx + msglen + if postIndex < 0 { + return ErrInvalidLengthCodec + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + if m.PublicKey == nil { + m.PublicKey = &crypto.PublicKey{} + } + if err := m.PublicKey.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + case 4: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field AccountNumber", wireType) + } + m.AccountNumber = 0 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowCodec + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + m.AccountNumber |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + case 5: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field Sequence", wireType) + } + m.Sequence = 0 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowCodec + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + m.Sequence |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + default: + iNdEx = preIndex + skippy, err := skipCodec(dAtA[iNdEx:]) + if err != nil { + return err + } + if skippy < 0 { + return ErrInvalidLengthCodec + } + if (iNdEx + skippy) < 0 { + return ErrInvalidLengthCodec + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + m.XXX_unrecognized = append(m.XXX_unrecognized, dAtA[iNdEx:iNdEx+skippy]...) + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} +func skipCodec(dAtA []byte) (n int, err error) { + l := len(dAtA) + iNdEx := 0 + depth := 0 + for iNdEx < l { + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowCodec + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= (uint64(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + wireType := int(wire & 0x7) + switch wireType { + case 0: + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowCodec + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + iNdEx++ + if dAtA[iNdEx-1] < 0x80 { + break + } + } + case 1: + iNdEx += 8 + case 2: + var length int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowCodec + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + length |= (int(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + if length < 0 { + return 0, ErrInvalidLengthCodec + } + iNdEx += length + case 3: + depth++ + case 4: + if depth == 0 { + return 0, ErrUnexpectedEndOfGroupCodec + } + depth-- + case 5: + iNdEx += 4 + default: + return 0, fmt.Errorf("proto: illegal wireType %d", wireType) + } + if iNdEx < 0 { + return 0, ErrInvalidLengthCodec + } + if depth == 0 { + return iNdEx, nil + } + } + return 0, io.ErrUnexpectedEOF +} + +var ( + ErrInvalidLengthCodec = fmt.Errorf("proto: negative length found during unmarshaling") + ErrIntOverflowCodec = fmt.Errorf("proto: integer overflow") + ErrUnexpectedEndOfGroupCodec = fmt.Errorf("proto: unexpected end of group") +) diff --git a/x/auth/types/codec.proto b/x/auth/types/codec.proto new file mode 100644 index 000000000000..af106a771946 --- /dev/null +++ b/x/auth/types/codec.proto @@ -0,0 +1,28 @@ +syntax = "proto3"; +package cosmos_sdk.x.auth.v1; + +import "vendor/github.com/gogo/protobuf/gogoproto/gogo.proto"; +import "types/codec.proto"; +import "crypto/codec.proto"; + +option go_package = "github.com/cosmos/cosmos-sdk/x/auth/types"; +option (gogoproto.goproto_stringer_all) = false; +option (gogoproto.stringer_all) = false; +option (gogoproto.marshaler_all) = true; +option (gogoproto.unmarshaler_all) = true; +option (gogoproto.sizer_all) = true; + +// TODO: +// +// 1. StdTx + +// BaseAccount defines a base account message structure. This can be extended by +// embedding within another account type that extends functionality. A BaseAccount +// provides the basic functionality needed to operate an account. +message BaseAccount { + bytes address = 1 [(gogoproto.casttype) = "github.com/cosmos/cosmos-sdk/types.AccAddress"]; + repeated cosmos_sdk.v1.Coin coins = 2 [(gogoproto.nullable) = false]; + cosmos_sdk.crypto.v1.PublicKey public_key = 3; + uint64 account_number = 4; + uint64 sequence = 5; +} \ No newline at end of file From 3af94498cbec1218be74e00f92bf0fe43f4ef02c Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Fri, 3 Jan 2020 10:47:57 -0500 Subject: [PATCH 27/40] Fix protocgen script --- scripts/protocgen.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/protocgen.sh b/scripts/protocgen.sh index 3e90d5d6eefb..ebe4ba0b96ac 100755 --- a/scripts/protocgen.sh +++ b/scripts/protocgen.sh @@ -8,6 +8,6 @@ for dir in $proto_dirs; do -I. \ -I${GOPATH}/src \ -Ivendor/github.com/gogo/protobuf/gogoproto \ - --gofast_out=. \ + --gofast_out=$GOPATH/src \ $(find "${dir}" -name '*.proto') done From f62cc360b61d6a64ed2a3da99cecc5fb1f5ba4ef Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Fri, 3 Jan 2020 10:49:25 -0500 Subject: [PATCH 28/40] Update types codec package name --- types/codec.pb.go | 9 +++++---- types/codec.proto | 2 +- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/types/codec.pb.go b/types/codec.pb.go index 5983add7382a..11f1c5a65894 100644 --- a/types/codec.pb.go +++ b/types/codec.pb.go @@ -137,7 +137,7 @@ func init() { func init() { proto.RegisterFile("types/codec.proto", fileDescriptor_97e5f89da6f0daf0) } var fileDescriptor_97e5f89da6f0daf0 = []byte{ - // 205 bytes of a gzipped FileDescriptorProto + // 216 bytes of a gzipped FileDescriptorProto 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x12, 0x2c, 0xa9, 0x2c, 0x48, 0x2d, 0xd6, 0x4f, 0xce, 0x4f, 0x49, 0x4d, 0xd6, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0xe2, 0x4d, 0xce, 0x2f, 0xce, 0xcd, 0x2f, 0x8e, 0x2f, 0x4e, 0xc9, 0xd6, 0x2b, 0x33, 0x94, 0x32, 0x29, 0x4b, @@ -147,10 +147,11 @@ var fileDescriptor_97e5f89da6f0daf0 = []byte{ 0x5e, 0x7e, 0xae, 0x04, 0xa3, 0x02, 0xa3, 0x06, 0x67, 0x10, 0x84, 0x23, 0xa4, 0xcc, 0xc5, 0x96, 0x98, 0x9b, 0x5f, 0x9a, 0x57, 0x22, 0xc1, 0xa4, 0xc0, 0xa8, 0xc1, 0xe3, 0xc4, 0x7d, 0xe2, 0x9e, 0x3c, 0xc3, 0xad, 0x7b, 0xf2, 0xcc, 0x9e, 0x79, 0x25, 0x41, 0x50, 0x29, 0x25, 0x17, 0x2e, 0x76, - 0x97, 0xd4, 0x64, 0x72, 0x4c, 0x71, 0x49, 0x4d, 0x86, 0x99, 0xe2, 0x24, 0x7f, 0xe3, 0xa1, 0x1c, + 0x97, 0xd4, 0x64, 0x72, 0x4c, 0x71, 0x49, 0x4d, 0x86, 0x99, 0xe2, 0x64, 0x73, 0xe3, 0xa1, 0x1c, 0x43, 0xc3, 0x23, 0x39, 0x86, 0x13, 0x8f, 0xe4, 0x18, 0x2f, 0x3c, 0x92, 0x63, 0x7c, 0xf0, 0x48, - 0x8e, 0x71, 0xc6, 0x63, 0x39, 0x86, 0x28, 0x56, 0xb0, 0xd7, 0x93, 0xd8, 0xc0, 0x0e, 0x36, 0x06, - 0x04, 0x00, 0x00, 0xff, 0xff, 0x47, 0x8a, 0x73, 0x02, 0x0a, 0x01, 0x00, 0x00, + 0x8e, 0x71, 0xc6, 0x63, 0x39, 0x86, 0x28, 0x25, 0x24, 0x1f, 0x41, 0xbc, 0x0c, 0xa5, 0x74, 0x8b, + 0x53, 0xb2, 0xf5, 0xc1, 0xe1, 0x92, 0xc4, 0x06, 0xf6, 0x8d, 0x31, 0x20, 0x00, 0x00, 0xff, 0xff, + 0xef, 0x4e, 0x92, 0x34, 0x27, 0x01, 0x00, 0x00, } func (m *Coin) Marshal() (dAtA []byte, err error) { diff --git a/types/codec.proto b/types/codec.proto index 228f9270ae28..a7275ed004ca 100644 --- a/types/codec.proto +++ b/types/codec.proto @@ -1,7 +1,7 @@ syntax = "proto3"; package cosmos_sdk.v1; -option go_package = "types"; +option go_package = "github.com/cosmos/cosmos-sdk/types"; option (gogoproto.goproto_stringer_all) = false; option (gogoproto.stringer_all) = false; option (gogoproto.marshaler_all) = true; From b7a67fe0137bc5eea7a493b99f22c3476e3e2c6f Mon Sep 17 00:00:00 2001 From: Aaron Craelius Date: Fri, 3 Jan 2020 11:45:22 -0500 Subject: [PATCH 29/40] Start integrating cosmos-proto (#5473) * Start integrating cosmos-proto * Comment out tendermint stuff that won't build --- Makefile | 1 + crypto/codec.pb.go | 288 ++------------------------------------- crypto/codec.proto | 19 +-- go.mod | 1 + go.sum | 2 + scripts/protocgen.sh | 7 +- types/codec.pb.go | 14 +- types/codec.proto | 3 - x/auth/types/codec.pb.go | 49 ++++--- x/auth/types/codec.proto | 3 - 10 files changed, 60 insertions(+), 327 deletions(-) diff --git a/Makefile b/Makefile index e0b02d93e961..695f31758681 100644 --- a/Makefile +++ b/Makefile @@ -231,6 +231,7 @@ proto-all: proto-gen proto-lint proto-check-breaking proto-gen: @go mod vendor + @go install github.com/regen-network/cosmos-proto/protoc-gen-gocosmos @./scripts/protocgen.sh proto-lint: diff --git a/crypto/codec.pb.go b/crypto/codec.pb.go index 06f67b5187e3..7df8f55185fd 100644 --- a/crypto/codec.pb.go +++ b/crypto/codec.pb.go @@ -6,7 +6,8 @@ package crypto import ( fmt "fmt" _ "github.com/gogo/protobuf/gogoproto" - proto "github.com/golang/protobuf/proto" + proto "github.com/gogo/protobuf/proto" + _ "github.com/regen-network/cosmos-proto" io "io" math "math" math_bits "math/bits" @@ -21,23 +22,16 @@ var _ = math.Inf // is compatible with the proto package it is being compiled against. // A compilation error at this line likely means your copy of the // proto package needs to be updated. -const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package +const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package // PublicKey defines a proto message with a single oneof field that represents a // public key as a byte slice. // // NOTE: Private keys must still be amino-encoded for backwards compatiblity. type PublicKey struct { - // pub defines an single supported public key type as a byte slice. - // - // Types that are valid to be assigned to Pub: - // *PublicKey_Secp256K1 - // *PublicKey_Ed25519 - // *PublicKey_Multisig - Pub isPublicKey_Pub `protobuf_oneof:"pub"` - XXX_NoUnkeyedLiteral struct{} `json:"-"` - XXX_unrecognized []byte `json:"-"` - XXX_sizecache int32 `json:"-"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` } func (m *PublicKey) Reset() { *m = PublicKey{} } @@ -73,63 +67,6 @@ func (m *PublicKey) XXX_DiscardUnknown() { var xxx_messageInfo_PublicKey proto.InternalMessageInfo -type isPublicKey_Pub interface { - isPublicKey_Pub() - MarshalTo([]byte) (int, error) - Size() int -} - -type PublicKey_Secp256K1 struct { - Secp256K1 []byte `protobuf:"bytes,1,opt,name=secp256k1,proto3,oneof" json:"secp256k1,omitempty"` -} -type PublicKey_Ed25519 struct { - Ed25519 []byte `protobuf:"bytes,2,opt,name=ed25519,proto3,oneof" json:"ed25519,omitempty"` -} -type PublicKey_Multisig struct { - Multisig []byte `protobuf:"bytes,3,opt,name=multisig,proto3,oneof" json:"multisig,omitempty"` -} - -func (*PublicKey_Secp256K1) isPublicKey_Pub() {} -func (*PublicKey_Ed25519) isPublicKey_Pub() {} -func (*PublicKey_Multisig) isPublicKey_Pub() {} - -func (m *PublicKey) GetPub() isPublicKey_Pub { - if m != nil { - return m.Pub - } - return nil -} - -func (m *PublicKey) GetSecp256K1() []byte { - if x, ok := m.GetPub().(*PublicKey_Secp256K1); ok { - return x.Secp256K1 - } - return nil -} - -func (m *PublicKey) GetEd25519() []byte { - if x, ok := m.GetPub().(*PublicKey_Ed25519); ok { - return x.Ed25519 - } - return nil -} - -func (m *PublicKey) GetMultisig() []byte { - if x, ok := m.GetPub().(*PublicKey_Multisig); ok { - return x.Multisig - } - return nil -} - -// XXX_OneofWrappers is for the internal use of the proto package. -func (*PublicKey) XXX_OneofWrappers() []interface{} { - return []interface{}{ - (*PublicKey_Secp256K1)(nil), - (*PublicKey_Ed25519)(nil), - (*PublicKey_Multisig)(nil), - } -} - func init() { proto.RegisterType((*PublicKey)(nil), "cosmos_sdk.crypto.v1.PublicKey") } @@ -137,21 +74,18 @@ func init() { func init() { proto.RegisterFile("crypto/codec.proto", fileDescriptor_0402281428f37664) } var fileDescriptor_0402281428f37664 = []byte{ - // 217 bytes of a gzipped FileDescriptorProto + // 174 bytes of a gzipped FileDescriptorProto 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x12, 0x4a, 0x2e, 0xaa, 0x2c, 0x28, 0xc9, 0xd7, 0x4f, 0xce, 0x4f, 0x49, 0x4d, 0xd6, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x12, 0x49, 0xce, 0x2f, 0xce, 0xcd, 0x2f, 0x8e, 0x2f, 0x4e, 0xc9, 0xd6, 0x83, 0x48, 0xeb, 0x95, 0x19, 0x4a, 0x99, 0x94, 0xa5, 0xe6, 0xa5, 0xe4, 0x17, 0xe9, 0xa7, 0x67, 0x96, 0x64, 0x94, 0x26, 0xe9, 0x25, 0xe7, 0xe7, 0xea, 0xa7, 0xe7, 0xa7, 0xe7, 0xeb, 0x83, 0xf5, 0x24, 0x95, 0xa6, 0x81, 0x79, - 0x60, 0x0e, 0x98, 0x05, 0x31, 0x4b, 0x29, 0x87, 0x8b, 0x33, 0xa0, 0x34, 0x29, 0x27, 0x33, 0xd9, - 0x3b, 0xb5, 0x52, 0x48, 0x8e, 0x8b, 0xb3, 0x38, 0x35, 0xb9, 0xc0, 0xc8, 0xd4, 0x2c, 0xdb, 0x50, - 0x82, 0x51, 0x81, 0x51, 0x83, 0xc7, 0x83, 0x21, 0x08, 0x21, 0x24, 0x24, 0xc5, 0xc5, 0x9e, 0x9a, - 0x62, 0x64, 0x6a, 0x6a, 0x68, 0x29, 0xc1, 0x04, 0x95, 0x85, 0x09, 0x08, 0xc9, 0x70, 0x71, 0xe4, - 0x96, 0xe6, 0x94, 0x64, 0x16, 0x67, 0xa6, 0x4b, 0x30, 0x43, 0x25, 0xe1, 0x22, 0x4e, 0xac, 0x5c, - 0xcc, 0x05, 0xa5, 0x49, 0x4e, 0xa6, 0x27, 0x1e, 0xc9, 0x31, 0x5e, 0x78, 0x24, 0xc7, 0xf8, 0xe0, - 0x91, 0x1c, 0xe3, 0x8c, 0xc7, 0x72, 0x0c, 0x51, 0xca, 0x48, 0xce, 0x85, 0x78, 0x0b, 0x4a, 0xe9, - 0x16, 0xa7, 0x64, 0xeb, 0x43, 0x7c, 0x97, 0xc4, 0x06, 0x76, 0xab, 0x31, 0x20, 0x00, 0x00, 0xff, - 0xff, 0x1d, 0x9c, 0xc5, 0xf1, 0x0d, 0x01, 0x00, 0x00, + 0x60, 0x0e, 0x98, 0x05, 0x31, 0x4b, 0xca, 0x12, 0x53, 0x57, 0x51, 0x6a, 0x7a, 0x6a, 0x9e, 0x6e, + 0x5e, 0x6a, 0x49, 0x79, 0x7e, 0x51, 0xb6, 0x3e, 0xc4, 0x2e, 0x5d, 0x88, 0x46, 0x08, 0x07, 0xa2, + 0x55, 0x89, 0x9b, 0x8b, 0x33, 0xa0, 0x34, 0x29, 0x27, 0x33, 0xd9, 0x3b, 0xb5, 0xd2, 0xc9, 0xf0, + 0xc4, 0x23, 0x39, 0xc6, 0x0b, 0x8f, 0xe4, 0x18, 0x1f, 0x3c, 0x92, 0x63, 0x8c, 0x52, 0x46, 0x32, + 0x0e, 0xa2, 0x07, 0x66, 0x4e, 0x71, 0x4a, 0xb6, 0x3e, 0xc4, 0xcd, 0x49, 0x6c, 0x60, 0x63, 0x8c, + 0x01, 0x01, 0x00, 0x00, 0xff, 0xff, 0xa9, 0xaf, 0xbc, 0x68, 0xe3, 0x00, 0x00, 0x00, } func (m *PublicKey) Marshal() (dAtA []byte, err error) { @@ -178,66 +112,9 @@ func (m *PublicKey) MarshalToSizedBuffer(dAtA []byte) (int, error) { i -= len(m.XXX_unrecognized) copy(dAtA[i:], m.XXX_unrecognized) } - if m.Pub != nil { - { - size := m.Pub.Size() - i -= size - if _, err := m.Pub.MarshalTo(dAtA[i:]); err != nil { - return 0, err - } - } - } - return len(dAtA) - i, nil -} - -func (m *PublicKey_Secp256K1) MarshalTo(dAtA []byte) (int, error) { - size := m.Size() - return m.MarshalToSizedBuffer(dAtA[:size]) -} - -func (m *PublicKey_Secp256K1) MarshalToSizedBuffer(dAtA []byte) (int, error) { - i := len(dAtA) - if m.Secp256K1 != nil { - i -= len(m.Secp256K1) - copy(dAtA[i:], m.Secp256K1) - i = encodeVarintCodec(dAtA, i, uint64(len(m.Secp256K1))) - i-- - dAtA[i] = 0xa - } return len(dAtA) - i, nil } -func (m *PublicKey_Ed25519) MarshalTo(dAtA []byte) (int, error) { - size := m.Size() - return m.MarshalToSizedBuffer(dAtA[:size]) -} -func (m *PublicKey_Ed25519) MarshalToSizedBuffer(dAtA []byte) (int, error) { - i := len(dAtA) - if m.Ed25519 != nil { - i -= len(m.Ed25519) - copy(dAtA[i:], m.Ed25519) - i = encodeVarintCodec(dAtA, i, uint64(len(m.Ed25519))) - i-- - dAtA[i] = 0x12 - } - return len(dAtA) - i, nil -} -func (m *PublicKey_Multisig) MarshalTo(dAtA []byte) (int, error) { - size := m.Size() - return m.MarshalToSizedBuffer(dAtA[:size]) -} - -func (m *PublicKey_Multisig) MarshalToSizedBuffer(dAtA []byte) (int, error) { - i := len(dAtA) - if m.Multisig != nil { - i -= len(m.Multisig) - copy(dAtA[i:], m.Multisig) - i = encodeVarintCodec(dAtA, i, uint64(len(m.Multisig))) - i-- - dAtA[i] = 0x1a - } - return len(dAtA) - i, nil -} func encodeVarintCodec(dAtA []byte, offset int, v uint64) int { offset -= sovCodec(v) base := offset @@ -255,52 +132,12 @@ func (m *PublicKey) Size() (n int) { } var l int _ = l - if m.Pub != nil { - n += m.Pub.Size() - } if m.XXX_unrecognized != nil { n += len(m.XXX_unrecognized) } return n } -func (m *PublicKey_Secp256K1) Size() (n int) { - if m == nil { - return 0 - } - var l int - _ = l - if m.Secp256K1 != nil { - l = len(m.Secp256K1) - n += 1 + l + sovCodec(uint64(l)) - } - return n -} -func (m *PublicKey_Ed25519) Size() (n int) { - if m == nil { - return 0 - } - var l int - _ = l - if m.Ed25519 != nil { - l = len(m.Ed25519) - n += 1 + l + sovCodec(uint64(l)) - } - return n -} -func (m *PublicKey_Multisig) Size() (n int) { - if m == nil { - return 0 - } - var l int - _ = l - if m.Multisig != nil { - l = len(m.Multisig) - n += 1 + l + sovCodec(uint64(l)) - } - return n -} - func sovCodec(x uint64) (n int) { return (math_bits.Len64(x|1) + 6) / 7 } @@ -336,105 +173,6 @@ func (m *PublicKey) Unmarshal(dAtA []byte) error { return fmt.Errorf("proto: PublicKey: illegal tag %d (wire type %d)", fieldNum, wire) } switch fieldNum { - case 1: - if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field Secp256K1", wireType) - } - var byteLen int - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowCodec - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - byteLen |= int(b&0x7F) << shift - if b < 0x80 { - break - } - } - if byteLen < 0 { - return ErrInvalidLengthCodec - } - postIndex := iNdEx + byteLen - if postIndex < 0 { - return ErrInvalidLengthCodec - } - if postIndex > l { - return io.ErrUnexpectedEOF - } - v := make([]byte, postIndex-iNdEx) - copy(v, dAtA[iNdEx:postIndex]) - m.Pub = &PublicKey_Secp256K1{v} - iNdEx = postIndex - case 2: - if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field Ed25519", wireType) - } - var byteLen int - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowCodec - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - byteLen |= int(b&0x7F) << shift - if b < 0x80 { - break - } - } - if byteLen < 0 { - return ErrInvalidLengthCodec - } - postIndex := iNdEx + byteLen - if postIndex < 0 { - return ErrInvalidLengthCodec - } - if postIndex > l { - return io.ErrUnexpectedEOF - } - v := make([]byte, postIndex-iNdEx) - copy(v, dAtA[iNdEx:postIndex]) - m.Pub = &PublicKey_Ed25519{v} - iNdEx = postIndex - case 3: - if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field Multisig", wireType) - } - var byteLen int - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowCodec - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - byteLen |= int(b&0x7F) << shift - if b < 0x80 { - break - } - } - if byteLen < 0 { - return ErrInvalidLengthCodec - } - postIndex := iNdEx + byteLen - if postIndex < 0 { - return ErrInvalidLengthCodec - } - if postIndex > l { - return io.ErrUnexpectedEOF - } - v := make([]byte, postIndex-iNdEx) - copy(v, dAtA[iNdEx:postIndex]) - m.Pub = &PublicKey_Multisig{v} - iNdEx = postIndex default: iNdEx = preIndex skippy, err := skipCodec(dAtA[iNdEx:]) diff --git a/crypto/codec.proto b/crypto/codec.proto index 32c26e1a0186..099daf7e5817 100644 --- a/crypto/codec.proto +++ b/crypto/codec.proto @@ -2,21 +2,22 @@ syntax = "proto3"; package cosmos_sdk.crypto.v1; import "vendor/github.com/gogo/protobuf/gogoproto/gogo.proto"; +import "vendor/github.com/regen-network/cosmos-proto/cosmos.proto"; option go_package = "github.com/cosmos/cosmos-sdk/crypto"; -option (gogoproto.marshaler_all) = true; -option (gogoproto.unmarshaler_all) = true; -option (gogoproto.sizer_all) = true; // PublicKey defines a proto message with a single oneof field that represents a // public key as a byte slice. // // NOTE: Private keys must still be amino-encoded for backwards compatiblity. message PublicKey { - // pub defines an single supported public key type as a byte slice. - oneof pub { - bytes secp256k1 = 1; - bytes ed25519 = 2; - bytes multisig = 3; - } +// option (cosmos_proto.interface_type) = "github.com/tendermint/tendermint/crypto.PubKey"; +// // pub defines an single supported public key type as a byte slice. +// oneof pub { +// // TODO make tendermint keys use []byte instead of [32]byte or generate the interface marshaling manually +// bytes secp256k1 = 1 [(gogoproto.casttype) = "github.com/tendermint/tendermint/crypto/secp256k1.PubKeySecp256k1"]; +// bytes ed25519 = 2 [(gogoproto.casttype) = "github.com/tendermint/tendermint/crypto/ed25519.PubKeyEd25519"]; +// // TODO define multisig as a proto message in tendermint: +// bytes multisig = 3; +// } } diff --git a/go.mod b/go.mod index bb0a000b84a3..95496bbd0361 100644 --- a/go.mod +++ b/go.mod @@ -16,6 +16,7 @@ require ( github.com/pelletier/go-toml v1.6.0 github.com/pkg/errors v0.8.1 github.com/rakyll/statik v0.1.6 + github.com/regen-network/cosmos-proto v0.0.0-20200103153758-711a23958115 github.com/spf13/afero v1.2.1 // indirect github.com/spf13/cobra v0.0.5 github.com/spf13/jwalterweatherman v1.1.0 // indirect diff --git a/go.sum b/go.sum index cc5d9b873f77..8e8ee0297870 100644 --- a/go.sum +++ b/go.sum @@ -199,6 +199,8 @@ github.com/rakyll/statik v0.1.6 h1:uICcfUXpgqtw2VopbIncslhAmE5hwc4g20TEyEENBNs= github.com/rakyll/statik v0.1.6/go.mod h1:OEi9wJV/fMUAGx1eNjq75DKDsJVuEv1U0oYdX6GX8Zs= github.com/rcrowley/go-metrics v0.0.0-20180503174638-e2704e165165 h1:nkcn14uNmFEuGCb2mBZbBb24RdNRL08b/wb+xBOYpuk= github.com/rcrowley/go-metrics v0.0.0-20180503174638-e2704e165165/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4= +github.com/regen-network/cosmos-proto v0.0.0-20200103153758-711a23958115 h1:ML7849Fm5pC076eQ/mfYSqHf9IlUH+7DUW0hQf0tk7M= +github.com/regen-network/cosmos-proto v0.0.0-20200103153758-711a23958115/go.mod h1:z+OxEChBwVwsOhJeKyvS6WqPRFDV/w2hhwDF6qpi4dA= github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg= github.com/rs/cors v1.6.0 h1:G9tHG9lebljV9mfp9SNPDL36nCDxmo3zTlAf1YgvzmI= github.com/rs/cors v1.6.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU= diff --git a/scripts/protocgen.sh b/scripts/protocgen.sh index ebe4ba0b96ac..b2f46670dad4 100755 --- a/scripts/protocgen.sh +++ b/scripts/protocgen.sh @@ -4,10 +4,7 @@ set -eo pipefail proto_dirs=$(find . -path ./vendor -prune -o -name '*.proto' -print0 | xargs -0 -n1 dirname | sort | uniq) for dir in $proto_dirs; do - protoc -I/usr/local/include \ - -I. \ - -I${GOPATH}/src \ - -Ivendor/github.com/gogo/protobuf/gogoproto \ - --gofast_out=$GOPATH/src \ + protoc -I. \ + --gocosmos_out=plugins=interfacetype,paths=source_relative:. \ $(find "${dir}" -name '*.proto') done diff --git a/types/codec.pb.go b/types/codec.pb.go index 11f1c5a65894..f4a3b87b9112 100644 --- a/types/codec.pb.go +++ b/types/codec.pb.go @@ -6,7 +6,7 @@ package types import ( fmt "fmt" _ "github.com/gogo/protobuf/gogoproto" - proto "github.com/golang/protobuf/proto" + proto "github.com/gogo/protobuf/proto" io "io" math "math" math_bits "math/bits" @@ -21,7 +21,7 @@ var _ = math.Inf // is compatible with the proto package it is being compiled against. // A compilation error at this line likely means your copy of the // proto package needs to be updated. -const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package +const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package // Coin defines a token with a denomination and an amount. // @@ -137,7 +137,7 @@ func init() { func init() { proto.RegisterFile("types/codec.proto", fileDescriptor_97e5f89da6f0daf0) } var fileDescriptor_97e5f89da6f0daf0 = []byte{ - // 216 bytes of a gzipped FileDescriptorProto + // 212 bytes of a gzipped FileDescriptorProto 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x12, 0x2c, 0xa9, 0x2c, 0x48, 0x2d, 0xd6, 0x4f, 0xce, 0x4f, 0x49, 0x4d, 0xd6, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0xe2, 0x4d, 0xce, 0x2f, 0xce, 0xcd, 0x2f, 0x8e, 0x2f, 0x4e, 0xc9, 0xd6, 0x2b, 0x33, 0x94, 0x32, 0x29, 0x4b, @@ -147,11 +147,11 @@ var fileDescriptor_97e5f89da6f0daf0 = []byte{ 0x5e, 0x7e, 0xae, 0x04, 0xa3, 0x02, 0xa3, 0x06, 0x67, 0x10, 0x84, 0x23, 0xa4, 0xcc, 0xc5, 0x96, 0x98, 0x9b, 0x5f, 0x9a, 0x57, 0x22, 0xc1, 0xa4, 0xc0, 0xa8, 0xc1, 0xe3, 0xc4, 0x7d, 0xe2, 0x9e, 0x3c, 0xc3, 0xad, 0x7b, 0xf2, 0xcc, 0x9e, 0x79, 0x25, 0x41, 0x50, 0x29, 0x25, 0x17, 0x2e, 0x76, - 0x97, 0xd4, 0x64, 0x72, 0x4c, 0x71, 0x49, 0x4d, 0x86, 0x99, 0xe2, 0x64, 0x73, 0xe3, 0xa1, 0x1c, + 0x97, 0xd4, 0x64, 0x72, 0x4c, 0x71, 0x49, 0x4d, 0x86, 0x99, 0xe2, 0x64, 0x71, 0xe3, 0xa1, 0x1c, 0x43, 0xc3, 0x23, 0x39, 0x86, 0x13, 0x8f, 0xe4, 0x18, 0x2f, 0x3c, 0x92, 0x63, 0x7c, 0xf0, 0x48, - 0x8e, 0x71, 0xc6, 0x63, 0x39, 0x86, 0x28, 0x25, 0x24, 0x1f, 0x41, 0xbc, 0x0c, 0xa5, 0x74, 0x8b, - 0x53, 0xb2, 0xf5, 0xc1, 0xe1, 0x92, 0xc4, 0x06, 0xf6, 0x8d, 0x31, 0x20, 0x00, 0x00, 0xff, 0xff, - 0xef, 0x4e, 0x92, 0x34, 0x27, 0x01, 0x00, 0x00, + 0x8e, 0x31, 0x4a, 0x09, 0xc9, 0x37, 0x10, 0xef, 0x42, 0x29, 0xdd, 0xe2, 0x94, 0x6c, 0x7d, 0x70, + 0x98, 0x24, 0xb1, 0x81, 0x7d, 0x62, 0x0c, 0x08, 0x00, 0x00, 0xff, 0xff, 0xba, 0x45, 0x2c, 0xa6, + 0x23, 0x01, 0x00, 0x00, } func (m *Coin) Marshal() (dAtA []byte, err error) { diff --git a/types/codec.proto b/types/codec.proto index a7275ed004ca..8732c9dc1cf7 100644 --- a/types/codec.proto +++ b/types/codec.proto @@ -4,9 +4,6 @@ package cosmos_sdk.v1; option go_package = "github.com/cosmos/cosmos-sdk/types"; option (gogoproto.goproto_stringer_all) = false; option (gogoproto.stringer_all) = false; -option (gogoproto.marshaler_all) = true; -option (gogoproto.unmarshaler_all) = true; -option (gogoproto.sizer_all) = true; import "vendor/github.com/gogo/protobuf/gogoproto/gogo.proto"; diff --git a/x/auth/types/codec.pb.go b/x/auth/types/codec.pb.go index 5e9d9d12f30f..6c0f52c2af17 100644 --- a/x/auth/types/codec.pb.go +++ b/x/auth/types/codec.pb.go @@ -9,7 +9,7 @@ import ( github_com_cosmos_cosmos_sdk_types "github.com/cosmos/cosmos-sdk/types" types "github.com/cosmos/cosmos-sdk/types" _ "github.com/gogo/protobuf/gogoproto" - proto "github.com/golang/protobuf/proto" + proto "github.com/gogo/protobuf/proto" io "io" math "math" math_bits "math/bits" @@ -24,7 +24,7 @@ var _ = math.Inf // is compatible with the proto package it is being compiled against. // A compilation error at this line likely means your copy of the // proto package needs to be updated. -const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package +const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package // BaseAccount defines a base account message structure. This can be extended by // embedding within another account type that extends functionality. A BaseAccount @@ -114,29 +114,28 @@ func init() { func init() { proto.RegisterFile("x/auth/types/codec.proto", fileDescriptor_e44d2a11716720ad) } var fileDescriptor_e44d2a11716720ad = []byte{ - // 339 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x7c, 0x51, 0xbf, 0x4e, 0xf3, 0x30, - 0x1c, 0x8c, 0xfb, 0xe7, 0xfb, 0xc0, 0x05, 0x24, 0x0c, 0x43, 0x94, 0x21, 0x8d, 0x90, 0x90, 0xc2, - 0x50, 0x5b, 0x2d, 0xcc, 0x48, 0x4d, 0xc7, 0x4a, 0x08, 0x65, 0x64, 0xa9, 0x12, 0xc7, 0xb4, 0x51, - 0x69, 0x1c, 0xe2, 0x38, 0x6a, 0x36, 0x9e, 0x80, 0x99, 0x47, 0xea, 0xc8, 0xc8, 0x54, 0x41, 0x78, - 0x0b, 0x26, 0x54, 0xbb, 0x45, 0x91, 0x90, 0x98, 0xfc, 0xbb, 0xd3, 0xdd, 0xd9, 0xf7, 0x33, 0x34, - 0x97, 0x24, 0x90, 0xf9, 0x8c, 0xe4, 0x65, 0xca, 0x04, 0xa1, 0x3c, 0x62, 0x14, 0xa7, 0x19, 0xcf, - 0x39, 0x3a, 0xa5, 0x5c, 0x2c, 0xb8, 0x98, 0x88, 0x68, 0x8e, 0x97, 0x78, 0x23, 0xc2, 0x45, 0xdf, - 0xba, 0x2a, 0x58, 0x12, 0xf1, 0x8c, 0x4c, 0xe3, 0x7c, 0x26, 0x43, 0x4c, 0xf9, 0x82, 0x4c, 0xf9, - 0x94, 0x13, 0xe5, 0x09, 0xe5, 0xbd, 0x42, 0x0a, 0xa8, 0x49, 0x67, 0x59, 0xc7, 0xbf, 0xe2, 0x2d, - 0x44, 0xb3, 0x32, 0xcd, 0x79, 0x9d, 0x3b, 0x7b, 0x6e, 0xc0, 0x8e, 0x17, 0x08, 0x36, 0xa4, 0x94, - 0xcb, 0x24, 0x47, 0x63, 0xf8, 0x3f, 0x88, 0xa2, 0x8c, 0x09, 0x61, 0x02, 0x07, 0xb8, 0x07, 0x5e, - 0xff, 0x6b, 0xdd, 0xed, 0xd5, 0xae, 0xd6, 0x4f, 0xdc, 0x1e, 0x3d, 0x11, 0xcd, 0x75, 0x0f, 0x3c, - 0xa4, 0x74, 0xa8, 0x8d, 0xfe, 0x2e, 0x01, 0x11, 0xd8, 0xa6, 0x3c, 0x4e, 0x84, 0xd9, 0x70, 0x9a, - 0x6e, 0x67, 0x70, 0x82, 0x6b, 0xfd, 0x8a, 0x3e, 0x1e, 0xf1, 0x38, 0xf1, 0x5a, 0xab, 0x75, 0xd7, - 0xf0, 0xb5, 0x0e, 0x5d, 0x43, 0x98, 0xca, 0xf0, 0x21, 0xa6, 0x93, 0x39, 0x2b, 0xcd, 0xa6, 0x03, - 0xdc, 0xce, 0xa0, 0x5b, 0x77, 0xe9, 0x06, 0x1b, 0xf3, 0xad, 0xd2, 0x8d, 0x59, 0xe9, 0xef, 0xa7, - 0xbb, 0x11, 0x9d, 0xc3, 0xa3, 0x40, 0x17, 0x99, 0x24, 0x72, 0x11, 0xb2, 0xcc, 0x6c, 0x39, 0xc0, - 0x6d, 0xf9, 0x87, 0x5b, 0xf6, 0x46, 0x91, 0xc8, 0x82, 0x7b, 0x82, 0x3d, 0x4a, 0x96, 0x50, 0x66, - 0xb6, 0x95, 0xe0, 0x07, 0x7b, 0xa3, 0xb7, 0x0f, 0xdb, 0x78, 0xaa, 0x6c, 0x63, 0x55, 0xd9, 0xe0, - 0xb5, 0xb2, 0xc1, 0x7b, 0x65, 0x83, 0x97, 0x4f, 0xdb, 0xb8, 0xbb, 0xf8, 0x73, 0x0b, 0xf5, 0x4f, - 0x0d, 0xff, 0xa9, 0xe5, 0x5e, 0x7e, 0x07, 0x00, 0x00, 0xff, 0xff, 0x7c, 0x3d, 0x84, 0x92, 0xeb, - 0x01, 0x00, 0x00, + // 335 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x7c, 0x51, 0x3d, 0x4f, 0xf3, 0x30, + 0x18, 0xac, 0xfb, 0xf1, 0x7e, 0xb8, 0xef, 0x8b, 0x84, 0x61, 0x88, 0x32, 0xa4, 0x11, 0x12, 0x52, + 0x18, 0x6a, 0xab, 0x85, 0x19, 0xd4, 0x30, 0x56, 0x42, 0x28, 0x23, 0x4b, 0x95, 0x38, 0xa6, 0x8d, + 0x4a, 0xf3, 0x84, 0x38, 0xae, 0x9a, 0x8d, 0x5f, 0xc0, 0xef, 0xea, 0xc8, 0xc8, 0x54, 0x41, 0x7e, + 0x06, 0x13, 0xaa, 0xdd, 0xa2, 0x48, 0x48, 0x4c, 0x7e, 0xee, 0x74, 0x77, 0xf6, 0x3d, 0xc6, 0xd6, + 0x8a, 0x85, 0xaa, 0x98, 0xb1, 0xa2, 0xcc, 0x84, 0x64, 0x1c, 0x62, 0xc1, 0x69, 0x96, 0x43, 0x01, + 0xe4, 0x98, 0x83, 0x5c, 0x80, 0x9c, 0xc8, 0x78, 0x4e, 0x57, 0x74, 0x2b, 0xa2, 0xcb, 0x81, 0x7d, + 0xb1, 0x14, 0x69, 0x0c, 0x39, 0x9b, 0x26, 0xc5, 0x4c, 0x45, 0x94, 0xc3, 0x82, 0x4d, 0x61, 0x0a, + 0x4c, 0x7b, 0x22, 0x75, 0xaf, 0x91, 0x06, 0x7a, 0x32, 0x59, 0xf6, 0xe1, 0xb7, 0x78, 0x9b, 0xf0, + 0xbc, 0xcc, 0x0a, 0xa8, 0x73, 0x27, 0xcf, 0x4d, 0xdc, 0xf5, 0x43, 0x29, 0x46, 0x9c, 0x83, 0x4a, + 0x0b, 0x32, 0xc6, 0xbf, 0xc3, 0x38, 0xce, 0x85, 0x94, 0x16, 0x72, 0x91, 0xf7, 0xcf, 0x1f, 0x7c, + 0x6c, 0x7a, 0xfd, 0xda, 0xd5, 0xe6, 0x89, 0xbb, 0xa3, 0x2f, 0xe3, 0xb9, 0xe9, 0x41, 0x47, 0x9c, + 0x8f, 0x8c, 0x31, 0xd8, 0x27, 0x10, 0x86, 0x3b, 0x1c, 0x92, 0x54, 0x5a, 0x4d, 0xb7, 0xe5, 0x75, + 0x87, 0x47, 0xb4, 0xd6, 0x6f, 0x39, 0xa0, 0xd7, 0x90, 0xa4, 0x7e, 0x7b, 0xbd, 0xe9, 0x35, 0x02, + 0xa3, 0x23, 0x97, 0x18, 0x67, 0x2a, 0x7a, 0x48, 0xf8, 0x64, 0x2e, 0x4a, 0xab, 0xe5, 0x22, 0xaf, + 0x3b, 0xec, 0xd5, 0x5d, 0xa6, 0xc1, 0xd6, 0x7c, 0xab, 0x75, 0x63, 0x51, 0x06, 0x7f, 0xb3, 0xfd, + 0x48, 0x4e, 0xf1, 0x41, 0x68, 0x8a, 0x4c, 0x52, 0xb5, 0x88, 0x44, 0x6e, 0xb5, 0x5d, 0xe4, 0xb5, + 0x83, 0xff, 0x3b, 0xf6, 0x46, 0x93, 0xc4, 0xc6, 0x7f, 0xa4, 0x78, 0x54, 0x22, 0xe5, 0xc2, 0xea, + 0x68, 0xc1, 0x17, 0xf6, 0xaf, 0x5e, 0xdf, 0x9d, 0xc6, 0x53, 0xe5, 0x34, 0xd6, 0x95, 0x83, 0x5e, + 0x2a, 0x07, 0xbd, 0x55, 0x0e, 0xba, 0x3b, 0xfb, 0x71, 0x03, 0xf5, 0x0f, 0x8d, 0x7e, 0xe9, 0xc5, + 0x9e, 0x7f, 0x06, 0x00, 0x00, 0xff, 0xff, 0x51, 0x92, 0x91, 0xe2, 0xe7, 0x01, 0x00, 0x00, } func (m *BaseAccount) Marshal() (dAtA []byte, err error) { diff --git a/x/auth/types/codec.proto b/x/auth/types/codec.proto index af106a771946..de15b9e776bb 100644 --- a/x/auth/types/codec.proto +++ b/x/auth/types/codec.proto @@ -8,9 +8,6 @@ import "crypto/codec.proto"; option go_package = "github.com/cosmos/cosmos-sdk/x/auth/types"; option (gogoproto.goproto_stringer_all) = false; option (gogoproto.stringer_all) = false; -option (gogoproto.marshaler_all) = true; -option (gogoproto.unmarshaler_all) = true; -option (gogoproto.sizer_all) = true; // TODO: // From 7bd01713745594e624623ae9fc755f6102cd90c9 Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Fri, 3 Jan 2020 11:46:04 -0500 Subject: [PATCH 30/40] Update auth codec --- x/auth/types/account.go | 47 ++++++++++++++- x/auth/types/account_retriever.go | 2 +- x/auth/types/account_retriever_test.go | 2 +- x/auth/types/codec.pb.go | 81 ++++++++------------------ x/auth/types/codec.proto | 2 + 5 files changed, 71 insertions(+), 63 deletions(-) diff --git a/x/auth/types/account.go b/x/auth/types/account.go index b3765181a569..14ef21f73a2e 100644 --- a/x/auth/types/account.go +++ b/x/auth/types/account.go @@ -6,7 +6,8 @@ import ( "errors" "time" - "github.com/tendermint/tendermint/crypto" + "github.com/cosmos/cosmos-sdk/crypto" + tmcrypto "github.com/tendermint/tendermint/crypto" yaml "gopkg.in/yaml.v2" sdk "github.com/cosmos/cosmos-sdk/types" @@ -21,7 +22,7 @@ var _ exported.GenesisAccount = (*BaseAccount)(nil) // NewBaseAccount creates a new BaseAccount object func NewBaseAccount( - address sdk.AccAddress, coins sdk.Coins, pubKey crypto.PubKey, accountNumber, sequence uint64, + address sdk.AccAddress, coins sdk.Coins, pubKey tmcrypto.PubKey, accountNumber, sequence uint64, ) *BaseAccount { return &BaseAccount{ @@ -45,6 +46,46 @@ func NewBaseAccountWithAddress(addr sdk.AccAddress) BaseAccount { } } +// GetAddress returns the account's address. +func (m *BaseAccount) GetAddress() sdk.AccAddress { + if m != nil { + return m.Address + } + return nil +} + +// GetCoins returns the account's coins. +func (m *BaseAccount) GetCoins() sdk.Coins { + if m != nil { + return m.Coins + } + return nil +} + +// GetPubKey return's the account's public key. +func (m *BaseAccount) GetPubKey() *crypto.PublicKey { + if m != nil { + return m.PublicKey + } + return nil +} + +// GetAccountNumber returns the account's account number. +func (m *BaseAccount) GetAccountNumber() uint64 { + if m != nil { + return m.AccountNumber + } + return 0 +} + +// GetSequence return's the account's sequence (nonce). +func (m *BaseAccount) GetSequence() uint64 { + if m != nil { + return m.Sequence + } + return 0 +} + // SetAddress - Implements sdk.Account. func (acc *BaseAccount) SetAddress(addr sdk.AccAddress) error { if len(acc.Address) != 0 { @@ -55,7 +96,7 @@ func (acc *BaseAccount) SetAddress(addr sdk.AccAddress) error { } // SetPubKey - Implements sdk.Account. -func (acc *BaseAccount) SetPubKey(pubKey crypto.PubKey) error { +func (acc *BaseAccount) SetPubKey(pubKey tmcrypto.PubKey) error { acc.PubKey = pubKey return nil } diff --git a/x/auth/types/account_retriever.go b/x/auth/types/account_retriever.go index ab87e3259b95..d9abe86f7eba 100644 --- a/x/auth/types/account_retriever.go +++ b/x/auth/types/account_retriever.go @@ -37,7 +37,7 @@ func (ar AccountRetriever) GetAccount(addr sdk.AccAddress) (exported.Account, er // height of the query with the account. An error is returned if the query // or decoding fails. func (ar AccountRetriever) GetAccountWithHeight(addr sdk.AccAddress) (exported.Account, int64, error) { - bs, err := ModuleCdc.MarshalJSON(NewQueryAccountParams(addr)) + bs, err := json.MarshalJSON(NewQueryAccountParams(addr)) if err != nil { return nil, 0, err } diff --git a/x/auth/types/account_retriever_test.go b/x/auth/types/account_retriever_test.go index 76126db115d3..8fe9bdbadd04 100644 --- a/x/auth/types/account_retriever_test.go +++ b/x/auth/types/account_retriever_test.go @@ -19,7 +19,7 @@ func TestAccountRetriever(t *testing.T) { mockNodeQuerier := mocks.NewMockNodeQuerier(mockCtrl) accRetr := NewAccountRetriever(mockNodeQuerier) addr := []byte("test") - bs, err := ModuleCdc.MarshalJSON(NewQueryAccountParams(addr)) + bs, err := json.MarshalJSON(NewQueryAccountParams(addr)) require.NoError(t, err) mockNodeQuerier.EXPECT().QueryWithData(gomock.Eq("custom/acc/account"), diff --git a/x/auth/types/codec.pb.go b/x/auth/types/codec.pb.go index 5e9d9d12f30f..d15d045c9d19 100644 --- a/x/auth/types/codec.pb.go +++ b/x/auth/types/codec.pb.go @@ -72,41 +72,6 @@ func (m *BaseAccount) XXX_DiscardUnknown() { var xxx_messageInfo_BaseAccount proto.InternalMessageInfo -func (m *BaseAccount) GetAddress() github_com_cosmos_cosmos_sdk_types.AccAddress { - if m != nil { - return m.Address - } - return nil -} - -func (m *BaseAccount) GetCoins() []types.Coin { - if m != nil { - return m.Coins - } - return nil -} - -func (m *BaseAccount) GetPublicKey() *crypto.PublicKey { - if m != nil { - return m.PublicKey - } - return nil -} - -func (m *BaseAccount) GetAccountNumber() uint64 { - if m != nil { - return m.AccountNumber - } - return 0 -} - -func (m *BaseAccount) GetSequence() uint64 { - if m != nil { - return m.Sequence - } - return 0 -} - func init() { proto.RegisterType((*BaseAccount)(nil), "cosmos_sdk.x.auth.v1.BaseAccount") } @@ -114,29 +79,29 @@ func init() { func init() { proto.RegisterFile("x/auth/types/codec.proto", fileDescriptor_e44d2a11716720ad) } var fileDescriptor_e44d2a11716720ad = []byte{ - // 339 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x7c, 0x51, 0xbf, 0x4e, 0xf3, 0x30, - 0x1c, 0x8c, 0xfb, 0xe7, 0xfb, 0xc0, 0x05, 0x24, 0x0c, 0x43, 0x94, 0x21, 0x8d, 0x90, 0x90, 0xc2, - 0x50, 0x5b, 0x2d, 0xcc, 0x48, 0x4d, 0xc7, 0x4a, 0x08, 0x65, 0x64, 0xa9, 0x12, 0xc7, 0xb4, 0x51, - 0x69, 0x1c, 0xe2, 0x38, 0x6a, 0x36, 0x9e, 0x80, 0x99, 0x47, 0xea, 0xc8, 0xc8, 0x54, 0x41, 0x78, - 0x0b, 0x26, 0x54, 0xbb, 0x45, 0x91, 0x90, 0x98, 0xfc, 0xbb, 0xd3, 0xdd, 0xd9, 0xf7, 0x33, 0x34, - 0x97, 0x24, 0x90, 0xf9, 0x8c, 0xe4, 0x65, 0xca, 0x04, 0xa1, 0x3c, 0x62, 0x14, 0xa7, 0x19, 0xcf, - 0x39, 0x3a, 0xa5, 0x5c, 0x2c, 0xb8, 0x98, 0x88, 0x68, 0x8e, 0x97, 0x78, 0x23, 0xc2, 0x45, 0xdf, - 0xba, 0x2a, 0x58, 0x12, 0xf1, 0x8c, 0x4c, 0xe3, 0x7c, 0x26, 0x43, 0x4c, 0xf9, 0x82, 0x4c, 0xf9, - 0x94, 0x13, 0xe5, 0x09, 0xe5, 0xbd, 0x42, 0x0a, 0xa8, 0x49, 0x67, 0x59, 0xc7, 0xbf, 0xe2, 0x2d, - 0x44, 0xb3, 0x32, 0xcd, 0x79, 0x9d, 0x3b, 0x7b, 0x6e, 0xc0, 0x8e, 0x17, 0x08, 0x36, 0xa4, 0x94, - 0xcb, 0x24, 0x47, 0x63, 0xf8, 0x3f, 0x88, 0xa2, 0x8c, 0x09, 0x61, 0x02, 0x07, 0xb8, 0x07, 0x5e, - 0xff, 0x6b, 0xdd, 0xed, 0xd5, 0xae, 0xd6, 0x4f, 0xdc, 0x1e, 0x3d, 0x11, 0xcd, 0x75, 0x0f, 0x3c, - 0xa4, 0x74, 0xa8, 0x8d, 0xfe, 0x2e, 0x01, 0x11, 0xd8, 0xa6, 0x3c, 0x4e, 0x84, 0xd9, 0x70, 0x9a, - 0x6e, 0x67, 0x70, 0x82, 0x6b, 0xfd, 0x8a, 0x3e, 0x1e, 0xf1, 0x38, 0xf1, 0x5a, 0xab, 0x75, 0xd7, - 0xf0, 0xb5, 0x0e, 0x5d, 0x43, 0x98, 0xca, 0xf0, 0x21, 0xa6, 0x93, 0x39, 0x2b, 0xcd, 0xa6, 0x03, - 0xdc, 0xce, 0xa0, 0x5b, 0x77, 0xe9, 0x06, 0x1b, 0xf3, 0xad, 0xd2, 0x8d, 0x59, 0xe9, 0xef, 0xa7, - 0xbb, 0x11, 0x9d, 0xc3, 0xa3, 0x40, 0x17, 0x99, 0x24, 0x72, 0x11, 0xb2, 0xcc, 0x6c, 0x39, 0xc0, - 0x6d, 0xf9, 0x87, 0x5b, 0xf6, 0x46, 0x91, 0xc8, 0x82, 0x7b, 0x82, 0x3d, 0x4a, 0x96, 0x50, 0x66, - 0xb6, 0x95, 0xe0, 0x07, 0x7b, 0xa3, 0xb7, 0x0f, 0xdb, 0x78, 0xaa, 0x6c, 0x63, 0x55, 0xd9, 0xe0, - 0xb5, 0xb2, 0xc1, 0x7b, 0x65, 0x83, 0x97, 0x4f, 0xdb, 0xb8, 0xbb, 0xf8, 0x73, 0x0b, 0xf5, 0x4f, - 0x0d, 0xff, 0xa9, 0xe5, 0x5e, 0x7e, 0x07, 0x00, 0x00, 0xff, 0xff, 0x7c, 0x3d, 0x84, 0x92, 0xeb, - 0x01, 0x00, 0x00, + // 348 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x7c, 0x51, 0xbf, 0x4e, 0xfb, 0x30, + 0x18, 0x4c, 0xda, 0xf4, 0xf7, 0xc7, 0x05, 0x24, 0x0c, 0x43, 0x94, 0x21, 0x89, 0x90, 0x90, 0xc2, + 0x50, 0x5b, 0x2d, 0x4c, 0x0c, 0x48, 0x4d, 0xc7, 0x4a, 0x08, 0x65, 0x64, 0xa9, 0x12, 0xc7, 0xb4, + 0x51, 0x69, 0x1c, 0xe2, 0xb8, 0x6a, 0x36, 0x46, 0x1e, 0x80, 0x81, 0x91, 0xc7, 0xe9, 0xc8, 0xc8, + 0x54, 0x41, 0x78, 0x0b, 0x26, 0x54, 0xbb, 0x45, 0x91, 0x90, 0x98, 0xfc, 0xdd, 0xe9, 0xee, 0xec, + 0xfb, 0x0c, 0xcc, 0x05, 0x0e, 0x45, 0x31, 0xc1, 0x45, 0x99, 0x51, 0x8e, 0x09, 0x8b, 0x29, 0x41, + 0x59, 0xce, 0x0a, 0x06, 0x0f, 0x09, 0xe3, 0x33, 0xc6, 0x47, 0x3c, 0x9e, 0xa2, 0x05, 0x5a, 0x8b, + 0xd0, 0xbc, 0x6b, 0x9d, 0xcd, 0x69, 0x1a, 0xb3, 0x1c, 0x8f, 0x93, 0x62, 0x22, 0x22, 0x44, 0xd8, + 0x0c, 0x8f, 0xd9, 0x98, 0x61, 0xe9, 0x89, 0xc4, 0x8d, 0x44, 0x12, 0xc8, 0x49, 0x65, 0x59, 0xfb, + 0x3f, 0xe2, 0x2d, 0x48, 0xf2, 0x32, 0x2b, 0x58, 0x9d, 0x3b, 0x7a, 0x6c, 0x80, 0xb6, 0x1f, 0x72, + 0xda, 0x27, 0x84, 0x89, 0xb4, 0x80, 0x43, 0xf0, 0x37, 0x8c, 0xe3, 0x9c, 0x72, 0x6e, 0xea, 0xae, + 0xee, 0xed, 0xf8, 0xdd, 0xcf, 0x95, 0xd3, 0xa9, 0x5d, 0xad, 0x9e, 0xb8, 0x39, 0x3a, 0x3c, 0x9e, + 0xaa, 0x1e, 0xa8, 0x4f, 0x48, 0x5f, 0x19, 0x83, 0x6d, 0x02, 0xc4, 0xa0, 0x45, 0x58, 0x92, 0x72, + 0xb3, 0xe1, 0x36, 0xbd, 0x76, 0xef, 0x00, 0xd5, 0xfa, 0xcd, 0xbb, 0x68, 0xc0, 0x92, 0xd4, 0x37, + 0x96, 0x2b, 0x47, 0x0b, 0x94, 0x0e, 0x5e, 0x00, 0x90, 0x89, 0xe8, 0x36, 0x21, 0xa3, 0x29, 0x2d, + 0xcd, 0xa6, 0xab, 0x7b, 0xed, 0x9e, 0x53, 0x77, 0xa9, 0x06, 0x6b, 0xf3, 0x95, 0xd4, 0x0d, 0x69, + 0x19, 0xfc, 0xcf, 0xb6, 0x23, 0x3c, 0x06, 0x7b, 0xa1, 0x2a, 0x32, 0x4a, 0xc5, 0x2c, 0xa2, 0xb9, + 0x69, 0xb8, 0xba, 0x67, 0x04, 0xbb, 0x1b, 0xf6, 0x52, 0x92, 0xd0, 0x02, 0xff, 0x38, 0xbd, 0x13, + 0x34, 0x25, 0xd4, 0x6c, 0x49, 0xc1, 0x37, 0x3e, 0x37, 0x1e, 0x9e, 0x1d, 0xcd, 0x1f, 0xbc, 0xbe, + 0xdb, 0xda, 0x7d, 0x65, 0x6b, 0xcb, 0xca, 0xd6, 0x5f, 0x2a, 0x5b, 0x7f, 0xab, 0x6c, 0xfd, 0xe9, + 0xc3, 0xd6, 0xae, 0x4f, 0x7e, 0xdd, 0x45, 0xfd, 0x6b, 0xa3, 0x3f, 0x72, 0xc5, 0xa7, 0x5f, 0x01, + 0x00, 0x00, 0xff, 0xff, 0xf4, 0x66, 0xb5, 0x7a, 0xf1, 0x01, 0x00, 0x00, } func (m *BaseAccount) Marshal() (dAtA []byte, err error) { diff --git a/x/auth/types/codec.proto b/x/auth/types/codec.proto index af106a771946..1f97609877ee 100644 --- a/x/auth/types/codec.proto +++ b/x/auth/types/codec.proto @@ -20,6 +20,8 @@ option (gogoproto.sizer_all) = true; // embedding within another account type that extends functionality. A BaseAccount // provides the basic functionality needed to operate an account. message BaseAccount { + option (gogoproto.goproto_getters) = false; + bytes address = 1 [(gogoproto.casttype) = "github.com/cosmos/cosmos-sdk/types.AccAddress"]; repeated cosmos_sdk.v1.Coin coins = 2 [(gogoproto.nullable) = false]; cosmos_sdk.crypto.v1.PublicKey public_key = 3; From e28fc0979f82e51c256c62d8db6a4dcd018a409d Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Fri, 3 Jan 2020 12:54:04 -0500 Subject: [PATCH 31/40] Rebase changes from bez/5444-proto-encoding-auth --- Makefile | 1 + crypto/codec.pb.go | 533 ++++++++++++++++++++++++++++++++++++++++++ crypto/codec.proto | 22 ++ crypto/pubkey.go | 55 +++++ crypto/pubkey_test.go | 72 ++++++ go.mod | 1 + scripts/protocgen.sh | 7 +- types/codec.pb.go | 41 +--- types/codec.proto | 3 - 9 files changed, 696 insertions(+), 39 deletions(-) create mode 100644 crypto/codec.pb.go create mode 100644 crypto/codec.proto create mode 100644 crypto/pubkey.go create mode 100644 crypto/pubkey_test.go diff --git a/Makefile b/Makefile index e0b02d93e961..695f31758681 100644 --- a/Makefile +++ b/Makefile @@ -231,6 +231,7 @@ proto-all: proto-gen proto-lint proto-check-breaking proto-gen: @go mod vendor + @go install github.com/regen-network/cosmos-proto/protoc-gen-gocosmos @./scripts/protocgen.sh proto-lint: diff --git a/crypto/codec.pb.go b/crypto/codec.pb.go new file mode 100644 index 000000000000..181e2d691412 --- /dev/null +++ b/crypto/codec.pb.go @@ -0,0 +1,533 @@ +// Code generated by protoc-gen-gogo. DO NOT EDIT. +// source: crypto/codec.proto + +package crypto + +import ( + fmt "fmt" + proto "github.com/gogo/protobuf/proto" + io "io" + math "math" + math_bits "math/bits" +) + +// Reference imports to suppress errors if they are not otherwise used. +var _ = proto.Marshal +var _ = fmt.Errorf +var _ = math.Inf + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the proto package it is being compiled against. +// A compilation error at this line likely means your copy of the +// proto package needs to be updated. +const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package + +// PublicKey defines a proto message with a single oneof field that represents a +// public key as a byte slice. +// +// NOTE: Private keys must still be amino-encoded for backwards compatiblity. +type PublicKey struct { + // pub defines an single supported public key type as a byte slice. + // + // Types that are valid to be assigned to Pub: + // *PublicKey_Secp256K1 + // *PublicKey_Ed25519 + // *PublicKey_Multisig + Pub isPublicKey_Pub `protobuf_oneof:"pub"` +} + +func (m *PublicKey) Reset() { *m = PublicKey{} } +func (m *PublicKey) String() string { return proto.CompactTextString(m) } +func (*PublicKey) ProtoMessage() {} +func (*PublicKey) Descriptor() ([]byte, []int) { + return fileDescriptor_0402281428f37664, []int{0} +} +func (m *PublicKey) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *PublicKey) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_PublicKey.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalToSizedBuffer(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (m *PublicKey) XXX_Merge(src proto.Message) { + xxx_messageInfo_PublicKey.Merge(m, src) +} +func (m *PublicKey) XXX_Size() int { + return m.Size() +} +func (m *PublicKey) XXX_DiscardUnknown() { + xxx_messageInfo_PublicKey.DiscardUnknown(m) +} + +var xxx_messageInfo_PublicKey proto.InternalMessageInfo + +type isPublicKey_Pub interface { + isPublicKey_Pub() + MarshalTo([]byte) (int, error) + Size() int +} + +type PublicKey_Secp256K1 struct { + Secp256K1 []byte `protobuf:"bytes,1,opt,name=secp256k1,proto3,oneof" json:"secp256k1,omitempty"` +} +type PublicKey_Ed25519 struct { + Ed25519 []byte `protobuf:"bytes,2,opt,name=ed25519,proto3,oneof" json:"ed25519,omitempty"` +} +type PublicKey_Multisig struct { + Multisig []byte `protobuf:"bytes,3,opt,name=multisig,proto3,oneof" json:"multisig,omitempty"` +} + +func (*PublicKey_Secp256K1) isPublicKey_Pub() {} +func (*PublicKey_Ed25519) isPublicKey_Pub() {} +func (*PublicKey_Multisig) isPublicKey_Pub() {} + +func (m *PublicKey) GetPub() isPublicKey_Pub { + if m != nil { + return m.Pub + } + return nil +} + +func (m *PublicKey) GetSecp256K1() []byte { + if x, ok := m.GetPub().(*PublicKey_Secp256K1); ok { + return x.Secp256K1 + } + return nil +} + +func (m *PublicKey) GetEd25519() []byte { + if x, ok := m.GetPub().(*PublicKey_Ed25519); ok { + return x.Ed25519 + } + return nil +} + +func (m *PublicKey) GetMultisig() []byte { + if x, ok := m.GetPub().(*PublicKey_Multisig); ok { + return x.Multisig + } + return nil +} + +// XXX_OneofWrappers is for the internal use of the proto package. +func (*PublicKey) XXX_OneofWrappers() []interface{} { + return []interface{}{ + (*PublicKey_Secp256K1)(nil), + (*PublicKey_Ed25519)(nil), + (*PublicKey_Multisig)(nil), + } +} + +func init() { + proto.RegisterType((*PublicKey)(nil), "cosmos_sdk.crypto.v1.PublicKey") +} + +func init() { proto.RegisterFile("crypto/codec.proto", fileDescriptor_0402281428f37664) } + +var fileDescriptor_0402281428f37664 = []byte{ + // 197 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x12, 0x4a, 0x2e, 0xaa, 0x2c, + 0x28, 0xc9, 0xd7, 0x4f, 0xce, 0x4f, 0x49, 0x4d, 0xd6, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x12, + 0x49, 0xce, 0x2f, 0xce, 0xcd, 0x2f, 0x8e, 0x2f, 0x4e, 0xc9, 0xd6, 0x83, 0x48, 0xeb, 0x95, 0x19, + 0x2a, 0xe5, 0x70, 0x71, 0x06, 0x94, 0x26, 0xe5, 0x64, 0x26, 0x7b, 0xa7, 0x56, 0x0a, 0xc9, 0x71, + 0x71, 0x16, 0xa7, 0x26, 0x17, 0x18, 0x99, 0x9a, 0x65, 0x1b, 0x4a, 0x30, 0x2a, 0x30, 0x6a, 0xf0, + 0x78, 0x30, 0x04, 0x21, 0x84, 0x84, 0xa4, 0xb8, 0xd8, 0x53, 0x53, 0x8c, 0x4c, 0x4d, 0x0d, 0x2d, + 0x25, 0x98, 0xa0, 0xb2, 0x30, 0x01, 0x21, 0x19, 0x2e, 0x8e, 0xdc, 0xd2, 0x9c, 0x92, 0xcc, 0xe2, + 0xcc, 0x74, 0x09, 0x66, 0xa8, 0x24, 0x5c, 0xc4, 0x89, 0x95, 0x8b, 0xb9, 0xa0, 0x34, 0xc9, 0xc9, + 0xf6, 0xc4, 0x23, 0x39, 0xc6, 0x0b, 0x8f, 0xe4, 0x18, 0x1f, 0x3c, 0x92, 0x63, 0x9c, 0xf0, 0x58, + 0x8e, 0xe1, 0xc2, 0x63, 0x39, 0x86, 0x1b, 0x8f, 0xe5, 0x18, 0xa2, 0x94, 0xd3, 0x33, 0x4b, 0x32, + 0x4a, 0x93, 0xf4, 0x92, 0xf3, 0x73, 0xf5, 0x21, 0x0e, 0x85, 0x52, 0xba, 0xc5, 0x29, 0xd9, 0xfa, + 0x10, 0xf7, 0x26, 0xb1, 0x81, 0x7d, 0x62, 0x0c, 0x08, 0x00, 0x00, 0xff, 0xff, 0x4f, 0xbe, 0x3a, + 0x48, 0xdf, 0x00, 0x00, 0x00, +} + +func (m *PublicKey) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalToSizedBuffer(dAtA[:size]) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *PublicKey) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *PublicKey) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + if m.Pub != nil { + { + size := m.Pub.Size() + i -= size + if _, err := m.Pub.MarshalTo(dAtA[i:]); err != nil { + return 0, err + } + } + } + return len(dAtA) - i, nil +} + +func (m *PublicKey_Secp256K1) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *PublicKey_Secp256K1) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + if m.Secp256K1 != nil { + i -= len(m.Secp256K1) + copy(dAtA[i:], m.Secp256K1) + i = encodeVarintCodec(dAtA, i, uint64(len(m.Secp256K1))) + i-- + dAtA[i] = 0xa + } + return len(dAtA) - i, nil +} +func (m *PublicKey_Ed25519) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *PublicKey_Ed25519) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + if m.Ed25519 != nil { + i -= len(m.Ed25519) + copy(dAtA[i:], m.Ed25519) + i = encodeVarintCodec(dAtA, i, uint64(len(m.Ed25519))) + i-- + dAtA[i] = 0x12 + } + return len(dAtA) - i, nil +} +func (m *PublicKey_Multisig) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *PublicKey_Multisig) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + if m.Multisig != nil { + i -= len(m.Multisig) + copy(dAtA[i:], m.Multisig) + i = encodeVarintCodec(dAtA, i, uint64(len(m.Multisig))) + i-- + dAtA[i] = 0x1a + } + return len(dAtA) - i, nil +} +func encodeVarintCodec(dAtA []byte, offset int, v uint64) int { + offset -= sovCodec(v) + base := offset + for v >= 1<<7 { + dAtA[offset] = uint8(v&0x7f | 0x80) + v >>= 7 + offset++ + } + dAtA[offset] = uint8(v) + return base +} +func (m *PublicKey) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + if m.Pub != nil { + n += m.Pub.Size() + } + return n +} + +func (m *PublicKey_Secp256K1) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + if m.Secp256K1 != nil { + l = len(m.Secp256K1) + n += 1 + l + sovCodec(uint64(l)) + } + return n +} +func (m *PublicKey_Ed25519) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + if m.Ed25519 != nil { + l = len(m.Ed25519) + n += 1 + l + sovCodec(uint64(l)) + } + return n +} +func (m *PublicKey_Multisig) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + if m.Multisig != nil { + l = len(m.Multisig) + n += 1 + l + sovCodec(uint64(l)) + } + return n +} + +func sovCodec(x uint64) (n int) { + return (math_bits.Len64(x|1) + 6) / 7 +} +func sozCodec(x uint64) (n int) { + return sovCodec(uint64((x << 1) ^ uint64((int64(x) >> 63)))) +} +func (m *PublicKey) Unmarshal(dAtA []byte) error { + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowCodec + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: PublicKey: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: PublicKey: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Secp256K1", wireType) + } + var byteLen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowCodec + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + byteLen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if byteLen < 0 { + return ErrInvalidLengthCodec + } + postIndex := iNdEx + byteLen + if postIndex < 0 { + return ErrInvalidLengthCodec + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + v := make([]byte, postIndex-iNdEx) + copy(v, dAtA[iNdEx:postIndex]) + m.Pub = &PublicKey_Secp256K1{v} + iNdEx = postIndex + case 2: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Ed25519", wireType) + } + var byteLen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowCodec + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + byteLen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if byteLen < 0 { + return ErrInvalidLengthCodec + } + postIndex := iNdEx + byteLen + if postIndex < 0 { + return ErrInvalidLengthCodec + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + v := make([]byte, postIndex-iNdEx) + copy(v, dAtA[iNdEx:postIndex]) + m.Pub = &PublicKey_Ed25519{v} + iNdEx = postIndex + case 3: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Multisig", wireType) + } + var byteLen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowCodec + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + byteLen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if byteLen < 0 { + return ErrInvalidLengthCodec + } + postIndex := iNdEx + byteLen + if postIndex < 0 { + return ErrInvalidLengthCodec + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + v := make([]byte, postIndex-iNdEx) + copy(v, dAtA[iNdEx:postIndex]) + m.Pub = &PublicKey_Multisig{v} + iNdEx = postIndex + default: + iNdEx = preIndex + skippy, err := skipCodec(dAtA[iNdEx:]) + if err != nil { + return err + } + if skippy < 0 { + return ErrInvalidLengthCodec + } + if (iNdEx + skippy) < 0 { + return ErrInvalidLengthCodec + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} +func skipCodec(dAtA []byte) (n int, err error) { + l := len(dAtA) + iNdEx := 0 + depth := 0 + for iNdEx < l { + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowCodec + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= (uint64(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + wireType := int(wire & 0x7) + switch wireType { + case 0: + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowCodec + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + iNdEx++ + if dAtA[iNdEx-1] < 0x80 { + break + } + } + case 1: + iNdEx += 8 + case 2: + var length int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowCodec + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + length |= (int(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + if length < 0 { + return 0, ErrInvalidLengthCodec + } + iNdEx += length + case 3: + depth++ + case 4: + if depth == 0 { + return 0, ErrUnexpectedEndOfGroupCodec + } + depth-- + case 5: + iNdEx += 4 + default: + return 0, fmt.Errorf("proto: illegal wireType %d", wireType) + } + if iNdEx < 0 { + return 0, ErrInvalidLengthCodec + } + if depth == 0 { + return iNdEx, nil + } + } + return 0, io.ErrUnexpectedEOF +} + +var ( + ErrInvalidLengthCodec = fmt.Errorf("proto: negative length found during unmarshaling") + ErrIntOverflowCodec = fmt.Errorf("proto: integer overflow") + ErrUnexpectedEndOfGroupCodec = fmt.Errorf("proto: unexpected end of group") +) diff --git a/crypto/codec.proto b/crypto/codec.proto new file mode 100644 index 000000000000..4ea9967f0eeb --- /dev/null +++ b/crypto/codec.proto @@ -0,0 +1,22 @@ +syntax = "proto3"; +package cosmos_sdk.crypto.v1; + +option go_package = "github.com/cosmos/cosmos-sdk/crypto"; + +// PublicKey defines a proto message with a single oneof field that represents a +// public key as a byte slice. +// +// NOTE: Private keys must still be amino-encoded for backwards compatiblity. +message PublicKey { + // TODO: Make tendermint keys use []byte instead of [32]byte so we can use the + // casttype extension. + // option (cosmos_proto.interface_type) = + // "github.com/tendermint/tendermint/crypto.PubKey"; + + // pub defines an single supported public key type as a byte slice. + oneof pub { + bytes secp256k1 = 1; + bytes ed25519 = 2; + bytes multisig = 3; + } +} diff --git a/crypto/pubkey.go b/crypto/pubkey.go new file mode 100644 index 000000000000..9aee95e09a02 --- /dev/null +++ b/crypto/pubkey.go @@ -0,0 +1,55 @@ +package crypto + +import ( + "github.com/pkg/errors" + "github.com/tendermint/tendermint/crypto/ed25519" + "github.com/tendermint/tendermint/crypto/secp256k1" +) + +// public key sentinel errors +var ( + ErrInvalidPubKeySecp256k1Length = errors.New("invalid PubKeySecp256k1 length") + ErrInvalidPubKeySecp256k1 = errors.New("incompatible PubKeySecp256k1") + ErrInvalidPubKeyEd25519Length = errors.New("invalid PubKeyEd25519 length") + ErrInvalidPubKeyEd25519 = errors.New("incompatible PubKeyEd25519") +) + +// GetPubKeySecp256k1 returns a Tendermint secp256k1 public key from the +// PublicKey message type. It will return an error if the size of the public key +// is invalid or the underlying Pub field is invalid. +// +// NOTE: Do not use or call bytes on the result when serializing. +func (m *PublicKey) GetPubKeySecp256k1() (secp256k1.PubKeySecp256k1, error) { + pk := secp256k1.PubKeySecp256k1{} + + if x, ok := m.GetPub().(*PublicKey_Secp256K1); ok { + if len(x.Secp256K1) != secp256k1.PubKeySecp256k1Size { + return pk, ErrInvalidPubKeySecp256k1Length + } + + copy(pk[:], x.Secp256K1) + return pk, nil + } + + return pk, ErrInvalidPubKeySecp256k1 +} + +// GetPubKeyEd25519 returns a Tendermint Ed25519 public key from the PublicKey +// message type. It will return an error if the size of the public key +// is invalid or the underlying Pub field is invalid. +// +// NOTE: Do not use or call bytes on the result when serializing. +func (m *PublicKey) GetPubKeyEd25519() (ed25519.PubKeyEd25519, error) { + pk := ed25519.PubKeyEd25519{} + + if x, ok := m.GetPub().(*PublicKey_Ed25519); ok { + if len(x.Ed25519) != ed25519.PubKeyEd25519Size { + return pk, ErrInvalidPubKeyEd25519Length + } + + copy(pk[:], x.Ed25519) + return pk, nil + } + + return pk, ErrInvalidPubKeyEd25519 +} diff --git a/crypto/pubkey_test.go b/crypto/pubkey_test.go new file mode 100644 index 000000000000..d53d48026f5b --- /dev/null +++ b/crypto/pubkey_test.go @@ -0,0 +1,72 @@ +package crypto_test + +import ( + "testing" + + "github.com/cosmos/cosmos-sdk/crypto" + "github.com/stretchr/testify/require" + "github.com/tendermint/tendermint/crypto/ed25519" + "github.com/tendermint/tendermint/crypto/secp256k1" +) + +func TestGetPubKeySecp256k1(t *testing.T) { + tmpk := (secp256k1.GenPrivKey().PubKey()).(secp256k1.PubKeySecp256k1) + testCases := []struct { + input *crypto.PublicKey + expected secp256k1.PubKeySecp256k1 + err error + }{ + { + &crypto.PublicKey{&crypto.PublicKey_Secp256K1{tmpk[:]}}, + tmpk, + nil, + }, + { + &crypto.PublicKey{&crypto.PublicKey_Secp256K1{append(tmpk[:], tmpk[:]...)}}, + secp256k1.PubKeySecp256k1{}, + crypto.ErrInvalidPubKeySecp256k1Length, + }, + { + &crypto.PublicKey{&crypto.PublicKey_Ed25519{}}, + secp256k1.PubKeySecp256k1{}, + crypto.ErrInvalidPubKeySecp256k1, + }, + } + + for _, tc := range testCases { + pk, err := tc.input.GetPubKeySecp256k1() + require.Equal(t, tc.err, err) + require.True(t, pk.Equals(tc.expected)) + } +} + +func TestGetPubKeyEd25519(t *testing.T) { + tmpk := (ed25519.GenPrivKey().PubKey()).(ed25519.PubKeyEd25519) + testCases := []struct { + input *crypto.PublicKey + expected ed25519.PubKeyEd25519 + err error + }{ + { + &crypto.PublicKey{&crypto.PublicKey_Ed25519{tmpk[:]}}, + tmpk, + nil, + }, + { + &crypto.PublicKey{&crypto.PublicKey_Ed25519{append(tmpk[:], tmpk[:]...)}}, + ed25519.PubKeyEd25519{}, + crypto.ErrInvalidPubKeyEd25519Length, + }, + { + &crypto.PublicKey{&crypto.PublicKey_Secp256K1{}}, + ed25519.PubKeyEd25519{}, + crypto.ErrInvalidPubKeyEd25519, + }, + } + + for _, tc := range testCases { + pk, err := tc.input.GetPubKeyEd25519() + require.Equal(t, tc.err, err) + require.True(t, pk.Equals(tc.expected)) + } +} diff --git a/go.mod b/go.mod index bb0a000b84a3..d8645982e275 100644 --- a/go.mod +++ b/go.mod @@ -16,6 +16,7 @@ require ( github.com/pelletier/go-toml v1.6.0 github.com/pkg/errors v0.8.1 github.com/rakyll/statik v0.1.6 + github.com/regen-network/cosmos-proto v0.0.0-20200103162838-2560887837f1 // indirect github.com/spf13/afero v1.2.1 // indirect github.com/spf13/cobra v0.0.5 github.com/spf13/jwalterweatherman v1.1.0 // indirect diff --git a/scripts/protocgen.sh b/scripts/protocgen.sh index ebe4ba0b96ac..b2f46670dad4 100755 --- a/scripts/protocgen.sh +++ b/scripts/protocgen.sh @@ -4,10 +4,7 @@ set -eo pipefail proto_dirs=$(find . -path ./vendor -prune -o -name '*.proto' -print0 | xargs -0 -n1 dirname | sort | uniq) for dir in $proto_dirs; do - protoc -I/usr/local/include \ - -I. \ - -I${GOPATH}/src \ - -Ivendor/github.com/gogo/protobuf/gogoproto \ - --gofast_out=$GOPATH/src \ + protoc -I. \ + --gocosmos_out=plugins=interfacetype,paths=source_relative:. \ $(find "${dir}" -name '*.proto') done diff --git a/types/codec.pb.go b/types/codec.pb.go index 11f1c5a65894..7ff477997983 100644 --- a/types/codec.pb.go +++ b/types/codec.pb.go @@ -6,7 +6,7 @@ package types import ( fmt "fmt" _ "github.com/gogo/protobuf/gogoproto" - proto "github.com/golang/protobuf/proto" + proto "github.com/gogo/protobuf/proto" io "io" math "math" math_bits "math/bits" @@ -21,7 +21,7 @@ var _ = math.Inf // is compatible with the proto package it is being compiled against. // A compilation error at this line likely means your copy of the // proto package needs to be updated. -const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package +const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package // Coin defines a token with a denomination and an amount. // @@ -31,10 +31,7 @@ type Coin struct { Denom string `protobuf:"bytes,1,opt,name=denom,proto3" json:"denom,omitempty"` // TODO: Consider using a string instead of bytes. This would require gogo proto // to support types other than bytes for the customtype extension. - Amount Int `protobuf:"bytes,2,opt,name=amount,proto3,customtype=Int" json:"amount"` - XXX_NoUnkeyedLiteral struct{} `json:"-"` - XXX_unrecognized []byte `json:"-"` - XXX_sizecache int32 `json:"-"` + Amount Int `protobuf:"bytes,2,opt,name=amount,proto3,customtype=Int" json:"amount"` } func (m *Coin) Reset() { *m = Coin{} } @@ -84,10 +81,7 @@ type DecCoin struct { Denom string `protobuf:"bytes,1,opt,name=denom,proto3" json:"denom,omitempty"` // TODO: Consider using a string instead of bytes. This would require gogo proto // to support types other than bytes for the customtype extension. - Amount Dec `protobuf:"bytes,2,opt,name=amount,proto3,customtype=Dec" json:"amount"` - XXX_NoUnkeyedLiteral struct{} `json:"-"` - XXX_unrecognized []byte `json:"-"` - XXX_sizecache int32 `json:"-"` + Amount Dec `protobuf:"bytes,2,opt,name=amount,proto3,customtype=Dec" json:"amount"` } func (m *DecCoin) Reset() { *m = DecCoin{} } @@ -137,7 +131,7 @@ func init() { func init() { proto.RegisterFile("types/codec.proto", fileDescriptor_97e5f89da6f0daf0) } var fileDescriptor_97e5f89da6f0daf0 = []byte{ - // 216 bytes of a gzipped FileDescriptorProto + // 225 bytes of a gzipped FileDescriptorProto 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x12, 0x2c, 0xa9, 0x2c, 0x48, 0x2d, 0xd6, 0x4f, 0xce, 0x4f, 0x49, 0x4d, 0xd6, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0xe2, 0x4d, 0xce, 0x2f, 0xce, 0xcd, 0x2f, 0x8e, 0x2f, 0x4e, 0xc9, 0xd6, 0x2b, 0x33, 0x94, 0x32, 0x29, 0x4b, @@ -147,11 +141,12 @@ var fileDescriptor_97e5f89da6f0daf0 = []byte{ 0x5e, 0x7e, 0xae, 0x04, 0xa3, 0x02, 0xa3, 0x06, 0x67, 0x10, 0x84, 0x23, 0xa4, 0xcc, 0xc5, 0x96, 0x98, 0x9b, 0x5f, 0x9a, 0x57, 0x22, 0xc1, 0xa4, 0xc0, 0xa8, 0xc1, 0xe3, 0xc4, 0x7d, 0xe2, 0x9e, 0x3c, 0xc3, 0xad, 0x7b, 0xf2, 0xcc, 0x9e, 0x79, 0x25, 0x41, 0x50, 0x29, 0x25, 0x17, 0x2e, 0x76, - 0x97, 0xd4, 0x64, 0x72, 0x4c, 0x71, 0x49, 0x4d, 0x86, 0x99, 0xe2, 0x64, 0x73, 0xe3, 0xa1, 0x1c, + 0x97, 0xd4, 0x64, 0x72, 0x4c, 0x71, 0x49, 0x4d, 0x86, 0x99, 0xe2, 0xe4, 0x72, 0xe3, 0xa1, 0x1c, 0x43, 0xc3, 0x23, 0x39, 0x86, 0x13, 0x8f, 0xe4, 0x18, 0x2f, 0x3c, 0x92, 0x63, 0x7c, 0xf0, 0x48, - 0x8e, 0x71, 0xc6, 0x63, 0x39, 0x86, 0x28, 0x25, 0x24, 0x1f, 0x41, 0xbc, 0x0c, 0xa5, 0x74, 0x8b, - 0x53, 0xb2, 0xf5, 0xc1, 0xe1, 0x92, 0xc4, 0x06, 0xf6, 0x8d, 0x31, 0x20, 0x00, 0x00, 0xff, 0xff, - 0xef, 0x4e, 0x92, 0x34, 0x27, 0x01, 0x00, 0x00, + 0x8e, 0x71, 0xc2, 0x63, 0x39, 0x86, 0x0b, 0x8f, 0xe5, 0x18, 0x6e, 0x3c, 0x96, 0x63, 0x88, 0x52, + 0x42, 0xf2, 0x19, 0xc4, 0xeb, 0x50, 0x4a, 0xb7, 0x38, 0x25, 0x5b, 0x1f, 0x1c, 0x3e, 0x49, 0x6c, + 0x60, 0x5f, 0x19, 0x03, 0x02, 0x00, 0x00, 0xff, 0xff, 0x6c, 0x9b, 0x51, 0x3b, 0x2f, 0x01, 0x00, + 0x00, } func (m *Coin) Marshal() (dAtA []byte, err error) { @@ -174,10 +169,6 @@ func (m *Coin) MarshalToSizedBuffer(dAtA []byte) (int, error) { _ = i var l int _ = l - if m.XXX_unrecognized != nil { - i -= len(m.XXX_unrecognized) - copy(dAtA[i:], m.XXX_unrecognized) - } { size := m.Amount.Size() i -= size @@ -218,10 +209,6 @@ func (m *DecCoin) MarshalToSizedBuffer(dAtA []byte) (int, error) { _ = i var l int _ = l - if m.XXX_unrecognized != nil { - i -= len(m.XXX_unrecognized) - copy(dAtA[i:], m.XXX_unrecognized) - } { size := m.Amount.Size() i -= size @@ -265,9 +252,6 @@ func (m *Coin) Size() (n int) { } l = m.Amount.Size() n += 1 + l + sovCodec(uint64(l)) - if m.XXX_unrecognized != nil { - n += len(m.XXX_unrecognized) - } return n } @@ -283,9 +267,6 @@ func (m *DecCoin) Size() (n int) { } l = m.Amount.Size() n += 1 + l + sovCodec(uint64(l)) - if m.XXX_unrecognized != nil { - n += len(m.XXX_unrecognized) - } return n } @@ -404,7 +385,6 @@ func (m *Coin) Unmarshal(dAtA []byte) error { if (iNdEx + skippy) > l { return io.ErrUnexpectedEOF } - m.XXX_unrecognized = append(m.XXX_unrecognized, dAtA[iNdEx:iNdEx+skippy]...) iNdEx += skippy } } @@ -523,7 +503,6 @@ func (m *DecCoin) Unmarshal(dAtA []byte) error { if (iNdEx + skippy) > l { return io.ErrUnexpectedEOF } - m.XXX_unrecognized = append(m.XXX_unrecognized, dAtA[iNdEx:iNdEx+skippy]...) iNdEx += skippy } } diff --git a/types/codec.proto b/types/codec.proto index a7275ed004ca..8732c9dc1cf7 100644 --- a/types/codec.proto +++ b/types/codec.proto @@ -4,9 +4,6 @@ package cosmos_sdk.v1; option go_package = "github.com/cosmos/cosmos-sdk/types"; option (gogoproto.goproto_stringer_all) = false; option (gogoproto.stringer_all) = false; -option (gogoproto.marshaler_all) = true; -option (gogoproto.unmarshaler_all) = true; -option (gogoproto.sizer_all) = true; import "vendor/github.com/gogo/protobuf/gogoproto/gogo.proto"; From 3338487455894286780e9bd272f01ab38dd7506d Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Fri, 3 Jan 2020 12:57:15 -0500 Subject: [PATCH 32/40] Update go.mod --- go.mod | 1 - 1 file changed, 1 deletion(-) diff --git a/go.mod b/go.mod index d8645982e275..bb0a000b84a3 100644 --- a/go.mod +++ b/go.mod @@ -16,7 +16,6 @@ require ( github.com/pelletier/go-toml v1.6.0 github.com/pkg/errors v0.8.1 github.com/rakyll/statik v0.1.6 - github.com/regen-network/cosmos-proto v0.0.0-20200103162838-2560887837f1 // indirect github.com/spf13/afero v1.2.1 // indirect github.com/spf13/cobra v0.0.5 github.com/spf13/jwalterweatherman v1.1.0 // indirect From 806ecf05f6589d649322e09691e4e8ff3a10afc6 Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Fri, 3 Jan 2020 12:58:13 -0500 Subject: [PATCH 33/40] Update go.{mod,sum} --- go.mod | 1 - go.sum | 4 ---- 2 files changed, 5 deletions(-) diff --git a/go.mod b/go.mod index d8645982e275..bb0a000b84a3 100644 --- a/go.mod +++ b/go.mod @@ -16,7 +16,6 @@ require ( github.com/pelletier/go-toml v1.6.0 github.com/pkg/errors v0.8.1 github.com/rakyll/statik v0.1.6 - github.com/regen-network/cosmos-proto v0.0.0-20200103162838-2560887837f1 // indirect github.com/spf13/afero v1.2.1 // indirect github.com/spf13/cobra v0.0.5 github.com/spf13/jwalterweatherman v1.1.0 // indirect diff --git a/go.sum b/go.sum index f095fd457870..cc5d9b873f77 100644 --- a/go.sum +++ b/go.sum @@ -199,10 +199,6 @@ github.com/rakyll/statik v0.1.6 h1:uICcfUXpgqtw2VopbIncslhAmE5hwc4g20TEyEENBNs= github.com/rakyll/statik v0.1.6/go.mod h1:OEi9wJV/fMUAGx1eNjq75DKDsJVuEv1U0oYdX6GX8Zs= github.com/rcrowley/go-metrics v0.0.0-20180503174638-e2704e165165 h1:nkcn14uNmFEuGCb2mBZbBb24RdNRL08b/wb+xBOYpuk= github.com/rcrowley/go-metrics v0.0.0-20180503174638-e2704e165165/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4= -github.com/regen-network/cosmos-proto v0.0.0-20200103153758-711a23958115 h1:ML7849Fm5pC076eQ/mfYSqHf9IlUH+7DUW0hQf0tk7M= -github.com/regen-network/cosmos-proto v0.0.0-20200103153758-711a23958115/go.mod h1:z+OxEChBwVwsOhJeKyvS6WqPRFDV/w2hhwDF6qpi4dA= -github.com/regen-network/cosmos-proto v0.0.0-20200103162838-2560887837f1 h1:3NogdWUoYfVSgTstfMgz+yaIWnAececvl3lI+/mZq8M= -github.com/regen-network/cosmos-proto v0.0.0-20200103162838-2560887837f1/go.mod h1:z+OxEChBwVwsOhJeKyvS6WqPRFDV/w2hhwDF6qpi4dA= github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg= github.com/rs/cors v1.6.0 h1:G9tHG9lebljV9mfp9SNPDL36nCDxmo3zTlAf1YgvzmI= github.com/rs/cors v1.6.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU= From 726ea57b71e7861b166d9a86ae8b2269f48805e3 Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Fri, 3 Jan 2020 15:17:16 -0500 Subject: [PATCH 34/40] Support multisig in crypto proto --- crypto/codec.pb.go | 277 ++++++++++++++++++++++++++++++++++++++---- crypto/codec.proto | 9 +- crypto/pubkey.go | 73 ++++++++++- crypto/pubkey_test.go | 40 ++++++ go.mod | 1 + go.sum | 2 + 6 files changed, 376 insertions(+), 26 deletions(-) diff --git a/crypto/codec.pb.go b/crypto/codec.pb.go index 181e2d691412..cefd67a69527 100644 --- a/crypto/codec.pb.go +++ b/crypto/codec.pb.go @@ -82,7 +82,7 @@ type PublicKey_Ed25519 struct { Ed25519 []byte `protobuf:"bytes,2,opt,name=ed25519,proto3,oneof" json:"ed25519,omitempty"` } type PublicKey_Multisig struct { - Multisig []byte `protobuf:"bytes,3,opt,name=multisig,proto3,oneof" json:"multisig,omitempty"` + Multisig *MultiSig `protobuf:"bytes,3,opt,name=multisig,proto3,oneof" json:"multisig,omitempty"` } func (*PublicKey_Secp256K1) isPublicKey_Pub() {} @@ -110,7 +110,7 @@ func (m *PublicKey) GetEd25519() []byte { return nil } -func (m *PublicKey) GetMultisig() []byte { +func (m *PublicKey) GetMultisig() *MultiSig { if x, ok := m.GetPub().(*PublicKey_Multisig); ok { return x.Multisig } @@ -126,27 +126,86 @@ func (*PublicKey) XXX_OneofWrappers() []interface{} { } } +// MultiSig defines a proto message representing a threshold multi-signature +// scheme. +type MultiSig struct { + K uint32 `protobuf:"varint,1,opt,name=k,proto3" json:"k,omitempty"` + Pubkeys []*PublicKey `protobuf:"bytes,2,rep,name=pubkeys,proto3" json:"pubkeys,omitempty"` +} + +func (m *MultiSig) Reset() { *m = MultiSig{} } +func (m *MultiSig) String() string { return proto.CompactTextString(m) } +func (*MultiSig) ProtoMessage() {} +func (*MultiSig) Descriptor() ([]byte, []int) { + return fileDescriptor_0402281428f37664, []int{1} +} +func (m *MultiSig) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *MultiSig) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_MultiSig.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalToSizedBuffer(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (m *MultiSig) XXX_Merge(src proto.Message) { + xxx_messageInfo_MultiSig.Merge(m, src) +} +func (m *MultiSig) XXX_Size() int { + return m.Size() +} +func (m *MultiSig) XXX_DiscardUnknown() { + xxx_messageInfo_MultiSig.DiscardUnknown(m) +} + +var xxx_messageInfo_MultiSig proto.InternalMessageInfo + +func (m *MultiSig) GetK() uint32 { + if m != nil { + return m.K + } + return 0 +} + +func (m *MultiSig) GetPubkeys() []*PublicKey { + if m != nil { + return m.Pubkeys + } + return nil +} + func init() { proto.RegisterType((*PublicKey)(nil), "cosmos_sdk.crypto.v1.PublicKey") + proto.RegisterType((*MultiSig)(nil), "cosmos_sdk.crypto.v1.MultiSig") } func init() { proto.RegisterFile("crypto/codec.proto", fileDescriptor_0402281428f37664) } var fileDescriptor_0402281428f37664 = []byte{ - // 197 bytes of a gzipped FileDescriptorProto + // 259 bytes of a gzipped FileDescriptorProto 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x12, 0x4a, 0x2e, 0xaa, 0x2c, 0x28, 0xc9, 0xd7, 0x4f, 0xce, 0x4f, 0x49, 0x4d, 0xd6, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x12, 0x49, 0xce, 0x2f, 0xce, 0xcd, 0x2f, 0x8e, 0x2f, 0x4e, 0xc9, 0xd6, 0x83, 0x48, 0xeb, 0x95, 0x19, - 0x2a, 0xe5, 0x70, 0x71, 0x06, 0x94, 0x26, 0xe5, 0x64, 0x26, 0x7b, 0xa7, 0x56, 0x0a, 0xc9, 0x71, - 0x71, 0x16, 0xa7, 0x26, 0x17, 0x18, 0x99, 0x9a, 0x65, 0x1b, 0x4a, 0x30, 0x2a, 0x30, 0x6a, 0xf0, - 0x78, 0x30, 0x04, 0x21, 0x84, 0x84, 0xa4, 0xb8, 0xd8, 0x53, 0x53, 0x8c, 0x4c, 0x4d, 0x0d, 0x2d, - 0x25, 0x98, 0xa0, 0xb2, 0x30, 0x01, 0x21, 0x19, 0x2e, 0x8e, 0xdc, 0xd2, 0x9c, 0x92, 0xcc, 0xe2, - 0xcc, 0x74, 0x09, 0x66, 0xa8, 0x24, 0x5c, 0xc4, 0x89, 0x95, 0x8b, 0xb9, 0xa0, 0x34, 0xc9, 0xc9, - 0xf6, 0xc4, 0x23, 0x39, 0xc6, 0x0b, 0x8f, 0xe4, 0x18, 0x1f, 0x3c, 0x92, 0x63, 0x9c, 0xf0, 0x58, - 0x8e, 0xe1, 0xc2, 0x63, 0x39, 0x86, 0x1b, 0x8f, 0xe5, 0x18, 0xa2, 0x94, 0xd3, 0x33, 0x4b, 0x32, - 0x4a, 0x93, 0xf4, 0x92, 0xf3, 0x73, 0xf5, 0x21, 0x0e, 0x85, 0x52, 0xba, 0xc5, 0x29, 0xd9, 0xfa, - 0x10, 0xf7, 0x26, 0xb1, 0x81, 0x7d, 0x62, 0x0c, 0x08, 0x00, 0x00, 0xff, 0xff, 0x4f, 0xbe, 0x3a, - 0x48, 0xdf, 0x00, 0x00, 0x00, + 0x2a, 0xf5, 0x30, 0x72, 0x71, 0x06, 0x94, 0x26, 0xe5, 0x64, 0x26, 0x7b, 0xa7, 0x56, 0x0a, 0xc9, + 0x71, 0x71, 0x16, 0xa7, 0x26, 0x17, 0x18, 0x99, 0x9a, 0x65, 0x1b, 0x4a, 0x30, 0x2a, 0x30, 0x6a, + 0xf0, 0x78, 0x30, 0x04, 0x21, 0x84, 0x84, 0xa4, 0xb8, 0xd8, 0x53, 0x53, 0x8c, 0x4c, 0x4d, 0x0d, + 0x2d, 0x25, 0x98, 0xa0, 0xb2, 0x30, 0x01, 0x21, 0x1b, 0x2e, 0x8e, 0xdc, 0xd2, 0x9c, 0x92, 0xcc, + 0xe2, 0xcc, 0x74, 0x09, 0x66, 0x05, 0x46, 0x0d, 0x6e, 0x23, 0x39, 0x3d, 0x6c, 0x56, 0xea, 0xf9, + 0x82, 0x54, 0x05, 0x67, 0xa6, 0x7b, 0x30, 0x04, 0xc1, 0x75, 0x38, 0xb1, 0x72, 0x31, 0x17, 0x94, + 0x26, 0x29, 0x05, 0x73, 0x71, 0xc0, 0xa4, 0x85, 0x78, 0xb8, 0x18, 0xb3, 0xc1, 0x8e, 0xe0, 0x0d, + 0x62, 0xcc, 0x16, 0xb2, 0xe4, 0x62, 0x2f, 0x28, 0x4d, 0xca, 0x4e, 0xad, 0x2c, 0x96, 0x60, 0x52, + 0x60, 0xd6, 0xe0, 0x36, 0x92, 0xc7, 0x6e, 0x3a, 0xdc, 0x33, 0x41, 0x30, 0xf5, 0x4e, 0xb6, 0x27, + 0x1e, 0xc9, 0x31, 0x5e, 0x78, 0x24, 0xc7, 0xf8, 0xe0, 0x91, 0x1c, 0xe3, 0x84, 0xc7, 0x72, 0x0c, + 0x17, 0x1e, 0xcb, 0x31, 0xdc, 0x78, 0x2c, 0xc7, 0x10, 0xa5, 0x9c, 0x9e, 0x59, 0x92, 0x51, 0x9a, + 0xa4, 0x97, 0x9c, 0x9f, 0xab, 0x0f, 0x31, 0x0d, 0x4a, 0xe9, 0x16, 0xa7, 0x64, 0xeb, 0x43, 0x0c, + 0x4d, 0x62, 0x03, 0x87, 0x9f, 0x31, 0x20, 0x00, 0x00, 0xff, 0xff, 0x57, 0xf9, 0xe4, 0xd1, 0x55, + 0x01, 0x00, 0x00, } func (m *PublicKey) Marshal() (dAtA []byte, err error) { @@ -221,14 +280,61 @@ func (m *PublicKey_Multisig) MarshalTo(dAtA []byte) (int, error) { func (m *PublicKey_Multisig) MarshalToSizedBuffer(dAtA []byte) (int, error) { i := len(dAtA) if m.Multisig != nil { - i -= len(m.Multisig) - copy(dAtA[i:], m.Multisig) - i = encodeVarintCodec(dAtA, i, uint64(len(m.Multisig))) + { + size, err := m.Multisig.MarshalToSizedBuffer(dAtA[:i]) + if err != nil { + return 0, err + } + i -= size + i = encodeVarintCodec(dAtA, i, uint64(size)) + } i-- dAtA[i] = 0x1a } return len(dAtA) - i, nil } +func (m *MultiSig) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalToSizedBuffer(dAtA[:size]) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *MultiSig) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *MultiSig) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + if len(m.Pubkeys) > 0 { + for iNdEx := len(m.Pubkeys) - 1; iNdEx >= 0; iNdEx-- { + { + size, err := m.Pubkeys[iNdEx].MarshalToSizedBuffer(dAtA[:i]) + if err != nil { + return 0, err + } + i -= size + i = encodeVarintCodec(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0x12 + } + } + if m.K != 0 { + i = encodeVarintCodec(dAtA, i, uint64(m.K)) + i-- + dAtA[i] = 0x8 + } + return len(dAtA) - i, nil +} + func encodeVarintCodec(dAtA []byte, offset int, v uint64) int { offset -= sovCodec(v) base := offset @@ -283,11 +389,28 @@ func (m *PublicKey_Multisig) Size() (n int) { var l int _ = l if m.Multisig != nil { - l = len(m.Multisig) + l = m.Multisig.Size() n += 1 + l + sovCodec(uint64(l)) } return n } +func (m *MultiSig) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + if m.K != 0 { + n += 1 + sovCodec(uint64(m.K)) + } + if len(m.Pubkeys) > 0 { + for _, e := range m.Pubkeys { + l = e.Size() + n += 1 + l + sovCodec(uint64(l)) + } + } + return n +} func sovCodec(x uint64) (n int) { return (math_bits.Len64(x|1) + 6) / 7 @@ -394,7 +517,7 @@ func (m *PublicKey) Unmarshal(dAtA []byte) error { if wireType != 2 { return fmt.Errorf("proto: wrong wireType = %d for field Multisig", wireType) } - var byteLen int + var msglen int for shift := uint(0); ; shift += 7 { if shift >= 64 { return ErrIntOverflowCodec @@ -404,23 +527,25 @@ func (m *PublicKey) Unmarshal(dAtA []byte) error { } b := dAtA[iNdEx] iNdEx++ - byteLen |= int(b&0x7F) << shift + msglen |= int(b&0x7F) << shift if b < 0x80 { break } } - if byteLen < 0 { + if msglen < 0 { return ErrInvalidLengthCodec } - postIndex := iNdEx + byteLen + postIndex := iNdEx + msglen if postIndex < 0 { return ErrInvalidLengthCodec } if postIndex > l { return io.ErrUnexpectedEOF } - v := make([]byte, postIndex-iNdEx) - copy(v, dAtA[iNdEx:postIndex]) + v := &MultiSig{} + if err := v.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } m.Pub = &PublicKey_Multisig{v} iNdEx = postIndex default: @@ -447,6 +572,112 @@ func (m *PublicKey) Unmarshal(dAtA []byte) error { } return nil } +func (m *MultiSig) Unmarshal(dAtA []byte) error { + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowCodec + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: MultiSig: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: MultiSig: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field K", wireType) + } + m.K = 0 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowCodec + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + m.K |= uint32(b&0x7F) << shift + if b < 0x80 { + break + } + } + case 2: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Pubkeys", wireType) + } + var msglen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowCodec + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + msglen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if msglen < 0 { + return ErrInvalidLengthCodec + } + postIndex := iNdEx + msglen + if postIndex < 0 { + return ErrInvalidLengthCodec + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.Pubkeys = append(m.Pubkeys, &PublicKey{}) + if err := m.Pubkeys[len(m.Pubkeys)-1].Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + default: + iNdEx = preIndex + skippy, err := skipCodec(dAtA[iNdEx:]) + if err != nil { + return err + } + if skippy < 0 { + return ErrInvalidLengthCodec + } + if (iNdEx + skippy) < 0 { + return ErrInvalidLengthCodec + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} func skipCodec(dAtA []byte) (n int, err error) { l := len(dAtA) iNdEx := 0 diff --git a/crypto/codec.proto b/crypto/codec.proto index 4ea9967f0eeb..209fbb00e594 100644 --- a/crypto/codec.proto +++ b/crypto/codec.proto @@ -17,6 +17,13 @@ message PublicKey { oneof pub { bytes secp256k1 = 1; bytes ed25519 = 2; - bytes multisig = 3; + MultiSig multisig = 3; } } + +// MultiSig defines a proto message representing a threshold multi-signature +// scheme. +message MultiSig { + uint32 k = 1; + repeated PublicKey pubkeys = 2; +} \ No newline at end of file diff --git a/crypto/pubkey.go b/crypto/pubkey.go index 9aee95e09a02..a9492ccc13d2 100644 --- a/crypto/pubkey.go +++ b/crypto/pubkey.go @@ -2,7 +2,9 @@ package crypto import ( "github.com/pkg/errors" + tmcrypto "github.com/tendermint/tendermint/crypto" "github.com/tendermint/tendermint/crypto/ed25519" + "github.com/tendermint/tendermint/crypto/multisig" "github.com/tendermint/tendermint/crypto/secp256k1" ) @@ -10,8 +12,11 @@ import ( var ( ErrInvalidPubKeySecp256k1Length = errors.New("invalid PubKeySecp256k1 length") ErrInvalidPubKeySecp256k1 = errors.New("incompatible PubKeySecp256k1") - ErrInvalidPubKeyEd25519Length = errors.New("invalid PubKeyEd25519 length") - ErrInvalidPubKeyEd25519 = errors.New("incompatible PubKeyEd25519") + + ErrInvalidPubKeyEd25519Length = errors.New("invalid PubKeyEd25519 length") + ErrInvalidPubKeyEd25519 = errors.New("incompatible PubKeyEd25519") + + ErrInvalidPubKeyMultisigThreshold = errors.New("incompatible PubKeyMultisigThreshold") ) // GetPubKeySecp256k1 returns a Tendermint secp256k1 public key from the @@ -53,3 +58,67 @@ func (m *PublicKey) GetPubKeyEd25519() (ed25519.PubKeyEd25519, error) { return pk, ErrInvalidPubKeyEd25519 } + +// GetPubKeyMultisigThreshold returns a Tendermint multi-sig threshold public key +// from the PublicKey message type. It will return an error if the size of the +// public key is invalid or the underlying Pub field is invalid. +// +// NOTE: Do not use or call bytes on the result when serializing. +func (m *PublicKey) GetPubKeyMultisigThreshold() (multisig.PubKeyMultisigThreshold, error) { + mspk := multisig.PubKeyMultisigThreshold{} + + if x, ok := m.GetPub().(*PublicKey_Multisig); ok { + mspk.K = uint(x.Multisig.K) + mspk.PubKeys = make([]tmcrypto.PubKey, len(x.Multisig.Pubkeys)) + + for i, pk := range x.Multisig.Pubkeys { + mspk.PubKeys[i] = pk.TendermintPubKey() + } + + return mspk, nil + } + + return mspk, ErrInvalidPubKeyMultisigThreshold +} + +// TendermintPubKey returns a Tendermint PubKey from the PublicKey proto message +// type. +func (m *PublicKey) TendermintPubKey() tmcrypto.PubKey { + switch m.GetPub().(type) { + case *PublicKey_Secp256K1: + pk, err := m.GetPubKeySecp256k1() + if err != nil { + return nil + } + + return pk + + case *PublicKey_Ed25519: + pk, err := m.GetPubKeyEd25519() + if err != nil { + return nil + } + + return pk + + case *PublicKey_Multisig: + mspk, err := m.GetPubKeyMultisigThreshold() + if err != nil { + return nil + } + + return mspk + + default: + return nil + } +} + +// Address returns the address of a Tendermint PubKey. +func (m *PublicKey) Address() tmcrypto.Address { + if pk := m.TendermintPubKey(); pk != nil { + return pk.Address() + } + + return nil +} diff --git a/crypto/pubkey_test.go b/crypto/pubkey_test.go index d53d48026f5b..521a08328fc7 100644 --- a/crypto/pubkey_test.go +++ b/crypto/pubkey_test.go @@ -4,8 +4,11 @@ import ( "testing" "github.com/cosmos/cosmos-sdk/crypto" + "github.com/stretchr/testify/require" + tmcrypto "github.com/tendermint/tendermint/crypto" "github.com/tendermint/tendermint/crypto/ed25519" + "github.com/tendermint/tendermint/crypto/multisig" "github.com/tendermint/tendermint/crypto/secp256k1" ) @@ -70,3 +73,40 @@ func TestGetPubKeyEd25519(t *testing.T) { require.True(t, pk.Equals(tc.expected)) } } + +func TestAddress(t *testing.T) { + ed25519pk := (ed25519.GenPrivKey().PubKey()).(ed25519.PubKeyEd25519) + secp256k1pk := (secp256k1.GenPrivKey().PubKey()).(secp256k1.PubKeySecp256k1) + secp256k1pk2 := (secp256k1.GenPrivKey().PubKey()).(secp256k1.PubKeySecp256k1) + multisigpk := multisig.NewPubKeyMultisigThreshold(2, []tmcrypto.PubKey{secp256k1pk, secp256k1pk2}) + + testCases := []struct { + pubkey tmcrypto.PubKey + input *crypto.PublicKey + }{ + { + ed25519pk, &crypto.PublicKey{&crypto.PublicKey_Ed25519{ed25519pk[:]}}, + }, + { + secp256k1pk, &crypto.PublicKey{&crypto.PublicKey_Secp256K1{secp256k1pk[:]}}, + }, + { + multisigpk, + &crypto.PublicKey{ + &crypto.PublicKey_Multisig{ + Multisig: &crypto.MultiSig{ + K: 2, + Pubkeys: []*crypto.PublicKey{ + {&crypto.PublicKey_Secp256K1{secp256k1pk[:]}}, + {&crypto.PublicKey_Secp256K1{secp256k1pk2[:]}}, + }, + }, + }, + }, + }, + } + + for _, tc := range testCases { + require.Equal(t, tc.pubkey.Address(), tc.input.Address()) + } +} diff --git a/go.mod b/go.mod index bb0a000b84a3..d8645982e275 100644 --- a/go.mod +++ b/go.mod @@ -16,6 +16,7 @@ require ( github.com/pelletier/go-toml v1.6.0 github.com/pkg/errors v0.8.1 github.com/rakyll/statik v0.1.6 + github.com/regen-network/cosmos-proto v0.0.0-20200103162838-2560887837f1 // indirect github.com/spf13/afero v1.2.1 // indirect github.com/spf13/cobra v0.0.5 github.com/spf13/jwalterweatherman v1.1.0 // indirect diff --git a/go.sum b/go.sum index cc5d9b873f77..8e7d48db473d 100644 --- a/go.sum +++ b/go.sum @@ -199,6 +199,8 @@ github.com/rakyll/statik v0.1.6 h1:uICcfUXpgqtw2VopbIncslhAmE5hwc4g20TEyEENBNs= github.com/rakyll/statik v0.1.6/go.mod h1:OEi9wJV/fMUAGx1eNjq75DKDsJVuEv1U0oYdX6GX8Zs= github.com/rcrowley/go-metrics v0.0.0-20180503174638-e2704e165165 h1:nkcn14uNmFEuGCb2mBZbBb24RdNRL08b/wb+xBOYpuk= github.com/rcrowley/go-metrics v0.0.0-20180503174638-e2704e165165/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4= +github.com/regen-network/cosmos-proto v0.0.0-20200103162838-2560887837f1 h1:3NogdWUoYfVSgTstfMgz+yaIWnAececvl3lI+/mZq8M= +github.com/regen-network/cosmos-proto v0.0.0-20200103162838-2560887837f1/go.mod h1:z+OxEChBwVwsOhJeKyvS6WqPRFDV/w2hhwDF6qpi4dA= github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg= github.com/rs/cors v1.6.0 h1:G9tHG9lebljV9mfp9SNPDL36nCDxmo3zTlAf1YgvzmI= github.com/rs/cors v1.6.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU= From 27a3fa2c940be9515dd982fb1de1f92bd3b63bd9 Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Fri, 3 Jan 2020 15:38:17 -0500 Subject: [PATCH 35/40] Updates to crypto proto API --- crypto/pubkey.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/crypto/pubkey.go b/crypto/pubkey.go index a9492ccc13d2..1df6b607665a 100644 --- a/crypto/pubkey.go +++ b/crypto/pubkey.go @@ -19,6 +19,8 @@ var ( ErrInvalidPubKeyMultisigThreshold = errors.New("incompatible PubKeyMultisigThreshold") ) +var _ tmcrypto.PubKey = (*PublicKey)(nil) + // GetPubKeySecp256k1 returns a Tendermint secp256k1 public key from the // PublicKey message type. It will return an error if the size of the public key // is invalid or the underlying Pub field is invalid. @@ -122,3 +124,35 @@ func (m *PublicKey) Address() tmcrypto.Address { return nil } + +// VerifyBytes attempts to construct a Tendermint PubKey and delegates the +// VerifyBytes call to the constructed type. +func (m *PublicKey) VerifyBytes(msg []byte, sig []byte) bool { + if pk := m.TendermintPubKey(); pk != nil { + return pk.VerifyBytes(msg, sig) + } + + return false +} + +// Equals attempts to construct a Tendermint PubKey and delegates the Equals call +// to the constructed type. +func (m *PublicKey) Equals(other tmcrypto.PubKey) bool { + if pk := m.TendermintPubKey(); pk != nil { + return pk.Equals(other) + } + + return false +} + +// Bytes returns the raw proto encoded bytes of the PublicKey proto message type. +// Note, this is not wire compatible with calling Bytes on the underlying +// Tendermint PubKey type. +func (m *PublicKey) Bytes() []byte { + bz, err := m.Marshal() + if err != nil { + panic(err) + } + + return bz +} From 1c07bc0b3550b9fdbc52e793ca40d76574472100 Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Fri, 3 Jan 2020 17:00:53 -0500 Subject: [PATCH 36/40] Move crypto proto to types --- crypto/codec.pb.go | 764 ------------------------------- crypto/codec.proto | 29 -- crypto/ledger_test.go | 18 +- types/address.go | 149 ++---- types/address_test.go | 33 +- types/codec.pb.go | 665 ++++++++++++++++++++++++++- types/codec.proto | 43 +- {crypto => types}/pubkey.go | 2 +- {crypto => types}/pubkey_test.go | 46 +- 9 files changed, 781 insertions(+), 968 deletions(-) delete mode 100644 crypto/codec.pb.go delete mode 100644 crypto/codec.proto rename {crypto => types}/pubkey.go (99%) rename {crypto => types}/pubkey_test.go (63%) diff --git a/crypto/codec.pb.go b/crypto/codec.pb.go deleted file mode 100644 index cefd67a69527..000000000000 --- a/crypto/codec.pb.go +++ /dev/null @@ -1,764 +0,0 @@ -// Code generated by protoc-gen-gogo. DO NOT EDIT. -// source: crypto/codec.proto - -package crypto - -import ( - fmt "fmt" - proto "github.com/gogo/protobuf/proto" - io "io" - math "math" - math_bits "math/bits" -) - -// Reference imports to suppress errors if they are not otherwise used. -var _ = proto.Marshal -var _ = fmt.Errorf -var _ = math.Inf - -// This is a compile-time assertion to ensure that this generated file -// is compatible with the proto package it is being compiled against. -// A compilation error at this line likely means your copy of the -// proto package needs to be updated. -const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package - -// PublicKey defines a proto message with a single oneof field that represents a -// public key as a byte slice. -// -// NOTE: Private keys must still be amino-encoded for backwards compatiblity. -type PublicKey struct { - // pub defines an single supported public key type as a byte slice. - // - // Types that are valid to be assigned to Pub: - // *PublicKey_Secp256K1 - // *PublicKey_Ed25519 - // *PublicKey_Multisig - Pub isPublicKey_Pub `protobuf_oneof:"pub"` -} - -func (m *PublicKey) Reset() { *m = PublicKey{} } -func (m *PublicKey) String() string { return proto.CompactTextString(m) } -func (*PublicKey) ProtoMessage() {} -func (*PublicKey) Descriptor() ([]byte, []int) { - return fileDescriptor_0402281428f37664, []int{0} -} -func (m *PublicKey) XXX_Unmarshal(b []byte) error { - return m.Unmarshal(b) -} -func (m *PublicKey) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { - if deterministic { - return xxx_messageInfo_PublicKey.Marshal(b, m, deterministic) - } else { - b = b[:cap(b)] - n, err := m.MarshalToSizedBuffer(b) - if err != nil { - return nil, err - } - return b[:n], nil - } -} -func (m *PublicKey) XXX_Merge(src proto.Message) { - xxx_messageInfo_PublicKey.Merge(m, src) -} -func (m *PublicKey) XXX_Size() int { - return m.Size() -} -func (m *PublicKey) XXX_DiscardUnknown() { - xxx_messageInfo_PublicKey.DiscardUnknown(m) -} - -var xxx_messageInfo_PublicKey proto.InternalMessageInfo - -type isPublicKey_Pub interface { - isPublicKey_Pub() - MarshalTo([]byte) (int, error) - Size() int -} - -type PublicKey_Secp256K1 struct { - Secp256K1 []byte `protobuf:"bytes,1,opt,name=secp256k1,proto3,oneof" json:"secp256k1,omitempty"` -} -type PublicKey_Ed25519 struct { - Ed25519 []byte `protobuf:"bytes,2,opt,name=ed25519,proto3,oneof" json:"ed25519,omitempty"` -} -type PublicKey_Multisig struct { - Multisig *MultiSig `protobuf:"bytes,3,opt,name=multisig,proto3,oneof" json:"multisig,omitempty"` -} - -func (*PublicKey_Secp256K1) isPublicKey_Pub() {} -func (*PublicKey_Ed25519) isPublicKey_Pub() {} -func (*PublicKey_Multisig) isPublicKey_Pub() {} - -func (m *PublicKey) GetPub() isPublicKey_Pub { - if m != nil { - return m.Pub - } - return nil -} - -func (m *PublicKey) GetSecp256K1() []byte { - if x, ok := m.GetPub().(*PublicKey_Secp256K1); ok { - return x.Secp256K1 - } - return nil -} - -func (m *PublicKey) GetEd25519() []byte { - if x, ok := m.GetPub().(*PublicKey_Ed25519); ok { - return x.Ed25519 - } - return nil -} - -func (m *PublicKey) GetMultisig() *MultiSig { - if x, ok := m.GetPub().(*PublicKey_Multisig); ok { - return x.Multisig - } - return nil -} - -// XXX_OneofWrappers is for the internal use of the proto package. -func (*PublicKey) XXX_OneofWrappers() []interface{} { - return []interface{}{ - (*PublicKey_Secp256K1)(nil), - (*PublicKey_Ed25519)(nil), - (*PublicKey_Multisig)(nil), - } -} - -// MultiSig defines a proto message representing a threshold multi-signature -// scheme. -type MultiSig struct { - K uint32 `protobuf:"varint,1,opt,name=k,proto3" json:"k,omitempty"` - Pubkeys []*PublicKey `protobuf:"bytes,2,rep,name=pubkeys,proto3" json:"pubkeys,omitempty"` -} - -func (m *MultiSig) Reset() { *m = MultiSig{} } -func (m *MultiSig) String() string { return proto.CompactTextString(m) } -func (*MultiSig) ProtoMessage() {} -func (*MultiSig) Descriptor() ([]byte, []int) { - return fileDescriptor_0402281428f37664, []int{1} -} -func (m *MultiSig) XXX_Unmarshal(b []byte) error { - return m.Unmarshal(b) -} -func (m *MultiSig) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { - if deterministic { - return xxx_messageInfo_MultiSig.Marshal(b, m, deterministic) - } else { - b = b[:cap(b)] - n, err := m.MarshalToSizedBuffer(b) - if err != nil { - return nil, err - } - return b[:n], nil - } -} -func (m *MultiSig) XXX_Merge(src proto.Message) { - xxx_messageInfo_MultiSig.Merge(m, src) -} -func (m *MultiSig) XXX_Size() int { - return m.Size() -} -func (m *MultiSig) XXX_DiscardUnknown() { - xxx_messageInfo_MultiSig.DiscardUnknown(m) -} - -var xxx_messageInfo_MultiSig proto.InternalMessageInfo - -func (m *MultiSig) GetK() uint32 { - if m != nil { - return m.K - } - return 0 -} - -func (m *MultiSig) GetPubkeys() []*PublicKey { - if m != nil { - return m.Pubkeys - } - return nil -} - -func init() { - proto.RegisterType((*PublicKey)(nil), "cosmos_sdk.crypto.v1.PublicKey") - proto.RegisterType((*MultiSig)(nil), "cosmos_sdk.crypto.v1.MultiSig") -} - -func init() { proto.RegisterFile("crypto/codec.proto", fileDescriptor_0402281428f37664) } - -var fileDescriptor_0402281428f37664 = []byte{ - // 259 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x12, 0x4a, 0x2e, 0xaa, 0x2c, - 0x28, 0xc9, 0xd7, 0x4f, 0xce, 0x4f, 0x49, 0x4d, 0xd6, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x12, - 0x49, 0xce, 0x2f, 0xce, 0xcd, 0x2f, 0x8e, 0x2f, 0x4e, 0xc9, 0xd6, 0x83, 0x48, 0xeb, 0x95, 0x19, - 0x2a, 0xf5, 0x30, 0x72, 0x71, 0x06, 0x94, 0x26, 0xe5, 0x64, 0x26, 0x7b, 0xa7, 0x56, 0x0a, 0xc9, - 0x71, 0x71, 0x16, 0xa7, 0x26, 0x17, 0x18, 0x99, 0x9a, 0x65, 0x1b, 0x4a, 0x30, 0x2a, 0x30, 0x6a, - 0xf0, 0x78, 0x30, 0x04, 0x21, 0x84, 0x84, 0xa4, 0xb8, 0xd8, 0x53, 0x53, 0x8c, 0x4c, 0x4d, 0x0d, - 0x2d, 0x25, 0x98, 0xa0, 0xb2, 0x30, 0x01, 0x21, 0x1b, 0x2e, 0x8e, 0xdc, 0xd2, 0x9c, 0x92, 0xcc, - 0xe2, 0xcc, 0x74, 0x09, 0x66, 0x05, 0x46, 0x0d, 0x6e, 0x23, 0x39, 0x3d, 0x6c, 0x56, 0xea, 0xf9, - 0x82, 0x54, 0x05, 0x67, 0xa6, 0x7b, 0x30, 0x04, 0xc1, 0x75, 0x38, 0xb1, 0x72, 0x31, 0x17, 0x94, - 0x26, 0x29, 0x05, 0x73, 0x71, 0xc0, 0xa4, 0x85, 0x78, 0xb8, 0x18, 0xb3, 0xc1, 0x8e, 0xe0, 0x0d, - 0x62, 0xcc, 0x16, 0xb2, 0xe4, 0x62, 0x2f, 0x28, 0x4d, 0xca, 0x4e, 0xad, 0x2c, 0x96, 0x60, 0x52, - 0x60, 0xd6, 0xe0, 0x36, 0x92, 0xc7, 0x6e, 0x3a, 0xdc, 0x33, 0x41, 0x30, 0xf5, 0x4e, 0xb6, 0x27, - 0x1e, 0xc9, 0x31, 0x5e, 0x78, 0x24, 0xc7, 0xf8, 0xe0, 0x91, 0x1c, 0xe3, 0x84, 0xc7, 0x72, 0x0c, - 0x17, 0x1e, 0xcb, 0x31, 0xdc, 0x78, 0x2c, 0xc7, 0x10, 0xa5, 0x9c, 0x9e, 0x59, 0x92, 0x51, 0x9a, - 0xa4, 0x97, 0x9c, 0x9f, 0xab, 0x0f, 0x31, 0x0d, 0x4a, 0xe9, 0x16, 0xa7, 0x64, 0xeb, 0x43, 0x0c, - 0x4d, 0x62, 0x03, 0x87, 0x9f, 0x31, 0x20, 0x00, 0x00, 0xff, 0xff, 0x57, 0xf9, 0xe4, 0xd1, 0x55, - 0x01, 0x00, 0x00, -} - -func (m *PublicKey) Marshal() (dAtA []byte, err error) { - size := m.Size() - dAtA = make([]byte, size) - n, err := m.MarshalToSizedBuffer(dAtA[:size]) - if err != nil { - return nil, err - } - return dAtA[:n], nil -} - -func (m *PublicKey) MarshalTo(dAtA []byte) (int, error) { - size := m.Size() - return m.MarshalToSizedBuffer(dAtA[:size]) -} - -func (m *PublicKey) MarshalToSizedBuffer(dAtA []byte) (int, error) { - i := len(dAtA) - _ = i - var l int - _ = l - if m.Pub != nil { - { - size := m.Pub.Size() - i -= size - if _, err := m.Pub.MarshalTo(dAtA[i:]); err != nil { - return 0, err - } - } - } - return len(dAtA) - i, nil -} - -func (m *PublicKey_Secp256K1) MarshalTo(dAtA []byte) (int, error) { - size := m.Size() - return m.MarshalToSizedBuffer(dAtA[:size]) -} - -func (m *PublicKey_Secp256K1) MarshalToSizedBuffer(dAtA []byte) (int, error) { - i := len(dAtA) - if m.Secp256K1 != nil { - i -= len(m.Secp256K1) - copy(dAtA[i:], m.Secp256K1) - i = encodeVarintCodec(dAtA, i, uint64(len(m.Secp256K1))) - i-- - dAtA[i] = 0xa - } - return len(dAtA) - i, nil -} -func (m *PublicKey_Ed25519) MarshalTo(dAtA []byte) (int, error) { - size := m.Size() - return m.MarshalToSizedBuffer(dAtA[:size]) -} - -func (m *PublicKey_Ed25519) MarshalToSizedBuffer(dAtA []byte) (int, error) { - i := len(dAtA) - if m.Ed25519 != nil { - i -= len(m.Ed25519) - copy(dAtA[i:], m.Ed25519) - i = encodeVarintCodec(dAtA, i, uint64(len(m.Ed25519))) - i-- - dAtA[i] = 0x12 - } - return len(dAtA) - i, nil -} -func (m *PublicKey_Multisig) MarshalTo(dAtA []byte) (int, error) { - size := m.Size() - return m.MarshalToSizedBuffer(dAtA[:size]) -} - -func (m *PublicKey_Multisig) MarshalToSizedBuffer(dAtA []byte) (int, error) { - i := len(dAtA) - if m.Multisig != nil { - { - size, err := m.Multisig.MarshalToSizedBuffer(dAtA[:i]) - if err != nil { - return 0, err - } - i -= size - i = encodeVarintCodec(dAtA, i, uint64(size)) - } - i-- - dAtA[i] = 0x1a - } - return len(dAtA) - i, nil -} -func (m *MultiSig) Marshal() (dAtA []byte, err error) { - size := m.Size() - dAtA = make([]byte, size) - n, err := m.MarshalToSizedBuffer(dAtA[:size]) - if err != nil { - return nil, err - } - return dAtA[:n], nil -} - -func (m *MultiSig) MarshalTo(dAtA []byte) (int, error) { - size := m.Size() - return m.MarshalToSizedBuffer(dAtA[:size]) -} - -func (m *MultiSig) MarshalToSizedBuffer(dAtA []byte) (int, error) { - i := len(dAtA) - _ = i - var l int - _ = l - if len(m.Pubkeys) > 0 { - for iNdEx := len(m.Pubkeys) - 1; iNdEx >= 0; iNdEx-- { - { - size, err := m.Pubkeys[iNdEx].MarshalToSizedBuffer(dAtA[:i]) - if err != nil { - return 0, err - } - i -= size - i = encodeVarintCodec(dAtA, i, uint64(size)) - } - i-- - dAtA[i] = 0x12 - } - } - if m.K != 0 { - i = encodeVarintCodec(dAtA, i, uint64(m.K)) - i-- - dAtA[i] = 0x8 - } - return len(dAtA) - i, nil -} - -func encodeVarintCodec(dAtA []byte, offset int, v uint64) int { - offset -= sovCodec(v) - base := offset - for v >= 1<<7 { - dAtA[offset] = uint8(v&0x7f | 0x80) - v >>= 7 - offset++ - } - dAtA[offset] = uint8(v) - return base -} -func (m *PublicKey) Size() (n int) { - if m == nil { - return 0 - } - var l int - _ = l - if m.Pub != nil { - n += m.Pub.Size() - } - return n -} - -func (m *PublicKey_Secp256K1) Size() (n int) { - if m == nil { - return 0 - } - var l int - _ = l - if m.Secp256K1 != nil { - l = len(m.Secp256K1) - n += 1 + l + sovCodec(uint64(l)) - } - return n -} -func (m *PublicKey_Ed25519) Size() (n int) { - if m == nil { - return 0 - } - var l int - _ = l - if m.Ed25519 != nil { - l = len(m.Ed25519) - n += 1 + l + sovCodec(uint64(l)) - } - return n -} -func (m *PublicKey_Multisig) Size() (n int) { - if m == nil { - return 0 - } - var l int - _ = l - if m.Multisig != nil { - l = m.Multisig.Size() - n += 1 + l + sovCodec(uint64(l)) - } - return n -} -func (m *MultiSig) Size() (n int) { - if m == nil { - return 0 - } - var l int - _ = l - if m.K != 0 { - n += 1 + sovCodec(uint64(m.K)) - } - if len(m.Pubkeys) > 0 { - for _, e := range m.Pubkeys { - l = e.Size() - n += 1 + l + sovCodec(uint64(l)) - } - } - return n -} - -func sovCodec(x uint64) (n int) { - return (math_bits.Len64(x|1) + 6) / 7 -} -func sozCodec(x uint64) (n int) { - return sovCodec(uint64((x << 1) ^ uint64((int64(x) >> 63)))) -} -func (m *PublicKey) Unmarshal(dAtA []byte) error { - l := len(dAtA) - iNdEx := 0 - for iNdEx < l { - preIndex := iNdEx - var wire uint64 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowCodec - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - wire |= uint64(b&0x7F) << shift - if b < 0x80 { - break - } - } - fieldNum := int32(wire >> 3) - wireType := int(wire & 0x7) - if wireType == 4 { - return fmt.Errorf("proto: PublicKey: wiretype end group for non-group") - } - if fieldNum <= 0 { - return fmt.Errorf("proto: PublicKey: illegal tag %d (wire type %d)", fieldNum, wire) - } - switch fieldNum { - case 1: - if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field Secp256K1", wireType) - } - var byteLen int - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowCodec - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - byteLen |= int(b&0x7F) << shift - if b < 0x80 { - break - } - } - if byteLen < 0 { - return ErrInvalidLengthCodec - } - postIndex := iNdEx + byteLen - if postIndex < 0 { - return ErrInvalidLengthCodec - } - if postIndex > l { - return io.ErrUnexpectedEOF - } - v := make([]byte, postIndex-iNdEx) - copy(v, dAtA[iNdEx:postIndex]) - m.Pub = &PublicKey_Secp256K1{v} - iNdEx = postIndex - case 2: - if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field Ed25519", wireType) - } - var byteLen int - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowCodec - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - byteLen |= int(b&0x7F) << shift - if b < 0x80 { - break - } - } - if byteLen < 0 { - return ErrInvalidLengthCodec - } - postIndex := iNdEx + byteLen - if postIndex < 0 { - return ErrInvalidLengthCodec - } - if postIndex > l { - return io.ErrUnexpectedEOF - } - v := make([]byte, postIndex-iNdEx) - copy(v, dAtA[iNdEx:postIndex]) - m.Pub = &PublicKey_Ed25519{v} - iNdEx = postIndex - case 3: - if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field Multisig", wireType) - } - var msglen int - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowCodec - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - msglen |= int(b&0x7F) << shift - if b < 0x80 { - break - } - } - if msglen < 0 { - return ErrInvalidLengthCodec - } - postIndex := iNdEx + msglen - if postIndex < 0 { - return ErrInvalidLengthCodec - } - if postIndex > l { - return io.ErrUnexpectedEOF - } - v := &MultiSig{} - if err := v.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { - return err - } - m.Pub = &PublicKey_Multisig{v} - iNdEx = postIndex - default: - iNdEx = preIndex - skippy, err := skipCodec(dAtA[iNdEx:]) - if err != nil { - return err - } - if skippy < 0 { - return ErrInvalidLengthCodec - } - if (iNdEx + skippy) < 0 { - return ErrInvalidLengthCodec - } - if (iNdEx + skippy) > l { - return io.ErrUnexpectedEOF - } - iNdEx += skippy - } - } - - if iNdEx > l { - return io.ErrUnexpectedEOF - } - return nil -} -func (m *MultiSig) Unmarshal(dAtA []byte) error { - l := len(dAtA) - iNdEx := 0 - for iNdEx < l { - preIndex := iNdEx - var wire uint64 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowCodec - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - wire |= uint64(b&0x7F) << shift - if b < 0x80 { - break - } - } - fieldNum := int32(wire >> 3) - wireType := int(wire & 0x7) - if wireType == 4 { - return fmt.Errorf("proto: MultiSig: wiretype end group for non-group") - } - if fieldNum <= 0 { - return fmt.Errorf("proto: MultiSig: illegal tag %d (wire type %d)", fieldNum, wire) - } - switch fieldNum { - case 1: - if wireType != 0 { - return fmt.Errorf("proto: wrong wireType = %d for field K", wireType) - } - m.K = 0 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowCodec - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - m.K |= uint32(b&0x7F) << shift - if b < 0x80 { - break - } - } - case 2: - if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field Pubkeys", wireType) - } - var msglen int - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowCodec - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - msglen |= int(b&0x7F) << shift - if b < 0x80 { - break - } - } - if msglen < 0 { - return ErrInvalidLengthCodec - } - postIndex := iNdEx + msglen - if postIndex < 0 { - return ErrInvalidLengthCodec - } - if postIndex > l { - return io.ErrUnexpectedEOF - } - m.Pubkeys = append(m.Pubkeys, &PublicKey{}) - if err := m.Pubkeys[len(m.Pubkeys)-1].Unmarshal(dAtA[iNdEx:postIndex]); err != nil { - return err - } - iNdEx = postIndex - default: - iNdEx = preIndex - skippy, err := skipCodec(dAtA[iNdEx:]) - if err != nil { - return err - } - if skippy < 0 { - return ErrInvalidLengthCodec - } - if (iNdEx + skippy) < 0 { - return ErrInvalidLengthCodec - } - if (iNdEx + skippy) > l { - return io.ErrUnexpectedEOF - } - iNdEx += skippy - } - } - - if iNdEx > l { - return io.ErrUnexpectedEOF - } - return nil -} -func skipCodec(dAtA []byte) (n int, err error) { - l := len(dAtA) - iNdEx := 0 - depth := 0 - for iNdEx < l { - var wire uint64 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return 0, ErrIntOverflowCodec - } - if iNdEx >= l { - return 0, io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - wire |= (uint64(b) & 0x7F) << shift - if b < 0x80 { - break - } - } - wireType := int(wire & 0x7) - switch wireType { - case 0: - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return 0, ErrIntOverflowCodec - } - if iNdEx >= l { - return 0, io.ErrUnexpectedEOF - } - iNdEx++ - if dAtA[iNdEx-1] < 0x80 { - break - } - } - case 1: - iNdEx += 8 - case 2: - var length int - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return 0, ErrIntOverflowCodec - } - if iNdEx >= l { - return 0, io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - length |= (int(b) & 0x7F) << shift - if b < 0x80 { - break - } - } - if length < 0 { - return 0, ErrInvalidLengthCodec - } - iNdEx += length - case 3: - depth++ - case 4: - if depth == 0 { - return 0, ErrUnexpectedEndOfGroupCodec - } - depth-- - case 5: - iNdEx += 4 - default: - return 0, fmt.Errorf("proto: illegal wireType %d", wireType) - } - if iNdEx < 0 { - return 0, ErrInvalidLengthCodec - } - if depth == 0 { - return iNdEx, nil - } - } - return 0, io.ErrUnexpectedEOF -} - -var ( - ErrInvalidLengthCodec = fmt.Errorf("proto: negative length found during unmarshaling") - ErrIntOverflowCodec = fmt.Errorf("proto: integer overflow") - ErrUnexpectedEndOfGroupCodec = fmt.Errorf("proto: unexpected end of group") -) diff --git a/crypto/codec.proto b/crypto/codec.proto deleted file mode 100644 index 209fbb00e594..000000000000 --- a/crypto/codec.proto +++ /dev/null @@ -1,29 +0,0 @@ -syntax = "proto3"; -package cosmos_sdk.crypto.v1; - -option go_package = "github.com/cosmos/cosmos-sdk/crypto"; - -// PublicKey defines a proto message with a single oneof field that represents a -// public key as a byte slice. -// -// NOTE: Private keys must still be amino-encoded for backwards compatiblity. -message PublicKey { - // TODO: Make tendermint keys use []byte instead of [32]byte so we can use the - // casttype extension. - // option (cosmos_proto.interface_type) = - // "github.com/tendermint/tendermint/crypto.PubKey"; - - // pub defines an single supported public key type as a byte slice. - oneof pub { - bytes secp256k1 = 1; - bytes ed25519 = 2; - MultiSig multisig = 3; - } -} - -// MultiSig defines a proto message representing a threshold multi-signature -// scheme. -message MultiSig { - uint32 k = 1; - repeated PublicKey pubkeys = 2; -} \ No newline at end of file diff --git a/crypto/ledger_test.go b/crypto/ledger_test.go index 0f726b410d55..a2876ffe75df 100644 --- a/crypto/ledger_test.go +++ b/crypto/ledger_test.go @@ -8,9 +8,11 @@ import ( tmcrypto "github.com/tendermint/tendermint/crypto" cryptoAmino "github.com/tendermint/tendermint/crypto/encoding/amino" + "github.com/tendermint/tendermint/crypto/secp256k1" "github.com/cosmos/cosmos-sdk/crypto/keys/hd" "github.com/cosmos/cosmos-sdk/tests" + "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types" ) @@ -32,7 +34,9 @@ func TestPublicKeyUnsafe(t *testing.T) { fmt.Sprintf("%x", priv.PubKey().Bytes()), "Is your device using test mnemonic: %s ?", tests.TestMnemonic) - pubKeyAddr, err := sdk.Bech32ifyAccPub(priv.PubKey()) + pubRaw := priv.PubKey().(secp256k1.PubKeySecp256k1) + pk := &sdk.PublicKey{&types.PublicKey_Ed25519{pubRaw[:]}} + pubKeyAddr, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, pk) require.NoError(t, err) require.Equal(t, "cosmospub1addwnpepqd87l8xhcnrrtzxnkql7k55ph8fr9jarf4hn6udwukfprlalu8lgw0urza0", pubKeyAddr, "Is your device using test mnemonic: %s ?", tests.TestMnemonic) @@ -74,7 +78,9 @@ func TestPublicKeyUnsafeHDPath(t *testing.T) { tmp := priv.(PrivKeyLedgerSecp256k1) (&tmp).AssertIsPrivKeyInner() - pubKeyAddr, err := sdk.Bech32ifyAccPub(priv.PubKey()) + pubRaw := priv.PubKey().(secp256k1.PubKeySecp256k1) + pk := &sdk.PublicKey{&types.PublicKey_Ed25519{pubRaw[:]}} + pubKeyAddr, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, pk) require.NoError(t, err) require.Equal(t, expectedAnswers[i], pubKeyAddr, @@ -108,7 +114,9 @@ func TestPublicKeySafe(t *testing.T) { fmt.Sprintf("%x", priv.PubKey().Bytes()), "Is your device using test mnemonic: %s ?", tests.TestMnemonic) - pubKeyAddr, err := sdk.Bech32ifyAccPub(priv.PubKey()) + pubRaw := priv.PubKey().(secp256k1.PubKeySecp256k1) + pk := &sdk.PublicKey{&types.PublicKey_Ed25519{pubRaw[:]}} + pubKeyAddr, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, pk) require.NoError(t, err) require.Equal(t, "cosmospub1addwnpepqd87l8xhcnrrtzxnkql7k55ph8fr9jarf4hn6udwukfprlalu8lgw0urza0", pubKeyAddr, "Is your device using test mnemonic: %s ?", tests.TestMnemonic) @@ -172,7 +180,9 @@ func TestPublicKeyHDPath(t *testing.T) { tmp := priv.(PrivKeyLedgerSecp256k1) (&tmp).AssertIsPrivKeyInner() - pubKeyAddr, err := sdk.Bech32ifyAccPub(priv.PubKey()) + pubRaw := priv.PubKey().(secp256k1.PubKeySecp256k1) + pk := &sdk.PublicKey{&types.PublicKey_Ed25519{pubRaw[:]}} + pubKeyAddr, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, pk) require.NoError(t, err) require.Equal(t, expectedPubKeys[i], pubKeyAddr, diff --git a/types/address.go b/types/address.go index 19e81a1a7712..1e2f1b35d556 100644 --- a/types/address.go +++ b/types/address.go @@ -8,11 +8,9 @@ import ( "fmt" "strings" - "github.com/tendermint/tendermint/crypto" - cryptoAmino "github.com/tendermint/tendermint/crypto/encoding/amino" - yaml "gopkg.in/yaml.v2" - + tmcrypto "github.com/tendermint/tendermint/crypto" "github.com/tendermint/tendermint/libs/bech32" + yaml "gopkg.in/yaml.v2" ) const ( @@ -454,7 +452,7 @@ func ConsAddressFromBech32(address string) (addr ConsAddress, err error) { } // get ConsAddress from pubkey -func GetConsAddress(pubkey crypto.PubKey) ConsAddress { +func GetConsAddress(pubkey tmcrypto.PubKey) ConsAddress { return ConsAddress(pubkey.Address()) } @@ -574,140 +572,83 @@ func (ca ConsAddress) Format(s fmt.State, verb rune) { // auxiliary // ---------------------------------------------------------------------------- -// Bech32ifyAccPub returns a Bech32 encoded string containing the -// Bech32PrefixAccPub prefix for a given account PubKey. -func Bech32ifyAccPub(pub crypto.PubKey) (string, error) { - bech32PrefixAccPub := GetConfig().GetBech32AccountPubPrefix() - return bech32.ConvertAndEncode(bech32PrefixAccPub, pub.Bytes()) -} - -// MustBech32ifyAccPub returns the result of Bech32ifyAccPub panicing on failure. -func MustBech32ifyAccPub(pub crypto.PubKey) string { - enc, err := Bech32ifyAccPub(pub) - if err != nil { - panic(err) - } - - return enc -} - -// Bech32ifyValPub returns a Bech32 encoded string containing the -// Bech32PrefixValPub prefix for a given validator operator's PubKey. -func Bech32ifyValPub(pub crypto.PubKey) (string, error) { - bech32PrefixValPub := GetConfig().GetBech32ValidatorPubPrefix() - return bech32.ConvertAndEncode(bech32PrefixValPub, pub.Bytes()) -} - -// MustBech32ifyValPub returns the result of Bech32ifyValPub panicing on failure. -func MustBech32ifyValPub(pub crypto.PubKey) string { - enc, err := Bech32ifyValPub(pub) - if err != nil { - panic(err) - } +type Bech32PubKeyType string - return enc -} +// Bech32 conversion constants +const ( + Bech32PubKeyTypeAccPub Bech32PubKeyType = "accpub" + Bech32PubKeyTypeValPub Bech32PubKeyType = "valpub" + Bech32PubKeyTypeConsPub Bech32PubKeyType = "conspub" +) -// Bech32ifyConsPub returns a Bech32 encoded string containing the -// Bech32PrefixConsPub prefixfor a given consensus node's PubKey. -func Bech32ifyConsPub(pub crypto.PubKey) (string, error) { - bech32PrefixConsPub := GetConfig().GetBech32ConsensusPubPrefix() - return bech32.ConvertAndEncode(bech32PrefixConsPub, pub.Bytes()) -} +// Bech32ifyPubKey returns a Bech32 encoded string containing the appropriate +// prefix based on the key type provided for a given PublicKey. +func Bech32ifyPubKey(pkt Bech32PubKeyType, pubkey *PublicKey) (string, error) { + var bech32Prefix string -// MustBech32ifyConsPub returns the result of Bech32ifyConsPub panicing on -// failure. -func MustBech32ifyConsPub(pub crypto.PubKey) string { - enc, err := Bech32ifyConsPub(pub) - if err != nil { - panic(err) - } + switch pkt { + case Bech32PubKeyTypeAccPub: + bech32Prefix = GetConfig().GetBech32AccountPubPrefix() - return enc -} + case Bech32PubKeyTypeValPub: + bech32Prefix = GetConfig().GetBech32ValidatorPubPrefix() -// GetAccPubKeyBech32 creates a PubKey for an account with a given public key -// string using the Bech32 Bech32PrefixAccPub prefix. -func GetAccPubKeyBech32(pubkey string) (pk crypto.PubKey, err error) { - bech32PrefixAccPub := GetConfig().GetBech32AccountPubPrefix() - bz, err := GetFromBech32(pubkey, bech32PrefixAccPub) - if err != nil { - return nil, err - } + case Bech32PubKeyTypeConsPub: + bech32Prefix = GetConfig().GetBech32ConsensusPubPrefix() - pk, err = cryptoAmino.PubKeyFromBytes(bz) - if err != nil { - return nil, err } - return pk, nil + return bech32.ConvertAndEncode(bech32Prefix, pubkey.Bytes()) } -// MustGetAccPubKeyBech32 returns the result of GetAccPubKeyBech32 panicing on -// failure. -func MustGetAccPubKeyBech32(pubkey string) (pk crypto.PubKey) { - pk, err := GetAccPubKeyBech32(pubkey) +// MustBech32ifyPubKey calls Bech32ifyPubKey except it panics on error. +func MustBech32ifyPubKey(pkt Bech32PubKeyType, pubkey *PublicKey) string { + res, err := Bech32ifyPubKey(pkt, pubkey) if err != nil { panic(err) } - return pk + return res } -// GetValPubKeyBech32 creates a PubKey for a validator's operator with a given -// public key string using the Bech32 Bech32PrefixValPub prefix. -func GetValPubKeyBech32(pubkey string) (pk crypto.PubKey, err error) { - bech32PrefixValPub := GetConfig().GetBech32ValidatorPubPrefix() - bz, err := GetFromBech32(pubkey, bech32PrefixValPub) - if err != nil { - return nil, err - } +// GetPubKeyFromBech32 returns a PublicKey from a bech32-encoded PublicKey with +// a given key type. +func GetPubKeyFromBech32(pkt Bech32PubKeyType, pubkeyStr string) (*PublicKey, error) { + var bech32Prefix string - pk, err = cryptoAmino.PubKeyFromBytes(bz) - if err != nil { - return nil, err - } + switch pkt { + case Bech32PubKeyTypeAccPub: + bech32Prefix = GetConfig().GetBech32AccountPubPrefix() - return pk, nil -} + case Bech32PubKeyTypeValPub: + bech32Prefix = GetConfig().GetBech32ValidatorPubPrefix() -// MustGetValPubKeyBech32 returns the result of GetValPubKeyBech32 panicing on -// failure. -func MustGetValPubKeyBech32(pubkey string) (pk crypto.PubKey) { - pk, err := GetValPubKeyBech32(pubkey) - if err != nil { - panic(err) - } + case Bech32PubKeyTypeConsPub: + bech32Prefix = GetConfig().GetBech32ConsensusPubPrefix() - return pk -} + } -// GetConsPubKeyBech32 creates a PubKey for a consensus node with a given public -// key string using the Bech32 Bech32PrefixConsPub prefix. -func GetConsPubKeyBech32(pubkey string) (pk crypto.PubKey, err error) { - bech32PrefixConsPub := GetConfig().GetBech32ConsensusPubPrefix() - bz, err := GetFromBech32(pubkey, bech32PrefixConsPub) + bz, err := GetFromBech32(pubkeyStr, bech32Prefix) if err != nil { return nil, err } - pk, err = cryptoAmino.PubKeyFromBytes(bz) - if err != nil { + pk := new(PublicKey) + if err := pk.Unmarshal(bz); err != nil { return nil, err } return pk, nil } -// MustGetConsPubKeyBech32 returns the result of GetConsPubKeyBech32 panicing on -// failure. -func MustGetConsPubKeyBech32(pubkey string) (pk crypto.PubKey) { - pk, err := GetConsPubKeyBech32(pubkey) +// MustGetPubKeyFromBech32 calls GetPubKeyFromBech32 except it panics on error. +func MustGetPubKeyFromBech32(pkt Bech32PubKeyType, pubkeyStr string) *PublicKey { + res, err := GetPubKeyFromBech32(pkt, pubkeyStr) if err != nil { panic(err) } - return pk + return res } // GetFromBech32 decodes a bytestring from a Bech32 encoded string. diff --git a/types/address_test.go b/types/address_test.go index d79ed5be92c8..6c84503fa886 100644 --- a/types/address_test.go +++ b/types/address_test.go @@ -59,34 +59,35 @@ func TestRandBech32PubkeyConsistency(t *testing.T) { for i := 0; i < 1000; i++ { rand.Read(pub[:]) + pk := &types.PublicKey{&types.PublicKey_Ed25519{pub[:]}} - mustBech32AccPub := types.MustBech32ifyAccPub(pub) - bech32AccPub, err := types.Bech32ifyAccPub(pub) + mustBech32AccPub := types.MustBech32ifyPubKey(types.Bech32PubKeyTypeAccPub, pk) + bech32AccPub, err := types.Bech32ifyPubKey(types.Bech32PubKeyTypeAccPub, pk) require.Nil(t, err) require.Equal(t, bech32AccPub, mustBech32AccPub) - mustBech32ValPub := types.MustBech32ifyValPub(pub) - bech32ValPub, err := types.Bech32ifyValPub(pub) + mustBech32ValPub := types.MustBech32ifyPubKey(types.Bech32PubKeyTypeValPub, pk) + bech32ValPub, err := types.Bech32ifyPubKey(types.Bech32PubKeyTypeValPub, pk) require.Nil(t, err) require.Equal(t, bech32ValPub, mustBech32ValPub) - mustBech32ConsPub := types.MustBech32ifyConsPub(pub) - bech32ConsPub, err := types.Bech32ifyConsPub(pub) + mustBech32ConsPub := types.MustBech32ifyPubKey(types.Bech32PubKeyTypeConsPub, pk) + bech32ConsPub, err := types.Bech32ifyPubKey(types.Bech32PubKeyTypeConsPub, pk) require.Nil(t, err) require.Equal(t, bech32ConsPub, mustBech32ConsPub) - mustAccPub := types.MustGetAccPubKeyBech32(bech32AccPub) - accPub, err := types.GetAccPubKeyBech32(bech32AccPub) + mustAccPub := types.MustGetPubKeyFromBech32(types.Bech32PubKeyTypeAccPub, bech32AccPub) + accPub, err := types.GetPubKeyFromBech32(types.Bech32PubKeyTypeAccPub, bech32AccPub) require.Nil(t, err) require.Equal(t, accPub, mustAccPub) - mustValPub := types.MustGetValPubKeyBech32(bech32ValPub) - valPub, err := types.GetValPubKeyBech32(bech32ValPub) + mustValPub := types.MustGetPubKeyFromBech32(types.Bech32PubKeyTypeValPub, bech32ValPub) + valPub, err := types.GetPubKeyFromBech32(types.Bech32PubKeyTypeValPub, bech32ValPub) require.Nil(t, err) require.Equal(t, valPub, mustValPub) - mustConsPub := types.MustGetConsPubKeyBech32(bech32ConsPub) - consPub, err := types.GetConsPubKeyBech32(bech32ConsPub) + mustConsPub := types.MustGetPubKeyFromBech32(types.Bech32PubKeyTypeConsPub, bech32ConsPub) + consPub, err := types.GetPubKeyFromBech32(types.Bech32PubKeyTypeConsPub, bech32ConsPub) require.Nil(t, err) require.Equal(t, consPub, mustConsPub) @@ -232,6 +233,8 @@ func TestConfiguredPrefix(t *testing.T) { for length := 1; length < 10; length++ { for times := 1; times < 20; times++ { rand.Read(pub[:]) + pk := &types.PublicKey{&types.PublicKey_Ed25519{pub[:]}} + // Test if randomly generated prefix of a given length works prefix := RandString(length) @@ -246,7 +249,7 @@ func TestConfiguredPrefix(t *testing.T) { acc.String(), prefix+types.PrefixAccount), acc.String()) - bech32Pub := types.MustBech32ifyAccPub(pub) + bech32Pub := types.MustBech32ifyPubKey(types.Bech32PubKeyTypeAccPub, pk) require.True(t, strings.HasPrefix( bech32Pub, prefix+types.PrefixPublic)) @@ -260,7 +263,7 @@ func TestConfiguredPrefix(t *testing.T) { val.String(), prefix+types.PrefixValidator+types.PrefixAddress)) - bech32ValPub := types.MustBech32ifyValPub(pub) + bech32ValPub := types.MustBech32ifyPubKey(types.Bech32PubKeyTypeValPub, pk) require.True(t, strings.HasPrefix( bech32ValPub, prefix+types.PrefixValidator+types.PrefixPublic)) @@ -274,7 +277,7 @@ func TestConfiguredPrefix(t *testing.T) { cons.String(), prefix+types.PrefixConsensus+types.PrefixAddress)) - bech32ConsPub := types.MustBech32ifyConsPub(pub) + bech32ConsPub := types.MustBech32ifyPubKey(types.Bech32PubKeyTypeConsPub, pk) require.True(t, strings.HasPrefix( bech32ConsPub, prefix+types.PrefixConsensus+types.PrefixPublic)) diff --git a/types/codec.pb.go b/types/codec.pb.go index 7ff477997983..7974c0b61565 100644 --- a/types/codec.pb.go +++ b/types/codec.pb.go @@ -23,21 +23,179 @@ var _ = math.Inf // proto package needs to be updated. const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package +// PublicKey defines a proto message with a single oneof field that represents a +// public key as a byte slice. +// +// NOTE: Private keys must still be amino-encoded for backwards compatiblity. +type PublicKey struct { + // pub defines an single supported public key type as a byte slice. + // + // Types that are valid to be assigned to Pub: + // *PublicKey_Secp256K1 + // *PublicKey_Ed25519 + // *PublicKey_Multisig + Pub isPublicKey_Pub `protobuf_oneof:"pub"` +} + +func (m *PublicKey) Reset() { *m = PublicKey{} } +func (m *PublicKey) String() string { return proto.CompactTextString(m) } +func (*PublicKey) ProtoMessage() {} +func (*PublicKey) Descriptor() ([]byte, []int) { + return fileDescriptor_97e5f89da6f0daf0, []int{0} +} +func (m *PublicKey) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *PublicKey) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_PublicKey.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalToSizedBuffer(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (m *PublicKey) XXX_Merge(src proto.Message) { + xxx_messageInfo_PublicKey.Merge(m, src) +} +func (m *PublicKey) XXX_Size() int { + return m.Size() +} +func (m *PublicKey) XXX_DiscardUnknown() { + xxx_messageInfo_PublicKey.DiscardUnknown(m) +} + +var xxx_messageInfo_PublicKey proto.InternalMessageInfo + +type isPublicKey_Pub interface { + isPublicKey_Pub() + MarshalTo([]byte) (int, error) + Size() int +} + +type PublicKey_Secp256K1 struct { + Secp256K1 []byte `protobuf:"bytes,1,opt,name=secp256k1,proto3,oneof" json:"secp256k1,omitempty"` +} +type PublicKey_Ed25519 struct { + Ed25519 []byte `protobuf:"bytes,2,opt,name=ed25519,proto3,oneof" json:"ed25519,omitempty"` +} +type PublicKey_Multisig struct { + Multisig *MultiSig `protobuf:"bytes,3,opt,name=multisig,proto3,oneof" json:"multisig,omitempty"` +} + +func (*PublicKey_Secp256K1) isPublicKey_Pub() {} +func (*PublicKey_Ed25519) isPublicKey_Pub() {} +func (*PublicKey_Multisig) isPublicKey_Pub() {} + +func (m *PublicKey) GetPub() isPublicKey_Pub { + if m != nil { + return m.Pub + } + return nil +} + +func (m *PublicKey) GetSecp256K1() []byte { + if x, ok := m.GetPub().(*PublicKey_Secp256K1); ok { + return x.Secp256K1 + } + return nil +} + +func (m *PublicKey) GetEd25519() []byte { + if x, ok := m.GetPub().(*PublicKey_Ed25519); ok { + return x.Ed25519 + } + return nil +} + +func (m *PublicKey) GetMultisig() *MultiSig { + if x, ok := m.GetPub().(*PublicKey_Multisig); ok { + return x.Multisig + } + return nil +} + +// XXX_OneofWrappers is for the internal use of the proto package. +func (*PublicKey) XXX_OneofWrappers() []interface{} { + return []interface{}{ + (*PublicKey_Secp256K1)(nil), + (*PublicKey_Ed25519)(nil), + (*PublicKey_Multisig)(nil), + } +} + +// MultiSig defines a proto message representing a threshold multi-signature +// scheme. +type MultiSig struct { + K uint32 `protobuf:"varint,1,opt,name=k,proto3" json:"k,omitempty"` + Pubkeys []*PublicKey `protobuf:"bytes,2,rep,name=pubkeys,proto3" json:"pubkeys,omitempty"` +} + +func (m *MultiSig) Reset() { *m = MultiSig{} } +func (m *MultiSig) String() string { return proto.CompactTextString(m) } +func (*MultiSig) ProtoMessage() {} +func (*MultiSig) Descriptor() ([]byte, []int) { + return fileDescriptor_97e5f89da6f0daf0, []int{1} +} +func (m *MultiSig) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *MultiSig) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_MultiSig.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalToSizedBuffer(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (m *MultiSig) XXX_Merge(src proto.Message) { + xxx_messageInfo_MultiSig.Merge(m, src) +} +func (m *MultiSig) XXX_Size() int { + return m.Size() +} +func (m *MultiSig) XXX_DiscardUnknown() { + xxx_messageInfo_MultiSig.DiscardUnknown(m) +} + +var xxx_messageInfo_MultiSig proto.InternalMessageInfo + +func (m *MultiSig) GetK() uint32 { + if m != nil { + return m.K + } + return 0 +} + +func (m *MultiSig) GetPubkeys() []*PublicKey { + if m != nil { + return m.Pubkeys + } + return nil +} + // Coin defines a token with a denomination and an amount. // // NOTE: The amount field is an Int which implements the custom method // signatures required by gogoproto. type Coin struct { Denom string `protobuf:"bytes,1,opt,name=denom,proto3" json:"denom,omitempty"` - // TODO: Consider using a string instead of bytes. This would require gogo proto - // to support types other than bytes for the customtype extension. + // TODO: Consider using a string instead of bytes. This would require gogo + // proto to support types other than bytes for the customtype extension. Amount Int `protobuf:"bytes,2,opt,name=amount,proto3,customtype=Int" json:"amount"` } func (m *Coin) Reset() { *m = Coin{} } func (*Coin) ProtoMessage() {} func (*Coin) Descriptor() ([]byte, []int) { - return fileDescriptor_97e5f89da6f0daf0, []int{0} + return fileDescriptor_97e5f89da6f0daf0, []int{2} } func (m *Coin) XXX_Unmarshal(b []byte) error { return m.Unmarshal(b) @@ -79,15 +237,15 @@ func (m *Coin) GetDenom() string { // signatures required by gogoproto. type DecCoin struct { Denom string `protobuf:"bytes,1,opt,name=denom,proto3" json:"denom,omitempty"` - // TODO: Consider using a string instead of bytes. This would require gogo proto - // to support types other than bytes for the customtype extension. + // TODO: Consider using a string instead of bytes. This would require gogo + // proto to support types other than bytes for the customtype extension. Amount Dec `protobuf:"bytes,2,opt,name=amount,proto3,customtype=Dec" json:"amount"` } func (m *DecCoin) Reset() { *m = DecCoin{} } func (*DecCoin) ProtoMessage() {} func (*DecCoin) Descriptor() ([]byte, []int) { - return fileDescriptor_97e5f89da6f0daf0, []int{1} + return fileDescriptor_97e5f89da6f0daf0, []int{3} } func (m *DecCoin) XXX_Unmarshal(b []byte) error { return m.Unmarshal(b) @@ -124,6 +282,8 @@ func (m *DecCoin) GetDenom() string { } func init() { + proto.RegisterType((*PublicKey)(nil), "cosmos_sdk.v1.PublicKey") + proto.RegisterType((*MultiSig)(nil), "cosmos_sdk.v1.MultiSig") proto.RegisterType((*Coin)(nil), "cosmos_sdk.v1.Coin") proto.RegisterType((*DecCoin)(nil), "cosmos_sdk.v1.DecCoin") } @@ -131,22 +291,157 @@ func init() { func init() { proto.RegisterFile("types/codec.proto", fileDescriptor_97e5f89da6f0daf0) } var fileDescriptor_97e5f89da6f0daf0 = []byte{ - // 225 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x12, 0x2c, 0xa9, 0x2c, 0x48, - 0x2d, 0xd6, 0x4f, 0xce, 0x4f, 0x49, 0x4d, 0xd6, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0xe2, 0x4d, - 0xce, 0x2f, 0xce, 0xcd, 0x2f, 0x8e, 0x2f, 0x4e, 0xc9, 0xd6, 0x2b, 0x33, 0x94, 0x32, 0x29, 0x4b, - 0xcd, 0x4b, 0xc9, 0x2f, 0xd2, 0x4f, 0xcf, 0x2c, 0xc9, 0x28, 0x4d, 0xd2, 0x4b, 0xce, 0xcf, 0xd5, - 0x4f, 0xcf, 0x4f, 0xcf, 0xd7, 0x07, 0x2b, 0x4e, 0x2a, 0x4d, 0x03, 0xf3, 0xc0, 0x1c, 0x30, 0x0b, - 0x62, 0x88, 0x92, 0x23, 0x17, 0x8b, 0x73, 0x7e, 0x66, 0x9e, 0x90, 0x08, 0x17, 0x6b, 0x4a, 0x6a, - 0x5e, 0x7e, 0xae, 0x04, 0xa3, 0x02, 0xa3, 0x06, 0x67, 0x10, 0x84, 0x23, 0xa4, 0xcc, 0xc5, 0x96, - 0x98, 0x9b, 0x5f, 0x9a, 0x57, 0x22, 0xc1, 0xa4, 0xc0, 0xa8, 0xc1, 0xe3, 0xc4, 0x7d, 0xe2, 0x9e, - 0x3c, 0xc3, 0xad, 0x7b, 0xf2, 0xcc, 0x9e, 0x79, 0x25, 0x41, 0x50, 0x29, 0x25, 0x17, 0x2e, 0x76, - 0x97, 0xd4, 0x64, 0x72, 0x4c, 0x71, 0x49, 0x4d, 0x86, 0x99, 0xe2, 0xe4, 0x72, 0xe3, 0xa1, 0x1c, - 0x43, 0xc3, 0x23, 0x39, 0x86, 0x13, 0x8f, 0xe4, 0x18, 0x2f, 0x3c, 0x92, 0x63, 0x7c, 0xf0, 0x48, - 0x8e, 0x71, 0xc2, 0x63, 0x39, 0x86, 0x0b, 0x8f, 0xe5, 0x18, 0x6e, 0x3c, 0x96, 0x63, 0x88, 0x52, - 0x42, 0xf2, 0x19, 0xc4, 0xeb, 0x50, 0x4a, 0xb7, 0x38, 0x25, 0x5b, 0x1f, 0x1c, 0x3e, 0x49, 0x6c, - 0x60, 0x5f, 0x19, 0x03, 0x02, 0x00, 0x00, 0xff, 0xff, 0x6c, 0x9b, 0x51, 0x3b, 0x2f, 0x01, 0x00, - 0x00, + // 366 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x9c, 0x92, 0xbd, 0x4e, 0xe3, 0x40, + 0x14, 0x85, 0x67, 0xe2, 0xfc, 0x4e, 0x92, 0x62, 0x47, 0x2b, 0xad, 0x95, 0x62, 0x12, 0x79, 0x9b, + 0x34, 0x6b, 0x2b, 0xde, 0xf5, 0x4a, 0xbb, 0x1d, 0xc1, 0x45, 0x10, 0x42, 0x42, 0xa6, 0xa3, 0x41, + 0x78, 0x3c, 0x18, 0xcb, 0xb1, 0xc7, 0xca, 0xd8, 0x91, 0xd2, 0xd1, 0xd3, 0x50, 0x52, 0xf2, 0x38, + 0x29, 0x53, 0x46, 0x14, 0x11, 0x38, 0x2f, 0x82, 0x18, 0xc7, 0xe1, 0xa7, 0xa4, 0xb2, 0xcf, 0x3d, + 0x47, 0x9f, 0xee, 0xd1, 0x1d, 0xf4, 0x2d, 0x5d, 0x24, 0x4c, 0x18, 0x94, 0x7b, 0x8c, 0xea, 0xc9, + 0x8c, 0xa7, 0x1c, 0x77, 0x29, 0x17, 0x11, 0x17, 0x17, 0xc2, 0x0b, 0xf5, 0xf9, 0xa8, 0xf7, 0x67, + 0xce, 0x62, 0x8f, 0xcf, 0x0c, 0x3f, 0x48, 0xaf, 0x33, 0x57, 0xa7, 0x3c, 0x32, 0x7c, 0xee, 0x73, + 0x43, 0x86, 0xdd, 0xec, 0x4a, 0x2a, 0x29, 0xe4, 0x5f, 0x01, 0xd1, 0x6e, 0x21, 0x6a, 0x9d, 0x66, + 0xee, 0x34, 0xa0, 0xc7, 0x6c, 0x81, 0x09, 0x6a, 0x09, 0x46, 0x13, 0xd3, 0xfa, 0x1b, 0x8e, 0x54, + 0x38, 0x80, 0xc3, 0xce, 0x04, 0x38, 0x6f, 0x23, 0xdc, 0x43, 0x0d, 0xe6, 0x99, 0x96, 0x35, 0xfa, + 0xa7, 0x56, 0x76, 0x6e, 0x39, 0xc0, 0x16, 0x6a, 0x46, 0xd9, 0x34, 0x0d, 0x44, 0xe0, 0xab, 0xca, + 0x00, 0x0e, 0xdb, 0xe6, 0x0f, 0xfd, 0xc3, 0x86, 0xfa, 0xc9, 0xab, 0x7d, 0x16, 0xf8, 0x13, 0xe0, + 0xec, 0xa3, 0xff, 0xab, 0xf7, 0x0f, 0x7d, 0x38, 0xae, 0x21, 0x25, 0xc9, 0x5c, 0xcd, 0x41, 0xcd, + 0x32, 0x84, 0x3b, 0x08, 0x86, 0x72, 0x87, 0xae, 0x03, 0x43, 0x6c, 0xa2, 0x46, 0x92, 0xb9, 0x21, + 0x5b, 0x08, 0xb5, 0x32, 0x50, 0x86, 0x6d, 0x53, 0xfd, 0x04, 0xdf, 0x97, 0x70, 0xca, 0x60, 0x81, + 0xd6, 0x0e, 0x50, 0xf5, 0x90, 0x07, 0x31, 0xfe, 0x8e, 0x6a, 0x1e, 0x8b, 0x79, 0x24, 0x99, 0x2d, + 0xa7, 0x10, 0xf8, 0x27, 0xaa, 0x5f, 0x46, 0x3c, 0x8b, 0xd3, 0xa2, 0xd0, 0xb8, 0xbd, 0xdc, 0xf4, + 0xc1, 0xe3, 0xa6, 0xaf, 0x1c, 0xc5, 0xa9, 0xb3, 0xb3, 0x34, 0x1b, 0x35, 0x6c, 0x46, 0xbf, 0x42, + 0xb1, 0x19, 0x2d, 0x29, 0x63, 0x7b, 0xfd, 0x4c, 0xc0, 0x4d, 0x4e, 0xc0, 0x32, 0x27, 0x70, 0x95, + 0x13, 0xf8, 0x94, 0x13, 0x78, 0xb7, 0x25, 0x60, 0xb5, 0x25, 0x60, 0xbd, 0x25, 0xe0, 0x5c, 0x7b, + 0x77, 0xbb, 0xa2, 0xdd, 0xee, 0xf3, 0x4b, 0x78, 0xa1, 0x21, 0x5f, 0x80, 0x5b, 0x97, 0x77, 0xfb, + 0xfd, 0x12, 0x00, 0x00, 0xff, 0xff, 0x2a, 0x54, 0x75, 0x95, 0x11, 0x02, 0x00, 0x00, +} + +func (m *PublicKey) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalToSizedBuffer(dAtA[:size]) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *PublicKey) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *PublicKey) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + if m.Pub != nil { + { + size := m.Pub.Size() + i -= size + if _, err := m.Pub.MarshalTo(dAtA[i:]); err != nil { + return 0, err + } + } + } + return len(dAtA) - i, nil +} + +func (m *PublicKey_Secp256K1) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *PublicKey_Secp256K1) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + if m.Secp256K1 != nil { + i -= len(m.Secp256K1) + copy(dAtA[i:], m.Secp256K1) + i = encodeVarintCodec(dAtA, i, uint64(len(m.Secp256K1))) + i-- + dAtA[i] = 0xa + } + return len(dAtA) - i, nil +} +func (m *PublicKey_Ed25519) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *PublicKey_Ed25519) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + if m.Ed25519 != nil { + i -= len(m.Ed25519) + copy(dAtA[i:], m.Ed25519) + i = encodeVarintCodec(dAtA, i, uint64(len(m.Ed25519))) + i-- + dAtA[i] = 0x12 + } + return len(dAtA) - i, nil +} +func (m *PublicKey_Multisig) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *PublicKey_Multisig) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + if m.Multisig != nil { + { + size, err := m.Multisig.MarshalToSizedBuffer(dAtA[:i]) + if err != nil { + return 0, err + } + i -= size + i = encodeVarintCodec(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0x1a + } + return len(dAtA) - i, nil +} +func (m *MultiSig) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalToSizedBuffer(dAtA[:size]) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *MultiSig) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *MultiSig) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + if len(m.Pubkeys) > 0 { + for iNdEx := len(m.Pubkeys) - 1; iNdEx >= 0; iNdEx-- { + { + size, err := m.Pubkeys[iNdEx].MarshalToSizedBuffer(dAtA[:i]) + if err != nil { + return 0, err + } + i -= size + i = encodeVarintCodec(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0x12 + } + } + if m.K != 0 { + i = encodeVarintCodec(dAtA, i, uint64(m.K)) + i-- + dAtA[i] = 0x8 + } + return len(dAtA) - i, nil } func (m *Coin) Marshal() (dAtA []byte, err error) { @@ -240,6 +535,72 @@ func encodeVarintCodec(dAtA []byte, offset int, v uint64) int { dAtA[offset] = uint8(v) return base } +func (m *PublicKey) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + if m.Pub != nil { + n += m.Pub.Size() + } + return n +} + +func (m *PublicKey_Secp256K1) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + if m.Secp256K1 != nil { + l = len(m.Secp256K1) + n += 1 + l + sovCodec(uint64(l)) + } + return n +} +func (m *PublicKey_Ed25519) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + if m.Ed25519 != nil { + l = len(m.Ed25519) + n += 1 + l + sovCodec(uint64(l)) + } + return n +} +func (m *PublicKey_Multisig) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + if m.Multisig != nil { + l = m.Multisig.Size() + n += 1 + l + sovCodec(uint64(l)) + } + return n +} +func (m *MultiSig) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + if m.K != 0 { + n += 1 + sovCodec(uint64(m.K)) + } + if len(m.Pubkeys) > 0 { + for _, e := range m.Pubkeys { + l = e.Size() + n += 1 + l + sovCodec(uint64(l)) + } + } + return n +} + func (m *Coin) Size() (n int) { if m == nil { return 0 @@ -276,6 +637,266 @@ func sovCodec(x uint64) (n int) { func sozCodec(x uint64) (n int) { return sovCodec(uint64((x << 1) ^ uint64((int64(x) >> 63)))) } +func (m *PublicKey) Unmarshal(dAtA []byte) error { + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowCodec + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: PublicKey: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: PublicKey: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Secp256K1", wireType) + } + var byteLen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowCodec + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + byteLen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if byteLen < 0 { + return ErrInvalidLengthCodec + } + postIndex := iNdEx + byteLen + if postIndex < 0 { + return ErrInvalidLengthCodec + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + v := make([]byte, postIndex-iNdEx) + copy(v, dAtA[iNdEx:postIndex]) + m.Pub = &PublicKey_Secp256K1{v} + iNdEx = postIndex + case 2: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Ed25519", wireType) + } + var byteLen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowCodec + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + byteLen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if byteLen < 0 { + return ErrInvalidLengthCodec + } + postIndex := iNdEx + byteLen + if postIndex < 0 { + return ErrInvalidLengthCodec + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + v := make([]byte, postIndex-iNdEx) + copy(v, dAtA[iNdEx:postIndex]) + m.Pub = &PublicKey_Ed25519{v} + iNdEx = postIndex + case 3: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Multisig", wireType) + } + var msglen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowCodec + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + msglen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if msglen < 0 { + return ErrInvalidLengthCodec + } + postIndex := iNdEx + msglen + if postIndex < 0 { + return ErrInvalidLengthCodec + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + v := &MultiSig{} + if err := v.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + m.Pub = &PublicKey_Multisig{v} + iNdEx = postIndex + default: + iNdEx = preIndex + skippy, err := skipCodec(dAtA[iNdEx:]) + if err != nil { + return err + } + if skippy < 0 { + return ErrInvalidLengthCodec + } + if (iNdEx + skippy) < 0 { + return ErrInvalidLengthCodec + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} +func (m *MultiSig) Unmarshal(dAtA []byte) error { + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowCodec + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: MultiSig: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: MultiSig: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field K", wireType) + } + m.K = 0 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowCodec + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + m.K |= uint32(b&0x7F) << shift + if b < 0x80 { + break + } + } + case 2: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Pubkeys", wireType) + } + var msglen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowCodec + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + msglen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if msglen < 0 { + return ErrInvalidLengthCodec + } + postIndex := iNdEx + msglen + if postIndex < 0 { + return ErrInvalidLengthCodec + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.Pubkeys = append(m.Pubkeys, &PublicKey{}) + if err := m.Pubkeys[len(m.Pubkeys)-1].Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + default: + iNdEx = preIndex + skippy, err := skipCodec(dAtA[iNdEx:]) + if err != nil { + return err + } + if skippy < 0 { + return ErrInvalidLengthCodec + } + if (iNdEx + skippy) < 0 { + return ErrInvalidLengthCodec + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} func (m *Coin) Unmarshal(dAtA []byte) error { l := len(dAtA) iNdEx := 0 diff --git a/types/codec.proto b/types/codec.proto index 8732c9dc1cf7..101b1a8b84a2 100644 --- a/types/codec.proto +++ b/types/codec.proto @@ -7,15 +7,45 @@ option (gogoproto.stringer_all) = false; import "vendor/github.com/gogo/protobuf/gogoproto/gogo.proto"; +// PublicKey defines a proto message with a single oneof field that represents a +// public key as a byte slice. +// +// NOTE: Private keys must still be amino-encoded for backwards compatiblity. +message PublicKey { + option (gogoproto.goproto_stringer) = true; + + // TODO: Make tendermint keys use []byte instead of [32]byte so we can use the + // casttype extension. + // option (cosmos_proto.interface_type) = + // "github.com/tendermint/tendermint/crypto.PubKey"; + + // pub defines an single supported public key type as a byte slice. + oneof pub { + bytes secp256k1 = 1; + bytes ed25519 = 2; + MultiSig multisig = 3; + } +} + +// MultiSig defines a proto message representing a threshold multi-signature +// scheme. +message MultiSig { + option (gogoproto.goproto_stringer) = true; + + uint32 k = 1; + repeated PublicKey pubkeys = 2; +} + // Coin defines a token with a denomination and an amount. // // NOTE: The amount field is an Int which implements the custom method // signatures required by gogoproto. message Coin { string denom = 1; - // TODO: Consider using a string instead of bytes. This would require gogo proto - // to support types other than bytes for the customtype extension. - bytes amount = 2 [ (gogoproto.customtype) = "Int", (gogoproto.nullable) = false ]; + // TODO: Consider using a string instead of bytes. This would require gogo + // proto to support types other than bytes for the customtype extension. + bytes amount = 2 + [(gogoproto.customtype) = "Int", (gogoproto.nullable) = false]; } // DecCoin defines a token with a denomination and a decimal amount. @@ -24,7 +54,8 @@ message Coin { // signatures required by gogoproto. message DecCoin { string denom = 1; - // TODO: Consider using a string instead of bytes. This would require gogo proto - // to support types other than bytes for the customtype extension. - bytes amount = 2 [ (gogoproto.customtype) = "Dec", (gogoproto.nullable) = false ]; + // TODO: Consider using a string instead of bytes. This would require gogo + // proto to support types other than bytes for the customtype extension. + bytes amount = 2 + [(gogoproto.customtype) = "Dec", (gogoproto.nullable) = false]; } diff --git a/crypto/pubkey.go b/types/pubkey.go similarity index 99% rename from crypto/pubkey.go rename to types/pubkey.go index 1df6b607665a..5c3846bb8633 100644 --- a/crypto/pubkey.go +++ b/types/pubkey.go @@ -1,4 +1,4 @@ -package crypto +package types import ( "github.com/pkg/errors" diff --git a/crypto/pubkey_test.go b/types/pubkey_test.go similarity index 63% rename from crypto/pubkey_test.go rename to types/pubkey_test.go index 521a08328fc7..952f2d26e2ba 100644 --- a/crypto/pubkey_test.go +++ b/types/pubkey_test.go @@ -1,9 +1,9 @@ -package crypto_test +package types_test import ( "testing" - "github.com/cosmos/cosmos-sdk/crypto" + "github.com/cosmos/cosmos-sdk/types" "github.com/stretchr/testify/require" tmcrypto "github.com/tendermint/tendermint/crypto" @@ -15,24 +15,24 @@ import ( func TestGetPubKeySecp256k1(t *testing.T) { tmpk := (secp256k1.GenPrivKey().PubKey()).(secp256k1.PubKeySecp256k1) testCases := []struct { - input *crypto.PublicKey + input *types.PublicKey expected secp256k1.PubKeySecp256k1 err error }{ { - &crypto.PublicKey{&crypto.PublicKey_Secp256K1{tmpk[:]}}, + &types.PublicKey{&types.PublicKey_Secp256K1{tmpk[:]}}, tmpk, nil, }, { - &crypto.PublicKey{&crypto.PublicKey_Secp256K1{append(tmpk[:], tmpk[:]...)}}, + &types.PublicKey{&types.PublicKey_Secp256K1{append(tmpk[:], tmpk[:]...)}}, secp256k1.PubKeySecp256k1{}, - crypto.ErrInvalidPubKeySecp256k1Length, + types.ErrInvalidPubKeySecp256k1Length, }, { - &crypto.PublicKey{&crypto.PublicKey_Ed25519{}}, + &types.PublicKey{&types.PublicKey_Ed25519{}}, secp256k1.PubKeySecp256k1{}, - crypto.ErrInvalidPubKeySecp256k1, + types.ErrInvalidPubKeySecp256k1, }, } @@ -46,24 +46,24 @@ func TestGetPubKeySecp256k1(t *testing.T) { func TestGetPubKeyEd25519(t *testing.T) { tmpk := (ed25519.GenPrivKey().PubKey()).(ed25519.PubKeyEd25519) testCases := []struct { - input *crypto.PublicKey + input *types.PublicKey expected ed25519.PubKeyEd25519 err error }{ { - &crypto.PublicKey{&crypto.PublicKey_Ed25519{tmpk[:]}}, + &types.PublicKey{&types.PublicKey_Ed25519{tmpk[:]}}, tmpk, nil, }, { - &crypto.PublicKey{&crypto.PublicKey_Ed25519{append(tmpk[:], tmpk[:]...)}}, + &types.PublicKey{&types.PublicKey_Ed25519{append(tmpk[:], tmpk[:]...)}}, ed25519.PubKeyEd25519{}, - crypto.ErrInvalidPubKeyEd25519Length, + types.ErrInvalidPubKeyEd25519Length, }, { - &crypto.PublicKey{&crypto.PublicKey_Secp256K1{}}, + &types.PublicKey{&types.PublicKey_Secp256K1{}}, ed25519.PubKeyEd25519{}, - crypto.ErrInvalidPubKeyEd25519, + types.ErrInvalidPubKeyEd25519, }, } @@ -82,23 +82,23 @@ func TestAddress(t *testing.T) { testCases := []struct { pubkey tmcrypto.PubKey - input *crypto.PublicKey + input *types.PublicKey }{ { - ed25519pk, &crypto.PublicKey{&crypto.PublicKey_Ed25519{ed25519pk[:]}}, + ed25519pk, &types.PublicKey{&types.PublicKey_Ed25519{ed25519pk[:]}}, }, { - secp256k1pk, &crypto.PublicKey{&crypto.PublicKey_Secp256K1{secp256k1pk[:]}}, + secp256k1pk, &types.PublicKey{&types.PublicKey_Secp256K1{secp256k1pk[:]}}, }, { multisigpk, - &crypto.PublicKey{ - &crypto.PublicKey_Multisig{ - Multisig: &crypto.MultiSig{ + &types.PublicKey{ + &types.PublicKey_Multisig{ + Multisig: &types.MultiSig{ K: 2, - Pubkeys: []*crypto.PublicKey{ - {&crypto.PublicKey_Secp256K1{secp256k1pk[:]}}, - {&crypto.PublicKey_Secp256K1{secp256k1pk2[:]}}, + Pubkeys: []*types.PublicKey{ + {&types.PublicKey_Secp256K1{secp256k1pk[:]}}, + {&types.PublicKey_Secp256K1{secp256k1pk2[:]}}, }, }, }, From 17b6ac4df87e7c3a15ba92e08e1c5123c53c55f3 Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Fri, 3 Jan 2020 17:01:32 -0500 Subject: [PATCH 37/40] Move crypto proto to types --- crypto/ledger_test.go | 18 +- types/address.go | 149 +++------- types/address_test.go | 33 ++- types/codec.pb.go | 665 ++++++++++++++++++++++++++++++++++++++++-- types/codec.proto | 43 ++- types/pubkey.go | 158 ++++++++++ types/pubkey_test.go | 112 +++++++ 7 files changed, 1027 insertions(+), 151 deletions(-) create mode 100644 types/pubkey.go create mode 100644 types/pubkey_test.go diff --git a/crypto/ledger_test.go b/crypto/ledger_test.go index 0f726b410d55..a2876ffe75df 100644 --- a/crypto/ledger_test.go +++ b/crypto/ledger_test.go @@ -8,9 +8,11 @@ import ( tmcrypto "github.com/tendermint/tendermint/crypto" cryptoAmino "github.com/tendermint/tendermint/crypto/encoding/amino" + "github.com/tendermint/tendermint/crypto/secp256k1" "github.com/cosmos/cosmos-sdk/crypto/keys/hd" "github.com/cosmos/cosmos-sdk/tests" + "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types" ) @@ -32,7 +34,9 @@ func TestPublicKeyUnsafe(t *testing.T) { fmt.Sprintf("%x", priv.PubKey().Bytes()), "Is your device using test mnemonic: %s ?", tests.TestMnemonic) - pubKeyAddr, err := sdk.Bech32ifyAccPub(priv.PubKey()) + pubRaw := priv.PubKey().(secp256k1.PubKeySecp256k1) + pk := &sdk.PublicKey{&types.PublicKey_Ed25519{pubRaw[:]}} + pubKeyAddr, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, pk) require.NoError(t, err) require.Equal(t, "cosmospub1addwnpepqd87l8xhcnrrtzxnkql7k55ph8fr9jarf4hn6udwukfprlalu8lgw0urza0", pubKeyAddr, "Is your device using test mnemonic: %s ?", tests.TestMnemonic) @@ -74,7 +78,9 @@ func TestPublicKeyUnsafeHDPath(t *testing.T) { tmp := priv.(PrivKeyLedgerSecp256k1) (&tmp).AssertIsPrivKeyInner() - pubKeyAddr, err := sdk.Bech32ifyAccPub(priv.PubKey()) + pubRaw := priv.PubKey().(secp256k1.PubKeySecp256k1) + pk := &sdk.PublicKey{&types.PublicKey_Ed25519{pubRaw[:]}} + pubKeyAddr, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, pk) require.NoError(t, err) require.Equal(t, expectedAnswers[i], pubKeyAddr, @@ -108,7 +114,9 @@ func TestPublicKeySafe(t *testing.T) { fmt.Sprintf("%x", priv.PubKey().Bytes()), "Is your device using test mnemonic: %s ?", tests.TestMnemonic) - pubKeyAddr, err := sdk.Bech32ifyAccPub(priv.PubKey()) + pubRaw := priv.PubKey().(secp256k1.PubKeySecp256k1) + pk := &sdk.PublicKey{&types.PublicKey_Ed25519{pubRaw[:]}} + pubKeyAddr, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, pk) require.NoError(t, err) require.Equal(t, "cosmospub1addwnpepqd87l8xhcnrrtzxnkql7k55ph8fr9jarf4hn6udwukfprlalu8lgw0urza0", pubKeyAddr, "Is your device using test mnemonic: %s ?", tests.TestMnemonic) @@ -172,7 +180,9 @@ func TestPublicKeyHDPath(t *testing.T) { tmp := priv.(PrivKeyLedgerSecp256k1) (&tmp).AssertIsPrivKeyInner() - pubKeyAddr, err := sdk.Bech32ifyAccPub(priv.PubKey()) + pubRaw := priv.PubKey().(secp256k1.PubKeySecp256k1) + pk := &sdk.PublicKey{&types.PublicKey_Ed25519{pubRaw[:]}} + pubKeyAddr, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, pk) require.NoError(t, err) require.Equal(t, expectedPubKeys[i], pubKeyAddr, diff --git a/types/address.go b/types/address.go index 19e81a1a7712..1e2f1b35d556 100644 --- a/types/address.go +++ b/types/address.go @@ -8,11 +8,9 @@ import ( "fmt" "strings" - "github.com/tendermint/tendermint/crypto" - cryptoAmino "github.com/tendermint/tendermint/crypto/encoding/amino" - yaml "gopkg.in/yaml.v2" - + tmcrypto "github.com/tendermint/tendermint/crypto" "github.com/tendermint/tendermint/libs/bech32" + yaml "gopkg.in/yaml.v2" ) const ( @@ -454,7 +452,7 @@ func ConsAddressFromBech32(address string) (addr ConsAddress, err error) { } // get ConsAddress from pubkey -func GetConsAddress(pubkey crypto.PubKey) ConsAddress { +func GetConsAddress(pubkey tmcrypto.PubKey) ConsAddress { return ConsAddress(pubkey.Address()) } @@ -574,140 +572,83 @@ func (ca ConsAddress) Format(s fmt.State, verb rune) { // auxiliary // ---------------------------------------------------------------------------- -// Bech32ifyAccPub returns a Bech32 encoded string containing the -// Bech32PrefixAccPub prefix for a given account PubKey. -func Bech32ifyAccPub(pub crypto.PubKey) (string, error) { - bech32PrefixAccPub := GetConfig().GetBech32AccountPubPrefix() - return bech32.ConvertAndEncode(bech32PrefixAccPub, pub.Bytes()) -} - -// MustBech32ifyAccPub returns the result of Bech32ifyAccPub panicing on failure. -func MustBech32ifyAccPub(pub crypto.PubKey) string { - enc, err := Bech32ifyAccPub(pub) - if err != nil { - panic(err) - } - - return enc -} - -// Bech32ifyValPub returns a Bech32 encoded string containing the -// Bech32PrefixValPub prefix for a given validator operator's PubKey. -func Bech32ifyValPub(pub crypto.PubKey) (string, error) { - bech32PrefixValPub := GetConfig().GetBech32ValidatorPubPrefix() - return bech32.ConvertAndEncode(bech32PrefixValPub, pub.Bytes()) -} - -// MustBech32ifyValPub returns the result of Bech32ifyValPub panicing on failure. -func MustBech32ifyValPub(pub crypto.PubKey) string { - enc, err := Bech32ifyValPub(pub) - if err != nil { - panic(err) - } +type Bech32PubKeyType string - return enc -} +// Bech32 conversion constants +const ( + Bech32PubKeyTypeAccPub Bech32PubKeyType = "accpub" + Bech32PubKeyTypeValPub Bech32PubKeyType = "valpub" + Bech32PubKeyTypeConsPub Bech32PubKeyType = "conspub" +) -// Bech32ifyConsPub returns a Bech32 encoded string containing the -// Bech32PrefixConsPub prefixfor a given consensus node's PubKey. -func Bech32ifyConsPub(pub crypto.PubKey) (string, error) { - bech32PrefixConsPub := GetConfig().GetBech32ConsensusPubPrefix() - return bech32.ConvertAndEncode(bech32PrefixConsPub, pub.Bytes()) -} +// Bech32ifyPubKey returns a Bech32 encoded string containing the appropriate +// prefix based on the key type provided for a given PublicKey. +func Bech32ifyPubKey(pkt Bech32PubKeyType, pubkey *PublicKey) (string, error) { + var bech32Prefix string -// MustBech32ifyConsPub returns the result of Bech32ifyConsPub panicing on -// failure. -func MustBech32ifyConsPub(pub crypto.PubKey) string { - enc, err := Bech32ifyConsPub(pub) - if err != nil { - panic(err) - } + switch pkt { + case Bech32PubKeyTypeAccPub: + bech32Prefix = GetConfig().GetBech32AccountPubPrefix() - return enc -} + case Bech32PubKeyTypeValPub: + bech32Prefix = GetConfig().GetBech32ValidatorPubPrefix() -// GetAccPubKeyBech32 creates a PubKey for an account with a given public key -// string using the Bech32 Bech32PrefixAccPub prefix. -func GetAccPubKeyBech32(pubkey string) (pk crypto.PubKey, err error) { - bech32PrefixAccPub := GetConfig().GetBech32AccountPubPrefix() - bz, err := GetFromBech32(pubkey, bech32PrefixAccPub) - if err != nil { - return nil, err - } + case Bech32PubKeyTypeConsPub: + bech32Prefix = GetConfig().GetBech32ConsensusPubPrefix() - pk, err = cryptoAmino.PubKeyFromBytes(bz) - if err != nil { - return nil, err } - return pk, nil + return bech32.ConvertAndEncode(bech32Prefix, pubkey.Bytes()) } -// MustGetAccPubKeyBech32 returns the result of GetAccPubKeyBech32 panicing on -// failure. -func MustGetAccPubKeyBech32(pubkey string) (pk crypto.PubKey) { - pk, err := GetAccPubKeyBech32(pubkey) +// MustBech32ifyPubKey calls Bech32ifyPubKey except it panics on error. +func MustBech32ifyPubKey(pkt Bech32PubKeyType, pubkey *PublicKey) string { + res, err := Bech32ifyPubKey(pkt, pubkey) if err != nil { panic(err) } - return pk + return res } -// GetValPubKeyBech32 creates a PubKey for a validator's operator with a given -// public key string using the Bech32 Bech32PrefixValPub prefix. -func GetValPubKeyBech32(pubkey string) (pk crypto.PubKey, err error) { - bech32PrefixValPub := GetConfig().GetBech32ValidatorPubPrefix() - bz, err := GetFromBech32(pubkey, bech32PrefixValPub) - if err != nil { - return nil, err - } +// GetPubKeyFromBech32 returns a PublicKey from a bech32-encoded PublicKey with +// a given key type. +func GetPubKeyFromBech32(pkt Bech32PubKeyType, pubkeyStr string) (*PublicKey, error) { + var bech32Prefix string - pk, err = cryptoAmino.PubKeyFromBytes(bz) - if err != nil { - return nil, err - } + switch pkt { + case Bech32PubKeyTypeAccPub: + bech32Prefix = GetConfig().GetBech32AccountPubPrefix() - return pk, nil -} + case Bech32PubKeyTypeValPub: + bech32Prefix = GetConfig().GetBech32ValidatorPubPrefix() -// MustGetValPubKeyBech32 returns the result of GetValPubKeyBech32 panicing on -// failure. -func MustGetValPubKeyBech32(pubkey string) (pk crypto.PubKey) { - pk, err := GetValPubKeyBech32(pubkey) - if err != nil { - panic(err) - } + case Bech32PubKeyTypeConsPub: + bech32Prefix = GetConfig().GetBech32ConsensusPubPrefix() - return pk -} + } -// GetConsPubKeyBech32 creates a PubKey for a consensus node with a given public -// key string using the Bech32 Bech32PrefixConsPub prefix. -func GetConsPubKeyBech32(pubkey string) (pk crypto.PubKey, err error) { - bech32PrefixConsPub := GetConfig().GetBech32ConsensusPubPrefix() - bz, err := GetFromBech32(pubkey, bech32PrefixConsPub) + bz, err := GetFromBech32(pubkeyStr, bech32Prefix) if err != nil { return nil, err } - pk, err = cryptoAmino.PubKeyFromBytes(bz) - if err != nil { + pk := new(PublicKey) + if err := pk.Unmarshal(bz); err != nil { return nil, err } return pk, nil } -// MustGetConsPubKeyBech32 returns the result of GetConsPubKeyBech32 panicing on -// failure. -func MustGetConsPubKeyBech32(pubkey string) (pk crypto.PubKey) { - pk, err := GetConsPubKeyBech32(pubkey) +// MustGetPubKeyFromBech32 calls GetPubKeyFromBech32 except it panics on error. +func MustGetPubKeyFromBech32(pkt Bech32PubKeyType, pubkeyStr string) *PublicKey { + res, err := GetPubKeyFromBech32(pkt, pubkeyStr) if err != nil { panic(err) } - return pk + return res } // GetFromBech32 decodes a bytestring from a Bech32 encoded string. diff --git a/types/address_test.go b/types/address_test.go index d79ed5be92c8..6c84503fa886 100644 --- a/types/address_test.go +++ b/types/address_test.go @@ -59,34 +59,35 @@ func TestRandBech32PubkeyConsistency(t *testing.T) { for i := 0; i < 1000; i++ { rand.Read(pub[:]) + pk := &types.PublicKey{&types.PublicKey_Ed25519{pub[:]}} - mustBech32AccPub := types.MustBech32ifyAccPub(pub) - bech32AccPub, err := types.Bech32ifyAccPub(pub) + mustBech32AccPub := types.MustBech32ifyPubKey(types.Bech32PubKeyTypeAccPub, pk) + bech32AccPub, err := types.Bech32ifyPubKey(types.Bech32PubKeyTypeAccPub, pk) require.Nil(t, err) require.Equal(t, bech32AccPub, mustBech32AccPub) - mustBech32ValPub := types.MustBech32ifyValPub(pub) - bech32ValPub, err := types.Bech32ifyValPub(pub) + mustBech32ValPub := types.MustBech32ifyPubKey(types.Bech32PubKeyTypeValPub, pk) + bech32ValPub, err := types.Bech32ifyPubKey(types.Bech32PubKeyTypeValPub, pk) require.Nil(t, err) require.Equal(t, bech32ValPub, mustBech32ValPub) - mustBech32ConsPub := types.MustBech32ifyConsPub(pub) - bech32ConsPub, err := types.Bech32ifyConsPub(pub) + mustBech32ConsPub := types.MustBech32ifyPubKey(types.Bech32PubKeyTypeConsPub, pk) + bech32ConsPub, err := types.Bech32ifyPubKey(types.Bech32PubKeyTypeConsPub, pk) require.Nil(t, err) require.Equal(t, bech32ConsPub, mustBech32ConsPub) - mustAccPub := types.MustGetAccPubKeyBech32(bech32AccPub) - accPub, err := types.GetAccPubKeyBech32(bech32AccPub) + mustAccPub := types.MustGetPubKeyFromBech32(types.Bech32PubKeyTypeAccPub, bech32AccPub) + accPub, err := types.GetPubKeyFromBech32(types.Bech32PubKeyTypeAccPub, bech32AccPub) require.Nil(t, err) require.Equal(t, accPub, mustAccPub) - mustValPub := types.MustGetValPubKeyBech32(bech32ValPub) - valPub, err := types.GetValPubKeyBech32(bech32ValPub) + mustValPub := types.MustGetPubKeyFromBech32(types.Bech32PubKeyTypeValPub, bech32ValPub) + valPub, err := types.GetPubKeyFromBech32(types.Bech32PubKeyTypeValPub, bech32ValPub) require.Nil(t, err) require.Equal(t, valPub, mustValPub) - mustConsPub := types.MustGetConsPubKeyBech32(bech32ConsPub) - consPub, err := types.GetConsPubKeyBech32(bech32ConsPub) + mustConsPub := types.MustGetPubKeyFromBech32(types.Bech32PubKeyTypeConsPub, bech32ConsPub) + consPub, err := types.GetPubKeyFromBech32(types.Bech32PubKeyTypeConsPub, bech32ConsPub) require.Nil(t, err) require.Equal(t, consPub, mustConsPub) @@ -232,6 +233,8 @@ func TestConfiguredPrefix(t *testing.T) { for length := 1; length < 10; length++ { for times := 1; times < 20; times++ { rand.Read(pub[:]) + pk := &types.PublicKey{&types.PublicKey_Ed25519{pub[:]}} + // Test if randomly generated prefix of a given length works prefix := RandString(length) @@ -246,7 +249,7 @@ func TestConfiguredPrefix(t *testing.T) { acc.String(), prefix+types.PrefixAccount), acc.String()) - bech32Pub := types.MustBech32ifyAccPub(pub) + bech32Pub := types.MustBech32ifyPubKey(types.Bech32PubKeyTypeAccPub, pk) require.True(t, strings.HasPrefix( bech32Pub, prefix+types.PrefixPublic)) @@ -260,7 +263,7 @@ func TestConfiguredPrefix(t *testing.T) { val.String(), prefix+types.PrefixValidator+types.PrefixAddress)) - bech32ValPub := types.MustBech32ifyValPub(pub) + bech32ValPub := types.MustBech32ifyPubKey(types.Bech32PubKeyTypeValPub, pk) require.True(t, strings.HasPrefix( bech32ValPub, prefix+types.PrefixValidator+types.PrefixPublic)) @@ -274,7 +277,7 @@ func TestConfiguredPrefix(t *testing.T) { cons.String(), prefix+types.PrefixConsensus+types.PrefixAddress)) - bech32ConsPub := types.MustBech32ifyConsPub(pub) + bech32ConsPub := types.MustBech32ifyPubKey(types.Bech32PubKeyTypeConsPub, pk) require.True(t, strings.HasPrefix( bech32ConsPub, prefix+types.PrefixConsensus+types.PrefixPublic)) diff --git a/types/codec.pb.go b/types/codec.pb.go index 7ff477997983..7974c0b61565 100644 --- a/types/codec.pb.go +++ b/types/codec.pb.go @@ -23,21 +23,179 @@ var _ = math.Inf // proto package needs to be updated. const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package +// PublicKey defines a proto message with a single oneof field that represents a +// public key as a byte slice. +// +// NOTE: Private keys must still be amino-encoded for backwards compatiblity. +type PublicKey struct { + // pub defines an single supported public key type as a byte slice. + // + // Types that are valid to be assigned to Pub: + // *PublicKey_Secp256K1 + // *PublicKey_Ed25519 + // *PublicKey_Multisig + Pub isPublicKey_Pub `protobuf_oneof:"pub"` +} + +func (m *PublicKey) Reset() { *m = PublicKey{} } +func (m *PublicKey) String() string { return proto.CompactTextString(m) } +func (*PublicKey) ProtoMessage() {} +func (*PublicKey) Descriptor() ([]byte, []int) { + return fileDescriptor_97e5f89da6f0daf0, []int{0} +} +func (m *PublicKey) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *PublicKey) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_PublicKey.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalToSizedBuffer(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (m *PublicKey) XXX_Merge(src proto.Message) { + xxx_messageInfo_PublicKey.Merge(m, src) +} +func (m *PublicKey) XXX_Size() int { + return m.Size() +} +func (m *PublicKey) XXX_DiscardUnknown() { + xxx_messageInfo_PublicKey.DiscardUnknown(m) +} + +var xxx_messageInfo_PublicKey proto.InternalMessageInfo + +type isPublicKey_Pub interface { + isPublicKey_Pub() + MarshalTo([]byte) (int, error) + Size() int +} + +type PublicKey_Secp256K1 struct { + Secp256K1 []byte `protobuf:"bytes,1,opt,name=secp256k1,proto3,oneof" json:"secp256k1,omitempty"` +} +type PublicKey_Ed25519 struct { + Ed25519 []byte `protobuf:"bytes,2,opt,name=ed25519,proto3,oneof" json:"ed25519,omitempty"` +} +type PublicKey_Multisig struct { + Multisig *MultiSig `protobuf:"bytes,3,opt,name=multisig,proto3,oneof" json:"multisig,omitempty"` +} + +func (*PublicKey_Secp256K1) isPublicKey_Pub() {} +func (*PublicKey_Ed25519) isPublicKey_Pub() {} +func (*PublicKey_Multisig) isPublicKey_Pub() {} + +func (m *PublicKey) GetPub() isPublicKey_Pub { + if m != nil { + return m.Pub + } + return nil +} + +func (m *PublicKey) GetSecp256K1() []byte { + if x, ok := m.GetPub().(*PublicKey_Secp256K1); ok { + return x.Secp256K1 + } + return nil +} + +func (m *PublicKey) GetEd25519() []byte { + if x, ok := m.GetPub().(*PublicKey_Ed25519); ok { + return x.Ed25519 + } + return nil +} + +func (m *PublicKey) GetMultisig() *MultiSig { + if x, ok := m.GetPub().(*PublicKey_Multisig); ok { + return x.Multisig + } + return nil +} + +// XXX_OneofWrappers is for the internal use of the proto package. +func (*PublicKey) XXX_OneofWrappers() []interface{} { + return []interface{}{ + (*PublicKey_Secp256K1)(nil), + (*PublicKey_Ed25519)(nil), + (*PublicKey_Multisig)(nil), + } +} + +// MultiSig defines a proto message representing a threshold multi-signature +// scheme. +type MultiSig struct { + K uint32 `protobuf:"varint,1,opt,name=k,proto3" json:"k,omitempty"` + Pubkeys []*PublicKey `protobuf:"bytes,2,rep,name=pubkeys,proto3" json:"pubkeys,omitempty"` +} + +func (m *MultiSig) Reset() { *m = MultiSig{} } +func (m *MultiSig) String() string { return proto.CompactTextString(m) } +func (*MultiSig) ProtoMessage() {} +func (*MultiSig) Descriptor() ([]byte, []int) { + return fileDescriptor_97e5f89da6f0daf0, []int{1} +} +func (m *MultiSig) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *MultiSig) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_MultiSig.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalToSizedBuffer(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (m *MultiSig) XXX_Merge(src proto.Message) { + xxx_messageInfo_MultiSig.Merge(m, src) +} +func (m *MultiSig) XXX_Size() int { + return m.Size() +} +func (m *MultiSig) XXX_DiscardUnknown() { + xxx_messageInfo_MultiSig.DiscardUnknown(m) +} + +var xxx_messageInfo_MultiSig proto.InternalMessageInfo + +func (m *MultiSig) GetK() uint32 { + if m != nil { + return m.K + } + return 0 +} + +func (m *MultiSig) GetPubkeys() []*PublicKey { + if m != nil { + return m.Pubkeys + } + return nil +} + // Coin defines a token with a denomination and an amount. // // NOTE: The amount field is an Int which implements the custom method // signatures required by gogoproto. type Coin struct { Denom string `protobuf:"bytes,1,opt,name=denom,proto3" json:"denom,omitempty"` - // TODO: Consider using a string instead of bytes. This would require gogo proto - // to support types other than bytes for the customtype extension. + // TODO: Consider using a string instead of bytes. This would require gogo + // proto to support types other than bytes for the customtype extension. Amount Int `protobuf:"bytes,2,opt,name=amount,proto3,customtype=Int" json:"amount"` } func (m *Coin) Reset() { *m = Coin{} } func (*Coin) ProtoMessage() {} func (*Coin) Descriptor() ([]byte, []int) { - return fileDescriptor_97e5f89da6f0daf0, []int{0} + return fileDescriptor_97e5f89da6f0daf0, []int{2} } func (m *Coin) XXX_Unmarshal(b []byte) error { return m.Unmarshal(b) @@ -79,15 +237,15 @@ func (m *Coin) GetDenom() string { // signatures required by gogoproto. type DecCoin struct { Denom string `protobuf:"bytes,1,opt,name=denom,proto3" json:"denom,omitempty"` - // TODO: Consider using a string instead of bytes. This would require gogo proto - // to support types other than bytes for the customtype extension. + // TODO: Consider using a string instead of bytes. This would require gogo + // proto to support types other than bytes for the customtype extension. Amount Dec `protobuf:"bytes,2,opt,name=amount,proto3,customtype=Dec" json:"amount"` } func (m *DecCoin) Reset() { *m = DecCoin{} } func (*DecCoin) ProtoMessage() {} func (*DecCoin) Descriptor() ([]byte, []int) { - return fileDescriptor_97e5f89da6f0daf0, []int{1} + return fileDescriptor_97e5f89da6f0daf0, []int{3} } func (m *DecCoin) XXX_Unmarshal(b []byte) error { return m.Unmarshal(b) @@ -124,6 +282,8 @@ func (m *DecCoin) GetDenom() string { } func init() { + proto.RegisterType((*PublicKey)(nil), "cosmos_sdk.v1.PublicKey") + proto.RegisterType((*MultiSig)(nil), "cosmos_sdk.v1.MultiSig") proto.RegisterType((*Coin)(nil), "cosmos_sdk.v1.Coin") proto.RegisterType((*DecCoin)(nil), "cosmos_sdk.v1.DecCoin") } @@ -131,22 +291,157 @@ func init() { func init() { proto.RegisterFile("types/codec.proto", fileDescriptor_97e5f89da6f0daf0) } var fileDescriptor_97e5f89da6f0daf0 = []byte{ - // 225 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x12, 0x2c, 0xa9, 0x2c, 0x48, - 0x2d, 0xd6, 0x4f, 0xce, 0x4f, 0x49, 0x4d, 0xd6, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0xe2, 0x4d, - 0xce, 0x2f, 0xce, 0xcd, 0x2f, 0x8e, 0x2f, 0x4e, 0xc9, 0xd6, 0x2b, 0x33, 0x94, 0x32, 0x29, 0x4b, - 0xcd, 0x4b, 0xc9, 0x2f, 0xd2, 0x4f, 0xcf, 0x2c, 0xc9, 0x28, 0x4d, 0xd2, 0x4b, 0xce, 0xcf, 0xd5, - 0x4f, 0xcf, 0x4f, 0xcf, 0xd7, 0x07, 0x2b, 0x4e, 0x2a, 0x4d, 0x03, 0xf3, 0xc0, 0x1c, 0x30, 0x0b, - 0x62, 0x88, 0x92, 0x23, 0x17, 0x8b, 0x73, 0x7e, 0x66, 0x9e, 0x90, 0x08, 0x17, 0x6b, 0x4a, 0x6a, - 0x5e, 0x7e, 0xae, 0x04, 0xa3, 0x02, 0xa3, 0x06, 0x67, 0x10, 0x84, 0x23, 0xa4, 0xcc, 0xc5, 0x96, - 0x98, 0x9b, 0x5f, 0x9a, 0x57, 0x22, 0xc1, 0xa4, 0xc0, 0xa8, 0xc1, 0xe3, 0xc4, 0x7d, 0xe2, 0x9e, - 0x3c, 0xc3, 0xad, 0x7b, 0xf2, 0xcc, 0x9e, 0x79, 0x25, 0x41, 0x50, 0x29, 0x25, 0x17, 0x2e, 0x76, - 0x97, 0xd4, 0x64, 0x72, 0x4c, 0x71, 0x49, 0x4d, 0x86, 0x99, 0xe2, 0xe4, 0x72, 0xe3, 0xa1, 0x1c, - 0x43, 0xc3, 0x23, 0x39, 0x86, 0x13, 0x8f, 0xe4, 0x18, 0x2f, 0x3c, 0x92, 0x63, 0x7c, 0xf0, 0x48, - 0x8e, 0x71, 0xc2, 0x63, 0x39, 0x86, 0x0b, 0x8f, 0xe5, 0x18, 0x6e, 0x3c, 0x96, 0x63, 0x88, 0x52, - 0x42, 0xf2, 0x19, 0xc4, 0xeb, 0x50, 0x4a, 0xb7, 0x38, 0x25, 0x5b, 0x1f, 0x1c, 0x3e, 0x49, 0x6c, - 0x60, 0x5f, 0x19, 0x03, 0x02, 0x00, 0x00, 0xff, 0xff, 0x6c, 0x9b, 0x51, 0x3b, 0x2f, 0x01, 0x00, - 0x00, + // 366 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x9c, 0x92, 0xbd, 0x4e, 0xe3, 0x40, + 0x14, 0x85, 0x67, 0xe2, 0xfc, 0x4e, 0x92, 0x62, 0x47, 0x2b, 0xad, 0x95, 0x62, 0x12, 0x79, 0x9b, + 0x34, 0x6b, 0x2b, 0xde, 0xf5, 0x4a, 0xbb, 0x1d, 0xc1, 0x45, 0x10, 0x42, 0x42, 0xa6, 0xa3, 0x41, + 0x78, 0x3c, 0x18, 0xcb, 0xb1, 0xc7, 0xca, 0xd8, 0x91, 0xd2, 0xd1, 0xd3, 0x50, 0x52, 0xf2, 0x38, + 0x29, 0x53, 0x46, 0x14, 0x11, 0x38, 0x2f, 0x82, 0x18, 0xc7, 0xe1, 0xa7, 0xa4, 0xb2, 0xcf, 0x3d, + 0x47, 0x9f, 0xee, 0xd1, 0x1d, 0xf4, 0x2d, 0x5d, 0x24, 0x4c, 0x18, 0x94, 0x7b, 0x8c, 0xea, 0xc9, + 0x8c, 0xa7, 0x1c, 0x77, 0x29, 0x17, 0x11, 0x17, 0x17, 0xc2, 0x0b, 0xf5, 0xf9, 0xa8, 0xf7, 0x67, + 0xce, 0x62, 0x8f, 0xcf, 0x0c, 0x3f, 0x48, 0xaf, 0x33, 0x57, 0xa7, 0x3c, 0x32, 0x7c, 0xee, 0x73, + 0x43, 0x86, 0xdd, 0xec, 0x4a, 0x2a, 0x29, 0xe4, 0x5f, 0x01, 0xd1, 0x6e, 0x21, 0x6a, 0x9d, 0x66, + 0xee, 0x34, 0xa0, 0xc7, 0x6c, 0x81, 0x09, 0x6a, 0x09, 0x46, 0x13, 0xd3, 0xfa, 0x1b, 0x8e, 0x54, + 0x38, 0x80, 0xc3, 0xce, 0x04, 0x38, 0x6f, 0x23, 0xdc, 0x43, 0x0d, 0xe6, 0x99, 0x96, 0x35, 0xfa, + 0xa7, 0x56, 0x76, 0x6e, 0x39, 0xc0, 0x16, 0x6a, 0x46, 0xd9, 0x34, 0x0d, 0x44, 0xe0, 0xab, 0xca, + 0x00, 0x0e, 0xdb, 0xe6, 0x0f, 0xfd, 0xc3, 0x86, 0xfa, 0xc9, 0xab, 0x7d, 0x16, 0xf8, 0x13, 0xe0, + 0xec, 0xa3, 0xff, 0xab, 0xf7, 0x0f, 0x7d, 0x38, 0xae, 0x21, 0x25, 0xc9, 0x5c, 0xcd, 0x41, 0xcd, + 0x32, 0x84, 0x3b, 0x08, 0x86, 0x72, 0x87, 0xae, 0x03, 0x43, 0x6c, 0xa2, 0x46, 0x92, 0xb9, 0x21, + 0x5b, 0x08, 0xb5, 0x32, 0x50, 0x86, 0x6d, 0x53, 0xfd, 0x04, 0xdf, 0x97, 0x70, 0xca, 0x60, 0x81, + 0xd6, 0x0e, 0x50, 0xf5, 0x90, 0x07, 0x31, 0xfe, 0x8e, 0x6a, 0x1e, 0x8b, 0x79, 0x24, 0x99, 0x2d, + 0xa7, 0x10, 0xf8, 0x27, 0xaa, 0x5f, 0x46, 0x3c, 0x8b, 0xd3, 0xa2, 0xd0, 0xb8, 0xbd, 0xdc, 0xf4, + 0xc1, 0xe3, 0xa6, 0xaf, 0x1c, 0xc5, 0xa9, 0xb3, 0xb3, 0x34, 0x1b, 0x35, 0x6c, 0x46, 0xbf, 0x42, + 0xb1, 0x19, 0x2d, 0x29, 0x63, 0x7b, 0xfd, 0x4c, 0xc0, 0x4d, 0x4e, 0xc0, 0x32, 0x27, 0x70, 0x95, + 0x13, 0xf8, 0x94, 0x13, 0x78, 0xb7, 0x25, 0x60, 0xb5, 0x25, 0x60, 0xbd, 0x25, 0xe0, 0x5c, 0x7b, + 0x77, 0xbb, 0xa2, 0xdd, 0xee, 0xf3, 0x4b, 0x78, 0xa1, 0x21, 0x5f, 0x80, 0x5b, 0x97, 0x77, 0xfb, + 0xfd, 0x12, 0x00, 0x00, 0xff, 0xff, 0x2a, 0x54, 0x75, 0x95, 0x11, 0x02, 0x00, 0x00, +} + +func (m *PublicKey) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalToSizedBuffer(dAtA[:size]) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *PublicKey) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *PublicKey) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + if m.Pub != nil { + { + size := m.Pub.Size() + i -= size + if _, err := m.Pub.MarshalTo(dAtA[i:]); err != nil { + return 0, err + } + } + } + return len(dAtA) - i, nil +} + +func (m *PublicKey_Secp256K1) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *PublicKey_Secp256K1) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + if m.Secp256K1 != nil { + i -= len(m.Secp256K1) + copy(dAtA[i:], m.Secp256K1) + i = encodeVarintCodec(dAtA, i, uint64(len(m.Secp256K1))) + i-- + dAtA[i] = 0xa + } + return len(dAtA) - i, nil +} +func (m *PublicKey_Ed25519) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *PublicKey_Ed25519) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + if m.Ed25519 != nil { + i -= len(m.Ed25519) + copy(dAtA[i:], m.Ed25519) + i = encodeVarintCodec(dAtA, i, uint64(len(m.Ed25519))) + i-- + dAtA[i] = 0x12 + } + return len(dAtA) - i, nil +} +func (m *PublicKey_Multisig) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *PublicKey_Multisig) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + if m.Multisig != nil { + { + size, err := m.Multisig.MarshalToSizedBuffer(dAtA[:i]) + if err != nil { + return 0, err + } + i -= size + i = encodeVarintCodec(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0x1a + } + return len(dAtA) - i, nil +} +func (m *MultiSig) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalToSizedBuffer(dAtA[:size]) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *MultiSig) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *MultiSig) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + if len(m.Pubkeys) > 0 { + for iNdEx := len(m.Pubkeys) - 1; iNdEx >= 0; iNdEx-- { + { + size, err := m.Pubkeys[iNdEx].MarshalToSizedBuffer(dAtA[:i]) + if err != nil { + return 0, err + } + i -= size + i = encodeVarintCodec(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0x12 + } + } + if m.K != 0 { + i = encodeVarintCodec(dAtA, i, uint64(m.K)) + i-- + dAtA[i] = 0x8 + } + return len(dAtA) - i, nil } func (m *Coin) Marshal() (dAtA []byte, err error) { @@ -240,6 +535,72 @@ func encodeVarintCodec(dAtA []byte, offset int, v uint64) int { dAtA[offset] = uint8(v) return base } +func (m *PublicKey) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + if m.Pub != nil { + n += m.Pub.Size() + } + return n +} + +func (m *PublicKey_Secp256K1) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + if m.Secp256K1 != nil { + l = len(m.Secp256K1) + n += 1 + l + sovCodec(uint64(l)) + } + return n +} +func (m *PublicKey_Ed25519) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + if m.Ed25519 != nil { + l = len(m.Ed25519) + n += 1 + l + sovCodec(uint64(l)) + } + return n +} +func (m *PublicKey_Multisig) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + if m.Multisig != nil { + l = m.Multisig.Size() + n += 1 + l + sovCodec(uint64(l)) + } + return n +} +func (m *MultiSig) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + if m.K != 0 { + n += 1 + sovCodec(uint64(m.K)) + } + if len(m.Pubkeys) > 0 { + for _, e := range m.Pubkeys { + l = e.Size() + n += 1 + l + sovCodec(uint64(l)) + } + } + return n +} + func (m *Coin) Size() (n int) { if m == nil { return 0 @@ -276,6 +637,266 @@ func sovCodec(x uint64) (n int) { func sozCodec(x uint64) (n int) { return sovCodec(uint64((x << 1) ^ uint64((int64(x) >> 63)))) } +func (m *PublicKey) Unmarshal(dAtA []byte) error { + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowCodec + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: PublicKey: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: PublicKey: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Secp256K1", wireType) + } + var byteLen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowCodec + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + byteLen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if byteLen < 0 { + return ErrInvalidLengthCodec + } + postIndex := iNdEx + byteLen + if postIndex < 0 { + return ErrInvalidLengthCodec + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + v := make([]byte, postIndex-iNdEx) + copy(v, dAtA[iNdEx:postIndex]) + m.Pub = &PublicKey_Secp256K1{v} + iNdEx = postIndex + case 2: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Ed25519", wireType) + } + var byteLen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowCodec + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + byteLen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if byteLen < 0 { + return ErrInvalidLengthCodec + } + postIndex := iNdEx + byteLen + if postIndex < 0 { + return ErrInvalidLengthCodec + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + v := make([]byte, postIndex-iNdEx) + copy(v, dAtA[iNdEx:postIndex]) + m.Pub = &PublicKey_Ed25519{v} + iNdEx = postIndex + case 3: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Multisig", wireType) + } + var msglen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowCodec + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + msglen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if msglen < 0 { + return ErrInvalidLengthCodec + } + postIndex := iNdEx + msglen + if postIndex < 0 { + return ErrInvalidLengthCodec + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + v := &MultiSig{} + if err := v.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + m.Pub = &PublicKey_Multisig{v} + iNdEx = postIndex + default: + iNdEx = preIndex + skippy, err := skipCodec(dAtA[iNdEx:]) + if err != nil { + return err + } + if skippy < 0 { + return ErrInvalidLengthCodec + } + if (iNdEx + skippy) < 0 { + return ErrInvalidLengthCodec + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} +func (m *MultiSig) Unmarshal(dAtA []byte) error { + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowCodec + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: MultiSig: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: MultiSig: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field K", wireType) + } + m.K = 0 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowCodec + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + m.K |= uint32(b&0x7F) << shift + if b < 0x80 { + break + } + } + case 2: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Pubkeys", wireType) + } + var msglen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowCodec + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + msglen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if msglen < 0 { + return ErrInvalidLengthCodec + } + postIndex := iNdEx + msglen + if postIndex < 0 { + return ErrInvalidLengthCodec + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.Pubkeys = append(m.Pubkeys, &PublicKey{}) + if err := m.Pubkeys[len(m.Pubkeys)-1].Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + default: + iNdEx = preIndex + skippy, err := skipCodec(dAtA[iNdEx:]) + if err != nil { + return err + } + if skippy < 0 { + return ErrInvalidLengthCodec + } + if (iNdEx + skippy) < 0 { + return ErrInvalidLengthCodec + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} func (m *Coin) Unmarshal(dAtA []byte) error { l := len(dAtA) iNdEx := 0 diff --git a/types/codec.proto b/types/codec.proto index 8732c9dc1cf7..101b1a8b84a2 100644 --- a/types/codec.proto +++ b/types/codec.proto @@ -7,15 +7,45 @@ option (gogoproto.stringer_all) = false; import "vendor/github.com/gogo/protobuf/gogoproto/gogo.proto"; +// PublicKey defines a proto message with a single oneof field that represents a +// public key as a byte slice. +// +// NOTE: Private keys must still be amino-encoded for backwards compatiblity. +message PublicKey { + option (gogoproto.goproto_stringer) = true; + + // TODO: Make tendermint keys use []byte instead of [32]byte so we can use the + // casttype extension. + // option (cosmos_proto.interface_type) = + // "github.com/tendermint/tendermint/crypto.PubKey"; + + // pub defines an single supported public key type as a byte slice. + oneof pub { + bytes secp256k1 = 1; + bytes ed25519 = 2; + MultiSig multisig = 3; + } +} + +// MultiSig defines a proto message representing a threshold multi-signature +// scheme. +message MultiSig { + option (gogoproto.goproto_stringer) = true; + + uint32 k = 1; + repeated PublicKey pubkeys = 2; +} + // Coin defines a token with a denomination and an amount. // // NOTE: The amount field is an Int which implements the custom method // signatures required by gogoproto. message Coin { string denom = 1; - // TODO: Consider using a string instead of bytes. This would require gogo proto - // to support types other than bytes for the customtype extension. - bytes amount = 2 [ (gogoproto.customtype) = "Int", (gogoproto.nullable) = false ]; + // TODO: Consider using a string instead of bytes. This would require gogo + // proto to support types other than bytes for the customtype extension. + bytes amount = 2 + [(gogoproto.customtype) = "Int", (gogoproto.nullable) = false]; } // DecCoin defines a token with a denomination and a decimal amount. @@ -24,7 +54,8 @@ message Coin { // signatures required by gogoproto. message DecCoin { string denom = 1; - // TODO: Consider using a string instead of bytes. This would require gogo proto - // to support types other than bytes for the customtype extension. - bytes amount = 2 [ (gogoproto.customtype) = "Dec", (gogoproto.nullable) = false ]; + // TODO: Consider using a string instead of bytes. This would require gogo + // proto to support types other than bytes for the customtype extension. + bytes amount = 2 + [(gogoproto.customtype) = "Dec", (gogoproto.nullable) = false]; } diff --git a/types/pubkey.go b/types/pubkey.go new file mode 100644 index 000000000000..5c3846bb8633 --- /dev/null +++ b/types/pubkey.go @@ -0,0 +1,158 @@ +package types + +import ( + "github.com/pkg/errors" + tmcrypto "github.com/tendermint/tendermint/crypto" + "github.com/tendermint/tendermint/crypto/ed25519" + "github.com/tendermint/tendermint/crypto/multisig" + "github.com/tendermint/tendermint/crypto/secp256k1" +) + +// public key sentinel errors +var ( + ErrInvalidPubKeySecp256k1Length = errors.New("invalid PubKeySecp256k1 length") + ErrInvalidPubKeySecp256k1 = errors.New("incompatible PubKeySecp256k1") + + ErrInvalidPubKeyEd25519Length = errors.New("invalid PubKeyEd25519 length") + ErrInvalidPubKeyEd25519 = errors.New("incompatible PubKeyEd25519") + + ErrInvalidPubKeyMultisigThreshold = errors.New("incompatible PubKeyMultisigThreshold") +) + +var _ tmcrypto.PubKey = (*PublicKey)(nil) + +// GetPubKeySecp256k1 returns a Tendermint secp256k1 public key from the +// PublicKey message type. It will return an error if the size of the public key +// is invalid or the underlying Pub field is invalid. +// +// NOTE: Do not use or call bytes on the result when serializing. +func (m *PublicKey) GetPubKeySecp256k1() (secp256k1.PubKeySecp256k1, error) { + pk := secp256k1.PubKeySecp256k1{} + + if x, ok := m.GetPub().(*PublicKey_Secp256K1); ok { + if len(x.Secp256K1) != secp256k1.PubKeySecp256k1Size { + return pk, ErrInvalidPubKeySecp256k1Length + } + + copy(pk[:], x.Secp256K1) + return pk, nil + } + + return pk, ErrInvalidPubKeySecp256k1 +} + +// GetPubKeyEd25519 returns a Tendermint Ed25519 public key from the PublicKey +// message type. It will return an error if the size of the public key +// is invalid or the underlying Pub field is invalid. +// +// NOTE: Do not use or call bytes on the result when serializing. +func (m *PublicKey) GetPubKeyEd25519() (ed25519.PubKeyEd25519, error) { + pk := ed25519.PubKeyEd25519{} + + if x, ok := m.GetPub().(*PublicKey_Ed25519); ok { + if len(x.Ed25519) != ed25519.PubKeyEd25519Size { + return pk, ErrInvalidPubKeyEd25519Length + } + + copy(pk[:], x.Ed25519) + return pk, nil + } + + return pk, ErrInvalidPubKeyEd25519 +} + +// GetPubKeyMultisigThreshold returns a Tendermint multi-sig threshold public key +// from the PublicKey message type. It will return an error if the size of the +// public key is invalid or the underlying Pub field is invalid. +// +// NOTE: Do not use or call bytes on the result when serializing. +func (m *PublicKey) GetPubKeyMultisigThreshold() (multisig.PubKeyMultisigThreshold, error) { + mspk := multisig.PubKeyMultisigThreshold{} + + if x, ok := m.GetPub().(*PublicKey_Multisig); ok { + mspk.K = uint(x.Multisig.K) + mspk.PubKeys = make([]tmcrypto.PubKey, len(x.Multisig.Pubkeys)) + + for i, pk := range x.Multisig.Pubkeys { + mspk.PubKeys[i] = pk.TendermintPubKey() + } + + return mspk, nil + } + + return mspk, ErrInvalidPubKeyMultisigThreshold +} + +// TendermintPubKey returns a Tendermint PubKey from the PublicKey proto message +// type. +func (m *PublicKey) TendermintPubKey() tmcrypto.PubKey { + switch m.GetPub().(type) { + case *PublicKey_Secp256K1: + pk, err := m.GetPubKeySecp256k1() + if err != nil { + return nil + } + + return pk + + case *PublicKey_Ed25519: + pk, err := m.GetPubKeyEd25519() + if err != nil { + return nil + } + + return pk + + case *PublicKey_Multisig: + mspk, err := m.GetPubKeyMultisigThreshold() + if err != nil { + return nil + } + + return mspk + + default: + return nil + } +} + +// Address returns the address of a Tendermint PubKey. +func (m *PublicKey) Address() tmcrypto.Address { + if pk := m.TendermintPubKey(); pk != nil { + return pk.Address() + } + + return nil +} + +// VerifyBytes attempts to construct a Tendermint PubKey and delegates the +// VerifyBytes call to the constructed type. +func (m *PublicKey) VerifyBytes(msg []byte, sig []byte) bool { + if pk := m.TendermintPubKey(); pk != nil { + return pk.VerifyBytes(msg, sig) + } + + return false +} + +// Equals attempts to construct a Tendermint PubKey and delegates the Equals call +// to the constructed type. +func (m *PublicKey) Equals(other tmcrypto.PubKey) bool { + if pk := m.TendermintPubKey(); pk != nil { + return pk.Equals(other) + } + + return false +} + +// Bytes returns the raw proto encoded bytes of the PublicKey proto message type. +// Note, this is not wire compatible with calling Bytes on the underlying +// Tendermint PubKey type. +func (m *PublicKey) Bytes() []byte { + bz, err := m.Marshal() + if err != nil { + panic(err) + } + + return bz +} diff --git a/types/pubkey_test.go b/types/pubkey_test.go new file mode 100644 index 000000000000..952f2d26e2ba --- /dev/null +++ b/types/pubkey_test.go @@ -0,0 +1,112 @@ +package types_test + +import ( + "testing" + + "github.com/cosmos/cosmos-sdk/types" + + "github.com/stretchr/testify/require" + tmcrypto "github.com/tendermint/tendermint/crypto" + "github.com/tendermint/tendermint/crypto/ed25519" + "github.com/tendermint/tendermint/crypto/multisig" + "github.com/tendermint/tendermint/crypto/secp256k1" +) + +func TestGetPubKeySecp256k1(t *testing.T) { + tmpk := (secp256k1.GenPrivKey().PubKey()).(secp256k1.PubKeySecp256k1) + testCases := []struct { + input *types.PublicKey + expected secp256k1.PubKeySecp256k1 + err error + }{ + { + &types.PublicKey{&types.PublicKey_Secp256K1{tmpk[:]}}, + tmpk, + nil, + }, + { + &types.PublicKey{&types.PublicKey_Secp256K1{append(tmpk[:], tmpk[:]...)}}, + secp256k1.PubKeySecp256k1{}, + types.ErrInvalidPubKeySecp256k1Length, + }, + { + &types.PublicKey{&types.PublicKey_Ed25519{}}, + secp256k1.PubKeySecp256k1{}, + types.ErrInvalidPubKeySecp256k1, + }, + } + + for _, tc := range testCases { + pk, err := tc.input.GetPubKeySecp256k1() + require.Equal(t, tc.err, err) + require.True(t, pk.Equals(tc.expected)) + } +} + +func TestGetPubKeyEd25519(t *testing.T) { + tmpk := (ed25519.GenPrivKey().PubKey()).(ed25519.PubKeyEd25519) + testCases := []struct { + input *types.PublicKey + expected ed25519.PubKeyEd25519 + err error + }{ + { + &types.PublicKey{&types.PublicKey_Ed25519{tmpk[:]}}, + tmpk, + nil, + }, + { + &types.PublicKey{&types.PublicKey_Ed25519{append(tmpk[:], tmpk[:]...)}}, + ed25519.PubKeyEd25519{}, + types.ErrInvalidPubKeyEd25519Length, + }, + { + &types.PublicKey{&types.PublicKey_Secp256K1{}}, + ed25519.PubKeyEd25519{}, + types.ErrInvalidPubKeyEd25519, + }, + } + + for _, tc := range testCases { + pk, err := tc.input.GetPubKeyEd25519() + require.Equal(t, tc.err, err) + require.True(t, pk.Equals(tc.expected)) + } +} + +func TestAddress(t *testing.T) { + ed25519pk := (ed25519.GenPrivKey().PubKey()).(ed25519.PubKeyEd25519) + secp256k1pk := (secp256k1.GenPrivKey().PubKey()).(secp256k1.PubKeySecp256k1) + secp256k1pk2 := (secp256k1.GenPrivKey().PubKey()).(secp256k1.PubKeySecp256k1) + multisigpk := multisig.NewPubKeyMultisigThreshold(2, []tmcrypto.PubKey{secp256k1pk, secp256k1pk2}) + + testCases := []struct { + pubkey tmcrypto.PubKey + input *types.PublicKey + }{ + { + ed25519pk, &types.PublicKey{&types.PublicKey_Ed25519{ed25519pk[:]}}, + }, + { + secp256k1pk, &types.PublicKey{&types.PublicKey_Secp256K1{secp256k1pk[:]}}, + }, + { + multisigpk, + &types.PublicKey{ + &types.PublicKey_Multisig{ + Multisig: &types.MultiSig{ + K: 2, + Pubkeys: []*types.PublicKey{ + {&types.PublicKey_Secp256K1{secp256k1pk[:]}}, + {&types.PublicKey_Secp256K1{secp256k1pk2[:]}}, + }, + }, + }, + }, + }, + } + + for _, tc := range testCases { + require.Equal(t, tc.pubkey.Address(), tc.input.Address()) + } +} From 9f07f11d7ef8a2eebfc0f72271cd30c5393b6c7c Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Fri, 3 Jan 2020 17:27:03 -0500 Subject: [PATCH 38/40] Move install to contrib --- Makefile | 1 - contrib/devtools/Makefile | 5 +++- go.mod | 3 +- go.sum | 4 +++ x/auth/exported/exported.go | 12 ++++---- x/auth/types/account.go | 31 ++++++++++---------- x/auth/types/codec.pb.go | 56 ++++++++++++++++++------------------- x/auth/types/codec.proto | 12 ++++---- 8 files changed, 63 insertions(+), 61 deletions(-) diff --git a/Makefile b/Makefile index 695f31758681..e0b02d93e961 100644 --- a/Makefile +++ b/Makefile @@ -231,7 +231,6 @@ proto-all: proto-gen proto-lint proto-check-breaking proto-gen: @go mod vendor - @go install github.com/regen-network/cosmos-proto/protoc-gen-gocosmos @./scripts/protocgen.sh proto-lint: diff --git a/contrib/devtools/Makefile b/contrib/devtools/Makefile index dda055299f22..388f76a49fc7 100644 --- a/contrib/devtools/Makefile +++ b/contrib/devtools/Makefile @@ -1,5 +1,5 @@ .PHONY: all tools tools-clean statik runsim golangci-lint \ -buf protoc-gen-buf-check-breaking protoc-gen-buf-check-lint +buf protoc-gen-buf-check-breaking protoc-gen-buf-check-lint protoc-gen-gocosmos ### # Find OS and Go environment @@ -83,6 +83,9 @@ $(RUNSIM): @echo "Installing runsim..." @(cd /tmp && go get github.com/cosmos/tools/cmd/runsim@v1.0.0) +protoc-gen-gocosmos: + @go install github.com/regen-network/cosmos-proto/protoc-gen-gocosmos + buf: protoc-gen-buf-check-breaking protoc-gen-buf-check-lint @echo "Installing buf..." @curl -sSL \ diff --git a/go.mod b/go.mod index bb0a000b84a3..9ff416d29d46 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ require ( github.com/99designs/keyring v1.1.3 github.com/bartekn/go-bip39 v0.0.0-20171116152956-a05967ea095d github.com/bgentry/speakeasy v0.1.0 - github.com/btcsuite/btcd v0.0.0-20190115013929-ed77733ec07d + github.com/btcsuite/btcd v0.0.0-20190523000118-16327141da8c github.com/cosmos/go-bip39 v0.0.0-20180618194314-52158e4697b8 github.com/cosmos/ledger-cosmos-go v0.11.1 github.com/gogo/protobuf v1.3.1 @@ -16,6 +16,7 @@ require ( github.com/pelletier/go-toml v1.6.0 github.com/pkg/errors v0.8.1 github.com/rakyll/statik v0.1.6 + github.com/regen-network/cosmos-proto v0.0.0-20200103162838-2560887837f1 // indirect github.com/spf13/afero v1.2.1 // indirect github.com/spf13/cobra v0.0.5 github.com/spf13/jwalterweatherman v1.1.0 // indirect diff --git a/go.sum b/go.sum index 8e7d48db473d..e175fc94a9ab 100644 --- a/go.sum +++ b/go.sum @@ -21,9 +21,13 @@ github.com/bgentry/speakeasy v0.1.0 h1:ByYyxL9InA1OWqxJqqp2A5pYHUrCiAL6K3J+LKSsQ github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs= github.com/btcsuite/btcd v0.0.0-20190115013929-ed77733ec07d h1:xG8Pj6Y6J760xwETNmMzmlt38QSwz0BLp1cZ09g27uw= github.com/btcsuite/btcd v0.0.0-20190115013929-ed77733ec07d/go.mod h1:d3C0AkH6BRcvO8T0UEPu53cnw4IbV63x1bEjildYhO0= +github.com/btcsuite/btcd v0.0.0-20190523000118-16327141da8c h1:aEbSeNALREWXk0G7UdNhR3ayBV7tZ4M2PNmnrCAph6Q= +github.com/btcsuite/btcd v0.0.0-20190523000118-16327141da8c/go.mod h1:3J08xEfcugPacsc34/LKRU2yO7YmuT8yt28J8k2+rrI= github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f/go.mod h1:TdznJufoqS23FtqVCzL0ZqgP5MqXbb4fg/WgDys70nA= github.com/btcsuite/btcutil v0.0.0-20180706230648-ab6388e0c60a h1:RQMUrEILyYJEoAT34XS/kLu40vC0+po/UfxrBBA4qZE= github.com/btcsuite/btcutil v0.0.0-20180706230648-ab6388e0c60a/go.mod h1:+5NJ2+qvTyV9exUAL/rxXi3DcLg2Ts+ymUAY5y4NvMg= +github.com/btcsuite/btcutil v0.0.0-20190425235716-9e5f4b9a998d h1:yJzD/yFppdVCf6ApMkVy8cUxV0XrxdP9rVf6D87/Mng= +github.com/btcsuite/btcutil v0.0.0-20190425235716-9e5f4b9a998d/go.mod h1:+5NJ2+qvTyV9exUAL/rxXi3DcLg2Ts+ymUAY5y4NvMg= github.com/btcsuite/go-socks v0.0.0-20170105172521-4720035b7bfd/go.mod h1:HHNXQzUsZCxOoE+CPiyCTO6x34Zs86zZUiwtpXoGdtg= github.com/btcsuite/goleveldb v0.0.0-20160330041536-7834afc9e8cd/go.mod h1:F+uVaaLLH7j4eDXPRvw78tMflu7Ie2bzYOH4Y8rRKBY= github.com/btcsuite/snappy-go v0.0.0-20151229074030-0bdef8d06723/go.mod h1:8woku9dyThutzjeg+3xrA5iCpBRH8XEEg3lh6TiUghc= diff --git a/x/auth/exported/exported.go b/x/auth/exported/exported.go index fe4fb16d8336..4f1dba682872 100644 --- a/x/auth/exported/exported.go +++ b/x/auth/exported/exported.go @@ -3,23 +3,21 @@ package exported import ( "time" - "github.com/tendermint/tendermint/crypto" - sdk "github.com/cosmos/cosmos-sdk/types" ) -// Account is an interface used to store coins at a given address within state. +// AccountI is an interface used to store coins at a given address within state. // It presumes a notion of sequence numbers for replay protection, // a notion of account numbers for replay protection for previously pruned accounts, // and a pubkey for authentication purposes. // // Many complex conditions can be used in the concrete struct which implements Account. -type Account interface { +type AccountI interface { GetAddress() sdk.AccAddress SetAddress(sdk.AccAddress) error // errors if already set. - GetPubKey() crypto.PubKey // can return nil. - SetPubKey(crypto.PubKey) error + GetPubKey() *sdk.PublicKey // can return nil. + SetPubKey(*sdk.PublicKey) error GetAccountNumber() uint64 SetAccountNumber(uint64) error @@ -55,6 +53,6 @@ func (ga GenesisAccounts) Contains(addr sdk.Address) bool { // GenesisAccount defines a genesis account that embeds an Account with validation capabilities. type GenesisAccount interface { - Account + AccountI Validate() error } diff --git a/x/auth/types/account.go b/x/auth/types/account.go index 14ef21f73a2e..08866b41d3ca 100644 --- a/x/auth/types/account.go +++ b/x/auth/types/account.go @@ -7,7 +7,6 @@ import ( "time" "github.com/cosmos/cosmos-sdk/crypto" - tmcrypto "github.com/tendermint/tendermint/crypto" yaml "gopkg.in/yaml.v2" sdk "github.com/cosmos/cosmos-sdk/types" @@ -17,25 +16,25 @@ import ( //----------------------------------------------------------------------------- // BaseAccount -var _ exported.Account = (*BaseAccount)(nil) +var _ exported.AccountI = (*BaseAccount)(nil) var _ exported.GenesisAccount = (*BaseAccount)(nil) // NewBaseAccount creates a new BaseAccount object func NewBaseAccount( - address sdk.AccAddress, coins sdk.Coins, pubKey tmcrypto.PubKey, accountNumber, sequence uint64, + address sdk.AccAddress, coins sdk.Coins, pubkey *crypto.PublicKey, accountNumber, sequence uint64, ) *BaseAccount { return &BaseAccount{ Address: address, Coins: coins, - PublicKey: pubKey.Bytes(), + PublicKey: pubkey, AccountNumber: accountNumber, Sequence: sequence, } } // ProtoBaseAccount - a prototype function for BaseAccount -func ProtoBaseAccount() exported.Account { +func ProtoBaseAccount() exported.AccountI { return &BaseAccount{} } @@ -96,8 +95,8 @@ func (acc *BaseAccount) SetAddress(addr sdk.AccAddress) error { } // SetPubKey - Implements sdk.Account. -func (acc *BaseAccount) SetPubKey(pubKey tmcrypto.PubKey) error { - acc.PubKey = pubKey +func (acc *BaseAccount) SetPubKey(pubkey *crypto.PublicKey) error { + acc.PublicKey = pubkey return nil } @@ -127,9 +126,9 @@ func (acc *BaseAccount) SpendableCoins(_ time.Time) sdk.Coins { // Validate checks for errors on the account fields func (acc BaseAccount) Validate() error { - if acc.PubKey != nil && acc.Address != nil && - !bytes.Equal(acc.PubKey.Address().Bytes(), acc.Address.Bytes()) { - return errors.New("pubkey and address pair is invalid") + if acc.PublicKey != nil && acc.Address != nil && + !bytes.Equal(acc.PublicKey.Address().Bytes(), acc.Address.Bytes()) { + return errors.New("pubkey and address pair are invalid") } return nil @@ -157,8 +156,8 @@ func (acc BaseAccount) MarshalYAML() (interface{}, error) { Sequence: acc.Sequence, } - if acc.PubKey != nil { - pks, err := sdk.Bech32ifyAccPub(acc.PubKey) + if acc.PublicKey != nil { + pks, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, acc.PublicKey) if err != nil { return nil, err } @@ -183,8 +182,8 @@ func (acc BaseAccount) MarshalJSON() ([]byte, error) { Sequence: acc.Sequence, } - if acc.PubKey != nil { - pks, err := sdk.Bech32ifyAccPub(acc.PubKey) + if acc.PublicKey != nil { + pks, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, acc.PublicKey) if err != nil { return nil, err } @@ -203,12 +202,12 @@ func (acc *BaseAccount) UnmarshalJSON(bz []byte) error { } if alias.PubKey != "" { - pk, err := sdk.GetAccPubKeyBech32(alias.PubKey) + pk, err := sdk.GetPubKeyFromBech32(sdk.Bech32PubKeyTypeAccPub, alias.PubKey) if err != nil { return err } - acc.PubKey = pk + acc.PublicKey = pk } acc.Address = alias.Address diff --git a/x/auth/types/codec.pb.go b/x/auth/types/codec.pb.go index a256fc36e2a8..7903df30aa4e 100644 --- a/x/auth/types/codec.pb.go +++ b/x/auth/types/codec.pb.go @@ -5,7 +5,6 @@ package types import ( fmt "fmt" - crypto "github.com/cosmos/cosmos-sdk/crypto" github_com_cosmos_cosmos_sdk_types "github.com/cosmos/cosmos-sdk/types" types "github.com/cosmos/cosmos-sdk/types" _ "github.com/gogo/protobuf/gogoproto" @@ -27,12 +26,12 @@ var _ = math.Inf const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package // BaseAccount defines a base account message structure. This can be extended by -// embedding within another account type that extends functionality. A BaseAccount -// provides the basic functionality needed to operate an account. +// embedding within another account type that extends functionality. A +// BaseAccount provides the basic functionality needed to operate an account. type BaseAccount struct { Address github_com_cosmos_cosmos_sdk_types.AccAddress `protobuf:"bytes,1,opt,name=address,proto3,casttype=github.com/cosmos/cosmos-sdk/types.AccAddress" json:"address,omitempty"` Coins []types.Coin `protobuf:"bytes,2,rep,name=coins,proto3" json:"coins"` - PublicKey *crypto.PublicKey `protobuf:"bytes,3,opt,name=public_key,json=publicKey,proto3" json:"public_key,omitempty"` + PublicKey *types.PublicKey `protobuf:"bytes,3,opt,name=public_key,json=publicKey,proto3" json:"public_key,omitempty"` AccountNumber uint64 `protobuf:"varint,4,opt,name=account_number,json=accountNumber,proto3" json:"account_number,omitempty"` Sequence uint64 `protobuf:"varint,5,opt,name=sequence,proto3" json:"sequence,omitempty"` } @@ -76,30 +75,29 @@ func init() { func init() { proto.RegisterFile("x/auth/types/codec.proto", fileDescriptor_e44d2a11716720ad) } var fileDescriptor_e44d2a11716720ad = []byte{ - // 354 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x7c, 0x51, 0x3f, 0x4b, 0xc3, 0x40, - 0x1c, 0x4d, 0xda, 0xd4, 0x3f, 0x57, 0x15, 0x8c, 0x0e, 0x21, 0xc3, 0x25, 0x08, 0x42, 0x1c, 0x7a, - 0xa1, 0xd5, 0xc9, 0x41, 0x68, 0x1c, 0x0b, 0x22, 0x19, 0x5d, 0x4a, 0x72, 0x39, 0xdb, 0x50, 0x9b, - 0x8b, 0xb9, 0x5c, 0x69, 0x36, 0x47, 0x47, 0x07, 0x3f, 0x80, 0x1f, 0xa7, 0x63, 0xc7, 0x4e, 0x45, - 0xd3, 0x6f, 0xe1, 0x24, 0xbd, 0x6b, 0x25, 0x20, 0x38, 0xdd, 0xef, 0x3d, 0xde, 0x7b, 0x77, 0xef, - 0x77, 0xc0, 0x98, 0xba, 0x01, 0xcf, 0x87, 0x6e, 0x5e, 0xa4, 0x84, 0xb9, 0x98, 0x46, 0x04, 0xa3, - 0x34, 0xa3, 0x39, 0xd5, 0x4f, 0x31, 0x65, 0x63, 0xca, 0xfa, 0x2c, 0x1a, 0xa1, 0x29, 0x5a, 0x8b, - 0xd0, 0xa4, 0x6d, 0x5e, 0x4d, 0x48, 0x12, 0xd1, 0xcc, 0x1d, 0xc4, 0xf9, 0x90, 0x87, 0x08, 0xd3, - 0xb1, 0x3b, 0xa0, 0x03, 0xea, 0x0a, 0x4f, 0xc8, 0x1f, 0x05, 0x12, 0x40, 0x4c, 0x32, 0xcb, 0x3c, - 0xfe, 0x13, 0x6f, 0xea, 0x38, 0x2b, 0xd2, 0x9c, 0x56, 0xb9, 0xb3, 0xf7, 0x1a, 0x68, 0x7a, 0x01, - 0x23, 0x5d, 0x8c, 0x29, 0x4f, 0x72, 0xbd, 0x07, 0x76, 0x83, 0x28, 0xca, 0x08, 0x63, 0x86, 0x6a, - 0xab, 0xce, 0x81, 0xd7, 0xfe, 0x5e, 0x5a, 0xad, 0xca, 0xd5, 0xf2, 0x89, 0x9b, 0xa3, 0xc5, 0xa2, - 0x91, 0xec, 0x81, 0xba, 0x18, 0x77, 0xa5, 0xd1, 0xdf, 0x26, 0xe8, 0x2e, 0x68, 0x60, 0x1a, 0x27, - 0xcc, 0xa8, 0xd9, 0x75, 0xa7, 0xd9, 0x39, 0x41, 0x95, 0x7e, 0x93, 0x36, 0xba, 0xa5, 0x71, 0xe2, - 0x69, 0xb3, 0xa5, 0xa5, 0xf8, 0x52, 0xa7, 0xdf, 0x00, 0x90, 0xf2, 0xf0, 0x29, 0xc6, 0xfd, 0x11, - 0x29, 0x8c, 0xba, 0xad, 0x3a, 0xcd, 0x8e, 0x55, 0x75, 0xc9, 0x06, 0x6b, 0xf3, 0xbd, 0xd0, 0xf5, - 0x48, 0xe1, 0xef, 0xa7, 0xdb, 0x51, 0x3f, 0x07, 0x47, 0x81, 0x2c, 0xd2, 0x4f, 0xf8, 0x38, 0x24, - 0x99, 0xa1, 0xd9, 0xaa, 0xa3, 0xf9, 0x87, 0x1b, 0xf6, 0x4e, 0x90, 0xba, 0x09, 0xf6, 0x18, 0x79, - 0xe6, 0x24, 0xc1, 0xc4, 0x68, 0x08, 0xc1, 0x2f, 0xbe, 0xd6, 0x5e, 0x3f, 0x2c, 0xc5, 0xeb, 0x2d, - 0xbe, 0xa0, 0xf2, 0x52, 0x42, 0x65, 0x56, 0x42, 0x75, 0x5e, 0x42, 0xf5, 0xb3, 0x84, 0xea, 0xdb, - 0x0a, 0x2a, 0xf3, 0x15, 0x54, 0x16, 0x2b, 0xa8, 0x3c, 0x5c, 0xfc, 0xbb, 0x93, 0xea, 0x17, 0x87, - 0x3b, 0x62, 0xd5, 0x97, 0x3f, 0x01, 0x00, 0x00, 0xff, 0xff, 0xb5, 0x52, 0x2c, 0x9d, 0xf9, 0x01, - 0x00, 0x00, + // 343 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x7c, 0x51, 0xbf, 0x4b, 0xc3, 0x40, + 0x14, 0xce, 0xb5, 0xa9, 0x3f, 0xae, 0x2a, 0x18, 0x1d, 0x8e, 0x0e, 0xd7, 0x20, 0x08, 0x71, 0xe8, + 0x1d, 0xad, 0x82, 0xe0, 0xd6, 0x38, 0x16, 0x44, 0x32, 0xba, 0x94, 0xe4, 0x72, 0xb6, 0xa1, 0x36, + 0x17, 0x73, 0xb9, 0xd2, 0x6e, 0x8e, 0x2e, 0x82, 0x7f, 0x82, 0x7f, 0x4e, 0xc7, 0x8e, 0x9d, 0x8a, + 0xa6, 0xff, 0x85, 0x93, 0x78, 0x69, 0xa5, 0x28, 0x38, 0xdd, 0xfb, 0x1e, 0xdf, 0xf7, 0xdd, 0x7b, + 0xdf, 0x83, 0x68, 0x4c, 0x7d, 0x95, 0xf5, 0x69, 0x36, 0x49, 0xb8, 0xa4, 0x4c, 0x84, 0x9c, 0x91, + 0x24, 0x15, 0x99, 0xb0, 0x8e, 0x99, 0x90, 0x43, 0x21, 0xbb, 0x32, 0x1c, 0x90, 0x31, 0xf9, 0x26, + 0x91, 0x51, 0xb3, 0x76, 0x31, 0xe2, 0x71, 0x28, 0x52, 0xda, 0x8b, 0xb2, 0xbe, 0x0a, 0x08, 0x13, + 0x43, 0xda, 0x13, 0x3d, 0x41, 0xb5, 0x26, 0x50, 0xf7, 0x1a, 0x69, 0xa0, 0xab, 0xc2, 0xab, 0x76, + 0xf8, 0xc7, 0xfe, 0xe4, 0xa5, 0x04, 0xab, 0xae, 0x2f, 0x79, 0x9b, 0x31, 0xa1, 0xe2, 0xcc, 0xea, + 0xc0, 0x6d, 0x3f, 0x0c, 0x53, 0x2e, 0x25, 0x02, 0x36, 0x70, 0xf6, 0xdc, 0xe6, 0xe7, 0xa2, 0xde, + 0xd8, 0xf8, 0xa6, 0x18, 0x67, 0xf5, 0x34, 0x64, 0x38, 0x28, 0x66, 0x26, 0x6d, 0xc6, 0xda, 0x85, + 0xd0, 0x5b, 0x3b, 0x58, 0x14, 0x56, 0x98, 0x88, 0x62, 0x89, 0x4a, 0x76, 0xd9, 0xa9, 0xb6, 0x8e, + 0xc8, 0xc6, 0x2e, 0xa3, 0x26, 0xb9, 0x16, 0x51, 0xec, 0x9a, 0xd3, 0x45, 0xdd, 0xf0, 0x0a, 0x9e, + 0x75, 0x09, 0x61, 0xa2, 0x82, 0x87, 0x88, 0x75, 0x07, 0x7c, 0x82, 0xca, 0x36, 0x70, 0xaa, 0x2d, + 0xf4, 0x4b, 0x75, 0xab, 0x09, 0x1d, 0x3e, 0xf1, 0x76, 0x93, 0x75, 0x69, 0x9d, 0xc2, 0x03, 0xbf, + 0xd8, 0xa0, 0x1b, 0xab, 0x61, 0xc0, 0x53, 0x64, 0xda, 0xc0, 0x31, 0xbd, 0xfd, 0x55, 0xf7, 0x46, + 0x37, 0xad, 0x1a, 0xdc, 0x91, 0xfc, 0x51, 0xf1, 0x98, 0x71, 0x54, 0xd1, 0x84, 0x1f, 0x7c, 0x65, + 0x3e, 0xbf, 0xd5, 0x0d, 0xb7, 0x33, 0xff, 0xc0, 0xc6, 0x53, 0x8e, 0x8d, 0x69, 0x8e, 0xc1, 0x2c, + 0xc7, 0xe0, 0x3d, 0xc7, 0xe0, 0x75, 0x89, 0x8d, 0xd9, 0x12, 0x1b, 0xf3, 0x25, 0x36, 0xee, 0xce, + 0xfe, 0x0d, 0x63, 0xf3, 0x8e, 0xc1, 0x96, 0xce, 0xf8, 0xfc, 0x2b, 0x00, 0x00, 0xff, 0xff, 0xea, + 0x9c, 0xc4, 0x82, 0xde, 0x01, 0x00, 0x00, } func (m *BaseAccount) Marshal() (dAtA []byte, err error) { @@ -341,7 +339,7 @@ func (m *BaseAccount) Unmarshal(dAtA []byte) error { return io.ErrUnexpectedEOF } if m.PublicKey == nil { - m.PublicKey = &crypto.PublicKey{} + m.PublicKey = &types.PublicKey{} } if err := m.PublicKey.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { return err diff --git a/x/auth/types/codec.proto b/x/auth/types/codec.proto index 564b05963a1e..485912b3478f 100644 --- a/x/auth/types/codec.proto +++ b/x/auth/types/codec.proto @@ -3,25 +3,25 @@ package cosmos_sdk.x.auth.v1; import "vendor/github.com/gogo/protobuf/gogoproto/gogo.proto"; import "types/codec.proto"; -import "crypto/codec.proto"; option go_package = "github.com/cosmos/cosmos-sdk/x/auth/types"; option (gogoproto.goproto_stringer_all) = false; option (gogoproto.stringer_all) = false; // TODO: -// +// // 1. StdTx // BaseAccount defines a base account message structure. This can be extended by -// embedding within another account type that extends functionality. A BaseAccount -// provides the basic functionality needed to operate an account. +// embedding within another account type that extends functionality. A +// BaseAccount provides the basic functionality needed to operate an account. message BaseAccount { option (gogoproto.goproto_getters) = false; - bytes address = 1 [(gogoproto.casttype) = "github.com/cosmos/cosmos-sdk/types.AccAddress"]; + bytes address = 1 + [(gogoproto.casttype) = "github.com/cosmos/cosmos-sdk/types.AccAddress"]; repeated cosmos_sdk.v1.Coin coins = 2 [(gogoproto.nullable) = false]; - cosmos_sdk.crypto.v1.PublicKey public_key = 3; + cosmos_sdk.v1.PublicKey public_key = 3; uint64 account_number = 4; uint64 sequence = 5; } \ No newline at end of file From 120da39a9e692584254d0d369e90549f55932d35 Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Sat, 4 Jan 2020 15:11:13 -0500 Subject: [PATCH 39/40] Update types pubkey --- go.mod | 2 +- types/pubkey.go | 14 ++++++++++++++ types/pubkey_test.go | 11 +++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 9ff416d29d46..2fb515ae2e7b 100644 --- a/go.mod +++ b/go.mod @@ -16,7 +16,7 @@ require ( github.com/pelletier/go-toml v1.6.0 github.com/pkg/errors v0.8.1 github.com/rakyll/statik v0.1.6 - github.com/regen-network/cosmos-proto v0.0.0-20200103162838-2560887837f1 // indirect + github.com/regen-network/cosmos-proto v0.0.0-20200103162838-2560887837f1 github.com/spf13/afero v1.2.1 // indirect github.com/spf13/cobra v0.0.5 github.com/spf13/jwalterweatherman v1.1.0 // indirect diff --git a/types/pubkey.go b/types/pubkey.go index 5c3846bb8633..7fc1ee822ed6 100644 --- a/types/pubkey.go +++ b/types/pubkey.go @@ -156,3 +156,17 @@ func (m *PublicKey) Bytes() []byte { return bz } + +// PubKeySecp256k1ToPublicKey converts a Tendermint PubKeySecp256k1 to a reference +// of PublicKey. If the provided PubKey is not the correct type, nil will be +// returned. +func PubKeySecp256k1ToPublicKey(tmpk tmcrypto.PubKey) *PublicKey { + var pk *PublicKey + if v, ok := tmpk.(secp256k1.PubKeySecp256k1); ok { + pk = &PublicKey{ + &PublicKey_Secp256K1{Secp256K1: v[:]}, + } + } + + return pk +} diff --git a/types/pubkey_test.go b/types/pubkey_test.go index 952f2d26e2ba..7a4111790087 100644 --- a/types/pubkey_test.go +++ b/types/pubkey_test.go @@ -110,3 +110,14 @@ func TestAddress(t *testing.T) { require.Equal(t, tc.pubkey.Address(), tc.input.Address()) } } + +func TestPubKeySecp256k1ToPublicKey(t *testing.T) { + secp256k1pk := (secp256k1.GenPrivKey().PubKey()).(secp256k1.PubKeySecp256k1) + pk := types.PubKeySecp256k1ToPublicKey(secp256k1pk) + require.NotNil(t, pk) + require.Equal(t, secp256k1pk.Address(), pk.Address()) + + ed25519pk := (ed25519.GenPrivKey().PubKey()).(ed25519.PubKeyEd25519) + pk = types.PubKeySecp256k1ToPublicKey(ed25519pk) + require.Nil(t, pk) +} From 2218efdae9c43ced8363f17a8df1193896ae313a Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Sat, 4 Jan 2020 15:11:42 -0500 Subject: [PATCH 40/40] Add JSON serialization from interface utils --- types/utils.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/types/utils.go b/types/utils.go index 2a6add6b45b4..8adeeb230e4a 100644 --- a/types/utils.go +++ b/types/utils.go @@ -14,6 +14,26 @@ var ( DBBackend = "" ) +// MarshalJSONSort returns a sorted JSON serialization of a type. +func MarshalJSONSort(v interface{}) ([]byte, error) { + bz, err := json.Marshal(v) + if err != nil { + return nil, err + } + + return SortJSON(bz) +} + +// MustMarshalJSONSort calls MarshalJSONSort where it panics on error. +func MustMarshalJSONSort(v interface{}) []byte { + bz, err := MarshalJSONSort(v) + if err != nil { + panic(err) + } + + return bz +} + // SortedJSON takes any JSON and returns it sorted by keys. Also, all white-spaces // are removed. // This method can be used to canonicalize JSON to be returned by GetSignBytes,