Skip to content

Commit

Permalink
refactor uncle rewards
Browse files Browse the repository at this point in the history
  • Loading branch information
iquidus committed Nov 28, 2020
1 parent 1f7b737 commit 28da721
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 16 deletions.
10 changes: 4 additions & 6 deletions payouts/unlocker.go
Expand Up @@ -230,9 +230,7 @@ func (u *BlockUnlocker) handleBlock(block *rpc.GetBlockReply, candidate *storage
reward := getConstReward(era)

// Add reward for including uncles
var bigHeight = new(big.Int).SetInt64(candidate.Height)

uncleReward := getRewardForUncle(bigHeight, reward)
uncleReward := getRewardForUncle(reward)
rewardForUncles := big.NewInt(0).Mul(uncleReward, big.NewInt(int64(len(block.Uncles))))
reward.Add(reward, rewardForUncles)

Expand Down Expand Up @@ -559,8 +557,8 @@ func getConstReward(era *big.Int) *big.Int {
return wr
}

func getRewardForUncle(era *big.Int, blockReward *big.Int) *big.Int {
return new(big.Int).Div(GetBlockWinnerRewardByEra(era, blockReward), big32) //return new(big.Int).Div(reward, new(big.Int).SetInt64(32))
func getRewardForUncle(blockReward *big.Int) *big.Int {
return new(big.Int).Div(blockReward, big32) //return new(big.Int).Div(reward, new(big.Int).SetInt64(32))
}

func getUncleReward(uHeight *big.Int, height *big.Int, era *big.Int, reward *big.Int) *big.Int {
Expand All @@ -575,7 +573,7 @@ func getUncleReward(uHeight *big.Int, height *big.Int, era *big.Int, reward *big

return r
}
return getRewardForUncle(era, reward)
return getRewardForUncle(reward)
}

func (u *BlockUnlocker) getExtraRewardForTx(block *rpc.GetBlockReply) (*big.Int, error) {
Expand Down
18 changes: 8 additions & 10 deletions payouts/unlocker_test.go
Expand Up @@ -116,25 +116,23 @@ func TestGetBlockWinnerRewardByEra(t *testing.T) {
}

func TestGetRewardForUncle(t *testing.T) {
// skip era 0 as it's handled by getUncleReward
era := big.NewInt(1)
baseReward := big.NewInt(5000000000000000000)
uncleReward := getRewardForUncle(era, baseReward)
baseReward := big.NewInt(4000000000000000000)
uncleReward := getRewardForUncle(baseReward)
if uncleReward.Cmp(big.NewInt(125000000000000000)) != 0 {
t.Error("Should return uncleReward 125000000000000000", "reward", uncleReward)
}
era = big.NewInt(2)
uncleReward = getRewardForUncle(era, baseReward)
baseReward = big.NewInt(3200000000000000000)
uncleReward = getRewardForUncle(baseReward)
if uncleReward.Cmp(big.NewInt(100000000000000000)) != 0 {
t.Error("Should return uncleReward 100000000000000000", "reward", uncleReward)
}
era = big.NewInt(3)
uncleReward = getRewardForUncle(era, baseReward)
baseReward = big.NewInt(2560000000000000000)
uncleReward = getRewardForUncle(baseReward)
if uncleReward.Cmp(big.NewInt(80000000000000000)) != 0 {
t.Error("Should return uncleReward 80000000000000000", "reward", uncleReward)
}
era = big.NewInt(4)
uncleReward = getRewardForUncle(era, baseReward)
baseReward = big.NewInt(2048000000000000000)
uncleReward = getRewardForUncle(baseReward)
if uncleReward.Cmp(big.NewInt(64000000000000000)) != 0 {
t.Error("Should return uncleReward 64000000000000000", "reward", uncleReward)
}
Expand Down

0 comments on commit 28da721

Please sign in to comment.