From 75b7ac59fd4eeaa9cbf77a4a3c24f8771d5b602c Mon Sep 17 00:00:00 2001 From: irrun Date: Thu, 6 Jun 2024 11:44:12 +0800 Subject: [PATCH] fix: waiting for the last simulation before pick best bid --- miner/bid_simulator.go | 23 ++--------------------- miner/worker.go | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 21 deletions(-) diff --git a/miner/bid_simulator.go b/miner/bid_simulator.go index 4f6f2b92de..a52c3e1bc4 100644 --- a/miner/bid_simulator.go +++ b/miner/bid_simulator.go @@ -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 ( @@ -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 @@ -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{ @@ -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 @@ -713,8 +696,6 @@ type BidRuntime struct { packedBlockReward *big.Int packedValidatorReward *big.Int - - duration time.Duration } func (r *BidRuntime) validReward() bool { diff --git a/miner/worker.go b/miner/worker.go index 54f7e962fb..748950bbba 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -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 @@ -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 {