Skip to content

Commit

Permalink
feat: add upgrade-oracle cli (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xHansLee authored Jan 3, 2023
1 parent a3fa962 commit 2309792
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 1 deletion.
1 change: 1 addition & 0 deletions cmd/oracled/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func init() {
updateOracleInfoCmd(),
startCmd(),
verifyReportCmd(),
upgradeOracle(),
)
}

Expand Down
119 changes: 119 additions & 0 deletions cmd/oracled/cmd/upgrade_oracle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package cmd

import (
"bufio"
"context"
"encoding/hex"
"fmt"
"os"

"github.com/cosmos/cosmos-sdk/client/input"
"github.com/edgelesssys/ego/enclave"
oracletypes "github.com/medibloc/panacea-core/v2/x/oracle/types"
"github.com/medibloc/panacea-oracle/client/flags"
"github.com/medibloc/panacea-oracle/config"
"github.com/medibloc/panacea-oracle/panacea"
"github.com/medibloc/panacea-oracle/service"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
tos "github.com/tendermint/tendermint/libs/os"
)

func upgradeOracle() *cobra.Command {
cmd := &cobra.Command{
Use: "upgrade-oracle",
Short: "Upgrade the oracle",
RunE: func(cmd *cobra.Command, args []string) error {
// load config
conf, err := loadConfigFromHome(cmd)
if err != nil {
return err
}

// get trusted block information
trustedBlockInfo, err := getTrustedBlockInfo(cmd)
if err != nil {
return err
}

queryClient, err := panacea.NewVerifiedQueryClient(context.Background(), conf, trustedBlockInfo)
if err != nil {
return fmt.Errorf("failed to create queryClient: %w", err)
}

svc, err := service.NewWithQueryClient(conf, queryClient)
if err != nil {
return fmt.Errorf("failed to create service: %w", err)
}
defer svc.Close()

if err := sendTxUpgradeOracle(conf, svc, trustedBlockInfo); err != nil {
return fmt.Errorf("failed to send tx UpgradeOracle: %w", err)
}

return nil
},
}

cmd.Flags().Int64(flags.FlagTrustedBlockHeight, 0, "Trusted block height")
cmd.Flags().String(flags.FlagTrustedBlockHash, "", "Trusted block hash")
if err := cmd.MarkFlagRequired(flags.FlagTrustedBlockHeight); err != nil {
panic(err)
}
if err := cmd.MarkFlagRequired(flags.FlagTrustedBlockHash); err != nil {
panic(err)
}

return cmd
}

func sendTxUpgradeOracle(conf *config.Config, svc service.Service, trustedBlockInfo *panacea.TrustedBlockInfo) error {
oracleAccount := svc.OracleAcc()

msgRegisterOracle, err := generateMsgUpgradeOracle(conf, oracleAccount, trustedBlockInfo)
if err != nil {
return fmt.Errorf("failed to generate MsgUpgradeOracle: %w", err)
}

txHeight, txHash, err := svc.BroadcastTx(msgRegisterOracle)
if err != nil {
return fmt.Errorf("failed to broadcast UpgradeOracle Tx: %w", err)
}

log.Infof("UpgradeOracle transaction succeed. height(%d), hash(%s)", txHeight, txHash)

return nil
}

func generateMsgUpgradeOracle(conf *config.Config, oracleAccount *panacea.OracleAccount, trustedBlockInfo *panacea.TrustedBlockInfo) (*oracletypes.MsgUpgradeOracle, error) {
// if node key exists, return error.
nodePrivKeyPath := conf.AbsNodePrivKeyPath()
if tos.FileExists(nodePrivKeyPath) {
buf := bufio.NewReader(os.Stdin)
ok, err := input.GetConfirmation("There is an existing node key. \nAre you sure to delete and re-generate node key?", buf, os.Stderr)
if err != nil || !ok {
log.Infof("Node key generation is canceled.")
return nil, err
}
}

// generate node key and its remote report
nodePubKey, nodePubKeyRemoteReport, err := generateAndSealedNodeKey(nodePrivKeyPath)
if err != nil {
return nil, fmt.Errorf("failed to generate node key pair: %w", err)
}

report, _ := enclave.VerifyRemoteReport(nodePubKeyRemoteReport)
uniqueID := hex.EncodeToString(report.UniqueID)

msgRegisterOracle := &oracletypes.MsgUpgradeOracle{
UniqueId: uniqueID,
OracleAddress: oracleAccount.GetAddress(),
NodePubKey: nodePubKey,
NodePubKeyRemoteReport: nodePubKeyRemoteReport,
TrustedBlockHeight: trustedBlockInfo.TrustedBlockHeight,
TrustedBlockHash: trustedBlockInfo.TrustedBlockHash,
}

return msgRegisterOracle, nil
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ require (
github.com/gorilla/mux v1.8.0
github.com/ipfs/go-ipfs-api v0.3.0
github.com/lestrrat-go/jwx/v2 v2.0.8
github.com/medibloc/panacea-core/v2 v2.0.6-0.20221215072001-b57b998128c9
github.com/medibloc/panacea-core/v2 v2.0.6-0.20221226084030-69bc58b8d972
github.com/sirupsen/logrus v1.9.0
github.com/spf13/cobra v1.6.1
github.com/spf13/viper v1.13.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1384,6 +1384,8 @@ github.com/medibloc/cosmos-sdk v0.45.9-panacea.1 h1:JTprXN6z/+6UjkjQU4OfDz7z+sUp
github.com/medibloc/cosmos-sdk v0.45.9-panacea.1/go.mod h1:Z5M4TX7PsHNHlF/1XanI2DIpORQ+Q/st7oaeufEjnvU=
github.com/medibloc/panacea-core/v2 v2.0.6-0.20221215072001-b57b998128c9 h1:A9HLqIkArmL82etQIUwSaFxfBYbWq6r20a7CaPzcP5Y=
github.com/medibloc/panacea-core/v2 v2.0.6-0.20221215072001-b57b998128c9/go.mod h1:jDmkCB2vXq/Daq/XvdzF5ELGH9eT20WpCLdTB7zhMBY=
github.com/medibloc/panacea-core/v2 v2.0.6-0.20221226084030-69bc58b8d972 h1:bpEcXkTMRRMwodYfRXy4ZcaMcpN+vitPeK3f8pXbqFc=
github.com/medibloc/panacea-core/v2 v2.0.6-0.20221226084030-69bc58b8d972/go.mod h1:YCYoLlbegqIP8fbrpvrvkK5LsQojDxukneKczbZ7hic=
github.com/mgechev/dots v0.0.0-20210922191527-e955255bf517/go.mod h1:KQ7+USdGKfpPjXk4Ga+5XxQM4Lm4e3gAogrreFAYpOg=
github.com/mgechev/revive v1.2.1/go.mod h1:+Ro3wqY4vakcYNtkBWdZC7dBg1xSB6sp054wWwmeFm0=
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE=
Expand Down

0 comments on commit 2309792

Please sign in to comment.