From 2309792ccacce5fd9c0eb4e10bc3dbeb2e91c25d Mon Sep 17 00:00:00 2001 From: Hansol Lee <38912532+H4NLee@users.noreply.github.com> Date: Tue, 3 Jan 2023 13:47:44 +0900 Subject: [PATCH] feat: add upgrade-oracle cli (#59) --- cmd/oracled/cmd/root.go | 1 + cmd/oracled/cmd/upgrade_oracle.go | 119 ++++++++++++++++++++++++++++++ go.mod | 2 +- go.sum | 2 + 4 files changed, 123 insertions(+), 1 deletion(-) create mode 100644 cmd/oracled/cmd/upgrade_oracle.go diff --git a/cmd/oracled/cmd/root.go b/cmd/oracled/cmd/root.go index e2033ed..06f908b 100644 --- a/cmd/oracled/cmd/root.go +++ b/cmd/oracled/cmd/root.go @@ -41,6 +41,7 @@ func init() { updateOracleInfoCmd(), startCmd(), verifyReportCmd(), + upgradeOracle(), ) } diff --git a/cmd/oracled/cmd/upgrade_oracle.go b/cmd/oracled/cmd/upgrade_oracle.go new file mode 100644 index 0000000..7dabd96 --- /dev/null +++ b/cmd/oracled/cmd/upgrade_oracle.go @@ -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 +} diff --git a/go.mod b/go.mod index 67e38eb..0748d28 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 278eb9a..c7b3497 100644 --- a/go.sum +++ b/go.sum @@ -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=