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

[R4R]broadcast block before commit block and add metrics #975

Merged
Merged
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
20 changes: 17 additions & 3 deletions miner/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/metrics"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/trie"
)
Expand Down Expand Up @@ -77,6 +78,12 @@ const (
staleThreshold = 11
)

var (
commitTxsTimer = metrics.NewRegisteredTimer("worker/committxs", nil)
writeBlockTimer = metrics.NewRegisteredTimer("worker/writeblock", nil)
finalizeBlockTimer = metrics.NewRegisteredTimer("worker/finalizeblock", nil)
)

// environment is the worker's current environment and holds all
// information of the sealing block generation.
type environment struct {
Expand Down Expand Up @@ -601,6 +608,7 @@ func (w *worker) mainLoop() {
// already included in the current sealing block. These transactions will
// be automatically eliminated.
if !w.isRunning() && w.current != nil {
start := time.Now()
// If block is already full, abort
if gp := w.current.gasPool; gp != nil && gp.Gas() < params.TxGas {
continue
Expand All @@ -613,6 +621,7 @@ func (w *worker) mainLoop() {
txset := types.NewTransactionsByPriceAndNonce(w.current.signer, txs, w.current.header.BaseFee)
tcount := w.current.tcount
w.commitTransactions(w.current, txset, nil)
commitTxsTimer.UpdateSince(start)

// Only update the snapshot if any new transactions were added
// to the pending block
Expand Down Expand Up @@ -746,19 +755,22 @@ func (w *worker) resultLoop() {
}
logs = append(logs, receipt.Logs...)
}

// Broadcast the block and announce chain insertion event
w.mux.Post(core.NewMinedBlockEvent{Block: block})

// Commit block and state to database.
task.state.SetExpectedStateRoot(block.Root())
start := time.Now()
_, err := w.chain.WriteBlockAndSetHead(block, receipts, logs, task.state, true)
if err != nil {
log.Error("Failed writing block to chain", "err", err)
continue
}
writeBlockTimer.UpdateSince(start)
log.Info("Successfully sealed new block", "number", block.Number(), "sealhash", sealhash, "hash", hash,
"elapsed", common.PrettyDuration(time.Since(task.createdAt)))

// Broadcast the block and announce chain insertion event
w.mux.Post(core.NewMinedBlockEvent{Block: block})

// Insert the block into the set of pending ones to resultLoop for confirmations
w.unconfirmed.Insert(block.NumberU64(), block.Hash())

Expand Down Expand Up @@ -1195,10 +1207,12 @@ func (w *worker) commit(env *environment, interval func(), update bool, start ti
}
s.CorrectAccountsRoot(w.chain.CurrentBlock().Root())

finalizeStart := time.Now()
block, receipts, err := w.engine.FinalizeAndAssemble(w.chain, types.CopyHeader(env.header), s, env.txs, env.unclelist(), env.receipts)
if err != nil {
return err
}
finalizeBlockTimer.UpdateSince(finalizeStart)
// If we're post merge, just ignore
if !w.isTTDReached(block.Header()) {
select {
Expand Down