From 8a2a5e7e854d39236b4b6bc5b7209b6b553d7993 Mon Sep 17 00:00:00 2001 From: Yusef Napora Date: Tue, 7 Jul 2020 17:02:29 -0400 Subject: [PATCH 1/4] allow fractional balance parameters --- lotus-soup/compositions/composition.toml | 2 +- lotus-soup/testkit/role_bootstrapper.go | 25 +++++++++++++++++++++--- lotus-soup/testkit/role_client.go | 2 +- lotus-soup/testkit/role_miner.go | 2 +- lotus-soup/testkit/sync.go | 2 +- 5 files changed, 26 insertions(+), 7 deletions(-) diff --git a/lotus-soup/compositions/composition.toml b/lotus-soup/compositions/composition.toml index 9ebdc4144..636de9c91 100644 --- a/lotus-soup/compositions/composition.toml +++ b/lotus-soup/compositions/composition.toml @@ -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" diff --git a/lotus-soup/testkit/role_bootstrapper.go b/lotus-soup/testkit/role_bootstrapper.go index 3049a48ca..fca11f9bb 100644 --- a/lotus-soup/testkit/role_bootstrapper.go +++ b/lotus-soup/testkit/role_bootstrapper.go @@ -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 +} \ No newline at end of file diff --git a/lotus-soup/testkit/role_client.go b/lotus-soup/testkit/role_client.go index 7c62862b3..08bb82fbb 100644 --- a/lotus-soup/testkit/role_client.go +++ b/lotus-soup/testkit/role_client.go @@ -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) diff --git a/lotus-soup/testkit/role_miner.go b/lotus-soup/testkit/role_miner.go index 86047dece..4850efedf 100644 --- a/lotus-soup/testkit/role_miner.go +++ b/lotus-soup/testkit/role_miner.go @@ -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) diff --git a/lotus-soup/testkit/sync.go b/lotus-soup/testkit/sync.go index f565c501d..51975ce29 100644 --- a/lotus-soup/testkit/sync.go +++ b/lotus-soup/testkit/sync.go @@ -27,7 +27,7 @@ var ( type InitialBalanceMsg struct { Addr address.Address - Balance int + Balance float64 } type PresealMsg struct { From 735c1ecc2a813be7bb192b9ddb0383a03498ed68 Mon Sep 17 00:00:00 2001 From: Yusef Napora Date: Tue, 7 Jul 2020 17:10:06 -0400 Subject: [PATCH 2/4] update param type in manifest --- lotus-soup/manifest.toml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lotus-soup/manifest.toml b/lotus-soup/manifest.toml index e76b1d396..c2f1a9db9 100644 --- a/lotus-soup/manifest.toml +++ b/lotus-soup/manifest.toml @@ -35,7 +35,7 @@ instances = { min = 1, max = 100, default = 5 } [testcases.params] clients = { type = "int", default = 1 } miners = { type = "int", default = 1 } - balance = { type = "int", default = 1 } + balance = { type = "float", default = 1 } sectors = { type = "int", default = 1 } role = { type = "string" } @@ -64,7 +64,7 @@ instances = { min = 1, max = 100, default = 5 } [testcases.params] clients = { type = "int", default = 1 } miners = { type = "int", default = 1 } - balance = { type = "int", default = 1 } + balance = { type = "float", default = 1 } sectors = { type = "int", default = 1 } role = { type = "string" } genesis_timestamp_offset = { type = "int", default = 0 } @@ -94,7 +94,7 @@ instances = { min = 1, max = 100, default = 5 } [testcases.params] clients = { type = "int", default = 1 } miners = { type = "int", default = 1 } - balance = { type = "int", default = 1 } + balance = { type = "float", default = 1 } sectors = { type = "int", default = 1 } role = { type = "string" } @@ -127,7 +127,7 @@ instances = { min = 1, max = 100, default = 5 } [testcases.params] clients = { type = "int", default = 1 } miners = { type = "int", default = 1 } - balance = { type = "int", default = 1 } + balance = { type = "float", default = 1 } sectors = { type = "int", default = 1 } role = { type = "string" } genesis_timestamp_offset = { type = "int", default = 0 } From 4926a788c1e3b676026c204cd5e42764a976a5ce Mon Sep 17 00:00:00 2001 From: Yusef Napora Date: Tue, 7 Jul 2020 17:15:12 -0400 Subject: [PATCH 3/4] fix max fil check --- lotus-soup/testkit/role_bootstrapper.go | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/lotus-soup/testkit/role_bootstrapper.go b/lotus-soup/testkit/role_bootstrapper.go index fca11f9bb..934050694 100644 --- a/lotus-soup/testkit/role_bootstrapper.go +++ b/lotus-soup/testkit/role_bootstrapper.go @@ -61,8 +61,9 @@ func PrepareBootstrapper(t *TestEnvironment) (*Bootstrapper, error) { totalBalance = big.Add(attoFil(b.Balance), totalBalance) } - t.RecordMessage("TOTAL BALANCE: %s AttoFIL (%f FIL)", totalBalance, fractionalFil(totalBalance)) - if max := types.TotalFilecoinInt; totalBalance.GreaterThanEqual(max) { + totalBalanceFil := attoFilToFil(totalBalance) + t.RecordMessage("TOTAL BALANCE: %s AttoFIL (%s FIL)", totalBalance, totalBalanceFil) + if max := types.TotalFilecoinInt; totalBalanceFil.GreaterThanEqual(max) { panic(fmt.Sprintf("total sum of balances is greater than max Filecoin ever; sum=%s, max=%s", totalBalance, max)) } @@ -188,12 +189,9 @@ func attoFil(f float64) big.Int { 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 -} \ No newline at end of file +func attoFilToFil(atto big.Int) big.Int { + i := big.NewInt(0) + i.Add(i.Int, atto.Int) + i.Div(i.Int, big.NewIntUnsigned(build.FilecoinPrecision).Int) + return i +} From 4227f2d9dc75ae9a8c63c7af5c3f15e9f4fed815 Mon Sep 17 00:00:00 2001 From: Yusef Napora Date: Wed, 8 Jul 2020 10:25:50 -0400 Subject: [PATCH 4/4] rename attoFil -> filToAttoFil --- lotus-soup/testkit/role_bootstrapper.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lotus-soup/testkit/role_bootstrapper.go b/lotus-soup/testkit/role_bootstrapper.go index 934050694..39d5dba58 100644 --- a/lotus-soup/testkit/role_bootstrapper.go +++ b/lotus-soup/testkit/role_bootstrapper.go @@ -58,7 +58,7 @@ func PrepareBootstrapper(t *TestEnvironment) (*Bootstrapper, error) { totalBalance := big.Zero() for _, b := range balances { - totalBalance = big.Add(attoFil(b.Balance), totalBalance) + totalBalance = big.Add(filToAttoFil(b.Balance), totalBalance) } totalBalanceFil := attoFilToFil(totalBalance) @@ -78,7 +78,7 @@ func PrepareBootstrapper(t *TestEnvironment) (*Bootstrapper, error) { var genesisMiners []genesis.Miner for _, bm := range balances { - balance := attoFil(bm.Balance) + balance := filToAttoFil(bm.Balance) t.RecordMessage("balance assigned to actor %s: %s AttoFIL", bm.Addr, balance) genesisActors = append(genesisActors, genesis.Actor{ @@ -181,8 +181,8 @@ func (b *Bootstrapper) RunDefault() error { return nil } -// attoFil converts a fractional filecoin value into AttoFIL, rounding if necessary -func attoFil(f float64) big.Int { +// filToAttoFil converts a fractional filecoin value into AttoFIL, rounding if necessary +func filToAttoFil(f float64) big.Int { a := mbig.NewFloat(f) a.Mul(a, mbig.NewFloat(float64(build.FilecoinPrecision))) i, _ := a.Int(nil)