Skip to content

Commit

Permalink
fixed CurrentBlockReward bug and created current block reward test
Browse files Browse the repository at this point in the history
  • Loading branch information
david-julien committed Jul 26, 2017
1 parent a5f6df4 commit 3c926a6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
5 changes: 3 additions & 2 deletions consensus/current.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package consensus

import (
"math"
"math/big"

"github.com/ubclaunchpad/cumulus/blockchain"
Expand All @@ -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
Expand Down
19 changes: 19 additions & 0 deletions consensus/current_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}

0 comments on commit 3c926a6

Please sign in to comment.