allow fractional balance parameters

This commit is contained in:
Yusef Napora 2020-07-07 17:02:29 -04:00
parent 6a45211a00
commit 8a2a5e7e85
5 changed files with 26 additions and 7 deletions

View File

@ -22,7 +22,7 @@
clients = "3"
miners = "2"
genesis_timestamp_offset = "0"
balance = "2000000000" ## be careful, this is in FIL.
balance = "20000000.5"
sectors = "10"
random_beacon_type = "mock"
mining_mode = "natural"

View File

@ -4,6 +4,7 @@ import (
"bytes"
"context"
"fmt"
mbig "math/big"
"time"
"github.com/filecoin-project/lotus/build"
@ -57,10 +58,10 @@ func PrepareBootstrapper(t *TestEnvironment) (*Bootstrapper, error) {
totalBalance := big.Zero()
for _, b := range balances {
totalBalance = big.Add(big.NewInt(int64(b.Balance)), totalBalance)
totalBalance = big.Add(attoFil(b.Balance), totalBalance)
}
t.RecordMessage("TOTAL BALANCE: %s", totalBalance)
t.RecordMessage("TOTAL BALANCE: %s AttoFIL (%f FIL)", totalBalance, fractionalFil(totalBalance))
if max := types.TotalFilecoinInt; totalBalance.GreaterThanEqual(max) {
panic(fmt.Sprintf("total sum of balances is greater than max Filecoin ever; sum=%s, max=%s", totalBalance, max))
}
@ -76,7 +77,7 @@ func PrepareBootstrapper(t *TestEnvironment) (*Bootstrapper, error) {
var genesisMiners []genesis.Miner
for _, bm := range balances {
balance := big.Mul(big.NewInt(int64(bm.Balance)), types.NewInt(build.FilecoinPrecision))
balance := attoFil(bm.Balance)
t.RecordMessage("balance assigned to actor %s: %s AttoFIL", bm.Addr, balance)
genesisActors = append(genesisActors,
genesis.Actor{
@ -178,3 +179,21 @@ func (b *Bootstrapper) RunDefault() error {
b.t.SyncClient.MustSignalAndWait(ctx, StateDone, b.t.TestInstanceCount)
return nil
}
// attoFil converts a fractional filecoin value into AttoFIL, rounding if necessary
func attoFil(f float64) big.Int {
a := mbig.NewFloat(f)
a.Mul(a, mbig.NewFloat(float64(build.FilecoinPrecision)))
i, _ := a.Int(nil)
return big.Int{Int: i}
}
// fractionalFil converts from AttoFIL to a fractional Fil value
// possibly losing some precision due to floating point gremlins
func fractionalFil(atto big.Int) float64 {
f := mbig.NewFloat(0)
f.SetInt(atto.Int)
f.Quo(f, mbig.NewFloat(float64(build.FilecoinPrecision)))
val, _ := f.Float64()
return val
}

View File

@ -45,7 +45,7 @@ func PrepareClient(t *TestEnvironment) (*LotusClient, error) {
}
// publish the account ID/balance
balance := t.IntParam("balance")
balance := t.FloatParam("balance")
balanceMsg := &InitialBalanceMsg{Addr: walletKey.Address, Balance: balance}
t.SyncClient.Publish(ctx, BalanceTopic, balanceMsg)

View File

@ -63,7 +63,7 @@ func PrepareMiner(t *TestEnvironment) (*LotusMiner, error) {
}
// publish the account ID/balance
balance := t.IntParam("balance")
balance := t.FloatParam("balance")
balanceMsg := &InitialBalanceMsg{Addr: walletKey.Address, Balance: balance}
t.SyncClient.Publish(ctx, BalanceTopic, balanceMsg)

View File

@ -27,7 +27,7 @@ var (
type InitialBalanceMsg struct {
Addr address.Address
Balance int
Balance float64
}
type PresealMsg struct {