Skip to content
This repository has been archived by the owner on Jul 30, 2024. It is now read-only.

Commit

Permalink
Merge pull request #2 from b-harvest/abci1.0/patch/2-add-ommitted-gen…
Browse files Browse the repository at this point in the history
…state-govshuttle

fix!: add omitted port conatract address on govshuttle genesis state
  • Loading branch information
dongsam authored Jul 29, 2024
2 parents febe40a + 8f9f0f1 commit d32085f
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 56 deletions.
2 changes: 1 addition & 1 deletion proto/canto/govshuttle/v1/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
26 changes: 12 additions & 14 deletions x/govshuttle/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
113 changes: 113 additions & 0 deletions x/govshuttle/genesis_test.go
Original file line number Diff line number Diff line change
@@ -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)
})
}
}
8 changes: 4 additions & 4 deletions x/govshuttle/keeper/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
12 changes: 4 additions & 8 deletions x/govshuttle/types/genesis.go
Original file line number Diff line number Diff line change
@@ -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,
}
}

Expand Down
57 changes: 28 additions & 29 deletions x/govshuttle/types/genesis.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d32085f

Please sign in to comment.