2019-06-05 16:26:17 -07:00
|
|
|
package types
|
|
|
|
|
|
|
|
import (
|
2019-09-13 15:26:29 -07:00
|
|
|
"encoding/json"
|
2019-06-05 16:26:17 -07:00
|
|
|
"fmt"
|
2019-09-12 12:22:25 -07:00
|
|
|
"sort"
|
|
|
|
|
2019-09-13 15:26:29 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
2019-09-12 12:22:25 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth/exported"
|
2019-06-05 16:26:17 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// GenesisState - all auth state that must be provided at genesis
|
|
|
|
type GenesisState struct {
|
2019-09-13 15:26:29 -07:00
|
|
|
Params Params `json:"params" yaml:"params"`
|
|
|
|
Accounts exported.GenesisAccounts `json:"accounts" yaml:"accounts"`
|
2019-06-05 16:26:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewGenesisState - Create a new genesis state
|
2019-09-13 15:26:29 -07:00
|
|
|
func NewGenesisState(params Params, accounts exported.GenesisAccounts) GenesisState {
|
2019-09-12 12:22:25 -07:00
|
|
|
return GenesisState{
|
|
|
|
Params: params,
|
|
|
|
Accounts: accounts,
|
|
|
|
}
|
2019-06-05 16:26:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultGenesisState - Return a default genesis state
|
|
|
|
func DefaultGenesisState() GenesisState {
|
2019-09-13 15:26:29 -07:00
|
|
|
return NewGenesisState(DefaultParams(), exported.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
|
2019-06-05 16:26:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// ValidateGenesis performs basic validation of auth genesis data returning an
|
|
|
|
// error for any failed validation criteria.
|
|
|
|
func ValidateGenesis(data GenesisState) error {
|
2019-09-12 12:22:25 -07:00
|
|
|
if err := data.Params.Validate(); err != nil {
|
|
|
|
return err
|
2019-06-05 16:26:17 -07:00
|
|
|
}
|
2019-09-12 12:22:25 -07:00
|
|
|
|
2019-10-10 08:53:30 -07:00
|
|
|
return ValidateGenAccounts(data.Accounts)
|
2019-09-12 12:22:25 -07:00
|
|
|
}
|
|
|
|
|
2019-09-13 15:26:29 -07:00
|
|
|
// SanitizeGenesisAccounts sorts accounts and coin sets.
|
|
|
|
func SanitizeGenesisAccounts(genAccs exported.GenesisAccounts) exported.GenesisAccounts {
|
2019-09-12 12:22:25 -07:00
|
|
|
sort.Slice(genAccs, func(i, j int) bool {
|
|
|
|
return genAccs[i].GetAccountNumber() < genAccs[j].GetAccountNumber()
|
|
|
|
})
|
|
|
|
|
|
|
|
for _, acc := range genAccs {
|
|
|
|
if err := acc.SetCoins(acc.GetCoins().Sort()); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2019-06-05 16:26:17 -07:00
|
|
|
}
|
2019-09-12 12:22:25 -07:00
|
|
|
|
|
|
|
return genAccs
|
|
|
|
}
|
|
|
|
|
2019-10-10 08:53:30 -07:00
|
|
|
// ValidateGenAccounts validates an array of GenesisAccounts and checks for duplicates
|
|
|
|
func ValidateGenAccounts(accounts exported.GenesisAccounts) error {
|
2019-09-12 12:22:25 -07:00
|
|
|
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())
|
|
|
|
}
|
2019-06-05 16:26:17 -07:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2019-09-13 15:26:29 -07:00
|
|
|
|
|
|
|
// 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(exported.Account) (stop bool),
|
|
|
|
) {
|
|
|
|
|
|
|
|
for _, genAcc := range GetGenesisStateFromAppState(cdc, appGenesis).Accounts {
|
|
|
|
if cb(genAcc) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|