-
Notifications
You must be signed in to change notification settings - Fork 0
/
genesis.go
163 lines (143 loc) · 5.26 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package distribution
import (
sdk "github.com/ci123chain/ci123chain/pkg/abci/types"
"github.com/ci123chain/ci123chain/pkg/account"
"github.com/ci123chain/ci123chain/pkg/distribution/keeper"
"github.com/ci123chain/ci123chain/pkg/distribution/types"
"github.com/ci123chain/ci123chain/pkg/supply"
)
func InitGenesis(ctx sdk.Context, ak account.AccountKeeper, sk supply.Keeper, k keeper.DistrKeeper, data types.GenesisState) {
//
var moduleHoldings = sdk.NewDecCoin(sdk.ChainCoinDenom, sdk.NewInt(0))
k.SetFeePool(ctx, data.FeePool)
k.SetParams(ctx, data.Params)
for _, dwi := range data.DelegatorWithdrawInfos {
k.SetDelegatorWithdrawAddr(ctx, dwi.DelegatorAddress, dwi.WithdrawAddress)
}
k.SetPreviousProposerAddr(ctx, data.PreviousProposer)
if data.OutstandingRewards != nil {
for _, rew := range data.OutstandingRewards {
k.SetValidatorOutstandingRewards(ctx, rew.ValidatorAddress, types.ValidatorOutstandingRewards{Rewards: rew.OutstandingRewards})
moduleHoldings = moduleHoldings.Add(rew.OutstandingRewards)
}
}
if data.ValidatorAccumulatedCommissions != nil {
for _, acc := range data.ValidatorAccumulatedCommissions {
k.SetValidatorAccumulatedCommission(ctx, acc.ValidatorAddress, acc.Accumulated)
}
}
if data.ValidatorHistoricalRewards != nil {
for _, his := range data.ValidatorHistoricalRewards {
k.SetValidatorHistoricalRewards(ctx, his.ValidatorAddress, his.Period, his.Rewards)
}
}
if data.ValidatorCurrentRewards != nil {
for _, cur := range data.ValidatorCurrentRewards {
k.SetValidatorCurrentRewards(ctx, cur.ValidatorAddress, cur.Rewards)
}
}
if data.DelegatorStartingInfos != nil {
for _, del := range data.DelegatorStartingInfos {
k.SetDelegatorStartingInfo(ctx, del.ValidatorAddress, del.DelegatorAddress, del.StartingInfo)
}
}
if data.ValidatorSlashEvents != nil {
for _, evt := range data.ValidatorSlashEvents {
k.SetValidatorSlashEvent(ctx, evt.ValidatorAddress, evt.Height, evt.Period, evt.Event)
}
}
/*
moduleHoldings = moduleHoldings.Add(data.FeePool.CommunityPool)
moduleHoldingsInt, _ := moduleHoldings.TruncateDecimal()
// check if the module account exists
moduleAcc := k.GetDistributionAccount(ctx)
if moduleAcc == nil {
panic(fmt.Sprintf("%s module account has not been set", types.ModuleName))
}
if ak.GetAllBalances(ctx, moduleAcc.GetAddress()).IsZero() {
if err := ak.SetBalances(ctx, moduleAcc.GetAddress(), sdk.NewCoins(moduleHoldingsInt)); err != nil {
panic(err)
}
sk.SetModuleAccount(ctx, moduleAcc)
}
*/
}
func ExportGenesis(ctx sdk.Context, ak account.AccountKeeper, sk supply.Keeper, k keeper.DistrKeeper) GenesisState {
params := k.GetParams(ctx)
feePool := k.GetFeePool(ctx)
dwi := make([]types.DelegatorWithdrawInfo, 0)
k.IterateDelegatorWithdrawAddrs(ctx, func(del sdk.AccAddress, addr sdk.AccAddress) (stop bool) {
dwi = append(dwi, types.DelegatorWithdrawInfo{
DelegatorAddress: del,
WithdrawAddress: addr,
})
return false
})
pp := k.GetPreviousProposerConsAddr(ctx)
outstanding := make([]types.ValidatorOutstandingRewardsRecord, 0)
rs := make([]types.ValidatorOutstandingRewardsRecord, 0)
k.IterateValidatorOutstandingRewards(ctx,
func(addr sdk.AccAddress, rewards types.ValidatorOutstandingRewards) (stop bool) {
outstanding = append(outstanding, types.ValidatorOutstandingRewardsRecord{
ValidatorAddress: addr,
OutstandingRewards: rewards.Rewards,
})
return false
},
)
acc := make([]types.ValidatorAccumulatedCommissionRecord, 0)
k.IterateValidatorAccumulatedCommissions(ctx,
func(addr sdk.AccAddress, commission types.ValidatorAccumulatedCommission) (stop bool) {
acc = append(acc, types.ValidatorAccumulatedCommissionRecord{
ValidatorAddress: addr,
Accumulated: commission,
})
return false
},
)
his := make([]types.ValidatorHistoricalRewardsRecord, 0)
k.IterateValidatorHistoricalRewards(ctx,
func(val sdk.AccAddress, period uint64, rewards types.ValidatorHistoricalRewards) (stop bool) {
his = append(his, types.ValidatorHistoricalRewardsRecord{
ValidatorAddress: val,
Period: period,
Rewards: rewards,
})
return false
},
)
cur := make([]types.ValidatorCurrentRewardsRecord, 0)
k.IterateValidatorCurrentRewards(ctx,
func(val sdk.AccAddress, rewards types.ValidatorCurrentRewards) (stop bool) {
cur = append(cur, types.ValidatorCurrentRewardsRecord{
ValidatorAddress: val,
Rewards: rewards,
})
return false
},
)
dels := make([]types.DelegatorStartingInfoRecord, 0)
k.IterateDelegatorStartingInfos(ctx,
func(val sdk.AccAddress, del sdk.AccAddress, info types.DelegatorStartingInfo) (stop bool) {
dels = append(dels, types.DelegatorStartingInfoRecord{
ValidatorAddress: val,
DelegatorAddress: del,
StartingInfo: info,
})
return false
},
)
slashes := make([]types.ValidatorSlashEventRecord, 0)
k.IterateValidatorSlashEvents(ctx,
func(val sdk.AccAddress, height uint64, event types.ValidatorSlashEvent) (stop bool) {
slashes = append(slashes, types.ValidatorSlashEventRecord{
ValidatorAddress: val,
Height: height,
Period: event.ValidatorPeriod,
Event: event,
})
return false
},
)
return types.NewGenesisState(params, feePool, dwi, pp.Bytes(), rs, acc, his, cur, dels, slashes)
}