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

net, test: invalid p2p messages and test framework improvements #19272

Merged
merged 5 commits into from
Jun 24, 2020

Conversation

jonatack
Copy link
Member

@jonatack jonatack commented Jun 14, 2020

...seen while reviewing #19264, #19252, #19304 and #19107:

in net_processing.cpp

  • make the debug logging for oversized message size misbehavior the same for addr, getdata, headers and inv messages

in p2p_invalid_messages

  • add missing logging
  • improve assertions/message sends, move cleanup disconnections outside the assertion scopes
  • split a slowish 3-part test into 3 order-independent tests
  • add a few p2p constants to the test framework

@DrahtBot DrahtBot added the Tests label Jun 14, 2020
@DrahtBot
Copy link
Contributor

DrahtBot commented Jun 14, 2020

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Conflicts

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.

@troygiorshev
Copy link
Contributor

ACK 6b18bfe

Copy link
Contributor

@troygiorshev troygiorshev left a comment

Choose a reason for hiding this comment

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

Reviewed and ran all tests. Just one suggestion if you plan on making any changes.

test/functional/p2p_invalid_messages.py Outdated Show resolved Hide resolved
@jonatack jonatack force-pushed the improve-p2p_invalid_messages branch from 6b18bfe to beecd6b Compare June 16, 2020 08:08
@jonatack
Copy link
Member Author

Thanks for reviewing @troygiorshev. Took your suggestion and also removed an unnecessary disconnect_p2ps() call in test_oversized_msg().

Copy link
Member

@glozow glozow left a comment

Choose a reason for hiding this comment

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

Concept ACK
Mentioned this elsewhere but my edit is already outdated. Small suggestion for p2p_invalid_messages.py, feel free to ignore:
If you replace the 3 (specifically, test_checksum, test_size, and test_msgtype) self.nodes[0].p2p with conn, all of the subtest disconnect_p2ps can also be removed. The only reason they need it now is to clear p2ps.

@jonatack jonatack force-pushed the improve-p2p_invalid_messages branch from beecd6b to 2ecb8f7 Compare June 17, 2020 10:09
@jonatack
Copy link
Member Author

Thank you for reviewing, @gzhao408. Took your suggestion and credited you as co-author in the commit.

@jnewbery
Copy link
Contributor

I don't think we should be removing the disconnect_p2ps calls at the end of each subtest. Ideally the subtest finishes by leaving the node and fixture in as close a state to they were in originally. See #19304 (comment)

@glozow
Copy link
Member

glozow commented Jun 17, 2020

@jnewbery good point...

Ideally the subtest finishes by leaving the node and fixture in as close a state to they were in originally

In terms of state, I'm thinking state = (1) the test_node.p2ps array and (2) the connections between the node and its peers.

(1) I think it's ok if the array has an extra entry if we're not using the .p2p property? Would you agree? If not, then yeah, we need to put disconnect_p2ps() back 😄 .

(2) For connections: Definitely agree that subtests should disconnect peers to clean up after themselves. Either node.disconnect_p2ps or peer.peer_disconnect Some of the subtests call wait_for_disconnect which makes sure that they are disconnected from the peer's POV (no peer._transport). With #19252,disconnect_p2ps waits for node's POV (not in node.getpeerinfo()).

I'm not sure if disconnected from peer's POV == disconnected from the node's POV. If they're effectively the same, I'd say it's unnecessary to call disconnect_p2ps after wait_for_disconnect.

I might be overthinking this haha, I dug into this when I thought superfluous disconnect_p2ps would cause performance problems 😅 .

@jonatack
Copy link
Member Author

AFAICT the tests are order-independent with this PR, and I checked that this passes when rebased to current master:

+from test_framework.util import assert_equal
  
+    def assert_no_connected_peers(self):
+        assert_equal(self.nodes[0].num_connected_mininodes(), 0)
+
     def run_test(self):
         self.test_magic_bytes()
+        self.assert_no_connected_peers()
         self.test_checksum()
+        self.assert_no_connected_peers()
         self.test_size()
+        self.assert_no_connected_peers()
         self.test_msgtype()
+        self.assert_no_connected_peers()
         self.test_oversized_inv_msg()
+        self.assert_no_connected_peers()
         self.test_oversized_getdata_msg()
+        self.assert_no_connected_peers()
         self.test_oversized_headers_msg()
+        self.assert_no_connected_peers()
         self.test_resource_exhaustion()
+        self.assert_no_connected_peers()

@jonatack jonatack force-pushed the improve-p2p_invalid_messages branch from 2ecb8f7 to 06a275f Compare June 18, 2020 05:12
@jonatack
Copy link
Member Author

jonatack commented Jun 18, 2020

Anyway, removed the removal of disconnect_p2ps, and rebased to master to be sure all is well with the latest version of disconnect_p2ps. Edit: Improved the logging and the assertions with new ideas after reviewing #19107.

