-
Notifications
You must be signed in to change notification settings - Fork 38.7k
torcontrol: Remove libevent usage #34158
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
base: master
Are you sure you want to change the base?
Conversation
|
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. Code Coverage & BenchmarksFor details see: https://corecheck.dev/bitcoin/bitcoin/pulls/34158. ReviewsSee the guideline for information on the review process.
If your review is incorrectly listed, please copy-paste ConflictsReviewers, 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. |
|
Concept ACK |
|
concept ACK |
3ec2c0e to
a830323
Compare
|
🚧 At least one of the CI tasks failed. HintsTry to run the tests locally, according to the documentation. However, a CI failure may still
Leave a comment here, if you need help tracking down a confusing failure. |
Heh, I did quite a bit of moving around after I got things working and seems like I picked from the branch where i didn't use it instead of the one where I did at the end. Pushed the right code for it now, using it in Will work on fixing the fuzz test which I completely missed 🙃 That's what's failing the CI. |
|
Concept ACK |
a830323 to
872feec
Compare
|
Concept ACK There's some uncovered new code in the coverage report, https://corecheck.dev/bitcoin/bitcoin/pulls/34158 |
janb84
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Concept ACK 8d6f1b0
PR removes LibEvent usages from TorControl and cleans/modernizes the code a bit where touched.
code review, build and test. I do not have sufficient exp. to say something about the fuzz improvement commit. Small NIT found while doing the code review.
67f9f14 to
4966648
Compare
|
🚧 At least one of the CI tasks failed. HintsTry to run the tests locally, according to the documentation. However, a CI failure may still
Leave a comment here, if you need help tracking down a confusing failure. |
|
Addressed feedback.
The old code was largely uncovered as well but I have looked into it and added a simple functional test that provides some basic end-to-end coverage. I thought about unit tests too but I am not sure they provide much value aside from the functional and fuzz tests. But happy to be convinced otherwise if there are ideas for what they should cover.
I don't know what that is and the screenshot doesn't allow me to see which lines are meant by the comment. Happy to look into them if you can transfer the comments matching to the correct lines here somehow. |
You can see the warnings on https://corecheck.dev/bitcoin/bitcoin/pulls/34158 scroll down the page, above benchmarks, below uncovered included code |
This is a helper struct to parse HTTP messages from data in buffers from sockets. HTTP messages begin with headers which are CRLF-terminated lines (\n or \r\n) followed by an arbitrary amount of body data. Whitespace is trimmed from the field lines but not the body. https://httpwg.org/specs/rfc9110.html#rfc.section.5
4966648 to
122ff0a
Compare
TIL, thanks. I addressed the ones that made sense to me. |
|
🚧 At least one of the CI tasks failed. HintsTry to run the tests locally, according to the documentation. However, a CI failure may still
Leave a comment here, if you need help tracking down a confusing failure. |
122ff0a to
3de90a1
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK 3de90a1
Reviewed each commit, built and ran tests on arm64/macos.
Ran on mainnet with -onlynet=onion and observed tor connections in and out. Monitored bitcoind logs with -debug=tor and tor daemon logs for errors.
I also used wireshark to capture torcontrol packets on port 9051 to compare this branch against release 30.2 and observed identical behavior 👍
Checked the exponential retry backoff by killing the tor daemon process while bitcoind was running. Restarted tor after a few minutes and bitcoind continued as expected.
This is a very nice simplification of torcontrol and also demonstrates how we can replace libevent in bitcoin-cli as well ;-).
Show Signature
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256
ACK 3de90a1159e606585aad32c876b6769d29f46ac1
-----BEGIN PGP SIGNATURE-----
iQIzBAEBCAAdFiEE5hdzzW4BBA4vG9eM5+KYS2KJyToFAmlpS64ACgkQ5+KYS2KJ
yTrIVhAAg03Hk1Hu/lEhqpcr1M1aTaxsyNUha2MOtj8L9JMkxYaylX2LlHDzDACq
g23L43GAECJtaMfSWH500Id9JqtWiNhn2tUhXLi9UEVDsbFsVf0usxDyF3kx2fec
CalOFldTrIV8gFOhmS650lyxt0HfyePQ8yTZBH9jeXLrWoyjOU77cjYGzPC3/wUW
gsVqLFFY5FOVzB789abu0ufsv3J6LrwAqwwxhsJpJ7IKhfrR0orX5IOMD4r0JMD6
7lkU+4es68yyHUHnlZxnlhzZMMjaqxgA9/dglNivdP3glUqiEQ+dGVFtjZ0bepff
gHjtm90wbFn59b+kPsTsiFVrYcBe6VBASljjx9QVxgPF8YW3rY64BIXjtOTNIduV
enaiZgLDqkj8hq6mNSSjcbsnk2uDh+dHUTYNLnOaTjzilRXqiwf1SxFqnSnxDQ6f
BESyS36JvznTXCctkaqMQ5KTFcjAOigLx+sPgaWrM0P0iioSvWaAXK1HICfWCCIZ
RG1LS4b4fIIgdIhDjBgL+Mnfz7SoR3U0vvF1XUr6sN5wJVIpDhkzSOB5I9cIMS13
NdbZ9idWLT4JW5sjXg3KZgVV6qADN3fVRvA0xww5B6halGD04qmO+R35nAifkm9h
CYBuURa2xrSWsVTApWwQAM8O901vZicDq09jJqOflkgHzx6soRQ=
=0WeV
-----END PGP SIGNATURE-----
pinheadmz's public key is on openpgp.org
Bitcoin Core client v30.99.0-3de90a1159e6 - server 70016/Satoshi:30.99.0/ - services wl2
<-> type net serv v mping ping send recv txn blk hb addrp addrl age id address version
in onion wl2 2 1325 1325 46 46 1 0 2 127.0.0.1:59473 70016/Satoshi:30.0.0/
out full onion nwcl2 2 931 931 4 0 0 1000 1 0 wffaznxih3nah5tbjpiifbn5v3fzmr5tqzcinacr3cjtnags45owvaqd.onion:8333 70016/Satoshi:30.0.0/
out full onion nbwl2 2 934 934 4 0 0 1001 0 1 uathi7edrzgqsby7dikuqe7ehwpwjem2cy4mpsbahrsrtchjdymufxyd.onion:8333 70016/Satoshi:29.0.0/
out full onion nwl 1 2810 2810 11 0 1000 0 3 t423yolpdj5udh2yyemopmjtenmmwtfx3rxcdtneda5g2rw6isnjo7id.onion:8333 70016/Satoshi:25.0.0/
ms ms sec sec min min min
onion total block
in 1 1
out 3 3 0
total 4 4
Local addresses
zai7kzajktbkqom3edw45zyxnn6e5z5t45d2m7dhmhilgpkz2l3frmad.onion port 8333 score 4
src/torcontrol.cpp
Outdated
| * | ||
| */ | ||
| static std::vector<uint8_t> ComputeResponse(const std::string &key, const std::vector<uint8_t> &cookie, const std::vector<uint8_t> &clientNonce, const std::vector<uint8_t> &serverNonce) | ||
| static std::vector<uint8_t> ComputeResponse(const std::string &key, const std::vector<uint8_t> &cookie, const std::vector<uint8_t> &client_nonce, const std::vector<uint8_t> &serverNonce) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why change client_nonce but not serverNonce? I'm guessing because there is a m_client_nonce at the call site, but the member property isn't being explicitly handled here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I was initially focussing on the member variables in order to not let this get out of hand but the param here was touched too by accident. Doesn't seem too bad to change the serverNonce too as well as the other local variables in this function, so taking care of all of them now.
src/torcontrol.cpp
Outdated
| if (m_recv_buffer.size() > MAX_LINE_LENGTH) { | ||
| LogWarning("tor: Disconnecting because MAX_LINE_LENGTH exceeded"); | ||
| return false; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We won't ever get multiple lines in the receive buffer?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, that's a good catch. If the processing is too slow this could fail unnecessarily and LineReader should catch this already fine, so I am removing this.
| // TODO: Maybe could give this as an option to LineReader instead | ||
| auto last_newline = std::find(m_recv_buffer.rbegin(), m_recv_buffer.rend(), std::byte{'\n'}); | ||
| if (last_newline == m_recv_buffer.rend()) return true; | ||
| size_t complete = last_newline.base() - m_recv_buffer.begin(); | ||
| util::LineReader reader({m_recv_buffer.data(), complete}, MAX_LINE_LENGTH); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm still learning how torcontrol protocol works all together, but without complete context it seems to me that all this could be simplified:
util::LineReader reader(m_recv_buffer, MAX_LINE_LENGTH);You shouldn't have to find '\n', that's LineReader's job ...?
torcontrol unit and functional tests still pass like this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The idea here is that we may receive an incomplete line in the stream just because of packets arriving that way and if we would ask the LineReader for it we would receive it but couldn't do anything with it. That's why I am looking for the last \n and then only process the buffer up until that point. But this is something that could be done by LineReader, too. So maybe there could be a bool param and then LineReader would only return the lines that end with \n but I didn't really spend time making up my mind if that is so much better. It looks cleaner here but this functionality would only have one user, so I don't know... Either way I will wait until LineReader is merged and then I might try to add the functionality there if you think it's better that way.
There should be a test for this though, currently the tests all send the data nice and complete.
3de90a1 to
d3ce644
Compare
Replace libevent-based approach with using the Sock class and CThreadInterrupt.
Gets rid of the Dummy class and adds coverage of get_socks_cb.
d3ce644 to
2808981
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed feedback from @pinheadmz , thanks for the review!
EDIT: And also, I had actually lost sight of bitcoin-cli, I will look at that next to see what, if any, could be reusable from here to apply there.
| // TODO: Maybe could give this as an option to LineReader instead | ||
| auto last_newline = std::find(m_recv_buffer.rbegin(), m_recv_buffer.rend(), std::byte{'\n'}); | ||
| if (last_newline == m_recv_buffer.rend()) return true; | ||
| size_t complete = last_newline.base() - m_recv_buffer.begin(); | ||
| util::LineReader reader({m_recv_buffer.data(), complete}, MAX_LINE_LENGTH); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The idea here is that we may receive an incomplete line in the stream just because of packets arriving that way and if we would ask the LineReader for it we would receive it but couldn't do anything with it. That's why I am looking for the last \n and then only process the buffer up until that point. But this is something that could be done by LineReader, too. So maybe there could be a bool param and then LineReader would only return the lines that end with \n but I didn't really spend time making up my mind if that is so much better. It looks cleaner here but this functionality would only have one user, so I don't know... Either way I will wait until LineReader is merged and then I might try to add the functionality there if you think it's better that way.
There should be a test for this though, currently the tests all send the data nice and complete.
src/torcontrol.cpp
Outdated
| if (m_recv_buffer.size() > MAX_LINE_LENGTH) { | ||
| LogWarning("tor: Disconnecting because MAX_LINE_LENGTH exceeded"); | ||
| return false; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, that's a good catch. If the processing is too slow this could fail unnecessarily and LineReader should catch this already fine, so I am removing this.
src/torcontrol.cpp
Outdated
| * | ||
| */ | ||
| static std::vector<uint8_t> ComputeResponse(const std::string &key, const std::vector<uint8_t> &cookie, const std::vector<uint8_t> &clientNonce, const std::vector<uint8_t> &serverNonce) | ||
| static std::vector<uint8_t> ComputeResponse(const std::string &key, const std::vector<uint8_t> &cookie, const std::vector<uint8_t> &client_nonce, const std::vector<uint8_t> &serverNonce) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I was initially focussing on the member variables in order to not let this get out of hand but the param here was touched too by accident. Doesn't seem too bad to change the serverNonce too as well as the other local variables in this function, so taking care of all of them now.
This is part of the effort to remove the libevent dependency from our code base: #31194
There is a dependency on #32061 but it only really needs one commit which is cherry-picked here in first position (add
LineReader). I hope that a first chunk of that PR can be sliced off and reviewed independently so this PR here is not blocked by it.The current approach tries to reuse existing code and follows roughly similar design decisions. It replaces the libevent-based async I/O with blocking I/O utilizing the existing
SockandCThreadInterrupt. The controller runs in a dedicated thread.There are some optional code modernizations thrown in made along the way (namings, constexpr etc.). These are not strictly necessary but make the end result with the new code more consistent.