Skip to content

Commit

Permalink
fix delegate set to account for existing delegation
Browse files Browse the repository at this point in the history
  • Loading branch information
omerlavanet committed Nov 26, 2024
1 parent 7c3f1a7 commit 2b56061
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
8 changes: 7 additions & 1 deletion x/dualstaking/keeper/delegate.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,9 +364,15 @@ func (k Keeper) GetAllDelegations(ctx sdk.Context) ([]types.Delegation, error) {
}

func (k Keeper) SetDelegation(ctx sdk.Context, delegation types.Delegation) error {
credit, creditTimestamp := k.CalculateCredit(ctx, delegation)
existingDelegation, found := k.GetDelegation(ctx, delegation.Provider, delegation.Delegator)
if !found {
return k.delegations.Set(ctx, types.DelegationKey(delegation.Provider, delegation.Delegator), delegation)
}
// calculate credit based on the existing delegation before changes
credit, creditTimestamp := k.CalculateCredit(ctx, existingDelegation)
delegation.Credit = credit
delegation.CreditTimestamp = creditTimestamp
delegation.Timestamp = ctx.BlockTime().UTC().Unix() // after setting credit we reset the delegation timestamp
return k.delegations.Set(ctx, types.DelegationKey(delegation.Provider, delegation.Delegator), delegation)
}

Expand Down
19 changes: 12 additions & 7 deletions x/dualstaking/keeper/delegate_credit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ func TestCalculateCredit(t *testing.T) {
bondDenom := commontypes.TokenDenom
timeNow := ctx.BlockTime()
tests := []struct {
name string
delegation types.Delegation
expectedCredit sdk.Coin
name string
delegation types.Delegation
expectedCredit sdk.Coin
expectedCreditTimestamp int64
}{
{
name: "initial delegation",
Expand All @@ -36,7 +37,8 @@ func TestCalculateCredit(t *testing.T) {
Timestamp: timeNow.Add(-time.Hour * 24 * 10).Unix(),
CreditTimestamp: 0,
},
expectedCredit: sdk.NewCoin(bondDenom, sdk.NewInt(1000)),
expectedCredit: sdk.NewCoin(bondDenom, sdk.NewInt(1000)),
expectedCreditTimestamp: timeNow.Add(-time.Hour * 24 * 10).Unix(),
},
{
name: "delegation with existing credit",
Expand All @@ -46,7 +48,8 @@ func TestCalculateCredit(t *testing.T) {
Timestamp: timeNow.Add(-time.Hour * 24 * 5).Unix(),
CreditTimestamp: timeNow.Add(-time.Hour * 24 * 10).Unix(),
},
expectedCredit: sdk.NewCoin(bondDenom, sdk.NewInt(1500)),
expectedCredit: sdk.NewCoin(bondDenom, sdk.NewInt(1500)),
expectedCreditTimestamp: timeNow.Add(-time.Hour * 24 * 5).Unix(),
},
{
name: "delegation older than 30 days",
Expand All @@ -56,14 +59,16 @@ func TestCalculateCredit(t *testing.T) {
Timestamp: timeNow.Add(-time.Hour * 24 * 40).Unix(),
CreditTimestamp: timeNow.Add(-time.Hour * 24 * 35).Unix(),
},
expectedCredit: sdk.NewCoin(bondDenom, sdk.NewInt(3000)),
expectedCredit: sdk.NewCoin(bondDenom, sdk.NewInt(3000)),
expectedCreditTimestamp: timeNow.Add(-time.Hour * 24 * 40).Unix(),
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
credit, _ := k.CalculateCredit(ctx, tt.delegation)
credit, creditTimestamp := k.CalculateCredit(ctx, tt.delegation)
require.Equal(t, tt.expectedCredit, credit)
require.Equal(t, tt.expectedCreditTimestamp, creditTimestamp)
})
}
}
Expand Down

0 comments on commit 2b56061

Please sign in to comment.