-
Notifications
You must be signed in to change notification settings - Fork 20
/
invariant.go
32 lines (28 loc) · 985 Bytes
/
invariant.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package mapper
import (
"fmt"
"github.com/QOSGroup/qbase/context"
btypes "github.com/QOSGroup/qbase/types"
qtypes "github.com/QOSGroup/qos/types"
)
// 账户数据检查
func AccountInvariant(module string) qtypes.Invariant {
return func(ctx context.Context) (string, btypes.BaseCoins, bool) {
var msg string
coins := btypes.BaseCoins{}
var count int
accounts := GetAccounts(ctx)
for _, account := range accounts {
coins = coins.Plus(append(btypes.BaseCoins{btypes.NewBaseCoin(qtypes.QOSCoinName, account.QOS)}, account.QSCs...))
// 不能有负值
if account.QOS.LT(btypes.ZeroInt()) || !account.QSCs.IsNotNegative() {
count++
msg += fmt.Sprintf("account %s has a negative values %s %s\n",
account.AccountAddress.String(), account.QOS.String(), account.QSCs.String())
}
}
broken := count != 0
return qtypes.FormatInvariant(module, "account",
fmt.Sprintf("amount of negative accounts found %d\n%s", count, msg), coins, broken)
}
}