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 upgrade oracle tx & cli #579

Merged
merged 35 commits into from
Jan 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
3893eae
feat: add extra commission rate flags to genoracle cmd
audtlr24 Dec 19, 2022
8330472
feat: add v2.1.0 upgrade handler (#552)
0xHansLee Dec 15, 2022
eda304c
ci: make actions work for main branch (#555)
Dec 16, 2022
fecab14
feat: add oracle upgrade proposal tx & cli
audtlr24 Dec 26, 2022
af4a238
feat: add test code
audtlr24 Dec 26, 2022
402eeb1
feat: add oracle upgrade proposal tx & cli
audtlr24 Dec 26, 2022
468a132
feat: add test code
audtlr24 Dec 26, 2022
31557d5
Merge remote-tracking branch 'origin/ft/569/oracle-upgrade-handler' i…
audtlr24 Dec 26, 2022
ec72841
feat: proto for oracle upgrade and its skeleton
Dec 26, 2022
8eec577
fix test
Dec 26, 2022
69bc58b
add event type of oracle upgrade
Dec 26, 2022
860223d
feat: add oracle upgrade to EndBlocker
audtlr24 Dec 26, 2022
909c3d8
add oracle upgrade key
Dec 27, 2022
07b94cc
fix
audtlr24 Dec 27, 2022
cb6b661
fix
audtlr24 Dec 27, 2022
1ee72d5
fix
audtlr24 Dec 27, 2022
34fee18
fix
audtlr24 Dec 27, 2022
660d959
Merge remote-tracking branch 'origin/ft/569/oracle-upgrade-handler' i…
audtlr24 Dec 27, 2022
12f0e02
feat: add upgrade oracle tx cli
audtlr24 Dec 27, 2022
c634f6f
Merge remote-tracking branch 'origin/ft/570/upgrade-endblocker' into …
audtlr24 Dec 27, 2022
6ebe8b6
feat: add upgrade oracle keeper
audtlr24 Dec 27, 2022
f290bf2
feat: add test code for oracle upgrade
audtlr24 Dec 27, 2022
61e8e4e
Merge remote-tracking branch 'origin/ft/570/upgrade-endblocker' into …
Dec 27, 2022
c66ff61
remove duplicated grpc query
Dec 27, 2022
f73a2ca
use the same request/response message for approval of oracle upgrade
Dec 28, 2022
44a3f47
fix
Dec 28, 2022
442479b
Revert "fix"
Dec 28, 2022
5da5d80
Revert "use the same request/response message for approval of oracle …
Dec 28, 2022
b7a94d9
Merge remote-tracking branch 'origin/ft/575/upgrade-proto' into ft/57…
audtlr24 Dec 28, 2022
9630dfb
fix
audtlr24 Dec 28, 2022
159a4fe
fix
audtlr24 Dec 30, 2022
41d0d4b
fix
audtlr24 Jan 2, 2023
7ed8894
Update x/oracle/keeper/upgrade_test.go
audtlr24 Jan 3, 2023
b973049
Merge remote-tracking branch 'origin/main' into ft/571/upgrade-oracle-tx
audtlr24 Jan 3, 2023
5b3d9a4
feat: add oracle upgrade related genesis (#583)
audtlr24 Jan 4, 2023
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
4 changes: 3 additions & 1 deletion proto/panacea/oracle/v2/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import "panacea/oracle/v2/oracle.proto";
message GenesisState {
repeated Oracle oracles = 1 [(gogoproto.nullable) = false];
repeated OracleRegistration oracle_registrations = 2 [(gogoproto.nullable) = false];
Params params = 3 [(gogoproto.nullable) = false];
repeated OracleUpgrade oracle_upgrades = 3 [(gogoproto.nullable) = false];
OracleUpgradeInfo oracle_upgrade_info = 4;
Params params = 5 [(gogoproto.nullable) = false];
}

// Params defines the oracle module's params.
Expand Down
1 change: 1 addition & 0 deletions x/oracle/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func (suite *abciTestSuite) TestOracleUpgradeSuccess() {
oracle.EndBlocker(ctx, suite.OracleKeeper)

suite.Require().Equal(upgradeUniqueID, suite.OracleKeeper.GetParams(ctx).UniqueId)

}

func (suite *abciTestSuite) TestOracleUpgradeFailedBeforeReachUpgradeHeight() {
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 @@ -32,6 +32,7 @@ func GetTxCmd() *cobra.Command {

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

return cmd
}
107 changes: 107 additions & 0 deletions x/oracle/client/cli/txUpgradeOracle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package cli

import (
"encoding/base64"
"encoding/hex"
"fmt"
"strconv"

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

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

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

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
}

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
}

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
}

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
}

msg := types.NewMsgUpgradeOracle(uniqueID, oracleAddress, nodePubKey, nodePubKeyRemoteReport, trustedBlockHeight, trustedBlockHash)
if err := msg.ValidateBasic(); err != nil {
return err
}

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

cmd.Flags().String(flagOracleUniqueID, "", "unique ID for oracle")
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")

if err := cmd.MarkFlagRequired(flagOracleUniqueID); err != nil {
panic(err)
}
if err := cmd.MarkFlagRequired(flagNodePublicKey); err != nil {
panic(err)
}
if err := cmd.MarkFlagRequired(flagNodePubKeyRemoteReport); err != nil {
panic(err)
}
if err := cmd.MarkFlagRequired(flagTrustedBlockHeight); err != nil {
panic(err)
}
if err := cmd.MarkFlagRequired(flagTrustedBlockHash); err != nil {
panic(err)
}

flags.AddTxFlagsToCmd(cmd)

return cmd
}
24 changes: 24 additions & 0 deletions x/oracle/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState)
}
}

