Skip to content
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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
48 changes: 48 additions & 0 deletions test/functional/p2p_post_invalidate_sync.py
@@ -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.

#
Copy link
Contributor

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

Copy link
Contributor Author

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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

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.

# Test invalidateblock
Copy link
Member

Choose a reason for hiding this comment

The 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
Copy link
Contributor

Choose a reason for hiding this comment

The 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 run_test:

        self.nodes[0].generate(1) # Leave IBD
        self.sync_all()

The test will run more quickly and it'll be clearer to readers what the test is for.

self.extra_args = [[],[]]
Copy link
Contributor

Choose a reason for hiding this comment

The 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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'if we have no wallet' is inaccurate. All calls to generate in the functional test framework will generate blocks to a hardcoded address.

Copy link
Contributor

Choose a reason for hiding this comment

The 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")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use self.log.info() rather than print

self.sync_all()
if (self.nodes[0].getblockcount() != cnt + 17):
raise AssertionError("Failed to sync shorter but valid chain")

if __name__ == '__main__':
InvalidateBlockTest().main()
1 change: 1 addition & 0 deletions test/functional/test_runner.py
Expand Up @@ -169,6 +169,7 @@
'rpc_preciousblock.py',
'wallet_importprunedfunds.py',
'p2p_leak_tx.py',
'p2p_post_invalidate_sync.py',
'rpc_signmessage.py',
'wallet_balance.py',
'feature_nulldummy.py',
Expand Down