Skip to content

Commit edc3160

Browse files
committed
test: Remove duplicate NodeContext hacks
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.
1 parent d52bfc4 commit edc3160

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
{
@@ -80,13 +81,13 @@ class NodeImpl : public Node
8081
}
8182
bool appInitMain() override
8283
{
83-
m_context.chain = MakeChain(m_context);
84-
return AppInitMain(m_context_ref, m_context);
84+
m_context->chain = MakeChain(*m_context);
85+
return AppInitMain(m_context_ref, *m_context);
8586
}
8687
void appShutdown() override
8788
{
88-
Interrupt(m_context);
89-
Shutdown(m_context);
89+
Interrupt(*m_context);
90+
Shutdown(*m_context);
9091
}
9192
void startShutdown() override
9293
{
@@ -107,19 +108,19 @@ class NodeImpl : public Node
107108
StopMapPort();
108109
}
109110
}
110-
void setupServerArgs() override { return SetupServerArgs(m_context); }
111+
void setupServerArgs() override { return SetupServerArgs(*m_context); }
111112
bool getProxy(Network net, proxyType& proxy_info) override { return GetProxy(net, proxy_info); }
112113
size_t getNodeCount(CConnman::NumConnections flags) override
113114
{
114-
return m_context.connman ? m_context.connman->GetNodeCount(flags) : 0;
115+
return m_context->connman ? m_context->connman->GetNodeCount(flags) : 0;
115116
}
116117
bool getNodesStats(NodesStats& stats) override
117118
{
118119
stats.clear();
119120

120-
if (m_context.connman) {
121+
if (m_context->connman) {
121122
std::vector<CNodeStats> stats_temp;
122-
m_context.connman->GetNodeStats(stats_temp);
123+
m_context->connman->GetNodeStats(stats_temp);
123124

124125
stats.reserve(stats_temp.size());
125126
for (auto& node_stats_temp : stats_temp) {
@@ -140,46 +141,46 @@ class NodeImpl : public Node
140141
}
141142
bool getBanned(banmap_t& banmap) override
142143
{
143-
if (m_context.banman) {
144-
m_context.banman->GetBanned(banmap);
144+
if (m_context->banman) {
145+
m_context->banman->GetBanned(banmap);
145146
return true;
146147
}
147148
return false;
148149
}
149150
bool ban(const CNetAddr& net_addr, int64_t ban_time_offset) override
150151
{
151-
if (m_context.banman) {
152-
m_context.banman->Ban(net_addr, ban_time_offset);
152+
if (m_context->banman) {
153+
m_context->banman->Ban(net_addr, ban_time_offset);
153154
return true;
154155
}
155156
return false;
156157
}
157158
bool unban(const CSubNet& ip) override
158159
{
159-
if (m_context.banman) {
160-
m_context.banman->Unban(ip);
160+
if (m_context->banman) {
161+
m_context->banman->Unban(ip);
161162
return true;
162163
}
163164
return false;
164165
}
165166
bool disconnectByAddress(const CNetAddr& net_addr) override
166167
{
167-
if (m_context.connman) {
168-
return m_context.connman->DisconnectNode(net_addr);
168+
if (m_context->connman) {
169+
return m_context->connman->DisconnectNode(net_addr);
169170
}
170171
return false;
171172
}
172173
bool disconnectById(NodeId id) override
173174
{
174-
if (m_context.connman) {
175-
return m_context.connman->DisconnectNode(id);
175+
if (m_context->connman) {
176+
return m_context->connman->DisconnectNode(id);
176177
}
177178
return false;
178179
}
179-
int64_t getTotalBytesRecv() override { return m_context.connman ? m_context.connman->GetTotalBytesRecv() : 0; }
180-
int64_t getTotalBytesSent() override { return m_context.connman ? m_context.connman->GetTotalBytesSent() : 0; }
181-
size_t getMempoolSize() override { return m_context.mempool ? m_context.mempool->size() : 0; }
182-
size_t getMempoolDynamicUsage() override { return m_context.mempool ? m_context.mempool->DynamicMemoryUsage() : 0; }
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; }
183184
bool getHeaderTip(int& height, int64_t& block_time) override
184185
{
185186
LOCK(::cs_main);
@@ -222,11 +223,11 @@ class NodeImpl : public Node
222223
bool getImporting() override { return ::fImporting; }
223224
void setNetworkActive(bool active) override
224225
{
225-
if (m_context.connman) {
226-
m_context.connman->SetNetworkActive(active);
226+
if (m_context->connman) {
227+
m_context->connman->SetNetworkActive(active);
227228
}
228229
}
229-
bool getNetworkActive() override { return m_context.connman && m_context.connman->GetNetworkActive(); }
230+
bool getNetworkActive() override { return m_context->connman && m_context->connman->GetNetworkActive(); }
230231
CFeeRate estimateSmartFee(int num_blocks, bool conservative, int* returned_target = nullptr) override
231232
{
232233
FeeCalculation fee_calc;
@@ -268,20 +269,20 @@ class NodeImpl : public Node
268269
std::vector<std::unique_ptr<Wallet>> getWallets() override
269270
{
270271
std::vector<std::unique_ptr<Wallet>> wallets;
271-
for (auto& client : m_context.chain_clients) {
272+
for (auto& client : m_context->chain_clients) {
272273
auto client_wallets = client->getWallets();
273274
std::move(client_wallets.begin(), client_wallets.end(), std::back_inserter(wallets));
274275
}
275276
return wallets;
276277
}
277278
std::unique_ptr<Wallet> loadWallet(const std::string& name, bilingual_str& error, std::vector<bilingual_str>& warnings) override
278279
{
279-
return MakeWallet(LoadWallet(*m_context.chain, name, error, warnings));
280+
return MakeWallet(LoadWallet(*m_context->chain, name, error, warnings));
280281
}
281282
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
282283
{
283284
std::shared_ptr<CWallet> wallet;
284-
status = CreateWallet(*m_context.chain, passphrase, wallet_creation_flags, name, error, warnings, wallet);
285+
status = CreateWallet(*m_context->chain, passphrase, wallet_creation_flags, name, error, warnings, wallet);
285286
return MakeWallet(wallet);
286287
}
287288
std::unique_ptr<Handler> handleInitMessage(InitMessageFn fn) override
@@ -335,13 +336,22 @@ class NodeImpl : public Node
335336
/* verification progress is unused when a header was received */ 0);
336337
}));
337338
}
338-
NodeContext* context() override { return &m_context; }
339-
NodeContext m_context;
340-
util::Ref m_context_ref{m_context};
339+
NodeContext* context() override { return m_context; }
340+
void setContext(NodeContext* context) override
341+
{
342+
m_context = context;
343+
if (context) {
344+
m_context_ref.Set(*context);
345+
} else {
346+
m_context_ref.Clear();
347+
}
348+
}
349+
NodeContext* m_context{nullptr};
350+
util::Ref m_context_ref;
341351
};
342352

343353
} // namespace
344354

345-
std::unique_ptr<Node> MakeNode() { return MakeUnique<NodeImpl>(); }
355+
std::unique_ptr<Node> MakeNode(NodeContext* context) { return MakeUnique<NodeImpl>(context); }
346356

347357
} // namespace interfaces

src/interfaces/node.h

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

266-
//! Return pointer to internal chain interface, useful for testing.
266+
//! Get and set internal node context. Useful for testing, but not
267+
//! accessible across processes.
267268
virtual NodeContext* context() { return nullptr; }
269+
virtual void setContext(NodeContext* context) { }
268270
};
269271

270272
//! Return implementation of Node interface.
271-
std::unique_ptr<Node> MakeNode();
273+
std::unique_ptr<Node> MakeNode(NodeContext* context = nullptr);
272274

273275
//! Block tip (could be a header or not, depends on the subscribed signal).
274276
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)