-
Notifications
You must be signed in to change notification settings - Fork 21.9k
miner: refactor helper functions in worker.go #21044
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
Changes from all commits
8b72a9d
60a82f6
03bb8ed
5f60bad
d80e017
78a0c58
4b96121
f1a1961
eb848f9
0ece00e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -288,6 +288,28 @@ func (w *worker) close() { | |
| close(w.exitCh) | ||
| } | ||
|
|
||
| // recalcRecommit recalculates the resubmitting interval upon feedback. | ||
| func recalcRecommit(minRecommit, prev time.Duration, target float64, inc bool) time.Duration { | ||
| var ( | ||
| prevF = float64(prev.Nanoseconds()) | ||
| next float64 | ||
| ) | ||
| if inc { | ||
| next = prevF*(1-intervalAdjustRatio) + intervalAdjustRatio*(target+intervalAdjustBias) | ||
| max := float64(maxRecommitInterval.Nanoseconds()) | ||
| if next > max { | ||
| next = max | ||
| } | ||
| } else { | ||
| next = prevF*(1-intervalAdjustRatio) + intervalAdjustRatio*(target-intervalAdjustBias) | ||
| min := float64(minRecommit.Nanoseconds()) | ||
| if next < min { | ||
| next = min | ||
| } | ||
| } | ||
| return time.Duration(int64(next)) | ||
| } | ||
|
|
||
| // newWorkLoop is a standalone goroutine to submit new mining work upon received events. | ||
| func (w *worker) newWorkLoop(recommit time.Duration) { | ||
| var ( | ||
|
|
@@ -310,27 +332,6 @@ func (w *worker) newWorkLoop(recommit time.Duration) { | |
| timer.Reset(recommit) | ||
| atomic.StoreInt32(&w.newTxs, 0) | ||
| } | ||
| // recalcRecommit recalculates the resubmitting interval upon feedback. | ||
| recalcRecommit := func(target float64, inc bool) { | ||
| var ( | ||
| prev = float64(recommit.Nanoseconds()) | ||
| next float64 | ||
| ) | ||
| if inc { | ||
| next = prev*(1-intervalAdjustRatio) + intervalAdjustRatio*(target+intervalAdjustBias) | ||
| // Recap if interval is larger than the maximum time interval | ||
| if next > float64(maxRecommitInterval.Nanoseconds()) { | ||
| next = float64(maxRecommitInterval.Nanoseconds()) | ||
| } | ||
| } else { | ||
| next = prev*(1-intervalAdjustRatio) + intervalAdjustRatio*(target-intervalAdjustBias) | ||
| // Recap if interval is less than the user specified minimum | ||
| if next < float64(minRecommit.Nanoseconds()) { | ||
| next = float64(minRecommit.Nanoseconds()) | ||
| } | ||
| } | ||
| recommit = time.Duration(int64(next)) | ||
| } | ||
| // clearPending cleans the stale pending tasks. | ||
| clearPending := func(number uint64) { | ||
| w.pendingMu.Lock() | ||
|
|
@@ -383,11 +384,12 @@ func (w *worker) newWorkLoop(recommit time.Duration) { | |
| // Adjust resubmit interval by feedback. | ||
| if adjust.inc { | ||
| before := recommit | ||
| recalcRecommit(float64(recommit.Nanoseconds())/adjust.ratio, true) | ||
| target := float64(recommit.Nanoseconds()) / adjust.ratio | ||
| recommit = recalcRecommit(minRecommit, recommit, target, true) | ||
| log.Trace("Increase miner recommit interval", "from", before, "to", recommit) | ||
| } else { | ||
| before := recommit | ||
| recalcRecommit(float64(minRecommit.Nanoseconds()), false) | ||
| recommit = recalcRecommit(minRecommit, recommit, float64(minRecommit.Nanoseconds()), false) | ||
| log.Trace("Decrease miner recommit interval", "from", before, "to", recommit) | ||
| } | ||
|
|
||
|
|
@@ -535,7 +537,7 @@ func (w *worker) taskLoop() { | |
| continue | ||
| } | ||
| w.pendingMu.Lock() | ||
| w.pendingTasks[w.engine.SealHash(task.block.Header())] = task | ||
| w.pendingTasks[sealHash] = task | ||
| w.pendingMu.Unlock() | ||
|
|
||
| if err := w.engine.Seal(w.chain, task.block, w.resultCh, stopCh); err != nil { | ||
|
|
@@ -954,13 +956,9 @@ func (w *worker) commitNewWork(interrupt *int32, noempty bool, timestamp int64) | |
| // and commits new work if consensus engine is running. | ||
| func (w *worker) commit(uncles []*types.Header, interval func(), update bool, start time.Time) error { | ||
| // Deep copy receipts here to avoid interaction between different tasks. | ||
| receipts := make([]*types.Receipt, len(w.current.receipts)) | ||
| for i, l := range w.current.receipts { | ||
| receipts[i] = new(types.Receipt) | ||
| *receipts[i] = *l | ||
| } | ||
| receipts := copyReceipts(w.current.receipts) | ||
| s := w.current.state.Copy() | ||
| block, err := w.engine.FinalizeAndAssemble(w.chain, w.current.header, s, w.current.txs, uncles, w.current.receipts) | ||
| block, err := w.engine.FinalizeAndAssemble(w.chain, w.current.header, s, w.current.txs, uncles, receipts) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
@@ -971,15 +969,10 @@ func (w *worker) commit(uncles []*types.Header, interval func(), update bool, st | |
| select { | ||
| case w.taskCh <- &task{receipts: receipts, state: s, block: block, createdAt: time.Now()}: | ||
| w.unconfirmed.Shift(block.NumberU64() - 1) | ||
|
|
||
| feesWei := new(big.Int) | ||
| for i, tx := range block.Transactions() { | ||
| feesWei.Add(feesWei, new(big.Int).Mul(new(big.Int).SetUint64(receipts[i].GasUsed), tx.GasPrice())) | ||
| } | ||
| feesEth := new(big.Float).Quo(new(big.Float).SetInt(feesWei), new(big.Float).SetInt(big.NewInt(params.Ether))) | ||
|
|
||
| log.Info("Commit new mining work", "number", block.Number(), "sealhash", w.engine.SealHash(block.Header()), | ||
| "uncles", len(uncles), "txs", w.current.tcount, "gas", block.GasUsed(), "fees", feesEth, "elapsed", common.PrettyDuration(time.Since(start))) | ||
| "uncles", len(uncles), "txs", w.current.tcount, | ||
| "gas", block.GasUsed(), "fees", totalFees(block, receipts), | ||
| "elapsed", common.PrettyDuration(time.Since(start))) | ||
|
|
||
| case <-w.exitCh: | ||
| log.Info("Worker has exited") | ||
|
|
@@ -991,10 +984,29 @@ func (w *worker) commit(uncles []*types.Header, interval func(), update bool, st | |
| return nil | ||
| } | ||
|
|
||
| // copyReceipts makes a deep copy of the given receipts. | ||
| func copyReceipts(receipts []*types.Receipt) []*types.Receipt { | ||
| result := make([]*types.Receipt, len(receipts)) | ||
| for i, l := range receipts { | ||
| cpy := *l | ||
| result[i] = &cpy | ||
| } | ||
| return result | ||
| } | ||
|
|
||
| // postSideBlock fires a side chain event, only use it for testing. | ||
| func (w *worker) postSideBlock(event core.ChainSideEvent) { | ||
| select { | ||
| case w.chainSideCh <- event: | ||
| case <-w.exitCh: | ||
| } | ||
| } | ||
|
|
||
| // totalFees computes total consumed fees in ETH. Block transactions and receipts have to have the same order. | ||
| func totalFees(block *types.Block, receipts []*types.Receipt) *big.Float { | ||
| feesWei := new(big.Int) | ||
| for i, tx := range block.Transactions() { | ||
| feesWei.Add(feesWei, new(big.Int).Mul(new(big.Int).SetUint64(receipts[i].GasUsed), tx.GasPrice())) | ||
| } | ||
| return new(big.Float).Quo(new(big.Float).SetInt(feesWei), new(big.Float).SetInt(big.NewInt(params.Ether))) | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method is again a small utility code that's used solely for 1 log line. Moving it into a separate method makes things weirder because it makes the reader wonder what the importance is.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My motivation is similar to the case with In general - I would be happy, and I hope others would also benefit, if we have more such abstractions. |
||
Uh oh!
There was an error while loading. Please reload this page.