-
Notifications
You must be signed in to change notification settings - Fork 113
/
genesis.go
71 lines (58 loc) · 1.94 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
package types
import (
"encoding/json"
"fmt"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/crypto/keys/multisig"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/axelarnetwork/axelar-core/x/permission/exported"
)
// NewGenesisState is the constructor for GenesisState
func NewGenesisState(params Params, governanceKey *multisig.LegacyAminoPubKey, accounts []GovAccount) *GenesisState {
return &GenesisState{
Params: params,
GovernanceKey: governanceKey,
GovAccounts: accounts,
}
}
// DefaultGenesisState returns a genesis state with default parameters
func DefaultGenesisState() *GenesisState {
return NewGenesisState(Params{}, nil, []GovAccount{})
}
// Validate performs a validation check on the genesis parameters
func (m GenesisState) Validate() error {
if err := m.Params.Validate(); err != nil {
return getValidateError(err)
}
if m.GovernanceKey != nil {
if int(m.GovernanceKey.Threshold) > len(m.GovernanceKey.PubKeys) {
return fmt.Errorf("threshold k of n multisignature: len(pubKeys) < k")
}
}
accessControlSet := false
for _, account := range m.GovAccounts {
if err := account.Validate(); err != nil {
return getValidateError(err)
}
// exactly one account with role ROLE_ACCESS_CONTROL
if account.Role == exported.ROLE_ACCESS_CONTROL {
if accessControlSet {
return getValidateError(fmt.Errorf("role access control already set"))
}
accessControlSet = true
}
}
return nil
}
func getValidateError(err error) error {
return sdkerrors.Wrapf(err, "genesis state for module %s is invalid", ModuleName)
}
// GetGenesisStateFromAppState returns x/permission GenesisState given raw application
// genesis state.
func GetGenesisStateFromAppState(cdc codec.JSONCodec, appState map[string]json.RawMessage) GenesisState {
var genesisState GenesisState
if appState[ModuleName] != nil {
cdc.MustUnmarshalJSON(appState[ModuleName], &genesisState)
}
return genesisState
}