|
|
|
@ -658,107 +658,6 @@ func testReorg(t *testing.T, first, second []int64, td int64, full bool, scheme |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Tests that the insertion functions detect banned hashes.
|
|
|
|
|
func TestBadHeaderHashes(t *testing.T) { |
|
|
|
|
testBadHashes(t, false, rawdb.HashScheme) |
|
|
|
|
testBadHashes(t, false, rawdb.PathScheme) |
|
|
|
|
} |
|
|
|
|
func TestBadBlockHashes(t *testing.T) { |
|
|
|
|
testBadHashes(t, true, rawdb.HashScheme) |
|
|
|
|
testBadHashes(t, true, rawdb.PathScheme) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func testBadHashes(t *testing.T, full bool, scheme string) { |
|
|
|
|
// Create a pristine chain and database
|
|
|
|
|
genDb, _, blockchain, err := newCanonical(ethash.NewFaker(), 0, full, scheme) |
|
|
|
|
if err != nil { |
|
|
|
|
t.Fatalf("failed to create pristine chain: %v", err) |
|
|
|
|
} |
|
|
|
|
defer blockchain.Stop() |
|
|
|
|
|
|
|
|
|
// Create a chain, ban a hash and try to import
|
|
|
|
|
if full { |
|
|
|
|
blocks := makeBlockChain(blockchain.chainConfig, blockchain.GetBlockByHash(blockchain.CurrentBlock().Hash()), 3, ethash.NewFaker(), genDb, 10) |
|
|
|
|
|
|
|
|
|
BadHashes[blocks[2].Header().Hash()] = true |
|
|
|
|
defer func() { delete(BadHashes, blocks[2].Header().Hash()) }() |
|
|
|
|
|
|
|
|
|
_, err = blockchain.InsertChain(blocks) |
|
|
|
|
} else { |
|
|
|
|
headers := makeHeaderChain(blockchain.chainConfig, blockchain.CurrentHeader(), 3, ethash.NewFaker(), genDb, 10) |
|
|
|
|
|
|
|
|
|
BadHashes[headers[2].Hash()] = true |
|
|
|
|
defer func() { delete(BadHashes, headers[2].Hash()) }() |
|
|
|
|
|
|
|
|
|
_, err = blockchain.InsertHeaderChain(headers) |
|
|
|
|
} |
|
|
|
|
if !errors.Is(err, ErrBannedHash) { |
|
|
|
|
t.Errorf("error mismatch: have: %v, want: %v", err, ErrBannedHash) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Tests that bad hashes are detected on boot, and the chain rolled back to a
|
|
|
|
|
// good state prior to the bad hash.
|
|
|
|
|
func TestReorgBadHeaderHashes(t *testing.T) { |
|
|
|
|
testReorgBadHashes(t, false, rawdb.HashScheme) |
|
|
|
|
testReorgBadHashes(t, false, rawdb.PathScheme) |
|
|
|
|
} |
|
|
|
|
func TestReorgBadBlockHashes(t *testing.T) { |
|
|
|
|
testReorgBadHashes(t, true, rawdb.HashScheme) |
|
|
|
|
testReorgBadHashes(t, true, rawdb.PathScheme) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func testReorgBadHashes(t *testing.T, full bool, scheme string) { |
|
|
|
|
// Create a pristine chain and database
|
|
|
|
|
genDb, gspec, blockchain, err := newCanonical(ethash.NewFaker(), 0, full, scheme) |
|
|
|
|
if err != nil { |
|
|
|
|
t.Fatalf("failed to create pristine chain: %v", err) |
|
|
|
|
} |
|
|
|
|
// Create a chain, import and ban afterwards
|
|
|
|
|
headers := makeHeaderChain(blockchain.chainConfig, blockchain.CurrentHeader(), 4, ethash.NewFaker(), genDb, 10) |
|
|
|
|
blocks := makeBlockChain(blockchain.chainConfig, blockchain.GetBlockByHash(blockchain.CurrentBlock().Hash()), 4, ethash.NewFaker(), genDb, 10) |
|
|
|
|
|
|
|
|
|
if full { |
|
|
|
|
if _, err = blockchain.InsertChain(blocks); err != nil { |
|
|
|
|
t.Errorf("failed to import blocks: %v", err) |
|
|
|
|
} |
|
|
|
|
if blockchain.CurrentBlock().Hash() != blocks[3].Hash() { |
|
|
|
|
t.Errorf("last block hash mismatch: have: %x, want %x", blockchain.CurrentBlock().Hash(), blocks[3].Header().Hash()) |
|
|
|
|
} |
|
|
|
|
BadHashes[blocks[3].Header().Hash()] = true |
|
|
|
|
defer func() { delete(BadHashes, blocks[3].Header().Hash()) }() |
|
|
|
|
} else { |
|
|
|
|
if _, err = blockchain.InsertHeaderChain(headers); err != nil { |
|
|
|
|
t.Errorf("failed to import headers: %v", err) |
|
|
|
|
} |
|
|
|
|
if blockchain.CurrentHeader().Hash() != headers[3].Hash() { |
|
|
|
|
t.Errorf("last header hash mismatch: have: %x, want %x", blockchain.CurrentHeader().Hash(), headers[3].Hash()) |
|
|
|
|
} |
|
|
|
|
BadHashes[headers[3].Hash()] = true |
|
|
|
|
defer func() { delete(BadHashes, headers[3].Hash()) }() |
|
|
|
|
} |
|
|
|
|
blockchain.Stop() |
|
|
|
|
|
|
|
|
|
// Create a new BlockChain and check that it rolled back the state.
|
|
|
|
|
ncm, err := NewBlockChain(blockchain.db, DefaultCacheConfigWithScheme(scheme), gspec, nil, ethash.NewFaker(), vm.Config{}, nil, nil) |
|
|
|
|
if err != nil { |
|
|
|
|
t.Fatalf("failed to create new chain manager: %v", err) |
|
|
|
|
} |
|
|
|
|
if full { |
|
|
|
|
if ncm.CurrentBlock().Hash() != blocks[2].Header().Hash() { |
|
|
|
|
t.Errorf("last block hash mismatch: have: %x, want %x", ncm.CurrentBlock().Hash(), blocks[2].Header().Hash()) |
|
|
|
|
} |
|
|
|
|
if blocks[2].Header().GasLimit != ncm.GasLimit() { |
|
|
|
|
t.Errorf("last block gasLimit mismatch: have: %d, want %d", ncm.GasLimit(), blocks[2].Header().GasLimit) |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
if ncm.CurrentHeader().Hash() != headers[2].Hash() { |
|
|
|
|
t.Errorf("last header hash mismatch: have: %x, want %x", ncm.CurrentHeader().Hash(), headers[2].Hash()) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
ncm.Stop() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Tests chain insertions in the face of one entity containing an invalid nonce.
|
|
|
|
|
func TestHeadersInsertNonceError(t *testing.T) { |
|
|
|
|
testInsertNonceError(t, false, rawdb.HashScheme) |
|
|
|
|