Skip to content

Commit

Permalink
Merge pull request #5 from datachainlab/ek-finality
Browse files Browse the repository at this point in the history
Add unfinalized eki support

Signed-off-by: Jun Kimura <jun.kimura@datachain.jp>
  • Loading branch information
bluele authored Oct 31, 2023
2 parents 9af831e + b36cacf commit dd26f16
Show file tree
Hide file tree
Showing 9 changed files with 272 additions and 115 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/ethereum/go-ethereum v1.12.0
github.com/gorilla/mux v1.8.0
github.com/grpc-ecosystem/grpc-gateway v1.16.0
github.com/hyperledger-labs/yui-relayer v0.4.15
github.com/hyperledger-labs/yui-relayer v0.4.17
github.com/oasisprotocol/oasis-core/go v0.2201.11
github.com/spf13/cobra v1.7.0
github.com/spf13/viper v1.16.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -715,8 +715,8 @@ github.com/huandu/skiplist v1.2.0 h1:gox56QD77HzSC0w+Ws3MH3iie755GBJU1OER3h5VsYw
github.com/huandu/skiplist v1.2.0/go.mod h1:7v3iFjLcSAzO4fN5B8dvebvo/qsfumiLiDXMrPiHF9w=
github.com/hudl/fargo v1.3.0/go.mod h1:y3CKSmjA+wD2gak7sUSXTAoopbhU08POFhmITJgmKTg=
github.com/hydrogen18/memlistener v0.0.0-20200120041712-dcc25e7acd91/go.mod h1:qEIFzExnS6016fRpRfxrExeVn2gbClQA99gQhnIcdhE=
github.com/hyperledger-labs/yui-relayer v0.4.15 h1:kzZJFDvz6SexAfXFDbAo7CyidCN8lIh8KQSnPJQxNJc=
github.com/hyperledger-labs/yui-relayer v0.4.15/go.mod h1:Hdc/ERCPDhbipri45/U6+/3kDH7EttIWGdql+Rd3tZg=
github.com/hyperledger-labs/yui-relayer v0.4.17 h1:cjXYvfkTFiJEpXLtaMPQ7Q6Kd3vFvwQ0FAZ3JncoEz0=
github.com/hyperledger-labs/yui-relayer v0.4.17/go.mod h1:Hdc/ERCPDhbipri45/U6+/3kDH7EttIWGdql+Rd3tZg=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/imkira/go-interpol v1.1.0/go.mod h1:z0h2/2T3XF8kyEPpRgJ3kmNv+C43p+I/CoI+jC3w2iA=
Expand Down
17 changes: 5 additions & 12 deletions relay/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package relay

import (
"context"
"log"

"github.com/hyperledger-labs/yui-relayer/config"
"github.com/hyperledger-labs/yui-relayer/core"
Expand Down Expand Up @@ -39,24 +38,18 @@ func updateEnclaveKeyCmd(ctx *config.Context) *cobra.Command {
return err
}
var (
target *core.ProvableChain
target *core.ProvableChain
verifier *core.ProvableChain
)
if viper.GetBool(flagSrc) {
target = c[src]
verifier = c[dst]
} else {
target = c[dst]
verifier = c[src]
}
prover := target.Prover.(*Prover)
updated, err := prover.updateActiveEnclaveKeyIfNeeded(context.TODO())
if err != nil {
return err
}
if updated {
log.Println("Active enclave key is updated")
} else {
log.Println("No need to update the active enclave key")
}
return nil
return prover.UpdateEKIfNeeded(context.TODO(), verifier)
},
}
return srcFlag(cmd)
Expand Down
104 changes: 104 additions & 0 deletions relay/db.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package relay

import (
"context"
"encoding/json"
"errors"
"fmt"
"log"
"os"
"path/filepath"

"github.com/datachainlab/lcp-go/relay/enclave"
"github.com/hyperledger-labs/yui-relayer/core"
)

const (
lastFinalizedEnclaveKeyInfoFile = "last_finalized_eki"
lastUnfinalizedEnclaveKeyInfoFile = "last_unfinalized_eki"
)

var ErrEnclaveKeyInfoNotFound = errors.New("enclave key info not found")

type unfinalizedEKI struct {
Info *enclave.EnclaveKeyInfo `json:"info"`
MsgIDBytes []byte `json:"msg_id_bytes"`
}

func (pr *Prover) dbPath() string {
return filepath.Join(pr.homePath, "lcp", pr.originChain.ChainID())
}

func (pr *Prover) lastEnclaveKeyInfoFilePath(finalized bool) string {
if finalized {
return filepath.Join(pr.dbPath(), lastFinalizedEnclaveKeyInfoFile)
} else {
return filepath.Join(pr.dbPath(), lastUnfinalizedEnclaveKeyInfoFile)
}
}

func (pr *Prover) loadLastFinalizedEnclaveKey(ctx context.Context) (*enclave.EnclaveKeyInfo, error) {
path := pr.lastEnclaveKeyInfoFilePath(true)
bz, err := os.ReadFile(path)
if err != nil {
if os.IsNotExist(err) {
return nil, fmt.Errorf("%v not found: %w", path, ErrEnclaveKeyInfoNotFound)
}
return nil, err
}
var eki enclave.EnclaveKeyInfo
if err := json.Unmarshal(bz, &eki); err != nil {
return nil, err
}
return &eki, nil
}

func (pr *Prover) loadLastUnfinalizedEnclaveKey(ctx context.Context) (*enclave.EnclaveKeyInfo, core.MsgID, error) {
path := pr.lastEnclaveKeyInfoFilePath(false)
bz, err := os.ReadFile(path)
if err != nil {
if os.IsNotExist(err) {
return nil, nil, fmt.Errorf("%v not found: %w", path, ErrEnclaveKeyInfoNotFound)
}
return nil, nil, err
}
var ueki unfinalizedEKI
if err := json.Unmarshal(bz, &ueki); err != nil {
return nil, nil, err
}
var unfinalizedMsgID core.MsgID
if err := pr.codec.UnmarshalInterface(ueki.MsgIDBytes, &unfinalizedMsgID); err != nil {
return nil, nil, err
}
return ueki.Info, unfinalizedMsgID, nil
}

func (pr *Prover) saveFinalizedEnclaveKeyInfo(ctx context.Context, eki *enclave.EnclaveKeyInfo) error {
log.Println("save finalized enclave key info")
bz, err := json.Marshal(eki)
if err != nil {
return err
}
return os.WriteFile(pr.lastEnclaveKeyInfoFilePath(true), bz, 0600)
}

func (pr *Prover) saveUnfinalizedEnclaveKeyInfo(ctx context.Context, eki *enclave.EnclaveKeyInfo, msgID core.MsgID) error {
log.Println("save unfinalized enclave key info")
msgIDBytes, err := pr.codec.MarshalInterface(msgID)
if err != nil {
return err
}
bz, err := json.Marshal(unfinalizedEKI{
Info: eki,
MsgIDBytes: msgIDBytes,
})
if err != nil {
return err
}
return os.WriteFile(pr.lastEnclaveKeyInfoFilePath(false), bz, 0600)
}

func (pr *Prover) removeUnfinalizedEnclaveKeyInfo(ctx context.Context) error {
log.Println("remove unfinalized enclave key info")
return os.Remove(pr.lastEnclaveKeyInfoFilePath(false))
}
Loading

0 comments on commit dd26f16

Please # to comment.