-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
genesis.go
195 lines (167 loc) · 5.45 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package types
import (
"encoding/json"
"fmt"
"sort"
proto "github.com/cosmos/gogoproto/proto"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
)
var _ types.UnpackInterfacesMessage = GenesisState{}
// RandomGenesisAccountsFn defines the function required to generate custom account types
type RandomGenesisAccountsFn func(simState *module.SimulationState) GenesisAccounts
// NewGenesisState - Create a new genesis state
func NewGenesisState(params Params, accounts GenesisAccounts) *GenesisState {
genAccounts, err := PackAccounts(accounts)
if err != nil {
panic(err)
}
return &GenesisState{
Params: params,
Accounts: genAccounts,
}
}
// UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces
func (g GenesisState) UnpackInterfaces(unpacker types.AnyUnpacker) error {
for _, any := range g.Accounts {
var account GenesisAccount
err := unpacker.UnpackAny(any, &account)
if err != nil {
return err
}
}
return nil
}
// DefaultGenesisState - Return a default genesis state
func DefaultGenesisState() *GenesisState {
return NewGenesisState(DefaultParams(), GenesisAccounts{})
}
// GetGenesisStateFromAppState returns x/auth GenesisState given raw application
// genesis state.
func GetGenesisStateFromAppState(cdc codec.Codec, appState map[string]json.RawMessage) GenesisState {
var genesisState GenesisState
if appState[ModuleName] != nil {
cdc.MustUnmarshalJSON(appState[ModuleName], &genesisState)
}
return genesisState
}
// ValidateGenesis performs basic validation of auth genesis data returning an
// error for any failed validation criteria.
func ValidateGenesis(data GenesisState) error {
if err := data.Params.Validate(); err != nil {
return err
}
genAccs, err := UnpackAccounts(data.Accounts)
if err != nil {
return err
}
return ValidateGenAccounts(genAccs)
}
// SanitizeGenesisAccounts sorts accounts and coin sets.
func SanitizeGenesisAccounts(genAccs GenesisAccounts) GenesisAccounts {
// Make sure there aren't any duplicated account numbers by fixing the duplicates with the lowest unused values.
// seenAccNum = easy lookup for used account numbers.
seenAccNum := map[uint64]bool{}
// dupAccNum = a map of account number to accounts with duplicate account numbers (excluding the 1st one seen).
dupAccNum := map[uint64]GenesisAccounts{}
for _, acc := range genAccs {
num := acc.GetAccountNumber()
if !seenAccNum[num] {
seenAccNum[num] = true
} else {
dupAccNum[num] = append(dupAccNum[num], acc)
}
}
// dupAccNums a sorted list of the account numbers with duplicates.
var dupAccNums []uint64
for num := range dupAccNum {
dupAccNums = append(dupAccNums, num)
}
sort.Slice(dupAccNums, func(i, j int) bool {
return dupAccNums[i] < dupAccNums[j]
})
// Change the account number of the duplicated ones to the first unused value.
globalNum := uint64(0)
for _, dupNum := range dupAccNums {
accs := dupAccNum[dupNum]
for _, acc := range accs {
for seenAccNum[globalNum] {
globalNum++
}
if err := acc.SetAccountNumber(globalNum); err != nil {
panic(err)
}
seenAccNum[globalNum] = true
}
}
// Then sort them all by account number.
sort.Slice(genAccs, func(i, j int) bool {
return genAccs[i].GetAccountNumber() < genAccs[j].GetAccountNumber()
})
return genAccs
}
// ValidateGenAccounts validates an array of GenesisAccounts and checks for duplicates
func ValidateGenAccounts(accounts GenesisAccounts) error {
addrMap := make(map[string]bool, len(accounts))
for _, acc := range accounts {
// check for duplicated accounts
addrStr := acc.GetAddress().String()
if _, ok := addrMap[addrStr]; ok {
return fmt.Errorf("duplicate account found in genesis state; address: %s", addrStr)
}
addrMap[addrStr] = true
// check account specific validation
if err := acc.Validate(); err != nil {
return fmt.Errorf("invalid account found in genesis state; address: %s, error: %s", addrStr, err.Error())
}
}
return nil
}
// GenesisAccountIterator implements genesis account iteration.
type GenesisAccountIterator struct{}
// IterateGenesisAccounts iterates over all the genesis accounts found in
// appGenesis and invokes a callback on each genesis account. If any call
// returns true, iteration stops.
func (GenesisAccountIterator) IterateGenesisAccounts(
cdc codec.Codec, appGenesis map[string]json.RawMessage, cb func(sdk.AccountI) (stop bool),
) {
for _, genAcc := range GetGenesisStateFromAppState(cdc, appGenesis).Accounts {
acc, ok := genAcc.GetCachedValue().(sdk.AccountI)
if !ok {
panic("expected account")
}
if cb(acc) {
break
}
}
}
// PackAccounts converts GenesisAccounts to Any slice
func PackAccounts(accounts GenesisAccounts) ([]*types.Any, error) {
accountsAny := make([]*types.Any, len(accounts))
for i, acc := range accounts {
msg, ok := acc.(proto.Message)
if !ok {
return nil, fmt.Errorf("cannot proto marshal %T", acc)
}
any, err := types.NewAnyWithValue(msg)
if err != nil {
return nil, err
}
accountsAny[i] = any
}
return accountsAny, nil
}
// UnpackAccounts converts Any slice to GenesisAccounts
func UnpackAccounts(accountsAny []*types.Any) (GenesisAccounts, error) {
accounts := make(GenesisAccounts, len(accountsAny))
for i, any := range accountsAny {
acc, ok := any.GetCachedValue().(GenesisAccount)
if !ok {
return nil, fmt.Errorf("expected genesis account")
}
accounts[i] = acc
}
return accounts, nil
}