Skip to content

Commit

Permalink
Merge pull request hyperledger-labs#124 from siburu/fix-create-msg-cr…
Browse files Browse the repository at this point in the history
…eate-client

change interfaces related to MsgCreateClient
  • Loading branch information
siburu authored Nov 21, 2023
2 parents df66cae + 523478c commit eb9a250
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 102 deletions.
17 changes: 2 additions & 15 deletions chains/tendermint/client-tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"time"

"github.com/cometbft/cometbft/light"
sdk "github.com/cosmos/cosmos-sdk/types"
clienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types"
commitmenttypes "github.com/cosmos/ibc-go/v7/modules/core/23-commitment/types"
tmclient "github.com/cosmos/ibc-go/v7/modules/light-clients/07-tendermint"
Expand All @@ -13,7 +12,7 @@ import (
func createClient(
dstHeader *tmclient.Header,
trustingPeriod, unbondingPeriod time.Duration,
signer sdk.AccAddress) *clienttypes.MsgCreateClient {
) *tmclient.ClientState {
if err := dstHeader.ValidateBasic(); err != nil {
panic(err)
}
Expand All @@ -30,17 +29,5 @@ func createClient(
[]string{"upgrade", "upgradedIBCState"},
)

msg, err := clienttypes.NewMsgCreateClient(
clientState,
dstHeader.ConsensusState(),
signer.String(),
)

if err != nil {
panic(err)
}
if err = msg.ValidateBasic(); err != nil {
panic(err)
}
return msg
return clientState
}
2 changes: 1 addition & 1 deletion chains/tendermint/cmd/light.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func updateLightCmd(ctx *config.Context) *cobra.Command {
return err
}

fmt.Printf("Updated light client for %s from height %d -> height %d\n", args[0], bh.Header.Height, ah.(*tmclient.Header).Header.Height)
fmt.Printf("Updated light client for %s from height %d -> height %d\n", args[0], bh.Header.Height, ah.Header.Height)
return nil
},
}
Expand Down
36 changes: 24 additions & 12 deletions chains/tendermint/prover.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/cometbft/cometbft/types"
tmtypes "github.com/cometbft/cometbft/types"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
clienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types"
ibcclient "github.com/cosmos/ibc-go/v7/modules/core/client"
ibcexported "github.com/cosmos/ibc-go/v7/modules/core/exported"
Expand Down Expand Up @@ -57,18 +56,31 @@ func (pr *Prover) ProveState(ctx core.QueryContext, path string, value []byte) (

/* LightClient implementation */

// CreateMsgCreateClient creates a MsgCreateClient for the counterparty chain
func (pr *Prover) CreateMsgCreateClient(selfHeader core.Header, signer sdk.AccAddress) (*clienttypes.MsgCreateClient, error) {
// CreateInitialLightClientState creates a pair of ClientState and ConsensusState submitted to the counterparty chain as MsgCreateClient
func (pr *Prover) CreateInitialLightClientState(height ibcexported.Height) (ibcexported.ClientState, ibcexported.ConsensusState, error) {
var tmHeight int64
if height != nil {
tmHeight = int64(height.GetRevisionHeight())
}
selfHeader, err := pr.UpdateLightClient(tmHeight)
if err != nil {
return nil, nil, fmt.Errorf("failed to update the local light client and get the header@%d: %v", tmHeight, err)
}

ubdPeriod, err := pr.chain.QueryUnbondingPeriod()
if err != nil {
return nil, err
return nil, nil, fmt.Errorf("failed to query for the unbonding period: %v", err)
}
return createClient(
selfHeader.(*tmclient.Header),

cs := createClient(
selfHeader,
pr.getTrustingPeriod(),
ubdPeriod,
signer,
), nil
)

cons := selfHeader.ConsensusState()

return cs, cons, nil
}

// SetupHeadersForUpdate returns the finalized header and any intermediate headers needed to apply it to the client on the counterpaty chain
Expand Down Expand Up @@ -108,9 +120,9 @@ func (pr *Prover) SetupHeadersForUpdate(counterparty core.FinalityAwareChain, la
return []core.Header{&h}, nil
}

// GetFinalizedHeader returns the finalized header at `height`
func (pr *Prover) GetFinalizedHeader(height uint64) (core.Header, error) {
return pr.UpdateLightClient(int64(height))
// GetLatestFinalizedHeader returns the latest finalized header
func (pr *Prover) GetLatestFinalizedHeader() (core.Header, error) {
return pr.UpdateLightClient(0)
}

func (pr *Prover) CheckRefreshRequired(counterparty core.ChainInfoICS02Querier) (bool, error) {
Expand Down Expand Up @@ -180,7 +192,7 @@ func (pr *Prover) GetLatestLightHeight() (int64, error) {
return client.LastTrustedHeight()
}

func (pr *Prover) UpdateLightClient(height int64) (core.Header, error) {
func (pr *Prover) UpdateLightClient(height int64) (*tmclient.Header, error) {
// create database connection
db, df, err := pr.NewLightDB()
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions chains/tendermint/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ func (c *Chain) QueryUnfinalizedRelayPackets(ctx core.QueryContext, counterparty
}

var counterpartyCtx core.QueryContext
if counterpartyH, err := counterparty.GetFinalizedHeader(0); err != nil {
if counterpartyH, err := counterparty.GetLatestFinalizedHeader(); err != nil {
return nil, err
} else {
counterpartyCtx = core.NewQueryContext(context.TODO(), counterpartyH.GetHeight())
Expand Down Expand Up @@ -262,7 +262,7 @@ func (c *Chain) QueryUnfinalizedRelayAcknowledgements(ctx core.QueryContext, cou
}

var counterpartyCtx core.QueryContext
if counterpartyH, err := counterparty.GetFinalizedHeader(0); err != nil {
if counterpartyH, err := counterparty.GetLatestFinalizedHeader(); err != nil {
return nil, err
} else {
counterpartyCtx = core.NewQueryContext(context.TODO(), counterpartyH.GetHeight())
Expand Down
25 changes: 21 additions & 4 deletions cmd/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ package cmd

import (
"context"
"fmt"
"strings"

"github.com/cosmos/cosmos-sdk/client/flags"
clienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types"
"github.com/cosmos/ibc-go/v7/modules/core/exported"
"github.com/hyperledger-labs/yui-relayer/config"
"github.com/hyperledger-labs/yui-relayer/core"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -66,14 +69,28 @@ func createClientsCmd(ctx *config.Context) *cobra.Command {
return err
}

srcHeight, err := cmd.Flags().GetUint64(flagSrcHeight)
if err != nil {
// if the option "src-height" is not set or is set zero, the latest finalized height is used.
var srcHeight exported.Height
if height, err := cmd.Flags().GetUint64(flagSrcHeight); err != nil {
return err
} else if height == 0 {
srcHeight = nil
} else if latestHeight, err := c[src].LatestHeight(); err != nil {
return fmt.Errorf("failed to get the latest height of src chain: %v", err)
} else {
srcHeight = clienttypes.NewHeight(latestHeight.GetRevisionNumber(), height)
}

dstHeight, err := cmd.Flags().GetUint64(flagDstHeight)
if err != nil {
// if the option "dst-height" is not set or is set zero, the latest finalized height is used.
var dstHeight exported.Height
if height, err := cmd.Flags().GetUint64(flagDstHeight); err != nil {
return err
} else if height == 0 {
dstHeight = nil
} else if latestHeight, err := c[dst].LatestHeight(); err != nil {
return fmt.Errorf("failed to get the latest height of dst chain: %v", err)
} else {
dstHeight = clienttypes.NewHeight(latestHeight.GetRevisionNumber(), height)
}

return core.CreateClients(c[src], c[dst], srcHeight, dstHeight)
Expand Down
55 changes: 17 additions & 38 deletions core/client.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,22 @@
package core

import (
"fmt"
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
clienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types"
"github.com/cosmos/ibc-go/v7/modules/core/exported"
"github.com/hyperledger-labs/yui-relayer/log"
"golang.org/x/sync/errgroup"
)

func CreateClients(src, dst *ProvableChain, srcHeight, dstHeight uint64) error {
func CreateClients(src, dst *ProvableChain, srcHeight, dstHeight exported.Height) error {
logger := GetChainPairLogger(src, dst)
defer logger.TimeTrack(time.Now(), "CreateClients")
var (
clients = &RelayMsgs{Src: []sdk.Msg{}, Dst: []sdk.Msg{}}
)

srcH, dstH, err := getHeadersForCreateClient(src, dst, srcHeight, dstHeight)
if err != nil {
logger.Error(
"failed to get headers for create client",
err,
)
return err
}

srcAddr, err := src.GetAddress()
if err != nil {
logger.Error(
Expand All @@ -42,24 +35,27 @@ func CreateClients(src, dst *ProvableChain, srcHeight, dstHeight uint64) error {
}

{
msg, err := dst.CreateMsgCreateClient(dstH, srcAddr)
cs, cons, err := dst.CreateInitialLightClientState(dstHeight)
if err != nil {
logger.Error(
"failed to create client",
err,
)
logger.Error("failed to create initial light client state", err)
return err
}
msg, err := clienttypes.NewMsgCreateClient(cs, cons, srcAddr.String())
if err != nil {
return fmt.Errorf("failed to create MsgCreateClient: %v", err)
}
clients.Src = append(clients.Src, msg)
}

{
msg, err := src.CreateMsgCreateClient(srcH, dstAddr)
cs, cons, err := src.CreateInitialLightClientState(srcHeight)
if err != nil {
logger.Error(
"failed to create client",
err,
)
logger.Error("failed to create initial light client state", err)
return err
}
msg, err := clienttypes.NewMsgCreateClient(cs, cons, dstAddr.String())
if err != nil {
logger.Error("failed to create MsgCreateClient: %v", err)
return err
}
clients.Dst = append(clients.Dst, msg)
Expand Down Expand Up @@ -117,23 +113,6 @@ func UpdateClients(src, dst *ProvableChain) error {
return nil
}

// getHeadersForCreateClient calls UpdateLightWithHeader on the passed chains concurrently
func getHeadersForCreateClient(src, dst LightClient, srcHeight, dstHeight uint64) (srch, dsth Header, err error) {
var eg = new(errgroup.Group)
eg.Go(func() error {
srch, err = src.GetFinalizedHeader(srcHeight)
return err
})
eg.Go(func() error {
dsth, err = dst.GetFinalizedHeader(dstHeight)
return err
})
if err := eg.Wait(); err != nil {
return nil, nil, err
}
return srch, dsth, nil
}

func GetClientPairLogger(src, dst Chain) *log.RelayLogger {
return log.GetLogger().
WithClientPair(
Expand Down
4 changes: 2 additions & 2 deletions core/headers.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,12 @@ func (sh *syncHeaders) Updates(src, dst ChainInfoLightClient) error {
return err
}

srcHeader, err := src.GetFinalizedHeader(0)
srcHeader, err := src.GetLatestFinalizedHeader()
if err != nil {
logger.Error("error getting latest finalized header of src", err)
return err
}
dstHeader, err := dst.GetFinalizedHeader(0)
dstHeader, err := dst.GetLatestFinalizedHeader()
if err != nil {
logger.Error("error getting latest finalized header of dst", err)
return err
Expand Down
15 changes: 8 additions & 7 deletions core/provers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"time"

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
clienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types"
"github.com/cosmos/ibc-go/v7/modules/core/exported"
)

// Prover represents a prover that supports generating a commitment proof
Expand All @@ -33,8 +33,10 @@ type StateProver interface {
type LightClient interface {
FinalityAware

// CreateMsgCreateClient creates a MsgCreateClient for the counterparty chain
CreateMsgCreateClient(selfHeader Header, signer sdk.AccAddress) (*clienttypes.MsgCreateClient, error)
// CreateInitialLightClientState returns a pair of ClientState and ConsensusState based on the state of the self chain at `height`.
// These states will be submitted to the counterparty chain as MsgCreateClient.
// If `height` is nil, the latest finalized height is selected automatically.
CreateInitialLightClientState(height exported.Height) (exported.ClientState, exported.ConsensusState, error)

// SetupHeadersForUpdate returns the finalized header and any intermediate headers needed to apply it to the client on the counterpaty chain
// The order of the returned header slice should be as: [<intermediate headers>..., <update header>]
Expand All @@ -48,10 +50,9 @@ type LightClient interface {

// FinalityAware provides the capability to determine the finality of the chain
type FinalityAware interface {
// GetFinalizedHeader returns the finalized header on this chain corresponding to `height`.
// If `height` is zero, this function returns the latest finalized header.
// If the header at `height` isn't finalized yet, this function returns an error.
GetFinalizedHeader(height uint64) (Header, error)
// GetLatestFinalizedHeader returns the latest finalized header on this chain
// The returned header is expected to be the latest one of headers that can be verified by the light client
GetLatestFinalizedHeader() (latestFinalizedHeader Header, err error)
}

// FinalityAwareChain is FinalityAware + Chain
Expand Down
2 changes: 1 addition & 1 deletion core/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func GetFinalizedMsgResult(chain ProvableChain, msgID MsgID) (MsgResult, error)

if err := retry.Do(func() error {
// query LFH for each retry because it can proceed.
lfHeader, err := chain.GetFinalizedHeader(0)
lfHeader, err := chain.GetLatestFinalizedHeader()
if err != nil {
return fmt.Errorf("failed to get latest finalized header: %v", err)
}
Expand Down
Loading

0 comments on commit eb9a250

Please sign in to comment.