-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathpricing.go
361 lines (282 loc) · 9.29 KB
/
pricing.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
package bidengine
import (
"context"
"crypto/rand"
"math/big"
"github.com/pkg/errors"
"github.com/shopspring/decimal"
sdk "github.com/cosmos/cosmos-sdk/types"
dtypes "github.com/akash-network/akash-api/go/node/deployment/v1beta3"
"github.com/akash-network/akash-api/go/node/types/unit"
atypes "github.com/akash-network/akash-api/go/node/types/v1beta3"
"github.com/akash-network/node/sdl"
"github.com/akash-network/provider/cluster/util"
)
type Request struct {
Owner string `json:"owner"`
GSpec *dtypes.GroupSpec
AllocatedResources dtypes.ResourceUnits
PricePrecision int
}
const (
DefaultPricePrecision = 6
)
type BidPricingStrategy interface {
CalculatePrice(ctx context.Context, req Request) (sdk.DecCoin, error)
}
var (
errAllScalesZero = errors.New("at least one bid price must be a non-zero number")
errNoPriceScaleForStorageClass = errors.New("no pricing configured for storage class")
errScaleNegative = errors.New("scale price cannot be negative")
)
type Storage map[string]decimal.Decimal
func (ss Storage) IsAnyZero() bool {
if len(ss) == 0 {
return true
}
for _, val := range ss {
if val.IsZero() {
return true
}
}
return false
}
func (ss Storage) IsAnyNegative() bool {
for _, val := range ss {
if val.IsNegative() {
return true
}
}
return false
}
// AllLessThenOrEqual check all storage classes fit into max limits
// note better have dedicated limits for each class
func (ss Storage) AllLessThenOrEqual(val decimal.Decimal) bool {
for _, storage := range ss {
if !storage.LessThanOrEqual(val) {
return false
}
}
return true
}
type scalePricing struct {
cpuScale decimal.Decimal
memoryScale decimal.Decimal
storageScale Storage
endpointScale decimal.Decimal
ipScale decimal.Decimal
}
func MakeScalePricing(
cpuScale decimal.Decimal,
memoryScale decimal.Decimal,
storageScale Storage,
endpointScale decimal.Decimal,
ipScale decimal.Decimal,
) (BidPricingStrategy, error) {
if cpuScale.IsZero() && memoryScale.IsZero() && storageScale.IsAnyZero() && endpointScale.IsZero() && ipScale.IsZero() {
return nil, errAllScalesZero
}
if cpuScale.IsNegative() || memoryScale.IsNegative() || storageScale.IsAnyNegative() || endpointScale.IsNegative() ||
ipScale.IsNegative() {
return nil, errScaleNegative
}
result := scalePricing{
cpuScale: cpuScale,
memoryScale: memoryScale,
storageScale: storageScale,
endpointScale: endpointScale,
ipScale: ipScale,
}
return result, nil
}
var (
ErrBidQuantityInvalid = errors.New("A bid quantity is invalid")
ErrBidZero = errors.New("A bid of zero was produced")
)
func ceilBigRatToBigInt(v *big.Rat) *big.Int {
numerator := v.Num()
denom := v.Denom()
result := big.NewInt(0).Div(numerator, denom)
if !v.IsInt() {
result.Add(result, big.NewInt(1))
}
return result
}
func (fp scalePricing) CalculatePrice(_ context.Context, req Request) (sdk.DecCoin, error) {
// Use unlimited precision math here.
// Otherwise, a correctly crafted order could create a cost of '1' given
// a possible configuration
cpuTotal := decimal.NewFromInt(0)
memoryTotal := decimal.NewFromInt(0)
storageTotal := make(Storage)
denom := req.GSpec.Price().Denom
for k := range fp.storageScale {
storageTotal[k] = decimal.NewFromInt(0)
}
endpointTotal := decimal.NewFromInt(0)
ipTotal := decimal.NewFromInt(0).Add(fp.ipScale)
ipTotal = ipTotal.Mul(decimal.NewFromInt(int64(util.GetEndpointQuantityOfResourceGroup(req.GSpec, atypes.Endpoint_LEASED_IP))))
// iterate over everything & sum it up
for _, group := range req.GSpec.Resources {
groupCount := decimal.NewFromInt(int64(group.Count)) // Expand uint32 to int64
cpuQuantity := decimal.NewFromBigInt(group.Resources.CPU.Units.Val.BigInt(), 0)
cpuQuantity = cpuQuantity.Mul(groupCount)
cpuTotal = cpuTotal.Add(cpuQuantity)
memoryQuantity := decimal.NewFromBigInt(group.Resources.Memory.Quantity.Val.BigInt(), 0)
memoryQuantity = memoryQuantity.Mul(groupCount)
memoryTotal = memoryTotal.Add(memoryQuantity)
for _, storage := range group.Resources.Storage {
storageQuantity := decimal.NewFromBigInt(storage.Quantity.Val.BigInt(), 0)
storageQuantity = storageQuantity.Mul(groupCount)
storageClass := sdl.StorageEphemeral
attr := storage.Attributes.Find(sdl.StorageAttributePersistent)
if isPersistent, _ := attr.AsBool(); isPersistent {
attr = storage.Attributes.Find(sdl.StorageAttributeClass)
if class, set := attr.AsString(); set {
storageClass = class
}
}
total, exists := storageTotal[storageClass]
if !exists {
return sdk.DecCoin{}, errors.Wrapf(errNoPriceScaleForStorageClass, storageClass)
}
total = total.Add(storageQuantity)
storageTotal[storageClass] = total
}
endpointQuantity := decimal.NewFromInt(int64(len(group.Resources.Endpoints)))
endpointTotal = endpointTotal.Add(endpointQuantity)
}
cpuTotal = cpuTotal.Mul(fp.cpuScale)
mebibytes := decimal.NewFromInt(unit.Mi)
memoryTotal = memoryTotal.Div(mebibytes)
memoryTotal = memoryTotal.Mul(fp.memoryScale)
for class, total := range storageTotal {
total = total.Div(mebibytes)
// at this point presence of class in storageScale has been validated
total = total.Mul(fp.storageScale[class])
storageTotal[class] = total
}
endpointTotal = endpointTotal.Mul(fp.endpointScale)
// Each quantity must be non-negative
// and fit into an Int64
if cpuTotal.IsNegative() ||
memoryTotal.IsNegative() ||
storageTotal.IsAnyNegative() ||
endpointTotal.IsNegative() ||
ipTotal.IsNegative() {
return sdk.DecCoin{}, ErrBidQuantityInvalid
}
totalCost := cpuTotal
totalCost = totalCost.Add(memoryTotal)
for _, total := range storageTotal {
totalCost = totalCost.Add(total)
}
totalCost = totalCost.Add(endpointTotal)
totalCost = totalCost.Add(ipTotal)
if totalCost.IsNegative() {
return sdk.DecCoin{}, ErrBidQuantityInvalid
}
if totalCost.IsZero() {
// Return an error indicating we can't bid with a cost of zero
return sdk.DecCoin{}, ErrBidZero
}
costDec, err := sdk.NewDecFromStr(totalCost.String())
if err != nil {
return sdk.DecCoin{}, err
}
if !costDec.LTE(sdk.MaxSortableDec) {
return sdk.DecCoin{}, ErrBidQuantityInvalid
}
return sdk.NewDecCoinFromDec(denom, costDec), nil
}
type randomRangePricing int
func MakeRandomRangePricing() (BidPricingStrategy, error) {
return randomRangePricing(0), nil
}
func (randomRangePricing) CalculatePrice(_ context.Context, req Request) (sdk.DecCoin, error) {
min, max := calculatePriceRange(req.GSpec)
if min.IsEqual(max) {
return max, nil
}
const scale = 10000
delta := max.Amount.Sub(min.Amount).Mul(sdk.NewDec(scale))
minbid := delta.TruncateInt64()
if minbid < 1 {
minbid = 1
}
val, err := rand.Int(rand.Reader, big.NewInt(minbid))
if err != nil {
return sdk.DecCoin{}, err
}
scaledValue := sdk.NewDecFromBigInt(val).QuoInt64(scale).QuoInt64(100)
amount := min.Amount.Add(scaledValue)
return sdk.NewDecCoinFromDec(min.Denom, amount), nil
}
func calculatePriceRange(gspec *dtypes.GroupSpec) (sdk.DecCoin, sdk.DecCoin) {
// memory-based pricing:
// min: requested memory * configured min price per Gi
// max: requested memory * configured max price per Gi
// assumption: group.Count > 0
// assumption: all same denom (returned by gspec.Price())
// assumption: gspec.Price() > 0
mem := sdk.NewInt(0)
for _, group := range gspec.Resources {
mem = mem.Add(
sdk.NewIntFromUint64(group.Resources.Memory.Quantity.Value()).
MulRaw(int64(group.Count)))
}
rmax := gspec.Price()
const minGroupMemPrice = int64(50)
const maxGroupMemPrice = int64(1048576)
cmin := sdk.NewDecFromInt(mem.MulRaw(
minGroupMemPrice).
Quo(sdk.NewInt(unit.Gi)))
cmax := sdk.NewDecFromInt(mem.MulRaw(
maxGroupMemPrice).
Quo(sdk.NewInt(unit.Gi)))
if cmax.GT(rmax.Amount) {
cmax = rmax.Amount
}
if cmin.IsZero() {
cmin = sdk.NewDec(1)
}
if cmax.IsZero() {
cmax = sdk.NewDec(1)
}
return sdk.NewDecCoinFromDec(rmax.Denom, cmin), sdk.NewDecCoinFromDec(rmax.Denom, cmax)
}
var (
errPathEmpty = errors.New("script path cannot be the empty string")
errProcessLimitZero = errors.New("process limit must be greater than zero")
errProcessRuntimeLimitZero = errors.New("process runtime limit must be greater than zero")
)
type storageElement struct {
Class string `json:"class"`
Size uint64 `json:"size"`
}
type gpuVendorAttributes struct {
Model string `json:"model"`
RAM *string `json:"ram,omitempty"`
Interface *string `json:"interface,omitempty"`
}
type gpuAttributes struct {
Vendor map[string]gpuVendorAttributes `json:"vendor,omitempty"`
}
type gpuElement struct {
Units uint64 `json:"units"`
Attributes gpuAttributes `json:"attributes"`
}
type dataForScriptElement struct {
Memory uint64 `json:"memory"`
CPU uint64 `json:"cpu"`
GPU gpuElement `json:"gpu"`
Storage []storageElement `json:"storage"`
Count uint32 `json:"count"`
EndpointQuantity int `json:"endpoint_quantity"`
IPLeaseQuantity uint `json:"ip_lease_quantity"`
}
type dataForScript struct {
Resources []dataForScriptElement `json:"resources"`
Price sdk.DecCoin `json:"price"`
PricePrecision *int `json:"price_precision,omitempty"`
}