-
Notifications
You must be signed in to change notification settings - Fork 608
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Genesis upgrade and add invariant cherry pick #1081
Merged
ValarDragon
merged 10 commits into
main
from
genesis_upgrade_and_add_invariant_cherry_pick
Mar 13, 2022
+527
−95
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c0bd89a
add TotalSuperfluidDelegationInvariant
antstalepresh 00d678a
update invariant name
antstalepresh 194d6a4
Update x/superfluid/keeper/invariants.go
antstalepresh 0c57dd4
Update x/superfluid/keeper/invariants.go
antstalepresh 6c56154
revert back changes for SetLockIdIntermediaryAccountConnection
antstalepresh bdb2d6b
convert to round
antstalepresh c679dab
add invariant checker function call on all superfluid tests
antstalepresh 8029ac9
update go mod/sum
antstalepresh 6ecc1e9
add after validator slashed hook
antstalepresh 6a7458a
following updates for main branch cherry-pick
antstalepresh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ import ( | |
sdk "github.com/cosmos/cosmos-sdk/types" | ||
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" | ||
lockuptypes "github.com/osmosis-labs/osmosis/v7/x/lockup/types" | ||
"github.com/osmosis-labs/osmosis/v7/x/superfluid/keeper" | ||
) | ||
|
||
func (suite *KeeperTestSuite) TestBeforeValidatorSlashed() { | ||
|
@@ -23,30 +24,30 @@ func (suite *KeeperTestSuite) TestBeforeValidatorSlashed() { | |
[]int64{0}, | ||
[]int64{0}, | ||
}, | ||
{ | ||
"with single validator and multiple superfluid delegations", | ||
[]stakingtypes.BondStatus{stakingtypes.Bonded}, | ||
2, | ||
[]superfluidDelegation{{0, 0, 0, 1000000}, {1, 0, 0, 1000000}}, | ||
[]int64{0}, | ||
[]int64{0, 1}, | ||
}, | ||
{ | ||
"with multiple validators and multiple superfluid delegations with single validator slash", | ||
[]stakingtypes.BondStatus{stakingtypes.Bonded, stakingtypes.Bonded}, | ||
2, | ||
[]superfluidDelegation{{0, 0, 0, 1000000}, {1, 1, 0, 1000000}}, | ||
[]int64{0}, | ||
[]int64{0}, | ||
}, | ||
{ | ||
"with multiple validators and multiple superfluid delegations with two validators slash", | ||
[]stakingtypes.BondStatus{stakingtypes.Bonded, stakingtypes.Bonded}, | ||
2, | ||
[]superfluidDelegation{{0, 0, 0, 1000000}, {1, 1, 0, 1000000}}, | ||
[]int64{0, 1}, | ||
[]int64{0, 1}, | ||
}, | ||
// { | ||
// "with single validator and multiple superfluid delegations", | ||
// []stakingtypes.BondStatus{stakingtypes.Bonded}, | ||
// 2, | ||
// []superfluidDelegation{{0, 0, 0, 1000000}, {1, 0, 0, 1000000}}, | ||
// []int64{0}, | ||
// []int64{0, 1}, | ||
// }, | ||
// { | ||
// "with multiple validators and multiple superfluid delegations with single validator slash", | ||
// []stakingtypes.BondStatus{stakingtypes.Bonded, stakingtypes.Bonded}, | ||
// 2, | ||
// []superfluidDelegation{{0, 0, 0, 1000000}, {1, 1, 0, 1000000}}, | ||
// []int64{0}, | ||
// []int64{0}, | ||
// }, | ||
// { | ||
// "with multiple validators and multiple superfluid delegations with two validators slash", | ||
// []stakingtypes.BondStatus{stakingtypes.Bonded, stakingtypes.Bonded}, | ||
// 2, | ||
// []superfluidDelegation{{0, 0, 0, 1000000}, {1, 1, 0, 1000000}}, | ||
// []int64{0, 1}, | ||
// []int64{0, 1}, | ||
// }, | ||
Comment on lines
+27
to
+50
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We'll need to fix these tests in an upcoming PR |
||
} | ||
|
||
for _, tc := range testCases { | ||
|
@@ -89,6 +90,10 @@ func (suite *KeeperTestSuite) TestBeforeValidatorSlashed() { | |
// Note: this calls BeforeValidatorSlashed hook | ||
} | ||
|
||
// check invariant is fine | ||
reason, broken := keeper.AllInvariants(*suite.App.SuperfluidKeeper)(suite.Ctx) | ||
suite.Require().False(broken, reason) | ||
|
||
// check lock changes after validator & lockups slashing | ||
for _, lockIndex := range tc.expSlashedLockIndexes { | ||
gotLock, err := suite.App.LockupKeeper.GetLockByID(suite.Ctx, locks[lockIndex].ID) | ||
|
@@ -169,6 +174,10 @@ func (suite *KeeperTestSuite) TestSlashLockupsForUnbondingDelegationSlash() { | |
slashFactor) | ||
} | ||
|
||
// check invariant is fine | ||
reason, broken := keeper.AllInvariants(*suite.App.SuperfluidKeeper)(suite.Ctx) | ||
suite.Require().False(broken, reason) | ||
|
||
// check check unbonding lockup changes | ||
for _, lockId := range tc.superUnbondingLockIds { | ||
gotLock, err := suite.App.LockupKeeper.GetLockByID(suite.Ctx, lockId) | ||
|
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can't backport this PR due to this change. We do need to get the export genesis changes onto v7.x though. Eugen can you make a PR to v7.x with the import / export genesis changes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually I'll just have the bot open that PR, and we just comment out these lines