Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(miner/worker): remove contention on worker.currentMu for pending API requests #477

Merged
merged 2 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions eth/api_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ func (b *EthApiBackend) HeaderByNumber(ctx context.Context, blockNr rpc.BlockNum
// Pending block is only known by the miner
if blockNr == rpc.PendingBlockNumber {
block := b.eth.miner.PendingBlock()
if block == nil {
return nil, errors.New("pending block is not available")
}
return block.Header(), nil
}
// Otherwise resolve and return the block
Expand All @@ -89,6 +92,9 @@ func (b *EthApiBackend) BlockByNumber(ctx context.Context, blockNr rpc.BlockNumb
// Pending block is only known by the miner
if blockNr == rpc.PendingBlockNumber {
block := b.eth.miner.PendingBlock()
if block == nil {
return nil, errors.New("pending block is not available")
}
return block, nil
}
// Otherwise resolve and return the block
Expand All @@ -102,6 +108,9 @@ func (b *EthApiBackend) StateAndHeaderByNumber(ctx context.Context, blockNr rpc.
// Pending state is only known by the miner
if blockNr == rpc.PendingBlockNumber {
block, state := b.eth.miner.Pending()
if block == nil {
return nil, nil, errors.New("pending block is not available")
}
return state, block.Header(), nil
}
// Otherwise resolve the block number and return its state
Expand Down
55 changes: 38 additions & 17 deletions miner/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ type worker struct {
currentMu sync.Mutex
current *Work

snapshotMu sync.RWMutex
snapshotBlock *types.Block
snapshotState *state.StateDB

uncleMu sync.Mutex
possibleUncles map[common.Hash]*types.Block

Expand Down Expand Up @@ -195,32 +199,34 @@ func (self *worker) setExtra(extra []byte) {
}

func (self *worker) pending() (*types.Block, *state.StateDB) {
if atomic.LoadInt32(&self.mining) == 0 {
// return a snapshot to avoid contention on currentMu mutex
self.snapshotMu.RLock()
defer self.snapshotMu.RUnlock()
if self.snapshotBlock == nil {
return nil, nil
}
return self.snapshotBlock, self.snapshotState.Copy()
}

self.currentMu.Lock()
defer self.currentMu.Unlock()

if atomic.LoadInt32(&self.mining) == 0 {
return types.NewBlock(
self.current.header,
self.current.txs,
nil,
self.current.receipts,
), self.current.state.Copy()
if self.current == nil {
return nil, nil
}
return self.current.Block, self.current.state.Copy()
}

func (self *worker) pendingBlock() *types.Block {
self.currentMu.Lock()
defer self.currentMu.Unlock()

if atomic.LoadInt32(&self.mining) == 0 {
return types.NewBlock(
self.current.header,
self.current.txs,
nil,
self.current.receipts,
)
// return a snapshot to avoid contention on currentMu mutex
self.snapshotMu.RLock()
defer self.snapshotMu.RUnlock()
return self.snapshotBlock
}

self.currentMu.Lock()
defer self.currentMu.Unlock()
return self.current.Block
}

Expand Down Expand Up @@ -316,6 +322,7 @@ func (self *worker) update() {
feeCapacity := state.GetTRC21FeeCapacityFromState(self.current.state)
txset, specialTxs := types.NewTransactionsByPriceAndNonce(self.current.signer, txs, nil, feeCapacity)
self.current.commitTransactions(self.mux, feeCapacity, txset, specialTxs, self.chain, self.coinbase)
self.updateSnapshot()
self.currentMu.Unlock()
} else {
// If we're mining, but nothing is being processed, wake on new transactions
Expand Down Expand Up @@ -816,6 +823,7 @@ func (self *worker) commitNewWork() {
self.lastParentBlockCommit = parent.Hash().Hex()
}
self.push(work)
self.updateSnapshot()
}

func (self *worker) commitUncle(work *Work, uncle *types.Header) error {
Expand Down Expand Up @@ -1076,6 +1084,19 @@ func (env *Work) commitTransactions(mux *event.TypeMux, balanceFee map[common.Ad
}
}

func (self *worker) updateSnapshot() {
self.snapshotMu.Lock()
defer self.snapshotMu.Unlock()

self.snapshotBlock = types.NewBlock(
self.current.header,
self.current.txs,
nil,
self.current.receipts,
)
self.snapshotState = self.current.state.Copy()
}

func (env *Work) commitTransaction(balanceFee map[common.Address]*big.Int, tx *types.Transaction, bc *core.BlockChain, coinbase common.Address, gp *core.GasPool) (error, []*types.Log, bool, uint64) {
snap := env.state.Snapshot()

Expand Down