From 8f9f0f17736f204135ed612ed2a95af2d3e3ae68 Mon Sep 17 00:00:00 2001 From: dongsam Date: Fri, 26 Jul 2024 14:33:49 +0900 Subject: [PATCH] fix!: add ommitted port conatract address on govshuttle genesis state --- proto/canto/govshuttle/v1/genesis.proto | 2 +- x/govshuttle/genesis.go | 26 +++--- x/govshuttle/genesis_test.go | 113 ++++++++++++++++++++++++ x/govshuttle/keeper/genesis_test.go | 8 +- x/govshuttle/types/genesis.go | 12 +-- x/govshuttle/types/genesis.pb.go | 57 ++++++------ 6 files changed, 162 insertions(+), 56 deletions(-) create mode 100644 x/govshuttle/genesis_test.go diff --git a/proto/canto/govshuttle/v1/genesis.proto b/proto/canto/govshuttle/v1/genesis.proto index b6028e230..6413d5ca2 100644 --- a/proto/canto/govshuttle/v1/genesis.proto +++ b/proto/canto/govshuttle/v1/genesis.proto @@ -10,6 +10,6 @@ option go_package = "github.com/Canto-Network/Canto/v7/x/govshuttle/types"; // GenesisState defines the govshuttle module's genesis state. message GenesisState { Params params = 1 [ (gogoproto.nullable) = false ]; - bytes port_address = 2; + string port_contract_addr = 2; // this line is used by starport scaffolding # genesis/proto/state } diff --git a/x/govshuttle/genesis.go b/x/govshuttle/genesis.go index 9c41dabbb..da24eec16 100644 --- a/x/govshuttle/genesis.go +++ b/x/govshuttle/genesis.go @@ -8,31 +8,29 @@ import ( "github.com/ethereum/go-ethereum/common" ) -// InitGenesis initializes the capability module's state from a provided genesis +// InitGenesis initializes the govshuttle module's state from a provided genesis // state. func InitGenesis(ctx sdk.Context, k keeper.Keeper, accountKeeper authkeeper.AccountKeeper, genState types.GenesisState) { - // this line is used by starport scaffolding # genesis/module/init k.SetParams(ctx, genState.Params) - if genState.PortAddress != nil { - k.SetPort(ctx, common.BytesToAddress(genState.PortAddress)) + + if genState.PortContractAddr != "" { + portAddr := common.HexToAddress(genState.PortContractAddr) + k.SetPort(ctx, portAddr) } + if acc := accountKeeper.GetModuleAccount(ctx, types.ModuleName); acc == nil { panic("the govshuttle module account has not been set") } - } -// ExportGenesis returns the capability module's exported genesis. +// ExportGenesis returns the govshuttle module's exported genesis. func ExportGenesis(ctx sdk.Context, k keeper.Keeper) *types.GenesisState { - portAddress, found := k.GetPort(ctx) - var genesis *types.GenesisState - if found { - genesis = types.NewGenesisState(k.GetParams(ctx), portAddress.Bytes()) - } else { - genesis = types.NewGenesisState(k.GetParams(ctx), nil) - } + genesis := types.DefaultGenesis() + genesis.Params = k.GetParams(ctx) - // this line is used by starport scaffolding # genesis/module/export + if portAddr, ok := k.GetPort(ctx); ok { + genesis.PortContractAddr = portAddr.String() + } return genesis } diff --git a/x/govshuttle/genesis_test.go b/x/govshuttle/genesis_test.go new file mode 100644 index 000000000..4cc3b88ed --- /dev/null +++ b/x/govshuttle/genesis_test.go @@ -0,0 +1,113 @@ +package govshuttle_test + +import ( + "testing" + + "github.com/stretchr/testify/require" + "github.com/stretchr/testify/suite" + + "github.com/cosmos/cosmos-sdk/simapp" + sdk "github.com/cosmos/cosmos-sdk/types" + + "github.com/tendermint/tendermint/libs/log" + tmproto "github.com/tendermint/tendermint/proto/tendermint/types" + dbm "github.com/tendermint/tm-db" + + "github.com/ethereum/go-ethereum/common" + "github.com/evmos/ethermint/encoding" + + "github.com/Canto-Network/Canto/v7/app" + "github.com/Canto-Network/Canto/v7/x/govshuttle" +) + +type GenesisTestSuite struct { + suite.Suite //top level testing suite + + appA *app.Canto + ctxA sdk.Context + + appB *app.Canto + ctxB sdk.Context +} + +var s *GenesisTestSuite + +func TestGenesisTestSuite(t *testing.T) { + s = new(GenesisTestSuite) + suite.Run(t, s) +} + +func (suite *GenesisTestSuite) DoSetupTest(t require.TestingT) { + + encCfg := encoding.MakeConfig(app.ModuleBasics) + + suite.appA = app.NewCanto( + log.NewNopLogger(), + dbm.NewMemDB(), + nil, + true, + make(map[int64]bool), + app.DefaultNodeHome, + 0, + false, + encCfg, + simapp.EmptyAppOptions{}, + ) + suite.ctxA = suite.appA.NewContext(true, tmproto.Header{}) + + suite.appB = app.NewCanto( + log.NewNopLogger(), + dbm.NewMemDB(), + nil, + true, + make(map[int64]bool), + app.DefaultNodeHome, + 0, + false, + encCfg, + simapp.EmptyAppOptions{}, + ) + suite.ctxB = suite.appB.NewContext(true, tmproto.Header{}) +} + +func (suite *GenesisTestSuite) SetupTest() { + suite.DoSetupTest(suite.T()) +} + +func (suite *GenesisTestSuite) TestGenesis() { + testCases := []struct { + name string + portAddr common.Address + malleate func(portAddr common.Address) + }{ + { + "empty port contract address", + common.Address{}, + func(_ common.Address) {}, + }, + { + "non-empty port contract address", + common.HexToAddress("0x648a5Aa0C4FbF2C1CF5a3B432c2766EeaF8E402d"), + func(portAddr common.Address) { + suite.appA.GovshuttleKeeper.SetPort(suite.ctxA, portAddr) + }, + }, + } + + for _, tc := range testCases { + suite.Run(tc.name, func() { + suite.SetupTest() + + tc.malleate(tc.portAddr) + + portAddr, _ := suite.appA.GovshuttleKeeper.GetPort(suite.ctxA) + suite.Require().Equal(tc.portAddr, portAddr) + + genesisState := govshuttle.ExportGenesis(suite.ctxA, suite.appA.GovshuttleKeeper) + + govshuttle.InitGenesis(suite.ctxB, suite.appB.GovshuttleKeeper, suite.appB.AccountKeeper, *genesisState) + portAddr, _ = suite.appB.GovshuttleKeeper.GetPort(suite.ctxB) + suite.Require().Equal(tc.portAddr, portAddr) + }) + } +} diff --git a/x/govshuttle/keeper/genesis_test.go b/x/govshuttle/keeper/genesis_test.go index 9ddbca948..2f29579ac 100644 --- a/x/govshuttle/keeper/genesis_test.go +++ b/x/govshuttle/keeper/genesis_test.go @@ -21,7 +21,7 @@ func (suite *KeeperTestSuite) TestImportExportGenesisEmpty() { _, found := suite.app.GovshuttleKeeper.GetPort(suite.ctx) suite.Require().False(found) genState := govshuttle.ExportGenesis(suite.ctx, suite.app.GovshuttleKeeper) - suite.Require().Nil(genState.PortAddress) + suite.Require().Equal(genState.PortContractAddr, "") // Copy genState to genState2 and init with it var genState2 types.GenesisState @@ -35,12 +35,12 @@ func (suite *KeeperTestSuite) TestImportExportGenesisEmpty() { genState3 := govshuttle.ExportGenesis(suite.ctx, suite.app.GovshuttleKeeper) suite.Equal(*genState, genState2) suite.Equal(genState2, *genState3) - suite.Require().Nil(genState.PortAddress) + suite.Require().Equal(genState.PortContractAddr, "") } func (suite *KeeperTestSuite) TestInitExportGenesis() { - portAddress := tests.GenerateAddress() - expGenesis := types.NewGenesisState(types.DefaultParams(), portAddress.Bytes()) + portContractAddr := tests.GenerateAddress() + expGenesis := types.NewGenesisState(types.DefaultParams(), portContractAddr.String()) govshuttle.InitGenesis(suite.ctx, suite.app.GovshuttleKeeper, suite.app.AccountKeeper, *expGenesis) genState := govshuttle.ExportGenesis(suite.ctx, suite.app.GovshuttleKeeper) diff --git a/x/govshuttle/types/genesis.go b/x/govshuttle/types/genesis.go index aa6e9e469..8756b0829 100644 --- a/x/govshuttle/types/genesis.go +++ b/x/govshuttle/types/genesis.go @@ -1,18 +1,14 @@ package types -// DefaultIndex is the default capability global index -const DefaultIndex uint64 = 1 - // DefaultGenesis returns the default Capability genesis state func DefaultGenesis() *GenesisState { - return NewGenesisState(DefaultParams(), nil) + return NewGenesisState(DefaultParams(), "") } -func NewGenesisState(params Params, portAddress []byte) *GenesisState { +func NewGenesisState(params Params, portContractAddr string) *GenesisState { return &GenesisState{ - Params: params, - PortAddress: portAddress, - // this line is used by starport scaffolding # genesis/types/init + Params: params, + PortContractAddr: portContractAddr, } } diff --git a/x/govshuttle/types/genesis.pb.go b/x/govshuttle/types/genesis.pb.go index 45929555b..0a85813af 100644 --- a/x/govshuttle/types/genesis.pb.go +++ b/x/govshuttle/types/genesis.pb.go @@ -25,8 +25,8 @@ const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package // GenesisState defines the govshuttle module's genesis state. type GenesisState struct { - Params Params `protobuf:"bytes,1,opt,name=params,proto3" json:"params"` - PortAddress []byte `protobuf:"bytes,2,opt,name=port_address,json=portAddress,proto3" json:"port_address,omitempty"` + Params Params `protobuf:"bytes,1,opt,name=params,proto3" json:"params"` + PortContractAddr string `protobuf:"bytes,2,opt,name=port_contract_addr,json=portContractAddr,proto3" json:"port_contract_addr,omitempty"` } func (m *GenesisState) Reset() { *m = GenesisState{} } @@ -69,11 +69,11 @@ func (m *GenesisState) GetParams() Params { return Params{} } -func (m *GenesisState) GetPortAddress() []byte { +func (m *GenesisState) GetPortContractAddr() string { if m != nil { - return m.PortAddress + return m.PortContractAddr } - return nil + return "" } func init() { @@ -83,22 +83,23 @@ func init() { func init() { proto.RegisterFile("canto/govshuttle/v1/genesis.proto", fileDescriptor_356493b1fc5972f0) } var fileDescriptor_356493b1fc5972f0 = []byte{ - // 237 bytes of a gzipped FileDescriptorProto + // 247 bytes of a gzipped FileDescriptorProto 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x52, 0x4c, 0x4e, 0xcc, 0x2b, 0xc9, 0xd7, 0x4f, 0xcf, 0x2f, 0x2b, 0xce, 0x28, 0x2d, 0x29, 0xc9, 0x49, 0xd5, 0x2f, 0x33, 0xd4, 0x4f, 0x4f, 0xcd, 0x4b, 0x2d, 0xce, 0x2c, 0xd6, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x12, 0x06, 0x2b, 0xd1, 0x43, 0x28, 0xd1, 0x2b, 0x33, 0x94, 0x12, 0x49, 0xcf, 0x4f, 0xcf, 0x07, 0xcb, 0xeb, - 0x83, 0x58, 0x10, 0xa5, 0x52, 0x2a, 0x58, 0x4d, 0x43, 0x68, 0x04, 0xab, 0x52, 0xca, 0xe1, 0xe2, + 0x83, 0x58, 0x10, 0xa5, 0x52, 0x2a, 0x58, 0x4d, 0x43, 0x68, 0x04, 0xab, 0x52, 0x2a, 0xe7, 0xe2, 0x71, 0x87, 0xd8, 0x10, 0x5c, 0x92, 0x58, 0x92, 0x2a, 0x64, 0xc9, 0xc5, 0x56, 0x90, 0x58, 0x94, 0x98, 0x5b, 0x2c, 0xc1, 0xa8, 0xc0, 0xa8, 0xc1, 0x6d, 0x24, 0xad, 0x87, 0xc5, 0x46, 0xbd, 0x00, - 0xb0, 0x12, 0x27, 0x96, 0x13, 0xf7, 0xe4, 0x19, 0x82, 0xa0, 0x1a, 0x84, 0x14, 0xb9, 0x78, 0x0a, - 0xf2, 0x8b, 0x4a, 0xe2, 0x13, 0x53, 0x52, 0x8a, 0x52, 0x8b, 0x8b, 0x25, 0x98, 0x14, 0x18, 0x35, - 0x78, 0x82, 0xb8, 0x41, 0x62, 0x8e, 0x10, 0x21, 0x27, 0xbf, 0x13, 0x8f, 0xe4, 0x18, 0x2f, 0x3c, - 0x92, 0x63, 0x7c, 0xf0, 0x48, 0x8e, 0x71, 0xc2, 0x63, 0x39, 0x86, 0x0b, 0x8f, 0xe5, 0x18, 0x6e, - 0x3c, 0x96, 0x63, 0x88, 0x32, 0x49, 0xcf, 0x2c, 0xc9, 0x28, 0x4d, 0xd2, 0x4b, 0xce, 0xcf, 0xd5, - 0x77, 0x06, 0xd9, 0xa8, 0xeb, 0x97, 0x5a, 0x52, 0x9e, 0x5f, 0x94, 0x0d, 0xe1, 0xe9, 0x97, 0x99, - 0xeb, 0x57, 0x20, 0xfb, 0xa5, 0xa4, 0xb2, 0x20, 0xb5, 0x38, 0x89, 0x0d, 0xec, 0x09, 0x63, 0x40, - 0x00, 0x00, 0x00, 0xff, 0xff, 0xb7, 0x01, 0xeb, 0xfd, 0x3a, 0x01, 0x00, 0x00, + 0xb0, 0x12, 0x27, 0x96, 0x13, 0xf7, 0xe4, 0x19, 0x82, 0xa0, 0x1a, 0x84, 0x74, 0xb8, 0x84, 0x0a, + 0xf2, 0x8b, 0x4a, 0xe2, 0x93, 0xf3, 0xf3, 0x4a, 0x8a, 0x12, 0x93, 0x4b, 0xe2, 0x13, 0x53, 0x52, + 0x8a, 0x24, 0x98, 0x14, 0x18, 0x35, 0x38, 0x83, 0x04, 0x40, 0x32, 0xce, 0x50, 0x09, 0xc7, 0x94, + 0x94, 0x22, 0x27, 0xbf, 0x13, 0x8f, 0xe4, 0x18, 0x2f, 0x3c, 0x92, 0x63, 0x7c, 0xf0, 0x48, 0x8e, + 0x71, 0xc2, 0x63, 0x39, 0x86, 0x0b, 0x8f, 0xe5, 0x18, 0x6e, 0x3c, 0x96, 0x63, 0x88, 0x32, 0x49, + 0xcf, 0x2c, 0xc9, 0x28, 0x4d, 0xd2, 0x4b, 0xce, 0xcf, 0xd5, 0x77, 0x06, 0x59, 0xae, 0xeb, 0x97, + 0x5a, 0x52, 0x9e, 0x5f, 0x94, 0x0d, 0xe1, 0xe9, 0x97, 0x99, 0xeb, 0x57, 0x20, 0x7b, 0xab, 0xa4, + 0xb2, 0x20, 0xb5, 0x38, 0x89, 0x0d, 0xec, 0x1f, 0x63, 0x40, 0x00, 0x00, 0x00, 0xff, 0xff, 0x66, + 0x33, 0x38, 0xcd, 0x45, 0x01, 0x00, 0x00, } func (m *GenesisState) Marshal() (dAtA []byte, err error) { @@ -121,10 +122,10 @@ func (m *GenesisState) MarshalToSizedBuffer(dAtA []byte) (int, error) { _ = i var l int _ = l - if len(m.PortAddress) > 0 { - i -= len(m.PortAddress) - copy(dAtA[i:], m.PortAddress) - i = encodeVarintGenesis(dAtA, i, uint64(len(m.PortAddress))) + if len(m.PortContractAddr) > 0 { + i -= len(m.PortContractAddr) + copy(dAtA[i:], m.PortContractAddr) + i = encodeVarintGenesis(dAtA, i, uint64(len(m.PortContractAddr))) i-- dAtA[i] = 0x12 } @@ -160,7 +161,7 @@ func (m *GenesisState) Size() (n int) { _ = l l = m.Params.Size() n += 1 + l + sovGenesis(uint64(l)) - l = len(m.PortAddress) + l = len(m.PortContractAddr) if l > 0 { n += 1 + l + sovGenesis(uint64(l)) } @@ -237,9 +238,9 @@ func (m *GenesisState) Unmarshal(dAtA []byte) error { iNdEx = postIndex case 2: if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field PortAddress", wireType) + return fmt.Errorf("proto: wrong wireType = %d for field PortContractAddr", wireType) } - var byteLen int + var stringLen uint64 for shift := uint(0); ; shift += 7 { if shift >= 64 { return ErrIntOverflowGenesis @@ -249,25 +250,23 @@ func (m *GenesisState) Unmarshal(dAtA []byte) error { } b := dAtA[iNdEx] iNdEx++ - byteLen |= int(b&0x7F) << shift + stringLen |= uint64(b&0x7F) << shift if b < 0x80 { break } } - if byteLen < 0 { + intStringLen := int(stringLen) + if intStringLen < 0 { return ErrInvalidLengthGenesis } - postIndex := iNdEx + byteLen + postIndex := iNdEx + intStringLen if postIndex < 0 { return ErrInvalidLengthGenesis } if postIndex > l { return io.ErrUnexpectedEOF } - m.PortAddress = append(m.PortAddress[:0], dAtA[iNdEx:postIndex]...) - if m.PortAddress == nil { - m.PortAddress = []byte{} - } + m.PortContractAddr = string(dAtA[iNdEx:postIndex]) iNdEx = postIndex default: iNdEx = preIndex