MCPcopy Index your code
hub / github.com/CovenantSQL/CovenantSQL / pushBlock

Method pushBlock

sqlchain/chain.go:367–439  ·  view source on GitHub ↗

pushBlock pushes the signed block header to extend the current main chain.

(b *types.Block)

Source from the content-addressed store, hash-verified

365
366// pushBlock pushes the signed block header to extend the current main chain.
367func (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],

Callers 2

genesisMethod · 0.95
CheckAndPushNewBlockMethod · 0.95

Calls 15

updateMetricsMethod · 0.95
AddResponseMethod · 0.95
removeMethod · 0.95
logEntryMethod · 0.95
ConcatAllFunction · 0.92
EncodeMsgPackFunction · 0.92
WithFieldsFunction · 0.92
getHeightFromTimeMethod · 0.80
indexKeyMethod · 0.80
PutMethod · 0.80
setHeadMethod · 0.80
WarnMethod · 0.80

Tested by

no test coverage detected