-
Notifications
You must be signed in to change notification settings - Fork 20
/
genesis.go
103 lines (86 loc) · 2.55 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
package token
import (
"github.com/Decentr-net/decentr/x/token/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)
type GenesisState struct {
Params types.Params `json:"params"`
Balances map[string]sdk.Int `json:"balances"`
Deltas map[string]sdk.Int `json:"deltas"`
History map[string][]types.RewardDistribution `json:"history"`
}
func ValidateGenesis(data GenesisState) error {
if err := data.Params.Validate(); err != nil {
return err
}
for account := range data.Balances {
if _, err := sdk.AccAddressFromBech32(account); err != nil {
return err
}
}
for account := range data.Deltas {
if _, err := sdk.AccAddressFromBech32(account); err != nil {
return err
}
}
for account := range data.History {
if _, err := sdk.AccAddressFromBech32(account); err != nil {
return err
}
}
return nil
}
func DefaultGenesisState() GenesisState {
return GenesisState{
Params: types.DefaultParams(),
Balances: make(map[string]sdk.Int),
Deltas: make(map[string]sdk.Int),
History: make(map[string][]types.RewardDistribution),
}
}
func InitGenesis(ctx sdk.Context, keeper Keeper, data GenesisState) {
keeper.SetParams(ctx, data.Params)
for account, balance := range data.Balances {
addr, _ := sdk.AccAddressFromBech32(account)
keeper.SetBalance(ctx, addr, balance)
}
for account, delta := range data.Deltas {
addr, _ := sdk.AccAddressFromBech32(account)
keeper.IncBalanceDelta(ctx, addr, delta)
}
for account, history := range data.History {
addr, _ := sdk.AccAddressFromBech32(account)
for _, v := range history {
keeper.AddRewardDistribution(ctx, addr, v.Height, v.Coins)
}
}
}
func ExportGenesis(ctx sdk.Context, keeper Keeper) GenesisState {
balances := make(map[string]sdk.Int)
deltas := make(map[string]sdk.Int)
history := make(map[string][]types.RewardDistribution)
it := keeper.GetBalanceIterator(ctx)
for ; it.Valid(); it.Next() {
owner := sdk.AccAddress(it.Key())
balances[owner.String()] = keeper.GetBalance(ctx, it.Key())
}
it.Close()
it = keeper.GetDeltasIterator(ctx)
for ; it.Valid(); it.Next() {
owner := sdk.AccAddress(it.Key())
deltas[owner.String()] = keeper.GetBalanceDelta(ctx, it.Key())
}
it.Close()
it = keeper.GetRewardsDistributionIterator(ctx)
for ; it.Valid(); it.Next() {
owner := sdk.AccAddress(it.Key())
history[owner.String()] = keeper.GetRewardsDistributionHistory(ctx, it.Key())
}
it.Close()
return GenesisState{
Params: keeper.GetParams(ctx),
Balances: balances,
Deltas: deltas,
History: history,
}
}