forked from regen-network/regen-ledger
/
genesis.go
228 lines (188 loc) · 6.36 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package server
import (
"encoding/json"
"fmt"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/pkg/errors"
"github.com/regen-network/regen-ledger/types/math"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/regen-network/regen-ledger/types"
"github.com/regen-network/regen-ledger/x/ecocredit"
)
// InitGenesis performs genesis initialization for the ecocredit module. It
// returns no validator updates.
func (s serverImpl) InitGenesis(ctx types.Context, cdc codec.Codec, data json.RawMessage) ([]abci.ValidatorUpdate, error) {
var genesisState ecocredit.GenesisState
cdc.MustUnmarshalJSON(data, &genesisState)
s.paramSpace.SetParamSet(ctx.Context, &genesisState.Params)
if err := s.creditTypeSeqTable.Import(ctx, genesisState.Sequences, 0); err != nil {
return nil, errors.Wrap(err, "sequences")
}
if err := s.classInfoTable.Import(ctx, genesisState.ClassInfo, 0); err != nil {
return nil, errors.Wrap(err, "class-info")
}
if err := s.batchInfoTable.Import(ctx, genesisState.BatchInfo, 0); err != nil {
return nil, errors.Wrap(err, "batch-info")
}
store := ctx.KVStore(s.storeKey)
if err := setBalanceAndSupply(store, genesisState.Balances); err != nil {
return nil, err
}
if err := validateSupplies(store, genesisState.Supplies); err != nil {
return nil, err
}
return []abci.ValidatorUpdate{}, nil
}
// validateSupplies returns an error if credit batch genesis supply does not equal to calculated supply.
func validateSupplies(store sdk.KVStore, supplies []*ecocredit.Supply) error {
var denomT batchDenomT
for _, supply := range supplies {
denomT = batchDenomT(supply.BatchDenom)
tradableSupply := math.NewDecFromInt64(0)
retiredSupply := math.NewDecFromInt64(0)
var err error
if supply.TradableSupply != "" {
tradableSupply, err = math.NewNonNegativeDecFromString(supply.TradableSupply)
if err != nil {
return err
}
}
tradable, err := getDecimal(store, TradableSupplyKey(denomT))
if err != nil {
return err
}
if tradableSupply.Cmp(tradable) != 0 {
return sdkerrors.ErrInvalidCoins.Wrapf("tradable supply is incorrect for %s credit batch, expected %v, got %v", supply.BatchDenom, tradable, tradableSupply)
}
if supply.RetiredSupply != "" {
retiredSupply, err = math.NewNonNegativeDecFromString(supply.RetiredSupply)
if err != nil {
return err
}
}
retired, err := getDecimal(store, RetiredSupplyKey(denomT))
if err != nil {
return err
}
if retiredSupply.Cmp(retired) != 0 {
return sdkerrors.ErrInvalidCoins.Wrapf("retired supply is incorrect for %s credit batch, expected %v, got %v", supply.BatchDenom, retired, retiredSupply)
}
}
return nil
}
// setBalanceAndSupply sets the tradable and retired balance for an account and update supply for batch denom.
func setBalanceAndSupply(store sdk.KVStore, balances []*ecocredit.Balance) error {
for _, balance := range balances {
addr, err := sdk.AccAddressFromBech32(balance.Address)
if err != nil {
return err
}
denomT := batchDenomT(balance.BatchDenom)
// set tradable balance and update supply
if balance.TradableBalance != "" {
d, err := math.NewNonNegativeDecFromString(balance.TradableBalance)
if err != nil {
return err
}
key := TradableBalanceKey(addr, denomT)
setDecimal(store, key, d)
key = TradableSupplyKey(denomT)
addAndSetDecimal(store, key, d)
}
// set retired balance and update supply
if balance.RetiredBalance != "" {
d, err := math.NewNonNegativeDecFromString(balance.RetiredBalance)
if err != nil {
return err
}
key := RetiredBalanceKey(addr, denomT)
setDecimal(store, key, d)
key = RetiredSupplyKey(denomT)
addAndSetDecimal(store, key, d)
}
}
return nil
}
// ExportGenesis will dump the ecocredit module state into a serializable GenesisState.
func (s serverImpl) ExportGenesis(ctx types.Context, cdc codec.Codec) (json.RawMessage, error) {
// Get Params from the store and put them in the genesis state
var params ecocredit.Params
s.paramSpace.GetParamSet(ctx.Context, ¶ms)
store := ctx.KVStore(s.storeKey)
var classInfo []*ecocredit.ClassInfo
if _, err := s.classInfoTable.Export(ctx, &classInfo); err != nil {
return nil, errors.Wrap(err, "class-info")
}
var batchInfo []*ecocredit.BatchInfo
if _, err := s.batchInfoTable.Export(ctx, &batchInfo); err != nil {
return nil, errors.Wrap(err, "batch-info")
}
var sequences []*ecocredit.CreditTypeSeq
if _, err := s.creditTypeSeqTable.Export(ctx, &sequences); err != nil {
return nil, errors.Wrap(err, "batch-info")
}
suppliesMap := make(map[string]*ecocredit.Supply)
iterateSupplies(store, TradableSupplyPrefix, func(denom, supply string) (bool, error) {
suppliesMap[denom] = &ecocredit.Supply{
BatchDenom: denom,
TradableSupply: supply,
}
return false, nil
})
iterateSupplies(store, RetiredSupplyPrefix, func(denom, supply string) (bool, error) {
if _, exists := suppliesMap[denom]; exists {
suppliesMap[denom].RetiredSupply = supply
} else {
suppliesMap[denom] = &ecocredit.Supply{
BatchDenom: denom,
RetiredSupply: supply,
}
}
return false, nil
})
supplies := make([]*ecocredit.Supply, len(suppliesMap))
index := 0
for _, supply := range suppliesMap {
supplies[index] = supply
index++
}
balancesMap := make(map[string]*ecocredit.Balance)
iterateBalances(store, TradableBalancePrefix, func(address, denom, balance string) bool {
balancesMap[fmt.Sprintf("%s%s", address, denom)] = &ecocredit.Balance{
Address: address,
BatchDenom: denom,
TradableBalance: balance,
}
return false
})
iterateBalances(store, RetiredBalancePrefix, func(address, denom, balance string) bool {
index := fmt.Sprintf("%s%s", address, denom)
if _, exists := balancesMap[index]; exists {
balancesMap[index].RetiredBalance = balance
} else {
balancesMap[index] = &ecocredit.Balance{
Address: address,
BatchDenom: denom,
RetiredBalance: balance,
}
}
return false
})
balances := make([]*ecocredit.Balance, len(balancesMap))
index = 0
for _, balance := range balancesMap {
balances[index] = balance
index++
}
gs := &ecocredit.GenesisState{
Params: params,
ClassInfo: classInfo,
BatchInfo: batchInfo,
Sequences: sequences,
Balances: balances,
Supplies: supplies,
}
return cdc.MustMarshalJSON(gs), nil
}