Skip to content

Commit

Permalink
Added SetMinerPreference private miner API
Browse files Browse the repository at this point in the history
  • Loading branch information
jdowning100 committed Oct 24, 2024
1 parent 901ed16 commit ac3a4ef
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
4 changes: 4 additions & 0 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -1165,6 +1165,10 @@ func (c *Core) SetLockupByte(lockupByte uint8) {
c.sl.miner.worker.SetLockupByte(lockupByte)
}

func (c *Core) SetMinerPreference(minerPreference float64) {
c.sl.miner.worker.SetMinerPreference(minerPreference)
}

// SubscribePendingLogs starts delivering logs from pending transactions
// to the given channel.
func (c *Core) SubscribePendingLogs(ch chan<- []*types.Log) event.Subscription {
Expand Down
16 changes: 15 additions & 1 deletion core/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ type worker struct {
primaryCoinbase common.Address
secondaryCoinbase common.Address
coinbaseLockup uint8
minerPreference float64
extra []byte

workerDb ethdb.Database
Expand Down Expand Up @@ -236,6 +237,7 @@ func newWorker(config *Config, chainConfig *params.ChainConfig, db ethdb.Databas
fillTransactionsRollingAverage: &RollingAverage{windowSize: 100},
logger: logger,
coinbaseLockup: config.CoinbaseLockup,
minerPreference: config.MinerPreference,
}
if worker.coinbaseLockup > uint8(len(params.LockupByteToBlockDepth))-1 {
logger.Errorf("Invalid coinbase lockup value %d, using default value %d", worker.coinbaseLockup, params.DefaultCoinbaseLockup)
Expand Down Expand Up @@ -286,7 +288,7 @@ func (w *worker) pickCoinbases() {
defer w.mu.Unlock()

// Use the MinerPreference to bias the decision
if rand.Float64() > w.config.MinerPreference {
if rand.Float64() > w.minerPreference {
// if MinerPreference < 0.5, bias is towards Quai
w.primaryCoinbase = w.quaiCoinbase
w.secondaryCoinbase = w.qiCoinbase
Expand Down Expand Up @@ -325,6 +327,18 @@ func (w *worker) GetSecondaryCoinbase() common.Address {
return w.secondaryCoinbase
}

func (w *worker) SetMinerPreference(preference float64) {
w.mu.Lock()
defer w.mu.Unlock()
w.minerPreference = preference
}

func (w *worker) GetMinerPreference() float64 {
w.mu.RLock()
defer w.mu.RUnlock()
return w.minerPreference
}

func (w *worker) GetLockupByte() uint8 {
w.mu.RLock()
defer w.mu.RUnlock()
Expand Down
13 changes: 13 additions & 0 deletions quai/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"io"
"math/big"
"os"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -126,6 +127,18 @@ func (api *PrivateMinerAPI) SetLockupByte(lockupByte hexutil.Uint64) (bool, erro
return true, nil
}

func (api *PrivateMinerAPI) SetMinerPreference(minerPreference string) (bool, error) {
preferenceFloat, err := strconv.ParseFloat(minerPreference, 64)
if err != nil {
return false, err
}
if preferenceFloat < 0 || preferenceFloat > 1 {
return false, errors.New("miner preference must be between 0 and 1")
}
api.e.Core().SetMinerPreference(preferenceFloat)
return true, nil
}

// SetRecommitInterval updates the interval for miner sealing work recommitting.
func (api *PrivateMinerAPI) SetRecommitInterval(interval int) {
api.e.Core().SetRecommitInterval(time.Duration(interval) * time.Millisecond)
Expand Down

0 comments on commit ac3a4ef

Please sign in to comment.