Unmarshal Difficulty bigint from JSON

This commit is contained in:
DataHoarder 2024-04-09 17:49:22 +02:00
parent 4a8971dae1
commit a79fa9f9f2
Signed by: DataHoarder
SSH key fingerprint: SHA256:OLTRf6Fl87G52SiR7sWLGNzlJt4WOX+tfI2yxo0z7xk

View file

@ -3,6 +3,7 @@ package types
import (
"database/sql/driver"
"errors"
"fmt"
"git.gammaspectra.live/P2Pool/consensus/v3/utils"
fasthex "github.com/tmthrgd/go-hex"
"io"
@ -249,7 +250,7 @@ func DifficultyFrom64(v uint64) Difficulty {
return NewDifficulty(v, 0)
}
func (d *Difficulty) UnmarshalJSON(b []byte) error {
func (d *Difficulty) UnmarshalJSON(b []byte) (err error) {
if len(b) == 0 {
return io.ErrUnexpectedEOF
}
@ -279,8 +280,24 @@ func (d *Difficulty) UnmarshalJSON(b []byte) error {
}
} else {
// Difficulty as uint64
var err error
if d.Lo, err = utils.ParseUint64(b); err != nil {
// Fallback to big int if number is out of range
if errors.Is(err, strconv.ErrRange) {
var bInt big.Int
if err = bInt.UnmarshalText(b); err != nil {
return err
} else {
//recover bigint panics
defer func() {
if e := recover(); e != nil {
if err = e.(error); err == nil {
err = fmt.Errorf("panic: %v", e)
}
}
}()
*d = Difficulty(uint128.FromBig(&bInt))
}
}
return err
} else {
d.Hi = 0