From 639139795b33369154e540a0e6ba552619790f8a Mon Sep 17 00:00:00 2001 From: whyrusleeping Date: Thu, 26 Sep 2019 16:09:10 -0700 Subject: [PATCH] remember why the go big math library works the way it does --- chain/vm/vm.go | 36 +++++++----------------------------- chain/vm/vm_test.go | 20 -------------------- 2 files changed, 7 insertions(+), 49 deletions(-) diff --git a/chain/vm/vm.go b/chain/vm/vm.go index 3cd88a2c9..6616e8312 100644 --- a/chain/vm/vm.go +++ b/chain/vm/vm.go @@ -633,36 +633,14 @@ func DepositFunds(act *types.Actor, amt types.BigInt) { act.Balance = types.BigAdd(act.Balance, amt) } +var IV = types.BigInt{big.NewInt(0).SetBytes(build.MiningRewardInitialAttoFilBytes)} +var miningRewardTotal = types.FromFil(build.MiningRewardTotal) + // MiningReward returns correct mining reward // coffer is amount of FIL in NetworkAddress func MiningReward(coffer types.BigInt) types.BigInt { - i := &big.Int{} - i.SetBytes(build.MiningRewardInitialAttoFilBytes) - IV := types.BigInt{i} - res := types.BigMul(IV, coffer) - res = types.BigDiv(res, types.FromFil(build.MiningRewardTotal)) - return res + ci := big.NewInt(0).Set(coffer.Int) + res := ci.Mul(ci, IV.Int) + res = res.Div(res, miningRewardTotal.Int) + return types.BigInt{res} } - -/* -func MiningRewardForBlock(height uint64) types.BigInt { - - //decay := e^(ln(0.5) / (HalvingPeriodBlocks / AdjustmentPeriod) - decay := 0.9977808404048861 - - totalMiningReward := types.FromFil(build.MiningRewardTotal) - - dval := big.NewFloat(math.Pow(decay, float64(uint64(height/build.AdjustmentPeriod)))) - - iv := big.NewFloat(0).Mul( - big.NewFloat(0).SetInt(totalMiningReward.Int), - big.NewFloat(1-decay), - ) - - //return (InitialReward * Math.pow(decay, Math.floor(height / adjustmentPeriod))) / adjustmentPeriod - - reward, _ := big.NewFloat(0).Quo(iv.Mul(iv, dval), big.NewFloat(build.AdjustmentPeriod)).Int(nil) - - return types.BigInt{reward} -} -*/ diff --git a/chain/vm/vm_test.go b/chain/vm/vm_test.go index b1fcc34c4..1c31bc729 100644 --- a/chain/vm/vm_test.go +++ b/chain/vm/vm_test.go @@ -2,7 +2,6 @@ package vm import ( "fmt" - "os" "testing" "github.com/filecoin-project/go-lotus/build" @@ -14,10 +13,6 @@ func TestBlockReward(t *testing.T) { sum := types.NewInt(0) N := build.HalvingPeriodBlocks for i := 0; i < N; i++ { - if i%20000 == 0 { - fmt.Fprintf(os.Stderr, "%.1f%%\r", float64(i)/float64(N)*100) - } - a := MiningReward(coffer) sum = types.BigAdd(sum, a) coffer = types.BigSub(coffer, a) @@ -29,19 +24,4 @@ func TestBlockReward(t *testing.T) { fmt.Printf("Total reward: %d\n", build.MiningRewardTotal) fmt.Printf("Remaining: %s\n", types.BigDiv(coffer, types.NewInt(build.FilecoinPrecision))) fmt.Printf("Given out: %s\n", types.BigDiv(sum, types.NewInt(build.FilecoinPrecision))) - - for i := 0; i < N; i++ { - if i%20000 == 0 { - fmt.Fprintf(os.Stderr, "%.1f%%\r", float64(i)/float64(N)*100) - } - - a := MiningReward(coffer) - sum = types.BigAdd(sum, a) - coffer = types.BigSub(coffer, a) - } - - fmt.Println("Next halving period") - fmt.Printf("Total reward: %d\n", build.MiningRewardTotal) - fmt.Printf("Remaining: %s\n", types.BigDiv(coffer, types.NewInt(build.FilecoinPrecision))) - fmt.Printf("Given out: %s\n", types.BigDiv(sum, types.NewInt(build.FilecoinPrecision))) }