mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-19 10:22:23 +00:00
miner: polish
This commit is contained in:
parent
2996d11184
commit
9e13d95fd0
2 changed files with 20 additions and 23 deletions
|
|
@ -213,9 +213,9 @@ type worker struct {
|
||||||
running int32 // The indicator whether the consensus engine is running or not.
|
running int32 // The indicator whether the consensus engine is running or not.
|
||||||
|
|
||||||
// Test hooks
|
// Test hooks
|
||||||
newTaskHook func(*task) // Method to call upon receiving a new sealing task
|
newTaskHook func(*task) // Method to call upon receiving a new sealing task
|
||||||
skipSealHook func(*task) bool // Method to decide whether skipping the sealing.
|
skipSealHook func(*task) bool // Method to decide whether skipping the sealing.
|
||||||
fullTaskInterval func() // Method to call before pushing the full sealing task
|
fullTaskHook func() // Method to call before pushing the full sealing task
|
||||||
}
|
}
|
||||||
|
|
||||||
func newWorker(config *params.ChainConfig, engine consensus.Engine, eth Backend, mux *event.TypeMux) *worker {
|
func newWorker(config *params.ChainConfig, engine consensus.Engine, eth Backend, mux *event.TypeMux) *worker {
|
||||||
|
|
@ -338,6 +338,7 @@ func (w *worker) mainLoop() {
|
||||||
// If our mining block contains less than 2 uncle blocks,
|
// If our mining block contains less than 2 uncle blocks,
|
||||||
// add the new uncle block if valid and regenerate a mining block.
|
// add the new uncle block if valid and regenerate a mining block.
|
||||||
if w.isRunning() && w.current != nil && w.current.uncles.Cardinality() < 2 {
|
if w.isRunning() && w.current != nil && w.current.uncles.Cardinality() < 2 {
|
||||||
|
start := time.Now()
|
||||||
if err := w.commitUncle(w.current, ev.Block.Header()); err == nil {
|
if err := w.commitUncle(w.current, ev.Block.Header()); err == nil {
|
||||||
var uncles []*types.Header
|
var uncles []*types.Header
|
||||||
w.current.uncles.Each(func(item interface{}) bool {
|
w.current.uncles.Each(func(item interface{}) bool {
|
||||||
|
|
@ -352,7 +353,7 @@ func (w *worker) mainLoop() {
|
||||||
uncles = append(uncles, uncle.Header())
|
uncles = append(uncles, uncle.Header())
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
w.finalize(uncles, nil, true)
|
w.commit(uncles, nil, true, start)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -667,7 +668,7 @@ func (w *worker) commitNewWork() {
|
||||||
|
|
||||||
// Create an empty block based on temporary copied state for sealing in advance without waiting block
|
// Create an empty block based on temporary copied state for sealing in advance without waiting block
|
||||||
// execution finished.
|
// execution finished.
|
||||||
w.finalize(uncles, nil, false)
|
w.commit(uncles, nil, false, tstart)
|
||||||
|
|
||||||
// Fill the block with all available pending transactions.
|
// Fill the block with all available pending transactions.
|
||||||
pending, err := w.eth.TxPool().Pending()
|
pending, err := w.eth.TxPool().Pending()
|
||||||
|
|
@ -683,26 +684,21 @@ func (w *worker) commitNewWork() {
|
||||||
txs := types.NewTransactionsByPriceAndNonce(w.current.signer, pending)
|
txs := types.NewTransactionsByPriceAndNonce(w.current.signer, pending)
|
||||||
env.commitTransactions(w.mux, txs, w.chain, w.coinbase)
|
env.commitTransactions(w.mux, txs, w.chain, w.coinbase)
|
||||||
|
|
||||||
w.finalize(uncles, w.fullTaskInterval, true)
|
w.commit(uncles, w.fullTaskHook, true, tstart)
|
||||||
}
|
}
|
||||||
|
|
||||||
// finalize runs any post-transaction state modifications, assembles the final block
|
// commit runs any post-transaction state modifications, assembles the final block
|
||||||
// and commits new work if consensus engine is running.
|
// and commits new work if consensus engine is running.
|
||||||
func (w *worker) finalize(uncles []*types.Header, interval func(), update bool) error {
|
func (w *worker) commit(uncles []*types.Header, interval func(), update bool, start time.Time) error {
|
||||||
current := w.current
|
|
||||||
// Deep copy receipts here to avoid interaction between different tasks.
|
// Deep copy receipts here to avoid interaction between different tasks.
|
||||||
receipts := make([]*types.Receipt, len(current.receipts))
|
receipts := make([]*types.Receipt, len(w.current.receipts))
|
||||||
for i, l := range current.receipts {
|
for i, l := range w.current.receipts {
|
||||||
receipts[i] = new(types.Receipt)
|
receipts[i] = new(types.Receipt)
|
||||||
*receipts[i] = *l
|
*receipts[i] = *l
|
||||||
}
|
}
|
||||||
|
s := w.current.state.Copy()
|
||||||
var (
|
block, err := w.engine.Finalize(w.chain, w.current.header, s, w.current.txs, uncles, w.current.receipts)
|
||||||
block *types.Block
|
if err != nil {
|
||||||
err error
|
|
||||||
)
|
|
||||||
state := current.state.Copy()
|
|
||||||
if block, err = w.engine.Finalize(w.chain, current.header, state, current.txs, uncles, current.receipts); err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if w.isRunning() {
|
if w.isRunning() {
|
||||||
|
|
@ -710,9 +706,10 @@ func (w *worker) finalize(uncles []*types.Header, interval func(), update bool)
|
||||||
interval()
|
interval()
|
||||||
}
|
}
|
||||||
select {
|
select {
|
||||||
case w.taskCh <- &task{receipts: receipts, state: state, block: block, createdAt: time.Now()}:
|
case w.taskCh <- &task{receipts: receipts, state: s, block: block, createdAt: time.Now()}:
|
||||||
w.unconfirmed.Shift(block.NumberU64() - 1)
|
w.unconfirmed.Shift(block.NumberU64() - 1)
|
||||||
log.Info("Commit new mining work", "number", block.Number(), "txs", current.tcount, "uncles", len(uncles))
|
log.Info("Commit new mining work", "number", block.Number(), "txs", w.current.tcount, "uncles", len(uncles),
|
||||||
|
"elapsed", common.PrettyDuration(time.Since(start)))
|
||||||
case <-w.exitCh:
|
case <-w.exitCh:
|
||||||
log.Info("Worker has exited")
|
log.Info("Worker has exited")
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -193,7 +193,7 @@ func testEmptyWork(t *testing.T, chainConfig *params.ChainConfig, engine consens
|
||||||
taskCh <- struct{}{}
|
taskCh <- struct{}{}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
w.fullTaskInterval = func() {
|
w.fullTaskHook = func() {
|
||||||
time.Sleep(100 * time.Millisecond)
|
time.Sleep(100 * time.Millisecond)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -241,7 +241,7 @@ func TestStreamUncleBlock(t *testing.T) {
|
||||||
w.skipSealHook = func(task *task) bool {
|
w.skipSealHook = func(task *task) bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
w.fullTaskInterval = func() {
|
w.fullTaskHook = func() {
|
||||||
time.Sleep(100 * time.Millisecond)
|
time.Sleep(100 * time.Millisecond)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -262,7 +262,7 @@ func TestStreamUncleBlock(t *testing.T) {
|
||||||
t.Error("new task timeout")
|
t.Error("new task timeout")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
b.PostChainEvents([]interface{}{core.ChainSideEvent{b.uncleBlock}})
|
b.PostChainEvents([]interface{}{core.ChainSideEvent{Block: b.uncleBlock}})
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case <-taskCh:
|
case <-taskCh:
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue