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: implement register oracle #519

Merged
merged 8 commits into from
Dec 8, 2022
Merged
Show file tree
Hide file tree
Changes from 6 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
25 changes: 21 additions & 4 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import (
"strings"

"github.com/cosmos/cosmos-sdk/x/authz"
"github.com/medibloc/panacea-core/v2/x/oracle"
oraclekeeper "github.com/medibloc/panacea-core/v2/x/oracle/keeper"
oracletypes "github.com/medibloc/panacea-core/v2/x/oracle/types"

"github.com/CosmWasm/wasmd/x/wasm"
wasmclient "github.com/CosmWasm/wasmd/x/wasm/client"
Expand Down Expand Up @@ -261,10 +264,11 @@ type App struct {
ScopedTransferKeeper capabilitykeeper.ScopedKeeper
ScopedWasmKeeper capabilitykeeper.ScopedKeeper

aolKeeper aolkeeper.Keeper
didKeeper didkeeper.Keeper
burnKeeper burnkeeper.Keeper
wasmKeeper wasm.Keeper
aolKeeper aolkeeper.Keeper
didKeeper didkeeper.Keeper
burnKeeper burnkeeper.Keeper
wasmKeeper wasm.Keeper
oracleKeeper oraclekeeper.Keeper

// the module manager
mm *module.Manager
Expand Down Expand Up @@ -309,6 +313,7 @@ func New(
burntypes.StoreKey,
wasm.StoreKey,
feegrant.StoreKey,
oracletypes.StoreKey,
)
tkeys := sdk.NewTransientStoreKeys(paramstypes.TStoreKey)
memKeys := sdk.NewMemoryStoreKeys(capabilitytypes.MemStoreKey)
Expand Down Expand Up @@ -448,6 +453,13 @@ func New(
wasmOpts...,
)

app.oracleKeeper = *oraclekeeper.NewKeeper(
appCodec,
keys[oracletypes.StoreKey],
keys[oracletypes.MemStoreKey],
app.GetSubspace(oracletypes.ModuleName),
)

// The gov proposal types can be individually enabled
if len(enabledWasmProposals) != 0 {
govRouter.AddRoute(wasm.RouterKey, wasm.NewWasmProposalHandler(app.wasmKeeper, enabledWasmProposals))
Expand Down Expand Up @@ -500,6 +512,7 @@ func New(
did.NewAppModule(appCodec, app.didKeeper),
burn.NewAppModule(appCodec, app.burnKeeper),
wasm.NewAppModule(appCodec, &app.wasmKeeper, app.StakingKeeper),
oracle.NewAppModule(appCodec, app.oracleKeeper),
)

// During begin block slashing happens after distr.BeginBlocker so that
Expand All @@ -523,6 +536,7 @@ func New(
authtypes.ModuleName,
aoltypes.ModuleName,
didtypes.ModuleName,
oracletypes.ModuleName,
wasm.ModuleName,
banktypes.ModuleName,
crisistypes.ModuleName,
Expand Down Expand Up @@ -551,6 +565,7 @@ func New(
slashingtypes.ModuleName,
ibctransfertypes.ModuleName,
aoltypes.ModuleName,
oracletypes.ModuleName,
wasm.ModuleName,
paramstypes.ModuleName,
authz.ModuleName,
Expand Down Expand Up @@ -579,6 +594,7 @@ func New(
aoltypes.ModuleName,
didtypes.ModuleName,
burntypes.ModuleName,
oracletypes.ModuleName,
wasm.ModuleName,
paramstypes.ModuleName,
upgradetypes.ModuleName,
Expand Down Expand Up @@ -808,6 +824,7 @@ func initParamsKeeper(appCodec codec.Codec, legacyAmino *codec.LegacyAmino, key,
paramsKeeper.Subspace(didtypes.ModuleName)
paramsKeeper.Subspace(burntypes.ModuleName)
paramsKeeper.Subspace(wasm.ModuleName)
paramsKeeper.Subspace(oracletypes.ModuleName)

return paramsKeeper
}
Expand Down
5 changes: 3 additions & 2 deletions proto/panacea/oracle/v2/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ message MsgApproveOracleRegistration {
// ApproveOracleRegistration defines for oracle registration approval
message ApproveOracleRegistration {
string unique_id = 1;
string target_oracle_address = 2;
bytes encrypted_oracle_priv_key = 3;
string approver_oracle_address = 2;
string target_oracle_address = 3;
bytes encrypted_oracle_priv_key = 4;
}

// MsgApproveOracleRegistrationResponse defines the Msg/ApproveOracleRegistration
Expand Down
14 changes: 14 additions & 0 deletions types/testsuite/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ import (
aoltypes "github.com/medibloc/panacea-core/v2/x/aol/types"
burnkeeper "github.com/medibloc/panacea-core/v2/x/burn/keeper"
burntypes "github.com/medibloc/panacea-core/v2/x/burn/types"
oraclekeeper "github.com/medibloc/panacea-core/v2/x/oracle/keeper"
oracletypes "github.com/medibloc/panacea-core/v2/x/oracle/types"
"github.com/stretchr/testify/suite"
"github.com/tendermint/tendermint/crypto/secp256k1"
"github.com/tendermint/tendermint/libs/log"
Expand Down Expand Up @@ -65,6 +67,8 @@ type TestSuite struct {
TransferKeeper ibctransferkeeper.Keeper
DIDMsgServer didtypes.MsgServer
DIDKeeper didkeeper.Keeper
OracleKeeper oraclekeeper.Keeper
OracleMsgServer oracletypes.MsgServer
WasmKeeper wasm.Keeper
UpgradeKeeper upgradekeeper.Keeper
}
Expand All @@ -76,6 +80,7 @@ func (suite *TestSuite) SetupTest() {
banktypes.StoreKey,
paramstypes.StoreKey,
didtypes.StoreKey,
oracletypes.StoreKey,
wasm.StoreKey,
ibchost.StoreKey,
capabilitytypes.StoreKey,
Expand Down Expand Up @@ -202,6 +207,15 @@ func (suite *TestSuite) SetupTest() {
memKeys[didtypes.MemStoreKey],
)
suite.DIDMsgServer = didkeeper.NewMsgServerImpl(suite.DIDKeeper)

suite.OracleKeeper = *oraclekeeper.NewKeeper(
cdc.Marshaler,
keyParams[oracletypes.StoreKey],
memKeys[oracletypes.MemStoreKey],
paramsKeeper.Subspace(oracletypes.ModuleName),
)

suite.OracleMsgServer = oraclekeeper.NewMsgServerImpl(suite.OracleKeeper)
}

func (suite *TestSuite) BeforeTest(suiteName, testName string) {
Expand Down
2 changes: 2 additions & 0 deletions x/oracle/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,7 @@ func GetTxCmd() *cobra.Command {
RunE: client.ValidateCmd,
}

cmd.AddCommand(CmdRegisterOracle())

return cmd
}
83 changes: 83 additions & 0 deletions x/oracle/client/cli/txRegisterOracle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
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"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/medibloc/panacea-core/v2/x/oracle/types"
"github.com/spf13/cobra"
)

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]",
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])
if err != nil {
return err
}

nodePubKeyRemoteReport, err := base64.StdEncoding.DecodeString(args[2])
if err != nil {
return err
}

trustedBlockHeight, err := strconv.ParseInt(args[3], 10, 64)
if err != nil {
return err
}

trustedBlockHash, err := hex.DecodeString(args[4])
if err != nil {
return err
}

endpoint := args[5]

oracleCommissionRateStr := args[6]

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

oracleCommissionRate, err := sdk.NewDecFromStr(oracleCommissionRateStr)
if err != nil {
return err
}

msg := types.NewMsgRegisterOracle(
args[0],
oracleAddress,
nodePubKey,
nodePubKeyRemoteReport,
trustedBlockHeight,
trustedBlockHash,
endpoint,
oracleCommissionRate,
)
if err := msg.ValidateBasic(); err != nil {
return err
}

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

flags.AddTxFlagsToCmd(cmd)

return cmd
}
3 changes: 1 addition & 2 deletions x/oracle/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ import (
// state.
func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState) {
//TODO implement me
panic("implement me")
}

// ExportGenesis returns the capability module's exported genesis.
func ExportGenesis(ctx sdk.Context, k keeper.Keeper) *types.GenesisState {
//TODO implement me
panic("implement me")
return &types.GenesisState{}
}
20 changes: 17 additions & 3 deletions x/oracle/keeper/msg_server_oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,26 @@ package keeper
import (
"context"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/medibloc/panacea-core/v2/x/oracle/types"
)

func (m msgServer) RegisterOracle(ctx context.Context, oracle *types.MsgRegisterOracle) (*types.MsgRegisterOracleResponse, error) {
//TODO implement me
panic("implement me")
func (m msgServer) RegisterOracle(goCtx context.Context, msg *types.MsgRegisterOracle) (*types.MsgRegisterOracleResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

if err := m.Keeper.RegisterOracle(ctx, msg); err != nil {
return nil, err
}

ctx.EventManager().EmitEvent(
sdk.NewEvent(

sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName),
),
)

return &types.MsgRegisterOracleResponse{}, nil
}

func (m msgServer) ApproveOracleRegistration(ctx context.Context, registration *types.MsgApproveOracleRegistration) (*types.MsgApproveOracleRegistrationResponse, error) {
Expand Down
121 changes: 121 additions & 0 deletions x/oracle/keeper/oracle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package keeper

import (
"errors"

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/medibloc/panacea-core/v2/x/oracle/types"
)

func (k Keeper) RegisterOracle(ctx sdk.Context, msg *types.MsgRegisterOracle) error {
oracleRegistration := types.NewOracleRegistration(msg)

if err := oracleRegistration.ValidateBasic(); err != nil {
return err
}

params := k.GetParams(ctx)
if params.UniqueId != oracleRegistration.UniqueId {
return sdkerrors.Wrapf(types.ErrOracleRegistration, "is not match the currently active uniqueID")
}

if oracle, err := k.GetOracle(ctx, oracleRegistration.OracleAddress); !errors.Is(types.ErrOracleNotFound, err) {
if oracle != nil {
return sdkerrors.Wrapf(types.ErrOracleRegistration, "already registered oracle. address(%s)", oracleRegistration.OracleAddress)
} else {
return sdkerrors.Wrapf(types.ErrOracleRegistration, err.Error())
}
}

if err := k.SetOracleRegistration(ctx, oracleRegistration); err != nil {
return err
}

ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeRegistration,
sdk.NewAttribute(types.AttributeKeyUniqueID, oracleRegistration.UniqueId),
sdk.NewAttribute(types.AttributeKeyOracleAddress, oracleRegistration.OracleAddress),
),
)
return nil
}

func (k Keeper) SetOracleRegistration(ctx sdk.Context, regOracle *types.OracleRegistration) error {
store := ctx.KVStore(k.storeKey)

accAddr, err := sdk.AccAddressFromBech32(regOracle.OracleAddress)
if err != nil {
return err
}
key := types.GetOracleRegistrationKey(regOracle.UniqueId, accAddr)
bz, err := k.cdc.MarshalLengthPrefixed(regOracle)
if err != nil {
return err
}

store.Set(key, bz)

return nil
}

func (k Keeper) GetOracleRegistration(ctx sdk.Context, uniqueID, address string) (*types.OracleRegistration, error) {
store := ctx.KVStore(k.storeKey)
accAddr, err := sdk.AccAddressFromBech32(address)
if err != nil {
return nil, err
}
key := types.GetOracleRegistrationKey(uniqueID, accAddr)
bz := store.Get(key)
if bz == nil {
return nil, sdkerrors.Wrapf(types.ErrGetOracleRegistration, "oracle registration not found")
}

oracleRegistration := &types.OracleRegistration{}

if err := k.cdc.UnmarshalLengthPrefixed(bz, oracleRegistration); err != nil {
return nil, err
}

return oracleRegistration, nil
}

func (k Keeper) SetOracle(ctx sdk.Context, oracle *types.Oracle) error {
store := ctx.KVStore(k.storeKey)
accAddr, err := sdk.AccAddressFromBech32(oracle.OracleAddress)
if err != nil {
return err
}
key := types.GetOracleKey(accAddr)
bz, err := k.cdc.MarshalLengthPrefixed(oracle)
if err != nil {
return err
}

store.Set(key, bz)

return nil
}

func (k Keeper) GetOracle(ctx sdk.Context, address string) (*types.Oracle, error) {
store := ctx.KVStore(k.storeKey)
accAddr, err := sdk.AccAddressFromBech32(address)
if err != nil {
return nil, err
}
key := types.GetOracleKey(accAddr)
bz := store.Get(key)
if bz == nil {
return nil, sdkerrors.Wrapf(types.ErrOracleNotFound, "oracle '%s' does not exist", address)
}

oracle := &types.Oracle{}

err = k.cdc.UnmarshalLengthPrefixed(bz, oracle)
if err != nil {
return nil, sdkerrors.Wrapf(types.ErrGetOracle, err.Error())
}

return oracle, nil
}
Loading