Skip to content

Conversation

jnewbery
Copy link
Contributor

p2p_segwit.py is a very long test, composed of multiple subtests. When it fails it's difficult to debug for a couple of reasons:

  • Control flow jumps between different methods in the test class, so it's a little difficult to follow the code.
  • state may be carried forward unintentionally from one subtest to the next.

Improve that by wrapping the subtests with a @subtest decorator which:

  • logs progress
  • asserts state after each subtest

As usual, I've also included a few commits which generally tidy up the test and improve style.

NUM_DROPS = 200 # 201 max ops per script!
NUM_OUTPUTS = 50
num_drops = 200 # 201 max ops per script!
num_outputs = 50
Copy link
Member

Choose a reason for hiding this comment

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

Imo we indicate "compile time" constants with upper case, so I'd prefer if they were left that way, since it makes it easier to read.

Copy link
Member

Choose a reason for hiding this comment

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

C.f. https://github.com/bitcoin/bitcoin/blob/master/doc/developer-notes.md#coding-style

Constant names are all uppercase, and use _ to separate words.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's not fair! Those are C++ style guides.

But fine, reverted to all caps.

CTxIn,
CTxInWitness,
CTxOut,
CTxWitness,
Copy link
Member

Choose a reason for hiding this comment

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

Those are message types and not from mininode. I'd prefer to keep it the way it was over changing it in the wrong way.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for pointing this out. These were previously being imported indirectly through the from mininode import * import, but you're right that I should fix that to import from messages.py in this commit.

I've now put them in the right place.

@DrahtBot
Copy link
Contributor

DrahtBot commented Jun 14, 2018

Note to 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.

@maflcko
Copy link
Member

maflcko commented Jun 21, 2018

Concept ACK, but let's try not to overshoot the target by pleasing some obscure style rules that contradict our style guide.

@jnewbery
Copy link
Contributor Author

Concept ACK, but let's try not to overshoot the target by pleasing some obscure style rules that contradict our style guide.

Sorry - will address your comments. Was travelling last week.

@jnewbery jnewbery force-pushed the tidy_up_p2p_segwit branch from 1b6730c to daad0c4 Compare June 21, 2018 21:51
@jnewbery
Copy link
Contributor Author

Comments addressed. Thanks for the review @MarcoFalke !

@jnewbery jnewbery force-pushed the tidy_up_p2p_segwit branch from daad0c4 to e3aab29 Compare June 29, 2018 19:45
@jnewbery
Copy link
Contributor Author

rebased

Copy link
Contributor

@conscott conscott left a comment

Choose a reason for hiding this comment

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

Tested ACK e3aab29

Nice cleanup.

As a follow up, what do you think about moving these functions back to mininode.py? It seems there are similiar patterns in other p2p tests, like here, which could be refactored.

@jnewbery
Copy link
Contributor Author

jnewbery commented Jul 3, 2018

Thanks @conscott !

what do you think about moving these functions back to mininode.py?

Seems reasonable if they can be used by multiple tests, although blocktools.py might be a better place for them.

@laanwj
Copy link
Member

laanwj commented Jul 5, 2018

utACK e3aab29

@laanwj laanwj merged commit e3aab29 into bitcoin:master Jul 5, 2018
laanwj added a commit that referenced this pull request Jul 5, 2018
e3aab29 [tests] p2p_segwit: sync_blocks in subtest wrapper. (John Newbery)
55e8050 [tests] p2p_segwit: remove unnecessary arguments from subtests. (John Newbery)
25711c2 [tests] p2p_segwit: log and assert segwit status in subtest wrapper. (John Newbery)
6839863 [tests] p2p_segwit: Make sure each subtest leaves utxos for the next. (John Newbery)
bfe3273 [tests] p2p_segwit: wrap subtests with subtest wrapper. (John Newbery)
2af4e39 [tests] p2p_segwit: re-order function definitions. (John Newbery)
94a0134 [tests] p2p_segwit: standardise comments/docstrings. (John Newbery)
f7c7f8e [tests] p2p_segwit: Fix flake8 warnings. (John Newbery)

Pull request description:

  `p2p_segwit.py` is a very long test, composed of multiple subtests. When it fails it's difficult to debug for a couple of reasons:

  - Control flow jumps between different methods in the test class, so it's a little difficult to follow the code.
  - state may be carried forward unintentionally from one subtest to the next.

  Improve that by wrapping the subtests with a `@subtest` decorator which:
  - logs progress
  - asserts state after each subtest

  As usual, I've also included a few commits which generally tidy up the test and improve style.

