2019-07-12 10:23:05 +00:00
|
|
|
package types
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math/big"
|
|
|
|
|
2020-02-08 02:18:32 +00:00
|
|
|
big2 "github.com/filecoin-project/specs-actors/actors/abi/big"
|
2019-09-25 20:52:28 +00:00
|
|
|
|
2020-02-08 02:18:32 +00:00
|
|
|
"github.com/filecoin-project/lotus/build"
|
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-30 00:46:56 +00:00
|
|
|
var EmptyInt = BigInt{}
|
|
|
|
|
2020-02-08 02:18:32 +00:00
|
|
|
type BigInt = big2.Int
|
2019-07-12 10:23:05 +00:00
|
|
|
|
|
|
|
func NewInt(i uint64) BigInt {
|
2020-03-06 06:46:07 +00:00
|
|
|
return BigInt{Int: big.NewInt(0).SetUint64(i)}
|
2019-07-12 10:23:05 +00:00
|
|
|
}
|
|
|
|
|
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)
|
2020-03-06 06:46:07 +00:00
|
|
|
return BigInt{Int: i}
|
2019-07-12 10:23:05 +00:00
|
|
|
}
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2020-03-06 06:46:07 +00:00
|
|
|
return BigInt{Int: v}, nil
|
2019-08-07 23:22:35 +00:00
|
|
|
}
|
|
|
|
|
2019-07-12 10:23:05 +00:00
|
|
|
func BigMul(a, b BigInt) BigInt {
|
2020-03-06 06:46:07 +00:00
|
|
|
return BigInt{Int: big.NewInt(0).Mul(a.Int, b.Int)}
|
2019-07-12 10:23:05 +00:00
|
|
|
}
|
|
|
|
|
2019-08-15 02:30:21 +00:00
|
|
|
func BigDiv(a, b BigInt) BigInt {
|
2020-03-06 06:46:07 +00:00
|
|
|
return BigInt{Int: big.NewInt(0).Div(a.Int, b.Int)}
|
2019-08-15 02:30:21 +00:00
|
|
|
}
|
|
|
|
|
2019-11-13 14:48:57 +00:00
|
|
|
func BigMod(a, b BigInt) BigInt {
|
2020-03-06 06:46:07 +00:00
|
|
|
return BigInt{Int: big.NewInt(0).Mod(a.Int, b.Int)}
|
2019-11-13 14:48:57 +00:00
|
|
|
}
|
|
|
|
|
2019-07-12 10:23:05 +00:00
|
|
|
func BigAdd(a, b BigInt) BigInt {
|
2020-03-06 06:46:07 +00:00
|
|
|
return BigInt{Int: big.NewInt(0).Add(a.Int, b.Int)}
|
2019-07-12 10:23:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func BigSub(a, b BigInt) BigInt {
|
2020-03-06 06:46:07 +00:00
|
|
|
return BigInt{Int: big.NewInt(0).Sub(a.Int, b.Int)}
|
2019-07-12 10:23:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func BigCmp(a, b BigInt) int {
|
|
|
|
return a.Int.Cmp(b.Int)
|
|
|
|
}
|
|
|
|
|
2020-04-21 19:46:50 +00:00
|
|
|
var byteSizeUnits = []string{"B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB"}
|
2019-12-16 13:49:58 +00:00
|
|
|
|
2020-02-08 02:18:32 +00:00
|
|
|
func SizeStr(bi BigInt) string {
|
2019-12-16 13:49:58 +00:00
|
|
|
r := new(big.Rat).SetInt(bi.Int)
|
|
|
|
den := big.NewRat(1, 1024)
|
|
|
|
|
|
|
|
var i int
|
2020-04-21 19:46:50 +00:00
|
|
|
for f, _ := r.Float64(); f >= 1024 && i+1 < len(byteSizeUnits); f, _ = r.Float64() {
|
2019-12-16 13:49:58 +00:00
|
|
|
i++
|
|
|
|
r = r.Mul(r, den)
|
|
|
|
}
|
|
|
|
|
|
|
|
f, _ := r.Float64()
|
2020-04-21 19:46:50 +00:00
|
|
|
return fmt.Sprintf("%.3g %s", f, byteSizeUnits[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
var decUnits = []string{"", "K", "M", "G", "T", "P", "E", "Z"}
|
|
|
|
|
|
|
|
func DecStr(bi BigInt) string {
|
|
|
|
r := new(big.Rat).SetInt(bi.Int)
|
|
|
|
den := big.NewRat(1, 1000)
|
|
|
|
|
|
|
|
var i int
|
|
|
|
for f, _ := r.Float64(); f >= 1000 && i+1 < len(decUnits); f, _ = r.Float64() {
|
|
|
|
i++
|
|
|
|
r = r.Mul(r, den)
|
|
|
|
}
|
|
|
|
|
|
|
|
f, _ := r.Float64()
|
|
|
|
return fmt.Sprintf("%.3g %s", f, decUnits[i])
|
2019-12-16 13:49:58 +00:00
|
|
|
}
|