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

feat: add localhost client + testing #2700

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions modules/core/02-client/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/cosmos/ibc-go/v6/modules/core/02-client/keeper"
"github.com/cosmos/ibc-go/v6/modules/core/02-client/types"
"github.com/cosmos/ibc-go/v6/modules/core/exported"
localhosttypes "github.com/cosmos/ibc-go/v6/modules/light-clients/09-localhost/types"
)

// InitGenesis initializes the ibc client submodule's state from a provided genesis
Expand Down Expand Up @@ -46,6 +47,12 @@ func InitGenesis(ctx sdk.Context, k keeper.Keeper, gs types.GenesisState) {
}

k.SetNextClientSequence(ctx, gs.NextClientSequence)

if gs.CreateLocalhost {
revision := uint64(0)
clientState := localhosttypes.NewClientState("", types.NewHeight(revision, 1)) // height start at block 1
k.SetClientState(ctx, exported.Localhost, clientState)
}
}

// ExportGenesis returns the ibc client submodule's exported genesis.
Expand Down
5 changes: 5 additions & 0 deletions modules/core/02-client/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,11 @@ func (k Keeper) GetSelfConsensusState(ctx sdk.Context, height exported.Height) (
// Client must be in same revision as the executing chain
func (k Keeper) ValidateSelfClient(ctx sdk.Context, clientState exported.ClientState) error {
tmClient, ok := clientState.(*ibctm.ClientState)

if clientState.ClientType() == exported.Localhost {
return nil
}

if !ok {
return sdkerrors.Wrapf(types.ErrInvalidClient, "client must be a Tendermint client, expected: %T, got: %T",
&ibctm.ClientState{}, tmClient)
Expand Down
2 changes: 1 addition & 1 deletion modules/core/02-client/types/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func DefaultGenesisState() GenesisState {
Clients: []IdentifiedClientState{},
ClientsConsensus: ClientsConsensusStates{},
Params: DefaultParams(),
CreateLocalhost: false,
CreateLocalhost: true,
NextClientSequence: 0,
}
}
Expand Down
6 changes: 6 additions & 0 deletions modules/core/02-client/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"

host "github.com/cosmos/ibc-go/v6/modules/core/24-host"
"github.com/cosmos/ibc-go/v6/modules/core/exported"
)

const (
Expand Down Expand Up @@ -45,6 +46,11 @@ func IsValidClientID(clientID string) bool {

// ParseClientIdentifier parses the client type and sequence from the client identifier.
func ParseClientIdentifier(clientID string) (string, uint64, error) {
// Localhost client ID == client type
if clientID == exported.Localhost {
return clientID, 0, nil
}

if !IsClientIDFormat(clientID) {
return "", 0, sdkerrors.Wrapf(host.ErrInvalidID, "invalid client identifier %s is not in format: `{client-type}-{N}`", clientID)
}
Expand Down
2 changes: 1 addition & 1 deletion modules/core/02-client/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

var (
// DefaultAllowedClients are "06-solomachine" and "07-tendermint"
DefaultAllowedClients = []string{exported.Solomachine, exported.Tendermint}
DefaultAllowedClients = []string{exported.Solomachine, exported.Tendermint, exported.Localhost}

// KeyAllowedClients is store's key for AllowedClients Params
KeyAllowedClients = []byte("AllowedClients")
Expand Down
36 changes: 23 additions & 13 deletions modules/core/03-connection/keeper/handshake.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,8 @@ func (k Keeper) ConnOpenTry(
// generate a new connection
connectionID := k.GenerateConnectionIdentifier(ctx)

selfHeight := clienttypes.GetSelfHeight(ctx)
if consensusHeight.GTE(selfHeight) {
return "", sdkerrors.Wrapf(
sdkerrors.ErrInvalidHeight,
"consensus height is greater than or equal to the current block height (%s >= %s)", consensusHeight, selfHeight,
)
if err := k.validateHeight(ctx, clientState.ClientType(), consensusHeight); err != nil {
return "", err
}

// validate client parameters of a chainB client stored on chainA
Expand Down Expand Up @@ -163,13 +159,8 @@ func (k Keeper) ConnOpenAck(
proofHeight exported.Height, // height that relayer constructed proofTry
consensusHeight exported.Height, // latest height of chainA that chainB has stored on its chainA client
) error {
// Check that chainB client hasn't stored invalid height
selfHeight := clienttypes.GetSelfHeight(ctx)
if consensusHeight.GTE(selfHeight) {
return sdkerrors.Wrapf(
sdkerrors.ErrInvalidHeight,
"consensus height is greater than or equal to the current block height (%s >= %s)", consensusHeight, selfHeight,
)
if err := k.validateHeight(ctx, clientState.ClientType(), consensusHeight); err != nil {
return err
}

// Retrieve connection
Expand Down Expand Up @@ -295,3 +286,22 @@ func (k Keeper) ConnOpenConfirm(

return nil
}

func (k Keeper) validateHeight(
ctx sdk.Context,
clientType string,
consensusHeight exported.Height, // latest height of chain B which chain A has stored in its chain B client
) error {
if clientType == exported.Localhost {
return nil
}

selfHeight := clienttypes.GetSelfHeight(ctx)
if consensusHeight.GTE(selfHeight) {
return sdkerrors.Wrapf(
sdkerrors.ErrInvalidHeight,
"consensus height is greater than or equal to the current block height (%s >= %s)", consensusHeight, selfHeight,
)
}
return nil
}
5 changes: 5 additions & 0 deletions modules/core/03-connection/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ func (k Keeper) SetConnection(ctx sdk.Context, connectionID string, connection t
// GetTimestampAtHeight returns the timestamp in nanoseconds of the consensus state at the
// given height.
func (k Keeper) GetTimestampAtHeight(ctx sdk.Context, connection types.ConnectionEnd, height exported.Height) (uint64, error) {
// Localhost client does not have a consensus state, use current block time.
if connection.ClientId == exported.Localhost {
return uint64(ctx.BlockTime().UnixNano()), nil
}

clientState, found := k.clientKeeper.GetClientState(ctx, connection.GetClientID())
if !found {
return 0, sdkerrors.Wrapf(
Expand Down
Loading