Suhas wrote this test to demonstrate a bug in invalidateblock back in 2015, and at some point between then and now it started passing, so best to just merge it so we don't regress.
Closes #5806 (the original issue in which this test was written)
Suhas wrote this test to demonstrate a bug in invalidateblock back in 2015, and at some point between then and now it started passing, so best to just merge it so we don't regress.
Closes #5806 (the original issue in which this test was written)
Can we turn off these lints? Who cares at all if we have some unused imports in some python tests? This seems entirely useless.
<!--e57a25ab6845829454e8d69fc972939a-->
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.
<!--174a7506f384e20aa4161008e828411d-->
Reviewers, this pull request conflicts with the following ones:
If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.
0 | @@ -0,0 +1,48 @@ 1 | +#!/usr/bin/env python3 2 | +# Copyright (c) 2014 The Bitcoin Core developers 3 | +# Distributed under the MIT software license, see the accompanying 4 | +# file COPYING or http://www.opensource.org/licenses/mit-license.php. 5 | + 6 | +# 7 | +# Test invalidateblock
for p2p, since we already have RPC tests for it(I had to look)
utACK
11 | + 12 | +class InvalidateBlockTest(BitcoinTestFramework): 13 | + def set_test_params(self): 14 | + self.num_nodes = 2 15 | + self.setup_clean_chain = True 16 | + self.extra_args = [['-debug', '-whitelist=127.0.0.1'],['-debug', '-whitelist=127.0.0.1']]
why -debug? It's on by default.
Please comment why -whitelist is required
Because that's what was there before, its just a test, and it doesn't seem to hurt anything.
How can it be there before if this is a new test?
The code was taken, almost verbatim from #5806, its not fresh.
Adding '-whitelist=127.0.0.1' to tests seems to me to make them less realistic, more brittle and harder to maintain if different connection options are added. It also changes p2p behaviour so that it's less obvious that the test is testing what you want to test in a focused way. Does whitelisting interact with how blocks are sync'ed in the event of a block being invalidated? I don't know, and by adding this option you're requiring reviewers to figure that out (or just rubberstamp ACK this PR without understanding the code). So my request is to either remove this or comment on why you've chosen to add this option.
0 | @@ -0,0 +1,48 @@ 1 | +#!/usr/bin/env python3 2 | +# Copyright (c) 2014 The Bitcoin Core developers 3 | +# Distributed under the MIT software license, see the accompanying 4 | +# file COPYING or http://www.opensource.org/licenses/mit-license.php. 5 | + 6 | +#
Prefer to use file docstrings over code comments for the top-level comment
I don't know what a docstrng is.
Here you go: https://www.python.org/dev/peps/pep-0257/
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.
31 | + self.nodes[1].invalidateblock(node1blocks[0]) 32 | + 33 | + if (self.nodes[0].getblockcount() != cnt): 34 | + raise AssertionError("Failed to invalidate initial blocks") 35 | + 36 | + # The test framework is a little dense wrt mining on nodes without wallets -
This comment is wrong. All nodes will generate to a deterministic address (whether wallet is enabled or not).
Other ways to achieve this:
sleep(1) to get a new timestamp (and hence new block hash)setmocktime() to get a new timestamp (and hence new block hash)~ probably not a good idea if you're testing p2p behaviour.also: I'd gently encourage you not to call other contributors' code 'a little dense'
I tried getnewaddress first - sadly this is just an issue for non-wallet nodes. Wallet'd nodes will generate a new deterministic address, but the test framework just hard-codes a bunch. That's totally fine, but it is a bit dense. I'm also a bit dense, and so is lots of code I write, cause its just a hack to get it to work. Didn't mean it at all as an insult, just describing the state of it.
It's not dense. It's how we get tests to work with wallet enabled and disabled.
Can you remove the incorrect comment please?
Fair, likely should not have used the word "dense". I cleared up the comment, though I still think this approach is the easiest?
Suhas wrote this test to demonstrate a bug in invalidateblock back
in 2015, and at some point between then and now it started passing,
so best to just merge it so we don't regress.
Closes #5806 (the original issue in which this test was written)
32 | + 33 | + if (self.nodes[0].getblockcount() != cnt): 34 | + raise AssertionError("Failed to invalidate initial blocks") 35 | + 36 | + # The test framework uses a static per-node address which will generate 37 | + # a deterministic block if we have no wallet.
'if we have no wallet' is inaccurate. All calls to generate in the functional test framework will generate blocks to a hardcoded address.
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.
11 | + 12 | +class InvalidateBlockTest(BitcoinTestFramework): 13 | + def set_test_params(self): 14 | + self.num_nodes = 2 15 | + self.setup_clean_chain = True 16 | + self.extra_args = [[],[]]
This isn't required if you're not adding any extra args.
37 | + # a deterministic block if we have no wallet. 38 | + # Instead, mine on nodes[0], which will use a different hardcoded address 39 | + # than the one we previously used, making this block unique. 40 | + self.nodes[0].generate(17) 41 | + 42 | + print("All blocks generated, trying to sync")
Use self.log.info() rather than print
10 | +from test_framework.test_framework import BitcoinTestFramework 11 | + 12 | +class InvalidateBlockTest(BitcoinTestFramework): 13 | + def set_test_params(self): 14 | + self.num_nodes = 2 15 | + self.setup_clean_chain = True
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.
Getting better. You'll be a python developer soon!
Concept ACK.
<!--cf906140f33d8803c4a75a2196329ecb-->
🐙 This pull request conflicts with the target branch and needs rebase.
This has unaddressed review comments from > 6 months ago. Closing and marking 'up for grabs'. @TheBlueMatt - if you want to pick this up again, let me know and I'll reopen.