2019-06-28 13:11:27 -07:00
|
|
|
package keeper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2020-02-18 04:50:13 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/supply/types"
|
2019-06-28 13:11:27 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// RegisterInvariants register all supply invariants
|
|
|
|
func RegisterInvariants(ir sdk.InvariantRegistry, k Keeper) {
|
|
|
|
ir.RegisterRoute(types.ModuleName, "total-supply", TotalSupply(k))
|
|
|
|
}
|
|
|
|
|
|
|
|
// AllInvariants runs all invariants of the supply module.
|
|
|
|
func AllInvariants(k Keeper) sdk.Invariant {
|
2019-07-11 03:56:43 -07:00
|
|
|
return func(ctx sdk.Context) (string, bool) {
|
2019-06-28 13:11:27 -07:00
|
|
|
return TotalSupply(k)(ctx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TotalSupply checks that the total supply reflects all the coins held in accounts
|
|
|
|
func TotalSupply(k Keeper) sdk.Invariant {
|
2019-07-11 03:56:43 -07:00
|
|
|
return func(ctx sdk.Context) (string, bool) {
|
2019-06-28 13:11:27 -07:00
|
|
|
var expectedTotal sdk.Coins
|
|
|
|
supply := k.GetSupply(ctx)
|
|
|
|
|
2020-01-30 13:31:16 -08:00
|
|
|
k.bk.IterateAllBalances(ctx, func(_ sdk.AccAddress, balance sdk.Coin) bool {
|
|
|
|
expectedTotal = expectedTotal.Add(balance)
|
2019-06-28 13:11:27 -07:00
|
|
|
return false
|
|
|
|
})
|
|
|
|
|
2019-08-01 11:56:50 -07:00
|
|
|
broken := !expectedTotal.IsEqual(supply.GetTotal())
|
2019-06-28 13:11:27 -07:00
|
|
|
|
2019-07-11 03:56:43 -07:00
|
|
|
return sdk.FormatInvariant(types.ModuleName, "total supply",
|
|
|
|
fmt.Sprintf(
|
|
|
|
"\tsum of accounts coins: %v\n"+
|
2019-07-15 09:56:38 -07:00
|
|
|
"\tsupply.Total: %v\n",
|
2019-08-05 11:21:44 -07:00
|
|
|
expectedTotal, supply.GetTotal())), broken
|
2019-06-28 13:11:27 -07:00
|
|
|
}
|
|
|
|
}
|