Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert Tendermint Block Response #4922

Closed
wants to merge 28 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
9c8bcba
Added the conversion of the block in order to convert HEX validator a…
RiccardoM Aug 19, 2019
37c46f2
Merge branch 'master' of github.com:cosmos/cosmos-sdk into riccardo/b…
RiccardoM Aug 19, 2019
604f494
Removed mutex
RiccardoM Aug 19, 2019
40604b4
Undo go mod change
RiccardoM Aug 19, 2019
4439a72
Removed Cosmos* type prefix
RiccardoM Aug 19, 2019
499818d
Sorted imports using goimports instead of gofmt
RiccardoM Aug 20, 2019
e29aba1
Applied suggestion to conversion call
RiccardoM Aug 21, 2019
6934f17
Undo changed in go.sum
RiccardoM Aug 21, 2019
93a8526
Used type wrappers instead of re defining the whole type while conver…
RiccardoM Aug 21, 2019
faa3f2c
Added // nolint: structtag
RiccardoM Aug 21, 2019
69fd89c
Removed useless comments and variables while converting a block
RiccardoM Aug 26, 2019
909faa0
Unified how different types are treated (encapsulation) and added mis…
RiccardoM Aug 26, 2019
70d5cb7
Reverted back encapsulation
RiccardoM Aug 26, 2019
c667b53
Merge remote-tracking branch 'origin/riccardo/block-response-conversi…
RiccardoM Aug 26, 2019
8910ec2
Update client/rpc/block_converter.go
RiccardoM Aug 26, 2019
80ffba3
Merge branch 'master' of github.com:cosmos/cosmos-sdk into riccardo/b…
RiccardoM Oct 16, 2019
59aff26
Added tests
RiccardoM Oct 16, 2019
b520774
Fixed the missing of some fields
RiccardoM Oct 17, 2019
d8daeab
Merge branch 'master' into riccardo/block-response-conversion
alexanderbez Oct 17, 2019
8e04baf
Merge branch 'master' into riccardo/block-response-conversion
alexanderbez Oct 17, 2019
3a4d706
Updated the comments and removed some useless variables as said by @a…
RiccardoM Oct 18, 2019
8a24e6d
Merge branch 'master' into riccardo/block-response-conversion
fedekunze Oct 28, 2019
dbeb759
Removed global test variable and implemented nil test
RiccardoM Nov 13, 2019
37f2585
Merge remote-tracking branch 'origin/riccardo/block-response-conversi…
RiccardoM Nov 13, 2019
f241029
Merge branch 'master' of github.com:cosmos/cosmos-sdk into riccardo/b…
RiccardoM Nov 13, 2019
1f9e9db
Renamed variables to solve Golint errors
RiccardoM Nov 13, 2019
56a37b9
Merge branch 'master' into riccardo/block-response-conversion
fedekunze Nov 19, 2019
1815c3a
Merge branch 'master' into riccardo/block-response-conversion
alexanderbez Dec 2, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions client/rpc/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
RiccardoM marked this conversation as resolved.
Show resolved Hide resolved
"strconv"

"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/client/flags"
Expand Down Expand Up @@ -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)
RiccardoM marked this conversation as resolved.
Show resolved Hide resolved
RiccardoM marked this conversation as resolved.
Show resolved Hide resolved
}

// get the current blockchain height
Expand Down
147 changes: 147 additions & 0 deletions client/rpc/block_converter.go
Original file line number Diff line number Diff line change
@@ -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 {
RiccardoM marked this conversation as resolved.
Show resolved Hide resolved
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 {
tac0turtle marked this conversation as resolved.
Show resolved Hide resolved
// 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
RiccardoM marked this conversation as resolved.
Show resolved Hide resolved
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
RiccardoM marked this conversation as resolved.
Show resolved Hide resolved
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,
RiccardoM marked this conversation as resolved.
Show resolved Hide resolved
}

return blockResult
RiccardoM marked this conversation as resolved.
Show resolved Hide resolved
}

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
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
RiccardoM marked this conversation as resolved.
Show resolved Hide resolved
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
Expand Down
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
RiccardoM marked this conversation as resolved.
Show resolved Hide resolved
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=
Expand All @@ -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=
Expand Down Expand Up @@ -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=
Expand All @@ -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=
Expand All @@ -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=
Expand Down