Skip to content

Commit

Permalink
chore: adding 09-localhost to client InitGenesis (#3092)
Browse files Browse the repository at this point in the history
* adding localhost AppModuleBasic to app.go. adding localhost client to InitGenesis. Fixing update localhost client

* renmaing variable

* adding migration handler for enabling localhost client

* updating to use clienttypes.GetSelfHeight()
  • Loading branch information
damiannolan authored Jan 31, 2023
1 parent bf26904 commit d1b0825
Show file tree
Hide file tree
Showing 13 changed files with 109 additions and 34 deletions.
4 changes: 2 additions & 2 deletions modules/core/02-client/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func BeginBlocker(ctx sdk.Context, k keeper.Keeper) {
}

// update the localhost client with the latest block height
if err := k.UpdateClient(ctx, exported.Localhost, nil); err != nil {
k.Logger(ctx).Error(err.Error())
if clientState, found := k.GetClientState(ctx, exported.Localhost); found {
k.UpdateLocalhostClient(ctx, clientState)
}
}
4 changes: 4 additions & 0 deletions modules/core/02-client/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ func InitGenesis(ctx sdk.Context, k keeper.Keeper, gs types.GenesisState) {
}

k.SetNextClientSequence(ctx, gs.NextClientSequence)

if err := k.CreateLocalhostClient(ctx); err != nil {
panic(fmt.Sprintf("failed to initialise localhost client: %s", err.Error()))
}
}

