Skip to content

Commit

Permalink
fix: waiting for the last simulation before pick best bid
Browse files Browse the repository at this point in the history
  • Loading branch information
irrun committed Jun 6, 2024
1 parent 7ad74a6 commit 75b7ac5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 21 deletions.
23 changes: 2 additions & 21 deletions miner/bid_simulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,6 @@ import (
const (
// maxBidPerBuilderPerBlock is the max bid number per builder
maxBidPerBuilderPerBlock = 3

// leftOverTimeRate is the rate of left over time to simulate a bid
leftOverTimeRate = 11
// leftOverTimeScale is the scale of left over time to simulate a bid
leftOverTimeScale = 10
)

var (
bidSimTimer = metrics.NewRegisteredTimer("bid/sim/duration", nil)
)

var (
Expand Down Expand Up @@ -319,12 +310,8 @@ func (b *bidSimulator) newBidLoop() {
// commit aborts in-flight bid execution with given signal and resubmits a new one.
commit := func(reason int32, bidRuntime *BidRuntime) {
// if the left time is not enough to do simulation, return
var simDuration time.Duration
if lastBid := b.GetBestBid(bidRuntime.bid.ParentHash); lastBid != nil && lastBid.duration != 0 {
simDuration = lastBid.duration
}

if time.Until(b.bidMustBefore(bidRuntime.bid.ParentHash)) <= simDuration*leftOverTimeRate/leftOverTimeScale {
delay := b.engine.Delay(b.chain, bidRuntime.env.header, &b.delayLeftOver)
if delay == nil || *delay <= 0 {
log.Info("BidSimulator: abort commit, not enough time to simulate",
"builder", bidRuntime.bid.Builder, "bidHash", bidRuntime.bid.Hash().Hex())
return
Expand Down Expand Up @@ -530,7 +517,6 @@ func (b *bidSimulator) simBid(interruptCh chan int32, bidRuntime *BidRuntime) {

// ensure simulation exited then start next simulation
b.SetSimulatingBid(parentHash, bidRuntime)
start := time.Now()

defer func(simStart time.Time) {
logCtx := []any{
Expand All @@ -556,11 +542,8 @@ func (b *bidSimulator) simBid(interruptCh chan int32, bidRuntime *BidRuntime) {
}

b.RemoveSimulatingBid(parentHash)
bidSimTimer.UpdateSince(start)

if success {
bidRuntime.duration = time.Since(simStart)

// only recommit self bid when newBidCh is empty
if len(b.newBidCh) > 0 {
return
Expand Down Expand Up @@ -713,8 +696,6 @@ type BidRuntime struct {

packedBlockReward *big.Int
packedValidatorReward *big.Int

duration time.Duration
}

func (r *BidRuntime) validReward() bool {
Expand Down
14 changes: 14 additions & 0 deletions miner/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ type getWorkReq struct {

type bidFetcher interface {
GetBestBid(parentHash common.Hash) *BidRuntime
GetSimulatingBid(prevBlockHash common.Hash) *BidRuntime
}

// worker is the main object which takes care of submitting new work to consensus engine
Expand Down Expand Up @@ -1336,6 +1337,19 @@ LOOP:
// when in-turn, compare with remote work.
from := bestWork.coinbase
if w.bidFetcher != nil && bestWork.header.Difficulty.Cmp(diffInTurn) == 0 {
waitBidTimer := time.NewTimer(w.config.DelayLeftOver / 5)
LOOP_WAIT_BID:
for {
select {
case <-waitBidTimer.C:
break LOOP_WAIT_BID
default:
if w.bidFetcher.GetSimulatingBid(bestWork.header.ParentHash) == nil {
break LOOP_WAIT_BID
}
}
}

bestBid := w.bidFetcher.GetBestBid(bestWork.header.ParentHash)

if bestBid != nil {
Expand Down

0 comments on commit 75b7ac5

Please sign in to comment.