for _, oracleUpgrade := range genState.OracleUpgrades {
if err := k.SetOracleUpgrade(ctx, &oracleUpgrade); err != nil {
panic(err)
}
}

if genState.OracleUpgradeInfo != nil {
if err := k.SetOracleUpgradeInfo(ctx, genState.OracleUpgradeInfo); err != nil {
panic(err)
}
}

k.SetParams(ctx, genState.Params)
}

Expand All @@ -40,6 +52,18 @@ func ExportGenesis(ctx sdk.Context, k keeper.Keeper) *types.GenesisState {
}
genesis.OracleRegistrations = oracleRegistrations

oracleUpgrades, err := k.GetAllOracleUpgradeList(ctx)
if err != nil {
panic(err)
}
genesis.OracleUpgrades = oracleUpgrades

upgradeInfo, err := k.GetOracleUpgradeInfo(ctx)
if err != nil {
panic(err)
}
genesis.OracleUpgradeInfo = upgradeInfo

genesis.Params = k.GetParams(ctx)

return genesis
Expand Down
69 changes: 67 additions & 2 deletions x/oracle/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ import (
type genesisTestSuite struct {
testsuite.TestSuite

uniqueID string
endpoint string
uniqueID string
uniqueID2 string
endpoint string

oracleAccPrivKey cryptotypes.PrivKey
oracleAccPubKey cryptotypes.PubKey
Expand All @@ -38,6 +39,7 @@ func TestGenesisTestSuite(t *testing.T) {

func (suite *genesisTestSuite) BeforeTest(_, _ string) {
suite.uniqueID = "uniqueID"
suite.uniqueID2 = "uniqueID2"
suite.endpoint = "https://my-validator.org"

suite.oracleAccPrivKey = secp256k1.GenPrivKey()
Expand Down Expand Up @@ -86,6 +88,7 @@ func (suite *genesisTestSuite) TestInitGenesis() {
OracleCommissionRate: sdk.NewDecWithPrec(1, 1),
OracleCommissionMaxRate: sdk.NewDecWithPrec(2, 1),
OracleCommissionMaxChangeRate: sdk.NewDecWithPrec(1, 2),
EncryptedOraclePrivKey: nil,
},
{
UniqueId: suite.uniqueID,
Expand All @@ -98,8 +101,33 @@ func (suite *genesisTestSuite) TestInitGenesis() {
OracleCommissionRate: sdk.NewDecWithPrec(1, 1),
OracleCommissionMaxRate: sdk.NewDecWithPrec(2, 1),
OracleCommissionMaxChangeRate: sdk.NewDecWithPrec(1, 2),
EncryptedOraclePrivKey: nil,
},
},
OracleUpgrades: []types.OracleUpgrade{
{
UniqueId: suite.uniqueID2,
OracleAddress: suite.oracleAccAddr.String(),
NodePubKey: suite.nodePubKey.SerializeCompressed(),
NodePubKeyRemoteReport: []byte("nodePubKeyRemoteReport"),
TrustedBlockHeight: 10,
TrustedBlockHash: nil,
EncryptedOraclePrivKey: nil,
},
{
UniqueId: suite.uniqueID2,
OracleAddress: suite.oracle2AccAddr.String(),
NodePubKey: suite.nodePubKey.SerializeCompressed(),
NodePubKeyRemoteReport: []byte("nodePubKeyRemoteReport"),
TrustedBlockHeight: 10,
TrustedBlockHash: nil,
EncryptedOraclePrivKey: nil,
},
},
OracleUpgradeInfo: &types.OracleUpgradeInfo{
UniqueId: suite.uniqueID2,
Height: 100,
},
Params: types.DefaultParams(),
}

Expand All @@ -118,6 +146,18 @@ func (suite *genesisTestSuite) TestInitGenesis() {
getOracleRegistration, err = suite.OracleKeeper.GetOracleRegistration(suite.Ctx, suite.uniqueID, suite.oracle2AccAddr.String())
suite.Require().NoError(err)
suite.Require().Equal(genesis.OracleRegistrations[1], *getOracleRegistration)

getOracleUpgrade, err := suite.OracleKeeper.GetOracleUpgrade(suite.Ctx, suite.uniqueID2, suite.oracleAccAddr.String())
suite.Require().NoError(err)
suite.Require().Equal(genesis.OracleUpgrades[0], *getOracleUpgrade)
getOracleUpgrade, err = suite.OracleKeeper.GetOracleUpgrade(suite.Ctx, suite.uniqueID2, suite.oracle2AccAddr.String())
suite.Require().NoError(err)
suite.Require().Equal(genesis.OracleUpgrades[1], *getOracleUpgrade)

getOracleUpgradeInfo, err := suite.OracleKeeper.GetOracleUpgradeInfo(suite.Ctx)
suite.Require().NoError(err)
suite.Require().Equal(genesis.OracleUpgradeInfo, getOracleUpgradeInfo)

}

func (suite *genesisTestSuite) TestExportGenesis() {
Expand All @@ -144,15 +184,40 @@ func (suite *genesisTestSuite) TestExportGenesis() {
OracleCommissionRate: sdk.NewDecWithPrec(1, 1),
OracleCommissionMaxRate: sdk.NewDecWithPrec(2, 1),
OracleCommissionMaxChangeRate: sdk.NewDecWithPrec(1, 2),
EncryptedOraclePrivKey: nil,
}

err = suite.OracleKeeper.SetOracleRegistration(suite.Ctx, oraRegistration)
suite.Require().NoError(err)

oracleUpgrade := &types.OracleUpgrade{
UniqueId: suite.uniqueID2,
OracleAddress: suite.oracleAccAddr.String(),
NodePubKey: suite.nodePubKey.SerializeCompressed(),
NodePubKeyRemoteReport: []byte("nodePubKeyRemoteReport"),
TrustedBlockHeight: 10,
TrustedBlockHash: nil,
EncryptedOraclePrivKey: nil,
}

err = suite.OracleKeeper.SetOracleUpgrade(suite.Ctx, oracleUpgrade)
suite.Require().NoError(err)

oracleUpgradeInfo := &types.OracleUpgradeInfo{
UniqueId: suite.uniqueID2,
Height: 100,
}

err = suite.OracleKeeper.SetOracleUpgradeInfo(suite.Ctx, oracleUpgradeInfo)
suite.Require().NoError(err)

params := types.DefaultParams()
suite.OracleKeeper.SetParams(suite.Ctx, params)

genesisStatus := oracle.ExportGenesis(suite.Ctx, suite.OracleKeeper)
suite.Require().Equal(*ora, genesisStatus.Oracles[0])
suite.Require().Equal(*oraRegistration, genesisStatus.OracleRegistrations[0])
suite.Require().Equal(*oracleUpgrade, genesisStatus.OracleUpgrades[0])
suite.Require().Equal(oracleUpgradeInfo, genesisStatus.OracleUpgradeInfo)
suite.Require().Equal(params, genesisStatus.Params)
}
3 changes: 3 additions & 0 deletions x/oracle/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ func NewHandler(k keeper.Keeper) sdk.Handler {
case *types.MsgUpdateOracleInfo:
res, err := msgServer.UpdateOracleInfo(sdk.WrapSDKContext(ctx), msg)
return sdk.WrapServiceResult(ctx, res, err)
case *types.MsgUpgradeOracle:
res, err := msgServer.UpgradeOracle(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
21 changes: 13 additions & 8 deletions x/oracle/keeper/grpc_query_oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ func (k Keeper) OracleRegistration(goCtx context.Context, request *types.QueryOr
return &types.QueryOracleRegistrationResponse{OracleRegistration: oracleRegistration}, nil
}

func (k Keeper) Params(goCtx context.Context, _ *types.QueryOracleParamsRequest) (*types.QueryParamsResponse, error) {
params := k.GetParams(sdk.UnwrapSDKContext(goCtx))
return &types.QueryParamsResponse{Params: &params}, nil
}

func (k Keeper) OracleUpgradeInfo(ctx context.Context, _ *types.QueryOracleUpgradeInfoRequest) (*types.QueryOracleUpgradeInfoResponse, error) {
upgradeInfo, err := k.GetOracleUpgradeInfo(sdk.UnwrapSDKContext(ctx))
if err != nil {
Expand All @@ -107,12 +112,12 @@ func (k Keeper) OracleUpgradeInfo(ctx context.Context, _ *types.QueryOracleUpgra
}, nil
}

func (k Keeper) Params(goCtx context.Context, _ *types.QueryOracleParamsRequest) (*types.QueryParamsResponse, error) {
params := k.GetParams(sdk.UnwrapSDKContext(goCtx))
return &types.QueryParamsResponse{Params: &params}, nil
}

func (k Keeper) OracleUpgrade(goCtx context.Context, request *types.QueryOracleUpgradeRequest) (*types.QueryOracleUpgradeResponse, error) {
// TODO: Implementation
return &types.QueryOracleUpgradeResponse{}, nil
}

oracleUpgrade, err := k.GetOracleUpgrade(sdk.UnwrapSDKContext(goCtx), request.UniqueId, request.OracleAddress)
if err != nil {
return nil, err
}

return &types.QueryOracleUpgradeResponse{OracleUpgrade: oracleUpgrade}, nil
}
26 changes: 26 additions & 0 deletions x/oracle/keeper/grpc_query_oracle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,3 +279,29 @@ func (suite *queryOracleTestSuite) TestOracleUpgradeInfo() {
suite.Require().Equal(upgradeInfo.UniqueId, getUpgradeInfo.OracleUpgradeInfo.UniqueId)
suite.Require().Equal(upgradeInfo.Height, getUpgradeInfo.OracleUpgradeInfo.Height)
}

func (suite *queryOracleTestSuite) TestOracleUpgrade() {
ctx := suite.Ctx
oracleKeeper := suite.OracleKeeper

oracleUpgrade := &types.OracleUpgrade{
UniqueId: suite.uniqueID,
OracleAddress: suite.oracleAccAddr.String(),
NodePubKey: suite.nodePubKey.SerializeCompressed(),
NodePubKeyRemoteReport: []byte("nodePubKeyRemoteReport"),
TrustedBlockHeight: 10,
TrustedBlockHash: []byte("hash"),
EncryptedOraclePrivKey: nil,
}

suite.Require().NoError(oracleKeeper.SetOracleUpgrade(ctx, oracleUpgrade))

getOracleUpgrade, err := oracleKeeper.GetOracleUpgrade(ctx, suite.uniqueID, suite.oracleAccAddr.String())
suite.Require().NoError(err)
suite.Require().Equal(oracleUpgrade.UniqueId, getOracleUpgrade.UniqueId)
suite.Require().Equal(oracleUpgrade.OracleAddress, getOracleUpgrade.OracleAddress)
suite.Require().Equal(oracleUpgrade.NodePubKey, getOracleUpgrade.NodePubKey)
suite.Require().Equal(oracleUpgrade.NodePubKeyRemoteReport, getOracleUpgrade.NodePubKeyRemoteReport)
suite.Require().Equal(oracleUpgrade.TrustedBlockHeight, getOracleUpgrade.TrustedBlockHeight)
suite.Require().Equal(oracleUpgrade.TrustedBlockHash, getOracleUpgrade.TrustedBlockHash)
}
4 changes: 3 additions & 1 deletion x/oracle/keeper/msg_server_oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ func (m msgServer) UpdateOracleInfo(goCtx context.Context, msg *types.MsgUpdateO
}

func (m msgServer) UpgradeOracle(goCtx context.Context, msg *types.MsgUpgradeOracle) (*types.MsgUpgradeOracleResponse, error) {
// TODO: Implementation
if err := m.Keeper.UpgradeOracle(sdk.UnwrapSDKContext(goCtx), msg); err != nil {
return nil, err
}
return &types.MsgUpgradeOracleResponse{}, nil
}

Expand Down
Loading