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: Add unit testing of node eviction logic #20477

Merged
merged 3 commits into from
Dec 16, 2020

Conversation

practicalswift
Copy link
Contributor

@practicalswift practicalswift commented Nov 24, 2020

Add unit testing of node eviction logic.

Closes #19966.

@practicalswift practicalswift force-pushed the unit-test-node-eviction-logic branch from a989adf to 9b6f891 Compare November 24, 2020 12:48
@practicalswift practicalswift force-pushed the unit-test-node-eviction-logic branch 3 times, most recently from cb12388 to 2c62fb6 Compare November 24, 2020 15:28
@DrahtBot
Copy link
Contributor

DrahtBot commented Nov 24, 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.

@jonatack
Copy link
Member

Thanks for working on adding testing here. I'll try to review this soon.

src/net.cpp Show resolved Hide resolved
@laanwj
Copy link
Member

laanwj commented Dec 3, 2020

Concept ACK. Thanks for adding tests.

I sometimes wonder if it would make sense to move "semi-internal" things that are only exposed externally for unit testing, like in this case "NodeEvictionCandidate" and "SelectNodeToEvict", to a separate set of headers. But not here anyhow.

Copy link
Contributor

@dhruv dhruv left a comment

Choose a reason for hiding this comment

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

utACK 2c62fb6. Thank you, @practicalswift for making the code more testable. Also for exposure to idiomatic modern C++. I will checkout the branch and run soon.

src/test/net_tests.cpp Outdated Show resolved Hide resolved
src/test/net_tests.cpp Outdated Show resolved Hide resolved
src/net.cpp Show resolved Hide resolved
src/net.cpp Outdated Show resolved Hide resolved
src/net.cpp Outdated Show resolved Hide resolved
src/net.cpp Outdated Show resolved Hide resolved
src/net.cpp Outdated Show resolved Hide resolved
src/test/net_tests.cpp Outdated Show resolved Hide resolved
src/test/net_tests.cpp Outdated Show resolved Hide resolved
src/test/net_tests.cpp Outdated Show resolved Hide resolved
src/test/net_tests.cpp Outdated Show resolved Hide resolved
@practicalswift practicalswift force-pushed the unit-test-node-eviction-logic branch from 2c62fb6 to 8da8e88 Compare December 4, 2020 22:40
@maflcko
Copy link
Member

maflcko commented Dec 5, 2020

tsan failue can be ignored or fixed by a rebase

@practicalswift
Copy link
Contributor Author

@MarcoFalke Thanks! Rebased!

@practicalswift practicalswift force-pushed the unit-test-node-eviction-logic branch from 8da8e88 to 071b4cd Compare December 5, 2020 10:05
src/net.cpp Outdated Show resolved Hide resolved
src/net.cpp Outdated Show resolved Hide resolved
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.

Very strong concept ACK. This area of the code is definitely undertested. The structure of the unit tests (passing in a setup lambda function to set the relevant parameters in the eviction candidates) is really nice.

src/test/net_tests.cpp Outdated Show resolved Hide resolved
@practicalswift practicalswift force-pushed the unit-test-node-eviction-logic branch from 071b4cd to 67f3759 Compare December 7, 2020 11:47
@maflcko
Copy link
Member

maflcko commented Dec 7, 2020

needs rebase

@practicalswift practicalswift force-pushed the unit-test-node-eviction-logic branch 2 times, most recently from 4d4639d to ccc8162 Compare December 7, 2020 12:03
@maflcko maflcko changed the title test/net: Add unit testing of node eviction logic net: Add unit testing of node eviction logic Dec 7, 2020
@practicalswift
Copy link
Contributor Author

practicalswift commented Dec 7, 2020

I understand why you'd want to pass by ref, but there's no benefit to passing by rvalue. This code suggests to me that SelectNodeToEvict() is going to keep ownership of vEvictionCandidates and you're using move semantics to avoid a copy, but that's not actually happening here.

I've now reverted to the original version (alternative 1 below), since your suggestion and sipa's suggestion are mutually exclusive :)

I'll let others chime in regarding the choice between the three alternatives:

