@@ -364,14 +364,12 @@ func (bc *ChainManager) insert(block *types.Block) {
364
364
func (bc * ChainManager ) write (block * types.Block ) {
365
365
tstart := time .Now ()
366
366
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
+ }
375
373
376
374
if glog .V (logger .Debug ) {
377
375
glog .Infof ("wrote block #%v %s. Took %v\n " , block .Number (), common .PP (block .Hash ().Bytes ()), time .Since (tstart ))
@@ -555,7 +553,8 @@ const (
555
553
sideStatTy
556
554
)
557
555
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 ) {
559
558
self .wg .Add (1 )
560
559
defer self .wg .Done ()
561
560
@@ -587,11 +586,15 @@ func (self *ChainManager) WriteBlock(block *types.Block) (status writeStatus, er
587
586
status = sideStatTy
588
587
}
589
588
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
+ }
595
598
// Delete from future blocks
596
599
self .futureBlocks .Remove (block .Hash ())
597
600
@@ -693,7 +696,7 @@ func (self *ChainManager) InsertChain(chain types.Blocks) (int, error) {
693
696
txcount += len (block .Transactions ())
694
697
695
698
// write the block to the chain and get the status
696
- status , err := self .WriteBlock (block )
699
+ status , err := self .WriteBlock (block , true )
697
700
if err != nil {
698
701
return i , err
699
702
}
0 commit comments