@@ -105,9 +105,9 @@ func NewChainWithContext(ctx context.Context, cfg *Config) (c *Chain, err error)
105105 ierr error
106106
107107 cld , ccl = context .WithCancel (ctx )
108- l = float64 (len (cfg .Peers .Servers ))
108+ l = uint32 (len (cfg .Peers .Servers ))
109109 t float64
110- m float64
110+ m uint32
111111
112112 st xi.Storage
113113 irre * blockNode
@@ -224,7 +224,7 @@ func NewChainWithContext(ctx context.Context, cfg *Config) (c *Chain, err error)
224224 if t = cfg .ComfirmThreshold ; t <= 0.0 {
225225 t = float64 (2 ) / 3.0
226226 }
227- if m = math .Ceil (l * t + 1 ); m > l {
227+ if m = uint32 ( math .Ceil (float64 ( l ) * t + 1 ) ); m > l {
228228 m = l
229229 }
230230
@@ -250,8 +250,8 @@ func NewChainWithContext(ctx context.Context, cfg *Config) (c *Chain, err error)
250250
251251 peers : cfg .Peers ,
252252 nodeID : cfg .NodeID ,
253- confirms : uint32 ( m ) ,
254- serversNum : uint32 ( len ( cfg . Peers . Servers )) ,
253+ confirms : m ,
254+ serversNum : l ,
255255 locSvIndex : uint32 (locSvIndex ),
256256 nextHeight : head .head .height + 1 ,
257257 offset : time .Duration (0 ), // TODO(leventeliu): initialize offset
@@ -474,6 +474,7 @@ func (c *Chain) processBlocks(ctx context.Context) {
474474 }).Debug (err )
475475 }
476476 case <- ctx .Done ():
477+ log .WithError (c .ctx .Err ()).Info ("abort block processing" )
477478 return
478479 }
479480 }
@@ -511,7 +512,7 @@ func (c *Chain) addTx(tx pi.Transaction) {
511512 select {
512513 case c .pendingTxs <- tx :
513514 case <- c .ctx .Done ():
514- log .WithError (c .ctx .Err ()).Error ("add transaction aborted" )
515+ log .WithError (c .ctx .Err ()).Warn ("add transaction aborted" )
515516 }
516517}
517518
@@ -531,21 +532,28 @@ func (c *Chain) processTxs(ctx context.Context) {
531532 case tx := <- c .pendingTxs :
532533 c .processTx (tx )
533534 case <- ctx .Done ():
535+ log .WithError (c .ctx .Err ()).Info ("abort transaction processing" )
534536 return
535537 }
536538 }
537539}
538540
539541func (c * Chain ) mainCycle (ctx context.Context ) {
542+ var timer = time .NewTimer (0 )
543+ defer func () {
544+ if ! timer .Stop () {
545+ <- timer .C
546+ }
547+ }()
540548 for {
541549 select {
542- case <- ctx .Done ():
543- log .WithError (ctx .Err ()).Debug ("abort main cycle" )
544- return
545- default :
550+ case <- timer .C :
546551 c .syncCurrentHead (ctx ) // Try to fetch block at height `nextHeight-1`
547-
548- if t , d := c .nextTick (); d > 0 {
552+ var t , d = c .nextTick ()
553+ if d <= 0 {
554+ // Try to produce block at `nextHeight` if it's my turn, and increase height by 1
555+ c .advanceNextHeight (t )
556+ } else {
549557 log .WithFields (log.Fields {
550558 "peer" : c .peerInfo (),
551559 "next_height" : c .getNextHeight (),
@@ -554,11 +562,11 @@ func (c *Chain) mainCycle(ctx context.Context) {
554562 "now_time" : t .Format (time .RFC3339Nano ),
555563 "duration" : d ,
556564 }).Debug ("main cycle" )
557- time .Sleep (d )
558- } else {
559- // Try to produce block at `nextHeight` if it's my turn, and increase height by 1
560- c .advanceNextHeight (t )
561565 }
566+ timer .Reset (d )
567+ case <- ctx .Done ():
568+ log .WithError (ctx .Err ()).Info ("abort main cycle" )
569+ return
562570 }
563571 }
564572}
@@ -795,7 +803,6 @@ func (c *Chain) stat() {
795803 "branch" : buff ,
796804 }).Info ("runtime state" )
797805 }
798- return
799806}
800807
801808func (c * Chain ) applyBlock (bl * types.BPBlock ) (err error ) {
@@ -814,7 +821,7 @@ func (c *Chain) applyBlock(bl *types.BPBlock) (err error) {
814821
815822 for i , v := range c .branches {
816823 // Grow a branch
817- if v .head .hash .IsEqual (& bl .SignedHeader . ParentHash ) {
824+ if v .head .hash .IsEqual (bl .ParentHash () ) {
818825 head = newBlockNode (height , bl , v .head )
819826 if br , ierr = v .applyBlock (head ); ierr != nil {
820827 err = errors .Wrapf (ierr , "failed to apply block %s" , head .hash .Short (4 ))
0 commit comments