-
Notifications
You must be signed in to change notification settings - Fork 608
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Genesis upgrade and add invariant cherry pick (#1081)
* add TotalSuperfluidDelegationInvariant * update invariant name * Update x/superfluid/keeper/invariants.go Co-authored-by: Dev Ojha <[email protected]> * Update x/superfluid/keeper/invariants.go Co-authored-by: Dev Ojha <[email protected]> * revert back changes for SetLockIdIntermediaryAccountConnection * convert to round * add invariant checker function call on all superfluid tests * update go mod/sum * add after validator slashed hook * following updates for main branch cherry-pick Co-authored-by: Dev Ojha <[email protected]>
- Loading branch information
1 parent
f897502
commit a454116
Showing
16 changed files
with
527 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package keeper | ||
|
||
import ( | ||
"fmt" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
|
||
"github.com/osmosis-labs/osmosis/v7/x/superfluid/types" | ||
) | ||
|
||
const totalSuperfluidDelegationInvariantName = "total-superfluid-delegation-invariant-name" | ||
|
||
// RegisterInvariants registers all governance invariants | ||
func RegisterInvariants(ir sdk.InvariantRegistry, keeper Keeper) { | ||
ir.RegisterRoute(types.ModuleName, totalSuperfluidDelegationInvariantName, TotalSuperfluidDelegationInvariant(keeper)) | ||
} | ||
|
||
// AllInvariants runs all invariants of the gamm module | ||
func AllInvariants(keeper Keeper) sdk.Invariant { | ||
return func(ctx sdk.Context) (string, bool) { | ||
return TotalSuperfluidDelegationInvariant(keeper)(ctx) | ||
} | ||
} | ||
|
||
// TotalSuperfluidDelegationInvariant checks the sum of intermediary account delegation is same as sum of individual lockup delegation | ||
func TotalSuperfluidDelegationInvariant(keeper Keeper) sdk.Invariant { | ||
return func(ctx sdk.Context) (string, bool) { | ||
accs := keeper.GetAllIntermediaryAccounts(ctx) | ||
totalSuperfluidDelegationTokens := sdk.ZeroDec() | ||
|
||
// Compute the total amount delegated from all intermediary accounts | ||
for _, acc := range accs { | ||
valAddr, err := sdk.ValAddressFromBech32(acc.ValAddr) | ||
if err != nil { | ||
return sdk.FormatInvariant(types.ModuleName, totalSuperfluidDelegationInvariantName, | ||
"\tinvalid validator address exists"), true | ||
} | ||
validator, found := keeper.sk.GetValidator(ctx, valAddr) | ||
if !found { | ||
return sdk.FormatInvariant(types.ModuleName, totalSuperfluidDelegationInvariantName, | ||
"\tvalidator does not exists for specified validator address on intermediary account"), true | ||
} | ||
delegation, found := keeper.sk.GetDelegation(ctx, acc.GetAccAddress(), valAddr) | ||
if found { | ||
tokens := validator.TokensFromShares(delegation.Shares) | ||
totalSuperfluidDelegationTokens = totalSuperfluidDelegationTokens.Add(tokens) | ||
} | ||
} | ||
|
||
// Compute the total delegation amount expected | ||
// from every lockID intermediary account connections | ||
totalExpectedSuperfluidAmount := sdk.ZeroInt() | ||
connections := keeper.GetAllLockIdIntermediaryAccountConnections(ctx) | ||
for _, connection := range connections { | ||
lockId := connection.LockId | ||
lock, err := keeper.lk.GetLockByID(ctx, lockId) | ||
if err != nil || lock == nil { | ||
return sdk.FormatInvariant(types.ModuleName, totalSuperfluidDelegationInvariantName, | ||
"\tinvalid superfluid lock id exists with no actual lockup"), true | ||
} | ||
if len(lock.Coins) != 1 { | ||
return sdk.FormatInvariant(types.ModuleName, totalSuperfluidDelegationInvariantName, | ||
"\tonly single coin lockup is eligible for superfluid staking"), true | ||
} | ||
amount := keeper.GetSuperfluidOSMOTokens(ctx, lock.Coins[0].Denom, lock.Coins[0].Amount) | ||
totalExpectedSuperfluidAmount = totalExpectedSuperfluidAmount.Add(amount) | ||
} | ||
|
||
if !totalExpectedSuperfluidAmount.Equal(totalSuperfluidDelegationTokens.TruncateInt()) { | ||
return sdk.FormatInvariant(types.ModuleName, | ||
totalSuperfluidDelegationInvariantName, | ||
fmt.Sprintf("\ttotal superfluid intermediary account delegation amount does not match total sum of lockup delegations: %s != %s\n", totalExpectedSuperfluidAmount.String(), totalSuperfluidDelegationTokens.String())), | ||
true | ||
} | ||
|
||
return sdk.FormatInvariant(types.ModuleName, totalSuperfluidDelegationInvariantName, | ||
"\ttotal superfluid intermediary account delegation amount matches total sum of lockup delegations\n"), false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.