-
Notifications
You must be signed in to change notification settings - Fork 31
/
genesis.go
156 lines (130 loc) · 4.23 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
package types
import (
"encoding/json"
"fmt"
"sort"
proto "github.com/gogo/protobuf/proto"
"github.com/Finschia/finschia-sdk/codec"
"github.com/Finschia/finschia-sdk/codec/types"
"github.com/Finschia/finschia-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 {
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(AccountI) (stop bool),
) {
for _, genAcc := range GetGenesisStateFromAppState(cdc, appGenesis).Accounts {
acc, ok := genAcc.GetCachedValue().(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
}