Tree-SHA512: 3650602b3ce9823dc968cc5f2e716757feadc3dbedb3605eb79bb3df91a6db8ae53431f253b440da690e3a8e9d76de84fad4368a2663aeb40e6b9427cf948870
# Verify that future segwit upgraded transactions are non-standard,
# but valid in blocks. Can run this before and after segwit activation.
def test_segwit_versions(self):
self.log.info("Testing standardness/consensus for segwit versions (0-16)")
assert(len(self.utxo))
NUM_TESTS = 17 # will test OP_0, OP1, ..., OP_16
if (len(self.utxo) < NUM_TESTS):
num_tests = 17 # will test OP_0, OP1, ..., OP_16
Copy link
Member

Choose a reason for hiding this comment

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

in commit f7c7f8e [tests] p2p_segwit: Fix flake8 warnings

You change a constant to lowercase. Imo this makes the code harder to read, as mentioned in my previous review.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sorry - recapitalized the ones that you mentioned in your review. Missed this one.

@jnewbery jnewbery deleted the tidy_up_p2p_segwit branch July 9, 2018 14:34
Copy link
Member

@maflcko maflcko left a comment

Choose a reason for hiding this comment

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

utACK e3aab29

Excellent addition of the @subtest wrapper! In the past I used to splatter around those assertions manually.

Left some comments to prove I actually reviewed the individual commits.

# Ensure that we've tested a situation where we use SIGHASH_SINGLE with
# an input index > number of outputs.
NUM_TESTS = 500
num_tests = 500
Copy link
Member

Choose a reason for hiding this comment

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

same here

Copy link
Contributor Author

@jnewbery jnewbery Jul 13, 2018

Choose a reason for hiding this comment

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

Will address in follow-up PR

def advance_to_segwit_lockin(self):
"""Mine enough blocks to lock in segwit, but don't activate."""
# TODO: we could verify that lockin only happens at the right threshold of
# signalling blocks, rather than just at the right period boundary.
Copy link
Member

Choose a reason for hiding this comment

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

Nit (unrelated):
In commit 94a0134 [tests] p2p_segwit: standardise comments/docstrings.

Could remove this todo, since it should be covered by the BIP9 tests. (No need to re-cover BIP9 logic in every softfork deployment that uses BIP9)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will address in follow-up PR

def advance_to_segwit_active(self):
"""Mine enough blocks to activate segwit."""
# TODO: we could verify that activation only happens at the right threshold
Copy link
Member

Choose a reason for hiding this comment

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

same here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will address in follow-up PR

@@ -41,7 +41,6 @@
from test_framework.mininode import (
P2PInterface,
mininode_lock,
network_thread_start,
Copy link
Member

Choose a reason for hiding this comment

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

in commit 2af4e39 [tests] p2p_segwit: re-order function definitions.

Unrelated change

return

def run_test(self):
# Setup the p2p connections and start up the network thread.
Copy link
Member

Choose a reason for hiding this comment

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

In commit 2af4e39 [tests] p2p_segwit: re-order function definitions.

Unrelated addition: Tests are not aware of a "network thread"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will address in follow-up PR

@@ -220,7 +218,77 @@ def update_witness_block_with_transactions(self, block, tx_list, nonce=0):
block.vtx.extend(tx_list)
add_witness_commitment(block, nonce)
block.solve()
return
Copy link
Member

Choose a reason for hiding this comment

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

In commit 2af4e39 [tests] p2p_segwit: re-order function definitions.

Unrelated non-moveonly removal


def test_standardness_v0(self, segwit_activated):
"""Test V0 txout standardness.

Copy link
Member

Choose a reason for hiding this comment

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

In commit 2af4e39 [tests] p2p_segwit: re-order function definitions.

Unrelated removal of trailing whitespace (should have been in one of the previous commits)


def test_segwit_versions(self):
"""Test validity of future segwit version transactions.

Copy link
Member

Choose a reason for hiding this comment

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

In commit 2af4e39 [tests] p2p_segwit: re-order function definitions.

Unrelated removal of trailing whitespace (should have been in one of the previous commits)

@jnewbery
Copy link
Contributor Author

jnewbery commented Jul 9, 2018

Thanks for the review @MarcoFalke - I'll try to address your comments in a follow-up PR.

maflcko pushed a commit that referenced this pull request Jul 13, 2018
eeeef80 qa: Fix some TODOs in p2p_segwit (MarcoFalke)

Pull request description:

  * I believe we don't need to redundantly test versionbits logic in every functional tests that tests a softfork deployment that is being done with versionbits. Thus, remove two `TODO`s that ask for that.
  * Replace another `TODO` with `wait_until`.
  * Some style fixups after #13467

Tree-SHA512: c7120404d50579d6f3b9092f1e259959190eeafe520231e3479c8c256a50bf7260ccc93f8301ac0e100c54037053f6849433ebb1c55607e01d94b9812e525083
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Sep 8, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants