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

refactor: remove bytes/HexBytes #15211

Merged
merged 11 commits into from
Mar 1, 2023
Merged
Changes from all commits
Commits
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
19 changes: 18 additions & 1 deletion UPGRADING.md
Original file line number Diff line number Diff line change
@@ -7,14 +7,18 @@ This guide provides instructions for upgrading to specific versions of Cosmos SD
### Migration to CometBFT (Part 2)

The Cosmos SDK has migrated in, its previous versions, to CometBFT.
Some functions have been renamed to reflect the naming change.
Some functions have been renamed to reflect the naming change. And the Cosmos SDK has removed the import of cmtbytes "github.com/cometbft/cometbft/libs/bytes".
There is something changed.Due to the import changes, this is a breaking change. Chains need to remove **entirely** their imports in their codebase, from direct and indirects imports.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
There is something changed.Due to the import changes, this is a breaking change. Chains need to remove **entirely** their imports in their codebase, from direct and indirects imports.
There is something changed. Due to the import changes, this is a breaking change. Chains need to remove **entirely** their imports in their codebase, from direct and indirects imports.


Following an exhaustive list:

* `client.TendermintRPC` -> `client.CometRPC`
* `clitestutil.MockTendermintRPC` -> `clitestutil.MockCometRPC`
* `clitestutilgenutil.CreateDefaultTendermintConfig` -> `clitestutilgenutil.CreateDefaultCometConfig`
* Package `client/grpc/tmservice` -> `client/grpc/cmtservice`
* Remove `github.com/cometbft/cometbft/libs/bytes` & Replace All `cmtbytes.HexBytes` by `[]byte` except `*pb.go`files
* Verify `github.com/cometbft/cometbft/libs/bytes` is not an indirect or direct dependency
* Run `make proto-gen`

Additionally, the commands and flags mentionning `tendermint` have been renamed to `comet`.
However, these commands and flags is still supported for backward compatibility.
@@ -52,6 +56,7 @@ See related issues:
### Protobuf

The SDK is in the process of removing all `gogoproto` annotations.
The SDK is in the process of removing all `(gogoproto.casttype) = "github.com/cometbft/cometbft/libs/bytes.HexBytes"`.

#### Stringer

@@ -101,6 +106,18 @@ By default, the new `ProposalCancelRatio` parameter is set to 0.5 during migrati

The `x/evidence` module is extracted to have a separate go.mod file which allows it be a standalone module.
All the evidence imports are now renamed to use `cosmossdk.io/x/evidence` instead of `github.com/cosmos/cosmos-sdk/x/evidence` across the SDK.
All the evidence removed `github.com/cometbft/cometbft/libs/bytes`.
All the evidence functions or params are now renamed to use `cmtbytes.HexBytes` or `bytes.HexBytes` instead of `[]byte` across the SDK.

#### `x/bank`

All the bank removed `github.com/cometbft/cometbft/libs/bytes`.
All the bank functions or params are now renamed to use `[]byte` instead of `cmtbytes.HexBytes` or `bytes.HexBytes` across the SDK.

#### `x/simulation`

All the simulation removed `github.com/cometbft/cometbft/libs/bytes`
All the simulation functions or params are now renamed to use `cmtbytes.HexBytes` or `bytes.HexBytes` instead of `[]byte` across the SDK.

#### `x/nft`

