Skip to content

Commit

Permalink
[1760]: Remove all uses of any ModuleCdc variables, and delete them all.
Browse files Browse the repository at this point in the history
  • Loading branch information
SpicyLemon committed May 6, 2024
1 parent 98bf7a0 commit 3c6c635
Show file tree
Hide file tree
Showing 20 changed files with 88 additions and 129 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ require (
google.golang.org/grpc v1.63.2
google.golang.org/protobuf v1.34.0
gopkg.in/yaml.v2 v2.4.0
sigs.k8s.io/yaml v1.4.0
)

require (
Expand Down Expand Up @@ -205,7 +206,6 @@ require (
gotest.tools/v3 v3.5.1 // indirect
nhooyr.io/websocket v1.8.10 // indirect
pgregory.net/rapid v1.1.0 // indirect
sigs.k8s.io/yaml v1.4.0 // indirect
)

replace (
Expand Down
2 changes: 1 addition & 1 deletion x/attribute/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func (k Keeper) IterateRecords(ctx sdk.Context, prefix []byte, handle Handler) e
record := types.Attribute{}
// get proto objects for legacy prefix with legacy amino codec.
if bytes.Equal(types.AttributeKeyPrefixAmino, prefix) {

Check failure on line 104 in x/attribute/keeper/keeper.go

View workflow job for this annotation

GitHub Actions / golangci-lint

dupBranchBody: both branches in if statement have same body (gocritic)
if err := types.ModuleCdc.Unmarshal(iterator.Value(), &record); err != nil {
if err := k.cdc.Unmarshal(iterator.Value(), &record); err != nil {
return err
}
} else {
Expand Down
13 changes: 8 additions & 5 deletions x/attribute/simulation/operations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/stretchr/testify/suite"

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
Expand All @@ -25,6 +26,7 @@ type SimTestSuite struct {

ctx sdk.Context
app *app.App
cdc codec.BinaryCodec
}

func TestSimTestSuite(t *testing.T) {
Expand All @@ -34,6 +36,7 @@ func TestSimTestSuite(t *testing.T) {
func (s *SimTestSuite) SetupTest() {
s.app = app.Setup(s.T())
s.ctx = s.app.BaseApp.NewContext(false)
s.cdc = s.app.GetEncodingConfig().Marshaler
}

// LogOperationMsg logs all fields of the provided operationMsg.
Expand Down Expand Up @@ -137,7 +140,7 @@ func (s *SimTestSuite) TestSimulateMsgAddAttribute() {
s.LogOperationMsg(operationMsg)

var msg types.MsgAddAttributeRequest
s.Require().NoError(types.ModuleCdc.Unmarshal(operationMsg.Msg, &msg), "UnmarshalJSON(operationMsg.Msg)")
s.Require().NoError(s.cdc.Unmarshal(operationMsg.Msg, &msg), "UnmarshalJSON(operationMsg.Msg)")

s.Assert().True(operationMsg.OK, "operationMsg.OK")
s.Assert().Equal("cosmos1tnh2q55v8wyygtt9srz5safamzdengsnqeycj3", msg.Account, "msg.Account")
Expand Down Expand Up @@ -169,7 +172,7 @@ func (s *SimTestSuite) TestSimulateMsgUpdateAttribute() {
s.LogOperationMsg(operationMsg)

var msg types.MsgUpdateAttributeRequest
s.Require().NoError(types.ModuleCdc.Unmarshal(operationMsg.Msg, &msg), "UnmarshalJSON(operationMsg.Msg)")
s.Require().NoError(s.cdc.Unmarshal(operationMsg.Msg, &msg), "UnmarshalJSON(operationMsg.Msg)")

s.Assert().True(operationMsg.OK, "operationMsg.OK")
s.Assert().Equal(sdk.MsgTypeURL(&msg), operationMsg.Name, "operationMsg.Name")
Expand Down Expand Up @@ -199,7 +202,7 @@ func (s *SimTestSuite) TestSimulateMsgDeleteAttribute() {
s.LogOperationMsg(operationMsg)

var msg types.MsgDeleteAttributeRequest
s.Require().NoError(types.ModuleCdc.Unmarshal(operationMsg.Msg, &msg), "UnmarshalJSON(operationMsg.Msg)")
s.Require().NoError(s.cdc.Unmarshal(operationMsg.Msg, &msg), "UnmarshalJSON(operationMsg.Msg)")

s.Assert().True(operationMsg.OK, "operationMsg.OK")
s.Assert().Equal(sdk.MsgTypeURL(&msg), operationMsg.Name, "operationMsg.Name")
Expand Down Expand Up @@ -229,7 +232,7 @@ func (s *SimTestSuite) TestSimulateMsgDeleteDistinctAttribute() {
s.LogOperationMsg(operationMsg)

var msg types.MsgDeleteDistinctAttributeRequest
s.Require().NoError(types.ModuleCdc.Unmarshal(operationMsg.Msg, &msg), "UnmarshalJSON(operationMsg.Msg)")
s.Require().NoError(s.cdc.Unmarshal(operationMsg.Msg, &msg), "UnmarshalJSON(operationMsg.Msg)")

s.Assert().True(operationMsg.OK, "operationMsg.OK")
s.Assert().Equal(sdk.MsgTypeURL(&msg), operationMsg.Name, "operationMsg.Name")
Expand All @@ -253,7 +256,7 @@ func (s *SimTestSuite) TestSimulateMsgSetAccountData() {
s.LogOperationMsg(operationMsg)

var msg types.MsgSetAccountDataRequest
s.Require().NoError(types.ModuleCdc.Unmarshal(operationMsg.Msg, &msg), "UnmarshalJSON(operationMsg.Msg)")
s.Require().NoError(s.cdc.Unmarshal(operationMsg.Msg, &msg), "UnmarshalJSON(operationMsg.Msg)")

s.Assert().True(operationMsg.OK, "operationMsg.OK")
s.Assert().Equal(sdk.MsgTypeURL(&msg), operationMsg.Name, "operationMsg.Name")
Expand Down
11 changes: 0 additions & 11 deletions x/attribute/types/codec.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package types

import (
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/msgservice"
Expand All @@ -15,13 +14,3 @@ func RegisterInterfaces(registry types.InterfaceRegistry) {
registry.RegisterImplementations((*sdk.Msg)(nil), messages...)
msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc)
}

var (
// ModuleCdc references the global x/attribute module codec. Note, the codec should
// ONLY be used in certain instances of tests and for JSON encoding as Amino is
// still used for that purpose.
//
// The actual codec used for serialization should be provided to x/attribute and
// defined at the application level.
ModuleCdc = codec.NewProtoCodec(types.NewInterfaceRegistry())
)
4 changes: 1 addition & 3 deletions x/ibchooks/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ func (AppModuleBasic) Name() string {
}

// RegisterLegacyAminoCodec registers the ibchooks module's types on the given LegacyAmino codec.
func (AppModuleBasic) RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {
types.RegisterCodec(cdc)
}
func (AppModuleBasic) RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {}

Check failure on line 48 in x/ibchooks/module.go

View workflow job for this annotation

GitHub Actions / golangci-lint

unused-parameter: parameter 'cdc' seems to be unused, consider removing or renaming it as _ (revive)

// RegisterInterfaces registers the module's interface types
func (a AppModuleBasic) RegisterInterfaces(reg cdctypes.InterfaceRegistry) {
Expand Down
19 changes: 0 additions & 19 deletions x/ibchooks/types/codec.go
Original file line number Diff line number Diff line change
@@ -1,35 +1,16 @@
package types

import (
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/msgservice"
"github.com/cosmos/gogoproto/proto"
)

func RegisterCodec(cdc *codec.LegacyAmino) {
cdc.RegisterConcrete(&MsgEmitIBCAck{}, "provenance/ibchooks/v1/emit-ibc-ack", nil)
}

// RegisterInterfaces registers concrete implementations for this module.
func RegisterInterfaces(registry types.InterfaceRegistry) {
messages := make([]proto.Message, len(AllRequestMsgs))
copy(messages, AllRequestMsgs)
registry.RegisterImplementations((*sdk.Msg)(nil), messages...)
msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc)
}

var (
amino = codec.NewLegacyAmino()
ModuleCdc = codec.NewProtoCodec(types.NewInterfaceRegistry())
)

func init() {
RegisterCodec(amino)
// Register all Amino interfaces and concrete types on the authz Amino codec so that this can later be
// used to properly serialize MsgGrant and MsgExec instances
sdk.RegisterLegacyAminoCodec(amino)

amino.Seal()
}
6 changes: 4 additions & 2 deletions x/marker/simulation/operations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type SimTestSuite struct {

ctx sdk.Context
app *app.App
cdc *codec.ProtoCodec
}

func TestSimTestSuite(t *testing.T) {
Expand All @@ -41,6 +42,7 @@ func (s *SimTestSuite) SetupTest() {
govtypes.DefaultMinDepositRatio = sdkmath.LegacyZeroDec()
s.app = app.Setup(s.T())
s.ctx = s.app.BaseApp.NewContext(false)
s.cdc = codec.NewProtoCodec(s.app.GetEncodingConfig().InterfaceRegistry)
}

// LogOperationMsg logs all fields of the provided operationMsg.
Expand Down Expand Up @@ -150,7 +152,7 @@ func (s *SimTestSuite) TestSimulateMsgAddMarker() {
s.Require().NoError(err)

var msg types.MsgAddMarkerRequest
types.ModuleCdc.UnmarshalJSON(operationMsg.Msg, &msg)
s.cdc.UnmarshalJSON(operationMsg.Msg, &msg)

s.Require().True(operationMsg.OK, operationMsg.String())
s.Require().Equal(sdk.MsgTypeURL(&msg), operationMsg.Name)
Expand All @@ -172,7 +174,7 @@ func (s *SimTestSuite) TestSimulateMsgAddActivateFinalizeMarker() {
s.Require().NoError(err)

var msg types.MsgAddFinalizeActivateMarkerRequest
types.ModuleCdc.Unmarshal(operationMsg.Msg, &msg)
s.cdc.Unmarshal(operationMsg.Msg, &msg)

s.Require().True(operationMsg.OK, operationMsg.String())
s.Require().Equal(sdk.MsgTypeURL(&msg), operationMsg.Name)
Expand Down
11 changes: 0 additions & 11 deletions x/marker/types/codec.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package types

import (
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/msgservice"
Expand Down Expand Up @@ -60,13 +59,3 @@ func RegisterInterfaces(registry types.InterfaceRegistry) {

msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc)
}

var (
// ModuleCdc references the global x/account module codec. Note, the codec should
// ONLY be used in certain instances of tests and for JSON encoding as Amino is
// still used for that purpose.
//
// The actual codec used for serialization should be provided to x/account and
// defined at the application level.
ModuleCdc = codec.NewProtoCodec(types.NewInterfaceRegistry())
)
11 changes: 0 additions & 11 deletions x/metadata/types/codec.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package types

import (
"github.com/cosmos/cosmos-sdk/codec"
cdctypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/msgservice"
Expand All @@ -22,13 +21,3 @@ func RegisterInterfaces(registry cdctypes.InterfaceRegistry) {
)
msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc)
}

var (
// ModuleCdc references the global x/metadata module codec. Note, the codec should
// ONLY be used in certain instances of tests and for JSON encoding as Amino is
// still used for that purpose.
//
// The actual codec used for serialization should be provided to x/metadata and
// defined at the application level.
ModuleCdc = codec.NewProtoCodec(cdctypes.NewInterfaceRegistry())
)
66 changes: 48 additions & 18 deletions x/metadata/types/msgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import (
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v2"
"sigs.k8s.io/yaml"

"github.com/cosmos/cosmos-sdk/codec"
cdctypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/provenance-io/provenance/app"
"github.com/provenance-io/provenance/testutil"

. "github.com/provenance-io/provenance/x/metadata/types"
Expand All @@ -29,6 +30,19 @@ func getTypeName(thing interface{}) string {
return rv[lastDot+1:]
}

// cdc holds a fully built codec.
// Do not use this variable directly, instead use the GetCdc function to get it.
var cdc *codec.ProtoCodec

// GetCdc returns the codec, creating it if needed.
func GetCdc(t *testing.T) *codec.ProtoCodec {
if cdc == nil {
encCfg := app.MakeTestEncodingConfig(t)
cdc = codec.NewProtoCodec(encCfg.InterfaceRegistry)
}
return cdc
}

func TestAllMsgsGetSigners(t *testing.T) {
singleSignerMsgMakers := []testutil.MsgMaker{
func(signer string) sdk.Msg {
Expand Down Expand Up @@ -82,29 +96,45 @@ func TestWriteScopeRoute(t *testing.T) {
var msg = NewMsgWriteScopeRequest(*scope, []string{}, 0)

require.Equal(t, sdk.MsgTypeURL(msg), "/provenance.metadata.v1.MsgWriteScopeRequest")

expectedJSON := "{" +
"\"scope\":{" +
"\"scope_id\":\"scope1qzxcpvj6czy5g354dews3nlruxjsahhnsp\"," +
"\"specification_id\":\"scopespec1qs30c9axgrw5669ft0kffe6h9gysfe58v3\"," +
"\"owners\":[{\"address\":\"data_owner\",\"role\":\"PARTY_TYPE_OWNER\",\"optional\":false}]," +
"\"data_access\":[\"data_accessor\"]," +
"\"value_owner_address\":\"value_owner\"," +
"\"require_party_rollup\":false" +
"}," +
"\"signers\":[]," +
"\"scope_uuid\":\"\"," +
"\"spec_uuid\":\"\"," +
"\"usd_mills\":\"0\"" +
"}"

expectedYaml := `scope:
scope_id: scope1qzxcpvj6czy5g354dews3nlruxjsahhnsp
specification_id: scopespec1qs30c9axgrw5669ft0kffe6h9gysfe58v3
data_access:
- data_accessor
owners:
- address: data_owner
role: 5
optional: false
data_access:
- data_accessor
value_owner_address: value_owner
role: PARTY_TYPE_OWNER
require_party_rollup: false
signers: []
scope_id: scope1qzxcpvj6czy5g354dews3nlruxjsahhnsp
specification_id: scopespec1qs30c9axgrw5669ft0kffe6h9gysfe58v3
value_owner_address: value_owner
scope_uuid: ""
signers: []
spec_uuid: ""
usdmills: 0
usd_mills: "0"
`
bz, err := yaml.Marshal(msg)
require.NoError(t, err, "yaml.Marshal(msg)")
assert.Equal(t, expectedYaml, string(bz), "scope as yaml")
jsonBZ, err := GetCdc(t).MarshalJSON(msg)
require.NoError(t, err, "MarshalJSON(msg)")
assert.Equal(t, expectedJSON, string(jsonBZ), "scope as json")

bz, err = ModuleCdc.MarshalJSON(msg)
require.NoError(t, err, "ModuleCdc.MarshalJSON(msg)")
assert.Equal(t, "{\"scope\":{\"scope_id\":\"scope1qzxcpvj6czy5g354dews3nlruxjsahhnsp\",\"specification_id\":\"scopespec1qs30c9axgrw5669ft0kffe6h9gysfe58v3\",\"owners\":[{\"address\":\"data_owner\",\"role\":\"PARTY_TYPE_OWNER\",\"optional\":false}],\"data_access\":[\"data_accessor\"],\"value_owner_address\":\"value_owner\",\"require_party_rollup\":false},\"signers\":[],\"scope_uuid\":\"\",\"spec_uuid\":\"\",\"usd_mills\":\"0\"}", string(bz))
yamlBZ, err := yaml.JSONToYAML(jsonBZ)
require.NoError(t, err, "yaml.JSONToYAML(jsonBZ)")
assert.Equal(t, expectedYaml, string(yamlBZ), "scope as yaml")
}

func TestWriteScopeValidation(t *testing.T) {
Expand Down Expand Up @@ -721,7 +751,7 @@ func TestBindOSLocator(t *testing.T) {

require.Equal(t, "/provenance.metadata.v1.MsgBindOSLocatorRequest", sdk.MsgTypeURL(bindRequestMsg))

bz, _ := ModuleCdc.MarshalJSON(bindRequestMsg)
bz, _ := GetCdc(t).MarshalJSON(bindRequestMsg)
require.Equal(t, "{\"locator\":{\"owner\":\"cosmos1sh49f6ze3vn7cdl2amh2gnc70z5mten3y08xck\",\"locator_uri\":\"http://foo.com\",\"encryption_key\":\"\"}}", string(bz))
}

Expand All @@ -733,7 +763,7 @@ func TestModifyOSLocator(t *testing.T) {

require.Equal(t, "/provenance.metadata.v1.MsgModifyOSLocatorRequest", sdk.MsgTypeURL(modifyRequest))

bz, _ := ModuleCdc.MarshalJSON(modifyRequest)
bz, _ := GetCdc(t).MarshalJSON(modifyRequest)
require.Equal(t, "{\"locator\":{\"owner\":\"cosmos1sh49f6ze3vn7cdl2amh2gnc70z5mten3y08xck\",\"locator_uri\":\"http://foo.com\",\"encryption_key\":\"\"}}", string(bz))
}

Expand All @@ -745,7 +775,7 @@ func TestDeleteOSLocator(t *testing.T) {

require.Equal(t, "/provenance.metadata.v1.MsgDeleteOSLocatorRequest", sdk.MsgTypeURL(deleteRequest))

bz, _ := ModuleCdc.MarshalJSON(deleteRequest)
bz, _ := GetCdc(t).MarshalJSON(deleteRequest)
require.Equal(t, "{\"locator\":{\"owner\":\"cosmos1sh49f6ze3vn7cdl2amh2gnc70z5mten3y08xck\",\"locator_uri\":\"http://foo.com\",\"encryption_key\":\"\"}}", string(bz))
}

Expand Down
7 changes: 0 additions & 7 deletions x/msgfees/types/codec.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package types

import (
"github.com/cosmos/cosmos-sdk/codec"
cdctypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
govtypesv1beta1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
Expand All @@ -23,9 +22,3 @@ func RegisterInterfaces(registry cdctypes.InterfaceRegistry) {
&UpdateConversionFeeDenomProposal{},
)
}

var (
// moving to protoCodec since this is a new module and should not use the
// amino codec..someone to double verify
ModuleCdc = codec.NewProtoCodec(cdctypes.NewInterfaceRegistry())
)
Loading

0 comments on commit 3c6c635

Please sign in to comment.