From 47d12417b3475c2000b03af759911d5191d56d97 Mon Sep 17 00:00:00 2001 From: whyrusleeping Date: Fri, 20 Sep 2019 17:22:28 -0700 Subject: [PATCH 1/8] working on correct block reward --- build/params.go | 6 ++++++ chain/gen/mining.go | 2 +- chain/sync.go | 2 +- chain/vm/vm.go | 19 +++++++++++++++++-- chain/vm/vm_test.go | 22 ++++++++++++++++++++++ 5 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 chain/vm/vm_test.go diff --git a/build/params.go b/build/params.go index b969975a6..d165fa6c6 100644 --- a/build/params.go +++ b/build/params.go @@ -28,6 +28,12 @@ const PerCapitaCollateralProportion = 5 const CollateralPrecision = 100 const TotalFilecoin = 2000000000 +const MiningRewardTotal = 1400000000 const FilecoinPrecision = 1000000000000000000 +// six years +const HalvingPeriodBlocks = 6 * 365 * 24 * 60 * 2 + +const AdjustmentPeriod = 7 * 24 * 60 * 2 + // TODO: Move other important consts here diff --git a/chain/gen/mining.go b/chain/gen/mining.go index af0adccb9..fb35cce4f 100644 --- a/chain/gen/mining.go +++ b/chain/gen/mining.go @@ -44,7 +44,7 @@ func MinerCreateBlock(ctx context.Context, sm *stmgr.StateManager, w *wallet.Wal } // apply miner reward - if err := vmi.TransferFunds(actors.NetworkAddress, owner, vm.MiningRewardForBlock(parents)); err != nil { + if err := vmi.TransferFunds(actors.NetworkAddress, owner, vm.MiningRewardForBlock(height)); err != nil { return nil, err } diff --git a/chain/sync.go b/chain/sync.go index 02a588465..a7eae3c3b 100644 --- a/chain/sync.go +++ b/chain/sync.go @@ -442,7 +442,7 @@ func (syncer *Syncer) ValidateBlock(ctx context.Context, b *types.FullBlock) err return xerrors.Errorf("getting miner owner for block miner failed: %w", err) } - if err := vmi.TransferFunds(actors.NetworkAddress, owner, vm.MiningRewardForBlock(baseTs)); err != nil { + if err := vmi.TransferFunds(actors.NetworkAddress, owner, vm.MiningRewardForBlock(h.Height)); err != nil { return xerrors.Errorf("fund transfer failed: %w", err) } diff --git a/chain/vm/vm.go b/chain/vm/vm.go index 33936a131..d35c5158e 100644 --- a/chain/vm/vm.go +++ b/chain/vm/vm.go @@ -3,7 +3,10 @@ package vm import ( "context" "fmt" + "math" + "math/big" + "github.com/filecoin-project/go-lotus/build" "github.com/filecoin-project/go-lotus/chain/actors" "github.com/filecoin-project/go-lotus/chain/actors/aerrors" "github.com/filecoin-project/go-lotus/chain/address" @@ -631,6 +634,18 @@ func DepositFunds(act *types.Actor, amt types.BigInt) { act.Balance = types.BigAdd(act.Balance, amt) } -func MiningRewardForBlock(base *types.TipSet) types.BigInt { - return types.NewInt(10000) +func MiningRewardForBlock(height uint64) types.BigInt { + + //decay := e^(ln(0.5) / (HalvingPeriodBlocks / AdjustmentPeriod) + decay := 0.9977869135615522 + + 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)) + + reward, _ := iv.Mul(iv, dval).Int(nil) + + return types.BigInt{reward} } diff --git a/chain/vm/vm_test.go b/chain/vm/vm_test.go new file mode 100644 index 000000000..0cf9e53e0 --- /dev/null +++ b/chain/vm/vm_test.go @@ -0,0 +1,22 @@ +package vm + +import ( + "fmt" + "testing" + + "github.com/filecoin-project/go-lotus/build" + "github.com/filecoin-project/go-lotus/chain/types" +) + +func TestBlockReward(t *testing.T) { + sum := types.NewInt(0) + for i := 0; i < 500000000; i += 60 { + sum = types.BigAdd(sum, MiningRewardForBlock(uint64(i))) + } + + sum = types.BigMul(sum, types.NewInt(60)) + + fmt.Println(sum) + + fmt.Println(types.BigDiv(sum, types.NewInt(build.FilecoinPrecision))) +} From 2f35a23f0f1fca4accd03b6f943a2026c09c051f Mon Sep 17 00:00:00 2001 From: whyrusleeping Date: Mon, 23 Sep 2019 21:15:07 -0700 Subject: [PATCH 2/8] fix block reward math --- chain/vm/vm.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/chain/vm/vm.go b/chain/vm/vm.go index d35c5158e..791496678 100644 --- a/chain/vm/vm.go +++ b/chain/vm/vm.go @@ -637,15 +637,20 @@ func DepositFunds(act *types.Actor, amt types.BigInt) { func MiningRewardForBlock(height uint64) types.BigInt { //decay := e^(ln(0.5) / (HalvingPeriodBlocks / AdjustmentPeriod) - decay := 0.9977869135615522 + 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)) + iv := big.NewFloat(0).Mul( + big.NewFloat(0).SetInt(totalMiningReward.Int), + big.NewFloat(1-decay), + ) - reward, _ := iv.Mul(iv, dval).Int(nil) + //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} } From ef5e7674db80f19f2662817abb86ce7e36ce6311 Mon Sep 17 00:00:00 2001 From: Jakub Sztandera Date: Fri, 27 Sep 2019 00:33:38 +0200 Subject: [PATCH 3/8] Use incremental alg for rewards License: MIT Signed-off-by: Jakub Sztandera --- build/mining_reward_test.go | 12 ++++++++++++ build/params.go | 3 +++ chain/gen/mining.go | 6 +++++- chain/sync.go | 7 ++++++- chain/vm/vm.go | 14 +++++++++++++- chain/vm/vm_test.go | 35 ++++++++++++++++++++++++++++++----- 6 files changed, 69 insertions(+), 8 deletions(-) create mode 100644 build/mining_reward_test.go diff --git a/build/mining_reward_test.go b/build/mining_reward_test.go new file mode 100644 index 000000000..9629623ed --- /dev/null +++ b/build/mining_reward_test.go @@ -0,0 +1,12 @@ +package build + +import ( + "math/big" + "testing" +) + +func TestEncodeMiningRewardInitial(t *testing.T) { + i := &big.Int{} + i.SetString("153686558225539943338", 10) + t.Logf("%#v", i.Bytes()) +} diff --git a/build/params.go b/build/params.go index d165fa6c6..0ee462a68 100644 --- a/build/params.go +++ b/build/params.go @@ -29,6 +29,9 @@ const CollateralPrecision = 100 const TotalFilecoin = 2000000000 const MiningRewardTotal = 1400000000 + +var MiningRewardInitialAttoFilBytes = []byte{0x8, 0x54, 0xd4, 0x56, 0x70, 0x99, 0xb2, 0x4b, 0xaa} + const FilecoinPrecision = 1000000000000000000 // six years diff --git a/chain/gen/mining.go b/chain/gen/mining.go index fb35cce4f..a691c5db5 100644 --- a/chain/gen/mining.go +++ b/chain/gen/mining.go @@ -42,9 +42,13 @@ func MinerCreateBlock(ctx context.Context, sm *stmgr.StateManager, w *wallet.Wal if err != nil { return nil, xerrors.Errorf("failed to get miner worker: %w", err) } + networkBalance, err := vmi.ActorBalance(actors.NetworkAddress) + if err != nil { + return nil, xerrors.Errorf("failed to get network balance: %w", err) + } // apply miner reward - if err := vmi.TransferFunds(actors.NetworkAddress, owner, vm.MiningRewardForBlock(height)); err != nil { + if err := vmi.TransferFunds(actors.NetworkAddress, owner, vm.MiningReward(networkBalance)); err != nil { return nil, err } diff --git a/chain/sync.go b/chain/sync.go index a7eae3c3b..2cf6239fa 100644 --- a/chain/sync.go +++ b/chain/sync.go @@ -441,8 +441,13 @@ func (syncer *Syncer) ValidateBlock(ctx context.Context, b *types.FullBlock) err if err != nil { return xerrors.Errorf("getting miner owner for block miner failed: %w", err) } + networkBalance, err := vmi.ActorBalance(actors.NetworkAddress) + if err != nil { + return xerrors.Errorf("getting network balance") + } - if err := vmi.TransferFunds(actors.NetworkAddress, owner, vm.MiningRewardForBlock(h.Height)); err != nil { + if err := vmi.TransferFunds(actors.NetworkAddress, owner, + vm.MiningReward(networkBalance)); err != nil { return xerrors.Errorf("fund transfer failed: %w", err) } diff --git a/chain/vm/vm.go b/chain/vm/vm.go index 791496678..3cd88a2c9 100644 --- a/chain/vm/vm.go +++ b/chain/vm/vm.go @@ -3,7 +3,6 @@ package vm import ( "context" "fmt" - "math" "math/big" "github.com/filecoin-project/go-lotus/build" @@ -634,6 +633,18 @@ func DepositFunds(act *types.Actor, amt types.BigInt) { act.Balance = types.BigAdd(act.Balance, amt) } +// 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 +} + +/* func MiningRewardForBlock(height uint64) types.BigInt { //decay := e^(ln(0.5) / (HalvingPeriodBlocks / AdjustmentPeriod) @@ -654,3 +665,4 @@ func MiningRewardForBlock(height uint64) types.BigInt { return types.BigInt{reward} } +*/ diff --git a/chain/vm/vm_test.go b/chain/vm/vm_test.go index 0cf9e53e0..b1fcc34c4 100644 --- a/chain/vm/vm_test.go +++ b/chain/vm/vm_test.go @@ -2,6 +2,7 @@ package vm import ( "fmt" + "os" "testing" "github.com/filecoin-project/go-lotus/build" @@ -9,14 +10,38 @@ import ( ) func TestBlockReward(t *testing.T) { + coffer := types.FromFil(build.MiningRewardTotal) sum := types.NewInt(0) - for i := 0; i < 500000000; i += 60 { - sum = types.BigAdd(sum, MiningRewardForBlock(uint64(i))) + 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) } - sum = types.BigMul(sum, types.NewInt(60)) + //sum = types.BigMul(sum, types.NewInt(60)) - fmt.Println(sum) + fmt.Println("After a 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))) - fmt.Println(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))) } From 639139795b33369154e540a0e6ba552619790f8a Mon Sep 17 00:00:00 2001 From: whyrusleeping Date: Thu, 26 Sep 2019 16:09:10 -0700 Subject: [PATCH 4/8] 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))) } From bd4924aa5671b94887cdcc58f8365f68e37b2575 Mon Sep 17 00:00:00 2001 From: Jakub Sztandera Date: Fri, 27 Sep 2019 01:32:31 +0200 Subject: [PATCH 5/8] Update to better const without AdustmentPeriod License: MIT Signed-off-by: Jakub Sztandera --- build/mining_reward_test.go | 2 +- build/params.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/build/mining_reward_test.go b/build/mining_reward_test.go index 9629623ed..e6baa46a5 100644 --- a/build/mining_reward_test.go +++ b/build/mining_reward_test.go @@ -7,6 +7,6 @@ import ( func TestEncodeMiningRewardInitial(t *testing.T) { i := &big.Int{} - i.SetString("153686558225539943338", 10) + i.SetString("153856870367821447423", 10) t.Logf("%#v", i.Bytes()) } diff --git a/build/params.go b/build/params.go index 0ee462a68..2d7e95b29 100644 --- a/build/params.go +++ b/build/params.go @@ -30,7 +30,7 @@ const CollateralPrecision = 100 const TotalFilecoin = 2000000000 const MiningRewardTotal = 1400000000 -var MiningRewardInitialAttoFilBytes = []byte{0x8, 0x54, 0xd4, 0x56, 0x70, 0x99, 0xb2, 0x4b, 0xaa} +var MiningRewardInitialAttoFilBytes = []byte{0x8, 0x57, 0x31, 0x68, 0x6e, 0x4f, 0x52, 0x40, 0xff} const FilecoinPrecision = 1000000000000000000 From b611637652ab7a1fa01c06d51513e4572379c6d6 Mon Sep 17 00:00:00 2001 From: Whyrusleeping Date: Fri, 27 Sep 2019 13:47:10 -0700 Subject: [PATCH 6/8] Apply suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Ɓukasz Magiera --- build/params.go | 2 ++ chain/vm/vm.go | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/build/params.go b/build/params.go index 2d7e95b29..89aca1d57 100644 --- a/build/params.go +++ b/build/params.go @@ -35,8 +35,10 @@ var MiningRewardInitialAttoFilBytes = []byte{0x8, 0x57, 0x31, 0x68, 0x6e, 0x4f, const FilecoinPrecision = 1000000000000000000 // six years +// Blocks const HalvingPeriodBlocks = 6 * 365 * 24 * 60 * 2 +// Blocks const AdjustmentPeriod = 7 * 24 * 60 * 2 // TODO: Move other important consts here diff --git a/chain/vm/vm.go b/chain/vm/vm.go index 6616e8312..8b9e8568b 100644 --- a/chain/vm/vm.go +++ b/chain/vm/vm.go @@ -633,12 +633,12 @@ 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 initialReward = 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 { +func MiningReward(remainingReward types.BigInt) types.BigInt { ci := big.NewInt(0).Set(coffer.Int) res := ci.Mul(ci, IV.Int) res = res.Div(res, miningRewardTotal.Int) From b90f45fa60d47f194f89d860dd93e4dcbb0518a4 Mon Sep 17 00:00:00 2001 From: Jakub Sztandera Date: Fri, 27 Sep 2019 23:42:12 +0200 Subject: [PATCH 7/8] Change build params License: MIT Signed-off-by: Jakub Sztandera --- build/mining_reward_test.go | 12 ------------ build/params.go | 17 ++++++++++++++++- chain/vm/vm.go | 5 ++--- chain/vm/vm_test.go | 15 ++++++++------- 4 files changed, 26 insertions(+), 23 deletions(-) delete mode 100644 build/mining_reward_test.go diff --git a/build/mining_reward_test.go b/build/mining_reward_test.go deleted file mode 100644 index e6baa46a5..000000000 --- a/build/mining_reward_test.go +++ /dev/null @@ -1,12 +0,0 @@ -package build - -import ( - "math/big" - "testing" -) - -func TestEncodeMiningRewardInitial(t *testing.T) { - i := &big.Int{} - i.SetString("153856870367821447423", 10) - t.Logf("%#v", i.Bytes()) -} diff --git a/build/params.go b/build/params.go index 89aca1d57..8440ed73d 100644 --- a/build/params.go +++ b/build/params.go @@ -1,5 +1,7 @@ package build +import "math/big" + // Core network constants const UnixfsChunkSize uint64 = 1 << 20 @@ -30,7 +32,9 @@ const CollateralPrecision = 100 const TotalFilecoin = 2000000000 const MiningRewardTotal = 1400000000 -var MiningRewardInitialAttoFilBytes = []byte{0x8, 0x57, 0x31, 0x68, 0x6e, 0x4f, 0x52, 0x40, 0xff} +const MiningRewardInitialAttoFilString = "153856870367821447423" + +var MiningRewardInitialAttoFil *big.Int const FilecoinPrecision = 1000000000000000000 @@ -42,3 +46,14 @@ const HalvingPeriodBlocks = 6 * 365 * 24 * 60 * 2 const AdjustmentPeriod = 7 * 24 * 60 * 2 // TODO: Move other important consts here + +func init() { + MiningRewardInitialAttoFil = new(big.Int) + + var ok bool + MiningRewardInitialAttoFil, ok = MiningRewardInitialAttoFil. + SetString(MiningRewardInitialAttoFilString, 10) + if !ok { + panic("could not parse MiningRewardInitialAttoFilString") + } +} diff --git a/chain/vm/vm.go b/chain/vm/vm.go index 8b9e8568b..a9ab6aaaa 100644 --- a/chain/vm/vm.go +++ b/chain/vm/vm.go @@ -633,14 +633,13 @@ func DepositFunds(act *types.Actor, amt types.BigInt) { act.Balance = types.BigAdd(act.Balance, amt) } -var initialReward = 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(remainingReward types.BigInt) types.BigInt { - ci := big.NewInt(0).Set(coffer.Int) - res := ci.Mul(ci, IV.Int) + ci := big.NewInt(0).Set(remainingReward.Int) + res := ci.Mul(ci, build.MiningRewardInitialAttoFil) res = res.Div(res, miningRewardTotal.Int) return types.BigInt{res} } diff --git a/chain/vm/vm_test.go b/chain/vm/vm_test.go index 1c31bc729..6c8c09600 100644 --- a/chain/vm/vm_test.go +++ b/chain/vm/vm_test.go @@ -2,6 +2,7 @@ package vm import ( "fmt" + "math/big" "testing" "github.com/filecoin-project/go-lotus/build" @@ -9,19 +10,19 @@ import ( ) func TestBlockReward(t *testing.T) { - coffer := types.FromFil(build.MiningRewardTotal) - sum := types.NewInt(0) + coffer := types.FromFil(build.MiningRewardTotal).Int + sum := new(big.Int) N := build.HalvingPeriodBlocks for i := 0; i < N; i++ { - a := MiningReward(coffer) - sum = types.BigAdd(sum, a) - coffer = types.BigSub(coffer, a) + a := MiningReward(types.BigInt{coffer}) + sum = sum.Add(sum, a.Int) + coffer = coffer.Sub(coffer, a.Int) } //sum = types.BigMul(sum, types.NewInt(60)) fmt.Println("After a 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))) + fmt.Printf("Remaining: %s\n", types.BigDiv(types.BigInt{coffer}, types.NewInt(build.FilecoinPrecision))) + fmt.Printf("Given out: %s\n", types.BigDiv(types.BigInt{sum}, types.NewInt(build.FilecoinPrecision))) } From 76ab2e8a7b4ed6af5535c85b99ae0ea6ffa62e3e Mon Sep 17 00:00:00 2001 From: Jakub Sztandera Date: Sat, 28 Sep 2019 00:02:02 +0200 Subject: [PATCH 8/8] De-javify License: MIT Signed-off-by: Jakub Sztandera --- build/params.go | 12 ++++++------ chain/vm/vm.go | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/build/params.go b/build/params.go index 8440ed73d..1a221575a 100644 --- a/build/params.go +++ b/build/params.go @@ -32,9 +32,9 @@ const CollateralPrecision = 100 const TotalFilecoin = 2000000000 const MiningRewardTotal = 1400000000 -const MiningRewardInitialAttoFilString = "153856870367821447423" +const InitialRewardStr = "153856870367821447423" -var MiningRewardInitialAttoFil *big.Int +var InitialReward *big.Int const FilecoinPrecision = 1000000000000000000 @@ -48,12 +48,12 @@ const AdjustmentPeriod = 7 * 24 * 60 * 2 // TODO: Move other important consts here func init() { - MiningRewardInitialAttoFil = new(big.Int) + InitialReward = new(big.Int) var ok bool - MiningRewardInitialAttoFil, ok = MiningRewardInitialAttoFil. - SetString(MiningRewardInitialAttoFilString, 10) + InitialReward, ok = InitialReward. + SetString(InitialRewardStr, 10) if !ok { - panic("could not parse MiningRewardInitialAttoFilString") + panic("could not parse InitialRewardStr") } } diff --git a/chain/vm/vm.go b/chain/vm/vm.go index a9ab6aaaa..c88806612 100644 --- a/chain/vm/vm.go +++ b/chain/vm/vm.go @@ -639,7 +639,7 @@ var miningRewardTotal = types.FromFil(build.MiningRewardTotal) // coffer is amount of FIL in NetworkAddress func MiningReward(remainingReward types.BigInt) types.BigInt { ci := big.NewInt(0).Set(remainingReward.Int) - res := ci.Mul(ci, build.MiningRewardInitialAttoFil) + res := ci.Mul(ci, build.InitialReward) res = res.Div(res, miningRewardTotal.Int) return types.BigInt{res} }