Skip to content

Commit 4b705b1

Browse files
author
MarcoFalke
committed
Merge #19098: test: Remove duplicate NodeContext hacks
edc3160 test: Remove duplicate NodeContext hacks (Russell Yanofsky) Pull request description: Qt tests currently are currently using two NodeContext structs at the same time, one in interfaces::NodeImpl::m_context, and the other in BasicTestingSetup::m_node, and the tests have hacks transferring state between them. Fix this by getting rid of the NodeImpl::m_context struct and making it a pointer. This way a common BitcoinApplication object can be used for all qt tests, but they can still have their own testing setups. Non-test code is changing but non-test behavior is still the same as before. Motivation for this PR is to be able to remove the "std::move(test.m_node.connman)" and mempool hacks for swapping individual NodeContext members in Qt tests, because followup PR #19099 adds yet another member (wallet_client) that needs to be swapped. After this change, the whole NodeContext struct can be swapped instead of individual members, so the workarounds are less fragile and invasive. ACKs for top commit: MarcoFalke: crACK edc3160 🌮 promag: ACK edc3160. Tree-SHA512: c1650e4127f43a4020304ca7c13b5d9122fb5723aacd8fa1cf855d03c6052fcfb7685810aa2a5ef708561015f0022fecaacbad479295104ca45d2c17579466a4
2 parents 6d85435 + edc3160 commit 4b705b1

File tree

7 files changed

+58
-38
lines changed

7 files changed

+58
-38
lines changed

