Skip to content

Commit 80572c7

Browse files
committed
Merge bitcoin/bitcoin#34158: torcontrol: Remove libevent usage
1401011 test: Add test for exceeding max line length in torcontrol (Fabian Jahr) 84c1f32 test: Add torcontrol coverage for PoW defense enablement (Fabian Jahr) 7dff9ec test: Add test for partial message handling in torcontrol (Fabian Jahr) 5693833 test: Add simple functional test for torcontrol (Fabian Jahr) 4117b92 fuzz: Improve torcontrol fuzz test (Fabian Jahr) b1869e9 torcontrol: Move tor controller into node context (Fabian Jahr) eae193e torcontrol: Remove libevent usage (Fabian Jahr) 8444efb refactor: Get rid of unnecessary newlines in logs (Fabian Jahr) 6bcb603 refactor: Modernize member variable names in torcontrol (Fabian Jahr) a36591d refactor: Use constexpr in torcontrol where possible (Fabian Jahr) Pull request description: This is part of the effort to remove the libevent dependency from our code base: bitcoin/bitcoin#31194 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 `Sock` and `CThreadInterrupt`. 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. ACKs for top commit: achow101: ACK 1401011 janb84: re ACK 1401011 pinheadmz: ACK 1401011 Tree-SHA512: 167f1d98a634524568cb1d723e7bdb7234bade2c5686586caf2accea58c3308f83a32e0705edc570d6db691ae578a91e474ae4773f126ec2e1619d3adf7df622
2 parents d2844c6 + 1401011 commit 80572c7

File tree

8 files changed

+552
-314
lines changed

8 files changed

+552
-314
lines changed

src/init.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,9 @@ void Interrupt(NodeContext& node)
276276
InterruptHTTPRPC();
277277
InterruptRPC();
278278
InterruptREST();
279-
InterruptTorControl();
279+
if (node.tor_controller) {
280+
node.tor_controller->Interrupt();
281+
}
280282
InterruptMapPort();
281283
if (node.connman)
282284
node.connman->Interrupt();
@@ -319,7 +321,10 @@ void Shutdown(NodeContext& node)
319321
if (node.peerman && node.validation_signals) node.validation_signals->UnregisterValidationInterface(node.peerman.get());
320322
if (node.connman) node.connman->Stop();
321323

322-
StopTorControl();
324+
if (node.tor_controller) {
325+
node.tor_controller->Join();
326+
node.tor_controller.reset();
327+
}
323328

324329
if (node.background_init_thread.joinable()) node.background_init_thread.join();
325330
// After everything has been shut down, but before things get flushed, stop the
@@ -2187,7 +2192,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
21872192
"for the automatically created Tor onion service."),
21882193
onion_service_target.ToStringAddrPort()));
21892194
}
2190-
StartTorControl(onion_service_target);
2195+
node.tor_controller = std::make_unique<TorController>(gArgs.GetArg("-torcontrol", DEFAULT_TOR_CONTROL), onion_service_target);
21912196
}
21922197

21932198
if (connOptions.bind_on_any) {

src/node/context.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <node/warnings.h>
1818
#include <policy/fees/block_policy_estimator.h>
1919
#include <scheduler.h>
20+
#include <torcontrol.h>
2021
#include <txmempool.h>
2122
#include <validation.h>
2223
#include <validationinterface.h>

src/node/context.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class ChainstateManager;
2525
class ECC_Context;
2626
class NetGroupManager;
2727
class PeerManager;
28+
class TorController;
2829
namespace interfaces {
2930
class Chain;
3031
class ChainClient;
@@ -69,6 +70,7 @@ struct NodeContext {
6970
std::unique_ptr<const NetGroupManager> netgroupman;
7071
std::unique_ptr<CBlockPolicyEstimator> fee_estimator;
7172
std::unique_ptr<PeerManager> peerman;
73+
std::unique_ptr<TorController> tor_controller;
7274
std::unique_ptr<ChainstateManager> chainman;
7375
std::unique_ptr<BanMan> banman;
7476
ArgsManager* args{nullptr}; // Currently a raw pointer because the memory is not managed by this struct

src/test/fuzz/torcontrol.cpp

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,6 @@
1212
#include <string>
1313
#include <vector>
1414

15-
class DummyTorControlConnection : public TorControlConnection
16-
{
17-
public:
18-
DummyTorControlConnection() : TorControlConnection{nullptr}
19-
{
20-
}
21-
22-
bool Connect(const std::string&, const ConnectionCB&, const ConnectionCB&)
23-
{
24-
return true;
25-
}
26-
27-
void Disconnect()
28-
{
29-
}
30-
31-
bool Command(const std::string&, const ReplyHandlerCB&)
32-
{
33-
return true;
34-
}
35-
};
36-
3715
void initialize_torcontrol()
3816
{
3917
static const auto testing_setup = MakeNoLogFileContext<>();
@@ -44,6 +22,9 @@ FUZZ_TARGET(torcontrol, .init = initialize_torcontrol)
4422
FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
4523

4624
TorController tor_controller;
25+
CThreadInterrupt interrupt;
26+
TorControlConnection conn{interrupt};
27+
4728
LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10000) {
4829
TorControlReply tor_control_reply;
4930
CallOneOf(
@@ -61,26 +42,26 @@ FUZZ_TARGET(torcontrol, .init = initialize_torcontrol)
6142
tor_control_reply.code = fuzzed_data_provider.ConsumeIntegral<int>();
6243
});
6344
tor_control_reply.lines = ConsumeRandomLengthStringVector(fuzzed_data_provider);
64-
if (tor_control_reply.lines.empty()) {
65-
break;
66-
}
67-
DummyTorControlConnection dummy_tor_control_connection;
45+
6846
CallOneOf(
6947
fuzzed_data_provider,
7048
[&] {
71-
tor_controller.add_onion_cb(dummy_tor_control_connection, tor_control_reply, /*pow_was_enabled=*/true);
49+
tor_controller.add_onion_cb(conn, tor_control_reply, /*pow_was_enabled=*/true);
50+
},
51+
[&] {
52+
tor_controller.add_onion_cb(conn, tor_control_reply, /*pow_was_enabled=*/false);
7253
},
7354
[&] {
74-
tor_controller.add_onion_cb(dummy_tor_control_connection, tor_control_reply, /*pow_was_enabled=*/false);
55+
tor_controller.auth_cb(conn, tor_control_reply);
7556
},
7657
[&] {
77-
tor_controller.auth_cb(dummy_tor_control_connection, tor_control_reply);
58+
tor_controller.authchallenge_cb(conn, tor_control_reply);
7859
},
7960
[&] {
80-
tor_controller.authchallenge_cb(dummy_tor_control_connection, tor_control_reply);
61+
tor_controller.protocolinfo_cb(conn, tor_control_reply);
8162
},
8263
[&] {
83-
tor_controller.protocolinfo_cb(dummy_tor_control_connection, tor_control_reply);
64+
tor_controller.get_socks_cb(conn, tor_control_reply);
8465
});
8566
}
8667
}

0 commit comments

Comments
 (0)