Skip to content
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

feat: use CacheContext() in AssertInvariants() #1167

Merged
merged 2 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (feat) [\#1121](https://github.com/Finschia/finschia-sdk/pull/1121) Add update-censorship cmd to x/foundation cli
* (server) [#1153](https://github.com/Finschia/finschia-sdk/pull/1153) remove grpc replace directive
* (crypto) [\#1163](https://github.com/Finschia/finschia-sdk/pull/1163) Update some secp256k1 logics with latest `dcrec`
* (x/crisis) [#1167](https://github.com/Finschia/finschia-sdk/pull/1167) Use `CacheContext()` in `AssertInvariants()`

### Bug Fixes
* chore(deps) [\#1141](https://github.com/Finschia/finschia-sdk/pull/1141) Bump github.com/cosmos/ledger-cosmos-go from 0.12.2 to 0.13.2 to fix ledger signing issue
Expand Down
4 changes: 3 additions & 1 deletion x/crisis/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ func (k Keeper) AssertInvariants(ctx sdk.Context) {
n := len(invarRoutes)
for i, ir := range invarRoutes {
logger.Info("asserting crisis invariants", "inv", fmt.Sprint(i+1, "/", n), "name", ir.FullRoute())
if res, stop := ir.Invar(ctx); stop {

invCtx, _ := ctx.CacheContext()
if res, stop := ir.Invar(invCtx); stop {
// TODO: Include app name as part of context to allow for this to be
// variable.
panic(fmt.Errorf("invariant broken: %s\n"+
Expand Down
15 changes: 3 additions & 12 deletions x/foundation/keeper/internal/invariants.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ func RegisterInvariants(ir sdk.InvariantRegistry, k Keeper) {

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())

Expand All @@ -41,24 +38,18 @@ func ModuleAccountInvariant(k Keeper) sdk.Invariant {

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))))
actual := 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)
msg := fmt.Sprintf("total weight of foundation; expected %s, got %s\n", expected, actual)
broken := !actual.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
Expand Down
Loading