commitNewWork creates a new block. Calling this function multiple times will abort the previous work on workers.
()
| 381 | |
| 382 | // commitNewWork creates a new block. Calling this function multiple times will abort the previous work on workers. |
| 383 | func (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())) |
no test coverage detected