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

Method processBlocks

sqlchain/chain.go:752–849  ·  view source on GitHub ↗
(ctx context.Context)

Source from the content-addressed store, hash-verified

750}
751
752func (c *Chain) processBlocks(ctx context.Context) {
753 var (
754 cld, ccl = context.WithCancel(ctx)
755 wg = &sync.WaitGroup{}
756 )
757
758 returnStash := func(stash []*types.Block) {
759 defer wg.Done()
760 for i, block := range stash {
761 select {
762 case c.blocks <- block:
763 case <-cld.Done():
764 c.logEntry().WithFields(log.Fields{
765 "remaining": len(stash) - i,
766 }).WithError(cld.Err()).Debug("abort stash returning")
767 return
768 }
769 }
770 }
771
772 defer func() {
773 ccl()
774 wg.Wait()
775 }()
776
777 var stash []*types.Block
778 for {
779 le := c.logEntryWithHeadState()
780 select {
781 case h := <-c.heights:
782 // Trigger billing
783 index, total := c.rt.getIndexTotal()
784 period := int32(c.updatePeriod)
785 isBillingPeriod := (h%period == 0)
786 isMyTurnBilling := (h/period%total == index)
787 if isBillingPeriod && isMyTurnBilling {
788 ub, err := c.billing(h, c.rt.getHead().node)
789 if err != nil {
790 le.WithError(err).Error("billing failed")
791 }
792 // allocate nonce
793 nonceReq := &types.NextAccountNonceReq{}
794 nonceResp := &types.NextAccountNonceResp{}
795 nonceReq.Addr = *c.addr
796 if err = rpc.RequestBP(route.MCCNextAccountNonce.String(), nonceReq, nonceResp); err != nil {
797 // allocate nonce failed
798 le.WithError(err).Warning("allocate nonce for transaction failed")
799 }
800 ub.Nonce = nonceResp.Nonce
801 if err = ub.Sign(c.pk); err != nil {
802 le.WithError(err).Warning("sign tx failed")
803 }
804
805 addTxReq := &types.AddTxReq{TTL: 1}
806 addTxResp := &types.AddTxResp{}
807 addTxReq.Tx = ub
808 le.Debugf("nonce in processBlocks: %d, addr: %s",
809 addTxReq.Tx.GetAccountNonce(), addTxReq.Tx.GetAccountAddress())

Callers

nothing calls this directly

Calls 15

logEntryMethod · 0.95
logEntryWithHeadStateMethod · 0.95
billingMethod · 0.95
CheckAndPushNewBlockMethod · 0.95
DebugMethod · 0.80
WithErrorMethod · 0.80
WithFieldsMethod · 0.80
WaitMethod · 0.80
getIndexTotalMethod · 0.80
ErrorMethod · 0.80
WarningMethod · 0.80
DebugfMethod · 0.80

Tested by

no test coverage detected