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

Update numeric store getters to return bool if value is missing #2169

Merged
merged 9 commits into from
Jul 27, 2023
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
5 changes: 1 addition & 4 deletions app/inflation/inflation.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,13 @@ func (c Calculator) InflationRate(ctx sdk.Context, minter minttypes.Minter, mint
ugovKeeper := c.UgovKeeperB.Keeper(&ctx)
inflationParams := ugovKeeper.InflationParams()
maxSupplyAmount := inflationParams.MaxSupply.Amount

totalSupply := c.MintKeeper.StakingTokenSupply(ctx)
if totalSupply.GTE(maxSupplyAmount) {
// supply is already reached the maximum amount, so inflation should be zero
return sdk.ZeroDec()
}

cycleEnd, err := ugovKeeper.GetInflationCycleEnd()
util.Panic(err)

cycleEnd := ugovKeeper.GetInflationCycleEnd()
if ctx.BlockTime().After(cycleEnd) {
// new inflation cycle is starting , so we need to update the inflation max and min rate
factor := bpmath.One - inflationParams.InflationReductionRate
Expand Down
37 changes: 16 additions & 21 deletions util/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
sdkmath "cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)

// GetValue loads value from the store using default Unmarshaler. Panics on failure to decode.
Expand All @@ -27,7 +26,7 @@
if bz := store.Get(key); len(bz) > 0 {
var c TPtr = new(T)
if err := c.Unmarshal(bz); err != nil {
panic(fmt.Sprintf("error unmarshaling %s into %T: %s", errField, c, err))

Check warning on line 29 in util/store/store.go

View check run for this annotation

Codecov / codecov/patch

util/store/store.go#L29

Added line #L29 was not covered by tests
}
return c
}
Expand All @@ -40,7 +39,7 @@
func SetValue[T Marshalable](store sdk.KVStore, key []byte, value T, errField string) error {
bz, err := value.Marshal()
if err != nil {
return fmt.Errorf("can't marshal %s: %s", errField, err)

Check warning on line 42 in util/store/store.go

View check run for this annotation

Codecov / codecov/patch

util/store/store.go#L42

Added line #L42 was not covered by tests
}
store.Set(key, bz)
return nil
Expand All @@ -48,26 +47,26 @@

// GetBinValue is similar to GetValue (loads value in the store),
// but uses UnmarshalBinary interface instead of protobuf
func GetBinValue[TPtr PtrBinMarshalable[T], T any](store sdk.KVStore, key []byte, errField string) (TPtr, error) {

Check warning on line 50 in util/store/store.go

View check run for this annotation

Codecov / codecov/patch

util/store/store.go#L50

Added line #L50 was not covered by tests
if bz := store.Get(key); len(bz) > 0 {
var c TPtr = new(T)
if err := c.UnmarshalBinary(bz); err != nil {
return nil, fmt.Errorf("error unmarshaling %s into %T: %s", errField, c, err)

Check warning on line 54 in util/store/store.go

View check run for this annotation

Codecov / codecov/patch

util/store/store.go#L52-L54

Added lines #L52 - L54 were not covered by tests
}
return c, nil

Check warning on line 56 in util/store/store.go

View check run for this annotation

Codecov / codecov/patch

util/store/store.go#L56

Added line #L56 was not covered by tests
}
return nil, nil

Check warning on line 58 in util/store/store.go

View check run for this annotation

Codecov / codecov/patch

util/store/store.go#L58

Added line #L58 was not covered by tests
}

// SetBinValue is similar to SetValue (stores value in the store),
// but uses UnmarshalBinary interface instead of protobuf
func SetBinValue[T BinMarshalable](store sdk.KVStore, key []byte, value T, errField string) error {
bz, err := value.MarshalBinary()
if err != nil {
return fmt.Errorf("can't marshal %s: %s", errField, err)

Check warning on line 66 in util/store/store.go

View check run for this annotation

Codecov / codecov/patch

util/store/store.go#L63-L66

Added lines #L63 - L66 were not covered by tests
}
store.Set(key, bz)
return nil

Check warning on line 69 in util/store/store.go

View check run for this annotation

Codecov / codecov/patch

util/store/store.go#L68-L69

Added lines #L68 - L69 were not covered by tests
}

// GetValueCdc is similar to GetValue, but uses codec for marshaling. For Protobuf objects the
Expand All @@ -75,38 +74,38 @@
// instead of GetValue.
// Returns a boolean indicating whether any data was found. If the return is false, the object
// is not changed by this function.
func GetValueCdc(store sdk.KVStore, cdc codec.Codec, key []byte, object codec.ProtoMarshaler, errField string) bool {
if bz := store.Get(key); len(bz) > 0 {
err := cdc.Unmarshal(bz, object)
if err != nil {
panic(errField + " could not be unmarshaled: " + err.Error())

Check warning on line 81 in util/store/store.go

View check run for this annotation

Codecov / codecov/patch

util/store/store.go#L77-L81

Added lines #L77 - L81 were not covered by tests
}
return true

Check warning on line 83 in util/store/store.go

View check run for this annotation

Codecov / codecov/patch

util/store/store.go#L83

Added line #L83 was not covered by tests
}
return false

Check warning on line 85 in util/store/store.go

View check run for this annotation

Codecov / codecov/patch

util/store/store.go#L85

Added line #L85 was not covered by tests
}

// SetValueCdc is similar to the SetValue, but uses codec for marshaling. For Protobuf objects the
// result is the same, unless codec.Any is used. In the latter case this function MUST be used,
// instead of SetValue.
func SetValueCdc(store sdk.KVStore, cdc codec.Codec, key []byte, object codec.ProtoMarshaler, errField string) error {
bz, err := cdc.Marshal(object)

Check warning on line 92 in util/store/store.go

View check run for this annotation

Codecov / codecov/patch

util/store/store.go#L91-L92

Added lines #L91 - L92 were not covered by tests
if err != nil {
return fmt.Errorf("failed to encode %s, %s", errField, err.Error())

Check warning on line 94 in util/store/store.go

View check run for this annotation

Codecov / codecov/patch

util/store/store.go#L94

Added line #L94 was not covered by tests
}
store.Set(key, bz)
return nil
}

// GetInt retrieves an sdkmath.Int from a KVStore, or returns zero if no value is stored.
// GetInt retrieves an sdkmath.Int from a KVStore, or returns (0, false) if no value is stored.
// It panics if a stored value fails to unmarshal or is negative.
// Accepts an additional string which should describe the field being retrieved in custom error messages.
func GetInt(store sdk.KVStore, key []byte, errField string) sdkmath.Int {
func GetInt(store sdk.KVStore, key []byte, errField string) (sdkmath.Int, bool) {
val := GetValue[*sdkmath.Int](store, key, errField)
if val == nil { // Not found
return sdk.ZeroInt()
return sdk.ZeroInt(), false
}
return *val
return *val, true
}

// SetInt stores an sdkmath.Int in a KVStore, or clears if setting to zero or nil.
Expand All @@ -115,19 +114,19 @@
func SetInt(store sdk.KVStore, key []byte, val sdkmath.Int, errField string) error {
if val.IsNil() || val.IsZero() {
store.Delete(key)
return nil

Check warning on line 117 in util/store/store.go

View check run for this annotation

Codecov / codecov/patch

util/store/store.go#L117

Added line #L117 was not covered by tests
}
return SetValue(store, key, &val, errField)
}

// GetDec retrieves an sdk.Dec from a KVStore, or returns zero if no value is stored.
// GetDec retrieves an sdk.Dec from a KVStore, or returns (0, false) if no value is stored.
// Accepts an additional string which should describe the field being retrieved in custom error messages.
func GetDec(store sdk.KVStore, key []byte, errField string) sdk.Dec {
func GetDec(store sdk.KVStore, key []byte, errField string) (sdk.Dec, bool) {
val := GetValue[*sdk.Dec](store, key, errField)
if val == nil { // Not found
return sdk.ZeroDec()
return sdk.ZeroDec(), false
}
return *val
return *val, true
}

// SetDec stores an sdk.Dec in a KVStore, or clears if setting to zero or nil.
Expand All @@ -136,7 +135,7 @@
func SetDec(store sdk.KVStore, key []byte, val sdk.Dec, errField string) error {
if val.IsNil() || val.IsZero() {
store.Delete(key)
return nil

Check warning on line 138 in util/store/store.go

View check run for this annotation

Codecov / codecov/patch

util/store/store.go#L138

Added line #L138 was not covered by tests
}
return SetValue(store, key, &val, errField)
}
Expand All @@ -156,19 +155,16 @@
func SetAddress(store sdk.KVStore, key []byte, val sdk.AccAddress) {
if val == nil || val.Empty() {
store.Delete(key)
return

Check warning on line 158 in util/store/store.go

View check run for this annotation

Codecov / codecov/patch

util/store/store.go#L158

Added line #L158 was not covered by tests
}
store.Set(key, val)
}

// GetTimeMs retrieves time saved as Unix time in Miliseconds.
// Returns sdkerrors.NotFound error if the value is not there, hence time = 0 is not supported.
robert-zaremba marked this conversation as resolved.
Show resolved Hide resolved
func GetTimeMs(store sdk.KVStore, key []byte) (time.Time, error) {
t := GetInteger[int64](store, key)
if t == 0 {
return time.Time{}, sdkerrors.ErrNotFound
}
return time.UnixMilli(t), nil
func GetTimeMs(store sdk.KVStore, key []byte) (time.Time, bool) {
t, ok := GetInteger[int64](store, key)
return time.UnixMilli(t), ok
}

// SetTimeMs saves time as Unix time in Miliseconds.
Expand All @@ -191,26 +187,25 @@
case uint32:
bz = make([]byte, 4)
binary.LittleEndian.PutUint32(bz, v)
case byte:
bz = []byte{v}

Check warning on line 191 in util/store/store.go

View check run for this annotation

Codecov / codecov/patch

util/store/store.go#L190-L191

Added lines #L190 - L191 were not covered by tests
}
store.Set(key, bz)
}

func GetInteger[T Integer](store sdk.KVStore, key []byte) T {
func GetInteger[T Integer](store sdk.KVStore, key []byte) (T, bool) {
bz := store.Get(key)
if bz == nil {
return 0
return 0, false
}
var v T
switch any(v).(type) {
case int64, uint64:
v2 := binary.LittleEndian.Uint64(bz)
return T(v2)
return T(binary.LittleEndian.Uint64(bz)), true
case int32, uint32:
return T(binary.LittleEndian.Uint32(bz))
return T(binary.LittleEndian.Uint32(bz)), true
case byte:
return T(bz[0])
return T(bz[0]), true

Check warning on line 208 in util/store/store.go

View check run for this annotation

Codecov / codecov/patch

util/store/store.go#L207-L208

Added lines #L207 - L208 were not covered by tests
}
panic("not possible: all types must be covered above")

Check warning on line 210 in util/store/store.go

View check run for this annotation

Codecov / codecov/patch

util/store/store.go#L210

Added line #L210 was not covered by tests
}
38 changes: 24 additions & 14 deletions util/store/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/stretchr/testify/require"
"gotest.tools/v3/assert"

Expand All @@ -17,30 +16,41 @@ func TestGetAndSetDec(t *testing.T) {
t.Parallel()
store := tsdk.KVStore(t)
key := []byte("decKey")
val := sdk.MustNewDecFromStr("1234")
err := SetDec(store, key, val, "no error")
v1 := sdk.MustNewDecFromStr("1234.5679")
v2, ok := GetDec(store, key, "no error")
assert.Equal(t, false, ok)
assert.DeepEqual(t, sdk.ZeroDec(), v2)

err := SetDec(store, key, v1, "no error")
assert.NilError(t, err)

v := GetDec(store, key, "no error")
assert.DeepEqual(t, v, val)
v2, ok = GetDec(store, key, "no error")
assert.Equal(t, true, ok)
assert.DeepEqual(t, v2, v1)
}

func TestGetAndSetInt(t *testing.T) {
t.Parallel()
store := tsdk.KVStore(t)
key := []byte("intKey")
val, ok := sdk.NewIntFromString("1234")
v2, ok := GetInt(store, key, "no error")
assert.Equal(t, false, ok)
assert.DeepEqual(t, sdk.ZeroInt(), v2)

v1, ok := sdk.NewIntFromString("1234")
assert.Equal(t, true, ok)
err := SetInt(store, key, val, "no error")
err := SetInt(store, key, v1, "no error")
assert.NilError(t, err)

v := GetInt(store, key, "no error")
assert.DeepEqual(t, v, val)
v2, ok = GetInt(store, key, "no error")
assert.Equal(t, true, ok)
assert.DeepEqual(t, v2, v1)
}

func checkStoreNumber[T Integer](name string, val T, store sdk.KVStore, key []byte, t *testing.T) {
SetInteger(store, key, val)
vOut := GetInteger[T](store, key)
vOut, ok := GetInteger[T](store, key)
require.True(t, ok)
require.Equal(t, val, vOut, name)
}

Expand Down Expand Up @@ -75,14 +85,14 @@ func TestGetAndSetTime(t *testing.T) {
store := tsdk.KVStore(t)
key := []byte("tKey")

_, err := GetTimeMs(store, key)
assert.ErrorIs(t, err, sdkerrors.ErrNotFound)
_, ok := GetTimeMs(store, key)
assert.Equal(t, false, ok)

val := time.Now()
SetTimeMs(store, key, val)

val2, err := GetTimeMs(store, key)
assert.NilError(t, err)
val2, ok := GetTimeMs(store, key)
assert.Equal(t, true, ok)
val = val.Truncate(time.Millisecond)
assert.Equal(t, val, val2)
}
14 changes: 8 additions & 6 deletions x/incentive/keeper/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
func (k Keeper) GetParams(ctx sdk.Context) incentive.Params {
params := store.GetValue[*incentive.Params](k.KVStore(ctx), keyPrefixParams, "params")
if params == nil {
// on missing module parameters, return defaults rather than panicking or returning zero values
return incentive.DefaultParams()
}

Check warning on line 16 in x/incentive/keeper/store.go

View check run for this annotation

Codecov / codecov/patch

x/incentive/keeper/store.go#L14-L16

Added lines #L14 - L16 were not covered by tests
return *params
}

Expand All @@ -29,7 +29,7 @@
// the status is NotExist. Error only if the program is found under multiple statuses.
func (k Keeper) incentiveProgramStatus(ctx sdk.Context, id uint32) (incentive.ProgramStatus, error) {
if id == 0 {
return incentive.ProgramStatusNotExist, incentive.ErrInvalidProgramID.Wrap("zero")

Check warning on line 32 in x/incentive/keeper/store.go

View check run for this annotation

Codecov / codecov/patch

x/incentive/keeper/store.go#L32

Added line #L32 was not covered by tests
}

statuses := []incentive.ProgramStatus{
Expand Down Expand Up @@ -57,11 +57,11 @@
case 1:
// If the program existed
return status, nil
default:
// If the program somehow existed in multiple statuses at once (should never happen)
return incentive.ProgramStatusNotExist, incentive.ErrInvalidProgramStatus.Wrapf(
"multiple statuses found for incentive program %d", id,
)

Check warning on line 64 in x/incentive/keeper/store.go

View check run for this annotation

Codecov / codecov/patch

x/incentive/keeper/store.go#L60-L64

Added lines #L60 - L64 were not covered by tests
}
}

Expand All @@ -72,24 +72,24 @@
) {
program := incentive.IncentiveProgram{}
if id == 0 {
return program, incentive.ProgramStatusNotExist, incentive.ErrInvalidProgramID.Wrap("zero")

Check warning on line 75 in x/incentive/keeper/store.go

View check run for this annotation

Codecov / codecov/patch

x/incentive/keeper/store.go#L75

Added line #L75 was not covered by tests
}

status, err := k.incentiveProgramStatus(ctx, id)
if err != nil {
return program, incentive.ProgramStatusNotExist, err
}

Check warning on line 81 in x/incentive/keeper/store.go

View check run for this annotation

Codecov / codecov/patch

x/incentive/keeper/store.go#L80-L81

Added lines #L80 - L81 were not covered by tests
if status == incentive.ProgramStatusNotExist {
return program, incentive.ProgramStatusNotExist, sdkerrors.ErrNotFound.Wrapf("program id %d", id)
}

if !k.getObject(&ctx, keyIncentiveProgram(id, status), &program, "incentive program") {
return program, incentive.ProgramStatusNotExist, sdkerrors.ErrNotFound.Wrapf("program id %d", id)
}

Check warning on line 88 in x/incentive/keeper/store.go

View check run for this annotation

Codecov / codecov/patch

x/incentive/keeper/store.go#L87-L88

Added lines #L87 - L88 were not covered by tests
// Enforces that program ID matches where it was stored
if program.ID != id {
return program, incentive.ProgramStatusNotExist, incentive.ErrInvalidProgramID.Wrap("mismatch")
}

Check warning on line 92 in x/incentive/keeper/store.go

View check run for this annotation

Codecov / codecov/patch

x/incentive/keeper/store.go#L91-L92

Added lines #L91 - L92 were not covered by tests
return program, status, nil
}

Expand All @@ -99,15 +99,15 @@
ctx sdk.Context, program incentive.IncentiveProgram, status incentive.ProgramStatus,
) error {
if program.ID == 0 {
return incentive.ErrInvalidProgramID.Wrap("zero")

Check warning on line 102 in x/incentive/keeper/store.go

View check run for this annotation

Codecov / codecov/patch

x/incentive/keeper/store.go#L102

Added line #L102 was not covered by tests
}
s, err := k.incentiveProgramStatus(ctx, program.ID)
if err != nil {
return err
}

Check warning on line 107 in x/incentive/keeper/store.go

View check run for this annotation

Codecov / codecov/patch

x/incentive/keeper/store.go#L106-L107

Added lines #L106 - L107 were not covered by tests
// error if program exists but not with intended status
if s != incentive.ProgramStatusNotExist && s != status {
return sdkerrors.ErrInvalidRequest.Wrapf("program %d already exists with status %d", program.ID, s)

Check warning on line 110 in x/incentive/keeper/store.go

View check run for this annotation

Codecov / codecov/patch

x/incentive/keeper/store.go#L110

Added line #L110 was not covered by tests
}

key := keyIncentiveProgram(program.ID, status)
Expand All @@ -122,8 +122,8 @@
return err
}
if status == incentive.ProgramStatusNotExist {
return sdkerrors.ErrNotFound.Wrapf("program %d", id)
}

Check warning on line 126 in x/incentive/keeper/store.go

View check run for this annotation

Codecov / codecov/patch

x/incentive/keeper/store.go#L125-L126

Added lines #L125 - L126 were not covered by tests

k.KVStore(ctx).Delete(keyIncentiveProgram(id, status))
return nil
Expand All @@ -131,7 +131,8 @@

// getNextProgramID gets the ID that will be assigned to the next incentive program passed by governance.
func (k Keeper) getNextProgramID(ctx sdk.Context) uint32 {
return store.GetInteger[uint32](k.KVStore(ctx), keyNextProgramID)
id, _ := store.GetInteger[uint32](k.KVStore(ctx), keyNextProgramID)
return id
}

// setNextProgramID sets the ID that will be assigned to the next incentive program passed by governance.
Expand All @@ -147,7 +148,8 @@
// getLastRewardsTime gets the last unix time incentive rewards were computed globally by EndBlocker.
// panics if it would return a negative value.
toteki marked this conversation as resolved.
Show resolved Hide resolved
func (k Keeper) GetLastRewardsTime(ctx sdk.Context) int64 {
return store.GetInteger[int64](k.KVStore(ctx), keyLastRewardsTime)
t, _ := store.GetInteger[int64](k.KVStore(ctx), keyLastRewardsTime)
return t
}

// setLastRewardsTime sets the last unix time incentive rewards were computed globally by EndBlocker.
Expand All @@ -164,14 +166,14 @@
// getTotalBonded retrieves the total amount of uTokens of a given denom which are bonded to the incentive module
func (k Keeper) getTotalBonded(ctx sdk.Context, denom string) sdk.Coin {
key := keyTotalBonded(denom)
amount := store.GetInt(k.KVStore(ctx), key, "total bonded")
amount, _ := store.GetInt(k.KVStore(ctx), key, "total bonded")
return sdk.NewCoin(denom, amount)
}

// GetBonded retrieves the amount of uTokens of a given denom which are bonded by an account
func (k Keeper) GetBonded(ctx sdk.Context, addr sdk.AccAddress, denom string) sdk.Coin {
key := keyBondAmount(addr, denom)
amount := store.GetInt(k.KVStore(ctx), key, "bonded amount")
amount, _ := store.GetInt(k.KVStore(ctx), key, "bonded amount")
return sdk.NewCoin(denom, amount)
}

Expand All @@ -198,15 +200,15 @@
// getUnbonding retrieves the amount of uTokens of a given denom which are unbonding by an account
func (k Keeper) getUnbondingAmount(ctx sdk.Context, addr sdk.AccAddress, denom string) sdk.Coin {
key := keyUnbondAmount(addr, denom)
amount := store.GetInt(k.KVStore(ctx), key, "unbonding amount")
amount, _ := store.GetInt(k.KVStore(ctx), key, "unbonding amount")
return sdk.NewCoin(denom, amount)
}

// getTotalUnbonding retrieves the total amount of uTokens of a given denom which are unbonding from
// the incentive module
func (k Keeper) getTotalUnbonding(ctx sdk.Context, denom string) sdk.Coin {
key := keyTotalUnbonding(denom)
amount := store.GetInt(k.KVStore(ctx), key, "total unbonding")
amount, _ := store.GetInt(k.KVStore(ctx), key, "total unbonding")
return sdk.NewCoin(denom, amount)
}

Expand Down
2 changes: 1 addition & 1 deletion x/incentive/keeper/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ func (k Keeper) EndBlock(ctx sdk.Context) (skipped bool, err error) {

if blockTime <= prevTime {
// Avoids this and related issues: https://github.com/tendermint/tendermint/issues/8773
k.Logger(ctx).Error(
k.Logger(ctx).Debug(
robert-zaremba marked this conversation as resolved.
Show resolved Hide resolved
"incentive module will wait for block time > prevRewardTime",
"current", blockTime,
"prev", prevTime,
Expand Down
10 changes: 2 additions & 8 deletions x/metoken/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,11 @@ func (k Keeper) InitGenesis(genState metoken.GenesisState) {

// ExportGenesis returns the x/metoken module's exported genesis state.
func (k Keeper) ExportGenesis() *metoken.GenesisState {
nextRebalancingTime, err := k.getNextRebalancingTime()
util.Panic(err)

nextInterestClaimTime, err := k.getNextInterestClaimTime()
util.Panic(err)

return &metoken.GenesisState{
Params: k.GetParams(),
Registry: k.GetAllRegisteredIndexes(),
Balances: k.GetAllIndexesBalances(),
NextRebalancingTime: nextRebalancingTime,
NextInterestClaimTime: nextInterestClaimTime,
NextRebalancingTime: k.getNextRebalancingTime(),
NextInterestClaimTime: k.getNextInterestClaimTime(),
}
}
12 changes: 8 additions & 4 deletions x/metoken/keeper/metoken.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ func (k Keeper) GetAllIndexesBalances() []metoken.IndexBalances {
}

// getNextRebalancingTime returns next x/metoken re-balancing time in Milliseconds.
func (k Keeper) getNextRebalancingTime() (time.Time, error) {
return store.GetTimeMs(k.store, keyPrefixNextRebalancingTime)
// Returns 0 unix time if the time was not set before.
func (k Keeper) getNextRebalancingTime() time.Time {
t, _ := store.GetTimeMs(k.store, keyPrefixNextRebalancingTime)
robert-zaremba marked this conversation as resolved.
Show resolved Hide resolved
return t
}

// setNextRebalancingTime next x/metoken re-balancing time in Milliseconds.
Expand All @@ -53,8 +55,10 @@ func (k Keeper) setNextRebalancingTime(nextRebalancingTime time.Time) {
}

// getNextInterestClaimTime returns next x/metoken interest claiming time in Milliseconds.
func (k Keeper) getNextInterestClaimTime() (time.Time, error) {
return store.GetTimeMs(k.store, keyPrefixNextInterestClaimTime)
// Returns 0 unix time if the time was not set before.
func (k Keeper) getNextInterestClaimTime() time.Time {
t, _ := store.GetTimeMs(k.store, keyPrefixNextInterestClaimTime)
return t
}

// setNextInterestClaimTime next x/metoken interest claiming time in Milliseconds.
Expand Down
4 changes: 1 addition & 3 deletions x/ugov/keeper/genesis.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package keeper

import (
"github.com/umee-network/umee/v5/util"
"github.com/umee-network/umee/v5/x/ugov"
)

func (k Keeper) ExportGenesis() *ugov.GenesisState {
cycleEndTime, err := k.GetInflationCycleEnd()
util.Panic(err)
cycleEndTime := k.GetInflationCycleEnd()
return &ugov.GenesisState{
MinGasPrice: k.MinGasPrice(),
InflationParams: k.InflationParams(),
Expand Down
6 changes: 4 additions & 2 deletions x/ugov/keeper/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ func (k Keeper) SetInflationCycleEnd(startTime time.Time) error {
return nil
}

func (k Keeper) GetInflationCycleEnd() (time.Time, error) {
return store.GetTimeMs(k.store, keyInflationCycleEnd)
// Returns zero unix time if the inflation cycle was not set.
func (k Keeper) GetInflationCycleEnd() time.Time {
t, _ := store.GetTimeMs(k.store, keyInflationCycleEnd)
return t
}
10 changes: 4 additions & 6 deletions x/ugov/keeper/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,12 @@ func TestInflationCycleEnd(t *testing.T) {
st := time.Time{}
err := k.SetInflationCycleEnd(st)
require.NoError(err)
in_c, err := k.GetInflationCycleEnd()
require.NoError(err)
require.Equal(in_c.IsZero(), true, "it should be default zero time")
end := k.GetInflationCycleEnd()
require.Equal(end.IsZero(), true, "it should be default zero time")

cycleEnd := time.Now()
err = k.SetInflationCycleEnd(cycleEnd)
require.NoError(err)
end, err := k.GetInflationCycleEnd()
require.NoError(err)
require.Equal(end.UnixMilli(), cycleEnd.UnixMilli(), "inflation cycle end time should be same")
end = k.GetInflationCycleEnd()
require.Equal(end, cycleEnd.Truncate(time.Millisecond), "inflation cycle end time should be same")
}
5 changes: 1 addition & 4 deletions x/ugov/keeper/query_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@ func (q Querier) InflationParams(ctx context.Context, _ *ugov.QueryInflationPara
func (q Querier) InflationCycleEnd(ctx context.Context, _ *ugov.QueryInflationCycleEnd) (
*ugov.QueryInflationCycleEndResponse, error) {
sdkCtx := sdk.UnwrapSDKContext(ctx)
cycleEndTime, err := q.Keeper(&sdkCtx).GetInflationCycleEnd()
if err != nil {
return nil, err
}
cycleEndTime := q.Keeper(&sdkCtx).GetInflationCycleEnd()
return &ugov.QueryInflationCycleEndResponse{End: &cycleEndTime}, nil
}
2 changes: 1 addition & 1 deletion x/uibc/quota/keeper/quota.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (k Keeper) GetAllOutflows() (sdk.DecCoins, error) {

// GetTokenOutflows returns sum of denom outflows in USD value in the DecCoin structure.
func (k Keeper) GetTokenOutflows(denom string) sdk.DecCoin {
amount := store.GetDec(k.store, KeyTotalOutflows(denom), "total_outflow")
amount, _ := store.GetDec(k.store, KeyTotalOutflows(denom), "total_outflow")
return sdk.NewDecCoinFromDec(denom, amount)
}

Expand Down
Loading