From 9c8bcbab793fe91074590c29efc2469bd4854a80 Mon Sep 17 00:00:00 2001 From: Riccardo Montagnin Date: Mon, 19 Aug 2019 14:37:22 +0200 Subject: [PATCH 01/18] Added the conversion of the block in order to convert HEX validator addresses to the ValAddress type --- client/rpc/block.go | 10 ++- client/rpc/block_converter.go | 147 ++++++++++++++++++++++++++++++++++ go.mod | 2 +- go.sum | 10 +++ 4 files changed, 164 insertions(+), 5 deletions(-) create mode 100644 client/rpc/block_converter.go diff --git a/client/rpc/block.go b/client/rpc/block.go index 6e3ea231f409..c5b19599393f 100644 --- a/client/rpc/block.go +++ b/client/rpc/block.go @@ -2,12 +2,11 @@ package rpc import ( "fmt" - "net/http" - "strconv" - "github.com/gorilla/mux" "github.com/spf13/cobra" "github.com/spf13/viper" + "net/http" + "strconv" "github.com/cosmos/cosmos-sdk/client/context" "github.com/cosmos/cosmos-sdk/client/flags" @@ -68,7 +67,10 @@ func getBlock(cliCtx context.CLIContext, height *int64) ([]byte, error) { return codec.Cdc.MarshalJSONIndent(res, "", " ") } - return codec.Cdc.MarshalJSON(res) + // convert to new type + converted := ConvertBlockResult(res) + + return codec.Cdc.MarshalJSON(converted) } // get the current blockchain height diff --git a/client/rpc/block_converter.go b/client/rpc/block_converter.go new file mode 100644 index 000000000000..9f9c6963e597 --- /dev/null +++ b/client/rpc/block_converter.go @@ -0,0 +1,147 @@ +package rpc + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + cmn "github.com/tendermint/tendermint/libs/common" + ctypes "github.com/tendermint/tendermint/rpc/core/types" + "github.com/tendermint/tendermint/types" + "github.com/tendermint/tendermint/version" + "sync" + "time" +) + +type CosmosResultBlock struct { + BlockMeta CosmosBlockMeta `json:"block_meta"` + Block CosmosBlock `json:"block"` +} + +type CosmosBlockMeta struct { + BlockID types.BlockID `json:"block_id"` // the block hash and partsethash + Header CosmosHeader `json:"header"` // The block's Header +} + +type CosmosHeader struct { + // basic block info + Version version.Consensus `json:"version"` + ChainID string `json:"chain_id"` + Height int64 `json:"height"` + Time time.Time `json:"time"` + NumTxs int64 `json:"num_txs"` + TotalTxs int64 `json:"total_txs"` + + // prev block info + LastBlockID types.BlockID `json:"last_block_id"` + + // hashes of block data + LastCommitHash cmn.HexBytes `json:"last_commit_hash"` // commit from validators from the last block + DataHash cmn.HexBytes `json:"data_hash"` // transactions + + // hashes from the app output from the prev block + ValidatorsHash cmn.HexBytes `json:"validators_hash"` // validators for the current block + NextValidatorsHash cmn.HexBytes `json:"next_validators_hash"` // validators for the next block + ConsensusHash cmn.HexBytes `json:"consensus_hash"` // consensus params for current block + AppHash cmn.HexBytes `json:"app_hash"` // state after txs from the previous block + LastResultsHash cmn.HexBytes `json:"last_results_hash"` // root hash of all results from the txs from the previous block + + // consensus info + EvidenceHash cmn.HexBytes `json:"evidence_hash"` // evidence included in the block + ProposerAddress sdk.ValAddress `json:"proposer_address"` // original proposer of the block +} + +type CosmosBlock struct { + mtx sync.Mutex + CosmosHeader `json:"header"` + types.Data `json:"data"` + Evidence types.EvidenceData `json:"evidence"` + LastCommit CosmosCommit `json:"last_commit"` +} + +type CosmosCommit struct { + BlockID types.BlockID `json:"block_id"` + Precommits []CosmosCommitSig `json:"precommits"` +} + +type CosmosCommitSig struct { + Type types.SignedMsgType `json:"type"` + Height int64 `json:"height"` + Round int `json:"round"` + BlockID types.BlockID `json:"block_id"` // zero if vote is nil. + Timestamp time.Time `json:"timestamp"` + ValidatorAddress sdk.ValAddress `json:"validator_address"` + ValidatorIndex int `json:"validator_index"` + Signature []byte `json:"signature"` +} + +func ConvertBlockResult(res *ctypes.ResultBlock) (blockResult CosmosResultBlock) { + + // header + header := CosmosHeader{ + Version: res.BlockMeta.Header.Version, + ChainID: res.BlockMeta.Header.ChainID, + Height: res.BlockMeta.Header.Height, + Time: res.BlockMeta.Header.Time, + NumTxs: res.BlockMeta.Header.NumTxs, + TotalTxs: res.BlockMeta.Header.TotalTxs, + + LastBlockID: res.BlockMeta.Header.LastBlockID, + + LastCommitHash: res.BlockMeta.Header.LastCommitHash, + DataHash: res.BlockMeta.Header.DataHash, + + ValidatorsHash: res.BlockMeta.Header.ValidatorsHash, + NextValidatorsHash: res.BlockMeta.Header.NextValidatorsHash, + ConsensusHash: res.BlockMeta.Header.ConsensusHash, + AppHash: res.BlockMeta.Header.AppHash, + LastResultsHash: res.BlockMeta.Header.LastResultsHash, + + EvidenceHash: res.BlockMeta.Header.EvidenceHash, + ProposerAddress: sdk.ValAddress(res.BlockMeta.Header.ProposerAddress), + } + + // meta + blockMeta := CosmosBlockMeta{ + BlockID: res.BlockMeta.BlockID, + Header: header, + } + + // commit + commit := CosmosCommit{ + BlockID: res.Block.LastCommit.BlockID, + Precommits: convertPreCommits(res.Block.LastCommit.Precommits), + } + + // block + block := CosmosBlock{ + CosmosHeader: header, + Data: res.Block.Data, + Evidence: res.Block.Evidence, + LastCommit: commit, + } + + // blockResult + blockResult = CosmosResultBlock{ + BlockMeta: blockMeta, + Block: block, + } + + return blockResult +} + +func convertPreCommits(preCommits []*types.CommitSig) (sigs []CosmosCommitSig) { + for _, commit := range preCommits { + sig := CosmosCommitSig{ + Type: commit.Type, + Height: commit.Height, + Round: commit.Round, + BlockID: commit.BlockID, + Timestamp: commit.Timestamp, + ValidatorAddress: sdk.ValAddress(commit.ValidatorAddress), + ValidatorIndex: commit.ValidatorIndex, + Signature: commit.Signature, + } + + sigs = append(sigs, sig) + } + + return sigs +} diff --git a/go.mod b/go.mod index 12b4b7dee963..f989781f3bab 100644 --- a/go.mod +++ b/go.mod @@ -6,11 +6,11 @@ require ( github.com/btcsuite/btcd v0.0.0-20190115013929-ed77733ec07d github.com/cosmos/go-bip39 v0.0.0-20180618194314-52158e4697b8 github.com/cosmos/ledger-cosmos-go v0.10.3 + github.com/cosmos/tools/cmd/runsim v0.0.0-20190816161256-f506590651e9 // indirect github.com/fortytw2/leaktest v1.3.0 // indirect github.com/gogo/protobuf v1.2.1 github.com/golang/mock v1.3.1-0.20190508161146-9fa652df1129 github.com/gorilla/mux v1.7.0 - github.com/gorilla/websocket v1.4.0 // indirect github.com/mattn/go-isatty v0.0.6 github.com/pelletier/go-toml v1.2.0 github.com/pkg/errors v0.8.1 diff --git a/go.sum b/go.sum index e14d04bd3380..a26c5d8e750f 100644 --- a/go.sum +++ b/go.sum @@ -7,6 +7,8 @@ github.com/aead/siphash v1.0.1/go.mod h1:Nywa3cDsYNNK3gaciGTWPwHt0wlpNV15vwmswBA github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8= +github.com/aws/aws-sdk-go v1.20.20 h1:OAR/GtjMOhenkp1NNKr1N1FgIP3mQXHeGbRhvVIAQp0= +github.com/aws/aws-sdk-go v1.20.20/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo= github.com/bartekn/go-bip39 v0.0.0-20171116152956-a05967ea095d h1:1aAija9gr0Hyv4KfQcRcwlmFIrhkDmIj2dz5bkg/s/8= github.com/bartekn/go-bip39 v0.0.0-20171116152956-a05967ea095d/go.mod h1:icNx/6QdFblhsEjZehARqbNumymUT/ydwlLojFdv7Sk= github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973 h1:xJ4a3vCFaGF/jqvzLMYoU8P317H5OQ+Via4RmuPwCS0= @@ -33,6 +35,8 @@ github.com/cosmos/ledger-cosmos-go v0.10.3 h1:Qhi5yTR5Pg1CaTpd00pxlGwNl4sFRdtK1J github.com/cosmos/ledger-cosmos-go v0.10.3/go.mod h1:J8//BsAGTo3OC/vDLjMRFLW6q0WAaXvHnVc7ZmE8iUY= github.com/cosmos/ledger-go v0.9.2 h1:Nnao/dLwaVTk1Q5U9THldpUMMXU94BOTWPddSmVB6pI= github.com/cosmos/ledger-go v0.9.2/go.mod h1:oZJ2hHAZROdlHiwTg4t7kP+GKIIkBT+o6c9QWFanOyI= +github.com/cosmos/tools/cmd/runsim v0.0.0-20190816161256-f506590651e9 h1:67d6wqNDQ2maFezoNBStA+FdtPF12ZgdlDOE02cx0r0= +github.com/cosmos/tools/cmd/runsim v0.0.0-20190816161256-f506590651e9/go.mod h1:J/WXP5By/qBDT9MkxIOq6HLhukR8GW+KfPnY7NAXM4s= github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwcJI5acqYI6dE= github.com/davecgh/go-spew v0.0.0-20171005155431-ecdeabc65495/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -84,6 +88,8 @@ github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpO github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= github.com/jessevdk/go-flags v0.0.0-20141203071132-1679536dcc89/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= +github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af h1:pmfjZENx5imkbgOkpRUYLnmbU7UEFbjtDA2hxJ1ichM= +github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k= github.com/jmhodges/levigo v1.0.0 h1:q5EC36kV79HWeTBWsod3mG11EgStG3qArTKcvlksN1U= github.com/jmhodges/levigo v1.0.0/go.mod h1:Q6Qx+uH3RAqyK4rFQroq9RL7mdkABMcfhEI+nNuzMJQ= github.com/jrick/logrotate v1.0.0/go.mod h1:LNinyqDIJnpAur+b8yyulnQw/wDuN1+BYKlTRt3OuAQ= @@ -96,6 +102,8 @@ github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515 h1:T+h1c/A9Gawja4Y9mFVWj github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc= github.com/libp2p/go-buffer-pool v0.0.1 h1:9Rrn/H46cXjaA2HQ5Y8lyhOS1NhTkZ4yuEs2r3Eechg= github.com/libp2p/go-buffer-pool v0.0.1/go.mod h1:xtyIz9PMobb13WaxR6Zo1Pd1zXJKYg0a8KiIvDp3TzQ= +github.com/lusis/go-slackbot v0.0.0-20180109053408-401027ccfef5/go.mod h1:c2mYKRyMb1BPkO5St0c/ps62L4S0W2NAkaTXj9qEI+0= +github.com/lusis/slack-test v0.0.0-20190426140909-c40012f20018/go.mod h1:sFlOUpQL1YcjhFVXhg1CG8ZASEs/Mf1oVb6H75JL/zg= github.com/magiconair/properties v1.8.0 h1:LLgXmsheXeRoUOBOjtwPQCWIYqM/LU1ayDtDePerRcY= github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= github.com/mattn/go-isatty v0.0.6 h1:SrwhHcpV4nWrMGdNcC2kXpMfcBVYGDuTArqyhocJgvA= @@ -106,6 +114,8 @@ github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrk github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE= github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= +github.com/nlopes/slack v0.5.0 h1:NbIae8Kd0NpqaEI3iUrsuS0KbcEDhzhc939jLW5fNm0= +github.com/nlopes/slack v0.5.0/go.mod h1:jVI4BBK3lSktibKahxBF74txcK2vyvkza1z/+rRnVAM= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.7.0 h1:WSHQ+IS43OoUrWtD1/bbclrwK8TTH5hzp+umCiuxHgs= github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= From 604f494acc4abd6699afdf07b57691b2afc92b8a Mon Sep 17 00:00:00 2001 From: Riccardo Montagnin Date: Mon, 19 Aug 2019 22:46:05 +0200 Subject: [PATCH 02/18] Removed mutex --- client/rpc/block_converter.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/client/rpc/block_converter.go b/client/rpc/block_converter.go index 9f9c6963e597..a04def0ff60d 100644 --- a/client/rpc/block_converter.go +++ b/client/rpc/block_converter.go @@ -6,7 +6,6 @@ import ( ctypes "github.com/tendermint/tendermint/rpc/core/types" "github.com/tendermint/tendermint/types" "github.com/tendermint/tendermint/version" - "sync" "time" ) @@ -49,7 +48,6 @@ type CosmosHeader struct { } type CosmosBlock struct { - mtx sync.Mutex CosmosHeader `json:"header"` types.Data `json:"data"` Evidence types.EvidenceData `json:"evidence"` From 40604b4a82e3d2827b2e86313b47ee1423298c81 Mon Sep 17 00:00:00 2001 From: Riccardo Montagnin Date: Mon, 19 Aug 2019 22:46:38 +0200 Subject: [PATCH 03/18] Undo go mod change --- go.mod | 1 - go.sum | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index f989781f3bab..683471337d39 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,6 @@ require ( github.com/btcsuite/btcd v0.0.0-20190115013929-ed77733ec07d github.com/cosmos/go-bip39 v0.0.0-20180618194314-52158e4697b8 github.com/cosmos/ledger-cosmos-go v0.10.3 - github.com/cosmos/tools/cmd/runsim v0.0.0-20190816161256-f506590651e9 // indirect github.com/fortytw2/leaktest v1.3.0 // indirect github.com/gogo/protobuf v1.2.1 github.com/golang/mock v1.3.1-0.20190508161146-9fa652df1129 diff --git a/go.sum b/go.sum index a26c5d8e750f..038779ca3eec 100644 --- a/go.sum +++ b/go.sum @@ -78,6 +78,7 @@ 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/gorilla/mux v1.7.0 h1:tOSd0UKHQd6urX6ApfOn4XdBMY6Sh1MfxV3kmaazO+U= github.com/gorilla/mux v1.7.0/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs= +github.com/gorilla/websocket v1.2.0 h1:VJtLvh6VQym50czpZzx07z/kw9EgAxI3x1ZB8taTMQQ= github.com/gorilla/websocket v1.2.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= github.com/gorilla/websocket v1.4.0 h1:WDFjx/TMzVgy9VdMMQi2K2Emtwi2QcUQsztZ/zLaH/Q= github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= From 4439a72645ce42baa89cfbf984006adb674d49bf Mon Sep 17 00:00:00 2001 From: Riccardo Montagnin Date: Mon, 19 Aug 2019 22:46:55 +0200 Subject: [PATCH 04/18] Removed Cosmos* type prefix --- client/rpc/block_converter.go | 54 +++++++++++++++++------------------ 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/client/rpc/block_converter.go b/client/rpc/block_converter.go index a04def0ff60d..3cf552fb47e8 100644 --- a/client/rpc/block_converter.go +++ b/client/rpc/block_converter.go @@ -9,17 +9,17 @@ import ( "time" ) -type CosmosResultBlock struct { - BlockMeta CosmosBlockMeta `json:"block_meta"` - Block CosmosBlock `json:"block"` +type ResultBlock struct { + BlockMeta BlockMeta `json:"block_meta"` + Block Block `json:"block"` } -type CosmosBlockMeta struct { +type BlockMeta struct { BlockID types.BlockID `json:"block_id"` // the block hash and partsethash - Header CosmosHeader `json:"header"` // The block's Header + Header Header `json:"header"` // The block's Header } -type CosmosHeader struct { +type Header struct { // basic block info Version version.Consensus `json:"version"` ChainID string `json:"chain_id"` @@ -47,19 +47,19 @@ type CosmosHeader struct { ProposerAddress sdk.ValAddress `json:"proposer_address"` // original proposer of the block } -type CosmosBlock struct { - CosmosHeader `json:"header"` - types.Data `json:"data"` - Evidence types.EvidenceData `json:"evidence"` - LastCommit CosmosCommit `json:"last_commit"` +type Block struct { + Header `json:"header"` + types.Data `json:"data"` + Evidence types.EvidenceData `json:"evidence"` + LastCommit Commit `json:"last_commit"` } -type CosmosCommit struct { - BlockID types.BlockID `json:"block_id"` - Precommits []CosmosCommitSig `json:"precommits"` +type Commit struct { + BlockID types.BlockID `json:"block_id"` + Precommits []CommitSig `json:"precommits"` } -type CosmosCommitSig struct { +type CommitSig struct { Type types.SignedMsgType `json:"type"` Height int64 `json:"height"` Round int `json:"round"` @@ -70,10 +70,10 @@ type CosmosCommitSig struct { Signature []byte `json:"signature"` } -func ConvertBlockResult(res *ctypes.ResultBlock) (blockResult CosmosResultBlock) { +func ConvertBlockResult(res *ctypes.ResultBlock) (blockResult ResultBlock) { // header - header := CosmosHeader{ + header := Header{ Version: res.BlockMeta.Header.Version, ChainID: res.BlockMeta.Header.ChainID, Height: res.BlockMeta.Header.Height, @@ -97,27 +97,27 @@ func ConvertBlockResult(res *ctypes.ResultBlock) (blockResult CosmosResultBlock) } // meta - blockMeta := CosmosBlockMeta{ + blockMeta := BlockMeta{ BlockID: res.BlockMeta.BlockID, Header: header, } // commit - commit := CosmosCommit{ + commit := Commit{ BlockID: res.Block.LastCommit.BlockID, Precommits: convertPreCommits(res.Block.LastCommit.Precommits), } // block - block := CosmosBlock{ - CosmosHeader: header, - Data: res.Block.Data, - Evidence: res.Block.Evidence, - LastCommit: commit, + block := Block{ + Header: header, + Data: res.Block.Data, + Evidence: res.Block.Evidence, + LastCommit: commit, } // blockResult - blockResult = CosmosResultBlock{ + blockResult = ResultBlock{ BlockMeta: blockMeta, Block: block, } @@ -125,9 +125,9 @@ func ConvertBlockResult(res *ctypes.ResultBlock) (blockResult CosmosResultBlock) return blockResult } -func convertPreCommits(preCommits []*types.CommitSig) (sigs []CosmosCommitSig) { +func convertPreCommits(preCommits []*types.CommitSig) (sigs []CommitSig) { for _, commit := range preCommits { - sig := CosmosCommitSig{ + sig := CommitSig{ Type: commit.Type, Height: commit.Height, Round: commit.Round, From 499818d49ddd5e4091d6516a6743bcb95238c365 Mon Sep 17 00:00:00 2001 From: Riccardo Montagnin Date: Tue, 20 Aug 2019 09:47:18 +0200 Subject: [PATCH 05/18] Sorted imports using goimports instead of gofmt --- client/rpc/block.go | 5 +++-- client/rpc/block_converter.go | 3 ++- x/simulation/config.go | 2 +- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/client/rpc/block.go b/client/rpc/block.go index c5b19599393f..874f9c7383be 100644 --- a/client/rpc/block.go +++ b/client/rpc/block.go @@ -2,11 +2,12 @@ package rpc import ( "fmt" + "net/http" + "strconv" + "github.com/gorilla/mux" "github.com/spf13/cobra" "github.com/spf13/viper" - "net/http" - "strconv" "github.com/cosmos/cosmos-sdk/client/context" "github.com/cosmos/cosmos-sdk/client/flags" diff --git a/client/rpc/block_converter.go b/client/rpc/block_converter.go index 3cf552fb47e8..2ec682235687 100644 --- a/client/rpc/block_converter.go +++ b/client/rpc/block_converter.go @@ -1,12 +1,13 @@ package rpc import ( + "time" + sdk "github.com/cosmos/cosmos-sdk/types" cmn "github.com/tendermint/tendermint/libs/common" ctypes "github.com/tendermint/tendermint/rpc/core/types" "github.com/tendermint/tendermint/types" "github.com/tendermint/tendermint/version" - "time" ) type ResultBlock struct { diff --git a/x/simulation/config.go b/x/simulation/config.go index 616da3bcc491..b15a6f2f3d48 100644 --- a/x/simulation/config.go +++ b/x/simulation/config.go @@ -20,4 +20,4 @@ type Config struct { OnOperation bool // run slow invariants every operation AllInvariants bool // print all failed invariants if a broken invariant is found -} \ No newline at end of file +} From e29aba1b8708003051d3be0febf30f3b2e922145 Mon Sep 17 00:00:00 2001 From: Riccardo Montagnin Date: Wed, 21 Aug 2019 10:38:31 +0200 Subject: [PATCH 06/18] Applied suggestion to conversion call --- client/rpc/block.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/client/rpc/block.go b/client/rpc/block.go index 874f9c7383be..a637e6552786 100644 --- a/client/rpc/block.go +++ b/client/rpc/block.go @@ -68,10 +68,7 @@ func getBlock(cliCtx context.CLIContext, height *int64) ([]byte, error) { return codec.Cdc.MarshalJSONIndent(res, "", " ") } - // convert to new type - converted := ConvertBlockResult(res) - - return codec.Cdc.MarshalJSON(converted) + return codec.Cdc.MarshalJSON(ConvertBlockResult(res)) } // get the current blockchain height From 6934f17b273708df3bb2ba7a53a23ffa279a447c Mon Sep 17 00:00:00 2001 From: Riccardo Montagnin Date: Wed, 21 Aug 2019 10:39:58 +0200 Subject: [PATCH 07/18] Undo changed in go.sum --- go.sum | 1 - 1 file changed, 1 deletion(-) diff --git a/go.sum b/go.sum index 038779ca3eec..2f07756ad94e 100644 --- a/go.sum +++ b/go.sum @@ -7,7 +7,6 @@ github.com/aead/siphash v1.0.1/go.mod h1:Nywa3cDsYNNK3gaciGTWPwHt0wlpNV15vwmswBA github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8= -github.com/aws/aws-sdk-go v1.20.20 h1:OAR/GtjMOhenkp1NNKr1N1FgIP3mQXHeGbRhvVIAQp0= github.com/aws/aws-sdk-go v1.20.20/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo= github.com/bartekn/go-bip39 v0.0.0-20171116152956-a05967ea095d h1:1aAija9gr0Hyv4KfQcRcwlmFIrhkDmIj2dz5bkg/s/8= github.com/bartekn/go-bip39 v0.0.0-20171116152956-a05967ea095d/go.mod h1:icNx/6QdFblhsEjZehARqbNumymUT/ydwlLojFdv7Sk= From 93a85263ecf547b03871b5a082cb9fe886d679db Mon Sep 17 00:00:00 2001 From: Riccardo Montagnin Date: Wed, 21 Aug 2019 10:47:07 +0200 Subject: [PATCH 08/18] Used type wrappers instead of re defining the whole type while converting the responses --- client/rpc/block_converter.go | 91 +++++++++++------------------------ 1 file changed, 29 insertions(+), 62 deletions(-) diff --git a/client/rpc/block_converter.go b/client/rpc/block_converter.go index 2ec682235687..0ea72cd0dc7b 100644 --- a/client/rpc/block_converter.go +++ b/client/rpc/block_converter.go @@ -1,13 +1,11 @@ package rpc import ( - "time" + "encoding/json" sdk "github.com/cosmos/cosmos-sdk/types" - cmn "github.com/tendermint/tendermint/libs/common" ctypes "github.com/tendermint/tendermint/rpc/core/types" "github.com/tendermint/tendermint/types" - "github.com/tendermint/tendermint/version" ) type ResultBlock struct { @@ -20,32 +18,22 @@ type BlockMeta struct { Header Header `json:"header"` // The block's Header } +// Header defines a wrapper around Tendermint's Header type overriding various fields. type Header struct { - // basic block info - Version version.Consensus `json:"version"` - ChainID string `json:"chain_id"` - Height int64 `json:"height"` - Time time.Time `json:"time"` - NumTxs int64 `json:"num_txs"` - TotalTxs int64 `json:"total_txs"` - - // prev block info - LastBlockID types.BlockID `json:"last_block_id"` - - // hashes of block data - LastCommitHash cmn.HexBytes `json:"last_commit_hash"` // commit from validators from the last block - DataHash cmn.HexBytes `json:"data_hash"` // transactions - - // hashes from the app output from the prev block - ValidatorsHash cmn.HexBytes `json:"validators_hash"` // validators for the current block - NextValidatorsHash cmn.HexBytes `json:"next_validators_hash"` // validators for the next block - ConsensusHash cmn.HexBytes `json:"consensus_hash"` // consensus params for current block - AppHash cmn.HexBytes `json:"app_hash"` // state after txs from the previous block - LastResultsHash cmn.HexBytes `json:"last_results_hash"` // root hash of all results from the txs from the previous block - - // consensus info - EvidenceHash cmn.HexBytes `json:"evidence_hash"` // evidence included in the block - ProposerAddress sdk.ValAddress `json:"proposer_address"` // original proposer of the block + // embed original type + types.Header + + // override fields from original type + ProposerAddress sdk.ValAddress `json:"proposer_address"` +} + +// MarshalJSON implements the json.Marshaler interface. We do this because Amino +// does not respect the JSON stdlib embedding semantics. +func (h Header) MarshalJSON() ([]byte, error) { + type headerJSON Header + _h := headerJSON(h) + + return json.Marshal(_h) } type Block struct { @@ -61,39 +49,24 @@ type Commit struct { } type CommitSig struct { - Type types.SignedMsgType `json:"type"` - Height int64 `json:"height"` - Round int `json:"round"` - BlockID types.BlockID `json:"block_id"` // zero if vote is nil. - Timestamp time.Time `json:"timestamp"` - ValidatorAddress sdk.ValAddress `json:"validator_address"` - ValidatorIndex int `json:"validator_index"` - Signature []byte `json:"signature"` + types.CommitSig + ValidatorAddress sdk.ValAddress `json:"validator_address"` +} + +// MarshalJSON implements the json.Marshaler interface. We do this because Amino +// does not respect the JSON stdlib embedding semantics. +func (c CommitSig) MarshalJSON() ([]byte, error) { + type headerJSON CommitSig + _h := headerJSON(c) + + return json.Marshal(_h) } func ConvertBlockResult(res *ctypes.ResultBlock) (blockResult ResultBlock) { // header header := Header{ - Version: res.BlockMeta.Header.Version, - ChainID: res.BlockMeta.Header.ChainID, - Height: res.BlockMeta.Header.Height, - Time: res.BlockMeta.Header.Time, - NumTxs: res.BlockMeta.Header.NumTxs, - TotalTxs: res.BlockMeta.Header.TotalTxs, - - LastBlockID: res.BlockMeta.Header.LastBlockID, - - LastCommitHash: res.BlockMeta.Header.LastCommitHash, - DataHash: res.BlockMeta.Header.DataHash, - - ValidatorsHash: res.BlockMeta.Header.ValidatorsHash, - NextValidatorsHash: res.BlockMeta.Header.NextValidatorsHash, - ConsensusHash: res.BlockMeta.Header.ConsensusHash, - AppHash: res.BlockMeta.Header.AppHash, - LastResultsHash: res.BlockMeta.Header.LastResultsHash, - - EvidenceHash: res.BlockMeta.Header.EvidenceHash, + Header: res.BlockMeta.Header, ProposerAddress: sdk.ValAddress(res.BlockMeta.Header.ProposerAddress), } @@ -129,14 +102,8 @@ func ConvertBlockResult(res *ctypes.ResultBlock) (blockResult ResultBlock) { func convertPreCommits(preCommits []*types.CommitSig) (sigs []CommitSig) { for _, commit := range preCommits { sig := CommitSig{ - Type: commit.Type, - Height: commit.Height, - Round: commit.Round, - BlockID: commit.BlockID, - Timestamp: commit.Timestamp, + CommitSig: *commit, ValidatorAddress: sdk.ValAddress(commit.ValidatorAddress), - ValidatorIndex: commit.ValidatorIndex, - Signature: commit.Signature, } sigs = append(sigs, sig) From faa3f2c8660824c84b15b74258d767716e2b9940 Mon Sep 17 00:00:00 2001 From: Riccardo Montagnin Date: Wed, 21 Aug 2019 18:23:16 +0200 Subject: [PATCH 09/18] Added // nolint: structtag --- client/rpc/block_converter.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/client/rpc/block_converter.go b/client/rpc/block_converter.go index 0ea72cd0dc7b..4393b5232776 100644 --- a/client/rpc/block_converter.go +++ b/client/rpc/block_converter.go @@ -19,6 +19,7 @@ type BlockMeta struct { } // Header defines a wrapper around Tendermint's Header type overriding various fields. +// nolint: structtag type Header struct { // embed original type types.Header @@ -48,6 +49,7 @@ type Commit struct { Precommits []CommitSig `json:"precommits"` } +// nolint: structtag type CommitSig struct { types.CommitSig ValidatorAddress sdk.ValAddress `json:"validator_address"` From 69fd89cbcbff4539f78bd374b0ed345cc5faf296 Mon Sep 17 00:00:00 2001 From: Riccardo Montagnin Date: Mon, 26 Aug 2019 07:52:36 +0200 Subject: [PATCH 10/18] Removed useless comments and variables while converting a block --- client/rpc/block_converter.go | 41 ++++++++++++----------------------- 1 file changed, 14 insertions(+), 27 deletions(-) diff --git a/client/rpc/block_converter.go b/client/rpc/block_converter.go index 4393b5232776..a78adfc1f8fd 100644 --- a/client/rpc/block_converter.go +++ b/client/rpc/block_converter.go @@ -66,39 +66,26 @@ func (c CommitSig) MarshalJSON() ([]byte, error) { func ConvertBlockResult(res *ctypes.ResultBlock) (blockResult ResultBlock) { - // header header := Header{ Header: res.BlockMeta.Header, ProposerAddress: sdk.ValAddress(res.BlockMeta.Header.ProposerAddress), } - // meta - blockMeta := BlockMeta{ - BlockID: res.BlockMeta.BlockID, - Header: header, + return ResultBlock{ + BlockMeta: BlockMeta{ + BlockID: res.BlockMeta.BlockID, + Header: header, + }, + Block: Block{ + Header: header, + Data: res.Block.Data, + Evidence: res.Block.Evidence, + LastCommit: Commit{ + BlockID: res.Block.LastCommit.BlockID, + Precommits: convertPreCommits(res.Block.LastCommit.Precommits), + }, + }, } - - // commit - commit := Commit{ - BlockID: res.Block.LastCommit.BlockID, - Precommits: convertPreCommits(res.Block.LastCommit.Precommits), - } - - // block - block := Block{ - Header: header, - Data: res.Block.Data, - Evidence: res.Block.Evidence, - LastCommit: commit, - } - - // blockResult - blockResult = ResultBlock{ - BlockMeta: blockMeta, - Block: block, - } - - return blockResult } func convertPreCommits(preCommits []*types.CommitSig) (sigs []CommitSig) { From 909faa0dd4a89396adcc47524b02ada2a447f380 Mon Sep 17 00:00:00 2001 From: Riccardo Montagnin Date: Mon, 26 Aug 2019 08:02:10 +0200 Subject: [PATCH 11/18] Unified how different types are treated (encapsulation) and added missing comments --- client/rpc/block_converter.go | 77 ++++++++++++++++++++++++++++------- 1 file changed, 62 insertions(+), 15 deletions(-) diff --git a/client/rpc/block_converter.go b/client/rpc/block_converter.go index a78adfc1f8fd..baf9a7f83382 100644 --- a/client/rpc/block_converter.go +++ b/client/rpc/block_converter.go @@ -8,14 +8,37 @@ import ( "github.com/tendermint/tendermint/types" ) +// ResultBlock defines a wrapper around the standard ResultBlock type overriding various fields. +// nolint: structtag type ResultBlock struct { + ctypes.ResultBlock BlockMeta BlockMeta `json:"block_meta"` Block Block `json:"block"` } +// MarshalJSON implements the json.Marshaler interface. We do this because Amino +// does not respect the JSON stdlib embedding semantics. +func (r ResultBlock) MarshalJSON() ([]byte, error) { + type resultBlockJSON ResultBlock + _h := resultBlockJSON(r) + + return json.Marshal(_h) +} + +// BlockMeta defines a wrapper around Tendermint's BlockMeta type overriding various fields. +// nolint: structtag type BlockMeta struct { - BlockID types.BlockID `json:"block_id"` // the block hash and partsethash - Header Header `json:"header"` // The block's Header + types.BlockMeta + Header Header `json:"header"` // The block's Header +} + +// MarshalJSON implements the json.Marshaler interface. We do this because Amino +// does not respect the JSON stdlib embedding semantics. +func (b BlockMeta) MarshalJSON() ([]byte, error) { + type blockMetaJSON BlockMeta + _h := blockMetaJSON(b) + + return json.Marshal(_h) } // Header defines a wrapper around Tendermint's Header type overriding various fields. @@ -37,18 +60,40 @@ func (h Header) MarshalJSON() ([]byte, error) { return json.Marshal(_h) } +// Block defines a wrapper around Tendermint's Block type overriding various fields. +// nolint: structtag type Block struct { + types.Block Header `json:"header"` - types.Data `json:"data"` - Evidence types.EvidenceData `json:"evidence"` - LastCommit Commit `json:"last_commit"` + LastCommit Commit `json:"last_commit"` +} + +// MarshalJSON implements the json.Marshaler interface. We do this because Amino +// does not respect the JSON stdlib embedding semantics. +func (b Block) MarshalJSON() ([]byte, error) { + type blockJSON Block + _h := blockJSON(b) + + return json.Marshal(_h) } +// Commit defines a wrapper around Tendermint's Commit type overriding various fields. +// nolint: structtag type Commit struct { - BlockID types.BlockID `json:"block_id"` - Precommits []CommitSig `json:"precommits"` + types.Commit + Precommits []CommitSig `json:"precommits"` +} + +// MarshalJSON implements the json.Marshaler interface. We do this because Amino +// does not respect the JSON stdlib embedding semantics. +func (c Commit) MarshalJSON() ([]byte, error) { + type commitJSON Commit + _h := commitJSON(c) + + return json.Marshal(_h) } +// CommitSig defines a wrapper around Tendermint's CommitSig type overriding various fields. // nolint: structtag type CommitSig struct { types.CommitSig @@ -58,12 +103,14 @@ type CommitSig struct { // MarshalJSON implements the json.Marshaler interface. We do this because Amino // does not respect the JSON stdlib embedding semantics. func (c CommitSig) MarshalJSON() ([]byte, error) { - type headerJSON CommitSig - _h := headerJSON(c) + type commitSigJSON CommitSig + _h := commitSigJSON(c) return json.Marshal(_h) } +// ConvertBlockResult allows to convert the given standard ResultBlock into a new ResultBlock having all the +// validator addresses as Bech32 strings instead of HEX ones. func ConvertBlockResult(res *ctypes.ResultBlock) (blockResult ResultBlock) { header := Header{ @@ -72,16 +119,16 @@ func ConvertBlockResult(res *ctypes.ResultBlock) (blockResult ResultBlock) { } return ResultBlock{ + ResultBlock: *res, BlockMeta: BlockMeta{ - BlockID: res.BlockMeta.BlockID, - Header: header, + BlockMeta: *res.BlockMeta, + Header: header, }, Block: Block{ - Header: header, - Data: res.Block.Data, - Evidence: res.Block.Evidence, + Block: *res.Block, + Header: header, LastCommit: Commit{ - BlockID: res.Block.LastCommit.BlockID, + Commit: *res.Block.LastCommit, Precommits: convertPreCommits(res.Block.LastCommit.Precommits), }, }, From 70d5cb7ebfdb61ac764655183a76eb86af133b9e Mon Sep 17 00:00:00 2001 From: Riccardo Montagnin Date: Mon, 26 Aug 2019 08:09:25 +0200 Subject: [PATCH 12/18] Reverted back encapsulation --- client/rpc/block_converter.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/client/rpc/block_converter.go b/client/rpc/block_converter.go index a78adfc1f8fd..93ab0a5882ea 100644 --- a/client/rpc/block_converter.go +++ b/client/rpc/block_converter.go @@ -8,11 +8,13 @@ import ( "github.com/tendermint/tendermint/types" ) +// Single block (with meta) type ResultBlock struct { BlockMeta BlockMeta `json:"block_meta"` Block Block `json:"block"` } +// BlockMeta contains meta information about a block - namely, it's ID and Header. type BlockMeta struct { BlockID types.BlockID `json:"block_id"` // the block hash and partsethash Header Header `json:"header"` // The block's Header @@ -37,6 +39,7 @@ func (h Header) MarshalJSON() ([]byte, error) { return json.Marshal(_h) } +// Block defines the atomic unit of a Tendermint blockchain. type Block struct { Header `json:"header"` types.Data `json:"data"` @@ -44,11 +47,14 @@ type Block struct { LastCommit Commit `json:"last_commit"` } +// Commit contains the evidence that a block was committed by a set of validators. +// NOTE: Commit is empty for height 1, but never nil. type Commit struct { BlockID types.BlockID `json:"block_id"` Precommits []CommitSig `json:"precommits"` } +// CommitSig defines a wrapper around Tendermint's CommitSig type overriding various fields. // nolint: structtag type CommitSig struct { types.CommitSig @@ -64,6 +70,8 @@ func (c CommitSig) MarshalJSON() ([]byte, error) { return json.Marshal(_h) } +// ConvertBlockResult allows to convert the given standard ResultBlock into a new ResultBlock having all the +// validator addresses as Bech32 strings instead of HEX ones. func ConvertBlockResult(res *ctypes.ResultBlock) (blockResult ResultBlock) { header := Header{ From 8910ec2ceb7b27c9271cc63629f2bc2000521aef Mon Sep 17 00:00:00 2001 From: Riccardo Montagnin Date: Mon, 26 Aug 2019 09:48:22 +0200 Subject: [PATCH 13/18] Update client/rpc/block_converter.go Co-Authored-By: Federico Kunze <31522760+fedekunze@users.noreply.github.com> --- client/rpc/block_converter.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/rpc/block_converter.go b/client/rpc/block_converter.go index 93ab0a5882ea..a4910febdb73 100644 --- a/client/rpc/block_converter.go +++ b/client/rpc/block_converter.go @@ -8,7 +8,7 @@ import ( "github.com/tendermint/tendermint/types" ) -// Single block (with meta) +// ResultBlock represents a single block with metadata type ResultBlock struct { BlockMeta BlockMeta `json:"block_meta"` Block Block `json:"block"` From 59aff26c00efe5ed3ae3c3ce7ca811b871fb932b Mon Sep 17 00:00:00 2001 From: Riccardo Montagnin Date: Wed, 16 Oct 2019 11:59:52 +0200 Subject: [PATCH 14/18] Added tests --- client/rpc/block_converter_test.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 client/rpc/block_converter_test.go diff --git a/client/rpc/block_converter_test.go b/client/rpc/block_converter_test.go new file mode 100644 index 000000000000..00dee53c7325 --- /dev/null +++ b/client/rpc/block_converter_test.go @@ -0,0 +1,24 @@ +package rpc + +import ( + "testing" + + "github.com/cosmos/cosmos-sdk/codec" + "github.com/stretchr/testify/assert" + ctypes "github.com/tendermint/tendermint/rpc/core/types" +) + +var defaultBlock = `{"block_meta":{"block_id":{"hash":"30CD3A9EF2082FF9F2575655E99E37ACC3936DC9E534ADF0BD7436C76258225C","parts":{"total":"1","hash":"463460DA73FA0AE441B6041BF2683AE625CE2D9FD290F4C86CD83D1A7FB9F439"}},"header":{"version":{"block":"10","app":"0"},"chain_id":"test-chain-RoCfX4","height":"16","time":"2019-10-16T09:47:21.054275229Z","num_txs":"0","total_txs":"0","last_block_id":{"hash":"168D140232DA3A59757658E257168B6BCE62024F574CA222C2D449BAB1AE4023","parts":{"total":"1","hash":"4E6D7A1C7DE6942470394C21B132A2D9F89B31908C3B6FA8C2AB96A87553B96E"}},"last_commit_hash":"3483509FCC8048496E9B85E1E4C90A3B6E5944F022FE3C0709096932A010F712","data_hash":"","validators_hash":"61E69204249E5EE6F777C0566040929276A1900CE665BCB11006CABF9A4ED9A3","next_validators_hash":"61E69204249E5EE6F777C0566040929276A1900CE665BCB11006CABF9A4ED9A3","consensus_hash":"048091BC7DDC283F77BFBF91D73C44DA58C3DF8A9CBC867405D8B7F3DAADA22F","app_hash":"070A8213F67F494F7D27A6296C29F0DF62DFA18FD37FB592D9761A44DEE96528","last_results_hash":"","evidence_hash":"","proposer_address":"E8B4AF895B301C3D7108CA93A0B9234E3A2A4B21"}},"block":{"header":{"version":{"block":"10","app":"0"},"chain_id":"test-chain-RoCfX4","height":"16","time":"2019-10-16T09:47:21.054275229Z","num_txs":"0","total_txs":"0","last_block_id":{"hash":"168D140232DA3A59757658E257168B6BCE62024F574CA222C2D449BAB1AE4023","parts":{"total":"1","hash":"4E6D7A1C7DE6942470394C21B132A2D9F89B31908C3B6FA8C2AB96A87553B96E"}},"last_commit_hash":"3483509FCC8048496E9B85E1E4C90A3B6E5944F022FE3C0709096932A010F712","data_hash":"","validators_hash":"61E69204249E5EE6F777C0566040929276A1900CE665BCB11006CABF9A4ED9A3","next_validators_hash":"61E69204249E5EE6F777C0566040929276A1900CE665BCB11006CABF9A4ED9A3","consensus_hash":"048091BC7DDC283F77BFBF91D73C44DA58C3DF8A9CBC867405D8B7F3DAADA22F","app_hash":"070A8213F67F494F7D27A6296C29F0DF62DFA18FD37FB592D9761A44DEE96528","last_results_hash":"","evidence_hash":"","proposer_address":"E8B4AF895B301C3D7108CA93A0B9234E3A2A4B21"},"data":{"txs":null},"evidence":{"evidence":null},"last_commit":{"block_id":{"hash":"168D140232DA3A59757658E257168B6BCE62024F574CA222C2D449BAB1AE4023","parts":{"total":"1","hash":"4E6D7A1C7DE6942470394C21B132A2D9F89B31908C3B6FA8C2AB96A87553B96E"}},"precommits":[{"type":2,"height":"15","round":"0","block_id":{"hash":"168D140232DA3A59757658E257168B6BCE62024F574CA222C2D449BAB1AE4023","parts":{"total":"1","hash":"4E6D7A1C7DE6942470394C21B132A2D9F89B31908C3B6FA8C2AB96A87553B96E"}},"timestamp":"2019-10-16T09:47:21.054275229Z","validator_address":"E8B4AF895B301C3D7108CA93A0B9234E3A2A4B21","validator_index":"0","signature":"eXB1NPmarUC5+2SCUQiWUCoGqtq0nrSn7giMAF/zWbfv3KmueE+1NoZ0CPLJAhBodz8Y8oL8xVy6ElA7J72kBw=="}]}}}` + +func TestConvertBlockResult(t *testing.T) { + cdc := codec.New() + + var block ctypes.ResultBlock + cdc.MustUnmarshalJSON([]byte(defaultBlock), &block) + + convertedBlock := ConvertBlockResult(&block) + //assert.Equal(t, "cosmosvaloper1hafptm4zxy5nw8rd2pxyg83c5ls2v62t4lkfqe", convertedBlock.BlockMeta.Header.ProposerAddress.String()) + + convertedBlockString := cdc.MustMarshalBinaryBare(&convertedBlock) + assert.NotContains(t, "E8B4AF895B301C3D7108CA93A0B9234E3A2A4B21", convertedBlockString) +} From b5207747f3cf92979da2d88e0abd9bf0c59e6499 Mon Sep 17 00:00:00 2001 From: Riccardo Montagnin Date: Thu, 17 Oct 2019 09:41:44 +0200 Subject: [PATCH 15/18] Fixed the missing of some fields --- client/rpc/block_converter.go | 145 +++++++++++++++++++++++++---- client/rpc/block_converter_test.go | 8 +- 2 files changed, 133 insertions(+), 20 deletions(-) diff --git a/client/rpc/block_converter.go b/client/rpc/block_converter.go index a4910febdb73..68907337fc14 100644 --- a/client/rpc/block_converter.go +++ b/client/rpc/block_converter.go @@ -6,6 +6,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" ctypes "github.com/tendermint/tendermint/rpc/core/types" "github.com/tendermint/tendermint/types" + "github.com/tendermint/tendermint/version" ) // ResultBlock represents a single block with metadata @@ -27,6 +28,11 @@ type Header struct { types.Header // override fields from original type + Version Consensus `json:"version"` + Height int64 `json:"height,string"` // Override int64 fields do to json.Marshal not converting them to string + NumTxs int64 `json:"num_txs,string"` // Override int64 fields do to json.Marshal not converting them to string + TotalTxs int64 `json:"total_txs,string"` // Override int64 fields do to json.Marshal not converting them to string + LastBlockID BlockID `json:"last_block_id"` ProposerAddress sdk.ValAddress `json:"proposer_address"` } @@ -35,37 +41,112 @@ type Header struct { func (h Header) MarshalJSON() ([]byte, error) { type headerJSON Header _h := headerJSON(h) + return json.Marshal(_h) +} + +// BlockID defines a wrapper around Tendermint's BlockID type overriding various fields. +// nolint: structtag +type BlockID struct { + // embed original type + types.BlockID + + // override fields from original type + PartsHeader PartSetHeader `json:"parts"` +} + +// MarshalJSON implements the json.Marshaler interface. We do this because Amino +// does not respect the JSON stdlib embedding semantics. +func (b BlockID) MarshalJSON() ([]byte, error) { + type blockIDJson BlockID + _h := blockIDJson(b) + return json.Marshal(_h) +} + +// PartSetHeader defines a wrapper around Tendermint's PartSetHeader type overriding various fields. +// nolint: structtag +type PartSetHeader struct { + // embed original type + types.PartSetHeader + + // override fields from original type + Total int `json:"total,string"` +} + +// MarshalJSON implements the json.Marshaler interface. We do this because Amino +// does not respect the JSON stdlib embedding semantics. +func (b PartSetHeader) MarshalJSON() ([]byte, error) { + type partSetHeadJson PartSetHeader + _h := partSetHeadJson(b) + return json.Marshal(_h) +} +// Consensus defines a wrapper around Tendermint's Consensus type overriding various fields. +// nolint: structtag +type Consensus struct { + // embed original type + version.Consensus + + // override fields from original type + App uint64 `json:"app,string"` // Override uint64 fields do to json.Marshal not converting them to string + Block uint64 `json:"block,string"` // Override uint64 fields do to json.Marshal not converting them to string +} + +// MarshalJSON implements the json.Marshaler interface. We do this because Amino +// does not respect the JSON stdlib embedding semantics. +func (b Consensus) MarshalJSON() ([]byte, error) { + type consensusJson Consensus + _h := consensusJson(b) return json.Marshal(_h) } // Block defines the atomic unit of a Tendermint blockchain. type Block struct { - Header `json:"header"` - types.Data `json:"data"` - Evidence types.EvidenceData `json:"evidence"` - LastCommit Commit `json:"last_commit"` + // embed original type + types.Block + + // override fields + Header Header `json:"header"` + LastCommit Commit `json:"last_commit"` +} + +// MarshalJSON implements the json.Marshaler interface. We do this because Amino +// does not respect the JSON stdlib embedding semantics. +func (b Block) MarshalJSON() ([]byte, error) { + type blockJSON Block + _b := blockJSON(b) + return json.Marshal(_b) } -// Commit contains the evidence that a block was committed by a set of validators. -// NOTE: Commit is empty for height 1, but never nil. +// Commit defines a wrapper around Tendermint's Commit type overriding various fields. +// nolint: structtag. type Commit struct { - BlockID types.BlockID `json:"block_id"` - Precommits []CommitSig `json:"precommits"` + // embed original type + *types.Commit + + // override fields + BlockID BlockID `json:"block_id"` + Precommits []CommitSig `json:"precommits"` } // CommitSig defines a wrapper around Tendermint's CommitSig type overriding various fields. // nolint: structtag type CommitSig struct { + // embed original type types.CommitSig + + // override fields + Height int64 `json:"height,string"` // Override int64 fields do to json.Marshal not converting them to string + Round int `json:"round,string"` // Override int fields do to json.Marshal not converting them to string + BlockID BlockID `json:"block_id"` ValidatorAddress sdk.ValAddress `json:"validator_address"` + ValidatorIndex int `json:"validator_index,string"` // Override int fields do to json.Marshal not converting them to string } // MarshalJSON implements the json.Marshaler interface. We do this because Amino // does not respect the JSON stdlib embedding semantics. func (c CommitSig) MarshalJSON() ([]byte, error) { - type headerJSON CommitSig - _h := headerJSON(c) + type commitSigJson CommitSig + _h := commitSigJson(c) return json.Marshal(_h) } @@ -75,7 +156,24 @@ func (c CommitSig) MarshalJSON() ([]byte, error) { func ConvertBlockResult(res *ctypes.ResultBlock) (blockResult ResultBlock) { header := Header{ - Header: res.BlockMeta.Header, + Header: res.BlockMeta.Header, + + Version: Consensus{ + Consensus: res.BlockMeta.Header.Version, + App: res.BlockMeta.Header.Version.App.Uint64(), + Block: res.BlockMeta.Header.Version.Block.Uint64(), + }, + Height: res.BlockMeta.Header.Height, + NumTxs: res.BlockMeta.Header.NumTxs, + TotalTxs: res.BlockMeta.Header.TotalTxs, + LastBlockID: BlockID{ + BlockID: res.BlockMeta.Header.LastBlockID, + PartsHeader: PartSetHeader{ + PartSetHeader: res.BlockMeta.Header.LastBlockID.PartsHeader, + Total: res.BlockMeta.Header.LastBlockID.PartsHeader.Total, + }, + }, + ProposerAddress: sdk.ValAddress(res.BlockMeta.Header.ProposerAddress), } @@ -85,11 +183,16 @@ func ConvertBlockResult(res *ctypes.ResultBlock) (blockResult ResultBlock) { Header: header, }, Block: Block{ - Header: header, - Data: res.Block.Data, - Evidence: res.Block.Evidence, + Header: header, LastCommit: Commit{ - BlockID: res.Block.LastCommit.BlockID, + Commit: res.Block.LastCommit, + BlockID: BlockID{ + BlockID: res.Block.LastCommit.BlockID, + PartsHeader: PartSetHeader{ + PartSetHeader: res.Block.LastCommit.BlockID.PartsHeader, + Total: res.Block.LastCommit.BlockID.PartsHeader.Total, + }, + }, Precommits: convertPreCommits(res.Block.LastCommit.Precommits), }, }, @@ -99,8 +202,18 @@ func ConvertBlockResult(res *ctypes.ResultBlock) (blockResult ResultBlock) { func convertPreCommits(preCommits []*types.CommitSig) (sigs []CommitSig) { for _, commit := range preCommits { sig := CommitSig{ - CommitSig: *commit, + CommitSig: *commit, + Height: commit.Height, + Round: commit.Round, + BlockID: BlockID{ + BlockID: commit.BlockID, + PartsHeader: PartSetHeader{ + PartSetHeader: commit.BlockID.PartsHeader, + Total: commit.BlockID.PartsHeader.Total, + }, + }, ValidatorAddress: sdk.ValAddress(commit.ValidatorAddress), + ValidatorIndex: commit.ValidatorIndex, } sigs = append(sigs, sig) diff --git a/client/rpc/block_converter_test.go b/client/rpc/block_converter_test.go index 00dee53c7325..b625d0267e8b 100644 --- a/client/rpc/block_converter_test.go +++ b/client/rpc/block_converter_test.go @@ -8,17 +8,17 @@ import ( ctypes "github.com/tendermint/tendermint/rpc/core/types" ) -var defaultBlock = `{"block_meta":{"block_id":{"hash":"30CD3A9EF2082FF9F2575655E99E37ACC3936DC9E534ADF0BD7436C76258225C","parts":{"total":"1","hash":"463460DA73FA0AE441B6041BF2683AE625CE2D9FD290F4C86CD83D1A7FB9F439"}},"header":{"version":{"block":"10","app":"0"},"chain_id":"test-chain-RoCfX4","height":"16","time":"2019-10-16T09:47:21.054275229Z","num_txs":"0","total_txs":"0","last_block_id":{"hash":"168D140232DA3A59757658E257168B6BCE62024F574CA222C2D449BAB1AE4023","parts":{"total":"1","hash":"4E6D7A1C7DE6942470394C21B132A2D9F89B31908C3B6FA8C2AB96A87553B96E"}},"last_commit_hash":"3483509FCC8048496E9B85E1E4C90A3B6E5944F022FE3C0709096932A010F712","data_hash":"","validators_hash":"61E69204249E5EE6F777C0566040929276A1900CE665BCB11006CABF9A4ED9A3","next_validators_hash":"61E69204249E5EE6F777C0566040929276A1900CE665BCB11006CABF9A4ED9A3","consensus_hash":"048091BC7DDC283F77BFBF91D73C44DA58C3DF8A9CBC867405D8B7F3DAADA22F","app_hash":"070A8213F67F494F7D27A6296C29F0DF62DFA18FD37FB592D9761A44DEE96528","last_results_hash":"","evidence_hash":"","proposer_address":"E8B4AF895B301C3D7108CA93A0B9234E3A2A4B21"}},"block":{"header":{"version":{"block":"10","app":"0"},"chain_id":"test-chain-RoCfX4","height":"16","time":"2019-10-16T09:47:21.054275229Z","num_txs":"0","total_txs":"0","last_block_id":{"hash":"168D140232DA3A59757658E257168B6BCE62024F574CA222C2D449BAB1AE4023","parts":{"total":"1","hash":"4E6D7A1C7DE6942470394C21B132A2D9F89B31908C3B6FA8C2AB96A87553B96E"}},"last_commit_hash":"3483509FCC8048496E9B85E1E4C90A3B6E5944F022FE3C0709096932A010F712","data_hash":"","validators_hash":"61E69204249E5EE6F777C0566040929276A1900CE665BCB11006CABF9A4ED9A3","next_validators_hash":"61E69204249E5EE6F777C0566040929276A1900CE665BCB11006CABF9A4ED9A3","consensus_hash":"048091BC7DDC283F77BFBF91D73C44DA58C3DF8A9CBC867405D8B7F3DAADA22F","app_hash":"070A8213F67F494F7D27A6296C29F0DF62DFA18FD37FB592D9761A44DEE96528","last_results_hash":"","evidence_hash":"","proposer_address":"E8B4AF895B301C3D7108CA93A0B9234E3A2A4B21"},"data":{"txs":null},"evidence":{"evidence":null},"last_commit":{"block_id":{"hash":"168D140232DA3A59757658E257168B6BCE62024F574CA222C2D449BAB1AE4023","parts":{"total":"1","hash":"4E6D7A1C7DE6942470394C21B132A2D9F89B31908C3B6FA8C2AB96A87553B96E"}},"precommits":[{"type":2,"height":"15","round":"0","block_id":{"hash":"168D140232DA3A59757658E257168B6BCE62024F574CA222C2D449BAB1AE4023","parts":{"total":"1","hash":"4E6D7A1C7DE6942470394C21B132A2D9F89B31908C3B6FA8C2AB96A87553B96E"}},"timestamp":"2019-10-16T09:47:21.054275229Z","validator_address":"E8B4AF895B301C3D7108CA93A0B9234E3A2A4B21","validator_index":"0","signature":"eXB1NPmarUC5+2SCUQiWUCoGqtq0nrSn7giMAF/zWbfv3KmueE+1NoZ0CPLJAhBodz8Y8oL8xVy6ElA7J72kBw=="}]}}}` +var defaultBlockJson = `{"block_meta":{"block_id":{"hash":"30CD3A9EF2082FF9F2575655E99E37ACC3936DC9E534ADF0BD7436C76258225C","parts":{"total":"1","hash":"463460DA73FA0AE441B6041BF2683AE625CE2D9FD290F4C86CD83D1A7FB9F439"}},"header":{"version":{"block":"10","app":"0"},"chain_id":"test-chain-RoCfX4","height":"16","time":"2019-10-16T09:47:21.054275229Z","num_txs":"0","total_txs":"0","last_block_id":{"hash":"168D140232DA3A59757658E257168B6BCE62024F574CA222C2D449BAB1AE4023","parts":{"total":"1","hash":"4E6D7A1C7DE6942470394C21B132A2D9F89B31908C3B6FA8C2AB96A87553B96E"}},"last_commit_hash":"3483509FCC8048496E9B85E1E4C90A3B6E5944F022FE3C0709096932A010F712","data_hash":"","validators_hash":"61E69204249E5EE6F777C0566040929276A1900CE665BCB11006CABF9A4ED9A3","next_validators_hash":"61E69204249E5EE6F777C0566040929276A1900CE665BCB11006CABF9A4ED9A3","consensus_hash":"048091BC7DDC283F77BFBF91D73C44DA58C3DF8A9CBC867405D8B7F3DAADA22F","app_hash":"070A8213F67F494F7D27A6296C29F0DF62DFA18FD37FB592D9761A44DEE96528","last_results_hash":"","evidence_hash":"","proposer_address":"E8B4AF895B301C3D7108CA93A0B9234E3A2A4B21"}},"block":{"header":{"version":{"block":"10","app":"0"},"chain_id":"test-chain-RoCfX4","height":"16","time":"2019-10-16T09:47:21.054275229Z","num_txs":"0","total_txs":"0","last_block_id":{"hash":"168D140232DA3A59757658E257168B6BCE62024F574CA222C2D449BAB1AE4023","parts":{"total":"1","hash":"4E6D7A1C7DE6942470394C21B132A2D9F89B31908C3B6FA8C2AB96A87553B96E"}},"last_commit_hash":"3483509FCC8048496E9B85E1E4C90A3B6E5944F022FE3C0709096932A010F712","data_hash":"","validators_hash":"61E69204249E5EE6F777C0566040929276A1900CE665BCB11006CABF9A4ED9A3","next_validators_hash":"61E69204249E5EE6F777C0566040929276A1900CE665BCB11006CABF9A4ED9A3","consensus_hash":"048091BC7DDC283F77BFBF91D73C44DA58C3DF8A9CBC867405D8B7F3DAADA22F","app_hash":"070A8213F67F494F7D27A6296C29F0DF62DFA18FD37FB592D9761A44DEE96528","last_results_hash":"","evidence_hash":"","proposer_address":"E8B4AF895B301C3D7108CA93A0B9234E3A2A4B21"},"data":{"txs":null},"evidence":{"evidence":null},"last_commit":{"block_id":{"hash":"168D140232DA3A59757658E257168B6BCE62024F574CA222C2D449BAB1AE4023","parts":{"total":"1","hash":"4E6D7A1C7DE6942470394C21B132A2D9F89B31908C3B6FA8C2AB96A87553B96E"}},"precommits":[{"type":2,"height":"15","round":"0","block_id":{"hash":"168D140232DA3A59757658E257168B6BCE62024F574CA222C2D449BAB1AE4023","parts":{"total":"1","hash":"4E6D7A1C7DE6942470394C21B132A2D9F89B31908C3B6FA8C2AB96A87553B96E"}},"timestamp":"2019-10-16T09:47:21.054275229Z","validator_address":"E8B4AF895B301C3D7108CA93A0B9234E3A2A4B21","validator_index":"0","signature":"eXB1NPmarUC5+2SCUQiWUCoGqtq0nrSn7giMAF/zWbfv3KmueE+1NoZ0CPLJAhBodz8Y8oL8xVy6ElA7J72kBw=="}]}}}` func TestConvertBlockResult(t *testing.T) { cdc := codec.New() var block ctypes.ResultBlock - cdc.MustUnmarshalJSON([]byte(defaultBlock), &block) + cdc.MustUnmarshalJSON([]byte(defaultBlockJson), &block) convertedBlock := ConvertBlockResult(&block) - //assert.Equal(t, "cosmosvaloper1hafptm4zxy5nw8rd2pxyg83c5ls2v62t4lkfqe", convertedBlock.BlockMeta.Header.ProposerAddress.String()) + assert.Equal(t, "cosmosvaloper1az62lz2mxqwr6ugge2f6pwfrfcaz5jephqhvkt", convertedBlock.BlockMeta.Header.ProposerAddress.String()) - convertedBlockString := cdc.MustMarshalBinaryBare(&convertedBlock) + convertedBlockString := string(cdc.MustMarshalJSON(&convertedBlock)) assert.NotContains(t, "E8B4AF895B301C3D7108CA93A0B9234E3A2A4B21", convertedBlockString) } From 3a4d70683e2ffd3f166a508cf51a48087bff120d Mon Sep 17 00:00:00 2001 From: Riccardo Montagnin Date: Fri, 18 Oct 2019 07:33:36 +0200 Subject: [PATCH 16/18] Updated the comments and removed some useless variables as said by @alexanderbez --- client/rpc/block_converter.go | 41 +++++++++++++++-------------------- 1 file changed, 17 insertions(+), 24 deletions(-) diff --git a/client/rpc/block_converter.go b/client/rpc/block_converter.go index 68907337fc14..d5aac8ca804e 100644 --- a/client/rpc/block_converter.go +++ b/client/rpc/block_converter.go @@ -27,11 +27,11 @@ type Header struct { // embed original type types.Header - // override fields from original type + // override fields so json.Marshal will marshal in accordance with amino JSON format Version Consensus `json:"version"` - Height int64 `json:"height,string"` // Override int64 fields do to json.Marshal not converting them to string - NumTxs int64 `json:"num_txs,string"` // Override int64 fields do to json.Marshal not converting them to string - TotalTxs int64 `json:"total_txs,string"` // Override int64 fields do to json.Marshal not converting them to string + Height int64 `json:"height,string"` + NumTxs int64 `json:"num_txs,string"` + TotalTxs int64 `json:"total_txs,string"` LastBlockID BlockID `json:"last_block_id"` ProposerAddress sdk.ValAddress `json:"proposer_address"` } @@ -40,8 +40,7 @@ type Header struct { // does not respect the JSON stdlib embedding semantics. func (h Header) MarshalJSON() ([]byte, error) { type headerJSON Header - _h := headerJSON(h) - return json.Marshal(_h) + return json.Marshal(headerJSON(h)) } // BlockID defines a wrapper around Tendermint's BlockID type overriding various fields. @@ -58,8 +57,7 @@ type BlockID struct { // does not respect the JSON stdlib embedding semantics. func (b BlockID) MarshalJSON() ([]byte, error) { type blockIDJson BlockID - _h := blockIDJson(b) - return json.Marshal(_h) + return json.Marshal(blockIDJson(b)) } // PartSetHeader defines a wrapper around Tendermint's PartSetHeader type overriding various fields. @@ -76,8 +74,7 @@ type PartSetHeader struct { // does not respect the JSON stdlib embedding semantics. func (b PartSetHeader) MarshalJSON() ([]byte, error) { type partSetHeadJson PartSetHeader - _h := partSetHeadJson(b) - return json.Marshal(_h) + return json.Marshal(partSetHeadJson(b)) } // Consensus defines a wrapper around Tendermint's Consensus type overriding various fields. @@ -86,17 +83,16 @@ type Consensus struct { // embed original type version.Consensus - // override fields from original type - App uint64 `json:"app,string"` // Override uint64 fields do to json.Marshal not converting them to string - Block uint64 `json:"block,string"` // Override uint64 fields do to json.Marshal not converting them to string + // override fields so json.Marshal will marshal in accordance with amino JSON format + App uint64 `json:"app,string"` + Block uint64 `json:"block,string"` } // MarshalJSON implements the json.Marshaler interface. We do this because Amino // does not respect the JSON stdlib embedding semantics. func (b Consensus) MarshalJSON() ([]byte, error) { type consensusJson Consensus - _h := consensusJson(b) - return json.Marshal(_h) + return json.Marshal(consensusJson(b)) } // Block defines the atomic unit of a Tendermint blockchain. @@ -113,8 +109,7 @@ type Block struct { // does not respect the JSON stdlib embedding semantics. func (b Block) MarshalJSON() ([]byte, error) { type blockJSON Block - _b := blockJSON(b) - return json.Marshal(_b) + return json.Marshal(blockJSON(b)) } // Commit defines a wrapper around Tendermint's Commit type overriding various fields. @@ -134,21 +129,19 @@ type CommitSig struct { // embed original type types.CommitSig - // override fields - Height int64 `json:"height,string"` // Override int64 fields do to json.Marshal not converting them to string - Round int `json:"round,string"` // Override int fields do to json.Marshal not converting them to string + // override fields so json.Marshal will marshal in accordance with amino JSON format + Height int64 `json:"height,string"` + Round int `json:"round,string"` BlockID BlockID `json:"block_id"` ValidatorAddress sdk.ValAddress `json:"validator_address"` - ValidatorIndex int `json:"validator_index,string"` // Override int fields do to json.Marshal not converting them to string + ValidatorIndex int `json:"validator_index,string"` } // MarshalJSON implements the json.Marshaler interface. We do this because Amino // does not respect the JSON stdlib embedding semantics. func (c CommitSig) MarshalJSON() ([]byte, error) { type commitSigJson CommitSig - _h := commitSigJson(c) - - return json.Marshal(_h) + return json.Marshal(commitSigJson(c)) } // ConvertBlockResult allows to convert the given standard ResultBlock into a new ResultBlock having all the From dbeb75929bef90ef2b9d503cc6b5c5b244a29318 Mon Sep 17 00:00:00 2001 From: Riccardo Montagnin Date: Wed, 13 Nov 2019 08:38:00 +0100 Subject: [PATCH 17/18] Removed global test variable and implemented nil test --- client/rpc/block_converter.go | 8 ++++++-- client/rpc/block_converter_test.go | 7 +++++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/client/rpc/block_converter.go b/client/rpc/block_converter.go index d5aac8ca804e..4ce769326299 100644 --- a/client/rpc/block_converter.go +++ b/client/rpc/block_converter.go @@ -146,7 +146,11 @@ func (c CommitSig) MarshalJSON() ([]byte, error) { // ConvertBlockResult allows to convert the given standard ResultBlock into a new ResultBlock having all the // validator addresses as Bech32 strings instead of HEX ones. -func ConvertBlockResult(res *ctypes.ResultBlock) (blockResult ResultBlock) { +func ConvertBlockResult(res *ctypes.ResultBlock) (blockResult *ResultBlock) { + + if res == nil { + return nil + } header := Header{ Header: res.BlockMeta.Header, @@ -170,7 +174,7 @@ func ConvertBlockResult(res *ctypes.ResultBlock) (blockResult ResultBlock) { ProposerAddress: sdk.ValAddress(res.BlockMeta.Header.ProposerAddress), } - return ResultBlock{ + return &ResultBlock{ BlockMeta: BlockMeta{ BlockID: res.BlockMeta.BlockID, Header: header, diff --git a/client/rpc/block_converter_test.go b/client/rpc/block_converter_test.go index b625d0267e8b..ca61fe78ca78 100644 --- a/client/rpc/block_converter_test.go +++ b/client/rpc/block_converter_test.go @@ -8,12 +8,12 @@ import ( ctypes "github.com/tendermint/tendermint/rpc/core/types" ) -var defaultBlockJson = `{"block_meta":{"block_id":{"hash":"30CD3A9EF2082FF9F2575655E99E37ACC3936DC9E534ADF0BD7436C76258225C","parts":{"total":"1","hash":"463460DA73FA0AE441B6041BF2683AE625CE2D9FD290F4C86CD83D1A7FB9F439"}},"header":{"version":{"block":"10","app":"0"},"chain_id":"test-chain-RoCfX4","height":"16","time":"2019-10-16T09:47:21.054275229Z","num_txs":"0","total_txs":"0","last_block_id":{"hash":"168D140232DA3A59757658E257168B6BCE62024F574CA222C2D449BAB1AE4023","parts":{"total":"1","hash":"4E6D7A1C7DE6942470394C21B132A2D9F89B31908C3B6FA8C2AB96A87553B96E"}},"last_commit_hash":"3483509FCC8048496E9B85E1E4C90A3B6E5944F022FE3C0709096932A010F712","data_hash":"","validators_hash":"61E69204249E5EE6F777C0566040929276A1900CE665BCB11006CABF9A4ED9A3","next_validators_hash":"61E69204249E5EE6F777C0566040929276A1900CE665BCB11006CABF9A4ED9A3","consensus_hash":"048091BC7DDC283F77BFBF91D73C44DA58C3DF8A9CBC867405D8B7F3DAADA22F","app_hash":"070A8213F67F494F7D27A6296C29F0DF62DFA18FD37FB592D9761A44DEE96528","last_results_hash":"","evidence_hash":"","proposer_address":"E8B4AF895B301C3D7108CA93A0B9234E3A2A4B21"}},"block":{"header":{"version":{"block":"10","app":"0"},"chain_id":"test-chain-RoCfX4","height":"16","time":"2019-10-16T09:47:21.054275229Z","num_txs":"0","total_txs":"0","last_block_id":{"hash":"168D140232DA3A59757658E257168B6BCE62024F574CA222C2D449BAB1AE4023","parts":{"total":"1","hash":"4E6D7A1C7DE6942470394C21B132A2D9F89B31908C3B6FA8C2AB96A87553B96E"}},"last_commit_hash":"3483509FCC8048496E9B85E1E4C90A3B6E5944F022FE3C0709096932A010F712","data_hash":"","validators_hash":"61E69204249E5EE6F777C0566040929276A1900CE665BCB11006CABF9A4ED9A3","next_validators_hash":"61E69204249E5EE6F777C0566040929276A1900CE665BCB11006CABF9A4ED9A3","consensus_hash":"048091BC7DDC283F77BFBF91D73C44DA58C3DF8A9CBC867405D8B7F3DAADA22F","app_hash":"070A8213F67F494F7D27A6296C29F0DF62DFA18FD37FB592D9761A44DEE96528","last_results_hash":"","evidence_hash":"","proposer_address":"E8B4AF895B301C3D7108CA93A0B9234E3A2A4B21"},"data":{"txs":null},"evidence":{"evidence":null},"last_commit":{"block_id":{"hash":"168D140232DA3A59757658E257168B6BCE62024F574CA222C2D449BAB1AE4023","parts":{"total":"1","hash":"4E6D7A1C7DE6942470394C21B132A2D9F89B31908C3B6FA8C2AB96A87553B96E"}},"precommits":[{"type":2,"height":"15","round":"0","block_id":{"hash":"168D140232DA3A59757658E257168B6BCE62024F574CA222C2D449BAB1AE4023","parts":{"total":"1","hash":"4E6D7A1C7DE6942470394C21B132A2D9F89B31908C3B6FA8C2AB96A87553B96E"}},"timestamp":"2019-10-16T09:47:21.054275229Z","validator_address":"E8B4AF895B301C3D7108CA93A0B9234E3A2A4B21","validator_index":"0","signature":"eXB1NPmarUC5+2SCUQiWUCoGqtq0nrSn7giMAF/zWbfv3KmueE+1NoZ0CPLJAhBodz8Y8oL8xVy6ElA7J72kBw=="}]}}}` - func TestConvertBlockResult(t *testing.T) { cdc := codec.New() + // test when the block is not nil var block ctypes.ResultBlock + defaultBlockJson := `{"block_meta":{"block_id":{"hash":"30CD3A9EF2082FF9F2575655E99E37ACC3936DC9E534ADF0BD7436C76258225C","parts":{"total":"1","hash":"463460DA73FA0AE441B6041BF2683AE625CE2D9FD290F4C86CD83D1A7FB9F439"}},"header":{"version":{"block":"10","app":"0"},"chain_id":"test-chain-RoCfX4","height":"16","time":"2019-10-16T09:47:21.054275229Z","num_txs":"0","total_txs":"0","last_block_id":{"hash":"168D140232DA3A59757658E257168B6BCE62024F574CA222C2D449BAB1AE4023","parts":{"total":"1","hash":"4E6D7A1C7DE6942470394C21B132A2D9F89B31908C3B6FA8C2AB96A87553B96E"}},"last_commit_hash":"3483509FCC8048496E9B85E1E4C90A3B6E5944F022FE3C0709096932A010F712","data_hash":"","validators_hash":"61E69204249E5EE6F777C0566040929276A1900CE665BCB11006CABF9A4ED9A3","next_validators_hash":"61E69204249E5EE6F777C0566040929276A1900CE665BCB11006CABF9A4ED9A3","consensus_hash":"048091BC7DDC283F77BFBF91D73C44DA58C3DF8A9CBC867405D8B7F3DAADA22F","app_hash":"070A8213F67F494F7D27A6296C29F0DF62DFA18FD37FB592D9761A44DEE96528","last_results_hash":"","evidence_hash":"","proposer_address":"E8B4AF895B301C3D7108CA93A0B9234E3A2A4B21"}},"block":{"header":{"version":{"block":"10","app":"0"},"chain_id":"test-chain-RoCfX4","height":"16","time":"2019-10-16T09:47:21.054275229Z","num_txs":"0","total_txs":"0","last_block_id":{"hash":"168D140232DA3A59757658E257168B6BCE62024F574CA222C2D449BAB1AE4023","parts":{"total":"1","hash":"4E6D7A1C7DE6942470394C21B132A2D9F89B31908C3B6FA8C2AB96A87553B96E"}},"last_commit_hash":"3483509FCC8048496E9B85E1E4C90A3B6E5944F022FE3C0709096932A010F712","data_hash":"","validators_hash":"61E69204249E5EE6F777C0566040929276A1900CE665BCB11006CABF9A4ED9A3","next_validators_hash":"61E69204249E5EE6F777C0566040929276A1900CE665BCB11006CABF9A4ED9A3","consensus_hash":"048091BC7DDC283F77BFBF91D73C44DA58C3DF8A9CBC867405D8B7F3DAADA22F","app_hash":"070A8213F67F494F7D27A6296C29F0DF62DFA18FD37FB592D9761A44DEE96528","last_results_hash":"","evidence_hash":"","proposer_address":"E8B4AF895B301C3D7108CA93A0B9234E3A2A4B21"},"data":{"txs":null},"evidence":{"evidence":null},"last_commit":{"block_id":{"hash":"168D140232DA3A59757658E257168B6BCE62024F574CA222C2D449BAB1AE4023","parts":{"total":"1","hash":"4E6D7A1C7DE6942470394C21B132A2D9F89B31908C3B6FA8C2AB96A87553B96E"}},"precommits":[{"type":2,"height":"15","round":"0","block_id":{"hash":"168D140232DA3A59757658E257168B6BCE62024F574CA222C2D449BAB1AE4023","parts":{"total":"1","hash":"4E6D7A1C7DE6942470394C21B132A2D9F89B31908C3B6FA8C2AB96A87553B96E"}},"timestamp":"2019-10-16T09:47:21.054275229Z","validator_address":"E8B4AF895B301C3D7108CA93A0B9234E3A2A4B21","validator_index":"0","signature":"eXB1NPmarUC5+2SCUQiWUCoGqtq0nrSn7giMAF/zWbfv3KmueE+1NoZ0CPLJAhBodz8Y8oL8xVy6ElA7J72kBw=="}]}}}` cdc.MustUnmarshalJSON([]byte(defaultBlockJson), &block) convertedBlock := ConvertBlockResult(&block) @@ -21,4 +21,7 @@ func TestConvertBlockResult(t *testing.T) { convertedBlockString := string(cdc.MustMarshalJSON(&convertedBlock)) assert.NotContains(t, "E8B4AF895B301C3D7108CA93A0B9234E3A2A4B21", convertedBlockString) + + // test when the block is nil + assert.Nil(t, ConvertBlockResult(nil)) } From 1f9e9db649979cbd0ad4cf98e5f9cda66c594719 Mon Sep 17 00:00:00 2001 From: Riccardo Montagnin Date: Wed, 13 Nov 2019 08:48:40 +0100 Subject: [PATCH 18/18] Renamed variables to solve Golint errors --- client/rpc/block_converter.go | 17 +++++++++-------- client/rpc/block_converter_test.go | 4 ++-- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/client/rpc/block_converter.go b/client/rpc/block_converter.go index 4ce769326299..6edf3f81d815 100644 --- a/client/rpc/block_converter.go +++ b/client/rpc/block_converter.go @@ -56,8 +56,8 @@ type BlockID struct { // MarshalJSON implements the json.Marshaler interface. We do this because Amino // does not respect the JSON stdlib embedding semantics. func (b BlockID) MarshalJSON() ([]byte, error) { - type blockIDJson BlockID - return json.Marshal(blockIDJson(b)) + type blockIDJSON BlockID + return json.Marshal(blockIDJSON(b)) } // PartSetHeader defines a wrapper around Tendermint's PartSetHeader type overriding various fields. @@ -73,8 +73,8 @@ type PartSetHeader struct { // MarshalJSON implements the json.Marshaler interface. We do this because Amino // does not respect the JSON stdlib embedding semantics. func (b PartSetHeader) MarshalJSON() ([]byte, error) { - type partSetHeadJson PartSetHeader - return json.Marshal(partSetHeadJson(b)) + type partSetHeadJSON PartSetHeader + return json.Marshal(partSetHeadJSON(b)) } // Consensus defines a wrapper around Tendermint's Consensus type overriding various fields. @@ -91,11 +91,12 @@ type Consensus struct { // MarshalJSON implements the json.Marshaler interface. We do this because Amino // does not respect the JSON stdlib embedding semantics. func (b Consensus) MarshalJSON() ([]byte, error) { - type consensusJson Consensus - return json.Marshal(consensusJson(b)) + type consensusJSON Consensus + return json.Marshal(consensusJSON(b)) } // Block defines the atomic unit of a Tendermint blockchain. +// nolint: structtag. type Block struct { // embed original type types.Block @@ -140,8 +141,8 @@ type CommitSig struct { // MarshalJSON implements the json.Marshaler interface. We do this because Amino // does not respect the JSON stdlib embedding semantics. func (c CommitSig) MarshalJSON() ([]byte, error) { - type commitSigJson CommitSig - return json.Marshal(commitSigJson(c)) + type commitSigJSON CommitSig + return json.Marshal(commitSigJSON(c)) } // ConvertBlockResult allows to convert the given standard ResultBlock into a new ResultBlock having all the diff --git a/client/rpc/block_converter_test.go b/client/rpc/block_converter_test.go index ca61fe78ca78..50d4d7f65b90 100644 --- a/client/rpc/block_converter_test.go +++ b/client/rpc/block_converter_test.go @@ -13,8 +13,8 @@ func TestConvertBlockResult(t *testing.T) { // test when the block is not nil var block ctypes.ResultBlock - defaultBlockJson := `{"block_meta":{"block_id":{"hash":"30CD3A9EF2082FF9F2575655E99E37ACC3936DC9E534ADF0BD7436C76258225C","parts":{"total":"1","hash":"463460DA73FA0AE441B6041BF2683AE625CE2D9FD290F4C86CD83D1A7FB9F439"}},"header":{"version":{"block":"10","app":"0"},"chain_id":"test-chain-RoCfX4","height":"16","time":"2019-10-16T09:47:21.054275229Z","num_txs":"0","total_txs":"0","last_block_id":{"hash":"168D140232DA3A59757658E257168B6BCE62024F574CA222C2D449BAB1AE4023","parts":{"total":"1","hash":"4E6D7A1C7DE6942470394C21B132A2D9F89B31908C3B6FA8C2AB96A87553B96E"}},"last_commit_hash":"3483509FCC8048496E9B85E1E4C90A3B6E5944F022FE3C0709096932A010F712","data_hash":"","validators_hash":"61E69204249E5EE6F777C0566040929276A1900CE665BCB11006CABF9A4ED9A3","next_validators_hash":"61E69204249E5EE6F777C0566040929276A1900CE665BCB11006CABF9A4ED9A3","consensus_hash":"048091BC7DDC283F77BFBF91D73C44DA58C3DF8A9CBC867405D8B7F3DAADA22F","app_hash":"070A8213F67F494F7D27A6296C29F0DF62DFA18FD37FB592D9761A44DEE96528","last_results_hash":"","evidence_hash":"","proposer_address":"E8B4AF895B301C3D7108CA93A0B9234E3A2A4B21"}},"block":{"header":{"version":{"block":"10","app":"0"},"chain_id":"test-chain-RoCfX4","height":"16","time":"2019-10-16T09:47:21.054275229Z","num_txs":"0","total_txs":"0","last_block_id":{"hash":"168D140232DA3A59757658E257168B6BCE62024F574CA222C2D449BAB1AE4023","parts":{"total":"1","hash":"4E6D7A1C7DE6942470394C21B132A2D9F89B31908C3B6FA8C2AB96A87553B96E"}},"last_commit_hash":"3483509FCC8048496E9B85E1E4C90A3B6E5944F022FE3C0709096932A010F712","data_hash":"","validators_hash":"61E69204249E5EE6F777C0566040929276A1900CE665BCB11006CABF9A4ED9A3","next_validators_hash":"61E69204249E5EE6F777C0566040929276A1900CE665BCB11006CABF9A4ED9A3","consensus_hash":"048091BC7DDC283F77BFBF91D73C44DA58C3DF8A9CBC867405D8B7F3DAADA22F","app_hash":"070A8213F67F494F7D27A6296C29F0DF62DFA18FD37FB592D9761A44DEE96528","last_results_hash":"","evidence_hash":"","proposer_address":"E8B4AF895B301C3D7108CA93A0B9234E3A2A4B21"},"data":{"txs":null},"evidence":{"evidence":null},"last_commit":{"block_id":{"hash":"168D140232DA3A59757658E257168B6BCE62024F574CA222C2D449BAB1AE4023","parts":{"total":"1","hash":"4E6D7A1C7DE6942470394C21B132A2D9F89B31908C3B6FA8C2AB96A87553B96E"}},"precommits":[{"type":2,"height":"15","round":"0","block_id":{"hash":"168D140232DA3A59757658E257168B6BCE62024F574CA222C2D449BAB1AE4023","parts":{"total":"1","hash":"4E6D7A1C7DE6942470394C21B132A2D9F89B31908C3B6FA8C2AB96A87553B96E"}},"timestamp":"2019-10-16T09:47:21.054275229Z","validator_address":"E8B4AF895B301C3D7108CA93A0B9234E3A2A4B21","validator_index":"0","signature":"eXB1NPmarUC5+2SCUQiWUCoGqtq0nrSn7giMAF/zWbfv3KmueE+1NoZ0CPLJAhBodz8Y8oL8xVy6ElA7J72kBw=="}]}}}` - cdc.MustUnmarshalJSON([]byte(defaultBlockJson), &block) + defaultBlockJSON := `{"block_meta":{"block_id":{"hash":"30CD3A9EF2082FF9F2575655E99E37ACC3936DC9E534ADF0BD7436C76258225C","parts":{"total":"1","hash":"463460DA73FA0AE441B6041BF2683AE625CE2D9FD290F4C86CD83D1A7FB9F439"}},"header":{"version":{"block":"10","app":"0"},"chain_id":"test-chain-RoCfX4","height":"16","time":"2019-10-16T09:47:21.054275229Z","num_txs":"0","total_txs":"0","last_block_id":{"hash":"168D140232DA3A59757658E257168B6BCE62024F574CA222C2D449BAB1AE4023","parts":{"total":"1","hash":"4E6D7A1C7DE6942470394C21B132A2D9F89B31908C3B6FA8C2AB96A87553B96E"}},"last_commit_hash":"3483509FCC8048496E9B85E1E4C90A3B6E5944F022FE3C0709096932A010F712","data_hash":"","validators_hash":"61E69204249E5EE6F777C0566040929276A1900CE665BCB11006CABF9A4ED9A3","next_validators_hash":"61E69204249E5EE6F777C0566040929276A1900CE665BCB11006CABF9A4ED9A3","consensus_hash":"048091BC7DDC283F77BFBF91D73C44DA58C3DF8A9CBC867405D8B7F3DAADA22F","app_hash":"070A8213F67F494F7D27A6296C29F0DF62DFA18FD37FB592D9761A44DEE96528","last_results_hash":"","evidence_hash":"","proposer_address":"E8B4AF895B301C3D7108CA93A0B9234E3A2A4B21"}},"block":{"header":{"version":{"block":"10","app":"0"},"chain_id":"test-chain-RoCfX4","height":"16","time":"2019-10-16T09:47:21.054275229Z","num_txs":"0","total_txs":"0","last_block_id":{"hash":"168D140232DA3A59757658E257168B6BCE62024F574CA222C2D449BAB1AE4023","parts":{"total":"1","hash":"4E6D7A1C7DE6942470394C21B132A2D9F89B31908C3B6FA8C2AB96A87553B96E"}},"last_commit_hash":"3483509FCC8048496E9B85E1E4C90A3B6E5944F022FE3C0709096932A010F712","data_hash":"","validators_hash":"61E69204249E5EE6F777C0566040929276A1900CE665BCB11006CABF9A4ED9A3","next_validators_hash":"61E69204249E5EE6F777C0566040929276A1900CE665BCB11006CABF9A4ED9A3","consensus_hash":"048091BC7DDC283F77BFBF91D73C44DA58C3DF8A9CBC867405D8B7F3DAADA22F","app_hash":"070A8213F67F494F7D27A6296C29F0DF62DFA18FD37FB592D9761A44DEE96528","last_results_hash":"","evidence_hash":"","proposer_address":"E8B4AF895B301C3D7108CA93A0B9234E3A2A4B21"},"data":{"txs":null},"evidence":{"evidence":null},"last_commit":{"block_id":{"hash":"168D140232DA3A59757658E257168B6BCE62024F574CA222C2D449BAB1AE4023","parts":{"total":"1","hash":"4E6D7A1C7DE6942470394C21B132A2D9F89B31908C3B6FA8C2AB96A87553B96E"}},"precommits":[{"type":2,"height":"15","round":"0","block_id":{"hash":"168D140232DA3A59757658E257168B6BCE62024F574CA222C2D449BAB1AE4023","parts":{"total":"1","hash":"4E6D7A1C7DE6942470394C21B132A2D9F89B31908C3B6FA8C2AB96A87553B96E"}},"timestamp":"2019-10-16T09:47:21.054275229Z","validator_address":"E8B4AF895B301C3D7108CA93A0B9234E3A2A4B21","validator_index":"0","signature":"eXB1NPmarUC5+2SCUQiWUCoGqtq0nrSn7giMAF/zWbfv3KmueE+1NoZ0CPLJAhBodz8Y8oL8xVy6ElA7J72kBw=="}]}}}` + cdc.MustUnmarshalJSON([]byte(defaultBlockJSON), &block) convertedBlock := ConvertBlockResult(&block) assert.Equal(t, "cosmosvaloper1az62lz2mxqwr6ugge2f6pwfrfcaz5jephqhvkt", convertedBlock.BlockMeta.Header.ProposerAddress.String())