forked from yancaitech/go-xrp
/
value.go
502 lines (459 loc) · 11.8 KB
/
value.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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
package data
import (
"bytes"
"encoding/binary"
"fmt"
"math"
"math/big"
"regexp"
"strconv"
"strings"
)
const (
minOffset int64 = -96
maxOffset int64 = 80
minValue uint64 = 1000000000000000
maxValue uint64 = 9999999999999999
maxNative uint64 = 9000000000000000000
maxNativeNetwork uint64 = 100000000000000000
notNative uint64 = 0x8000000000000000
positive uint64 = 0x4000000000000000
maxNativeSqrt uint64 = 3000000000
maxNativeDiv uint64 = 2095475792 // MaxNative / 2^32
tenTo14 uint64 = 100000000000000
tenTo14m1 uint64 = tenTo14 - 1
tenTo17 uint64 = tenTo14 * 1000
tenTo17m1 uint64 = tenTo17 - 1
xrpPrecision uint64 = 1000000
)
var (
bigTen = big.NewInt(10)
bigTenTo14 = big.NewInt(0).SetUint64(tenTo14)
bigTenTo17 = big.NewInt(0).SetUint64(tenTo17)
zeroNative = *newValue(true, false, 0, 0)
zeroNonNative = *newValue(false, false, 0, 0)
xrpMultipler = newValue(true, false, xrpPrecision, 0)
)
// Value is the numeric type in Ripple. It can store numbers in two different
// representations: native and non-native.
// Non-native numbers are stored as a mantissa (Num) in the range [1e15,1e16)
// plus an exponent (Offset) in the range [-96,80].
// Native values are stored as an integer number of "drips" each representing
// 1/1000000.
// Throughout the library, native values are interpreted as drips unless
// otherwise specified.
type Value struct {
native bool
negative bool
num uint64
offset int64
}
func init() {
if err := zeroNative.canonicalise(); err != nil {
panic(err)
}
if err := zeroNonNative.canonicalise(); err != nil {
panic(err)
}
if err := xrpMultipler.canonicalise(); err != nil {
panic(err)
}
}
func newValue(native, negative bool, num uint64, offset int64) *Value {
return &Value{
native: native,
negative: negative,
num: num,
offset: offset,
}
}
// NewNativeValue returns a Value with n drops.
// If the value is impossible an error is returned.
func NewNativeValue(n int64) (*Value, error) {
v := newValue(true, n < 0, uint64(n), 0)
return v, v.canonicalise()
}
// NewNonNativeValue returns a Value of n*10^offset.
func NewNonNativeValue(n int64, offset int64) (*Value, error) {
v := newValue(false, n < 0, uint64(n), offset)
return v, v.canonicalise()
}
// Match fields:
// 0 = whole input
// 1 = sign
// 2 = integer portion
// 3 = whole fraction (with '.')
// 4 = fraction (without '.')
// 5 = whole exponent (with 'e')
// 6 = exponent sign
// 7 = exponent number
var valueRegex = regexp.MustCompile("([+-]?)(\\d*)(\\.(\\d*))?([eE]([+-]?)(\\d+))?")
// NewValue accepts a string representation of a value and a flag to indicate if it
// should be stored as native. If the native flag is set AND a decimal is used, the
// number is interpreted as XRP. If no decimal is used, it is interpreted as drips.
func NewValue(s string, native bool) (*Value, error) {
var err error
v := Value{
native: native,
}
matches := valueRegex.FindStringSubmatch(s)
if matches == nil {
return nil, fmt.Errorf("Invalid Number: %s", s)
}
if len(matches[2])+len(matches[4]) > 32 {
return nil, fmt.Errorf("Overlong Number: %s", s)
}
if matches[1] == "-" {
v.negative = true
}
if len(matches[4]) == 0 {
if v.num, err = strconv.ParseUint(matches[2], 10, 64); err != nil {
return nil, fmt.Errorf("Invalid Number: %s Reason: %s", s, err.Error())
}
v.offset = 0
} else {
if v.num, err = strconv.ParseUint(matches[2]+matches[4], 10, 64); err != nil {
return nil, fmt.Errorf("Invalid Number: %s Reason: %s", s, err.Error())
}
v.offset = -int64(len(matches[4]))
}
if len(matches[5]) > 0 {
exp, err := strconv.ParseInt(matches[7], 10, 64)
if err != nil {
return nil, fmt.Errorf("Invalid Number: %s %s", s, err.Error())
}
if matches[6] == "-" {
v.offset -= exp
} else {
v.offset += exp
}
}
if v.IsNative() && len(matches[3]) > 0 {
v.offset += 6
}
return &v, v.canonicalise()
}
func (v *Value) canonicalise() error {
if v.IsNative() {
if v.num == 0 {
v.offset = 0
v.negative = false
} else {
for v.offset < 0 {
v.num /= 10
v.offset++
}
for v.offset > 0 {
v.num *= 10
v.offset--
}
if v.num > maxNative {
return fmt.Errorf("Native amount out of range: %s", v.debug())
}
}
} else {
if v.num == 0 {
v.offset = -100
v.negative = false
} else {
for v.num < minValue && v.offset > minOffset {
v.num *= 10
v.offset--
}
for v.num > maxValue {
if v.offset >= maxOffset {
return fmt.Errorf("Value overflow: %s", v.debug())
}
v.num /= 10
v.offset++
}
if v.offset < minOffset || v.num < minValue {
v.num = 0
v.offset = 0
v.negative = false
}
if v.offset > maxOffset {
return fmt.Errorf("Value overflow: %s", v.debug())
}
}
}
return nil
}
// Native returns a clone of the value in native format.
func (v Value) Native() (*Value, error) {
v.native = true
return &v, v.canonicalise()
}
// NonNative returns a clone of the value in non-native format.
func (v Value) NonNative() (*Value, error) {
v.native = false
return &v, v.canonicalise()
}
// Clone returns a Value which is a copy of v.
func (v Value) Clone() *Value {
return newValue(v.native, v.negative, v.num, v.offset)
}
// ZeroClone returns a zero Value, native or non-native depending on v's setting.
func (v Value) ZeroClone() *Value {
if v.IsNative() {
return zeroNative.Clone()
}
return zeroNonNative.Clone()
}
// Abs returns a copy of v with a positive sign.
func (v Value) Abs() *Value {
return newValue(v.native, false, v.num, v.offset)
}
// Negate returns a new Value with the opposite sign of v.
func (v Value) Negate() *Value {
return newValue(v.native, !v.negative, v.num, v.offset)
}
func (a Value) factor(b Value) (int64, int64, int64) {
ao, bo := a.offset, b.offset
av, bv := int64(a.num), int64(b.num)
if a.negative {
av = -av
}
if b.negative {
bv = -bv
}
if a.IsZero() {
return av, bv, bo
}
if b.IsZero() {
return av, bv, ao
}
// FIXME: This can silently underflow
for ; ao < bo; ao++ {
av /= 10
}
for ; bo < ao; bo++ {
bv /= 10
}
return av, bv, ao
}
// Add adds a to b and returns the sum as a new Value.
func (a Value) Add(b Value) (*Value, error) {
switch {
case a.IsNative() != b.IsNative():
return nil, fmt.Errorf("Cannot add native and non-native values")
case a.IsZero():
return b.Clone(), nil
case b.IsZero():
return a.Clone(), nil
default:
av, bv, ao := a.factor(b)
v := newValue(a.native, (av+bv) < 0, abs(av+bv), ao)
return v, v.canonicalise()
}
}
func (a Value) Subtract(b Value) (*Value, error) {
return a.Add(*b.Negate())
}
func normalise(a, b Value) (uint64, uint64, int64, int64) {
av, bv := a.num, b.num
ao, bo := a.offset, b.offset
if a.IsNative() {
for ; av < minValue; av *= 10 {
ao--
}
}
if b.IsNative() {
for ; bv < minValue; bv *= 10 {
bo--
}
}
return av, bv, ao, bo
}
func (a Value) Multiply(b Value) (*Value, error) {
if a.IsZero() || b.IsZero() {
return a.ZeroClone(), nil
}
if a.IsNative() && b.IsNative() {
min := min64(a.num, b.num)
max := max64(a.num, b.num)
if min > maxNativeSqrt || (((max >> 32) * min) > maxNativeDiv) {
return nil, fmt.Errorf("Native value overflow: %s*%s", a.debug(), b.debug())
}
return NewNativeValue(int64(min * max))
}
av, bv, ao, bo := normalise(a, b)
// Compute (numerator * denominator) / 10^14 with rounding
// 10^16 <= result <= 10^18
m := big.NewInt(0).SetUint64(av)
m.Mul(m, big.NewInt(0).SetUint64(bv))
m.Div(m, bigTenTo14)
// 10^16 <= product <= 10^18
if len(m.Bytes()) > 64 {
return nil, fmt.Errorf("Multiply: %s*%s", a.debug(), b.debug())
}
v := newValue(a.native, a.negative != b.negative, m.Uint64()+7, ao+bo+14)
return v, v.canonicalise()
}
func (num Value) Divide(den Value) (*Value, error) {
if den.IsZero() {
return nil, fmt.Errorf("Division by zero")
}
if num.IsZero() {
return num.ZeroClone(), nil
}
av, bv, ao, bo := normalise(num, den)
// Compute (numerator * 10^17) / denominator
d := big.NewInt(0).SetUint64(av)
d.Mul(d, bigTenTo17)
d.Div(d, big.NewInt(0).SetUint64(bv))
// 10^16 <= quotient <= 10^18
if len(d.Bytes()) > 64 {
return nil, fmt.Errorf("Divide: %s/%s", num.debug(), den.debug())
}
v := newValue(num.native, num.negative != den.negative, d.Uint64()+5, ao-bo-17)
return v, v.canonicalise()
}
// Ratio returns the ratio a/b. XRP are interpreted at face value rather than drips.
// The result of Ratio is always a non-native Value for additional precision.
func (a Value) Ratio(b Value) (*Value, error) {
var err error
num := &a
den := &b
if num.IsNative() {
num, err = num.NonNative()
if err != nil {
return nil, err
}
num, err = num.Divide(*xrpMultipler)
if err != nil {
return nil, err
}
}
if den.IsNative() {
den, err = den.NonNative()
if err != nil {
return nil, err
}
den, err = den.Divide(*xrpMultipler)
if err != nil {
return nil, err
}
}
quotient, err := num.Divide(*den)
if err != nil {
return nil, err
}
return quotient, nil
}
// Less compares values and returns true if a is less than b.
func (a Value) Less(b Value) bool {
return a.Compare(b) < 0
}
func (a Value) Equals(b Value) bool {
return a.Compare(b) == 0
}
// Compare returns an integer comparing two Values.
// The result will be 0 if a==b, -1 if a < b, and +1 if a > b.
func (a Value) Compare(b Value) int {
return a.Rat().Cmp(b.Rat())
}
// isScientific indicates when the value should be String()ed in scientific notation.
func (v Value) isScientific() bool {
return v.offset != 0 && (v.offset < -25 || v.offset > -5)
}
func (v Value) IsNative() bool {
return v.native
}
func (v Value) IsNegative() bool {
return v.negative
}
func (v Value) IsZero() bool {
return v.num == 0
}
func (v *Value) Bytes() []byte {
if v == nil {
return nil
}
var u uint64
if !v.negative && (v.num > 0 || v.IsNative()) {
u |= 1 << 62
}
if v.IsNative() {
u |= v.num & ((1 << 62) - 1)
} else {
u |= 1 << 63
u |= v.num & ((1 << 54) - 1)
if v.num > 0 {
u |= uint64(v.offset+97) << 54
}
}
var b [8]byte
binary.BigEndian.PutUint64(b[:], u)
return b[:]
}
func (v Value) MarshalBinary() ([]byte, error) {
return v.Bytes(), nil
}
func (v *Value) UnmarshalBinary(b []byte) error {
buf := bytes.NewBuffer(b)
return v.Unmarshal(buf)
}
// Rat returns the value as a big.Rat.
func (v Value) Rat() *big.Rat {
n := big.NewInt(int64(v.num))
if v.negative {
n.Neg(n)
}
d := big.NewInt(1)
if v.offset < 0 {
d.Exp(big.NewInt(10), big.NewInt(-v.offset), nil)
} else if v.offset > 0 {
mult := big.NewInt(1)
mult.Exp(big.NewInt(10), big.NewInt(v.offset), nil)
n.Mul(n, mult)
}
res := big.NewRat(0, 1)
res.SetFrac(n, d)
return res
}
func (v Value) Float() float64 {
switch {
case v.negative && v.native:
return -float64(v.num) / 1000000
case v.native:
return float64(v.num) / 1000000
case v.negative:
return -float64(v.num) * math.Pow10(int(v.offset))
default:
return float64(v.num) * math.Pow10(int(v.offset))
}
}
// String returns the Value as a string for human consumption. Native values are
// represented as decimal XRP rather than drips.
func (v Value) String() string {
if v.IsZero() {
return "0"
}
if !v.IsNative() && v.isScientific() {
value := strconv.FormatUint(v.num, 10)
origLen := len(value)
value = strings.TrimRight(value, "0")
offset := strconv.FormatInt(v.offset+int64(origLen-len(value)), 10)
if v.negative {
return "-" + value + "e" + offset
}
return value + "e" + offset
}
rat := v.Rat()
if v.IsNative() {
rat.Quo(rat, big.NewRat(int64(xrpPrecision), 1))
}
left := rat.FloatString(0)
if rat.IsInt() {
return left
}
length := len(left)
if v.negative {
length -= 1
}
return strings.TrimRight(rat.FloatString(32-length), "0")
}
func (v Value) debug() string {
return fmt.Sprintf("Native: %t Negative: %t Value: %d Offset: %d", v.native, v.negative, v.num, v.offset)
}