-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add invariants to x/foundation (#758)
* Add module account invariant * Fix InitGenesis order * Add proposal invariant * Add total weight invariant * Remove reverse dependency on stakingplus * Update CHANGELOG.md
- Loading branch information
Showing
10 changed files
with
185 additions
and
28 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package keeper | ||
|
||
import ( | ||
"fmt" | ||
|
||
sdk "github.com/line/lbm-sdk/types" | ||
"github.com/line/lbm-sdk/x/foundation" | ||
) | ||
|
||
const ( | ||
moduleAccountInvariant = "module-accounts" | ||
totalWeightInvariant = "total-weight" | ||
proposalInvariant = "proposals" | ||
) | ||
|
||
func RegisterInvariants(ir sdk.InvariantRegistry, k Keeper) { | ||
ir.RegisterRoute(foundation.ModuleName, moduleAccountInvariant, ModuleAccountInvariant(k)) | ||
ir.RegisterRoute(foundation.ModuleName, totalWeightInvariant, ProposalInvariant(k)) | ||
ir.RegisterRoute(foundation.ModuleName, proposalInvariant, ProposalInvariant(k)) | ||
} | ||
|
||
func ModuleAccountInvariant(k Keeper) sdk.Invariant { | ||
return func(ctx sdk.Context) (string, bool) { | ||
// cache, we don't want to write changes | ||
ctx, _ = ctx.CacheContext() | ||
|
||
treasuryAcc := k.authKeeper.GetModuleAccount(ctx, foundation.TreasuryName) | ||
balance := k.bankKeeper.GetAllBalances(ctx, treasuryAcc.GetAddress()) | ||
|
||
treasury := k.GetTreasury(ctx) | ||
msg := fmt.Sprintf("coins in the treasury; expected %s, got %s\n", treasury, balance) | ||
broken := !treasury.IsEqual(sdk.NewDecCoinsFromCoins(balance...)) | ||
|
||
return sdk.FormatInvariant(foundation.ModuleName, moduleAccountInvariant, msg), broken | ||
} | ||
} | ||
|
||
func TotalWeightInvariant(k Keeper) sdk.Invariant { | ||
return func(ctx sdk.Context) (string, bool) { | ||
// cache, we don't want to write changes | ||
ctx, _ = ctx.CacheContext() | ||
|
||
expected := k.GetFoundationInfo(ctx).TotalWeight | ||
real := sdk.NewDec(int64(len(k.GetMembers(ctx)))) | ||
|
||
msg := fmt.Sprintf("total weight of foundation; expected %s, got %s\n", expected, real) | ||
broken := !real.Equal(expected) | ||
|
||
return sdk.FormatInvariant(foundation.ModuleName, totalWeightInvariant, msg), broken | ||
} | ||
} | ||
|
||
func ProposalInvariant(k Keeper) sdk.Invariant { | ||
return func(ctx sdk.Context) (string, bool) { | ||
// cache, we don't want to write changes | ||
ctx, _ = ctx.CacheContext() | ||
|
||
version := k.GetFoundationInfo(ctx).Version | ||
msg := fmt.Sprintf("current foundation version; %d\n", version) | ||
broken := false | ||
|
||
k.iterateProposals(ctx, func(proposal foundation.Proposal) (stop bool) { | ||
if proposal.FoundationVersion == version { | ||
return true | ||
} | ||
|
||
if proposal.Status == foundation.PROPOSAL_STATUS_SUBMITTED { | ||
msg += fmt.Sprintf("active old proposal %d found; version %d\n", proposal.Id, proposal.FoundationVersion) | ||
broken = true | ||
} | ||
|
||
return false | ||
}) | ||
|
||
return sdk.FormatInvariant(foundation.ModuleName, proposalInvariant, msg), broken | ||
} | ||
} |
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,96 @@ | ||
package keeper_test | ||
|
||
import ( | ||
sdk "github.com/line/lbm-sdk/types" | ||
"github.com/line/lbm-sdk/x/foundation" | ||
"github.com/line/lbm-sdk/x/foundation/keeper" | ||
) | ||
|
||
func (s *KeeperTestSuite) TestModuleAccountInvariant() { | ||
testCases := map[string]struct { | ||
malleate func(ctx sdk.Context) | ||
valid bool | ||
}{ | ||
"invariant not broken": { | ||
valid: true, | ||
}, | ||
"treasury differs from the balance": { | ||
malleate: func(ctx sdk.Context) { | ||
balance := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, s.balance.Add(sdk.OneInt()))) | ||
s.keeper.SetPool(ctx, foundation.Pool{ | ||
Treasury: sdk.NewDecCoinsFromCoins(balance...), | ||
}) | ||
}, | ||
}, | ||
} | ||
|
||
for name, tc := range testCases { | ||
ctx, _ := s.ctx.CacheContext() | ||
if tc.malleate != nil { | ||
tc.malleate(ctx) | ||
} | ||
|
||
invariant := keeper.ModuleAccountInvariant(s.keeper) | ||
_, broken := invariant(ctx) | ||
s.Require().Equal(!tc.valid, broken, name) | ||
} | ||
} | ||
|
||
func (s *KeeperTestSuite) TestTotalWeightInvariant() { | ||
testCases := map[string]struct { | ||
malleate func(ctx sdk.Context) | ||
valid bool | ||
}{ | ||
"invariant not broken": { | ||
valid: true, | ||
}, | ||
"total weight differs from the number of foundation members": { | ||
malleate: func(ctx sdk.Context) { | ||
info := s.keeper.GetFoundationInfo(ctx) | ||
numMembers := len(s.keeper.GetMembers(ctx)) | ||
info.TotalWeight = sdk.NewDec(int64(numMembers)).Add(sdk.OneDec()) | ||
s.keeper.SetFoundationInfo(ctx, info) | ||
}, | ||
}, | ||
} | ||
|
||
for name, tc := range testCases { | ||
ctx, _ := s.ctx.CacheContext() | ||
if tc.malleate != nil { | ||
tc.malleate(ctx) | ||
} | ||
|
||
invariant := keeper.TotalWeightInvariant(s.keeper) | ||
_, broken := invariant(ctx) | ||
s.Require().Equal(!tc.valid, broken, name) | ||
} | ||
} | ||
|
||
func (s *KeeperTestSuite) TestProposalInvariant() { | ||
testCases := map[string]struct { | ||
malleate func(ctx sdk.Context) | ||
valid bool | ||
}{ | ||
"invariant not broken": { | ||
valid: true, | ||
}, | ||
"active old proposal exists": { | ||
malleate: func(ctx sdk.Context) { | ||
info := s.keeper.GetFoundationInfo(ctx) | ||
info.Version-- | ||
s.keeper.SetFoundationInfo(ctx, info) | ||
}, | ||
}, | ||
} | ||
|
||
for name, tc := range testCases { | ||
ctx, _ := s.ctx.CacheContext() | ||
if tc.malleate != nil { | ||
tc.malleate(ctx) | ||
} | ||
|
||
invariant := keeper.ProposalInvariant(s.keeper) | ||
_, broken := invariant(ctx) | ||
s.Require().Equal(!tc.valid, broken, name) | ||
} | ||
} |
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