/
sp_storage_price.go
146 lines (127 loc) · 4.53 KB
/
sp_storage_price.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
package keeper
import (
"fmt"
"sort"
"github.com/cosmos/cosmos-sdk/store/prefix"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/bnb-chain/greenfield/x/sp/types"
)
// SetSpStoragePrice set a specific SpStoragePrice in the store from its index
func (k Keeper) SetSpStoragePrice(ctx sdk.Context, spStoragePrice types.SpStoragePrice) {
event := &types.EventSpStoragePriceUpdate{
SpId: spStoragePrice.SpId,
UpdateTimeSec: spStoragePrice.UpdateTimeSec,
ReadPrice: spStoragePrice.ReadPrice,
StorePrice: spStoragePrice.StorePrice,
FreeReadQuota: spStoragePrice.FreeReadQuota,
}
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.SpStoragePriceKeyPrefix)
key := types.SpStoragePriceKey(spStoragePrice.SpId)
spStoragePrice.SpId = 0
b := k.cdc.MustMarshal(&spStoragePrice)
store.Set(key, b)
_ = ctx.EventManager().EmitTypedEvents(event)
}
// GetSpStoragePrice returns a SpStoragePrice from its index
func (k Keeper) GetSpStoragePrice(
ctx sdk.Context,
spId uint32,
) (val types.SpStoragePrice, found bool) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.SpStoragePriceKeyPrefix)
b := store.Get(types.SpStoragePriceKey(spId))
if b == nil {
return val, false
}
k.cdc.MustUnmarshal(b, &val)
val.SpId = spId
return val, true
}
// GetAllSpStoragePrice returns all SpStoragePrice
func (k Keeper) GetAllSpStoragePrice(ctx sdk.Context) (list []types.SpStoragePrice) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.SpStoragePriceKeyPrefix)
iterator := storetypes.KVStorePrefixIterator(store, []byte{})
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var val types.SpStoragePrice
k.cdc.MustUnmarshal(iterator.Value(), &val)
spId := types.ParseSpStoragePriceKey(iterator.Key())
val.SpId = spId
list = append(list, val)
}
return
}
func (k Keeper) SetGlobalSpStorePrice(ctx sdk.Context, globalSpStorePrice types.GlobalSpStorePrice) {
event := &types.EventGlobalSpStorePriceUpdate{
UpdateTimeSec: globalSpStorePrice.UpdateTimeSec,
PrimaryStorePrice: globalSpStorePrice.PrimaryStorePrice,
SecondaryStorePrice: globalSpStorePrice.SecondaryStorePrice,
ReadPrice: globalSpStorePrice.ReadPrice,
}
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.GlobalSpStorePriceKeyPrefix)
key := types.GlobalSpStorePriceKey(
globalSpStorePrice.UpdateTimeSec,
)
globalSpStorePrice.UpdateTimeSec = 0
b := k.cdc.MustMarshal(&globalSpStorePrice)
store.Set(key, b)
_ = ctx.EventManager().EmitTypedEvents(event)
}
// UpdateGlobalSpStorePrice calculate the global prices by the median price of all sp store price
func (k Keeper) UpdateGlobalSpStorePrice(ctx sdk.Context) error {
sps := k.GetAllStorageProviders(ctx)
current := ctx.BlockTime().Unix()
storePrices := make([]sdk.Dec, 0)
readPrices := make([]sdk.Dec, 0)
for _, sp := range sps {
if sp.Status == types.STATUS_IN_SERVICE || sp.Status == types.STATUS_IN_MAINTENANCE {
price, found := k.GetSpStoragePrice(ctx, sp.Id)
if !found {
return fmt.Errorf("cannot find price for storage provider %d", sp.Id)
}
storePrices = append(storePrices, price.StorePrice)
readPrices = append(readPrices, price.ReadPrice)
}
}
l := len(storePrices)
if l == 0 {
return nil
}
primaryStorePrice := k.calculateMedian(storePrices)
secondaryStorePrice := k.SecondarySpStorePriceRatio(ctx).Mul(primaryStorePrice)
readPrice := k.calculateMedian(readPrices)
globalSpStorePrice := types.GlobalSpStorePrice{
PrimaryStorePrice: primaryStorePrice,
SecondaryStorePrice: secondaryStorePrice,
ReadPrice: readPrice,
UpdateTimeSec: current,
}
k.SetGlobalSpStorePrice(ctx, globalSpStorePrice)
return nil
}
func (k Keeper) calculateMedian(prices []sdk.Dec) sdk.Dec {
l := len(prices)
sort.Slice(prices, func(i, j int) bool { return prices[i].LT(prices[j]) })
var median sdk.Dec
if l%2 == 0 {
median = prices[l/2-1].Add(prices[l/2]).QuoInt64(2)
} else {
median = prices[l/2]
}
return median
}
func (k Keeper) GetGlobalSpStorePriceByTime(ctx sdk.Context, time int64) (val types.GlobalSpStorePrice, err error) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.GlobalSpStorePriceKeyPrefix)
startKey := types.GlobalSpStorePriceKey(
time,
)
iterator := store.ReverseIterator(nil, startKey)
defer iterator.Close()
if !iterator.Valid() {
return val, fmt.Errorf("no price found")
}
k.cdc.MustUnmarshal(iterator.Value(), &val)
updateTimeSec := types.ParseGlobalSpStorePriceKey(iterator.Key())
val.UpdateTimeSec = updateTimeSec
return val, nil
}