127 changes: 62 additions & 65 deletions api/cosmos/evidence/v1beta1/query.pulsar.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions client/query.go
Original file line number Diff line number Diff line change
@@ -7,7 +7,6 @@ import (

"github.com/cockroachdb/errors"
abci "github.com/cometbft/cometbft/abci/types"
cmtbytes "github.com/cometbft/cometbft/libs/bytes"
rpcclient "github.com/cometbft/cometbft/rpc/client"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
@@ -45,7 +44,7 @@ func (ctx Context) QueryWithData(path string, data []byte) ([]byte, int64, error
// QueryStore performs a query to a CometBFT node with the provided key and
// store name. It returns the result and height of the query upon success
// or an error if the query fails.
func (ctx Context) QueryStore(key cmtbytes.HexBytes, storeName string) ([]byte, int64, error) {
func (ctx Context) QueryStore(key []byte, storeName string) ([]byte, int64, error) {
return ctx.queryStore(key, storeName, "key")
}

@@ -129,7 +128,7 @@ func sdkErrorToGRPCError(resp abci.ResponseQuery) error {
// query performs a query to a CometBFT node with the provided store name
// and path. It returns the result and height of the query upon success
// or an error if the query fails.
func (ctx Context) query(path string, key cmtbytes.HexBytes) ([]byte, int64, error) {
func (ctx Context) query(path string, key []byte) ([]byte, int64, error) {
resp, err := ctx.queryABCI(abci.RequestQuery{
Path: path,
Data: key,
@@ -145,7 +144,7 @@ func (ctx Context) query(path string, key cmtbytes.HexBytes) ([]byte, int64, err
// queryStore performs a query to a CometBFT node with the provided a store
// name and path. It returns the result and height of the query upon success
// or an error if the query fails.
func (ctx Context) queryStore(key cmtbytes.HexBytes, storeName, endPath string) ([]byte, int64, error) {
func (ctx Context) queryStore(key []byte, storeName, endPath string) ([]byte, int64, error) {
path := fmt.Sprintf("/store/%s/%s", storeName, endPath)
return ctx.query(path, key)
}
3 changes: 1 addition & 2 deletions client/rpc/status.go
Original file line number Diff line number Diff line change
@@ -5,7 +5,6 @@ import (

"github.com/spf13/cobra"

"github.com/cometbft/cometbft/libs/bytes"
"github.com/cometbft/cometbft/p2p"
coretypes "github.com/cometbft/cometbft/rpc/core/types"

@@ -18,7 +17,7 @@ import (
// ValidatorInfo is info about the node's validator, same as CometBFT,
// except that we use our own PubKey.
type validatorInfo struct {
Address bytes.HexBytes
Address []byte
PubKey cryptotypes.PubKey
VotingPower int64
}
3 changes: 1 addition & 2 deletions proto/cosmos/evidence/v1beta1/query.proto
Original file line number Diff line number Diff line change
@@ -25,8 +25,7 @@ service Query {
message QueryEvidenceRequest {
// evidence_hash defines the hash of the requested evidence.
// Deprecated: Use hash, a HEX encoded string, instead.
bytes evidence_hash = 1
[deprecated = true, (gogoproto.casttype) = "github.com/cometbft/cometbft/libs/bytes.HexBytes"];
bytes evidence_hash = 1 [deprecated = true];

// hash defines the evidence hash of the requested evidence.
//
5 changes: 2 additions & 3 deletions types/context.go
Original file line number Diff line number Diff line change
@@ -6,7 +6,6 @@ import (

"cosmossdk.io/log"
abci "github.com/cometbft/cometbft/abci/types"
cmtbytes "github.com/cometbft/cometbft/libs/bytes"
cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"
"github.com/cosmos/gogoproto/proto"

@@ -26,7 +25,7 @@ type Context struct {
baseCtx context.Context
ms storetypes.MultiStore
header cmtproto.Header
headerHash cmtbytes.HexBytes
headerHash []byte
chainID string
txBytes []byte
logger log.Logger
@@ -72,7 +71,7 @@ func (c Context) BlockHeader() cmtproto.Header {
}

// HeaderHash returns a copy of the header hash obtained during abci.RequestBeginBlock
func (c Context) HeaderHash() cmtbytes.HexBytes {
func (c Context) HeaderHash() []byte {
hash := make([]byte, len(c.headerHash))
copy(hash, c.headerHash)
return hash
2 changes: 1 addition & 1 deletion types/context_test.go
Original file line number Diff line number Diff line change
@@ -121,7 +121,7 @@ func (s *contextTestSuite) TestContextWithCustom() {
s.Require().Equal(meter, ctx.GasMeter())
s.Require().Equal(minGasPrices, ctx.MinGasPrices())
s.Require().Equal(blockGasMeter, ctx.BlockGasMeter())
s.Require().Equal(headerHash, ctx.HeaderHash().Bytes())
s.Require().Equal(headerHash, ctx.HeaderHash())
s.Require().False(ctx.WithIsCheckTx(false).IsCheckTx())
s.Require().Equal(zeroGasCfg, ctx.KVGasConfig())
s.Require().Equal(zeroGasCfg, ctx.TransientKVGasConfig())
5 changes: 2 additions & 3 deletions types/result_test.go
Original file line number Diff line number Diff line change
@@ -8,7 +8,6 @@ import (
"time"

abci "github.com/cometbft/cometbft/abci/types"
"github.com/cometbft/cometbft/libs/bytes"
coretypes "github.com/cometbft/cometbft/rpc/core/types"
"github.com/golang/protobuf/proto" //nolint:staticcheck // grpc-gateway uses deprecated golang/protobuf
"github.com/stretchr/testify/require"
@@ -81,7 +80,7 @@ func (s *resultTestSuite) TestResponseResultTx() {
GasUsed: 90,
}
resultTx := &coretypes.ResultTx{
Hash: bytes.HexBytes([]byte("test")),
Hash: []byte("test"),
Height: 10,
TxResult: deliverTxResult,
}
@@ -128,7 +127,7 @@ txhash: "74657374"
Codespace: "codespace",
Data: []byte("data"),
Log: `[]`,
Hash: bytes.HexBytes([]byte("test")),
Hash: []byte("test"),
}

s.Require().Equal(&sdk.TxResponse{
6 changes: 3 additions & 3 deletions x/bank/keeper/keeper_test.go
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@ package keeper_test

import (
"crypto/sha256"
"encoding/hex"
"fmt"
"strings"
"testing"
@@ -16,7 +17,6 @@ import (

storetypes "cosmossdk.io/store/types"

cmtbytes "github.com/cometbft/cometbft/libs/bytes"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/testutil"
sdk "github.com/cosmos/cosmos-sdk/types"
@@ -79,10 +79,10 @@ func newIbcCoin(amt int64) sdk.Coin {
}

func getIBCDenom(path string, baseDenom string) string {
return fmt.Sprintf("%s/%s", "ibc", getIBCHash(path, baseDenom))
return fmt.Sprintf("%s/%s", "ibc", hex.EncodeToString(getIBCHash(path, baseDenom)))
}

func getIBCHash(path string, baseDenom string) cmtbytes.HexBytes {
func getIBCHash(path string, baseDenom string) []byte {
hash := sha256.Sum256([]byte(path + "/" + baseDenom))
return hash[:]
}
6 changes: 3 additions & 3 deletions x/evidence/README.md
Original file line number Diff line number Diff line change
@@ -54,7 +54,7 @@ type Evidence interface {

Route() string
String() string
Hash() cmtbytes.HexBytes
Hash() []byte
ValidateBasic() error

// Height at which the infraction occurred
@@ -153,7 +153,7 @@ as follows:
```go
func SubmitEvidence(ctx Context, evidence Evidence) error {
if _, ok := GetEvidence(ctx, evidence.Hash()); ok {
return errorsmod.Wrap(types.ErrEvidenceExists, evidence.Hash().String())
return errorsmod.Wrap(types.ErrEvidenceExists, strings.ToUpper(hex.EncodeToString(evidence.Hash())))
}
if !router.HasRoute(evidence.Route()) {
return errorsmod.Wrap(types.ErrNoEvidenceHandlerExists, evidence.Route())
@@ -167,7 +167,7 @@ func SubmitEvidence(ctx Context, evidence Evidence) error {
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeSubmitEvidence,
sdk.NewAttribute(types.AttributeKeyEvidenceHash, evidence.Hash().String()),
sdk.NewAttribute(types.AttributeKeyEvidenceHash, strings.ToUpper(hex.EncodeToString(evidence.Hash()))),
),
)

3 changes: 1 addition & 2 deletions x/evidence/exported/evidence.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package exported

import (
cmtbytes "github.com/cometbft/cometbft/libs/bytes"
"github.com/cosmos/gogoproto/proto"

sdk "github.com/cosmos/cosmos-sdk/types"
@@ -14,7 +13,7 @@ type Evidence interface {

Route() string
String() string
Hash() cmtbytes.HexBytes
Hash() []byte
ValidateBasic() error

// Height at which the infraction occurred
5 changes: 3 additions & 2 deletions x/evidence/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keeper_test

import (
"encoding/hex"
"fmt"
"strings"

@@ -37,7 +38,7 @@ func (suite *KeeperTestSuite) TestQueryEvidence() {
func() {
numEvidence := 1
evidence = suite.populateEvidence(suite.ctx, numEvidence)
evidenceHash := evidence[0].Hash().String()
evidenceHash := strings.ToUpper(hex.EncodeToString(evidence[0].Hash()))
reqHash := strings.Repeat("a", len(evidenceHash))
req = types.NewQueryEvidenceRequest(reqHash)
},
@@ -51,7 +52,7 @@ func (suite *KeeperTestSuite) TestQueryEvidence() {
func() {
numEvidence := 100
evidence = suite.populateEvidence(suite.ctx, numEvidence)
req = types.NewQueryEvidenceRequest(evidence[0].Hash().String())
req = types.NewQueryEvidenceRequest(strings.ToUpper(hex.EncodeToString(evidence[0].Hash())))
},
true,
"",
9 changes: 5 additions & 4 deletions x/evidence/keeper/keeper.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package keeper

import (
"encoding/hex"
"fmt"
"strings"

"cosmossdk.io/log"
"cosmossdk.io/x/evidence/exported"
"cosmossdk.io/x/evidence/types"
cmtbytes "github.com/cometbft/cometbft/libs/bytes"

"cosmossdk.io/errors"
"cosmossdk.io/store/prefix"
@@ -79,7 +80,7 @@ func (k Keeper) GetEvidenceHandler(evidenceRoute string) (types.Handler, error)
// persisted.
func (k Keeper) SubmitEvidence(ctx sdk.Context, evidence exported.Evidence) error {
if _, ok := k.GetEvidence(ctx, evidence.Hash()); ok {
return errors.Wrap(types.ErrEvidenceExists, evidence.Hash().String())
return errors.Wrap(types.ErrEvidenceExists, strings.ToUpper(hex.EncodeToString(evidence.Hash())))
}
if !k.router.HasRoute(evidence.Route()) {
return errors.Wrap(types.ErrNoEvidenceHandlerExists, evidence.Route())
@@ -93,7 +94,7 @@ func (k Keeper) SubmitEvidence(ctx sdk.Context, evidence exported.Evidence) erro
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeSubmitEvidence,
sdk.NewAttribute(types.AttributeKeyEvidenceHash, evidence.Hash().String()),
sdk.NewAttribute(types.AttributeKeyEvidenceHash, strings.ToUpper(hex.EncodeToString(evidence.Hash()))),
),
)

@@ -109,7 +110,7 @@ func (k Keeper) SetEvidence(ctx sdk.Context, evidence exported.Evidence) {

// GetEvidence retrieves Evidence by hash if it exists. If no Evidence exists for
// the given hash, (nil, false) is returned.
func (k Keeper) GetEvidence(ctx sdk.Context, hash cmtbytes.HexBytes) (exported.Evidence, bool) {
func (k Keeper) GetEvidence(ctx sdk.Context, hash []byte) (exported.Evidence, bool) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixEvidence)

bz := store.Get(hash)
3 changes: 1 addition & 2 deletions x/evidence/types/evidence.go
Original file line number Diff line number Diff line change
@@ -7,7 +7,6 @@ import (
"cosmossdk.io/x/evidence/exported"
abci "github.com/cometbft/cometbft/abci/types"
"github.com/cometbft/cometbft/crypto/tmhash"
cmtbytes "github.com/cometbft/cometbft/libs/bytes"

sdk "github.com/cosmos/cosmos-sdk/types"
)
@@ -21,7 +20,7 @@ var _ exported.Evidence = &Equivocation{}
func (e *Equivocation) Route() string { return RouteEquivocation }

// Hash returns the hash of an Equivocation object.
func (e *Equivocation) Hash() cmtbytes.HexBytes {
func (e *Equivocation) Hash() []byte {
bz, err := e.Marshal()
if err != nil {
panic(err)
6 changes: 4 additions & 2 deletions x/evidence/types/evidence_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package types_test

import (
"encoding/hex"
"strings"
"testing"
"time"

@@ -28,7 +30,7 @@ func TestEquivocation_Valid(t *testing.T) {
require.Equal(t, e.GetConsensusAddress().String(), e.ConsensusAddress)
require.Equal(t, e.GetHeight(), e.Height)
require.Equal(t, e.Route(), types.RouteEquivocation)
require.Equal(t, e.Hash().String(), "1E10F9267BEA3A9A4AB5302C2C510CC1AFD7C54E232DA5B2E3360DFAFACF7A76")
require.Equal(t, strings.ToUpper(hex.EncodeToString(e.Hash())), "1E10F9267BEA3A9A4AB5302C2C510CC1AFD7C54E232DA5B2E3360DFAFACF7A76")
require.Equal(t, "height:100 time:<seconds:1136214245 > power:1000000 consensus_address:\"cosmosvalcons1vehk7h6lta047h6lta047h6lta047h6l8m4r53\" ", e.String())
require.NoError(t, e.ValidateBasic())

@@ -38,7 +40,7 @@ func TestEquivocation_Valid(t *testing.T) {
require.Equal(t, e.ConsensusAddress, e.GetConsensusAddress().String())
require.Equal(t, e.Height, e.GetHeight())
require.Equal(t, types.RouteEquivocation, e.Route())
require.Equal(t, "1E10F9267BEA3A9A4AB5302C2C510CC1AFD7C54E232DA5B2E3360DFAFACF7A76", e.Hash().String())
require.Equal(t, "1E10F9267BEA3A9A4AB5302C2C510CC1AFD7C54E232DA5B2E3360DFAFACF7A76", strings.ToUpper(hex.EncodeToString(e.Hash())))
require.Equal(t, "height:100 time:<seconds:1136214245 > power:1000000 consensus_address:\"cosmosvalcons1vehk7h6lta047h6lta047h6lta047h6l8m4r53\" ", e.String())
require.NoError(t, e.ValidateBasic())
}
5 changes: 2 additions & 3 deletions x/evidence/types/genesis_test.go
Original file line number Diff line number Diff line change
@@ -7,7 +7,6 @@ import (

"cosmossdk.io/x/evidence/exported"
"cosmossdk.io/x/evidence/types"
cmtbytes "github.com/cometbft/cometbft/libs/bytes"
"github.com/stretchr/testify/require"

"github.com/cosmos/cosmos-sdk/codec"
@@ -171,8 +170,8 @@ func (*TestEvidence) Route() string {
func (*TestEvidence) ProtoMessage() {}
func (*TestEvidence) Reset() {}

func (*TestEvidence) Hash() cmtbytes.HexBytes {
return cmtbytes.HexBytes([]byte("test-hash"))
func (*TestEvidence) Hash() []byte {
return []byte("test-hash")
}

func (*TestEvidence) ValidateBasic() error {
65 changes: 31 additions & 34 deletions x/evidence/types/query.pb.go
3 changes: 1 addition & 2 deletions x/simulation/mock_cometbft.go
Original file line number Diff line number Diff line change
@@ -9,7 +9,6 @@ import (

abci "github.com/cometbft/cometbft/abci/types"
cryptoenc "github.com/cometbft/cometbft/crypto/encoding"
cmtbytes "github.com/cometbft/cometbft/libs/bytes"
cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"
)

@@ -60,7 +59,7 @@ func (vals mockValidators) getKeys() []string {
}

// randomProposer picks a random proposer from the current validator set
func (vals mockValidators) randomProposer(r *rand.Rand) cmtbytes.HexBytes {
func (vals mockValidators) randomProposer(r *rand.Rand) []byte {
keys := vals.getKeys()
if len(keys) == 0 {
return nil