2019-07-12 10:23:05 +00:00
|
|
|
package types
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2019-08-20 04:37:53 +00:00
|
|
|
"io"
|
2019-07-12 10:23:05 +00:00
|
|
|
"math/big"
|
|
|
|
|
2019-10-18 04:47:41 +00:00
|
|
|
"github.com/filecoin-project/lotus/build"
|
2019-07-12 10:23:05 +00:00
|
|
|
cbor "github.com/ipfs/go-ipld-cbor"
|
|
|
|
"github.com/polydawn/refmt/obj/atlas"
|
2019-09-25 20:52:28 +00:00
|
|
|
|
2019-08-20 04:37:53 +00:00
|
|
|
cbg "github.com/whyrusleeping/cbor-gen"
|
2019-09-06 22:33:30 +00:00
|
|
|
"golang.org/x/xerrors"
|
2019-07-12 10:23:05 +00:00
|
|
|
)
|
|
|
|
|
2019-08-20 04:37:53 +00:00
|
|
|
const BigIntMaxSerializedLen = 128 // is this big enough? or too big?
|
|
|
|
|
2019-10-13 13:03:15 +00:00
|
|
|
var TotalFilecoinInt = FromFil(build.TotalFilecoin)
|
|
|
|
|
2019-07-12 10:23:05 +00:00
|
|
|
func init() {
|
2019-09-26 04:03:13 +00:00
|
|
|
cbor.RegisterCborType(atlas.BuildEntry(BigInt{}).Transform().
|
2019-07-12 10:23:05 +00:00
|
|
|
TransformMarshal(atlas.MakeMarshalTransformFunc(
|
|
|
|
func(i BigInt) ([]byte, error) {
|
2019-09-26 04:03:13 +00:00
|
|
|
return i.cborBytes(), nil
|
2019-07-12 10:23:05 +00:00
|
|
|
})).
|
|
|
|
TransformUnmarshal(atlas.MakeUnmarshalTransformFunc(
|
|
|
|
func(x []byte) (BigInt, error) {
|
2019-09-26 04:03:13 +00:00
|
|
|
return fromCborBytes(x)
|
2019-07-12 10:23:05 +00:00
|
|
|
})).
|
|
|
|
Complete())
|
|
|
|
}
|
|
|
|
|
2019-07-30 00:46:56 +00:00
|
|
|
var EmptyInt = BigInt{}
|
|
|
|
|
2019-07-12 10:23:05 +00:00
|
|
|
type BigInt struct {
|
|
|
|
*big.Int
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewInt(i uint64) BigInt {
|
|
|
|
return BigInt{big.NewInt(0).SetUint64(i)}
|
|
|
|
}
|
|
|
|
|
2019-09-16 16:47:42 +00:00
|
|
|
func FromFil(i uint64) BigInt {
|
|
|
|
return BigMul(NewInt(i), NewInt(build.FilecoinPrecision))
|
2019-09-12 23:48:03 +00:00
|
|
|
}
|
|
|
|
|
2019-07-12 10:23:05 +00:00
|
|
|
func BigFromBytes(b []byte) BigInt {
|
|
|
|
i := big.NewInt(0).SetBytes(b)
|
|
|
|
return BigInt{i}
|
|
|
|
}
|
|
|
|
|
2019-08-07 23:22:35 +00:00
|
|
|
func BigFromString(s string) (BigInt, error) {
|
|
|
|
v, ok := big.NewInt(0).SetString(s, 10)
|
|
|
|
if !ok {
|
|
|
|
return BigInt{}, fmt.Errorf("failed to parse string as a big int")
|
|
|
|
}
|
|
|
|
|
|
|
|
return BigInt{v}, nil
|
|
|
|
}
|
|
|
|
|
2019-07-12 10:23:05 +00:00
|
|
|
func BigMul(a, b BigInt) BigInt {
|
|
|
|
return BigInt{big.NewInt(0).Mul(a.Int, b.Int)}
|
|
|
|
}
|
|
|
|
|
2019-08-15 02:30:21 +00:00
|
|
|
func BigDiv(a, b BigInt) BigInt {
|
|
|
|
return BigInt{big.NewInt(0).Div(a.Int, b.Int)}
|
|
|
|
}
|
|
|
|
|
2019-07-12 10:23:05 +00:00
|
|
|
func BigAdd(a, b BigInt) BigInt {
|
|
|
|
return BigInt{big.NewInt(0).Add(a.Int, b.Int)}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BigSub(a, b BigInt) BigInt {
|
|
|
|
return BigInt{big.NewInt(0).Sub(a.Int, b.Int)}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BigCmp(a, b BigInt) int {
|
|
|
|
return a.Int.Cmp(b.Int)
|
|
|
|
}
|
|
|
|
|
2019-09-26 20:47:34 +00:00
|
|
|
func (bi BigInt) Nil() bool {
|
2019-07-12 10:23:05 +00:00
|
|
|
return bi.Int == nil
|
|
|
|
}
|
|
|
|
|
2019-09-23 17:11:44 +00:00
|
|
|
// LessThan returns true if bi < o
|
2019-09-26 20:47:34 +00:00
|
|
|
func (bi BigInt) LessThan(o BigInt) bool {
|
|
|
|
return BigCmp(bi, o) < 0
|
2019-09-23 17:11:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// LessThan returns true if bi > o
|
2019-09-26 20:47:34 +00:00
|
|
|
func (bi BigInt) GreaterThan(o BigInt) bool {
|
|
|
|
return BigCmp(bi, o) > 0
|
2019-09-23 17:11:44 +00:00
|
|
|
}
|
|
|
|
|
2019-07-12 10:23:05 +00:00
|
|
|
func (bi *BigInt) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(bi.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bi *BigInt) UnmarshalJSON(b []byte) error {
|
|
|
|
var s string
|
|
|
|
if err := json.Unmarshal(b, &s); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
i, ok := big.NewInt(0).SetString(s, 10)
|
|
|
|
if !ok {
|
2019-07-18 19:12:30 +00:00
|
|
|
if string(s) == "<nil>" {
|
|
|
|
return nil
|
|
|
|
}
|
2019-09-06 22:33:30 +00:00
|
|
|
return xerrors.Errorf("failed to parse bigint string: '%s'", string(b))
|
2019-07-12 10:23:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bi.Int = i
|
|
|
|
return nil
|
|
|
|
}
|
2019-08-20 04:37:53 +00:00
|
|
|
|
2019-09-26 04:03:13 +00:00
|
|
|
func (bi *BigInt) cborBytes() []byte {
|
2019-08-22 01:29:19 +00:00
|
|
|
if bi.Int == nil {
|
2019-09-26 04:03:13 +00:00
|
|
|
return []byte{}
|
2019-08-22 01:29:19 +00:00
|
|
|
}
|
|
|
|
|
2019-09-25 20:52:28 +00:00
|
|
|
switch {
|
|
|
|
case bi.Sign() == 0:
|
2019-09-26 04:03:13 +00:00
|
|
|
return []byte{}
|
2019-09-25 20:52:28 +00:00
|
|
|
case bi.Sign() > 0:
|
2019-09-26 04:03:13 +00:00
|
|
|
return append([]byte{0}, bi.Bytes()...)
|
2019-09-25 20:52:28 +00:00
|
|
|
case bi.Sign() < 0:
|
2019-09-26 04:03:13 +00:00
|
|
|
return append([]byte{1}, bi.Bytes()...)
|
|
|
|
}
|
|
|
|
|
|
|
|
panic("unreachable")
|
|
|
|
}
|
|
|
|
|
|
|
|
func fromCborBytes(buf []byte) (BigInt, error) {
|
2019-10-14 14:59:47 +00:00
|
|
|
if len(buf) == 0 {
|
|
|
|
return NewInt(0), nil
|
|
|
|
}
|
|
|
|
|
2019-09-26 04:03:13 +00:00
|
|
|
var negative bool
|
|
|
|
switch buf[0] {
|
|
|
|
case 0:
|
|
|
|
negative = false
|
|
|
|
case 1:
|
|
|
|
negative = true
|
|
|
|
default:
|
|
|
|
return EmptyInt, fmt.Errorf("big int prefix should be either 0 or 1, got %d", buf[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
i := big.NewInt(0).SetBytes(buf[1:])
|
|
|
|
if negative {
|
|
|
|
i.Neg(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
return BigInt{i}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bi *BigInt) MarshalCBOR(w io.Writer) error {
|
|
|
|
if bi.Int == nil {
|
|
|
|
zero := NewInt(0)
|
|
|
|
return zero.MarshalCBOR(w)
|
2019-08-20 04:37:53 +00:00
|
|
|
}
|
|
|
|
|
2019-09-26 04:03:13 +00:00
|
|
|
enc := bi.cborBytes()
|
|
|
|
|
2019-09-25 20:52:28 +00:00
|
|
|
header := cbg.CborEncodeMajorType(cbg.MajByteString, uint64(len(enc)))
|
2019-08-20 04:37:53 +00:00
|
|
|
if _, err := w.Write(header); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-09-25 20:52:28 +00:00
|
|
|
if _, err := w.Write(enc); err != nil {
|
2019-08-20 04:37:53 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-08-22 01:29:19 +00:00
|
|
|
func (bi *BigInt) UnmarshalCBOR(br io.Reader) error {
|
2019-08-20 04:37:53 +00:00
|
|
|
maj, extra, err := cbg.CborReadHeader(br)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-09-25 20:52:28 +00:00
|
|
|
if maj != cbg.MajByteString {
|
|
|
|
return fmt.Errorf("cbor input for fil big int was not a byte string")
|
2019-08-20 04:37:53 +00:00
|
|
|
}
|
|
|
|
|
2019-09-25 20:52:28 +00:00
|
|
|
if extra == 0 {
|
|
|
|
bi.Int = big.NewInt(0)
|
|
|
|
return nil
|
2019-08-20 04:37:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if extra > BigIntMaxSerializedLen {
|
|
|
|
return fmt.Errorf("big integer byte array too long")
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := make([]byte, extra)
|
|
|
|
if _, err := io.ReadFull(br, buf); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-09-26 04:03:13 +00:00
|
|
|
i, err := fromCborBytes(buf)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-09-25 20:52:28 +00:00
|
|
|
}
|
|
|
|
|
2019-09-26 04:03:13 +00:00
|
|
|
*bi = i
|
2019-08-20 04:37:53 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|