Skip to content

Commit d8fe64a

Browse files
committed
core, miner: added queued write to WriteBlock
This fixes an issue with the lru cache not being available when calling WriteBlock. WriteBlock previously always assumed to be called from the InsertChain where the lru cache was always created prior to calling WriteBlock. When being called from the worker this could lead in to a nil pointer exception being thrown and causing database corruption.
1 parent 7625b07 commit d8fe64a

File tree

2 files changed

+19
-16
lines changed

2 files changed

+19
-16
lines changed

core/chain_manager.go

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -364,14 +364,12 @@ func (bc *ChainManager) insert(block *types.Block) {
364364
func (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
}

miner/worker.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ func (self *worker) wait() {
233233
continue
234234
}
235235

236-
_, err := self.chain.WriteBlock(block)
236+
_, err := self.chain.WriteBlock(block, false)
237237
if err != nil {
238238
glog.V(logger.Error).Infoln("error writing block to chain", err)
239239
continue

0 commit comments

Comments
 (0)