src/interfaces/node.cpp

Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ namespace {
5656
class NodeImpl : public Node
5757
{
5858
public:
59+
NodeImpl(NodeContext* context) { setContext(context); }
5960
void initError(const bilingual_str& message) override { InitError(message); }
6061
bool parseParameters(int argc, const char* const argv[], std::string& error) override
6162
{
@@ -81,13 +82,13 @@ class NodeImpl : public Node
8182
}
8283
bool appInitMain() override
8384
{
84-
m_context.chain = MakeChain(m_context);
85-
return AppInitMain(m_context_ref, m_context);
85+
m_context->chain = MakeChain(*m_context);
86+
return AppInitMain(m_context_ref, *m_context);
8687
}
8788
void appShutdown() override
8889
{
89-
Interrupt(m_context);
90-
Shutdown(m_context);
90+
Interrupt(*m_context);
91+
Shutdown(*m_context);
9192
}
9293
void startShutdown() override
9394
{
@@ -108,19 +109,19 @@ class NodeImpl : public Node
108109
StopMapPort();
109110
}
110111
}
111-
void setupServerArgs() override { return SetupServerArgs(m_context); }
112+
void setupServerArgs() override { return SetupServerArgs(*m_context); }
112113
bool getProxy(Network net, proxyType& proxy_info) override { return GetProxy(net, proxy_info); }
113114
size_t getNodeCount(CConnman::NumConnections flags) override
114115
{
115-
return m_context.connman ? m_context.connman->GetNodeCount(flags) : 0;
116+
return m_context->connman ? m_context->connman->GetNodeCount(flags) : 0;
116117
}
117118
bool getNodesStats(NodesStats& stats) override
118119
{
119120
stats.clear();
120121

121-
if (m_context.connman) {
122+
if (m_context->connman) {
122123
std::vector<CNodeStats> stats_temp;
123-
m_context.connman->GetNodeStats(stats_temp);
124+
m_context->connman->GetNodeStats(stats_temp);
124125

125126
stats.reserve(stats_temp.size());
126127
for (auto& node_stats_temp : stats_temp) {
@@ -141,46 +142,46 @@ class NodeImpl : public Node
141142
}
142143
bool getBanned(banmap_t& banmap) override
143144
{
144-
if (m_context.banman) {
145-
m_context.banman->GetBanned(banmap);
145+
if (m_context->banman) {
146+
m_context->banman->GetBanned(banmap);
146147
return true;
147148
}
148149
return false;
149150
}
150151
bool ban(const CNetAddr& net_addr, int64_t ban_time_offset) override
151152
{
152-
if (m_context.banman) {
153-
m_context.banman->Ban(net_addr, ban_time_offset);
153+
if (m_context->banman) {
154+
m_context->banman->Ban(net_addr, ban_time_offset);
154155
return true;
155156
}
156157
return false;
157158
}
158159
bool unban(const CSubNet& ip) override
159160
{
160-
if (m_context.banman) {
161-
m_context.banman->Unban(ip);
161+
if (m_context->banman) {
162+
m_context->banman->Unban(ip);
162163
return true;
163164
}
164165
return false;
165166
}
166167
bool disconnectByAddress(const CNetAddr& net_addr) override
167168
{
168-
if (m_context.connman) {
169-
return m_context.connman->DisconnectNode(net_addr);
169+
if (m_context->connman) {
170+
return m_context->connman->DisconnectNode(net_addr);
170171
}
171172
return false;
172173
}
173174
bool disconnectById(NodeId id) override
174175
{
175-
if (m_context.connman) {
176-
return m_context.connman->DisconnectNode(id);
176+
if (m_context->connman) {
177+
return m_context->connman->DisconnectNode(id);
177178
}
178179
return false;
179180
}
180-
int64_t getTotalBytesRecv() override { return m_context.connman ? m_context.connman->GetTotalBytesRecv() : 0; }
181-
int64_t getTotalBytesSent() override { return m_context.connman ? m_context.connman->GetTotalBytesSent() : 0; }
182-
size_t getMempoolSize() override { return m_context.mempool ? m_context.mempool->size() : 0; }
183-
size_t getMempoolDynamicUsage() override { return m_context.mempool ? m_context.mempool->DynamicMemoryUsage() : 0; }
181+
int64_t getTotalBytesRecv() override { return m_context->connman ? m_context->connman->GetTotalBytesRecv() : 0; }
182+
int64_t getTotalBytesSent() override { return m_context->connman ? m_context->connman->GetTotalBytesSent() : 0; }
183+
size_t getMempoolSize() override { return m_context->mempool ? m_context->mempool->size() : 0; }
184+
size_t getMempoolDynamicUsage() override { return m_context->mempool ? m_context->mempool->DynamicMemoryUsage() : 0; }
184185
bool getHeaderTip(int& height, int64_t& block_time) override
185186
{
186187
LOCK(::cs_main);
@@ -223,11 +224,11 @@ class NodeImpl : public Node
223224
bool getImporting() override { return ::fImporting; }
224225
void setNetworkActive(bool active) override
225226
{
226-
if (m_context.connman) {
227-
m_context.connman->SetNetworkActive(active);
227+
if (m_context->connman) {
228+
m_context->connman->SetNetworkActive(active);
228229
}
229230
}
230-
bool getNetworkActive() override { return m_context.connman && m_context.connman->GetNetworkActive(); }
231+
bool getNetworkActive() override { return m_context->connman && m_context->connman->GetNetworkActive(); }
231232
CFeeRate estimateSmartFee(int num_blocks, bool conservative, int* returned_target = nullptr) override
232233
{
233234
FeeCalculation fee_calc;
@@ -269,20 +270,20 @@ class NodeImpl : public Node
269270
std::vector<std::unique_ptr<Wallet>> getWallets() override
270271
{
271272
std::vector<std::unique_ptr<Wallet>> wallets;
272-
for (auto& client : m_context.chain_clients) {
273+
for (auto& client : m_context->chain_clients) {
273274
auto client_wallets = client->getWallets();
274275
std::move(client_wallets.begin(), client_wallets.end(), std::back_inserter(wallets));
275276
}
276277
return wallets;
277278
}
278279
std::unique_ptr<Wallet> loadWallet(const std::string& name, bilingual_str& error, std::vector<bilingual_str>& warnings) override
279280
{
280-
return MakeWallet(LoadWallet(*m_context.chain, name, error, warnings));
281+
return MakeWallet(LoadWallet(*m_context->chain, name, error, warnings));
281282
}
282283
std::unique_ptr<Wallet> createWallet(const SecureString& passphrase, uint64_t wallet_creation_flags, const std::string& name, bilingual_str& error, std::vector<bilingual_str>& warnings, WalletCreationStatus& status) override
283284
{
284285
std::shared_ptr<CWallet> wallet;
285-
status = CreateWallet(*m_context.chain, passphrase, wallet_creation_flags, name, error, warnings, wallet);
286+
status = CreateWallet(*m_context->chain, passphrase, wallet_creation_flags, name, error, warnings, wallet);
286287
return MakeWallet(wallet);
287288
}
288289
std::unique_ptr<Handler> handleInitMessage(InitMessageFn fn) override
@@ -336,13 +337,22 @@ class NodeImpl : public Node
336337
/* verification progress is unused when a header was received */ 0);
337338
}));
338339
}
339-
NodeContext* context() override { return &m_context; }
340-
NodeContext m_context;
341-
util::Ref m_context_ref{m_context};
340+
NodeContext* context() override { return m_context; }
341+
void setContext(NodeContext* context) override
342+
{
343+
m_context = context;
344+
if (context) {
345+
m_context_ref.Set(*context);
346+
} else {
347+
m_context_ref.Clear();
348+
}
349+
}
350+
NodeContext* m_context{nullptr};
351+
util::Ref m_context_ref;
342352
};
343353

344354
} // namespace
345355

346-
std::unique_ptr<Node> MakeNode() { return MakeUnique<NodeImpl>(); }
356+
std::unique_ptr<Node> MakeNode(NodeContext* context) { return MakeUnique<NodeImpl>(context); }
347357

348358
} // namespace interfaces

src/interfaces/node.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,12 +268,14 @@ class Node
268268
std::function<void(SynchronizationState, interfaces::BlockTip tip, double verification_progress)>;
269269
virtual std::unique_ptr<Handler> handleNotifyHeaderTip(NotifyHeaderTipFn fn) = 0;
270270

271-
//! Return pointer to internal chain interface, useful for testing.
271+
//! Get and set internal node context. Useful for testing, but not
272+
//! accessible across processes.
272273
virtual NodeContext* context() { return nullptr; }
274+
virtual void setContext(NodeContext* context) { }
273275
};
274276

275277
//! Return implementation of Node interface.
276-
std::unique_ptr<Node> MakeNode();
278+
std::unique_ptr<Node> MakeNode(NodeContext* context = nullptr);
277279

278280
//! Block tip (could be a header or not, depends on the subscribed signal).
279281
struct BlockTip {

src/qt/bitcoin.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
#include <interfaces/handler.h>
3131
#include <interfaces/node.h>
32+
#include <node/context.h>
3233
#include <noui.h>
3334
#include <uint256.h>
3435
#include <util/system.h>
@@ -430,7 +431,8 @@ int GuiMain(int argc, char* argv[])
430431
SetupEnvironment();
431432
util::ThreadSetInternalName("main");
432433

433-
std::unique_ptr<interfaces::Node> node = interfaces::MakeNode();
434+
NodeContext node_context;
435+
std::unique_ptr<interfaces::Node> node = interfaces::MakeNode(&node_context);
434436

435437
// Subscribe to global signals from core
436438
std::unique_ptr<interfaces::Handler> handler_message_box = node->handleMessageBox(noui_ThreadSafeMessageBox);

src/qt/test/addressbooktests.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <key.h>
1919
#include <key_io.h>
2020
#include <wallet/wallet.h>
21+
#include <walletinitinterface.h>
2122

2223
#include <QApplication>
2324
#include <QTimer>
@@ -59,6 +60,7 @@ void EditAddressAndSubmit(
5960
void TestAddAddressesToSendBook(interfaces::Node& node)
6061
{
6162
TestChain100Setup test;
63+
node.setContext(&test.m_node);
6264
std::shared_ptr<CWallet> wallet = std::make_shared<CWallet>(node.context()->chain.get(), WalletLocation(), CreateMockWalletDatabase());
6365
wallet->SetupLegacyScriptPubKeyMan();
6466
bool firstRun;

src/qt/test/test_main.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ int main(int argc, char* argv[])
5252
BasicTestingSetup dummy{CBaseChainParams::REGTEST};
5353
}
5454

55-
std::unique_ptr<interfaces::Node> node = interfaces::MakeNode();
55+
NodeContext node_context;
56+
std::unique_ptr<interfaces::Node> node = interfaces::MakeNode(&node_context);
5657

5758
bool fInvalid = false;
5859

src/qt/test/wallettests.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,7 @@ void TestGUI(interfaces::Node& node)
138138
for (int i = 0; i < 5; ++i) {
139139
test.CreateAndProcessBlock({}, GetScriptForRawPubKey(test.coinbaseKey.GetPubKey()));
140140
}
141-
node.context()->connman = std::move(test.m_node.connman);
142-
node.context()->mempool = std::move(test.m_node.mempool);
141+
node.setContext(&test.m_node);
143142
std::shared_ptr<CWallet> wallet = std::make_shared<CWallet>(node.context()->chain.get(), WalletLocation(), CreateMockWalletDatabase());
144143
bool firstRun;
145144
wallet->LoadWallet(firstRun);

src/test/util/setup_common.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <consensus/validation.h>
1212
#include <crypto/sha256.h>
1313
#include <init.h>
14+
#include <interfaces/chain.h>
1415
#include <miner.h>
1516
#include <net.h>
1617
#include <net_processing.h>
@@ -32,6 +33,7 @@
3233
#include <util/vector.h>
3334
#include <validation.h>
3435
#include <validationinterface.h>
36+
#include <walletinitinterface.h>
3537

3638
#include <functional>
3739

@@ -104,6 +106,8 @@ BasicTestingSetup::BasicTestingSetup(const std::string& chainName, const std::ve
104106
SetupNetworking();
105107
InitSignatureCache();
106108
InitScriptExecutionCache();
109+
m_node.chain = interfaces::MakeChain(m_node);
110+
g_wallet_init_interface.Construct(m_node);
107111
fCheckBlockIndex = true;
108112
static bool noui_connected = false;
109113
if (!noui_connected) {

0 commit comments

Comments
 (0)