Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Classic Rewards and Uncle Rewards #404

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion config.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@
"keepTxFees": false,
"interval": "10m",
"daemon": "http://127.0.0.1:8545",
"timeout": "10s"
"timeout": "10s",
"classic": true
},

"payouts": {
Expand Down
47 changes: 30 additions & 17 deletions payouts/unlocker.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ type UnlockerConfig struct {
Interval string `json:"interval"`
Daemon string `json:"daemon"`
Timeout string `json:"timeout"`
Classic bool `json:"classic"`
}

const minDepth = 16
const byzantiumHardForkHeight = 4370000

var homesteadReward = math.MustParseBig256("5000000000000000000")
var byzantiumReward = math.MustParseBig256("3000000000000000000")
var classicReward = math.MustParseBig256("4000000000000000000")

// Donate 10% from pool fees to developers
const donationFee = 10.0
Expand Down Expand Up @@ -159,7 +161,7 @@ func (u *BlockUnlocker) unlockCandidates(candidates []*storage.BlockData) (*Unlo
orphan = false
result.uncles++

err := handleUncle(height, uncle, candidate)
err := handleUncle(height, uncle, candidate, u.config.Classic)
if err != nil {
u.halt = true
u.lastFail = err
Expand Down Expand Up @@ -209,7 +211,7 @@ func (u *BlockUnlocker) handleBlock(block *rpc.GetBlockReply, candidate *storage
return err
}
candidate.Height = correctHeight
reward := getConstReward(candidate.Height)
reward := getConstReward(candidate.Height, u.config.Classic)

// Add TX fees
extraTxReward, err := u.getExtraRewardForTx(block)
Expand All @@ -223,7 +225,7 @@ func (u *BlockUnlocker) handleBlock(block *rpc.GetBlockReply, candidate *storage
}

// Add reward for including uncles
uncleReward := getRewardForUncle(candidate.Height)
uncleReward := getRewardForUncle(candidate.Height, u.config.Classic)
rewardForUncles := big.NewInt(0).Mul(uncleReward, big.NewInt(int64(len(block.Uncles))))
reward.Add(reward, rewardForUncles)

Expand All @@ -233,12 +235,12 @@ func (u *BlockUnlocker) handleBlock(block *rpc.GetBlockReply, candidate *storage
return nil
}

func handleUncle(height int64, uncle *rpc.GetBlockReply, candidate *storage.BlockData) error {
func handleUncle(height int64, uncle *rpc.GetBlockReply, candidate *storage.BlockData, isClassic bool) error {
uncleHeight, err := strconv.ParseInt(strings.Replace(uncle.Number, "0x", "", -1), 16, 64)
if err != nil {
return err
}
reward := getUncleReward(uncleHeight, height)
reward := getUncleReward(uncleHeight, height, isClassic)
candidate.Height = height
candidate.UncleHeight = uncleHeight
candidate.Orphan = false
Expand Down Expand Up @@ -501,24 +503,35 @@ func weiToShannonInt64(wei *big.Rat) int64 {
return value
}

func getConstReward(height int64) *big.Int {
if height >= byzantiumHardForkHeight {
return new(big.Int).Set(byzantiumReward)
func getConstReward(height int64, isClassic bool) *big.Int {
if !isClassic {
if height >= byzantiumHardForkHeight {
return new(big.Int).Set(byzantiumReward)
}
return new(big.Int).Set(homesteadReward)
} else {
return new(big.Int).Set(classicReward)
}
return new(big.Int).Set(homesteadReward)
}

func getRewardForUncle(height int64) *big.Int {
reward := getConstReward(height)
func getRewardForUncle(height int64, isClassic bool) *big.Int {
reward := getConstReward(height, isClassic)
return new(big.Int).Div(reward, new(big.Int).SetInt64(32))
}

func getUncleReward(uHeight, height int64) *big.Int {
reward := getConstReward(height)
k := height - uHeight
reward.Mul(big.NewInt(8-k), reward)
reward.Div(reward, big.NewInt(8))
return reward
func getUncleReward(uHeight, height int64, isClassic bool) *big.Int {
if !isClassic {
reward := getConstReward(height, isClassic)
k := height - uHeight
reward.Mul(big.NewInt(8-k), reward)
reward.Div(reward, big.NewInt(8))
return reward
} else {
reward := getConstReward(height, isClassic)
reward.Mul(reward, big.NewInt(3125))
reward.Div(reward, big.NewInt(100000))
return reward
}
}

func (u *BlockUnlocker) getExtraRewardForTx(block *rpc.GetBlockReply) (*big.Int, error) {
Expand Down
23 changes: 18 additions & 5 deletions payouts/unlocker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,20 @@ func TestWeiToShannonInt64(t *testing.T) {
t.Error("Must charge original value")
}
}

func TestGetClassicUncleReward(t *testing.T) {
rewards := make(map[int64]string)
expectedRewards := map[int64]string{
1: "125000000000000000",
}
for i := int64(1); i < 2; i++ {
rewards[i] = getUncleReward(1, i+1, true).String()
}
for i, reward := range rewards {
if expectedRewards[i] != rewards[i] {
t.Errorf("Incorrect uncle reward for %v, expected %v vs %v", i, expectedRewards[i], reward)
}
}
}
func TestGetUncleReward(t *testing.T) {
rewards := make(map[int64]string)
expectedRewards := map[int64]string{
Expand All @@ -78,7 +91,7 @@ func TestGetUncleReward(t *testing.T) {
7: "625000000000000000",
}
for i := int64(1); i < 8; i++ {
rewards[i] = getUncleReward(1, i+1).String()
rewards[i] = getUncleReward(1, i+1, false).String()
}
for i, reward := range rewards {
if expectedRewards[i] != rewards[i] {
Expand All @@ -99,7 +112,7 @@ func TestGetByzantiumUncleReward(t *testing.T) {
7: "375000000000000000",
}
for i := int64(1); i < 8; i++ {
rewards[i] = getUncleReward(byzantiumHardForkHeight, byzantiumHardForkHeight+i).String()
rewards[i] = getUncleReward(byzantiumHardForkHeight, byzantiumHardForkHeight+i, false).String()
}
for i, reward := range rewards {
if expectedRewards[i] != rewards[i] {
Expand All @@ -109,15 +122,15 @@ func TestGetByzantiumUncleReward(t *testing.T) {
}

func TestGetRewardForUngle(t *testing.T) {
reward := getRewardForUncle(1).String()
reward := getRewardForUncle(1, false).String()
expectedReward := "156250000000000000"
if expectedReward != reward {
t.Errorf("Incorrect uncle bonus for height %v, expected %v vs %v", 1, expectedReward, reward)
}
}

func TestGetByzantiumRewardForUngle(t *testing.T) {
reward := getRewardForUncle(byzantiumHardForkHeight).String()
reward := getRewardForUncle(byzantiumHardForkHeight, false).String()
expectedReward := "93750000000000000"
if expectedReward != reward {
t.Errorf("Incorrect uncle bonus for height %v, expected %v vs %v", byzantiumHardForkHeight, expectedReward, reward)
Expand Down