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 oracle genesis #522

Merged
merged 7 commits into from
Dec 8, 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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ build-all: proto-lint proto-gen update-swagger-docs build
### Analyzing

lint:
GO111MODULE=off go get github.com/golangci/golangci-lint/cmd/golangci-lint
#GO111MODULE=off go get github.com/golangci/golangci-lint/cmd/golangci-lint
0xHansLee marked this conversation as resolved.
Show resolved Hide resolved
golangci-lint run --timeout 5m0s --allow-parallel-runners
find . -name '*.go' -type f -not -path "./vendor*" -not -path "*.git*" | xargs gofmt -d -s
go mod verify
Expand Down
26 changes: 22 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 @@ -188,6 +191,7 @@ var (
aol.AppModuleBasic{},
did.AppModuleBasic{},
burn.AppModuleBasic{},
oracle.AppModuleBasic{},
wasm.AppModuleBasic{},
feegrantmodule.AppModuleBasic{},
authzmodule.AppModuleBasic{},
Expand Down Expand Up @@ -261,10 +265,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 @@ -307,6 +312,7 @@ func New(
aoltypes.StoreKey,
didtypes.StoreKey,
burntypes.StoreKey,
oracletypes.StoreKey,
wasm.StoreKey,
feegrant.StoreKey,
)
Expand Down Expand Up @@ -421,6 +427,13 @@ func New(
app.BankKeeper,
)

app.oracleKeeper = *oraclekeeper.NewKeeper(
0xHansLee marked this conversation as resolved.
Show resolved Hide resolved
appCodec,
keys[oracletypes.StoreKey],
keys[oracletypes.MemStoreKey],
app.GetSubspace(oracletypes.ModuleName),
)

wasmDir := filepath.Join(homePath, wasm.ModuleName)
wasmConfig, err := wasm.ReadWasmConfig(appOpts)
if err != nil {
Expand Down Expand Up @@ -499,6 +512,7 @@ func New(
aol.NewAppModule(appCodec, app.aolKeeper),
did.NewAppModule(appCodec, app.didKeeper),
burn.NewAppModule(appCodec, app.burnKeeper),
oracle.NewAppModule(appCodec, app.oracleKeeper),
wasm.NewAppModule(appCodec, &app.wasmKeeper, app.StakingKeeper),
)

Expand All @@ -523,6 +537,7 @@ func New(
authtypes.ModuleName,
aoltypes.ModuleName,
didtypes.ModuleName,
oracletypes.ModuleName,
wasm.ModuleName,
banktypes.ModuleName,
crisistypes.ModuleName,
Expand Down Expand Up @@ -551,6 +566,7 @@ func New(
slashingtypes.ModuleName,
ibctransfertypes.ModuleName,
aoltypes.ModuleName,
oracletypes.ModuleName,
wasm.ModuleName,
paramstypes.ModuleName,
authz.ModuleName,
Expand Down Expand Up @@ -579,6 +595,7 @@ func New(
aoltypes.ModuleName,
didtypes.ModuleName,
burntypes.ModuleName,
oracletypes.ModuleName,
wasm.ModuleName,
paramstypes.ModuleName,
upgradetypes.ModuleName,
Expand Down Expand Up @@ -807,6 +824,7 @@ func initParamsKeeper(appCodec codec.Codec, legacyAmino *codec.LegacyAmino, key,
paramsKeeper.Subspace(aoltypes.ModuleName)
paramsKeeper.Subspace(didtypes.ModuleName)
paramsKeeper.Subspace(burntypes.ModuleName)
paramsKeeper.Subspace(oracletypes.ModuleName)
paramsKeeper.Subspace(wasm.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 approved_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
3 changes: 3 additions & 0 deletions x/oracle/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,8 @@ func GetQueryCmd(queryRoute string) *cobra.Command {
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}

cmd.AddCommand(CmdGetParams())

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

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

func CmdGetParams() *cobra.Command {
cmd := &cobra.Command{
Use: "params",
Short: "Query params of oracle module",
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}

queryClient := types.NewQueryClient(clientCtx)

res, err := queryClient.Params(cmd.Context(), &types.QueryOracleParamsRequest{})
if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)

return cmd
}
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
}
34 changes: 30 additions & 4 deletions x/oracle/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,38 @@ import (
// InitGenesis initializes the capability module's state from a provided genesis
// state.
func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState) {
//TODO implement me
panic("implement me")
for _, oracle := range genState.Oracles {
if err := k.SetOracle(ctx, &oracle); err != nil {
panic(err)
}
}

for _, oracleRegistration := range genState.OracleRegistrations {
if err := k.SetOracleRegistration(ctx, &oracleRegistration); err != nil {
panic(err)
}
}

k.SetParams(ctx, genState.Params)
}

// ExportGenesis returns the capability module's exported genesis.
func ExportGenesis(ctx sdk.Context, k keeper.Keeper) *types.GenesisState {
//TODO implement me
panic("implement me")
genesis := types.DefaultGenesis()

oracles, err := k.GetAllOracleList(ctx)
if err != nil {
panic(err)
}
genesis.Oracles = oracles

oracleRegistrations, err := k.GetAllOracleRegistrationList(ctx)
if err != nil {
panic(err)
}
genesis.OracleRegistrations = oracleRegistrations

genesis.Params = k.GetParams(ctx)

return genesis
}
Loading