From 3c926a6758998203ece298d0381001ebe2eda6f2 Mon Sep 17 00:00:00 2001 From: David Julien Date: Wed, 26 Jul 2017 17:08:39 -0400 Subject: [PATCH] fixed CurrentBlockReward bug and created current block reward test --- consensus/current.go | 5 +++-- consensus/current_test.go | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/consensus/current.go b/consensus/current.go index 76a788f..2a108ee 100644 --- a/consensus/current.go +++ b/consensus/current.go @@ -1,6 +1,7 @@ package consensus import ( + "math" "math/big" "github.com/ubclaunchpad/cumulus/blockchain" @@ -24,8 +25,8 @@ var ( // CurrentBlockReward determines the current block reward using the // the length of the blockchain func CurrentBlockReward(bc *blockchain.BlockChain) uint64 { - return StartingBlockReward / - uint64(((len(bc.Blocks) / blockRewardHalvingRate) + 1)) + timesHalved := float64((len(bc.Blocks) / blockRewardHalvingRate)) + return StartingBlockReward / uint64(math.Pow(float64(2), timesHalved)) } // CurrentTarget returns the current target based on the CurrentDifficulty diff --git a/consensus/current_test.go b/consensus/current_test.go index efb9de6..4147ef9 100644 --- a/consensus/current_test.go +++ b/consensus/current_test.go @@ -12,3 +12,22 @@ func TestCurrentTarget(t *testing.T) { t.Fail() } } + +func TestCurrentBlockReward(t *testing.T) { + bc, _ := blockchain.NewValidBlockChainFixture() + if CurrentBlockReward(bc) != StartingBlockReward { + t.Fail() + } + for i := len(bc.Blocks); i < blockRewardHalvingRate; i++ { + bc.AppendBlock(new(blockchain.Block)) + } + if CurrentBlockReward(bc) != StartingBlockReward/2 { + t.Fail() + } + for i := 0; i < blockRewardHalvingRate; i++ { + bc.AppendBlock(new(blockchain.Block)) + } + if CurrentBlockReward(bc) != StartingBlockReward/4 { + t.Fail() + } +}