fix time elapsed calculation + test (#838)

This commit is contained in:
Denali Marsh 2021-02-19 21:24:00 +01:00 committed by GitHub
parent 6c88c01eb8
commit 0a51a737cb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 3 deletions

View File

@ -1,6 +1,8 @@
package keeper package keeper
import ( import (
"math"
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/kava-labs/kava/x/hard/types" "github.com/kava-labs/kava/x/hard/types"
@ -64,7 +66,9 @@ func (k Keeper) AccrueInterest(ctx sdk.Context, denom string) error {
return nil return nil
} }
timeElapsed := ctx.BlockTime().Unix() - previousAccrualTime.Unix() timeElapsed := int64(math.RoundToEven(
ctx.BlockTime().Sub(previousAccrualTime).Seconds(),
))
if timeElapsed == 0 { if timeElapsed == 0 {
return nil return nil
} }

View File

@ -887,7 +887,7 @@ func (suite *KeeperTestSuite) TestBorrowInterest() {
// ------------------------------------------------------------------------------------- // -------------------------------------------------------------------------------------
// Set up snapshot chain context and run begin blocker // Set up snapshot chain context and run begin blocker
runAtTime := time.Unix(prevCtx.BlockTime().Unix()+(snapshot.elapsedTime), 0) runAtTime := prevCtx.BlockTime().Add(time.Duration(int64(time.Second) * snapshot.elapsedTime))
snapshotCtx := prevCtx.WithBlockTime(runAtTime) snapshotCtx := prevCtx.WithBlockTime(runAtTime)
hard.BeginBlocker(snapshotCtx, suite.keeper) hard.BeginBlocker(snapshotCtx, suite.keeper)
@ -1317,7 +1317,7 @@ func (suite *KeeperTestSuite) TestSupplyInterest() {
// ------------------------------------------------------------------------------------- // -------------------------------------------------------------------------------------
// Set up snapshot chain context and run begin blocker // Set up snapshot chain context and run begin blocker
runAtTime := time.Unix(prevCtx.BlockTime().Unix()+(snapshot.elapsedTime), 0) runAtTime := prevCtx.BlockTime().Add(time.Duration(int64(time.Second) * snapshot.elapsedTime))
snapshotCtx := prevCtx.WithBlockTime(runAtTime) snapshotCtx := prevCtx.WithBlockTime(runAtTime)
hard.BeginBlocker(snapshotCtx, suite.keeper) hard.BeginBlocker(snapshotCtx, suite.keeper)