-
Notifications
You must be signed in to change notification settings - Fork 28
/
genesis.go
120 lines (95 loc) · 2.74 KB
/
genesis.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package types
import (
errorsmod "cosmossdk.io/errors"
"github.com/irisnet/irismod/modules/mt/exported"
)
// NewGenesisState creates a new genesis state.
func NewGenesisState(collections []Collection, owners []Owner) *GenesisState {
return &GenesisState{
Collections: collections,
Owners: owners,
}
}
// ValidateGenesis performs basic validation of mts genesis data returning an
// error for any failed validation criteria.
func ValidateGenesis(data GenesisState) error {
var denomMap1 map[string]int
denomMap1 = make(map[string]int)
var mtMap1 map[string]uint64
mtMap1 = make(map[string]uint64)
for _, c := range data.Collections {
denomMap1[c.Denom.Id] = len(c.Mts)
for _, m := range c.Mts {
mtMap1[c.Denom.Id+m.Id] = m.Supply
}
}
mtCount1 := len(mtMap1)
// --------------------------
var denomMap2 map[string]bool
denomMap2 = make(map[string]bool)
var mtMap2 map[string]uint64
mtMap2 = make(map[string]uint64)
for _, o := range data.Owners {
for _, d := range o.Denoms {
denomMap2[d.DenomId] = true
if _, ok := denomMap1[d.DenomId]; !ok {
return errorsmod.Wrapf(errorsmod.ErrPanic, "unknown mt denom, (%s)", d.DenomId)
}
for _, b := range d.Balances {
mtMap2[d.DenomId+b.MtId] = mtMap2[d.DenomId+b.MtId] + b.Amount
}
}
}
mtCount2 := len(mtMap2)
if mtCount1 != mtCount2 {
return errorsmod.Wrapf(errorsmod.ErrPanic, "mt count mismatch, (%d, %d)", mtCount1, mtCount2)
}
for id1, supply1 := range mtMap1 {
supply2 := mtMap2[id1]
if supply1 != supply2 {
return errorsmod.Wrapf(errorsmod.ErrPanic, "mt supply mismatch, id: %s (%d, %d)", id1, supply1, mtCount2)
}
}
return nil
}
// ---------------------------------------------
// NewCollection creates a new MT Collection
func NewCollection(denom Denom, mts []exported.MT) (c Collection) {
c.Denom = &denom
for _, mt := range mts {
c = c.AddMT(mt.(MT))
}
return c
}
// AddMT adds an MT to the collection
func (c Collection) AddMT(mt MT) Collection {
c.Mts = append(c.Mts, mt)
return c
}
// Supply queries supply of a collection
func (c Collection) Supply() int {
return len(c.Mts)
}
// NewCollections creates collections
func NewCollections(c ...Collection) []Collection {
return append([]Collection{}, c...)
}
// ---------------------------------------------
// NewOwner creates a new owner balance
func NewOwner(address string, denoms []DenomBalance) (o Owner) {
o.Address = address
o.Denoms = denoms
return o
}
// NewDenomBalance creates a new denom balance
func NewDenomBalance(denomID string, balances []Balance) (d DenomBalance) {
d.DenomId = denomID
d.Balances = balances
return d
}
// NewBalance creates a new mt balance
func NewBalance(mtID string, amount uint64) (b Balance) {
b.MtId = mtID
b.Amount = amount
return b
}