From 9362b303aba74c59aa9d5db7db47498a4666aac1 Mon Sep 17 00:00:00 2001 From: yuriy0803 <68668177+yuriy0803@users.noreply.github.com> Date: Tue, 23 Feb 2021 03:59:53 +0100 Subject: [PATCH] Added Classic Rewards and Uncle Rewards + config.json --- configs/api.json | 5 ++-- configs/payout.json | 11 +++---- configs/stratum2b.json | 5 ++-- configs/stratum4b.json | 11 +++---- configs/stratum8b.json | 13 +++++---- configs/stratum9b.json | 13 +++++---- configs/unlocker.json | 11 +++---- payouts/unlocker.go | 62 ++++++++++++++++++++++++++++------------ payouts/unlocker_test.go | 59 +++++++++++++++++++++++++++++++++++--- 9 files changed, 137 insertions(+), 53 deletions(-) diff --git a/configs/api.json b/configs/api.json index 2dc93e4..e869d31 100644 --- a/configs/api.json +++ b/configs/api.json @@ -65,7 +65,7 @@ "upstream": [ { "name": "main", - "url": "http://40.121.105.44:8501", + "url": "http://127.0.0.1:8545", "timeout": "10s" }, { @@ -92,7 +92,8 @@ "keepTxFees": false, "interval": "10m", "daemon": "http://127.0.0.1:8545", - "timeout": "10s" + "timeout": "10s", + "classic": true }, "payouts": { diff --git a/configs/payout.json b/configs/payout.json index 96dfaed..b1b9aa3 100644 --- a/configs/payout.json +++ b/configs/payout.json @@ -65,7 +65,7 @@ "upstream": [ { "name": "main", - "url": "http://40.121.105.44:8501", + "url": "http://127.0.0.1:8545", "timeout": "10s" }, { @@ -91,17 +91,18 @@ "immatureDepth": 20, "keepTxFees": false, "interval": "10m", - "daemon": "http://127.0.0.1:8591", - "timeout": "10s" + "daemon": "http://127.0.0.1:8545", + "timeout": "10s", + "classic": true }, "payouts": { "enabled": true, "requirePeers": 4, "interval": "1h", - "daemon": "http://127.0.0.1:8501", + "daemon": "http://127.0.0.1:8545", "timeout": "120s", - "address": "0x3f156afdb248618892cb5089ba5a5fcac8ee0b01", + "address": "0xd92fa5a9732a0aec36dc8d5a6a1305dc2d3e09e6", "autoGas": true, "gas": "21000", "gasPrice": "50000000000", diff --git a/configs/stratum2b.json b/configs/stratum2b.json index c709b8d..a97ec01 100644 --- a/configs/stratum2b.json +++ b/configs/stratum2b.json @@ -19,7 +19,7 @@ "blockRefreshInterval": "120ms", "stateUpdateInterval": "3s", "difficulty": 2000000000, - "stratumHostname": "perkle-pool.esprezzo.io", + "stratumHostname": "Domain name", "healthCheck": true, "maxFails": 100, "hashrateExpiration": "3h", @@ -66,7 +66,7 @@ "upstream": [ { "name": "backup", - "url": "http://127.0.0.1:8501", + "url": "http://127.0.0.1:8545", "timeout": "10s" } ], @@ -89,6 +89,7 @@ "interval": "10m", "daemon": "http://127.0.0.1:8545", "timeout": "10s" + "classic": true }, "payouts": { diff --git a/configs/stratum4b.json b/configs/stratum4b.json index cfe28ae..cd62eee 100644 --- a/configs/stratum4b.json +++ b/configs/stratum4b.json @@ -19,7 +19,7 @@ "blockRefreshInterval": "120ms", "stateUpdateInterval": "3s", "difficulty": 4000000000, - "stratumHostname": "perkle-pool.esprezzo.io", + "stratumHostname": "Domain name", "healthCheck": true, "maxFails": 100, "hashrateExpiration": "3h", @@ -66,7 +66,7 @@ "upstream": [ { "name": "main", - "url": "http://40.121.105.44:8501", + "url": "http://127.0.0.1:8545", "timeout": "10s" }, { @@ -92,15 +92,16 @@ "immatureDepth": 20, "keepTxFees": false, "interval": "10m", - "daemon": "http://127.0.0.1:8501", - "timeout": "10s" + "daemon": "http://127.0.0.1:8545", + "timeout": "10s", + "classic": true }, "payouts": { "enabled": false, "requirePeers": 5, "interval": "3h", - "daemon": "http://127.0.0.1:8501", + "daemon": "http://127.0.0.1:8545", "timeout": "10s", "address": "0x", "autoGas": true, diff --git a/configs/stratum8b.json b/configs/stratum8b.json index 20768b7..d6cc342 100644 --- a/configs/stratum8b.json +++ b/configs/stratum8b.json @@ -19,7 +19,7 @@ "blockRefreshInterval": "120ms", "stateUpdateInterval": "3s", "difficulty": 8000000000, - "stratumHostname": "perkle-pool.esprezzo.io", + "stratumHostname": "Domain name", "healthCheck": true, "maxFails": 100, "hashrateExpiration": "3h", @@ -66,12 +66,12 @@ "upstream": [ { "name": "main", - "url": "http://40.121.105.44:8501", + "url": "http://127.0.0.1:8545", "timeout": "10s" }, { "name": "backup", - "url": "http://127.0.0.2:8501", + "url": "http://127.0.0.2:8545", "timeout": "10s" } ], @@ -86,14 +86,15 @@ "unlocker": { "enabled": false, "poolFee": 0.5, - "poolFeeAddress": "0x0f31986d7a0d4f160acd97583e3c3b591dcb5dde", + "poolFeeAddress": "0xd92fa5a9732a0aec36dc8d5a6a1305dc2d3e09e6", "donate": true, "depth": 120, "immatureDepth": 20, "keepTxFees": false, "interval": "10m", - "daemon": "http://127.0.0.1:8501", - "timeout": "10s" + "daemon": "http://127.0.0.1:8545", + "timeout": "10s", + "classic": true }, "payouts": { diff --git a/configs/stratum9b.json b/configs/stratum9b.json index fa70b4f..cf4168f 100644 --- a/configs/stratum9b.json +++ b/configs/stratum9b.json @@ -19,7 +19,7 @@ "blockRefreshInterval": "120ms", "stateUpdateInterval": "3s", "difficulty": 9000000000, - "stratumHostname": "perkle-pool.esprezzo.io", + "stratumHostname": "Domain name", "healthCheck": true, "maxFails": 100, "hashrateExpiration": "3h", @@ -66,12 +66,12 @@ "upstream": [ { "name": "main", - "url": "http://40.121.105.44:8501", + "url": "http://127.0.01:8545", "timeout": "10s" }, { "name": "backup", - "url": "http://127.0.0.2:8501", + "url": "http://127.0.0.2:8545", "timeout": "10s" } ], @@ -86,14 +86,15 @@ "unlocker": { "enabled": false, "poolFee": 0.5, - "poolFeeAddress": "0x0f31986d7a0d4f160acd97583e3c3b591dcb5dde", + "poolFeeAddress": "0xd92fa5a9732a0aec36dc8d5a6a1305dc2d3e09e6", "donate": true, "depth": 120, "immatureDepth": 20, "keepTxFees": false, "interval": "10m", - "daemon": "http://127.0.0.1:8501", - "timeout": "10s" + "daemon": "http://127.0.0.1:8545", + "timeout": "10s", + "classic": true }, "payouts": { diff --git a/configs/unlocker.json b/configs/unlocker.json index e7b1a9c..d22658c 100644 --- a/configs/unlocker.json +++ b/configs/unlocker.json @@ -65,12 +65,12 @@ "upstream": [ { "name": "main", - "url": "http://40.121.105.44:8501", + "url": "http://127.0.0.1:8545", "timeout": "10s" }, { "name": "backup", - "url": "http://127.0.0.1:8501", + "url": "http://127.0.0.1:8545", "timeout": "10s" } ], @@ -85,14 +85,15 @@ "unlocker": { "enabled": true, "poolFee": 0.5, - "poolFeeAddress": "0x0f31986d7a0d4f160acd97583e3c3b591dcb5dde", + "poolFeeAddress": "0xd92fa5a9732a0aec36dc8d5a6a1305dc2d3e09e6", "donate": true, "depth": 120, "immatureDepth": 20, "keepTxFees": false, "interval": "10m", - "daemon": "http://127.0.0.1:8501", - "timeout": "120s" + "daemon": "http://127.0.0.1:8545", + "timeout": "120s", + "classic": true }, "payouts": { diff --git a/payouts/unlocker.go b/payouts/unlocker.go index 07a4576..7d7a328 100644 --- a/payouts/unlocker.go +++ b/payouts/unlocker.go @@ -4,7 +4,7 @@ import ( "fmt" "log" "math/big" - "os" + "os" "strconv" "strings" "time" @@ -27,15 +27,18 @@ 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 constReward = math.MustParseBig256("3200000000000000000") -var uncleReward = new(big.Int).Div(constReward, new(big.Int).SetInt64(32)) +var homesteadReward = math.MustParseBig256("5000000000000000000") +var byzantiumReward = math.MustParseBig256("3200000000000000000") +var classicReward = math.MustParseBig256("3200000000000000000") -// Donate 5% from pool fees to developers -const donationFee = 5.0 +// Donate 10% from pool fees to developers +const donationFee = 10 const donationAccount = "0xd92fa5a9732a0aec36dc8d5a6a1305dc2d3e09e6" type BlockUnlocker struct { @@ -159,7 +162,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 @@ -204,14 +207,12 @@ func matchCandidate(block *rpc.GetBlockReply, candidate *storage.BlockData) bool } func (u *BlockUnlocker) handleBlock(block *rpc.GetBlockReply, candidate *storage.BlockData) error { - // Initial 5 Ether static reward - reward := new(big.Int).Set(constReward) - correctHeight, err := strconv.ParseInt(strings.Replace(block.Number, "0x", "", -1), 16, 64) if err != nil { return err } candidate.Height = correctHeight + reward := getConstReward(candidate.Height, u.config.Classic) // Add TX fees extraTxReward, err := u.getExtraRewardForTx(block) @@ -225,6 +226,7 @@ func (u *BlockUnlocker) handleBlock(block *rpc.GetBlockReply, candidate *storage } // Add reward for including uncles + uncleReward := getRewardForUncle(candidate.Height, u.config.Classic) rewardForUncles := big.NewInt(0).Mul(uncleReward, big.NewInt(int64(len(block.Uncles)))) reward.Add(reward, rewardForUncles) @@ -234,12 +236,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 @@ -337,7 +339,7 @@ func (u *BlockUnlocker) unlockPendingBlocks() { entries = append(entries, fmt.Sprintf("\tREWARD %v: %v: %v Shannon", block.RoundKey(), login, reward)) per := new(big.Rat) if val, ok := percents[login]; ok { - per = val + per = val } u.backend.WriteReward(login, reward, per, true, block) } @@ -519,11 +521,35 @@ func weiToShannonInt64(wei *big.Rat) int64 { return value } -func getUncleReward(uHeight, height int64) *big.Int { - reward := new(big.Int).Set(constReward) - reward.Mul(big.NewInt(uHeight+8-height), reward) - reward.Div(reward, big.NewInt(8)) - return reward +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) + } +} + +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, 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) { @@ -542,4 +568,4 @@ func (u *BlockUnlocker) getExtraRewardForTx(block *rpc.GetBlockReply) (*big.Int, } } return amount, nil -} +} \ No newline at end of file diff --git a/payouts/unlocker_test.go b/payouts/unlocker_test.go index bd57fcf..822425a 100644 --- a/payouts/unlocker_test.go +++ b/payouts/unlocker_test.go @@ -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{ @@ -75,9 +88,10 @@ func TestGetUncleReward(t *testing.T) { 4: "2500000000000000000", 5: "1875000000000000000", 6: "1250000000000000000", + 7: "625000000000000000", } - for i := int64(1); i < 7; i++ { - rewards[i] = getUncleReward(1, i+1).String() + for i := int64(1); i < 8; i++ { + rewards[i] = getUncleReward(1, i+1, false).String() } for i, reward := range rewards { if expectedRewards[i] != rewards[i] { @@ -86,6 +100,43 @@ func TestGetUncleReward(t *testing.T) { } } +func TestGetByzantiumUncleReward(t *testing.T) { + rewards := make(map[int64]string) + expectedRewards := map[int64]string{ + 1: "2625000000000000000", + 2: "2250000000000000000", + 3: "1875000000000000000", + 4: "1500000000000000000", + 5: "1125000000000000000", + 6: "750000000000000000", + 7: "375000000000000000", + } + for i := int64(1); i < 8; i++ { + rewards[i] = getUncleReward(byzantiumHardForkHeight, byzantiumHardForkHeight+i, false).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 TestGetRewardForUngle(t *testing.T) { + 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, false).String() + expectedReward := "93750000000000000" + if expectedReward != reward { + t.Errorf("Incorrect uncle bonus for height %v, expected %v vs %v", byzantiumHardForkHeight, expectedReward, reward) + } +} + func TestMatchCandidate(t *testing.T) { gethBlock := &rpc.GetBlockReply{Hash: "0x12345A", Nonce: "0x1A"} parityBlock := &rpc.GetBlockReply{Hash: "0x12345A", SealFields: []string{"0x0A", "0x1A"}} @@ -110,4 +161,4 @@ func TestMatchCandidate(t *testing.T) { if !matchCandidate(block, immature) { t.Error("Must match with hash") } -} +} \ No newline at end of file