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() + } +}