@jonatack jonatack force-pushed the improve-p2p_invalid_messages branch from 06a275f to efb9a95 Compare June 18, 2020 10:36
maflcko pushed a commit to maflcko/bitcoin-core that referenced this pull request Jun 18, 2020
… header is split across two buffers

80d4423 Test buffered valid message (Troy Giorshev)

Pull request description:

  This PR is a tweak of bitcoin#19302.  This sends a valid message.

  Additionally, this test includes logging in the same vein as bitcoin#19272.

ACKs for top commit:
  MarcoFalke:
    tested ACK 80d4423 (added an assert(false) to observe deterministic coverage) 🌦
  gzhao408:
    ACK bitcoin@80d4423 👊

Tree-SHA512: 3b1aa5ec480a1661917354788923d64595e2886448c9697ec0606a81293e8b4a4642b2b3cc9afb2206ce6f74e5c6d687308c5ad19cb73c5b354d3071ad8496f8
@jonatack
Copy link
Member Author

@MarcoFalke several PRs on this test have been merged but no approval from you on this one. I think they are good improvements but feel free to close it if you disagree; it will save me from spinning my wheels.

@maflcko
Copy link
Member

maflcko commented Jun 18, 2020

I generally use the conflicts list of DrahtBot to see which pr has the most reviews and is the most ready to merge and then get them in one by one if they have received their ACKs.

Will take another look here. One sec

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.

Fine with me

test/functional/p2p_invalid_messages.py Outdated Show resolved Hide resolved
test/functional/p2p_invalid_messages.py Outdated Show resolved Hide resolved
@jonatack jonatack force-pushed the improve-p2p_invalid_messages branch from efb9a95 to 255fbca Compare June 18, 2020 12:28
@jonatack
Copy link
Member Author

jonatack commented Jun 18, 2020

Rebased for merged #19304, reverted the chain setting, and removed a line left over from the last change (thanks Marco).

-MSG_LIMIT = 4 * 1000 * 1000  # 4MB, per MAX_PROTOCOL_MESSAGE_LENGTH
VALID_DATA_LIMIT = MAX_PROTOCOL_MESSAGE_LENGTH - 5  # Account for the 5-byte length prefix

-        self.setup_clean_chain = False
+        self.setup_clean_chain = True

Copy link
Contributor

@jnewbery jnewbery left a comment

Choose a reason for hiding this comment

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

Nice changes, Jon. I've left a few minor comments inline.

src/net_processing.cpp Outdated Show resolved Hide resolved
test/functional/p2p_invalid_messages.py Outdated Show resolved Hide resolved
test/functional/p2p_invalid_messages.py Outdated Show resolved Hide resolved
@jonatack jonatack force-pushed the improve-p2p_invalid_messages branch from 255fbca to 11c1184 Compare June 18, 2020 16:46
@jonatack jonatack force-pushed the improve-p2p_invalid_messages branch from 11c1184 to ca65836 Compare June 18, 2020 17:25
@jonatack jonatack changed the title test: p2p_invalid_messages and test framework improvements net, test: p2p_invalid_messages and test framework improvements Jun 18, 2020
@jonatack jonatack changed the title net, test: p2p_invalid_messages and test framework improvements net, test: invalid p2p messages and test framework improvements Jun 18, 2020
@troygiorshev
Copy link
Contributor

reACK ca65836

Reviewed and stepped through each test. Verified that none of these changes caused linting errors.

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.

c6e7519647ef8736aaa5d8e7f804d0340a36029a

test/functional/p2p_invalid_messages.py Outdated Show resolved Hide resolved
test/functional/p2p_invalid_messages.py Outdated Show resolved Hide resolved
@jonatack jonatack force-pushed the improve-p2p_invalid_messages branch 2 times, most recently from 0730884 to 98af0f0 Compare June 18, 2020 20:39
@troygiorshev
Copy link
Contributor

reACK 98af0f0

jonatack added 5 commits June 19, 2020 14:14
so that oversized ADDR, GETDATA, HEADERS and INV messages print
the same consistent debug logs.
- call disconnect_p2ps() outside of the assert_debug_log scopes
- send messages directly from the p2p conn rather than via nodes[0].p2p
- add an assertion
@jonatack jonatack force-pushed the improve-p2p_invalid_messages branch from 98af0f0 to 56010f9 Compare June 19, 2020 12:28
@jonatack
Copy link
Member Author

jonatack commented Jun 22, 2020

Rebased, no changes. Thank you for ACKing @troygiorshev! Should be trivial to re-ack with git range-diff 8ef15e8 98af0f0 56010f9

@jonatack jonatack requested a review from maflcko June 23, 2020 04:36
@troygiorshev
Copy link
Contributor

reACK 56010f9

Copy link
Contributor

@rajarshimaitra rajarshimaitra left a comment

Choose a reason for hiding this comment

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

Concept ACK. Nice changes, easy to review. Compiled without issue, verified all unit and functions tests passing.
Below are a nonblocking nit and a question for explanation.

@@ -24,8 +27,7 @@
wait_until,
)

