diff --git a/common/db.go b/common/db.go index 60c090cdce..efd3dcbf4e 100644 --- a/common/db.go +++ b/common/db.go @@ -20,6 +20,7 @@ package common type Database interface { Put(key []byte, value []byte) error Get(key []byte) ([]byte, error) + Has(key []byte) bool Delete(key []byte) error Close() Flush() error diff --git a/core/chain_manager.go b/core/chain_manager.go index 1647031b1c..e0a7bd854f 100644 --- a/core/chain_manager.go +++ b/core/chain_manager.go @@ -364,9 +364,7 @@ func (bc *ChainManager) HasBlock(hash common.Hash) bool { if bc.cache.Contains(hash) { return true } - - data, _ := bc.chainDb.Get(append(blockHashPre, hash[:]...)) - return len(data) != 0 + return bc.chainDb.Has(append(blockHashPre, hash[:]...)) } func (self *ChainManager) GetBlockHashesFromHash(hash common.Hash, max uint64) (chain []common.Hash) { @@ -560,6 +558,10 @@ func (self *ChainManager) InsertChain(chain types.Blocks) (int, error) { glog.V(logger.Debug).Infoln("Premature abort during chain processing") break } + if self.HasBlock(block.Hash()) { + stats.ignored++ + continue + } bstart := time.Now() // Wait for block i's nonce to be verified before processing diff --git a/ethdb/database.go b/ethdb/database.go index 9e80e54091..c4efe9fce2 100644 --- a/ethdb/database.go +++ b/ethdb/database.go @@ -138,6 +138,11 @@ func (self *LDBDatabase) Delete(key []byte) error { return self.db.Delete(key, nil) } +func (self *LDBDatabase) Has(key []byte) bool { + b, _ := self.db.Has(key, nil) + return b +} + func (self *LDBDatabase) NewIterator() iterator.Iterator { return self.db.NewIterator(nil, nil) } diff --git a/ethdb/memory_database.go b/ethdb/memory_database.go index 70b03dfade..ba2c485497 100644 --- a/ethdb/memory_database.go +++ b/ethdb/memory_database.go @@ -57,6 +57,11 @@ func (db *MemDatabase) GetKeys() []*common.Key { } */ +func (db *MemDatabase) Has(key []byte) bool { + _, ok := db.db[string(key)] + return ok +} + func (db *MemDatabase) Delete(key []byte) error { delete(db.db, string(key))