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

Remove hybrid codec usage #6843

Merged
merged 11 commits into from
Jul 25, 2020
16 changes: 8 additions & 8 deletions x/auth/keeper/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,24 @@ import (
)

// NewQuerier creates a querier for auth REST endpoints
func NewQuerier(k AccountKeeper, cdc codec.JSONMarshaler) sdk.Querier {
func NewQuerier(k AccountKeeper, legacyQuerierCdc codec.JSONMarshaler) sdk.Querier {
return func(ctx sdk.Context, path []string, req abci.RequestQuery) ([]byte, error) {
switch path[0] {
case types.QueryAccount:
return queryAccount(ctx, req, k, cdc)
return queryAccount(ctx, req, k, legacyQuerierCdc)

case types.QueryParams:
return queryParams(ctx, k, cdc)
return queryParams(ctx, k, legacyQuerierCdc)

default:
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unknown query path: %s", path[0])
}
}
}

func queryAccount(ctx sdk.Context, req abci.RequestQuery, k AccountKeeper, cdc codec.JSONMarshaler) ([]byte, error) {
func queryAccount(ctx sdk.Context, req abci.RequestQuery, k AccountKeeper, legacyQuerierCdc codec.JSONMarshaler) ([]byte, error) {
var params types.QueryAccountRequest
if err := cdc.UnmarshalJSON(req.Data, &params); err != nil {
if err := legacyQuerierCdc.UnmarshalJSON(req.Data, &params); err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
}

Expand All @@ -36,18 +36,18 @@ func queryAccount(ctx sdk.Context, req abci.RequestQuery, k AccountKeeper, cdc c
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownAddress, "account %s does not exist", params.Address)
}

bz, err := codec.MarshalJSONIndent(cdc, account)
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, account)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}

return bz, nil
}

