-
Notifications
You must be signed in to change notification settings - Fork 22
/
store.go
346 lines (303 loc) · 11.5 KB
/
store.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package keeper
import (
"fmt"
"time"
sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
stratos "github.com/stratosnet/stratos-chain/types"
"github.com/stratosnet/stratos-chain/x/register/types"
)
// deprecated
func (k Keeper) SetInitialGenesisDepositTotal(ctx sdk.Context, deposit sdkmath.Int) {
store := ctx.KVStore(k.storeKey)
b := k.cdc.MustMarshalLengthPrefixed(&stratos.Int{Value: &deposit})
store.Set(types.InitialGenesisDepositTotalKey, b)
}
// deprecated
func (k Keeper) GetInitialGenesisDepositTotal(ctx sdk.Context) (deposit sdkmath.Int) {
store := ctx.KVStore(k.storeKey)
b := store.Get(types.InitialGenesisDepositTotalKey)
if b == nil {
return sdkmath.ZeroInt()
}
value := stratos.Int{}
k.cdc.MustUnmarshalLengthPrefixed(b, &value)
deposit = *value.Value
return
}
func (k Keeper) SetRemainingOzoneLimit(ctx sdk.Context, value sdkmath.Int) {
store := ctx.KVStore(k.storeKey)
b := k.cdc.MustMarshalLengthPrefixed(&stratos.Int{Value: &value})
store.Set(types.UpperBoundOfTotalOzoneKey, b)
}
func (k Keeper) GetRemainingOzoneLimit(ctx sdk.Context) (value sdkmath.Int) {
store := ctx.KVStore(k.storeKey)
b := store.Get(types.UpperBoundOfTotalOzoneKey)
if b == nil {
return sdkmath.ZeroInt()
}
intVal := stratos.Int{}
k.cdc.MustUnmarshalLengthPrefixed(b, &intVal)
value = *intVal.Value
return
}
func (k *Keeper) GetPrepayParams(ctx sdk.Context) (St, Pt, Lt sdkmath.Int) {
St = k.GetEffectiveTotalDeposit(ctx)
Pt = k.GetTotalUnissuedPrepay(ctx).Amount
Lt = k.GetRemainingOzoneLimit(ctx)
return
}
func (k *Keeper) CalculatePurchaseAmount(ctx sdk.Context, amount sdkmath.Int) (sdkmath.Int, sdkmath.Int, error) {
St, Pt, Lt := k.GetPrepayParams(ctx)
purchase := Lt.ToLegacyDec().
Mul(amount.ToLegacyDec()).
Quo((St.
Add(Pt).
Add(amount)).ToLegacyDec()).
TruncateInt()
if purchase.GT(Lt) {
return sdkmath.NewInt(0), sdkmath.NewInt(0), fmt.Errorf("not enough remaining ozone limit to complete prepay")
}
remaining := Lt.Sub(purchase)
return purchase, remaining, nil
}
func (k Keeper) IsUnbondable(ctx sdk.Context, unbondAmt sdkmath.Int) bool {
remaining := k.GetRemainingOzoneLimit(ctx)
depositNozRate := k.GetDepositNozRate(ctx)
return remaining.ToLegacyDec().GTE(unbondAmt.ToLegacyDec().Quo(depositNozRate))
}
// SetUnbondingNode sets the unbonding node
func (k Keeper) SetUnbondingNode(ctx sdk.Context, ubd types.UnbondingNode) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshalLengthPrefixed(&ubd)
networkAddr, err := stratos.SdsAddressFromBech32(ubd.GetNetworkAddr())
if err != nil {
return
}
key := types.GetUBDNodeKey(networkAddr)
store.Set(key, bz)
}
// RemoveUnbondingNode removes the unbonding node object
func (k Keeper) RemoveUnbondingNode(ctx sdk.Context, networkAddr stratos.SdsAddress) {
store := ctx.KVStore(k.storeKey)
key := types.GetUBDNodeKey(networkAddr)
store.Delete(key)
}
// GetUnbondingNode return a unbonding node
func (k Keeper) GetUnbondingNode(ctx sdk.Context, networkAddr stratos.SdsAddress) (ubd types.UnbondingNode, found bool) {
store := ctx.KVStore(k.storeKey)
key := types.GetUBDNodeKey(networkAddr)
value := store.Get(key)
if value == nil {
return ubd, false
}
k.cdc.MustUnmarshalLengthPrefixed(value, &ubd)
return ubd, true
}
func (k Keeper) GetAllUnbondingNode(ctx sdk.Context) (unbondingNodes []types.UnbondingNode) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.UBDNodeKey)
defer iterator.Close()
unbondingNodes = make([]types.UnbondingNode, 0)
for ; iterator.Valid(); iterator.Next() {
node := types.UnbondingNode{}
k.cdc.MustUnmarshalLengthPrefixed(iterator.Value(), &node)
unbondingNodes = append(unbondingNodes, node)
}
return
}
// SetUnbondingNodeQueueTimeSlice sets a specific unbonding queue timeslice.
func (k Keeper) SetUnbondingNodeQueueTimeSlice(ctx sdk.Context, timestamp time.Time, networkAddrs []string) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshalLengthPrefixed(&stratos.SdsAddresses{Addresses: networkAddrs})
store.Set(types.GetUBDTimeKey(timestamp), bz)
}
// GetUnbondingNodeQueueTimeSlice gets a specific unbonding queue timeslice. A timeslice is a slice of DVPairs
// corresponding to unbonding delegations that expire at a certain time.
func (k Keeper) GetUnbondingNodeQueueTimeSlice(ctx sdk.Context, timestamp time.Time) (networkAddrs []string) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.GetUBDTimeKey(timestamp))
if bz == nil {
return make([]string, 0)
}
addrValue := stratos.SdsAddresses{}
k.cdc.MustUnmarshalLengthPrefixed(bz, &addrValue)
networkAddrs = addrValue.GetAddresses()
return networkAddrs
}
// UnbondingNodeQueueIterator returns all the unbonding queue timeslices from time 0 until endTime
func (k Keeper) UnbondingNodeQueueIterator(ctx sdk.Context, endTime time.Time) sdk.Iterator {
store := ctx.KVStore(k.storeKey)
return store.Iterator(types.UBDNodeQueueKey, sdk.InclusiveEndBytes(types.GetUBDTimeKey(endTime)))
}
func (k Keeper) SetBondedResourceNodeCnt(ctx sdk.Context, count sdkmath.Int) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshalLengthPrefixed(&stratos.Int{Value: &count})
store.Set(types.ResourceNodeCntKey, bz)
}
func (k Keeper) GetBondedResourceNodeCnt(ctx sdk.Context) (balance sdkmath.Int) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.ResourceNodeCntKey)
if bz == nil {
return sdkmath.ZeroInt()
}
intValue := stratos.Int{}
k.cdc.MustUnmarshalLengthPrefixed(bz, &intValue)
balance = *intValue.Value
return
}
func (k Keeper) SetBondedMetaNodeCnt(ctx sdk.Context, count sdkmath.Int) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshalLengthPrefixed(&stratos.Int{Value: &count})
store.Set(types.MetaNodeCntKey, bz)
}
func (k Keeper) GetBondedMetaNodeCnt(ctx sdk.Context) (balance sdkmath.Int) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.MetaNodeCntKey)
if bz == nil {
return sdkmath.ZeroInt()
}
intValue := stratos.Int{}
k.cdc.MustUnmarshalLengthPrefixed(bz, &intValue)
balance = *intValue.Value
return
}
func (k Keeper) DeleteMetaNodeRegistrationVotePool(ctx sdk.Context, nodeAddr stratos.SdsAddress) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.GetMetaNodeRegistrationVotesKey(nodeAddr))
}
func (k Keeper) SetMetaNodeRegistrationVotePool(ctx sdk.Context, votePool types.MetaNodeRegistrationVotePool) {
nodeAddr := votePool.GetNetworkAddress()
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshalLengthPrefixed(&votePool)
node, _ := stratos.SdsAddressFromBech32(nodeAddr)
store.Set(types.GetMetaNodeRegistrationVotesKey(node), bz)
}
func (k Keeper) GetMetaNodeRegistrationVotePool(ctx sdk.Context, nodeAddr stratos.SdsAddress) (votePool types.MetaNodeRegistrationVotePool, found bool) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.GetMetaNodeRegistrationVotesKey(nodeAddr))
if bz == nil {
return votePool, false
}
k.cdc.MustUnmarshalLengthPrefixed(bz, &votePool)
return votePool, true
}
func (k Keeper) GetAllMetaNodeRegVotePool(ctx sdk.Context) (votePools []types.MetaNodeRegistrationVotePool) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.MetaNodeRegistrationVotesKey)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
voteInfo := types.MetaNodeRegistrationVotePool{}
k.cdc.MustUnmarshalLengthPrefixed(iterator.Value(), &voteInfo)
votePools = append(votePools, voteInfo)
}
return
}
func (k Keeper) SetKickMetaNodeVotePool(ctx sdk.Context, votePool types.KickMetaNodeVotePool) {
targetNetworkAddr := votePool.GetTargetNetworkAddress()
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshalLengthPrefixed(&votePool)
node, _ := stratos.SdsAddressFromBech32(targetNetworkAddr)
store.Set(types.GetKickMetaNodeVotesKey(node), bz)
}
func (k Keeper) GetKickMetaNodeVotePool(ctx sdk.Context, targetNetworkAddr stratos.SdsAddress) (votePool types.KickMetaNodeVotePool, found bool) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.GetKickMetaNodeVotesKey(targetNetworkAddr))
if bz == nil {
return votePool, false
}
k.cdc.MustUnmarshalLengthPrefixed(bz, &votePool)
return votePool, true
}
func (k Keeper) GetAllKickMetaNodeVotePool(ctx sdk.Context) (votePools []types.KickMetaNodeVotePool) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.KickMetaNodeVotesKey)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
voteInfo := types.KickMetaNodeVotePool{}
k.cdc.MustUnmarshalLengthPrefixed(iterator.Value(), &voteInfo)
votePools = append(votePools, voteInfo)
}
return
}
func (k Keeper) GetAllExpiredKickMetaNodeVotePool(ctx sdk.Context) (votePools []types.KickMetaNodeVotePool) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.KickMetaNodeVotesKey)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
votePool := types.KickMetaNodeVotePool{}
k.cdc.MustUnmarshalLengthPrefixed(iterator.Value(), &votePool)
if votePool.GetExpireTime().Before(ctx.BlockTime()) {
votePools = append(votePools, votePool)
}
}
return
}
func (k Keeper) DeleteKickMetaNodeVotePool(ctx sdk.Context, targetNetworkAddr stratos.SdsAddress) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.GetKickMetaNodeVotesKey(targetNetworkAddr))
}
func (k Keeper) SetEffectiveTotalDeposit(ctx sdk.Context, deposit sdkmath.Int) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshalLengthPrefixed(&stratos.Int{Value: &deposit})
store.Set(types.EffectiveGenesisDepositTotalKey, bz)
}
func (k Keeper) GetEffectiveTotalDeposit(ctx sdk.Context) (deposit sdkmath.Int) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.EffectiveGenesisDepositTotalKey)
if bz == nil {
return sdkmath.ZeroInt()
}
intValue := stratos.Int{}
k.cdc.MustUnmarshalLengthPrefixed(bz, &intValue)
deposit = *intValue.Value
return
}
func (k Keeper) SetDepositNozRate(ctx sdk.Context, depositNozRate sdkmath.LegacyDec) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshalLengthPrefixed(&stratos.Dec{Value: &depositNozRate})
store.Set(types.DepositNozRateKey, bz)
}
func (k Keeper) GetDepositNozRate(ctx sdk.Context) (depositNozRate sdkmath.LegacyDec) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.DepositNozRateKey)
if bz == nil {
panic("Stored deposit noz rate should not be nil")
}
decValue := stratos.Dec{}
k.cdc.MustUnmarshalLengthPrefixed(bz, &decValue)
depositNozRate = *decValue.Value
return
}
// IteratorSlashingInfo Iteration for each slashing
func (k Keeper) IteratorSlashingInfo(ctx sdk.Context, handler func(walletAddress sdk.AccAddress, slashing sdkmath.Int) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iter := sdk.KVStorePrefixIterator(store, types.SlashingPrefix)
defer iter.Close()
for ; iter.Valid(); iter.Next() {
walletAddress := sdk.AccAddress(iter.Key()[len(types.SlashingPrefix):])
intValue := stratos.Int{}
k.cdc.MustUnmarshalLengthPrefixed(iter.Value(), &intValue)
slashing := *intValue.Value
if handler(walletAddress, slashing) {
break
}
}
}
func (k Keeper) SetSlashing(ctx sdk.Context, walletAddress sdk.AccAddress, slashing sdkmath.Int) {
store := ctx.KVStore(k.storeKey)
storeKey := types.GetSlashingKey(walletAddress)
bz := k.cdc.MustMarshalLengthPrefixed(&stratos.Int{Value: &slashing})
store.Set(storeKey, bz)
}
func (k Keeper) GetSlashing(ctx sdk.Context, walletAddress sdk.AccAddress) (res sdkmath.Int) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.GetSlashingKey(walletAddress))
if bz == nil {
return sdkmath.ZeroInt()
}
intValue := stratos.Int{}
k.cdc.MustUnmarshalLengthPrefixed(bz, &intValue)
res = *intValue.Value
return
}