Skip to content

Commit

Permalink
fix: make ModuleAccountInvariants pass for IS SDK changes (#12783)
Browse files Browse the repository at this point in the history
* fix bug breaking ModuleAccountInvariants

* set UnbondingOnHold to false explicitly
  • Loading branch information
sainoe committed Feb 9, 2023
1 parent be019bb commit b308609
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 29 deletions.
8 changes: 4 additions & 4 deletions x/staking/keeper/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -511,10 +511,10 @@ func (k Keeper) SetRedelegationEntry(ctx sdk.Context,
red, found := k.GetRedelegation(ctx, delegatorAddr, validatorSrcAddr, validatorDstAddr)
id := k.IncrementUnbondingId(ctx)
if found {
red.AddEntry(creationHeight, minTime, balance, sharesDst, false, id)
red.AddEntry(creationHeight, minTime, balance, sharesDst, id)
} else {
red = types.NewRedelegation(delegatorAddr, validatorSrcAddr,
validatorDstAddr, creationHeight, minTime, balance, sharesDst, false, id)
validatorDstAddr, creationHeight, minTime, balance, sharesDst, id)
}

k.SetRedelegation(ctx, red)
Expand Down Expand Up @@ -865,7 +865,7 @@ func (k Keeper) CompleteUnbonding(ctx sdk.Context, delAddr sdk.AccAddress, valAd

// loop through all the entries and try to complete unbonding mature entries
for i := 0; i < len(ubd.Entries); i++ {
entry := &ubd.Entries[i]
entry := ubd.Entries[i]
if entry.IsMature(ctxTime) && !entry.UnbondingOnHold {
// Proceed with unbonding
ubd.RemoveEntry(int64(i))
Expand Down Expand Up @@ -969,7 +969,7 @@ func (k Keeper) CompleteRedelegation(

// loop through all the entries and try to complete mature redelegation entries
for i := 0; i < len(red.Entries); i++ {
entry := &red.Entries[i]
entry := red.Entries[i]
if entry.IsMature(ctxTime) && !entry.UnbondingOnHold {
red.RemoveEntry(int64(i))
i--
Expand Down
4 changes: 2 additions & 2 deletions x/staking/keeper/delegation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ func TestGetRedelegationsFromSrcValidator(t *testing.T) {

rd := types.NewRedelegation(addrDels[0], addrVals[0], addrVals[1], 0,
time.Unix(0, 0), sdk.NewInt(5),
sdk.NewDec(5), false, 1)
sdk.NewDec(5), 1)

// set and retrieve a record
app.StakingKeeper.SetRedelegation(ctx, rd)
Expand All @@ -643,7 +643,7 @@ func TestRedelegation(t *testing.T) {

rd := types.NewRedelegation(addrDels[0], addrVals[0], addrVals[1], 0,
time.Unix(0, 0).UTC(), sdk.NewInt(5),
sdk.NewDec(5), false, 1)
sdk.NewDec(5), 1)

// test shouldn't have and redelegations
has := app.StakingKeeper.HasReceivingRedelegation(ctx, addrDels[0], addrVals[1])
Expand Down
1 change: 0 additions & 1 deletion x/staking/keeper/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,6 @@ func RedelegationsToRedelegationResponses(
entry.SharesDst,
entry.InitialBalance,
val.TokensFromShares(entry.SharesDst).TruncateInt(),
entry.UnbondingOnHold,
entry.UnbondingId,
)
}
Expand Down
6 changes: 3 additions & 3 deletions x/staking/keeper/slash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func TestSlashRedelegation(t *testing.T) {
// set a redelegation with an expiration timestamp beyond which the
// redelegation shouldn't be slashed
rd := types.NewRedelegation(addrDels[0], addrVals[0], addrVals[1], 0,
time.Unix(5, 0), sdk.NewInt(10), sdk.NewDec(10), false, 1)
time.Unix(5, 0), sdk.NewInt(10), sdk.NewDec(10), 1)

app.StakingKeeper.SetRedelegation(ctx, rd)

Expand Down Expand Up @@ -390,7 +390,7 @@ func TestSlashWithRedelegation(t *testing.T) {
// set a redelegation
rdTokens := app.StakingKeeper.TokensFromConsensusPower(ctx, 6)
rd := types.NewRedelegation(addrDels[0], addrVals[0], addrVals[1], 11,
time.Unix(0, 0), rdTokens, rdTokens.ToDec(), false, 1)
time.Unix(0, 0), rdTokens, rdTokens.ToDec(), 1)
app.StakingKeeper.SetRedelegation(ctx, rd)

// set the associated delegation
Expand Down Expand Up @@ -544,7 +544,7 @@ func TestSlashBoth(t *testing.T) {
rdATokens := app.StakingKeeper.TokensFromConsensusPower(ctx, 6)
rdA := types.NewRedelegation(addrDels[0], addrVals[0], addrVals[1], 11,
time.Unix(0, 0), rdATokens,
rdATokens.ToDec(), false, 1)
rdATokens.ToDec(), 1)
app.StakingKeeper.SetRedelegation(ctx, rdA)

// set the associated delegation
Expand Down
2 changes: 1 addition & 1 deletion x/staking/simulation/decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestDecodeStore(t *testing.T) {
require.NoError(t, err)
del := types.NewDelegation(delAddr1, valAddr1, sdk.OneDec())
ubd := types.NewUnbondingDelegation(delAddr1, valAddr1, 15, bondTime, sdk.OneInt(), 1)
red := types.NewRedelegation(delAddr1, valAddr1, valAddr1, 12, bondTime, sdk.OneInt(), sdk.OneDec(), false, 0)
red := types.NewRedelegation(delAddr1, valAddr1, valAddr1, 12, bondTime, sdk.OneInt(), sdk.OneDec(), 0)

kvPairs := kv.Pairs{
Pairs: []kv.Pair{
Expand Down
27 changes: 14 additions & 13 deletions x/staking/types/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,12 @@ func (d Delegations) String() (out string) {

func NewUnbondingDelegationEntry(creationHeight int64, completionTime time.Time, balance sdk.Int, id uint64) UnbondingDelegationEntry {
return UnbondingDelegationEntry{
CreationHeight: creationHeight,
CompletionTime: completionTime,
InitialBalance: balance,
Balance: balance,
UnbondingId: id,
CreationHeight: creationHeight,
CompletionTime: completionTime,
InitialBalance: balance,
Balance: balance,
UnbondingId: id,
UnbondingOnHold: false,
}
}

Expand Down Expand Up @@ -210,14 +211,14 @@ func (ubds UnbondingDelegations) String() (out string) {
return strings.TrimSpace(out)
}

func NewRedelegationEntry(creationHeight int64, completionTime time.Time, balance sdk.Int, sharesDst sdk.Dec, onHold bool, id uint64) RedelegationEntry {
func NewRedelegationEntry(creationHeight int64, completionTime time.Time, balance sdk.Int, sharesDst sdk.Dec, id uint64) RedelegationEntry {
return RedelegationEntry{
CreationHeight: creationHeight,
CompletionTime: completionTime,
InitialBalance: balance,
SharesDst: sharesDst,
UnbondingOnHold: onHold,
UnbondingId: id,
UnbondingOnHold: false,
}
}

Expand All @@ -235,21 +236,21 @@ func (e RedelegationEntry) IsMature(currentTime time.Time) bool {
//nolint:interfacer
func NewRedelegation(
delegatorAddr sdk.AccAddress, validatorSrcAddr, validatorDstAddr sdk.ValAddress,
creationHeight int64, minTime time.Time, balance sdk.Int, sharesDst sdk.Dec, onHold bool, unbondingId uint64,
creationHeight int64, minTime time.Time, balance sdk.Int, sharesDst sdk.Dec, unbondingId uint64,
) Redelegation {
return Redelegation{
DelegatorAddress: delegatorAddr.String(),
ValidatorSrcAddress: validatorSrcAddr.String(),
ValidatorDstAddress: validatorDstAddr.String(),
Entries: []RedelegationEntry{
NewRedelegationEntry(creationHeight, minTime, balance, sharesDst, onHold, unbondingId),
NewRedelegationEntry(creationHeight, minTime, balance, sharesDst, unbondingId),
},
}
}

// AddEntry - append entry to the unbonding delegation
func (red *Redelegation) AddEntry(creationHeight int64, minTime time.Time, balance sdk.Int, sharesDst sdk.Dec, onHold bool, unbondingId uint64) {
entry := NewRedelegationEntry(creationHeight, minTime, balance, sharesDst, onHold, unbondingId)
func (red *Redelegation) AddEntry(creationHeight int64, minTime time.Time, balance sdk.Int, sharesDst sdk.Dec, unbondingId uint64) {
entry := NewRedelegationEntry(creationHeight, minTime, balance, sharesDst, unbondingId)
red.Entries = append(red.Entries, entry)
}

Expand Down Expand Up @@ -376,9 +377,9 @@ func NewRedelegationResponse(

// NewRedelegationEntryResponse creates a new RedelegationEntryResponse instance.
func NewRedelegationEntryResponse(
creationHeight int64, completionTime time.Time, sharesDst sdk.Dec, initialBalance, balance sdk.Int, onHold bool, id uint64) RedelegationEntryResponse {
creationHeight int64, completionTime time.Time, sharesDst sdk.Dec, initialBalance, balance sdk.Int, id uint64) RedelegationEntryResponse {
return RedelegationEntryResponse{
RedelegationEntry: NewRedelegationEntry(creationHeight, completionTime, initialBalance, sharesDst, onHold, id),
RedelegationEntry: NewRedelegationEntry(creationHeight, completionTime, initialBalance, sharesDst, id),
Balance: balance,
}
}
Expand Down
10 changes: 5 additions & 5 deletions x/staking/types/delegation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ func TestUnbondingDelegationString(t *testing.T) {
func TestRedelegationEqual(t *testing.T) {
r1 := types.NewRedelegation(sdk.AccAddress(valAddr1), valAddr2, valAddr3, 0,
time.Unix(0, 0), sdk.NewInt(0),
sdk.NewDec(0), false, 1)
sdk.NewDec(0), 1)
r2 := types.NewRedelegation(sdk.AccAddress(valAddr1), valAddr2, valAddr3, 0,
time.Unix(0, 0), sdk.NewInt(0),
sdk.NewDec(0), false, 2)
sdk.NewDec(0), 2)

ok := r1.String() == r2.String()
require.True(t, ok)
Expand All @@ -74,7 +74,7 @@ func TestRedelegationEqual(t *testing.T) {
func TestRedelegationString(t *testing.T) {
r := types.NewRedelegation(sdk.AccAddress(valAddr1), valAddr2, valAddr3, 0,
time.Unix(0, 0), sdk.NewInt(0),
sdk.NewDec(10), false, 1)
sdk.NewDec(10), 1)

require.NotEmpty(t, r.String())
}
Expand Down Expand Up @@ -111,8 +111,8 @@ func TestDelegationResponses(t *testing.T) {
func TestRedelegationResponses(t *testing.T) {
cdc := codec.NewLegacyAmino()
entries := []types.RedelegationEntryResponse{
types.NewRedelegationEntryResponse(0, time.Unix(0, 0), sdk.NewDec(5), sdk.NewInt(5), sdk.NewInt(5), false, 0),
types.NewRedelegationEntryResponse(0, time.Unix(0, 0), sdk.NewDec(5), sdk.NewInt(5), sdk.NewInt(5), false, 0),
types.NewRedelegationEntryResponse(0, time.Unix(0, 0), sdk.NewDec(5), sdk.NewInt(5), sdk.NewInt(5), 0),
types.NewRedelegationEntryResponse(0, time.Unix(0, 0), sdk.NewDec(5), sdk.NewInt(5), sdk.NewInt(5), 0),
}
rdr1 := types.NewRedelegationResponse(sdk.AccAddress(valAddr1), valAddr2, valAddr3, entries)
rdr2 := types.NewRedelegationResponse(sdk.AccAddress(valAddr2), valAddr1, valAddr3, entries)
Expand Down

0 comments on commit b308609

Please sign in to comment.