func queryParams(ctx sdk.Context, k AccountKeeper, cdc codec.JSONMarshaler) ([]byte, error) {
func queryParams(ctx sdk.Context, k AccountKeeper, legacyQuerierCdc codec.JSONMarshaler) ([]byte, error) {
params := k.GetParams(ctx)

res, err := codec.MarshalJSONIndent(cdc, params)
res, err := codec.MarshalJSONIndent(legacyQuerierCdc, params)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
Expand Down
10 changes: 5 additions & 5 deletions x/auth/keeper/querier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ import (

func TestQueryAccount(t *testing.T) {
app, ctx := createTestApp(true)
jsonCdc := codec.NewAminoCodec(app.Codec())
legacyQuerierCdc := codec.NewAminoCodec(app.Codec())

req := abci.RequestQuery{
Path: "",
Data: []byte{},
}

path := []string{types.QueryAccount}
querier := keep.NewQuerier(app.AccountKeeper, jsonCdc)
querier := keep.NewQuerier(app.AccountKeeper, legacyQuerierCdc)

bz, err := querier(ctx, []string{"other"}, req)
require.Error(t, err)
Expand All @@ -39,13 +39,13 @@ func TestQueryAccount(t *testing.T) {
require.Error(t, err)
require.Nil(t, res)

req.Data = jsonCdc.MustMarshalJSON(types.QueryAccountRequest{Address: []byte("")})
req.Data = legacyQuerierCdc.MustMarshalJSON(types.QueryAccountRequest{Address: []byte("")})
res, err = querier(ctx, path, req)
require.Error(t, err)
require.Nil(t, res)

_, _, addr := testdata.KeyTestPubAddr()
req.Data = jsonCdc.MustMarshalJSON(types.QueryAccountRequest{Address: addr})
req.Data = legacyQuerierCdc.MustMarshalJSON(types.QueryAccountRequest{Address: addr})
res, err = querier(ctx, path, req)
require.Error(t, err)
require.Nil(t, res)
Expand All @@ -60,6 +60,6 @@ func TestQueryAccount(t *testing.T) {
require.NotNil(t, res)

var account types.AccountI
err2 := jsonCdc.UnmarshalJSON(res, &account)
err2 := legacyQuerierCdc.UnmarshalJSON(res, &account)
require.Nil(t, err2)
}
4 changes: 2 additions & 2 deletions x/auth/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ func (AppModule) QuerierRoute() string {
}

// LegacyQuerierHandler returns the auth module sdk.Querier.
func (am AppModule) LegacyQuerierHandler(jsonCdc codec.JSONMarshaler) sdk.Querier {
return keeper.NewQuerier(am.accountKeeper, jsonCdc)
func (am AppModule) LegacyQuerierHandler(legacyQuerierCdc codec.JSONMarshaler) sdk.Querier {
return keeper.NewQuerier(am.accountKeeper, legacyQuerierCdc)
}

// RegisterQueryService registers a GRPC query service to respond to the
Expand Down
34 changes: 17 additions & 17 deletions x/bank/keeper/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,65 +11,65 @@ import (
)

// NewQuerier returns a new sdk.Keeper instance.
func NewQuerier(k Keeper, cdc codec.JSONMarshaler) sdk.Querier {
func NewQuerier(k Keeper, legacyQuerierCdc codec.JSONMarshaler) sdk.Querier {
return func(ctx sdk.Context, path []string, req abci.RequestQuery) ([]byte, error) {
switch path[0] {
case types.QueryBalance:
return queryBalance(ctx, req, k, cdc)
return queryBalance(ctx, req, k, legacyQuerierCdc)

case types.QueryAllBalances:
return queryAllBalance(ctx, req, k, cdc)
return queryAllBalance(ctx, req, k, legacyQuerierCdc)

case types.QueryTotalSupply:
return queryTotalSupply(ctx, req, k, cdc)
return queryTotalSupply(ctx, req, k, legacyQuerierCdc)

case types.QuerySupplyOf:
return querySupplyOf(ctx, req, k, cdc)
return querySupplyOf(ctx, req, k, legacyQuerierCdc)

default:
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unknown %s query endpoint: %s", types.ModuleName, path[0])
}
}
}

func queryBalance(ctx sdk.Context, req abci.RequestQuery, k Keeper, cdc codec.JSONMarshaler) ([]byte, error) {
func queryBalance(ctx sdk.Context, req abci.RequestQuery, k Keeper, legacyQuerierCdc codec.JSONMarshaler) ([]byte, error) {
var params types.QueryBalanceRequest

if err := cdc.UnmarshalJSON(req.Data, &params); err != nil {
if err := legacyQuerierCdc.UnmarshalJSON(req.Data, &params); err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
}

balance := k.GetBalance(ctx, params.Address, params.Denom)

bz, err := codec.MarshalJSONIndent(cdc, balance)
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, balance)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}

return bz, nil
}

func queryAllBalance(ctx sdk.Context, req abci.RequestQuery, k Keeper, cdc codec.JSONMarshaler) ([]byte, error) {
func queryAllBalance(ctx sdk.Context, req abci.RequestQuery, k Keeper, legacyQuerierCdc codec.JSONMarshaler) ([]byte, error) {
var params types.QueryAllBalancesRequest

if err := cdc.UnmarshalJSON(req.Data, &params); err != nil {
if err := legacyQuerierCdc.UnmarshalJSON(req.Data, &params); err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
}

balances := k.GetAllBalances(ctx, params.Address)

bz, err := codec.MarshalJSONIndent(types.ModuleCdc, balances)
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, balances)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}

return bz, nil
}

func queryTotalSupply(ctx sdk.Context, req abci.RequestQuery, k Keeper, cdc codec.JSONMarshaler) ([]byte, error) {
func queryTotalSupply(ctx sdk.Context, req abci.RequestQuery, k Keeper, legacyQuerierCdc codec.JSONMarshaler) ([]byte, error) {
var params types.QueryTotalSupplyParams

err := cdc.UnmarshalJSON(req.Data, &params)
err := legacyQuerierCdc.UnmarshalJSON(req.Data, &params)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
}
Expand All @@ -83,26 +83,26 @@ func queryTotalSupply(ctx sdk.Context, req abci.RequestQuery, k Keeper, cdc code
totalSupply = totalSupply[start:end]
}

res, err := cdc.MarshalJSON(totalSupply)
res, err := legacyQuerierCdc.MarshalJSON(totalSupply)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}

return res, nil
}

func querySupplyOf(ctx sdk.Context, req abci.RequestQuery, k Keeper, cdc codec.JSONMarshaler) ([]byte, error) {
func querySupplyOf(ctx sdk.Context, req abci.RequestQuery, k Keeper, legacyQuerierCdc codec.JSONMarshaler) ([]byte, error) {
var params types.QuerySupplyOfParams

err := cdc.UnmarshalJSON(req.Data, &params)
err := legacyQuerierCdc.UnmarshalJSON(req.Data, &params)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
}

amount := k.GetSupply(ctx).GetTotal().AmountOf(params.Denom)
supply := sdk.NewCoin(params.Denom, amount)

bz, err := codec.MarshalJSONIndent(cdc, supply)
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, supply)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
Expand Down
20 changes: 10 additions & 10 deletions x/bank/keeper/querier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ import (

func (suite *IntegrationTestSuite) TestQuerier_QueryBalance() {
app, ctx := suite.app, suite.ctx
jsonCdc := codec.NewAminoCodec(app.Codec())
legacyQuerierCdc := codec.NewAminoCodec(app.Codec())
_, _, addr := testdata.KeyTestPubAddr()
req := abci.RequestQuery{
Path: fmt.Sprintf("custom/%s/%s", types.ModuleName, types.QueryBalance),
Data: []byte{},
}

querier := keeper.NewQuerier(app.BankKeeper, jsonCdc)
querier := keeper.NewQuerier(app.BankKeeper, legacyQuerierCdc)

res, err := querier(ctx, []string{types.QueryBalance}, req)
suite.Require().NotNil(err)
Expand Down Expand Up @@ -52,14 +52,14 @@ func (suite *IntegrationTestSuite) TestQuerier_QueryBalance() {

func (suite *IntegrationTestSuite) TestQuerier_QueryAllBalances() {
app, ctx := suite.app, suite.ctx
jsonCdc := codec.NewAminoCodec(app.Codec())
legacyQuerierCdc := codec.NewAminoCodec(app.Codec())
_, _, addr := testdata.KeyTestPubAddr()
req := abci.RequestQuery{
Path: fmt.Sprintf("custom/%s/%s", types.ModuleName, types.QueryAllBalances),
Data: []byte{},
}

querier := keeper.NewQuerier(app.BankKeeper, jsonCdc)
querier := keeper.NewQuerier(app.BankKeeper, legacyQuerierCdc)

res, err := querier(ctx, []string{types.QueryAllBalances}, req)
suite.Require().NotNil(err)
Expand Down Expand Up @@ -89,7 +89,7 @@ func (suite *IntegrationTestSuite) TestQuerier_QueryAllBalances() {

func (suite *IntegrationTestSuite) TestQuerier_QueryTotalSupply() {
app, ctx := suite.app, suite.ctx
jsonCdc := codec.NewAminoCodec(app.Codec())
legacyQuerierCdc := codec.NewAminoCodec(app.Codec())
expectedTotalSupply := types.NewSupply(sdk.NewCoins(sdk.NewInt64Coin("test", 400000000)))
app.BankKeeper.SetSupply(ctx, expectedTotalSupply)

Expand All @@ -98,7 +98,7 @@ func (suite *IntegrationTestSuite) TestQuerier_QueryTotalSupply() {
Data: []byte{},
}

querier := keeper.NewQuerier(app.BankKeeper, jsonCdc)
querier := keeper.NewQuerier(app.BankKeeper, legacyQuerierCdc)

res, err := querier(ctx, []string{types.QueryTotalSupply}, req)
suite.Require().NotNil(err)
Expand All @@ -116,7 +116,7 @@ func (suite *IntegrationTestSuite) TestQuerier_QueryTotalSupply() {

func (suite *IntegrationTestSuite) TestQuerier_QueryTotalSupplyOf() {
app, ctx := suite.app, suite.ctx
jsonCdc := codec.NewAminoCodec(app.Codec())
legacyQuerierCdc := codec.NewAminoCodec(app.Codec())

test1Supply := sdk.NewInt64Coin("test1", 4000000)
test2Supply := sdk.NewInt64Coin("test2", 700000000)
Expand All @@ -128,7 +128,7 @@ func (suite *IntegrationTestSuite) TestQuerier_QueryTotalSupplyOf() {
Data: []byte{},
}

querier := keeper.NewQuerier(app.BankKeeper, jsonCdc)
querier := keeper.NewQuerier(app.BankKeeper, legacyQuerierCdc)

res, err := querier(ctx, []string{types.QuerySupplyOf}, req)
suite.Require().NotNil(err)
Expand All @@ -146,13 +146,13 @@ func (suite *IntegrationTestSuite) TestQuerier_QueryTotalSupplyOf() {

func (suite *IntegrationTestSuite) TestQuerierRouteNotFound() {
app, ctx := suite.app, suite.ctx
jsonCdc := codec.NewAminoCodec(app.Codec())
legacyQuerierCdc := codec.NewAminoCodec(app.Codec())
req := abci.RequestQuery{
Path: fmt.Sprintf("custom/%s/invalid", types.ModuleName),
Data: []byte{},
}

querier := keeper.NewQuerier(app.BankKeeper, jsonCdc)
querier := keeper.NewQuerier(app.BankKeeper, legacyQuerierCdc)
_, err := querier(ctx, []string{"invalid"}, req)
suite.Error(err)
}
4 changes: 2 additions & 2 deletions x/bank/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ func (am AppModule) Route() sdk.Route {
func (AppModule) QuerierRoute() string { return types.RouterKey }

// LegacyQuerierHandler returns the bank module sdk.Querier.
func (am AppModule) LegacyQuerierHandler(jsonCdc codec.JSONMarshaler) sdk.Querier {
return keeper.NewQuerier(am.keeper, jsonCdc)
func (am AppModule) LegacyQuerierHandler(legacyQuerierCdc codec.JSONMarshaler) sdk.Querier {
return keeper.NewQuerier(am.keeper, legacyQuerierCdc)
}

// InitGenesis performs genesis initialization for the bank module. It returns
Expand Down
6 changes: 3 additions & 3 deletions x/capability/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type (
// The keeper allows the ability to create scoped sub-keepers which are tied to
// a single specific module.
Keeper struct {
cdc codec.Marshaler
cdc codec.BinaryMarshaler
storeKey sdk.StoreKey
memKey sdk.StoreKey
capMap map[uint64]*types.Capability
Expand All @@ -41,15 +41,15 @@ type (
// by name, in addition to creating new capabilities & authenticating capabilities
// passed by other modules.
ScopedKeeper struct {
cdc codec.Marshaler
cdc codec.BinaryMarshaler
storeKey sdk.StoreKey
memKey sdk.StoreKey
capMap map[uint64]*types.Capability
module string
}
)

func NewKeeper(cdc codec.Marshaler, storeKey, memKey sdk.StoreKey) *Keeper {
func NewKeeper(cdc codec.BinaryMarshaler, storeKey, memKey sdk.StoreKey) *Keeper {
return &Keeper{
cdc: cdc,
storeKey: storeKey,
Expand Down
Loading