Skip to content

Commit

Permalink
broadcast block before commit block and add metrics
Browse files Browse the repository at this point in the history
Signed-off-by: cryyl <yl.on.the.way@gmail.com>
  • Loading branch information
kyrie-yl committed Jul 4, 2022
1 parent 0483394 commit 1e6782e
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions miner/worker.go
Expand Up @@ -81,7 +81,9 @@ const (
)

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

// environment is the worker's current environment and holds all of the current state information.
Expand Down Expand Up @@ -635,19 +637,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.WriteBlockWithState(block, receipts, logs, task.state, true)
if err != nil {
log.Error("Failed writing block to chain", "err", err)
continue
}
commitBlockTimer.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 @@ -1017,10 +1022,12 @@ func (w *worker) commit(uncles []*types.Header, interval func(), update bool, st

s.CorrectAccountsRoot(w.chain.CurrentBlock().Root())

finalizeStart := time.Now()
block, receipts, err := w.engine.FinalizeAndAssemble(w.chain, types.CopyHeader(w.current.header), s, w.current.txs, uncles, w.current.receipts)
if err != nil {
return err
}
finalizeBlockTimer.UpdateSince(finalizeStart)
if w.isRunning() {
if interval != nil {
interval()
Expand Down

0 comments on commit 1e6782e

Please sign in to comment.