MCPcopy Create free account
hub / github.com/CPChain/chain / commitNewWork

Method commitNewWork

miner/engine.go:383–474  ·  view source on GitHub ↗

commitNewWork creates a new block. Calling this function multiple times will abort the previous work on workers.

()

Source from the content-addressed store, hash-verified

381
382// commitNewWork creates a new block. Calling this function multiple times will abort the previous work on workers.
383func (e *engine) commitNewWork() {
384 e.mu.Lock()
385 defer e.mu.Unlock()
386 e.currentMu.Lock()
387 defer e.currentMu.Unlock()
388
389 parent := e.chain.CurrentBlock() // the head of the blockchain
390 tstart := time.Now()
391 num := parent.Number()
392 header := &types.Header{
393 ParentHash: parent.Hash(),
394 Number: big.NewInt(0).SetUint64(num.Uint64() + 1),
395 GasLimit: core.CalcGasLimit(parent),
396 Extra: e.extra,
397 }
398 // Only set the coinbase if we are mining (avoid spurious block rewards)
399 if atomic.LoadInt32(&e.mining) == 1 {
400 header.Coinbase = e.coinbase
401 }
402 if err := e.cons.PrepareBlock(e.chain, header); err != nil {
403 log.Error("Failed to prepare header for mining", "err", err)
404 return
405 }
406
407 // delay to add more txs to tx pool
408 delay := header.Timestamp().Sub(time.Now())
409 delay = delayBeforeSeal(delay)
410 log.Debug("Waiting for slot to seal", "delay", delay)
411
412 select {
413 case <-time.After(delay):
414 log.Debug("now to make work and try to seal", "delay", delay)
415 }
416
417 err := e.makeCurrentWork(parent, header)
418 if err != nil {
419 log.Error("Failed to create mining context", "err", err)
420 return
421 }
422 // create the current work task and check any fork transitions needed
423 // note, there is no transaction in this block
424 work := e.currentWork
425
426 // we now populate the work with pending transactions
427 pending, err := e.backend.TxPool().Pending()
428 if err != nil {
429 log.Error("Failed to fetch pending transactions", "err", err)
430 return
431 }
432 txs := types.NewTransactionsByPriceAndNonce(e.currentWork.signer, pending)
433
434 // break early at header.timestamp - delayBeforeSeal
435 // timeline ------------------------------------------
436 // | 9/10 | 1/10 |
437 // now commitTxsBreakTime header.timestamp
438 //
439 // timeline ------------------------------------------
440 log.Debug("timelog header.timestamp and now", "header.timestamp", header.Timestamp(), "now", time.Now(), "delay", header.Timestamp().Sub(time.Now()))

Callers 3

newEngineFunction · 0.95
updateMethod · 0.95
StartMethod · 0.80

Calls 15

TimestampMethod · 0.95
makeCurrentWorkMethod · 0.95
pushMethod · 0.95
updateSnapshotMethod · 0.95
delayBeforeSealFunction · 0.85
LockMethod · 0.80
UnlockMethod · 0.80
Uint64Method · 0.80
DebugMethod · 0.80
commitTransactionsMethod · 0.80
HeaderMethod · 0.80
CurrentBlockMethod · 0.65

Tested by

no test coverage detected