@@ -834,19 +834,16 @@ func (self *BlockChain) InsertChain(chain types.Blocks) (int, error) {
834834 // faster than direct delivery and requires much less mutex
835835 // acquiring.
836836 var (
837- stats struct { queued , processed , ignored int }
837+ stats = insertStats { startTime : time . Now () }
838838 events = make ([]interface {}, 0 , len (chain ))
839839 coalescedLogs vm.Logs
840- tstart = time .Now ()
841-
842- nonceChecked = make ([]bool , len (chain ))
840+ nonceChecked = make ([]bool , len (chain ))
843841 )
844842
845843 // Start the parallel nonce verifier.
846844 nonceAbort , nonceResults := verifyNoncesFromBlocks (self .pow , chain )
847845 defer close (nonceAbort )
848846
849- txcount := 0
850847 for i , block := range chain {
851848 if atomic .LoadInt32 (& self .procInterrupt ) == 1 {
852849 glog .V (logger .Debug ).Infoln ("Premature abort during block chain processing" )
@@ -941,7 +938,6 @@ func (self *BlockChain) InsertChain(chain types.Blocks) (int, error) {
941938 return i , err
942939 }
943940
944- txcount += len (block .Transactions ())
945941 // write the block to the chain and get the status
946942 status , err := self .WriteBlock (block )
947943 if err != nil {
@@ -976,19 +972,54 @@ func (self *BlockChain) InsertChain(chain types.Blocks) (int, error) {
976972 case SplitStatTy :
977973 events = append (events , ChainSplitEvent {block , logs })
978974 }
975+
979976 stats .processed ++
977+ if glog .V (logger .Info ) {
978+ stats .report (chain , i )
979+ }
980980 }
981981
982- if (stats .queued > 0 || stats .processed > 0 || stats .ignored > 0 ) && bool (glog .V (logger .Info )) {
983- tend := time .Since (tstart )
984- start , end := chain [0 ], chain [len (chain )- 1 ]
985- glog .Infof ("imported %d block(s) (%d queued %d ignored) including %d txs in %v. #%v [%x / %x]\n " , stats .processed , stats .queued , stats .ignored , txcount , tend , end .Number (), start .Hash ().Bytes ()[:4 ], end .Hash ().Bytes ()[:4 ])
986- }
987982 go self .postChainEvents (events , coalescedLogs )
988983
989984 return 0 , nil
990985}
991986
987+ // insertStats tracks and reports on block insertion.
988+ type insertStats struct {
989+ queued , processed , ignored int
990+ lastIndex int
991+ startTime time.Time
992+ }
993+
994+ const (
995+ statsReportLimit = 1024
996+ statsReportTimeLimit = 8 * time .Second
997+ )
998+
999+ // report prints statistics if some number of blocks have been processed
1000+ // or more than a few seconds have passed since the last message.
1001+ func (st * insertStats ) report (chain []* types.Block , index int ) {
1002+ limit := statsReportLimit
1003+ if index == len (chain )- 1 {
1004+ limit = 0 // Always print a message for the last block.
1005+ }
1006+ now := time .Now ()
1007+ duration := now .Sub (st .startTime )
1008+ if duration > statsReportTimeLimit || st .queued > limit || st .processed > limit || st .ignored > limit {
1009+ start , end := chain [st .lastIndex ], chain [index ]
1010+ txcount := countTransactions (chain [st .lastIndex :index ])
1011+ glog .Infof ("imported %d block(s) (%d queued %d ignored) including %d txs in %v. #%v [%x / %x]\n " , st .processed , st .queued , st .ignored , txcount , duration , end .Number (), start .Hash ().Bytes ()[:4 ], end .Hash ().Bytes ()[:4 ])
1012+ * st = insertStats {startTime : now , lastIndex : index }
1013+ }
1014+ }
1015+
1016+ func countTransactions (chain []* types.Block ) (c int ) {
1017+ for _ , b := range chain {
1018+ c += len (b .Transactions ())
1019+ }
1020+ return c
1021+ }
1022+
9921023// reorgs takes two blocks, an old chain and a new chain and will reconstruct the blocks and inserts them
9931024// to be part of the new canonical chain and accumulates potential missing transactions and post an
9941025// event about them
0 commit comments