Skip to content

Commit 8db11dd

Browse files
committed
Pass chain and client variables where needed
This commit does not change behavior. All it does is pass new function parameters. It is easiest to review this change with: git log -p -n1 -U0 --word-diff-regex=.
1 parent 7e2e62c commit 8db11dd

24 files changed

+138
-63
lines changed

src/bench/coin_selection.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

55
#include <bench/bench.h>
6+
#include <interfaces/chain.h>
67
#include <wallet/wallet.h>
78
#include <wallet/coinselection.h>
89

@@ -33,7 +34,8 @@ static void addCoin(const CAmount& nValue, const CWallet& wallet, std::vector<Ou
3334
// (https://github.com/bitcoin/bitcoin/issues/7883#issuecomment-224807484)
3435
static void CoinSelection(benchmark::State& state)
3536
{
36-
const CWallet wallet(WalletLocation(), WalletDatabase::CreateDummy());
37+
auto chain = interfaces::MakeChain();
38+
const CWallet wallet(*chain, WalletLocation(), WalletDatabase::CreateDummy());
3739
LOCK(wallet.cs_wallet);
3840

3941
// Add coins.
@@ -57,7 +59,8 @@ static void CoinSelection(benchmark::State& state)
5759
}
5860

5961
typedef std::set<CInputCoin> CoinSet;
60-
static const CWallet testWallet(WalletLocation(), WalletDatabase::CreateDummy());
62+
static auto testChain = interfaces::MakeChain();
63+
static const CWallet testWallet(*testChain, WalletLocation(), WalletDatabase::CreateDummy());
6164
std::vector<std::unique_ptr<CWalletTx>> wtxn;
6265

6366
// Copied from src/wallet/test/coinselector_tests.cpp

src/bitcoind.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <clientversion.h>
1212
#include <compat.h>
1313
#include <fs.h>
14+
#include <interfaces/chain.h>
1415
#include <rpc/server.h>
1516
#include <init.h>
1617
#include <noui.h>
@@ -58,6 +59,9 @@ static void WaitForShutdown()
5859
//
5960
static bool AppInit(int argc, char* argv[])
6061
{
62+
InitInterfaces interfaces;
63+
interfaces.chain = interfaces::MakeChain();
64+
6165
bool fRet = false;
6266

6367
//
@@ -164,7 +168,7 @@ static bool AppInit(int argc, char* argv[])
164168
// If locking the data directory failed, exit immediately
165169
return false;
166170
}
167-
fRet = AppInitMain();
171+
fRet = AppInitMain(interfaces);
168172
}
169173
catch (const std::exception& e) {
170174
PrintExceptionContinue(&e, "AppInit()");
@@ -178,7 +182,7 @@ static bool AppInit(int argc, char* argv[])
178182
} else {
179183
WaitForShutdown();
180184
}
181-
Shutdown();
185+
Shutdown(interfaces);
182186

183187
return fRet;
184188
}

src/dummywallet.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ class DummyWalletInit : public WalletInitInterface {
1515
void AddWalletOptions() const override;
1616
bool ParameterInteraction() const override {return true;}
1717
void RegisterRPC(CRPCTable &) const override {}
18-
bool Verify() const override {return true;}
19-
bool Open() const override {LogPrintf("No wallet support compiled in!\n"); return true;}
18+
bool Verify(interfaces::Chain& chain) const override {return true;}
19+
bool Open(interfaces::Chain& chain) const override {LogPrintf("No wallet support compiled in!\n"); return true;}
2020
void Start(CScheduler& scheduler) const override {}
2121
void Flush() const override {}
2222
void Stop() const override {}

src/init.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include <rpc/server.h>
3333
#include <rpc/register.h>
3434
#include <rpc/blockchain.h>
35+
#include <rpc/util.h>
3536
#include <script/standard.h>
3637
#include <script/sigcache.h>
3738
#include <scheduler.h>
@@ -157,7 +158,7 @@ void Interrupt()
157158
}
158159
}
159160

160-
void Shutdown()
161+
void Shutdown(InitInterfaces& interfaces)
161162
{
162163
LogPrintf("%s: In progress...\n", __func__);
163164
static CCriticalSection cs_Shutdown;
@@ -1158,7 +1159,7 @@ bool AppInitLockDataDirectory()
11581159
return true;
11591160
}
11601161