Alternative 1. Optional<NodeId> SelectNodeToEvict(std::vector<NodeEvictionCandidate>)
Alternative 2. Optional<NodeId> SelectNodeToEvict(std::vector<NodeEvictionCandidate>&)
Alternative 3. Optional<NodeId> SelectNodeToEvict(std::vector<NodeEvictionCandidate>&&)

Trying to summarize the pros/cons:

  • Alternative 1. Simple and no gotchas. Does an extraneous copy.
  • Alternative 2. No extraneous copy. Possibly surprising that a function named SelectNodeToEvict modifies its input.
  • Alternative 3. No extraneous copy. Risk of accidental use-after-moved-from in the future. Possibly confusing (see net: Add unit testing of node eviction logic #20477 (comment)).

Perhaps the members of the review club could chime in. Some questions for the review club: Do you agree with the pros/cons of each alternative? Which alternative do you find easier to review? Which alternative do you prefer? Is it possible to quantify the real-world impact of the extraneous copy in a theoretical worst-case scenario?

@jonatack
Copy link
Member

jonatack commented Dec 11, 2020

Will review properly as soon as my laptop is done with the gitian builds for 3 RCs...until then, it's basically unusable.

@practicalswift
Copy link
Contributor Author

Updated to address @dhruv's feedback. Should hopefully be ready for final review. Please re-review :)

@practicalswift practicalswift force-pushed the unit-test-node-eviction-logic branch from a9cfb77 to 1c9b235 Compare December 15, 2020 15:32
@dhruv
Copy link
Contributor

dhruv commented Dec 15, 2020

ACK 1c9b235

@maflcko
Copy link
Member

maflcko commented Dec 16, 2020

cr ACK 6767d63 🤠

Show signature and timestamp

Signature:

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

cr ACK 6767d63d02cc8a670f3d409db1c78a77fdcb5363 🤠
-----BEGIN PGP SIGNATURE-----

iQGzBAEBCgAdFiEE+rVPoUahrI9sLGYTzit1aX5ppUgFAlwqrYAACgkQzit1aX5p
pUgJLwwAljffgO2Xw1Iez563pRA0ztSBIkuzEBPGf9nYifcbQJsfPMCdLzuu3MPQ
ZgfWPQNmtEuSTNq0R4Hl+XaPmBcoS5LDOTfxJWsQtb+kE0uXVfN1nRu4+d91/Sor
+NT9Ach7YRVvJPOZZc0euxyka0pxLwX3+LdXa1k26gL2teT5knaROnCfSxenrFrO
YIzbBfkn2lm/TMu4FhC2hW+OZon4zfHHCPzJYkQR2FByixLDqIIEOJiOY9ycLJQm
ys9xGzn0lZz0/hw4ApEe2b+lLREn/6YMjPNefo0hWcADAYDbjsvEYBsFm0Z6m5Mw
BXA4lhUPWes5QboZ1LCxrEdxGG10uj8LwIBwESp/KVpZ53w0f9yU+mBQ68bruAHh
5hFAJmriES6q8Ml/q2b3IUq8tXwOBM+2poGR8MtTYV6Obk/3eus2bA33EVsfNzm3
ZXzo/F1yQvXlD1kSDU3Aez1yodeLK3GQNoTKaPIGeIwOM3sPhDhJ6REMYskLM4tt
PxqHvOZz
=Gf1H
-----END PGP SIGNATURE-----

Timestamp of file with hash 23398d20d71a2405d911d0dc99e4fc62b592150f49fa6c17afd6d588c418522c -

@maflcko
Copy link
Member

maflcko commented Dec 16, 2020

cr ACK 1c9b235 👲

Show signature and timestamp

Signature:

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

cr ACK 1c9b235c330fe7b9b71f687ef14cb10fe588172b 👲
-----BEGIN PGP SIGNATURE-----

iQGzBAEBCgAdFiEE+rVPoUahrI9sLGYTzit1aX5ppUgFAlwqrYAACgkQzit1aX5p
pUjQWgv/TnEWYjD/Ws2y2WL23agZIEyXZwlxbLCJTWkydKzBnOrPdP1QOBl3DZ95
1ICN5MfBErZtO825TG8NU2VfCesJRIJKV4996nw7GqpN+KfBlkYHNipYfoF9xdHB
J0MT/97/wsrsWGhYEdwx3ftk0CHMNNDMN++N4BSx3h314jGnuNzV4DiUoz2ea9X3
1IAsitmrqa37xIlLIvdLSwRVDcWQyJ7oQ+NUo2KsSztXJ6XEpZcUAqhfRwuS9Ku4
R3DIJHLXKk8PUEXlnmDWVtfnhuhGW4g3V6ZJdUId7OfUr85H/rGSxSeejqSqxLt/
8hGiRsuftelXPfd7SOAjRW5h6x6NFCJBgcVVOYKzjvjyknyH3it3IrPi9WFwLLWS
98fNiUtiE2SsXOUFXxbbyalCZlQols/pBklibFoPNPhfHCT7ZPvazINGBJwkwxVS
GL1IIAKftgsUJhmiSDy/LHfHwXC2vOL2ipdPLJuR7CPLPj9iBZ2picP4h3zRpSo2
lubdxdSX
=z6dl
-----END PGP SIGNATURE-----

Timestamp of file with hash 3a47dabfa2f12ebc4fbd136dcd7771912bfcd7ed7ce86ba72f473c364fba70a0 -

@maflcko
Copy link
Member

maflcko commented Dec 16, 2020

I ACKed both versions. Let me know which one to merge.

@jnewbery
Copy link
Contributor

code review ACK 1c9b235

Either are fine to merge.

@jonatack
Copy link
Member

wait for meeee 😀

src/net.cpp Show resolved Hide resolved
src/net.cpp Outdated Show resolved Hide resolved
src/test/net_tests.cpp Outdated Show resolved Hide resolved
src/test/net_tests.cpp Outdated Show resolved Hide resolved
@jonatack
Copy link
Member

ACK 1c9b235 modulo a few suggestions

@practicalswift practicalswift force-pushed the unit-test-node-eviction-logic branch from 1c9b235 to fee8823 Compare December 16, 2020 12:00
@practicalswift
Copy link
Contributor Author

@jonatack Thanks for reviewing! Feedback addressed: please re-review :)

