pushBlock pushes the signed block header to extend the current main chain.
(b *types.Block)
| 365 | |
| 366 | // pushBlock pushes the signed block header to extend the current main chain. |
| 367 | func (c *Chain) pushBlock(b *types.Block) (err error) { |
| 368 | // Prepare and encode |
| 369 | var ( |
| 370 | h = c.rt.getHeightFromTime(b.Timestamp()) |
| 371 | node = newBlockNode(h, b, c.rt.getHead().node) |
| 372 | head = &state{ |
| 373 | node: node, |
| 374 | Head: node.hash, |
| 375 | Height: node.height, |
| 376 | } |
| 377 | |
| 378 | blockKey = utils.ConcatAll(c.metaBlockIndex, node.indexKey()) |
| 379 | encBlock *bytes.Buffer |
| 380 | ) |
| 381 | if encBlock, err = utils.EncodeMsgPack(b); err != nil { |
| 382 | return |
| 383 | } |
| 384 | |
| 385 | // Put block |
| 386 | err = blkDB.Put(blockKey, encBlock.Bytes(), nil) |
| 387 | if err != nil { |
| 388 | err = errors.Wrapf(err, "put %s", string(node.indexKey())) |
| 389 | return |
| 390 | } |
| 391 | atomic.AddInt32(&c.cachedBlockCount, 1) |
| 392 | c.rt.setHead(head) |
| 393 | c.bi.addBlock(node) |
| 394 | |
| 395 | // update metrics |
| 396 | c.updateMetrics() |
| 397 | |
| 398 | // Keep track of the queries from the new block |
| 399 | var ( |
| 400 | ierr error |
| 401 | le = log.WithFields(log.Fields{ |
| 402 | "db": c.databaseID, |
| 403 | "producer": b.Producer(), |
| 404 | "block_hash": b.BlockHash(), |
| 405 | }) |
| 406 | ) |
| 407 | for i, v := range b.QueryTxs { |
| 408 | if ierr = c.AddResponse(v.Response); ierr != nil { |
| 409 | le.WithFields(log.Fields{ |
| 410 | "index": i, |
| 411 | }).WithError(ierr).Warn("failed to add Response to ackIndex") |
| 412 | } |
| 413 | } |
| 414 | for i, v := range b.Acks { |
| 415 | if ierr = c.remove(v); ierr != nil { |
| 416 | le.WithFields(log.Fields{ |
| 417 | "index": i, |
| 418 | }).WithError(ierr).Warn("failed to remove Ack from ackIndex") |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | c.logEntry().WithFields(log.Fields{ |
| 423 | "block": b.BlockHash().String()[:8], |
| 424 | "producer": b.Producer()[:8], |
no test coverage detected