-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
genesis_state.go
77 lines (63 loc) · 2.02 KB
/
genesis_state.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
package types
import (
"encoding/json"
"fmt"
"sort"
"time"
"github.com/cosmos/cosmos-sdk/codec"
)
// State to Unmarshal
type GenesisState GenesisAccounts
// get the genesis state from the expected app 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
}
// set the genesis state within the expected app state
func SetGenesisStateInAppState(cdc *codec.Codec,
appState map[string]json.RawMessage, genesisState GenesisState) map[string]json.RawMessage {
genesisStateBz := cdc.MustMarshalJSON(genesisState)
appState[ModuleName] = genesisStateBz
return appState
}
// Sanitize sorts accounts and coin sets.
func (gs GenesisState) Sanitize() {
sort.Slice(gs, func(i, j int) bool {
return gs[i].AccountNumber < gs[j].AccountNumber
})
for _, acc := range gs {
acc.Coins = acc.Coins.Sort()
}
}
// ValidateGenesis performs validation of genesis accounts. It
// ensures that there are no duplicate accounts in the genesis state and any
// provided vesting accounts are valid.
func ValidateGenesis(genesisState GenesisState) error {
addrMap := make(map[string]bool, len(genesisState))
for _, acc := range genesisState {
addrStr := acc.Address.String()
// disallow any duplicate accounts
if _, ok := addrMap[addrStr]; ok {
return fmt.Errorf("duplicate account found in genesis state; address: %s", addrStr)
}
// validate any vesting fields
if !acc.OriginalVesting.IsZero() {
if acc.EndTime == 0 {
return fmt.Errorf("missing end time for vesting account; address: %s", addrStr)
}
if acc.StartTime >= acc.EndTime {
return fmt.Errorf(
"vesting start time must before end time; address: %s, start: %s, end: %s",
addrStr,
time.Unix(acc.StartTime, 0).UTC().Format(time.RFC3339),
time.Unix(acc.EndTime, 0).UTC().Format(time.RFC3339),
)
}
}
addrMap[addrStr] = true
}
return nil
}