-
Notifications
You must be signed in to change notification settings - Fork 20.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
core: persist bad blocks #21827
Merged
Merged
core: persist bad blocks #21827
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e928ff0
core: persist bad blocks
rjl493456442 cc44fcb
core, eth, internal: address comments
rjl493456442 9f2c6f7
core/rawdb: add badblocks to inspector
rjl493456442 d80ed4f
core, eth: update
rjl493456442 eb1f3c7
internal: revert
rjl493456442 c165a9d
core, eth: only save 10 bad blocks
rjl493456442 06130b0
core/rawdb: address comments
rjl493456442 a3f247e
core/rawdb: fix
rjl493456442 7b67be6
core: address comments
rjl493456442 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -22,6 +22,7 @@ import ( | |||||||
"fmt" | ||||||||
"io/ioutil" | ||||||||
"math/big" | ||||||||
"math/rand" | ||||||||
"os" | ||||||||
"reflect" | ||||||||
"testing" | ||||||||
|
@@ -188,6 +189,75 @@ func TestPartialBlockStorage(t *testing.T) { | |||||||
} | ||||||||
} | ||||||||
|
||||||||
// Tests block storage and retrieval operations. | ||||||||
func TestBadBlockStorage(t *testing.T) { | ||||||||
db := NewMemoryDatabase() | ||||||||
|
||||||||
// Create a test block to move around the database and make sure it's really new | ||||||||
block := types.NewBlockWithHeader(&types.Header{ | ||||||||
Number: big.NewInt(1), | ||||||||
Extra: []byte("bad block"), | ||||||||
UncleHash: types.EmptyUncleHash, | ||||||||
TxHash: types.EmptyRootHash, | ||||||||
ReceiptHash: types.EmptyRootHash, | ||||||||
}) | ||||||||
if entry := ReadBadBlock(db, block.Hash()); entry != nil { | ||||||||
t.Fatalf("Non existent block returned: %v", entry) | ||||||||
} | ||||||||
// Write and verify the block in the database | ||||||||
WriteBadBlock(db, block) | ||||||||
if entry := ReadBadBlock(db, block.Hash()); entry == nil { | ||||||||
t.Fatalf("Stored block not found") | ||||||||
} else if entry.Hash() != block.Hash() { | ||||||||
t.Fatalf("Retrieved block mismatch: have %v, want %v", entry, block) | ||||||||
} | ||||||||
// Write one more bad block | ||||||||
blockTwo := types.NewBlockWithHeader(&types.Header{ | ||||||||
Number: big.NewInt(2), | ||||||||
Extra: []byte("bad block two"), | ||||||||
UncleHash: types.EmptyUncleHash, | ||||||||
TxHash: types.EmptyRootHash, | ||||||||
ReceiptHash: types.EmptyRootHash, | ||||||||
}) | ||||||||
WriteBadBlock(db, blockTwo) | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Test uniqueness too:
Suggested change
|
||||||||
|
||||||||
// Write the block one again, should be filtered out. | ||||||||
WriteBadBlock(db, block) | ||||||||
badBlocks := ReadAllBadBlocks(db) | ||||||||
if len(badBlocks) != 2 { | ||||||||
t.Fatalf("Failed to load all bad blocks") | ||||||||
} | ||||||||
|
||||||||
// Write a bunch of bad blocks, all the blocks are should sorted | ||||||||
// in reverse order. The extra blocks should be truncated. | ||||||||
for _, n := range rand.Perm(100) { | ||||||||
block := types.NewBlockWithHeader(&types.Header{ | ||||||||
Number: big.NewInt(int64(n)), | ||||||||
Extra: []byte("bad block"), | ||||||||
UncleHash: types.EmptyUncleHash, | ||||||||
TxHash: types.EmptyRootHash, | ||||||||
ReceiptHash: types.EmptyRootHash, | ||||||||
}) | ||||||||
WriteBadBlock(db, block) | ||||||||
} | ||||||||
badBlocks = ReadAllBadBlocks(db) | ||||||||
if len(badBlocks) != badBlockToKeep { | ||||||||
t.Fatalf("The number of persised bad blocks in incorrect %d", len(badBlocks)) | ||||||||
} | ||||||||
for i := 0; i < len(badBlocks)-1; i++ { | ||||||||
if badBlocks[i].NumberU64() < badBlocks[i+1].NumberU64() { | ||||||||
t.Fatalf("The bad blocks are not sorted #[%d](%d) < #[%d](%d)", i, i+1, badBlocks[i].NumberU64(), badBlocks[i+1].NumberU64()) | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
// Delete all bad blocks | ||||||||
DeleteBadBlocks(db) | ||||||||
badBlocks = ReadAllBadBlocks(db) | ||||||||
if len(badBlocks) != 0 { | ||||||||
t.Fatalf("Failed to delete bad blocks") | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
// Tests block total difficulty storage and retrieval operations. | ||||||||
func TestTdStorage(t *testing.T) { | ||||||||
db := NewMemoryDatabase() | ||||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm thinking out loud: the first bad block is usually the most interesting as it contains the chain split. Maybe it's not a great idea to yeet it out
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, but usually all the following blocks will be skipped. So we meet this specific bad block over and over again.