-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcomputers.go
201 lines (173 loc) · 4.81 KB
/
computers.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
package core
import (
"math/big"
"strconv"
"strings"
"time"
"github.com/ElrondNetwork/elrond-go-core/core/check"
"github.com/ElrondNetwork/elrond-go-core/data"
)
// MaxInt32 returns the maximum of two given numbers
func MaxInt32(a int32, b int32) int32 {
if a > b {
return a
}
return b
}
// MinInt returns the minimum of two given numbers
func MinInt(a int, b int) int {
if a < b {
return a
}
return b
}
// MaxInt returns the maximum of two given numbers
func MaxInt(a int, b int) int {
if a > b {
return a
}
return b
}
// MinInt32 returns the minimum of two given numbers
func MinInt32(a int32, b int32) int32 {
if a < b {
return a
}
return b
}
// MaxUint32 returns the maximum of two given numbers
func MaxUint32(a uint32, b uint32) uint32 {
if a > b {
return a
}
return b
}
// MinUint32 returns the minimum of two given numbers
func MinUint32(a uint32, b uint32) uint32 {
if a < b {
return a
}
return b
}
// MaxUint64 returns the maximum of two given numbers
func MaxUint64(a uint64, b uint64) uint64 {
if a > b {
return a
}
return b
}
// MinUint64 returns the minimum of two given numbers
func MinUint64(a uint64, b uint64) uint64 {
if a < b {
return a
}
return b
}
// MaxInt64 returns the maximum of two given numbers
func MaxInt64(a int64, b int64) int64 {
if a > b {
return a
}
return b
}
// MinInt64 returns the minimum of two given numbers
func MinInt64(a int64, b int64) int64 {
if a < b {
return a
}
return b
}
// MaxFloat64 returns the maximum of two given numbers
func MaxFloat64(a float64, b float64) float64 {
if a > b {
return a
}
return b
}
// AbsDuration returns the absolute value of the provided time.Duration parameter
func AbsDuration(duration time.Duration) time.Duration {
if duration < 0 {
return -duration
}
return duration
}
// GetApproximatePercentageOfValue returns the approximate percentage of value
// the approximation comes from floating point operations, which in case of large numbers
// has some loss in accuracy and can cause the result to be slightly lower or higher than the actual value
func GetApproximatePercentageOfValue(value *big.Int, percentage float64) *big.Int {
x := new(big.Float).SetInt(value)
y := big.NewFloat(percentage)
z := new(big.Float).Mul(x, y)
op := big.NewInt(0)
result, _ := z.Int(op)
return result
}
// GetIntTrimmedPercentageOfValue returns the exact percentage of value, that fits into the integer (with loss of division remainder)
func GetIntTrimmedPercentageOfValue(value *big.Int, percentage float64) *big.Int {
x := big.NewInt(0).Set(value)
percentageString := strconv.FormatFloat(percentage, 'f', -1, 64)
exp, fra := splitExponentFraction(percentageString)
concatExpFra := exp + fra
concatBigInt, _ := big.NewInt(0).SetString(concatExpFra, 10)
intMultiplier, _ := big.NewInt(0).SetString("1"+strings.Repeat("0", len(fra)), 10)
x.Mul(x, concatBigInt)
x.Div(x, intMultiplier)
return x
}
// IsInRangeInclusive returns true if the provided value is in the given range, including the provided min and max values
func IsInRangeInclusive(value, min, max *big.Int) bool {
return value.Cmp(min) >= 0 && value.Cmp(max) <= 0
}
// IsInRangeInclusiveFloat64 returns true if the provided value is in the given range including the provided min and max values
func IsInRangeInclusiveFloat64(value, min, max float64) bool {
return value >= min && value <= max
}
func splitExponentFraction(val string) (string, string) {
split := strings.Split(val, ".")
if len(split) == 2 {
return split[0], split[1]
}
return val, ""
}
// SafeMul returns multiplication results of two uint64 values into a big int
func SafeMul(a uint64, b uint64) *big.Int {
return big.NewInt(0).Mul(big.NewInt(0).SetUint64(a), big.NewInt(0).SetUint64(b))
}
// SafeSubUint64 performs subtraction on uint64 and returns an error if it overflows
func SafeSubUint64(a, b uint64) (uint64, error) {
if a < b {
return 0, ErrSubtractionOverflow
}
return a - b, nil
}
// SafeAddUint64 performs addition on uint64 and returns an error if the addition overflows
func SafeAddUint64(a, b uint64) (uint64, error) {
s := a + b
if s >= a && s >= b {
return s, nil
}
return 0, ErrAdditionOverflow
}
// IsValidESDTRole returns true if the input string represents a valid ESDT role
func IsValidESDTRole(role string) bool {
switch role {
case ESDTRoleNFTCreate, ESDTRoleNFTAddQuantity, ESDTRoleNFTBurn, ESDTRoleLocalMint, ESDTRoleLocalBurn, ESDTRoleNFTUpdateAttributes,
ESDTRoleNFTAddURI, ESDTRoleTransfer:
return true
default:
return false
}
}
// GetHeaderType will return the type of the provided header
func GetHeaderType(header data.HeaderHandler) HeaderType {
switch {
case check.IfNil(header):
return ""
case header.GetShardID() == MetachainShardId:
return MetaHeader
case check.IfNil(header.GetAdditionalData()):
return ShardHeaderV1
default:
return ShardHeaderV2
}
}