Skip to content

Commit

Permalink
add create-elc command
Browse files Browse the repository at this point in the history
Signed-off-by: Jun Kimura <[email protected]>
  • Loading branch information
bluele committed May 29, 2024
1 parent fc0995a commit 0c7a00c
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 22 deletions.
24 changes: 24 additions & 0 deletions relay/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func LCPCmd(ctx *config.Context) *cobra.Command {
}

cmd.AddCommand(
createELCCmd(ctx),
updateEnclaveKeyCmd(ctx),
activateClientCmd(ctx),
restoreELCStateCmd(ctx),
Expand Down Expand Up @@ -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]",
Expand Down
58 changes: 58 additions & 0 deletions relay/lcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
23 changes: 1 addition & 22 deletions relay/prover.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 0c7a00c

Please sign in to comment.