From 0c7a00cc2106539ecc52c15b21ef0f04042a1880 Mon Sep 17 00:00:00 2001 From: Jun Kimura Date: Wed, 29 May 2024 17:07:08 +0900 Subject: [PATCH] add `create-elc` command Signed-off-by: Jun Kimura --- relay/cmd.go | 24 ++++++++++++++++++++ relay/lcp.go | 58 +++++++++++++++++++++++++++++++++++++++++++++++++ relay/prover.go | 23 +------------------- 3 files changed, 83 insertions(+), 22 deletions(-) diff --git a/relay/cmd.go b/relay/cmd.go index 99296d6..588fe0f 100644 --- a/relay/cmd.go +++ b/relay/cmd.go @@ -21,6 +21,7 @@ func LCPCmd(ctx *config.Context) *cobra.Command { } cmd.AddCommand( + createELCCmd(ctx), updateEnclaveKeyCmd(ctx), activateClientCmd(ctx), restoreELCStateCmd(ctx), @@ -90,6 +91,29 @@ func activateClientCmd(ctx *config.Context) *cobra.Command { return srcFlag(cmd) } +func createELCCmd(ctx *config.Context) *cobra.Command { + cmd := &cobra.Command{ + Use: "create-elc [path]", + Short: "Create ELC client", + Args: cobra.ExactArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + c, src, dst, err := ctx.Config.ChainsFromPath(args[0]) + if err != nil { + return err + } + var target *core.ProvableChain + if viper.GetBool(flagSrc) { + target = c[src] + } else { + target = c[dst] + } + prover := target.Prover.(*Prover) + return prover.doCreateELC(viper.GetUint64(flagHeight)) + }, + } + return heightFlag(srcFlag(cmd)) +} + func restoreELCStateCmd(ctx *config.Context) *cobra.Command { cmd := &cobra.Command{ Use: "restore-elc-state [path]", diff --git a/relay/lcp.go b/relay/lcp.go index 78e10b0..8f58ae3 100644 --- a/relay/lcp.go +++ b/relay/lcp.go @@ -9,6 +9,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" clienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types" + "github.com/cosmos/ibc-go/v8/modules/core/exported" ibcexported "github.com/cosmos/ibc-go/v8/modules/core/exported" mapset "github.com/deckarep/golang-set/v2" "github.com/hyperledger-labs/yui-relayer/core" @@ -345,6 +346,63 @@ func (pr *Prover) registerEnclaveKey(verifier core.Chain, eki *enclave.EnclaveKe return ids[0], nil } +// height: 0 means the latest height +func (pr *Prover) doCreateELC(height uint64) error { + header, err := pr.originProver.GetLatestFinalizedHeader() + if err != nil { + return err + } + latestHeight := header.GetHeight() + if height == 0 { + height = latestHeight.GetRevisionHeight() + } else if height >= latestHeight.GetRevisionHeight() { + return fmt.Errorf("height %v is greater than the latest height %v", height, latestHeight.GetRevisionHeight()) + } + h := clienttypes.NewHeight(latestHeight.GetRevisionNumber(), height) + log.Printf("try to create ELC client: height=%v", h) + res, err := pr.createELC(h) + if err != nil { + return err + } + log.Printf("created ELC client: %v", res.ClientId) + // ensure the message is valid + msg, err := lcptypes.EthABIDecodeHeaderedProxyMessage(res.Message) + if err != nil { + return err + } + m, err := msg.GetUpdateStateProxyMessage() + if err != nil { + return err + } + log.Printf("created state: post_height=%v post_state_id=0x%x", m.PostHeight, m.PostStateID) + return err +} + +func (pr *Prover) createELC(height exported.Height) (*elc.MsgCreateClientResponse, error) { + // NOTE: Query the LCP for available keys, but no need to register it into on-chain here + tmpEKI, err := pr.selectNewEnclaveKey(context.TODO()) + if err != nil { + return nil, err + } + originClientState, originConsensusState, err := pr.originProver.CreateInitialLightClientState(height) + if err != nil { + return nil, err + } + anyOriginClientState, err := clienttypes.PackClientState(originClientState) + if err != nil { + return nil, err + } + anyOriginConsensusState, err := clienttypes.PackConsensusState(originConsensusState) + if err != nil { + return nil, err + } + return pr.lcpServiceClient.CreateClient(context.TODO(), &elc.MsgCreateClient{ + ClientState: anyOriginClientState, + ConsensusState: anyOriginConsensusState, + Signer: tmpEKI.EnclaveKeyAddress, + }) +} + func activateClient(pathEnd *core.PathEnd, src, dst *core.ProvableChain) error { srcProver := src.Prover.(*Prover) if err := srcProver.UpdateEKIfNeeded(context.TODO(), dst); err != nil { diff --git a/relay/prover.go b/relay/prover.go index 3133382..c41ef03 100644 --- a/relay/prover.go +++ b/relay/prover.go @@ -99,28 +99,7 @@ func (pr *Prover) GetChainID() string { // These states will be submitted to the counterparty chain as MsgCreateClient. // If `height` is nil, the latest finalized height is selected automatically. func (pr *Prover) CreateInitialLightClientState(height exported.Height) (exported.ClientState, exported.ConsensusState, error) { - // NOTE: Query the LCP for available keys, but no need to register it into on-chain here - tmpEKI, err := pr.selectNewEnclaveKey(context.TODO()) - if err != nil { - return nil, nil, err - } - originClientState, originConsensusState, err := pr.originProver.CreateInitialLightClientState(height) - if err != nil { - return nil, nil, err - } - anyOriginClientState, err := clienttypes.PackClientState(originClientState) - if err != nil { - return nil, nil, err - } - anyOriginConsensusState, err := clienttypes.PackConsensusState(originConsensusState) - if err != nil { - return nil, nil, err - } - res, err := pr.lcpServiceClient.CreateClient(context.TODO(), &elc.MsgCreateClient{ - ClientState: anyOriginClientState, - ConsensusState: anyOriginConsensusState, - Signer: tmpEKI.EnclaveKeyAddress, - }) + res, err := pr.createELC(height) if err != nil { return nil, nil, err }