remember why the go big math library works the way it does

This commit is contained in:
whyrusleeping 2019-09-26 16:09:10 -07:00
parent ef5e7674db
commit 639139795b
2 changed files with 7 additions and 49 deletions

View File

@ -633,36 +633,14 @@ func DepositFunds(act *types.Actor, amt types.BigInt) {
act.Balance = types.BigAdd(act.Balance, amt) 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 // MiningReward returns correct mining reward
// coffer is amount of FIL in NetworkAddress // coffer is amount of FIL in NetworkAddress
func MiningReward(coffer types.BigInt) types.BigInt { func MiningReward(coffer types.BigInt) types.BigInt {
i := &big.Int{} ci := big.NewInt(0).Set(coffer.Int)
i.SetBytes(build.MiningRewardInitialAttoFilBytes) res := ci.Mul(ci, IV.Int)
IV := types.BigInt{i} res = res.Div(res, miningRewardTotal.Int)
res := types.BigMul(IV, coffer) return types.BigInt{res}
res = types.BigDiv(res, types.FromFil(build.MiningRewardTotal))
return 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}
}
*/

View File

@ -2,7 +2,6 @@ package vm
import ( import (
"fmt" "fmt"
"os"
"testing" "testing"
"github.com/filecoin-project/go-lotus/build" "github.com/filecoin-project/go-lotus/build"
@ -14,10 +13,6 @@ func TestBlockReward(t *testing.T) {
sum := types.NewInt(0) sum := types.NewInt(0)
N := build.HalvingPeriodBlocks N := build.HalvingPeriodBlocks
for i := 0; i < N; i++ { for i := 0; i < N; i++ {
if i%20000 == 0 {
fmt.Fprintf(os.Stderr, "%.1f%%\r", float64(i)/float64(N)*100)
}
a := MiningReward(coffer) a := MiningReward(coffer)
sum = types.BigAdd(sum, a) sum = types.BigAdd(sum, a)
coffer = types.BigSub(coffer, 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("Total reward: %d\n", build.MiningRewardTotal)
fmt.Printf("Remaining: %s\n", types.BigDiv(coffer, types.NewInt(build.FilecoinPrecision))) 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))) 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)))
} }