1161-
bool AppInitMain()
1162+
bool AppInitMain(InitInterfaces& interfaces)
11621163
{
11631164
const CChainParams& chainparams = Params();
11641165
// ********************************************************* Step 4a: application initialization
@@ -1226,6 +1227,7 @@ bool AppInitMain()
12261227
*/
12271228
RegisterAllCoreRPCCommands(tableRPC);
12281229
g_wallet_init_interface.RegisterRPC(tableRPC);
1230+
g_rpc_interfaces = &interfaces;
12291231
#if ENABLE_ZMQ
12301232
RegisterZMQRPCCommands(tableRPC);
12311233
#endif
@@ -1243,7 +1245,7 @@ bool AppInitMain()
12431245
}
12441246

12451247
// ********************************************************* Step 5: verify wallet database integrity
1246-
if (!g_wallet_init_interface.Verify()) return false;
1248+
if (!g_wallet_init_interface.Verify(*interfaces.chain)) return false;
12471249

12481250
// ********************************************************* Step 6: network initialization
12491251
// Note that we absolutely cannot open any actual connections
@@ -1562,7 +1564,7 @@ bool AppInitMain()
15621564
}
15631565

15641566
// ********************************************************* Step 9: load wallet
1565-
if (!g_wallet_init_interface.Open()) return false;
1567+
if (!g_wallet_init_interface.Open(*interfaces.chain)) return false;
15661568

15671569
// ********************************************************* Step 10: data directory maintenance
15681570

src/init.h

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,17 @@
1010
#include <string>
1111
#include <util/system.h>
1212

13-
class CScheduler;
14-
class CWallet;
13+
namespace interfaces {
14+
class Chain;
15+
class ChainClient;
16+
} // namespace interfaces
17+
18+
//! Pointers to interfaces used during init and destroyed on shutdown.
19+
struct InitInterfaces
20+
{
21+
std::unique_ptr<interfaces::Chain> chain;
22+
std::vector<std::unique_ptr<interfaces::ChainClient>> chain_clients;
23+
};
1524