MSG_LIMIT = 4 * 1000 * 1000 # 4MB, per MAX_PROTOCOL_MESSAGE_LENGTH
VALID_DATA_LIMIT = MSG_LIMIT - 5 # Account for the 5-byte length prefix
VALID_DATA_LIMIT = MAX_PROTOCOL_MESSAGE_LENGTH - 5 # Account for the 5-byte length prefix
Copy link
Contributor

Choose a reason for hiding this comment

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

Probably I am missing something, but this 5 bytes message length seems to be coming from ser_string() in messages.py. But is it something that the node also recognizes? It doesn't seem to be part of the p2p protocol.

Copy link
Member Author

Choose a reason for hiding this comment

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

We set VALID_DATA_LIMIT so that the length of the serialised msg_unrecognized message built with it will equal MAX_PROTOCOL_MESSAGE_LENGTH. This is verified at line 153; you can also add the following assertion at line 111

             msg = msg_unrecognized(str_data="d" * (VALID_DATA_LIMIT + 1))
+            assert_equal(len(msg.serialize()), MAX_PROTOCOL_MESSAGE_LENGTH + 1)

or try changing the offset from 5 to 4 or 6.

I've added this assertion and some other ones while testing @troygiorshev's net message headers refactoring and may propose the changes if they look worthwhile to me after a few days' time.

test/functional/p2p_invalid_messages.py Show resolved Hide resolved
@maflcko
Copy link
Member

maflcko commented Jun 24, 2020

ACK 56010f9 🎛

Show signature and timestamp

Signature:

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

ACK 56010f9256 🎛
-----BEGIN PGP SIGNATURE-----

iQGzBAEBCgAdFiEE+rVPoUahrI9sLGYTzit1aX5ppUgFAlwqrYAACgkQzit1aX5p
pUicWwv+KIsi9o22/6uZbUTRM0UsyIPesQTV0ZcUzpXX8Sz27WY/psDv60xlD3+W
eUGgldLLYL1EBH8wWRbyMWsQ4WFQJILWXl6+8Cr2b8Lv9N1NhJ/z+u+FLfHHMlXt
dRxOMXXsytLgH8g3eW8f7zWbH0RaHtre+gcBiYqWAEbzq4i8D8h4lEO8ggA5c9TC
iGgj4ACk04YvOXDTOOgfF4DJQXIn3CuHrh7y55zo5X7+hZp43YEDRaVtKnggLTiM
dXZ3r3p+6i4Wqn2uFiZuw06rJN6PtSPEcBa8T3dmGVHixzPlXV5W3aILjU9TUGSV
tMoysA6J0N/RPZVEL0pucUOjrJPB3l6ULSEnl4anjC9rvURvaWJogqwJ5Fa/73Tq
OAFMkcO5iZod95zCDr6Alt34VI7nsJ//rej6/qpkeOSNc6vhQFbuSPaj5QNo/2Sc
BF2ySsHCMluT0JrkGY8EYvMUJka5cO7D894tRvaudfhn6wWT2mNc/E+95LTsGflG
EWs3YKPd
=Yt/T
-----END PGP SIGNATURE-----

Timestamp of file with hash 937d2e5bea5f5ff38ae343e4664be89319c7ac06458b75aca1a579bbd3abbcd1 -

@maflcko maflcko merged commit 67881de into bitcoin:master Jun 24, 2020
@jonatack jonatack deleted the improve-p2p_invalid_messages branch June 25, 2020 07:13
deadalnix pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request May 17, 2021
…mprovements

Summary:
56010f92564a94b0ca6c008c0e6f74a19fad4a2a test: hoist p2p values to test framework constants (Jon Atack)
75447f0893f9ad9bf83d182b301d139430d8de1c test: improve msg sends and p2p disconnections in p2p_invalid_messages (Jon Atack)
57960192a5362ff1a7b996995332535f4c2a25c3 test: refactor test_large_inv() into 3 tests with common method (Jon Atack)
e2b21d8a597c536a8617408d43958bfe9f98a442 test: add p2p_invalid_messages logging (Jon Atack)
9fa494dc0969c61d5ef33708a08923cca19ce091 net: update misbehavior logging for oversized messages (Jon Atack)

Pull request description:

...seen while reviewing #19264, #19252, #19304 and #19107:

in `net_processing.cpp`
- make the debug logging for oversized message size misbehavior the same for `addr`, `getdata`, `headers` and `inv` messages

in `p2p_invalid_messages`
- add missing logging
- improve assertions/message sends, move cleanup disconnections outside the assertion scopes
- split a slowish 3-part test into 3 order-independent tests
- add a few p2p constants to the test framework

---

Backport of [[bitcoin/bitcoin#19272 | core#19272]]

Test Plan:
  ninja all check check-functional

Reviewers: #bitcoin_abc, PiRK

Reviewed By: #bitcoin_abc, PiRK

Differential Revision: https://reviews.bitcoinabc.org/D9530
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Feb 15, 2022
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.

7 participants