Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add UpdateOracleInfo tx #540

Merged
merged 17 commits into from
Dec 14, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion proto/panacea/oracle/v2/oracle.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@ package panacea.oracle.v2;
option go_package = "github.com/medibloc/panacea-core/x/oracle/types";

import "gogoproto/gogo.proto";
import "google/protobuf/timestamp.proto";

// Oracle defines a detail of oracle.
message Oracle {
string oracle_address = 1;
string unique_id = 2;
string endpoint = 3;
string oracle_commission_rate = 4 [(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false];
google.protobuf.Timestamp update_time = 4 [(gogoproto.nullable) = false, (gogoproto.stdtime) = true];
string oracle_commission_rate = 5 [(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false];
string oracle_commission_max_rate = 6 [(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false];
string oracle_commission_max_change_rate = 7 [(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false];
}

// OracleRegistration defines the detailed states of the registration of oracle.
Expand All @@ -28,4 +32,6 @@ message OracleRegistration {
bytes trusted_block_hash = 6;
string endpoint = 7;
string oracle_commission_rate = 8 [(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false];
string oracle_commission_max_rate = 9 [(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false];
string oracle_commission_max_change_rate = 10 [(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false];
}
2 changes: 2 additions & 0 deletions proto/panacea/oracle/v2/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ message MsgRegisterOracle {
bytes trusted_block_hash = 6;
string endpoint = 7;
string oracle_commission_rate = 8 [(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false];
string oracle_commission_max_rate = 9 [(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false];
string oracle_commission_max_change_rate = 10 [(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false];
}

// MsgRegisterOracleResponse defines the Msg/RegisterOracle response type.
Expand Down
13 changes: 13 additions & 0 deletions x/oracle/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@ import (
"github.com/spf13/cobra"
)

const (
flagOracleUniqueID = "oracle-unique-id"
flagNodePublicKey = "node-public-key"
flagNodePubKeyRemoteReport = "node-public-key-remote-report"
flagTrustedBlockHeight = "trusted-block-height"
flagTrustedBlockHash = "trusted-block-hash"
flagOracleEndpoint = "oracle-endpoint"
flagOracleCommRate = "oracle-commission-rate"
flagOracleCommMaxRate = "oracle-commission-max-rate"
flagOracleCommMaxChangeRate = "oracle-commission-max-change-rate"
)

// GetTxCmd returns the transaction commands for this module
func GetTxCmd() *cobra.Command {
cmd := &cobra.Command{
Expand All @@ -19,6 +31,7 @@ func GetTxCmd() *cobra.Command {
}

cmd.AddCommand(CmdRegisterOracle())
cmd.AddCommand(CmdUpdateOracleInfo())

return cmd
}
89 changes: 80 additions & 9 deletions x/oracle/client/cli/txRegisterOracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,39 +16,70 @@ import (

func CmdRegisterOracle() *cobra.Command {
cmd := &cobra.Command{
Use: "register-oracle [unique ID] [node public key] [node public key remote report] [trusted block height] [trusted block hash] [endpoint] [oracle's commission rate]",
Use: "register-oracle",
Short: "Register a new oracle",
Args: cobra.ExactArgs(7),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

oracleAddress := clientCtx.GetFromAddress().String()
nodePubKey, err := base64.StdEncoding.DecodeString(args[1])

uniqueID, err := cmd.Flags().GetString(flagOracleUniqueID)
if err != nil {
return fmt.Errorf("failed to get oracle unique ID")
}

nodePubkeyStr, err := cmd.Flags().GetString(flagNodePublicKey)
if err != nil {
return fmt.Errorf("failed to get node public key")
}

nodePubKey, err := base64.StdEncoding.DecodeString(nodePubkeyStr)
if err != nil {
return err
}

nodePubKeyRemoteReport, err := base64.StdEncoding.DecodeString(args[2])
nodePubkeyRemoteReportStr, err := cmd.Flags().GetString(flagNodePubKeyRemoteReport)
if err != nil {
return fmt.Errorf("failed to get node public key remote report")
}

nodePubKeyRemoteReport, err := base64.StdEncoding.DecodeString(nodePubkeyRemoteReportStr)
if err != nil {
return err
}

trustedBlockHeight, err := strconv.ParseInt(args[3], 10, 64)
trustedBlockHeightStr, err := cmd.Flags().GetString(flagTrustedBlockHeight)
if err != nil {
return fmt.Errorf("failed to get trsuted block height")
}

trustedBlockHeight, err := strconv.ParseInt(trustedBlockHeightStr, 10, 64)
if err != nil {
return err
}

trustedBlockHash, err := hex.DecodeString(args[4])
trustedBlockHashStr, err := cmd.Flags().GetString(flagTrustedBlockHash)
if err != nil {
return fmt.Errorf("failed to get trsuted block hash")
}

trustedBlockHash, err := hex.DecodeString(trustedBlockHashStr)
if err != nil {
return err
}

endpoint := args[5]
endpoint, err := cmd.Flags().GetString(flagOracleEndpoint)
if err != nil {
return fmt.Errorf("failed to get oralce end point")
}

oracleCommissionRateStr := args[6]
oracleCommissionRateStr, err := cmd.Flags().GetString(flagOracleCommRate)
if err != nil {
return fmt.Errorf("failed to get oralce commission rate")
}

if len(oracleCommissionRateStr) == 0 {
audtlr24 marked this conversation as resolved.
Show resolved Hide resolved
return fmt.Errorf("oracleCommissionRate is empty")
Expand All @@ -59,15 +90,45 @@ func CmdRegisterOracle() *cobra.Command {
return err
}

oracleCommissionMaxRateStr, err := cmd.Flags().GetString(flagOracleCommMaxRate)
if err != nil {
return fmt.Errorf("failed to get oralce commission max rate")
}

if len(oracleCommissionMaxRateStr) == 0 {
return fmt.Errorf("oracleCommissionMaxRate is empty")
}
audtlr24 marked this conversation as resolved.
Show resolved Hide resolved

oracleCommissionMaxRate, err := sdk.NewDecFromStr(oracleCommissionMaxRateStr)
if err != nil {
return err
}

oracleCommissionMaxChangeRateStr, err := cmd.Flags().GetString(flagOracleCommMaxChangeRate)
if err != nil {
return fmt.Errorf("failed to get oralce commission max change rate")
}

if len(oracleCommissionMaxChangeRateStr) == 0 {
return fmt.Errorf("oracleCommissionMaxChangeRate is empty")
}
audtlr24 marked this conversation as resolved.
Show resolved Hide resolved

oracleCommissionMaxChangeRate, err := sdk.NewDecFromStr(oracleCommissionMaxChangeRateStr)
if err != nil {
return err
}

msg := types.NewMsgRegisterOracle(
args[0],
uniqueID,
oracleAddress,
nodePubKey,
nodePubKeyRemoteReport,
trustedBlockHeight,
trustedBlockHash,
endpoint,
oracleCommissionRate,
oracleCommissionMaxRate,
oracleCommissionMaxChangeRate,
)
if err := msg.ValidateBasic(); err != nil {
return err
Expand All @@ -77,6 +138,16 @@ func CmdRegisterOracle() *cobra.Command {
},
}

cmd.Flags().String(flagOracleUniqueID, "", "unique ID for oracle")
audtlr24 marked this conversation as resolved.
Show resolved Hide resolved
cmd.Flags().String(flagNodePublicKey, "", "node public key for oracle")
cmd.Flags().String(flagNodePubKeyRemoteReport, "", "node public key remote report for oracle")
cmd.Flags().String(flagTrustedBlockHeight, "", "trusted block height of panacea trusted block")
cmd.Flags().String(flagTrustedBlockHash, "", "trusted block hash of panacea trusted block")
cmd.Flags().String(flagOracleEndpoint, "", "oracle's endpoint")
cmd.Flags().String(flagOracleCommRate, "", "oracle's commission rate")
cmd.Flags().String(flagOracleCommMaxRate, "", "oracle's commission max rate")
cmd.Flags().String(flagOracleCommMaxChangeRate, "", "oracle's commission max change rate")

flags.AddTxFlagsToCmd(cmd)

return cmd
Expand Down
60 changes: 60 additions & 0 deletions x/oracle/client/cli/txUpdateOracleInfo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package cli

import (
"fmt"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/medibloc/panacea-core/v2/x/oracle/types"
"github.com/spf13/cobra"
)

func CmdUpdateOracleInfo() *cobra.Command {
cmd := &cobra.Command{
Use: "update-oracle-info",
Short: "update an oracle information",
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

oracleAddress := clientCtx.GetFromAddress().String()

endpoint, err := cmd.Flags().GetString(flagOracleEndpoint)
if err != nil {
return fmt.Errorf("failed to get oralce end point")
}

oracleCommissionRateStr, err := cmd.Flags().GetString(flagOracleCommRate)
if err != nil {
return fmt.Errorf("failed to get oralce commission rate")
audtlr24 marked this conversation as resolved.
Show resolved Hide resolved
}

if len(oracleCommissionRateStr) == 0 {
return fmt.Errorf("oracleCommissionRate is empty")
}

audtlr24 marked this conversation as resolved.
Show resolved Hide resolved
oracleCommissionRate, err := sdk.NewDecFromStr(oracleCommissionRateStr)
if err != nil {
return err
}

msg := types.NewMsgUpdateOracleInfo(oracleAddress, endpoint, oracleCommissionRate)
if err := msg.ValidateBasic(); err != nil {
return err
}

return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}

cmd.Flags().String(flagOracleEndpoint, "", "oracle's endpoint")
cmd.Flags().String(flagOracleCommRate, "", "oracle's commission rate")
0xHansLee marked this conversation as resolved.
Show resolved Hide resolved

flags.AddTxFlagsToCmd(cmd)

return cmd
}
72 changes: 44 additions & 28 deletions x/oracle/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package oracle_test

import (
"testing"
"time"

"github.com/btcsuite/btcd/btcec"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
Expand Down Expand Up @@ -59,34 +60,44 @@ func (suite *genesisTestSuite) TestInitGenesis() {
suite.uniqueID,
suite.endpoint,
sdk.NewDecWithPrec(1, 1),
sdk.NewDecWithPrec(2, 1),
sdk.NewDecWithPrec(1, 2),
time.Unix(0, 0).UTC(),
),
*types.NewOracle(
suite.oracle2AccAddr.String(),
suite.uniqueID,
suite.endpoint,
sdk.NewDecWithPrec(1, 1),
sdk.NewDecWithPrec(2, 1),
sdk.NewDecWithPrec(1, 2),
time.Unix(0, 0).UTC(),
),
},
OracleRegistrations: []types.OracleRegistration{
{
UniqueId: suite.uniqueID,
OracleAddress: suite.oracleAccAddr.String(),
NodePubKey: suite.nodePubKey.SerializeCompressed(),
NodePubKeyRemoteReport: []byte("nodePubKeyRemoteReport"),
TrustedBlockHeight: 10,
TrustedBlockHash: nil,
Endpoint: suite.endpoint,
OracleCommissionRate: sdk.NewDecWithPrec(1, 1),
UniqueId: suite.uniqueID,
OracleAddress: suite.oracleAccAddr.String(),
NodePubKey: suite.nodePubKey.SerializeCompressed(),
NodePubKeyRemoteReport: []byte("nodePubKeyRemoteReport"),
TrustedBlockHeight: 10,
TrustedBlockHash: nil,
Endpoint: suite.endpoint,
OracleCommissionRate: sdk.NewDecWithPrec(1, 1),
OracleCommissionMaxRate: sdk.NewDecWithPrec(2, 1),
OracleCommissionMaxChangeRate: sdk.NewDecWithPrec(1, 2),
},
{
UniqueId: suite.uniqueID,
OracleAddress: suite.oracle2AccAddr.String(),
NodePubKey: suite.nodePubKey.SerializeCompressed(),
NodePubKeyRemoteReport: []byte("nodePubKeyRemoteReport"),
TrustedBlockHeight: 10,
TrustedBlockHash: nil,
Endpoint: suite.endpoint,
OracleCommissionRate: sdk.NewDecWithPrec(1, 2),
UniqueId: suite.uniqueID,
OracleAddress: suite.oracle2AccAddr.String(),
NodePubKey: suite.nodePubKey.SerializeCompressed(),
NodePubKeyRemoteReport: []byte("nodePubKeyRemoteReport"),
TrustedBlockHeight: 10,
TrustedBlockHash: nil,
Endpoint: suite.endpoint,
OracleCommissionRate: sdk.NewDecWithPrec(1, 1),
OracleCommissionMaxRate: sdk.NewDecWithPrec(2, 1),
OracleCommissionMaxChangeRate: sdk.NewDecWithPrec(1, 2),
},
},
Params: types.DefaultParams(),
Expand All @@ -111,23 +122,28 @@ func (suite *genesisTestSuite) TestInitGenesis() {

func (suite *genesisTestSuite) TestExportGenesis() {
ora := &types.Oracle{
OracleAddress: suite.oracleAccAddr.String(),
UniqueId: suite.uniqueID,
Endpoint: suite.endpoint,
OracleCommissionRate: sdk.NewDecWithPrec(1, 1),
OracleAddress: suite.oracleAccAddr.String(),
UniqueId: suite.uniqueID,
Endpoint: suite.endpoint,
UpdateTime: time.Unix(0, 0).UTC(),
OracleCommissionRate: sdk.NewDecWithPrec(1, 1),
OracleCommissionMaxRate: sdk.NewDecWithPrec(2, 1),
OracleCommissionMaxChangeRate: sdk.NewDecWithPrec(1, 2),
}
err := suite.OracleKeeper.SetOracle(suite.Ctx, ora)
suite.Require().NoError(err)

oraRegistration := &types.OracleRegistration{
UniqueId: suite.uniqueID,
OracleAddress: suite.oracleAccAddr.String(),
NodePubKey: suite.nodePubKey.SerializeCompressed(),
NodePubKeyRemoteReport: []byte("nodePubKeyRemoteReport"),
TrustedBlockHeight: 10,
TrustedBlockHash: nil,
Endpoint: suite.endpoint,
OracleCommissionRate: sdk.NewDecWithPrec(1, 1),
UniqueId: suite.uniqueID,
OracleAddress: suite.oracleAccAddr.String(),
NodePubKey: suite.nodePubKey.SerializeCompressed(),
NodePubKeyRemoteReport: []byte("nodePubKeyRemoteReport"),
TrustedBlockHeight: 10,
TrustedBlockHash: nil,
Endpoint: suite.endpoint,
OracleCommissionRate: sdk.NewDecWithPrec(1, 1),
OracleCommissionMaxRate: sdk.NewDecWithPrec(2, 1),
OracleCommissionMaxChangeRate: sdk.NewDecWithPrec(1, 2),
}
err = suite.OracleKeeper.SetOracleRegistration(suite.Ctx, oraRegistration)
suite.Require().NoError(err)
Expand Down
6 changes: 3 additions & 3 deletions x/oracle/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ func NewHandler(k keeper.Keeper) sdk.Handler {
//case *types.MsgApproveOracleRegistration:
// res, err := msgServer.ApproveOracleRegistration(sdk.WrapSDKContext(ctx), msg)
// return sdk.WrapServiceResult(ctx, res, err)
//case *types.MsgUpdateOracleInfo:
// res, err := msgServer.UpdateOracleInfo(sdk.WrapSDKContext(ctx), msg)
// return sdk.WrapServiceResult(ctx, res, err)
case *types.MsgUpdateOracleInfo:
res, err := msgServer.UpdateOracleInfo(sdk.WrapSDKContext(ctx), msg)
return sdk.WrapServiceResult(ctx, res, err)
default:
errMsg := fmt.Sprintf("unrecognized %s message type: %T", types.ModuleName, msg)
return nil, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, errMsg)
Expand Down
Loading