1625
namespace boost
1726
{
@@ -20,7 +29,7 @@ class thread_group;
2029

2130
/** Interrupt threads */
2231
void Interrupt();
23-
void Shutdown();
32+
void Shutdown(InitInterfaces& interfaces);
2433
//!Initialize the logging infrastructure
2534
void InitLogging();
2635
//!Parameter interaction: change current parameters depending on various rules
@@ -54,7 +63,7 @@ bool AppInitLockDataDirectory();
5463
* @note This should only be done after daemonization. Call Shutdown() if this function fails.
5564
* @pre Parameters should be parsed and config file should be read, AppInitLockDataDirectory should have been called.
5665
*/
57-
bool AppInitMain();
66+
bool AppInitMain(InitInterfaces& interfaces);
5867

5968
/**
6069
* Setup the arguments for gArgs

src/interfaces/node.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <chain.h>
1010
#include <chainparams.h>
1111
#include <init.h>
12+
#include <interfaces/chain.h>
1213
#include <interfaces/handler.h>
1314
#include <interfaces/wallet.h>
1415
#include <net.h>
@@ -50,6 +51,8 @@ namespace {
5051

5152
class NodeImpl : public Node
5253
{
54+
public:
55+
NodeImpl() { m_interfaces.chain = MakeChain(); }
5356
bool parseParameters(int argc, const char* const argv[], std::string& error) override
5457
{
5558
return gArgs.ParseParameters(argc, argv, error);
@@ -68,11 +71,11 @@ class NodeImpl : public Node
6871
return AppInitBasicSetup() && AppInitParameterInteraction() && AppInitSanityChecks() &&
6972
AppInitLockDataDirectory();
7073
}
71-
bool appInitMain() override { return AppInitMain(); }
74+
bool appInitMain() override { return AppInitMain(m_interfaces); }
7275
void appShutdown() override
7376
{
7477
Interrupt();
75-
Shutdown();
78+
Shutdown(m_interfaces);
7679
}
7780
void startShutdown() override { StartShutdown(); }
7881
bool shutdownRequested() override { return ShutdownRequested(); }
@@ -291,6 +294,7 @@ class NodeImpl : public Node
291294
GuessVerificationProgress(Params().TxData(), block));
292295
}));
293296
}
297+
InitInterfaces m_interfaces;
294298
};
295299

296300
} // namespace

src/qt/test/addressbooktests.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <qt/test/util.h>
33
#include <test/test_bitcoin.h>
44

5+
#include <interfaces/chain.h>
56
#include <interfaces/node.h>
67
#include <qt/addressbookpage.h>
78
#include <qt/addresstablemodel.h>
@@ -56,7 +57,8 @@ void EditAddressAndSubmit(
5657
void TestAddAddressesToSendBook()
5758
{
5859
TestChain100Setup test;
59-
std::shared_ptr<CWallet> wallet = std::make_shared<CWallet>(WalletLocation(), WalletDatabase::CreateMock());
60+
auto chain = interfaces::MakeChain();
61+
std::shared_ptr<CWallet> wallet = std::make_shared<CWallet>(*chain, WalletLocation(), WalletDatabase::CreateMock());
6062
bool firstRun;
6163
wallet->LoadWallet(firstRun);
6264

src/qt/test/wallettests.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <qt/test/wallettests.h>
22
#include <qt/test/util.h>
33

4+
#include <interfaces/chain.h>
45
#include <interfaces/node.h>
56
#include <base58.h>
67
#include <qt/bitcoinamountfield.h>
@@ -132,7 +133,8 @@ void TestGUI()
132133
for (int i = 0; i < 5; ++i) {
133134
test.CreateAndProcessBlock({}, GetScriptForRawPubKey(test.coinbaseKey.GetPubKey()));
134135
}
135-
std::shared_ptr<CWallet> wallet = std::make_shared<CWallet>(WalletLocation(), WalletDatabase::CreateMock());
136+
auto chain = interfaces::MakeChain();
137+
std::shared_ptr<CWallet> wallet = std::make_shared<CWallet>(*chain, WalletLocation(), WalletDatabase::CreateMock());
136138
bool firstRun;
137139
wallet->LoadWallet(firstRun);
138140
{

src/rpc/rawtransaction.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <consensus/validation.h>
1010
#include <core_io.h>
1111
#include <index/txindex.h>
12+
#include <init.h>
1213
#include <keystore.h>
1314
#include <validation.h>
1415
#include <validationinterface.h>
@@ -20,6 +21,7 @@
2021
#include <primitives/transaction.h>
2122
#include <rpc/rawtransaction.h>
2223
#include <rpc/server.h>
24+
#include <rpc/util.h>
2325
#include <script/script.h>
2426
#include <script/script_error.h>
2527
#include <script/sign.h>
@@ -754,7 +756,7 @@ static UniValue combinerawtransaction(const JSONRPCRequest& request)
754756
return EncodeHexTx(mergedTx);
755757
}
756758

757-
UniValue SignTransaction(CMutableTransaction& mtx, const UniValue& prevTxsUnival, CBasicKeyStore *keystore, bool is_temp_keystore, const UniValue& hashType)
759+
UniValue SignTransaction(interfaces::Chain& chain, CMutableTransaction& mtx, const UniValue& prevTxsUnival, CBasicKeyStore *keystore, bool is_temp_keystore, const UniValue& hashType)
758760
{
759761
// Fetch previous transactions (inputs):
760762
CCoinsView viewDummy;
@@ -969,7 +971,7 @@ static UniValue signrawtransactionwithkey(const JSONRPCRequest& request)
969971
keystore.AddKey(key);
970972
}
971973

972-
return SignTransaction(mtx, request.params[2], &keystore, true, request.params[3]);
974+
return SignTransaction(*g_rpc_interfaces->chain, mtx, request.params[2], &keystore, true, request.params[3]);
973975
}
974976

975977
UniValue signrawtransaction(const JSONRPCRequest& request)

src/rpc/rawtransaction.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@ class CBasicKeyStore;
99
struct CMutableTransaction;
1010
class UniValue;
1111

12+
namespace interfaces {
13+
class Chain;
14+
} // namespace interfaces
15+
1216
/** Sign a transaction with the given keystore and previous transactions */
13-
UniValue SignTransaction(CMutableTransaction& mtx, const UniValue& prevTxs, CBasicKeyStore *keystore, bool tempKeystore, const UniValue& hashType);
17+
UniValue SignTransaction(interfaces::Chain& chain, CMutableTransaction& mtx, const UniValue& prevTxs, CBasicKeyStore *keystore, bool tempKeystore, const UniValue& hashType);
1418

1519
/** Create a transaction from univalue parameters */
1620
CMutableTransaction ConstructTransaction(const UniValue& inputs_in, const UniValue& outputs_in, const UniValue& locktime, const UniValue& rbf);

0 commit comments

Comments
 (0)