Encode types.Difficulty as uint64 if Difficulty.Hi is zero
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
010a2a0d03
commit
ae74c56a08
|
@ -8,10 +8,12 @@ import (
|
||||||
"git.gammaspectra.live/P2Pool/p2pool-observer/utils"
|
"git.gammaspectra.live/P2Pool/p2pool-observer/utils"
|
||||||
"github.com/holiman/uint256"
|
"github.com/holiman/uint256"
|
||||||
fasthex "github.com/tmthrgd/go-hex"
|
fasthex "github.com/tmthrgd/go-hex"
|
||||||
|
"io"
|
||||||
"lukechampine.com/uint128"
|
"lukechampine.com/uint128"
|
||||||
"math"
|
"math"
|
||||||
"math/big"
|
"math/big"
|
||||||
"math/bits"
|
"math/bits"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -189,6 +191,10 @@ func (d Difficulty) Big() *big.Int {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d Difficulty) MarshalJSON() ([]byte, error) {
|
func (d Difficulty) MarshalJSON() ([]byte, error) {
|
||||||
|
if d.Hi == 0 {
|
||||||
|
return []byte(strconv.FormatUint(d.Lo, 10)), nil
|
||||||
|
}
|
||||||
|
|
||||||
var encodeBuf [DifficultySize]byte
|
var encodeBuf [DifficultySize]byte
|
||||||
d.PutBytesBE(encodeBuf[:])
|
d.PutBytesBE(encodeBuf[:])
|
||||||
|
|
||||||
|
@ -243,28 +249,42 @@ func DifficultyFrom64(v uint64) Difficulty {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Difficulty) UnmarshalJSON(b []byte) error {
|
func (d *Difficulty) UnmarshalJSON(b []byte) error {
|
||||||
var s string
|
if len(b) == 0 {
|
||||||
if err := utils.UnmarshalJSON(b, &s); err != nil {
|
return io.ErrUnexpectedEOF
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(b) == DifficultySize*2+2 {
|
if b[0] == '"' {
|
||||||
// fast path
|
if len(b) < 2 || (len(b)%2) != 0 || b[len(b)-1] != '"' {
|
||||||
var buf [DifficultySize]byte
|
return errors.New("invalid bytes")
|
||||||
if _, err := fasthex.Decode(buf[:], b[1:len(b)-1]); err != nil {
|
}
|
||||||
|
|
||||||
|
if len(b) == DifficultySize*2+2 {
|
||||||
|
// fast path
|
||||||
|
var buf [DifficultySize]byte
|
||||||
|
if _, err := fasthex.Decode(buf[:], b[1:len(b)-1]); err != nil {
|
||||||
|
return err
|
||||||
|
} else {
|
||||||
|
*d = DifficultyFromBytes(buf[:])
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if diff, err := DifficultyFromString(string(b[1 : len(b)-1])); err != nil {
|
||||||
return err
|
return err
|
||||||
} else {
|
} else {
|
||||||
*d = DifficultyFromBytes(buf[:])
|
*d = diff
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if diff, err := DifficultyFromString(s); err != nil {
|
|
||||||
return err
|
|
||||||
} else {
|
} else {
|
||||||
*d = diff
|
// Difficulty as uint64
|
||||||
|
var err error
|
||||||
return nil
|
if d.Lo, err = utils.ParseUint64(b); err != nil {
|
||||||
|
return err
|
||||||
|
} else {
|
||||||
|
d.Hi = 0
|
||||||
|
return nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
|
"fmt"
|
||||||
"github.com/jxskiss/base62"
|
"github.com/jxskiss/base62"
|
||||||
fasthex "github.com/tmthrgd/go-hex"
|
fasthex "github.com/tmthrgd/go-hex"
|
||||||
"math/bits"
|
"math/bits"
|
||||||
|
@ -162,3 +163,42 @@ func UVarInt64Size[T uint64 | int](v T) (n int) {
|
||||||
return binary.MaxVarintLen64
|
return binary.MaxVarintLen64
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ParseUint64 parses uint64 from s.
|
||||||
|
//
|
||||||
|
// It is equivalent to strconv.ParseUint(s, 10, 64), but is faster.
|
||||||
|
//
|
||||||
|
// From https://github.com/valyala/fastjson
|
||||||
|
func ParseUint64(s []byte) (uint64, error) {
|
||||||
|
if len(s) == 0 {
|
||||||
|
return 0, fmt.Errorf("cannot parse uint64 from empty string")
|
||||||
|
}
|
||||||
|
i := uint(0)
|
||||||
|
d := uint64(0)
|
||||||
|
j := i
|
||||||
|
for i < uint(len(s)) {
|
||||||
|
if s[i] >= '0' && s[i] <= '9' {
|
||||||
|
d = d*10 + uint64(s[i]-'0')
|
||||||
|
i++
|
||||||
|
if i > 18 {
|
||||||
|
// The integer part may be out of range for uint64.
|
||||||
|
// Fall back to slow parsing.
|
||||||
|
dd, err := strconv.ParseUint(string(s), 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return dd, nil
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if i <= j {
|
||||||
|
return 0, fmt.Errorf("cannot parse uint64 from %q", s)
|
||||||
|
}
|
||||||
|
if i < uint(len(s)) {
|
||||||
|
// Unparsed tail left.
|
||||||
|
return 0, fmt.Errorf("unparsed tail left after parsing uint64 from %q: %q", s, s[i:])
|
||||||
|
}
|
||||||
|
return d, nil
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue