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
Show file tree
Hide file tree
Changes from 6 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
68 changes: 68 additions & 0 deletions UPGRADING.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,74 @@ This guide provides instructions for upgrading to specific versions of Cosmos SD

## [Unreleased]

### Refactor:remove bytes/HexBytes
chixiaowen marked this conversation as resolved.
Show resolved Hide resolved

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.

* 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`

### Protobuf

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

### test

use `ctx.HeaderHash()` in `context_test.go` instead of `ctx.HeaderHash().Bytes()`
use `[]byte("xxx")` instead of `bytes.HexBytes([]byte("xxx"))`

### Modules

#### `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/evidence`

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

renamed to use `strings.ToUpper(hex.EncodeToString(evidence.Hash()))` instead of `evidence.Hash().String()`

as follows:

```go
func (k Keeper) SubmitEvidence(ctx sdk.Context, evidence exported.Evidence) error {
if _, ok := k.GetEvidence(ctx, evidence.Hash()); ok {
return errors.Wrap(types.ErrEvidenceExists, strings.ToUpper(hex.EncodeToString(evidence.Hash())))
}
if !k.router.HasRoute(evidence.Route()) {
return errors.Wrap(types.ErrNoEvidenceHandlerExists, evidence.Route())
}

handler := k.router.GetRoute(evidence.Route())
if err := handler(ctx, evidence); err != nil {
return errors.Wrap(types.ErrInvalidEvidence, err.Error())
}

ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeSubmitEvidence,
sdk.NewAttribute(types.AttributeKeyEvidenceHash, strings.ToUpper(hex.EncodeToString(evidence.Hash()))),
),
)

k.SetEvidence(ctx, evidence)
return nil
}
```

#### `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

## [Unreleased]

### Migration to CometBFT (Part 2)

The Cosmos SDK has migrated in, its previous versions, to CometBFT.
Expand Down
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
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
}

Expand Down Expand Up @@ -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,
Expand All @@ -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)
}
Expand Down
3 changes: 1 addition & 2 deletions client/rpc/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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
}
Expand Down
3 changes: 1 addition & 2 deletions proto/cosmos/evidence/v1beta1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -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.
//
Expand Down
5 changes: 2 additions & 3 deletions types/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion types/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
Loading