miner: not sleep if locks multiple mutex, close XFN-40 (#1639)

This commit is contained in:
Daniel Liu 2025-10-31 14:54:32 +08:00 committed by GitHub
parent 20ec25c0ca
commit a5e68b9c52
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -596,7 +596,7 @@ func abs(x int64) int64 {
return x return x
} }
func (w *worker) commitNewWork() { func (w *worker) checkPreCommitWithLock() (*types.Block, bool) {
w.mu.Lock() w.mu.Lock()
defer w.mu.Unlock() defer w.mu.Unlock()
w.uncleMu.Lock() w.uncleMu.Lock()
@ -604,8 +604,10 @@ func (w *worker) commitNewWork() {
w.currentMu.Lock() w.currentMu.Lock()
defer w.currentMu.Unlock() defer w.currentMu.Unlock()
tstart := time.Now() return w.checkPreCommit()
}
func (w *worker) checkPreCommit() (*types.Block, bool) {
c := w.engine.(*XDPoS.XDPoS) c := w.engine.(*XDPoS.XDPoS)
var parent *types.Block var parent *types.Block
if c != nil { if c != nil {
@ -614,12 +616,11 @@ func (w *worker) commitNewWork() {
parent = w.chain.CurrentBlock() parent = w.chain.CurrentBlock()
} }
var signers map[common.Address]struct{}
if parent.Hash().Hex() == w.lastParentBlockCommit { if parent.Hash().Hex() == w.lastParentBlockCommit {
return return parent, true
} }
if !w.announceTxs && atomic.LoadInt32(&w.mining) == 0 { if !w.announceTxs && atomic.LoadInt32(&w.mining) == 0 {
return return parent, true
} }
// Only try to commit new work if we are mining // Only try to commit new work if we are mining
@ -629,14 +630,24 @@ func (w *worker) commitNewWork() {
ok, err := c.YourTurn(w.chain, parent.Header(), w.coinbase) ok, err := c.YourTurn(w.chain, parent.Header(), w.coinbase)
if err != nil { if err != nil {
log.Warn("Failed when trying to commit new work", "err", err) log.Warn("Failed when trying to commit new work", "err", err)
return return parent, true
} }
if !ok { if !ok {
log.Info("Not my turn to commit block. Waiting...") log.Info("Not my turn to commit block. Waiting...")
return return parent, true
} }
} }
} }
return parent, false
}
func (w *worker) commitNewWork() {
parent, shouldReturn := w.checkPreCommitWithLock()
if shouldReturn {
return
}
tstart := time.Now()
tstamp := tstart.Unix() tstamp := tstart.Unix()
if parent.Time() >= uint64(tstamp) { if parent.Time() >= uint64(tstamp) {
tstamp = int64(parent.Time() + 1) tstamp = int64(parent.Time() + 1)
@ -648,6 +659,18 @@ func (w *worker) commitNewWork() {
time.Sleep(wait) time.Sleep(wait)
} }
w.mu.Lock()
defer w.mu.Unlock()
w.uncleMu.Lock()
defer w.uncleMu.Unlock()
w.currentMu.Lock()
defer w.currentMu.Unlock()
parent, shouldReturn = w.checkPreCommit()
if shouldReturn {
return
}
num := parent.Number() num := parent.Number()
header := &types.Header{ header := &types.Header{
ParentHash: parent.Hash(), ParentHash: parent.Hash(),
@ -720,6 +743,7 @@ func (w *worker) commitNewWork() {
log.Error("[commitNewWork] fail to check if block is epoch switch block when fetching pending transactions", "BlockNum", header.Number, "Hash", header.Hash()) log.Error("[commitNewWork] fail to check if block is epoch switch block when fetching pending transactions", "BlockNum", header.Number, "Hash", header.Hash())
} }
if !isEpochSwitchBlock { if !isEpochSwitchBlock {
var signers map[common.Address]struct{}
pending := w.eth.TxPool().Pending(true) pending := w.eth.TxPool().Pending(true)
txs, specialTxs = types.NewTransactionsByPriceAndNonce(w.current.signer, pending, signers, feeCapacity) txs, specialTxs = types.NewTransactionsByPriceAndNonce(w.current.signer, pending, signers, feeCapacity)
} }