@@ -364,14 +364,12 @@ func (bc *ChainManager) insert(block *types.Block) {
364364func (bc * ChainManager ) write (block * types.Block ) {
365365 tstart := time .Now ()
366366
367- go func () {
368- enc , _ := rlp .EncodeToBytes ((* types .StorageBlock )(block ))
369- key := append (blockHashPre , block .Hash ().Bytes ()... )
370- err := bc .blockDb .Put (key , enc )
371- if err != nil {
372- glog .Fatal ("db write fail:" , err )
373- }
374- }()
367+ enc , _ := rlp .EncodeToBytes ((* types .StorageBlock )(block ))
368+ key := append (blockHashPre , block .Hash ().Bytes ()... )
369+ err := bc .blockDb .Put (key , enc )
370+ if err != nil {
371+ glog .Fatal ("db write fail:" , err )
372+ }
375373
376374 if glog .V (logger .Debug ) {
377375 glog .Infof ("wrote block #%v %s. Took %v\n " , block .Number (), common .PP (block .Hash ().Bytes ()), time .Since (tstart ))
@@ -555,7 +553,8 @@ const (
555553 sideStatTy
556554)
557555
558- func (self * ChainManager ) WriteBlock (block * types.Block ) (status writeStatus , err error ) {
556+ // WriteBlock writes the block to the chain (or pending queue)
557+ func (self * ChainManager ) WriteBlock (block * types.Block , queued bool ) (status writeStatus , err error ) {
559558 self .wg .Add (1 )
560559 defer self .wg .Done ()
561560
@@ -587,11 +586,15 @@ func (self *ChainManager) WriteBlock(block *types.Block) (status writeStatus, er
587586 status = sideStatTy
588587 }
589588
590- // Write block to database. Eventually we'll have to improve on this and throw away blocks that are
591- // not in the canonical chain.
592- self .mu .Lock ()
593- self .enqueueForWrite (block )
594- self .mu .Unlock ()
589+ if queued {
590+ // Write block to database. Eventually we'll have to improve on this and throw away blocks that are
591+ // not in the canonical chain.
592+ self .mu .Lock ()
593+ self .enqueueForWrite (block )
594+ self .mu .Unlock ()
595+ } else {
596+ self .write (block )
597+ }
595598 // Delete from future blocks
596599 self .futureBlocks .Remove (block .Hash ())
597600
@@ -693,7 +696,7 @@ func (self *ChainManager) InsertChain(chain types.Blocks) (int, error) {
693696 txcount += len (block .Transactions ())
694697
695698 // write the block to the chain and get the status
696- status , err := self .WriteBlock (block )
699+ status , err := self .WriteBlock (block , true )
697700 if err != nil {
698701 return i , err
699702 }
0 commit comments