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 3 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
1 change: 1 addition & 0 deletions x/oracle/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func GetTxCmd() *cobra.Command {
}

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

return cmd
}
28 changes: 26 additions & 2 deletions x/oracle/client/cli/txRegisterOracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ 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 [unique-ID] [node-public-key] [node-public-key-remote-report] [trusted-block-height] [trusted-block-hash] [endpoint] [oracle-commission-rate] [oracle-commission-max-rate] [oracle-commission-max-change-rate]",
audtlr24 marked this conversation as resolved.
Show resolved Hide resolved
Short: "Register a new oracle",
Args: cobra.ExactArgs(7),
Args: cobra.ExactArgs(9),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
Expand Down Expand Up @@ -59,6 +59,28 @@ func CmdRegisterOracle() *cobra.Command {
return err
}

oracleCommissionMaxRateStr := args[7]

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 := args[8]

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],
oracleAddress,
Expand All @@ -68,6 +90,8 @@ func CmdRegisterOracle() *cobra.Command {
trustedBlockHash,
endpoint,
oracleCommissionRate,
oracleCommissionMaxRate,
oracleCommissionMaxChangeRate,
)
if err := msg.ValidateBasic(); err != nil {
return err
Expand Down
52 changes: 52 additions & 0 deletions x/oracle/client/cli/txUpdateOracleInfo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
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 [endpoint] [oracle-commission-rate]",
audtlr24 marked this conversation as resolved.
Show resolved Hide resolved
Short: "update an oracle information",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

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

endpoint := args[0]

oracleCommissionRateStr := args[1]

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)
},
}

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