-
Notifications
You must be signed in to change notification settings - Fork 124
/
Copy pathdimension.go
200 lines (176 loc) · 4.4 KB
/
dimension.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
// Copyright (C) 2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package fees
import (
"encoding/binary"
"encoding/json"
"errors"
"fmt"
"strconv"
"github.com/ava-labs/avalanchego/utils/math"
"github.com/ava-labs/hypersdk/consts"
)
const (
Bandwidth Dimension = 0
Compute Dimension = 1
StorageRead Dimension = 2
StorageAllocate Dimension = 3
StorageWrite Dimension = 4 // includes delete
FeeDimensions = 5
DimensionsLen = consts.Uint64Len * FeeDimensions
dimensionsFormatter = "(Bandwidth=%d, Compute=%d, Storage(Read)=%d, Storage(Allocate)=%d, Storage(Write)=%d)"
)
var ErrWrongDimensionSize = errors.New("wrong dimensions size")
type (
Dimension int
Dimensions [FeeDimensions]uint64
)
func Add(a, b Dimensions) (Dimensions, error) {
d := Dimensions{}
for i := Dimension(0); i < FeeDimensions; i++ {
v, err := math.Add(a[i], b[i])
if err != nil {
return Dimensions{}, err
}
d[i] = v
}
return d, nil
}
func MulSum(a, b Dimensions) (uint64, error) {
val := uint64(0)
for i := Dimension(0); i < FeeDimensions; i++ {
v, err := math.Mul(a[i], b[i])
if err != nil {
return 0, err
}
newVal, err := math.Add(val, v)
if err != nil {
return 0, err
}
val = newVal
}
return val, nil
}
func (d Dimensions) Add(i Dimension, v uint64) error {
newValue, err := math.Add(d[i], v)
if err != nil {
return err
}
d[i] = newValue
return nil
}
func (d Dimensions) CanAdd(a Dimensions, l Dimensions) bool {
for i := Dimension(0); i < FeeDimensions; i++ {
consumed, err := math.Add(d[i], a[i])
if err != nil {
return false
}
if consumed > l[i] {
return false
}
}
return true
}
func (d Dimensions) Bytes() []byte {
bytes := make([]byte, DimensionsLen)
for i := Dimension(0); i < FeeDimensions; i++ {
binary.BigEndian.PutUint64(bytes[i*consts.Uint64Len:], d[i])
}
return bytes
}
// Greater is used to determine if the max units allowed
// are greater than the units consumed by a transaction.
//
// This would be considered a fatal error.
func (d Dimensions) Greater(o Dimensions) bool {
for i := Dimension(0); i < FeeDimensions; i++ {
if d[i] < o[i] {
return false
}
}
return true
}
func (d Dimensions) String() string {
return fmt.Sprintf(
dimensionsFormatter,
d[Bandwidth],
d[Compute],
d[StorageRead],
d[StorageAllocate],
d[StorageWrite],
)
}
func (d Dimensions) MarshalText() ([]byte, error) {
return []byte(d.String()), nil
}
func (d *Dimensions) UnmarshalText(b []byte) error {
n, err := fmt.Sscanf(
string(b),
dimensionsFormatter,
&d[Bandwidth],
&d[Compute],
&d[StorageRead],
&d[StorageAllocate],
&d[StorageWrite],
)
if err != nil {
return err
}
if n != FeeDimensions {
return fmt.Errorf("failed to parse %d successive dimensions, found %d", FeeDimensions, n)
}
return nil
}
type DimensionJSON struct {
Bandwidth uint64 `json:"bandwidth"`
Compute uint64 `json:"compute"`
StorageRead uint64 `json:"storageRead"`
StorageAllocate uint64 `json:"storageAllocate"`
StorageWrite uint64 `json:"storageWrite"`
}
func (d Dimensions) MarshalJSON() ([]byte, error) {
dimensionJSON := DimensionJSON{
Bandwidth: d[Bandwidth],
Compute: d[Compute],
StorageRead: d[StorageRead],
StorageAllocate: d[StorageAllocate],
StorageWrite: d[StorageWrite],
}
return json.Marshal(dimensionJSON)
}
func (d *Dimensions) UnmarshalJSON(b []byte) error {
dimensionJSON := DimensionJSON{}
if err := json.Unmarshal(b, &dimensionJSON); err != nil {
return err
}
d[Bandwidth] = dimensionJSON.Bandwidth
d[Compute] = dimensionJSON.Compute
d[StorageRead] = dimensionJSON.StorageRead
d[StorageAllocate] = dimensionJSON.StorageAllocate
d[StorageWrite] = dimensionJSON.StorageWrite
return nil
}
func UnpackDimensions(raw []byte) (Dimensions, error) {
if len(raw) != DimensionsLen {
return Dimensions{}, fmt.Errorf("%w: found=%d wanted=%d", ErrWrongDimensionSize, len(raw), DimensionsLen)
}
d := Dimensions{}
for i := Dimension(0); i < FeeDimensions; i++ {
d[i] = binary.BigEndian.Uint64(raw[i*consts.Uint64Len:])
}
return d, nil
}
func ParseDimensions(raw []string) (Dimensions, error) {
if len(raw) != FeeDimensions {
return Dimensions{}, ErrWrongDimensionSize
}
d := Dimensions{}
for i := Dimension(0); i < FeeDimensions; i++ {
v, err := strconv.ParseUint(raw[i], 10, 64)
if err != nil {
return Dimensions{}, err
}
d[i] = v
}
return d, nil
}