DoS fix: add check for wrong block number in chain fork

This commit is contained in:
Gustav Simonsson 2015-05-06 15:24:54 +02:00
parent 323216ed85
commit 8a7c9ca61c
2 changed files with 26 additions and 1 deletions

View file

@ -571,8 +571,15 @@ func (self *ChainManager) InsertChain(chain types.Blocks) (int, error) {
// Compare the TD of the last known block in the canonical chain to make sure it's greater. // Compare the TD of the last known block in the canonical chain to make sure it's greater.
// At this point it's possible that a different chain (fork) becomes the new canonical chain. // At this point it's possible that a different chain (fork) becomes the new canonical chain.
if block.Td.Cmp(self.td) > 0 { if block.Td.Cmp(self.td) > 0 {
//key := append(blockNumPre, block.Number().Bytes()...)
//self.blockDb.Put(key, block.Hash().Bytes())
// Check for chain forks. If H(block.num - 1) != block.parent, we're on a fork and need to do some merging // Check for chain forks. If H(block.num - 1) != block.parent, we're on a fork and need to do some merging
if previous := self.getBlockByNumber(block.NumberU64() - 1); previous.Hash() != block.ParentHash() { previous := self.getBlockByNumber(block.NumberU64() - 1)
if previous == nil {
glog.V(logger.Error).Infof("INVALID block #%v (%x)\n", block.Header().Number, block.Header().Hash().Bytes())
return i, BlockNumberErr
}
if previous.Hash() != block.ParentHash() {
chash := cblock.Hash() chash := cblock.Hash()
hash := block.Hash() hash := block.Hash()

View file

@ -241,6 +241,24 @@ func TestBrokenChain(t *testing.T) {
} }
} }
func TestChainForkWithWrongBlockNum(t *testing.T) {
db, _ := ethdb.NewMemDatabase()
genesis := GenesisBlock(db)
bc := chm(genesis, db)
chain1 := makeChainWithDiff(genesis, []int{1, 2, 3}, 10)
chain2 := makeChainWithDiff(genesis, []int{4, 5}, 11)
chain2[0].Header().Number = big.NewInt(int64(4))
chain2[1].Header().Number = big.NewInt(int64(5))
_, _ = bc.InsertChain(chain1)
_, err := bc.InsertChain(chain2)
if err != BlockNumberErr {
t.Errorf("should have failed with BlockNumberErr")
}
}
func TestChainInsertions(t *testing.T) { func TestChainInsertions(t *testing.T) {
t.Skip() // travil fails. t.Skip() // travil fails.