@jonatack
Copy link
Member

ACK fee8823

Thanks for adding this nicely done coverage! Looking forward to more.

@maflcko
Copy link
Member

maflcko commented Dec 16, 2020

ACK fee8823 🐼

Show signature and timestamp

Signature:

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

ACK fee88237e03c21bf81f21098e6b89ecfa5327cee 🐼
-----BEGIN PGP SIGNATURE-----

iQGzBAEBCgAdFiEE+rVPoUahrI9sLGYTzit1aX5ppUgFAlwqrYAACgkQzit1aX5p
pUiu/Av/RLQ2/lM0mJ7uTlvDebTgk57QGAdIs4rv+t9X7wUizqNhEjFHU2KC0HS0
hU6QjHZelMuy0IvIRrcT33LCZkczPQbEQ4ZMAV5VZhuRNvAisi1PHxueX5U+oT83
UKS899fiy7VN3RD7g7E/sMDNuGAo1hSV7MgYQMOlCG8dSJ3bbuF2d1jNEYqJQ7oa
MkmdtB0SwvCOtZyvcsVDI8yC8UJ5r2ybgE+bYfMK9BzMUyWQriWmOnoVQBrnwb5j
Zr7g/4pnq04078EuaBVP981P+Pc4pj6Eo63iIinabl74aLU3hsHa0bnUCkpihBSg
stooipOqKXQsOS/jI1XyElhclE3EolEE6+0QIXFpx3H7AlkovPdF/LeeezH0dVDB
hSl7VrdKrTxP7kjSBKBMOiPbzBdVSqo6ktaqhBI7YXlDepR7V54/PlDs7QEcM91r
3iEvD/wbMFJQZHzHEbmCINPXBwdOh9MleFgm0dgTMor7djOpaxgbYq/2Y5Bx4/4K
s1ZuLUUS
=f3JL
-----END PGP SIGNATURE-----

