github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/cosmos-sdk/x/gov/keeper/invariants.go (about)

     1  package keeper
     2  
     3  // DONTCOVER
     4  
     5  import (
     6  	"fmt"
     7  
     8  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
     9  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/gov/types"
    10  )
    11  
    12  // RegisterInvariants registers all governance invariants
    13  func RegisterInvariants(ir sdk.InvariantRegistry, keeper Keeper) {
    14  	ir.RegisterRoute(types.ModuleName, "module-account", ModuleAccountInvariant(keeper))
    15  }
    16  
    17  // AllInvariants runs all invariants of the governance module
    18  func AllInvariants(keeper Keeper) sdk.Invariant {
    19  	return func(ctx sdk.Context) (string, bool) {
    20  		return ModuleAccountInvariant(keeper)(ctx)
    21  	}
    22  }
    23  
    24  // ModuleAccountInvariant checks that the module account coins reflects the sum of
    25  // deposit amounts held on store
    26  func ModuleAccountInvariant(keeper Keeper) sdk.Invariant {
    27  	return func(ctx sdk.Context) (string, bool) {
    28  		var expectedDeposits sdk.Coins
    29  
    30  		keeper.IterateAllDeposits(ctx, func(deposit types.Deposit) bool {
    31  			expectedDeposits = expectedDeposits.Add(deposit.Amount...)
    32  			return false
    33  		})
    34  
    35  		macc := keeper.GetGovernanceAccount(ctx)
    36  		broken := !macc.GetCoins().IsEqual(expectedDeposits)
    37  
    38  		return sdk.FormatInvariant(types.ModuleName, "deposits",
    39  			fmt.Sprintf("\tgov ModuleAccount coins: %s\n\tsum of deposit amounts:  %s\n",
    40  				macc.GetCoins(), expectedDeposits)), broken
    41  	}
    42  }