aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorobscuren <geffobscura@gmail.com>2015-04-20 18:58:17 +0800
committerobscuren <geffobscura@gmail.com>2015-04-20 18:58:17 +0800
commit97a9753f87cce4f0072feba63f2e137138a16063 (patch)
tree2dd6b3a49fde192a93cafeabc6c95f5c3e250550
parent45da3e17e2ada226d7c82db667d263f8e004cbfc (diff)
downloaddexon-97a9753f87cce4f0072feba63f2e137138a16063.tar
dexon-97a9753f87cce4f0072feba63f2e137138a16063.tar.gz
dexon-97a9753f87cce4f0072feba63f2e137138a16063.tar.bz2
dexon-97a9753f87cce4f0072feba63f2e137138a16063.tar.lz
dexon-97a9753f87cce4f0072feba63f2e137138a16063.tar.xz
dexon-97a9753f87cce4f0072feba63f2e137138a16063.tar.zst
dexon-97a9753f87cce4f0072feba63f2e137138a16063.zip
core: added chain reset mechanism on bad blocks
-rw-r--r--core/blocks.go7
-rw-r--r--core/chain_manager.go35
2 files changed, 30 insertions, 12 deletions
diff --git a/core/blocks.go b/core/blocks.go
new file mode 100644
index 000000000..b26e8f6ee
--- /dev/null
+++ b/core/blocks.go
@@ -0,0 +1,7 @@
+package core
+
+import "github.com/ethereum/go-ethereum/common"
+
+var badHashes = []common.Hash{
+ common.HexToHash("f269c503aed286caaa0d114d6a5320e70abbc2febe37953207e76a2873f2ba79"),
+}
diff --git a/core/chain_manager.go b/core/chain_manager.go
index d312f2495..dcd8df28f 100644
--- a/core/chain_manager.go
+++ b/core/chain_manager.go
@@ -97,6 +97,21 @@ type ChainManager struct {
func NewChainManager(blockDb, stateDb common.Database, mux *event.TypeMux) *ChainManager {
bc := &ChainManager{blockDb: blockDb, stateDb: stateDb, genesisBlock: GenesisBlock(stateDb), eventMux: mux, quit: make(chan struct{}), cache: NewBlockCache(blockCacheLimit)}
bc.setLastBlock()
+
+ // Check the current state of the block hashes and make sure that we do not have any of the bad blocks in our chain
+ for _, hash := range badHashes {
+ if block := bc.GetBlock(hash); block != nil {
+ glog.V(logger.Error).Infof("Found bad hash. Reorganising chain to state %x\n", block.ParentHash().Bytes()[:4])
+ block = bc.GetBlock(block.ParentHash())
+ if block == nil {
+ glog.Fatal("Unable to complete. Parent block not found. Corrupted DB?")
+ }
+ bc.SetHead(block)
+
+ glog.V(logger.Error).Infoln("Chain reorg was successfull. Resuming normal operation")
+ }
+ }
+
bc.transState = bc.State().Copy()
// Take ownership of this particular state
bc.txState = state.ManageState(bc.State().Copy())
@@ -109,27 +124,23 @@ func NewChainManager(blockDb, stateDb common.Database, mux *event.TypeMux) *Chai
return bc
}
-func (bc *ChainManager) SetHead(block *types.Block) {
+func (bc *ChainManager) SetHead(head *types.Block) {
bc.mu.Lock()
defer bc.mu.Unlock()
- for block := bc.currentBlock; block != nil && block.Hash() != block.Hash(); block = bc.GetBlock(block.Header().ParentHash) {
+ for block := bc.currentBlock; block != nil && block.Hash() != head.Hash(); block = bc.GetBlock(block.Header().ParentHash) {
bc.removeBlock(block)
}
- if bc.cache == nil {
- bc.cache = NewBlockCache(blockCacheLimit)
- }
-
- bc.currentBlock = block
+ bc.cache = NewBlockCache(blockCacheLimit)
+ bc.currentBlock = head
bc.makeCache()
- statedb := state.New(block.Root(), bc.stateDb)
+ statedb := state.New(head.Root(), bc.stateDb)
bc.txState = state.ManageState(statedb)
bc.transState = statedb.Copy()
- bc.setTotalDifficulty(block.Td)
- bc.setLastBlock()
- bc.insert(block)
+ bc.setTotalDifficulty(head.Td)
+ bc.insert(head)
bc.setLastBlock()
}
@@ -510,7 +521,7 @@ func (self *ChainManager) InsertChain(chain types.Blocks) error {
h := block.Header()
- glog.V(logger.Error).Infof("INVALID block #%v (%x)\n", h.Number, h.Hash().Bytes()[:4])
+ glog.V(logger.Error).Infof("INVALID block #%v (%x)\n", h.Number, h.Hash().Bytes())
glog.V(logger.Error).Infoln(err)
glog.V(logger.Debug).Infoln(block)