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 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
Expand Up @@ -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.
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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`

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
5 changes: 2 additions & 3 deletions types/result_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
}
Expand Down Expand Up @@ -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{
Expand Down
Loading