This repository has been archived by the owner on Jul 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
genesis.go
74 lines (59 loc) · 1.97 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
package authx
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/coinexchain/cet-sdk/modules/authx/internal/types"
)
type GenesisState struct {
Params types.Params `json:"params"`
AccountXs types.AccountXs `json:"accountxs"`
}
func NewGenesisState(params types.Params, accountXs types.AccountXs) GenesisState {
return GenesisState{
Params: params,
AccountXs: accountXs,
}
}
// DefaultGenesisState - Return a default genesis state
func DefaultGenesisState() GenesisState {
return NewGenesisState(types.DefaultParams(), types.AccountXs{})
}
// InitGenesis - Init store state from genesis data
func InitGenesis(ctx sdk.Context, keeper AccountXKeeper, data GenesisState) {
keeper.SetParams(ctx, data.Params)
for _, accx := range data.AccountXs {
accountX := types.NewAccountX(accx.Address, accx.MemoRequired,
accx.LockedCoins, accx.FrozenCoins,
accx.Referee, accx.RefereeChangeTime)
keeper.SetAccountX(ctx, accountX)
}
}
// ExportGenesis returns a GenesisState for a given context and keeper
func ExportGenesis(ctx sdk.Context, keeper AccountXKeeper) GenesisState {
var accountXs types.AccountXs
keeper.IterateAccounts(ctx, func(accountX types.AccountX) (stop bool) {
accountXs = append(accountXs, accountX)
return false
})
return NewGenesisState(keeper.GetParams(ctx), accountXs)
}
// ValidateGenesis performs basic validation of asset genesis data returning an
// error for any failed validation criteria.
func (data GenesisState) ValidateGenesis() error {
err := data.Params.ValidateGenesis()
if err != nil {
return err
}
addrMap := make(map[string]bool, len(data.AccountXs))
for _, accx := range data.AccountXs {
addrStr := accx.Address.String()
if addrStr == "" {
return fmt.Errorf("nil accountX found in genesis state; address: %s", addrStr)
}
if _, exists := addrMap[addrStr]; exists {
return fmt.Errorf("duplicate accountX found in genesis state; address: %s", addrStr)
}
addrMap[addrStr] = true
}
return nil
}