-
Notifications
You must be signed in to change notification settings - Fork 35.5k
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
Add test for syncing blocks generated after invalidateblock. #17335
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#!/usr/bin/env python3 | ||
# Copyright (c) 2014 The Bitcoin Core developers | ||
# Distributed under the MIT software license, see the accompanying | ||
# file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||
|
||
# | ||
# Test invalidateblock | ||
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. for p2p, since we already have RPC tests for it(I had to look) |
||
# | ||
|
||
from test_framework.test_framework import BitcoinTestFramework | ||
|
||
class InvalidateBlockTest(BitcoinTestFramework): | ||
def set_test_params(self): | ||
self.num_nodes = 2 | ||
self.setup_clean_chain = True | ||
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. If you remove this line, you can also remove the following lines from
The test will run more quickly and it'll be clearer to readers what the test is for. |
||
self.extra_args = [[],[]] | ||
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. This isn't required if you're not adding any extra args. |
||
|
||
def run_test(self): | ||
self.nodes[0].generate(1) # Leave IBD | ||
self.sync_all() | ||
|
||
cnt = self.nodes[0].getblockcount() | ||
|
||
node1blocks = self.nodes[1].generate(18) | ||
|
||
self.sync_all() | ||
if (self.nodes[0].getblockcount() != cnt + 18): | ||
raise AssertionError("Failed to sync initial blocks") | ||
|
||
self.nodes[0].invalidateblock(node1blocks[0]) | ||
self.nodes[1].invalidateblock(node1blocks[0]) | ||
|
||
if (self.nodes[0].getblockcount() != cnt): | ||
raise AssertionError("Failed to invalidate initial blocks") | ||
|
||
# The test framework uses a static per-node address which will generate | ||
# a deterministic block if we have no wallet. | ||
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. 'if we have no wallet' is inaccurate. All calls to 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. I think it's slightly clearer if all blocks are generated on node0 and sync'ed to node1. It makes it more obvious to someone reading the test that this is testing node1 being able to resync to an less-work chain if it previously invalidated a different chain. |
||
# Instead, mine on nodes[0], which will use a different hardcoded address | ||
# than the one we previously used, making this block unique. | ||
self.nodes[0].generate(17) | ||
|
||
print("All blocks generated, trying to sync") | ||
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. Use |
||
self.sync_all() | ||
if (self.nodes[0].getblockcount() != cnt + 17): | ||
raise AssertionError("Failed to sync shorter but valid chain") | ||
|
||
if __name__ == '__main__': | ||
InvalidateBlockTest().main() |
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.
Prefer to use file docstrings over code comments for the top-level comment
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.
I don't know what a docstrng is.
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.
Here you go: https://www.python.org/dev/peps/pep-0257/
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.
Take a look at the other tests, particularly https://github.com/bitcoin/bitcoin/blob/master/test/functional/example_test.py#L5 and https://github.com/bitcoin/bitcoin/blob/master/test/functional/README.md#style-guidelines. Documenting what the test is supposed to be testing and why is a courtesy to any other developer who tries to understand this in future. "Test invalidateblock" doesn't explain what this test is for.