Skip to content

Commit

Permalink
fix date add bug using months instead of days
Browse files Browse the repository at this point in the history
  • Loading branch information
omerlavanet committed Dec 3, 2024
1 parent b90718b commit 8f3ee81
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
2 changes: 1 addition & 1 deletion x/dualstaking/keeper/delegate_credit.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (k Keeper) CalculateCredit(ctx sdk.Context, delegation types.Delegation) (c
creditTimestamp := time.Unix(delegation.CreditTimestamp, 0)
// we normalize dates before we start the calculation
// maximum scope is 30 days, we start with the delegation truncation then the credit
monthAgo := currentTimestamp.AddDate(0, -1, 0)
monthAgo := currentTimestamp.AddDate(0, 0, -30) // we are doing 30 days not a month a month can be a different amount of days
if monthAgo.After(delegationTimestamp) {
// in the case the delegation wasn't changed for 30 days or more we truncate the timestamp to 30 days ago
// and disable the credit for older dates since they are irrelevant
Expand Down
18 changes: 8 additions & 10 deletions x/dualstaking/keeper/delegate_credit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/lavanet/lava/v4/testutil/common"
keepertest "github.com/lavanet/lava/v4/testutil/keeper"
"github.com/lavanet/lava/v4/utils"
commontypes "github.com/lavanet/lava/v4/utils/common/types"
"github.com/lavanet/lava/v4/x/dualstaking/keeper"
"github.com/lavanet/lava/v4/x/dualstaking/types"
Expand Down Expand Up @@ -255,15 +256,12 @@ func TestCalculateMonthlyCredit(t *testing.T) {

func TestDelegationSet(t *testing.T) {
ts := newTester(t)

// 1 delegator, 1 provider staked, 0 provider unstaked, 0 provider unstaking
ts.setupForDelegation(1, 1, 0, 0)
ctx := ts.Ctx
_, client1Addr := ts.GetAccount(common.CONSUMER, 0)
_, provider1Addr := ts.GetAccount(common.PROVIDER, 0)
k := ts.Keepers.Dualstaking
bondDenom := commontypes.TokenDenom
timeNow := ctx.BlockTime()
tests := []struct {
amount sdk.Coin
expectedMonthlyCredit sdk.Coin
Expand Down Expand Up @@ -303,7 +301,7 @@ func TestDelegationSet(t *testing.T) {
{ // 6
timeWait: 15 * time.Hour * 24,
amount: sdk.NewCoin(bondDenom, sdk.NewInt(500*720)),
expectedMonthlyCredit: sdk.NewCoin(bondDenom, sdk.NewInt(1000*720/2+500*720/2)),
expectedMonthlyCredit: sdk.NewCoin(bondDenom, sdk.NewInt(1000*720/2+500*720/2)), // 540000
},
{ // 7
timeWait: 15 * time.Hour * 24,
Expand Down Expand Up @@ -372,16 +370,16 @@ func TestDelegationSet(t *testing.T) {
Amount: tests[iteration].amount,
}
if tests[iteration].remove {
k.RemoveDelegation(ctx, delegation)
k.RemoveDelegation(ts.Ctx, delegation)
}
timeNow = ctx.BlockTime()
ctx = ctx.WithBlockTime(timeNow.Add(tests[iteration].timeWait))
utils.LavaFormatDebug("block times for credit", utils.LogAttr("block time", ts.Ctx.BlockTime()), utils.LogAttr("time wait", tests[iteration].timeWait))
ts.Ctx = ts.Ctx.WithBlockTime(ts.Ctx.BlockTime().Add(tests[iteration].timeWait))

err := k.SetDelegation(ctx, delegation)
err := k.SetDelegation(ts.Ctx, delegation)
require.NoError(t, err)
delegationGot, found := k.GetDelegation(ctx, delegation.Provider, delegation.Delegator)
delegationGot, found := k.GetDelegation(ts.Ctx, delegation.Provider, delegation.Delegator)
require.True(t, found)
monthlyCredit := k.CalculateMonthlyCredit(ctx, delegationGot)
monthlyCredit := k.CalculateMonthlyCredit(ts.Ctx, delegationGot)
require.Equal(t, tests[iteration].expectedMonthlyCredit, monthlyCredit)
})
}
Expand Down

0 comments on commit 8f3ee81

Please sign in to comment.