// ExportGenesis returns the ibc client submodule's exported genesis.
Expand Down
11 changes: 8 additions & 3 deletions modules/core/02-client/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,17 @@ func (suite *KeeperTestSuite) TestQueryClientStates() {
{
"empty pagination",
func() {
localhost := types.NewIdentifiedClientState(exported.Localhost, suite.chainA.GetClientState(exported.Localhost))
expClientStates = types.IdentifiedClientStates{localhost}
req = &types.QueryClientStatesRequest{}
},
true,
},
{
"success, no results",
"success, only localhost",
func() {
localhost := types.NewIdentifiedClientState(exported.Localhost, suite.chainA.GetClientState(exported.Localhost))
expClientStates = types.IdentifiedClientStates{localhost}
req = &types.QueryClientStatesRequest{
Pagination: &query.PageRequest{
Limit: 3,
Expand All @@ -139,11 +143,12 @@ func (suite *KeeperTestSuite) TestQueryClientStates() {
clientStateA1 := path1.EndpointA.GetClientState()
clientStateA2 := path2.EndpointA.GetClientState()

localhost := types.NewIdentifiedClientState(exported.Localhost, suite.chainA.GetClientState(exported.Localhost))
idcs := types.NewIdentifiedClientState(path1.EndpointA.ClientID, clientStateA1)
idcs2 := types.NewIdentifiedClientState(path2.EndpointA.ClientID, clientStateA2)

// order is sorted by client id
expClientStates = types.IdentifiedClientStates{idcs, idcs2}.Sort()
expClientStates = types.IdentifiedClientStates{localhost, idcs, idcs2}.Sort()
req = &types.QueryClientStatesRequest{
Pagination: &query.PageRequest{
Limit: 20,
Expand All @@ -158,10 +163,10 @@ func (suite *KeeperTestSuite) TestQueryClientStates() {
for _, tc := range testCases {
suite.Run(fmt.Sprintf("Case %s", tc.msg), func() {
suite.SetupTest() // reset

tc.malleate()

ctx := sdk.WrapSDKContext(suite.chainA.GetContext())

res, err := suite.chainA.QueryServer.ClientStates(ctx, req)

if tc.expPass {
Expand Down
12 changes: 12 additions & 0 deletions modules/core/02-client/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
host "github.com/cosmos/ibc-go/v7/modules/core/24-host"
"github.com/cosmos/ibc-go/v7/modules/core/exported"
ibctm "github.com/cosmos/ibc-go/v7/modules/light-clients/07-tendermint"
localhost "github.com/cosmos/ibc-go/v7/modules/light-clients/09-localhost"
)

// Keeper represents a type that grants read and write permissions to any client
Expand Down Expand Up @@ -53,6 +54,17 @@ func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", "x/"+exported.ModuleName+"/"+types.SubModuleName)
}

// CreateLocalhostClient initialises the 09-localhost client state and sets it in state.
func (k Keeper) CreateLocalhostClient(ctx sdk.Context) error {
var clientState localhost.ClientState
return clientState.Initialize(ctx, k.cdc, k.ClientStore(ctx, exported.Localhost), nil)
}

// UpdateLocalhostClient updates the 09-localhost client to the latest block height and chain ID.
func (k Keeper) UpdateLocalhostClient(ctx sdk.Context, clientState exported.ClientState) []exported.Height {
return clientState.UpdateState(ctx, k.cdc, k.ClientStore(ctx, exported.Localhost), nil)
}

// GenerateClientIdentifier returns the next client identifier.
func (k Keeper) GenerateClientIdentifier(ctx sdk.Context, clientType string) string {
nextClientSeq := k.GetNextClientSequence(ctx)
Expand Down
23 changes: 17 additions & 6 deletions modules/core/02-client/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/cosmos/ibc-go/v7/modules/core/exported"
solomachine "github.com/cosmos/ibc-go/v7/modules/light-clients/06-solomachine"
ibctm "github.com/cosmos/ibc-go/v7/modules/light-clients/07-tendermint"
localhost "github.com/cosmos/ibc-go/v7/modules/light-clients/09-localhost"
ibctesting "github.com/cosmos/ibc-go/v7/testing"
ibctestingmock "github.com/cosmos/ibc-go/v7/testing/mock"
"github.com/cosmos/ibc-go/v7/testing/simapp"
Expand Down Expand Up @@ -236,9 +237,10 @@ func (suite *KeeperTestSuite) TestValidateSelfClient() {

func (suite KeeperTestSuite) TestGetAllGenesisClients() { //nolint:govet // this is a test, we are okay with copying locks
clientIDs := []string{
testClientID2, testClientID3, testClientID,
exported.Localhost, testClientID2, testClientID3, testClientID,
}
expClients := []exported.ClientState{
localhost.NewClientState(suite.chainA.ChainID, types.GetSelfHeight(suite.chainA.GetContext())),
ibctm.NewClientState(testChainID, ibctm.DefaultTrustLevel, trustingPeriod, ubdPeriod, maxClockDrift, types.ZeroHeight(), commitmenttypes.GetSDKSpecs(), ibctesting.UpgradePath),
ibctm.NewClientState(testChainID, ibctm.DefaultTrustLevel, trustingPeriod, ubdPeriod, maxClockDrift, types.ZeroHeight(), commitmenttypes.GetSDKSpecs(), ibctesting.UpgradePath),
ibctm.NewClientState(testChainID, ibctm.DefaultTrustLevel, trustingPeriod, ubdPeriod, maxClockDrift, types.ZeroHeight(), commitmenttypes.GetSDKSpecs(), ibctesting.UpgradePath),
Expand Down Expand Up @@ -418,22 +420,31 @@ func (suite KeeperTestSuite) TestIterateClientStates() { //nolint:govet // this
testCases := []struct {
name string
prefix []byte
expClientIDs []string
expClientIDs func() []string
}{
{
"all clientIDs",
nil,
append(expSMClientIDs, expTMClientIDs...),
func() []string {
allClientIDs := []string{exported.Localhost}
allClientIDs = append(allClientIDs, expSMClientIDs...)
allClientIDs = append(allClientIDs, expTMClientIDs...)
return allClientIDs
},
},
{
"tendermint clientIDs",
[]byte(exported.Tendermint),
expTMClientIDs,
func() []string {
return expTMClientIDs
},
},
{
"solo machine clientIDs",
[]byte(exported.Solomachine),
expSMClientIDs,
func() []string {
return expSMClientIDs
},
},
}

Expand All @@ -446,7 +457,7 @@ func (suite KeeperTestSuite) TestIterateClientStates() { //nolint:govet // this
return false
})

suite.Require().Equal(tc.expClientIDs, clientIDs)
suite.Require().ElementsMatch(tc.expClientIDs(), clientIDs)
})
}
}
8 changes: 7 additions & 1 deletion modules/core/02-client/keeper/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func NewMigrator(keeper Keeper) Migrator {
return Migrator{keeper: keeper}
}

// Migrate2to3 migrates from version 2 to 3.
// Migrate2to3 migrates from consensus version 2 to 3.
// This migration
// - migrates solo machine client states from v2 to v3 protobuf definition
// - prunes solo machine consensus states
Expand All @@ -25,3 +25,9 @@ func NewMigrator(keeper Keeper) Migrator {
func (m Migrator) Migrate2to3(ctx sdk.Context) error {
return v7.MigrateStore(ctx, m.keeper.storeKey, m.keeper.cdc, m.keeper)
}

// Migrate3to4 migrates from consensus version 3 to 4.
// This migration enables the localhost client.
func (m Migrator) Migrate3to4(ctx sdk.Context) error {
return v7.MigrateLocalhostClient(ctx, m.keeper)
}
1 change: 1 addition & 0 deletions modules/core/02-client/migrations/v7/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ type ClientKeeper interface {
GetClientState(ctx sdk.Context, clientID string) (exported.ClientState, bool)
SetClientState(ctx sdk.Context, clientID string, clientState exported.ClientState)
ClientStore(ctx sdk.Context, clientID string) sdk.KVStore
CreateLocalhostClient(ctx sdk.Context) error
}
10 changes: 10 additions & 0 deletions modules/core/02-client/migrations/v7/localhost.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package v7

import (
sdk "github.com/cosmos/cosmos-sdk/types"
)

// MigrateLocalhostClient initialises the 09-localhost client state and sets it in state.
func MigrateLocalhostClient(ctx sdk.Context, clientKeeper ClientKeeper) error {
return clientKeeper.CreateLocalhostClient(ctx)
}
26 changes: 26 additions & 0 deletions modules/core/02-client/migrations/v7/localhost_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package v7_test

import (
v7 "github.com/cosmos/ibc-go/v7/modules/core/02-client/migrations/v7"
host "github.com/cosmos/ibc-go/v7/modules/core/24-host"
"github.com/cosmos/ibc-go/v7/modules/core/exported"
)

func (suite *MigrationsV7TestSuite) TestMigrateLocalhostClient() {
suite.SetupTest()

// note: explicitly remove the localhost client before running migration handler
clientStore := suite.chainA.GetSimApp().GetIBCKeeper().ClientKeeper.ClientStore(suite.chainA.GetContext(), exported.Localhost)
clientStore.Delete(host.ClientStateKey())

clientState, found := suite.chainA.GetSimApp().GetIBCKeeper().ClientKeeper.GetClientState(suite.chainA.GetContext(), exported.Localhost)
suite.Require().False(found)
suite.Require().Nil(clientState)

err := v7.MigrateLocalhostClient(suite.chainA.GetContext(), suite.chainA.GetSimApp().GetIBCKeeper().ClientKeeper)
suite.Require().NoError(err)

clientState, found = suite.chainA.GetSimApp().GetIBCKeeper().ClientKeeper.GetClientState(suite.chainA.GetContext(), exported.Localhost)
suite.Require().True(found)
suite.Require().NotNil(clientState)
}
19 changes: 0 additions & 19 deletions modules/core/03-connection/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,6 @@ func NewKeeper(cdc codec.BinaryCodec, key storetypes.StoreKey, paramSpace paramt
}
}

// EnableLocalhost is called by init genesis or an upgrade handler?
// EnableLocalhost sets the localhost loopback connection end in store
// NOTES:
// - channel handshake code can remain the same
// - packet handlers require access to a ClientState and use the following methods:
// - Status()
// - GetLatestHeight()
// - GetTimestampAtHeight()
// - VerifyMembership
// - VerifyNonMembership
func (k Keeper) EnableLocalhost(ctx sdk.Context) {
counterparty := types.NewCounterparty(exported.Localhost, types.LocalhostID, commitmenttypes.NewMerklePrefix(k.GetCommitmentPrefix().Bytes()))
connectionEnd := types.NewConnectionEnd(types.OPEN, exported.Localhost, counterparty, types.ExportedVersionsToProto(types.GetCompatibleVersions()), 0)

store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshal(&connectionEnd)
store.Set(host.ConnectionKey(types.LocalhostID), bz)
}

// Logger returns a module-specific logger.
func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", "x/"+exported.ModuleName+"/"+types.SubModuleName)
Expand Down
12 changes: 11 additions & 1 deletion modules/core/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,17 @@ func (am AppModule) RegisterServices(cfg module.Configurator) {
}

connectionMigrator := connectionkeeper.NewMigrator(am.keeper.ConnectionKeeper)
if err := cfg.RegisterMigration(exported.ModuleName, 3, connectionMigrator.Migrate3to4); err != nil {
if err := cfg.RegisterMigration(exported.ModuleName, 3, func(ctx sdk.Context) error {
if err := connectionMigrator.Migrate3to4(ctx); err != nil {
return err
}

if err := clientMigrator.Migrate3to4(ctx); err != nil {
return err
}

return nil
}); err != nil {
panic(err)
}
}
Expand Down
11 changes: 9 additions & 2 deletions modules/light-clients/09-localhost/client_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,18 @@ func (cs ClientState) ZeroCustomFields() exported.ClientState {
}

// Initialize ensures that initial consensus state for localhost is nil.
func (cs ClientState) Initialize(_ sdk.Context, _ codec.BinaryCodec, _ sdk.KVStore, consState exported.ConsensusState) error {
func (cs ClientState) Initialize(ctx sdk.Context, cdc codec.BinaryCodec, clientStore sdk.KVStore, consState exported.ConsensusState) error {
if consState != nil {
return sdkerrors.Wrap(clienttypes.ErrInvalidConsensus, "initial consensus state for localhost must be nil.")
}

clientState := ClientState{
ChainId: ctx.ChainID(),
LatestHeight: clienttypes.GetSelfHeight(ctx),
}

clientStore.Set([]byte(host.KeyClientState), clienttypes.MustMarshalClientState(cdc, &clientState))

return nil
}

Expand Down Expand Up @@ -144,7 +151,7 @@ func (cs ClientState) UpdateStateOnMisbehaviour(_ sdk.Context, _ codec.BinaryCod
// UpdateState updates and stores as necessary any associated information for an IBC client, such as the ClientState and corresponding ConsensusState.
// Upon successful update, a list of consensus heights is returned. It assumes the ClientMessage has already been verified.
func (cs ClientState) UpdateState(ctx sdk.Context, cdc codec.BinaryCodec, clientStore sdk.KVStore, clientMsg exported.ClientMessage) []exported.Height {
height := clienttypes.NewHeight(clienttypes.ParseChainID(ctx.ChainID()), uint64(ctx.BlockHeight()))
height := clienttypes.GetSelfHeight(ctx)

clientState := NewClientState(ctx.ChainID(), height)
clientStore.Set([]byte(host.KeyClientState), clienttypes.MustMarshalClientState(cdc, clientState))
Expand Down
2 changes: 2 additions & 0 deletions testing/simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ import (
ibckeeper "github.com/cosmos/ibc-go/v7/modules/core/keeper"
solomachine "github.com/cosmos/ibc-go/v7/modules/light-clients/06-solomachine"
ibctm "github.com/cosmos/ibc-go/v7/modules/light-clients/07-tendermint"
localhost "github.com/cosmos/ibc-go/v7/modules/light-clients/09-localhost"
ibcmock "github.com/cosmos/ibc-go/v7/testing/mock"
simappparams "github.com/cosmos/ibc-go/v7/testing/simapp/params"
simappupgrades "github.com/cosmos/ibc-go/v7/testing/simapp/upgrades"
Expand Down Expand Up @@ -159,6 +160,7 @@ var (
slashing.AppModuleBasic{},
ibc.AppModuleBasic{},
ibctm.AppModuleBasic{},
localhost.AppModuleBasic{},
solomachine.AppModuleBasic{},
feegrantmodule.AppModuleBasic{},
upgrade.AppModuleBasic{},
Expand Down

0 comments on commit d1b0825

Please sign in to comment.