Skip to content
This repository has been archived by the owner on Nov 16, 2022. It is now read-only.

chain: Improve code quality and conciseness in x/oracle/keeper #1933

Merged
merged 3 commits into from
Jun 12, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions CHANGELOG_UNRELEASED.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

### Chain

- (chore) [\#1933](https://github.com/bandprotocol/bandchain/pull/1933) Improve code quality and conciseness in x/oracle/keeper.
- (bugs) [\#1935](https://github.com/bandprotocol/bandchain/pull/1935) Fix wrong order on call search latest request.
- (feat) [\#1927](https://github.com/bandprotocol/bandchain/pull/1927) Add reporter list of validator REST endpoint.
- (impv) [\#1891](https://github.com/bandprotocol/bandchain/pull/1891) Add end-to-end request flow test.
Expand Down
3 changes: 2 additions & 1 deletion chain/x/oracle/keeper/data_source.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package keeper

import (
"github.com/bandprotocol/bandchain/chain/x/oracle/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"

"github.com/bandprotocol/bandchain/chain/x/oracle/types"
)

// HasDataSource checks if the data source of this ID exists in the storage.
Expand Down
21 changes: 7 additions & 14 deletions chain/x/oracle/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
porttypes "github.com/cosmos/cosmos-sdk/x/ibc/05-port/types"
"github.com/cosmos/cosmos-sdk/x/params"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
"github.com/cosmos/cosmos-sdk/x/staking"
"github.com/tendermint/tendermint/libs/log"

"github.com/bandprotocol/bandchain/chain/pkg/filecache"
Expand All @@ -30,8 +29,8 @@ type Keeper struct {

// NewKeeper creates a new oracle Keeper instance.
func NewKeeper(
cdc *codec.Codec, key sdk.StoreKey, fileDir string,
paramSpace params.Subspace, stakingKeeper staking.Keeper, channelKeeper types.ChannelKeeper,
cdc *codec.Codec, key sdk.StoreKey, fileDir string, paramSpace params.Subspace,
stakingKeeper types.StakingKeeper, channelKeeper types.ChannelKeeper,
scopedKeeper capability.ScopedKeeper, portKeeper types.PortKeeper,
) Keeper {
if !paramSpace.HasKeyTable() {
Expand Down Expand Up @@ -114,18 +113,15 @@ func (k Keeper) GetRequestLastExpired(ctx sdk.Context) int64 {
// If the global request count is not set, it initializes it with value 0.
func (k Keeper) GetNextRequestID(ctx sdk.Context) types.RequestID {
requestNumber := k.GetRequestCount(ctx)
store := ctx.KVStore(k.storeKey)

bz := k.cdc.MustMarshalBinaryLengthPrefixed(requestNumber + 1)
store.Set(types.RequestCountStoreKey, bz)
ctx.KVStore(k.storeKey).Set(types.RequestCountStoreKey, bz)
return types.RequestID(requestNumber + 1)
}

// GetDataSourceCount returns the current number of all data sources ever exist.
func (k Keeper) GetDataSourceCount(ctx sdk.Context) int64 {
var dataSourceCount int64
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.DataSourceCountStoreKey)
bz := ctx.KVStore(k.storeKey).Get(types.DataSourceCountStoreKey)
if bz == nil {
return 0
}
Expand All @@ -137,17 +133,15 @@ func (k Keeper) GetDataSourceCount(ctx sdk.Context) int64 {
// If the global data source count is not set, it initializes the value and returns 1.
func (k Keeper) GetNextDataSourceID(ctx sdk.Context) types.DataSourceID {
dataSourceCount := k.GetDataSourceCount(ctx)
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshalBinaryLengthPrefixed(dataSourceCount + 1)
store.Set(types.DataSourceCountStoreKey, bz)
ctx.KVStore(k.storeKey).Set(types.DataSourceCountStoreKey, bz)
return types.DataSourceID(dataSourceCount + 1)
}

// GetOracleScriptCount returns the current number of all oracle scripts ever exist.
func (k Keeper) GetOracleScriptCount(ctx sdk.Context) int64 {
var oracleScriptCount int64
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.OracleScriptCountStoreKey)
bz := ctx.KVStore(k.storeKey).Get(types.OracleScriptCountStoreKey)
if bz == nil {
return 0
}
Expand All @@ -159,9 +153,8 @@ func (k Keeper) GetOracleScriptCount(ctx sdk.Context) int64 {
// If the global oracle script count is not set, it initializes the value and returns 1.
func (k Keeper) GetNextOracleScriptID(ctx sdk.Context) types.OracleScriptID {
oracleScriptCount := k.GetOracleScriptCount(ctx)
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshalBinaryLengthPrefixed(oracleScriptCount + 1)
store.Set(types.OracleScriptCountStoreKey, bz)
ctx.KVStore(k.storeKey).Set(types.OracleScriptCountStoreKey, bz)
return types.OracleScriptID(oracleScriptCount + 1)
}

Expand Down
4 changes: 2 additions & 2 deletions chain/x/oracle/keeper/oracle_script.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package keeper

import (
"github.com/bandprotocol/bandchain/chain/x/oracle/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"

"github.com/bandprotocol/bandchain/chain/x/oracle/types"
)

// HasOracleScript checks if the oracle script of this ID exists in the storage.
Expand Down Expand Up @@ -47,7 +48,6 @@ func (k Keeper) AddOracleScript(ctx sdk.Context, oracleScript types.OracleScript
// MustEditOracleScript edits the given oracle script by id and flushes it to the storage. Panic if not exists.
func (k Keeper) MustEditOracleScript(ctx sdk.Context, id types.OracleScriptID, new types.OracleScript) {
oracleScript := k.MustGetOracleScript(ctx, id)

oracleScript.Owner = new.Owner
oracleScript.Name = modify(oracleScript.Name, new.Name)
oracleScript.Description = modify(oracleScript.Description, new.Description)
Expand Down
3 changes: 2 additions & 1 deletion chain/x/oracle/keeper/report.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package keeper

import (
"github.com/bandprotocol/bandchain/chain/x/oracle/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"

"github.com/bandprotocol/bandchain/chain/x/oracle/types"
)

// HasReport checks if the report of this ID triple exists in the storage.
Expand Down
3 changes: 2 additions & 1 deletion chain/x/oracle/keeper/reporter.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package keeper

import (
"github.com/bandprotocol/bandchain/chain/x/oracle/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"

"github.com/bandprotocol/bandchain/chain/x/oracle/types"
)

// IsReporter returns true iff the address is an authorized reporter for the given validator.
Expand Down