Timestamp of file with hash fb125172e7abd1e8e484ae4f527e5e86e05737d1b1e006310dca8dbb38203afc -

@maflcko maflcko merged commit b440c33 into bitcoin:master Dec 16, 2020
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Dec 17, 2020
laanwj added a commit that referenced this pull request Mar 30, 2021
…eviction protection test coverage

0cca08a Add unit test coverage for our onion peer eviction protection (Jon Atack)
caa21f5 Protect onion+localhost peers in ProtectEvictionCandidatesByRatio() (Jon Atack)
8f1a53e Use EraseLastKElements() throughout SelectNodeToEvict() (Jon Atack)
8b1e156 Add m_inbound_onion to AttemptToEvictConnection() (Jon Atack)
72e30e8 Add unit tests for ProtectEvictionCandidatesByRatio() (Jon Atack)
ca63b53 Use std::unordered_set instead of std::vector in IsEvicted() (Jon Atack)
41f84d5 Move peer eviction tests to a separate test file (Jon Atack)
f126cbd Extract ProtectEvictionCandidatesByRatio from SelectNodeToEvict (Jon Atack)

Pull request description:

  Now that #19991 and #20210 have been merged, we can determine inbound onion peers using `CNode::m_inbound_onion` and add it to the localhost peers protection in `AttemptToEvictConnection`, which was added in #19670 to address issue #19500.

  Update 28 February 2021: I've updated this to follow gmaxwell's suggestion in #20197 (comment).

  This branch now protects up to 1/4 onion peers (connected via our tor control service), if any, sorted by longest uptime. If any (or all) onion slots remain after that operation, they are then allocated to protect localhost peers, or a minimum of 2 localhost peers in the case that no onion slots remain and 2 or more onion peers were protected, sorted as before by longest uptime.

  This patch also adds test coverage for the longest uptime, localhost, and onion peer eviction protection logic to build on the welcome initial unit testing of #20477.

  Suggest reviewing the commits that move code with `colorMoved = dimmed-zebra` and `colorMovedWs = allow-indentation-change`.

  Closes #11537.

ACKs for top commit:
  laanwj:
    Code review ACK 0cca08a
  vasild:
    ACK 0cca08a

Tree-SHA512: 2f5a63f942acaae7882920fc61f0185dcd51da85e5b736df9d1fc72343726dd17da740e02f30fa5dc5eb3b2d8345707aed96031bec143d48a2497a610aa19abd
@practicalswift practicalswift deleted the unit-test-node-eviction-logic branch April 10, 2021 19:43
Fabcien pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Jun 15, 2021
Summary:
```
Onion peers are disadvantaged under our eviction criteria, so prevent
eventual eviction of them in the presence of contention for inbound
slots by reserving some slots for localhost peers (sorted by longest
uptime).

Block-relay-only connections exist as a protection against eclipse
attacks, by creating a path for block propagation that may be unknown to
adversaries. Protect against inbound peer connection slot attacks from
disconnecting such peers by attempting to protect up to 8 peers that are
not relaying transactions but appear to be full-nodes, sorted by recency
of last delivered block.
```

Backport of [[bitcoin/bitcoin#19670 | core#19670]].

Note that tests will be added when porting this PR:
bitcoin/bitcoin#20477

Ref T1611.

Test Plan:
  ninja all check-all

Reviewers: #bitcoin_abc, PiRK

Reviewed By: #bitcoin_abc, PiRK

Maniphest Tasks: T1611

Differential Revision: https://reviews.bitcoinabc.org/D9676
Fabcien pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Jun 15, 2021
Summary:
Backport of [[bitcoin/bitcoin#20477 | core#20477]].

Depends on D9683.

Ref T1611.

Test Plan:
  ninja all check-all

Reviewers: #bitcoin_abc, PiRK

Reviewed By: #bitcoin_abc, PiRK

Maniphest Tasks: T1611

Differential Revision: https://reviews.bitcoinabc.org/D9684
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Aug 16, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add unit tests for eviction logic
10 participants