Skip to content

Commit fbc6bb8

Browse files
committed
bitcoin-wallet tool: Drop MakeChain calls
Pass null Chain interface pointer to CWallet. This is needed to drop libbitcoin_server dependency and avoid linking node code.
1 parent 6a135fb commit fbc6bb8

File tree

9 files changed

+24
-28
lines changed

9 files changed

+24
-28
lines changed

src/bench/coin_selection.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ static void addCoin(const CAmount& nValue, const CWallet& wallet, std::vector<st
2929
static void CoinSelection(benchmark::State& state)
3030
{
3131
auto chain = interfaces::MakeChain();
32-
const CWallet wallet(*chain, WalletLocation(), WalletDatabase::CreateDummy());
32+
const CWallet wallet(chain.get(), WalletLocation(), WalletDatabase::CreateDummy());
3333
std::vector<std::unique_ptr<CWalletTx>> wtxs;
3434
LOCK(wallet.cs_wallet);
3535

@@ -61,7 +61,7 @@ static void CoinSelection(benchmark::State& state)
6161

6262
typedef std::set<CInputCoin> CoinSet;
6363
static auto testChain = interfaces::MakeChain();
64-
static const CWallet testWallet(*testChain, WalletLocation(), WalletDatabase::CreateDummy());
64+
static const CWallet testWallet(testChain.get(), WalletLocation(), WalletDatabase::CreateDummy());
6565
std::vector<std::unique_ptr<CWalletTx>> wtxn;
6666

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

src/qt/test/addressbooktests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ void TestAddAddressesToSendBook()
5858
{
5959
TestChain100Setup test;
6060
auto chain = interfaces::MakeChain();
61-
std::shared_ptr<CWallet> wallet = std::make_shared<CWallet>(*chain, WalletLocation(), WalletDatabase::CreateMock());
61+
std::shared_ptr<CWallet> wallet = std::make_shared<CWallet>(chain.get(), WalletLocation(), WalletDatabase::CreateMock());
6262
bool firstRun;
6363
wallet->LoadWallet(firstRun);
6464

src/qt/test/wallettests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ void TestGUI()
135135
test.CreateAndProcessBlock({}, GetScriptForRawPubKey(test.coinbaseKey.GetPubKey()));
136136
}
137137
auto chain = interfaces::MakeChain();
138-
std::shared_ptr<CWallet> wallet = std::make_shared<CWallet>(*chain, WalletLocation(), WalletDatabase::CreateMock());
138+
std::shared_ptr<CWallet> wallet = std::make_shared<CWallet>(chain.get(), WalletLocation(), WalletDatabase::CreateMock());
139139
bool firstRun;
140140
wallet->LoadWallet(firstRun);
141141
{

src/wallet/test/coinselector_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ typedef std::set<CInputCoin> CoinSet;
2929

3030
static std::vector<COutput> vCoins;
3131
static auto testChain = interfaces::MakeChain();
32-
static CWallet testWallet(*testChain, WalletLocation(), WalletDatabase::CreateDummy());
32+
static CWallet testWallet(testChain.get(), WalletLocation(), WalletDatabase::CreateDummy());
3333
static CAmount balance = 0;
3434

3535
CoinEligibilityFilter filter_standard(1, 6, 0);

src/wallet/test/wallet_test_fixture.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include <wallet/rpcwallet.h>
1010

1111
WalletTestingSetup::WalletTestingSetup(const std::string& chainName):
12-
TestingSetup(chainName), m_wallet(*m_chain, WalletLocation(), WalletDatabase::CreateMock())
12+
TestingSetup(chainName), m_wallet(m_chain.get(), WalletLocation(), WalletDatabase::CreateMock())
1313
{
1414
bool fFirstRun;
1515
m_wallet.LoadWallet(fFirstRun);

src/wallet/test/wallet_tests.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ BOOST_FIXTURE_TEST_CASE(scan_for_wallet_transactions, TestChain100Setup)
4949

5050
// Verify ScanForWalletTransactions accommodates a null start block.
5151
{
52-
CWallet wallet(*chain, WalletLocation(), WalletDatabase::CreateDummy());
52+
CWallet wallet(chain.get(), WalletLocation(), WalletDatabase::CreateDummy());
5353
AddKey(wallet, coinbaseKey);
5454
WalletRescanReserver reserver(&wallet);
5555
reserver.reserve();
@@ -64,7 +64,7 @@ BOOST_FIXTURE_TEST_CASE(scan_for_wallet_transactions, TestChain100Setup)
6464
// Verify ScanForWalletTransactions picks up transactions in both the old
6565
// and new block files.
6666
{
67-
CWallet wallet(*chain, WalletLocation(), WalletDatabase::CreateDummy());
67+
CWallet wallet(chain.get(), WalletLocation(), WalletDatabase::CreateDummy());
6868
AddKey(wallet, coinbaseKey);
6969
WalletRescanReserver reserver(&wallet);
7070
reserver.reserve();
@@ -83,7 +83,7 @@ BOOST_FIXTURE_TEST_CASE(scan_for_wallet_transactions, TestChain100Setup)
8383
// Verify ScanForWalletTransactions only picks transactions in the new block
8484
// file.
8585
{
86-
CWallet wallet(*chain, WalletLocation(), WalletDatabase::CreateDummy());
86+
CWallet wallet(chain.get(), WalletLocation(), WalletDatabase::CreateDummy());
8787
AddKey(wallet, coinbaseKey);
8888
WalletRescanReserver reserver(&wallet);
8989
reserver.reserve();
@@ -101,7 +101,7 @@ BOOST_FIXTURE_TEST_CASE(scan_for_wallet_transactions, TestChain100Setup)
101101

102102
// Verify ScanForWalletTransactions scans no blocks.
103103
{
104-
CWallet wallet(*chain, WalletLocation(), WalletDatabase::CreateDummy());
104+
CWallet wallet(chain.get(), WalletLocation(), WalletDatabase::CreateDummy());
105105
AddKey(wallet, coinbaseKey);
106106
WalletRescanReserver reserver(&wallet);
107107
reserver.reserve();
@@ -135,7 +135,7 @@ BOOST_FIXTURE_TEST_CASE(importmulti_rescan, TestChain100Setup)
135135
// before the missing block, and success for a key whose creation time is
136136
// after.
137137
{
138-
std::shared_ptr<CWallet> wallet = std::make_shared<CWallet>(*chain, WalletLocation(), WalletDatabase::CreateDummy());
138+
std::shared_ptr<CWallet> wallet = std::make_shared<CWallet>(chain.get(), WalletLocation(), WalletDatabase::CreateDummy());
139139
AddWallet(wallet);
140140
UniValue keys;
141141
keys.setArray();
@@ -198,7 +198,7 @@ BOOST_FIXTURE_TEST_CASE(importwallet_rescan, TestChain100Setup)
198198

199199
// Import key into wallet and call dumpwallet to create backup file.
200200
{
201-
std::shared_ptr<CWallet> wallet = std::make_shared<CWallet>(*chain, WalletLocation(), WalletDatabase::CreateDummy());
201+
std::shared_ptr<CWallet> wallet = std::make_shared<CWallet>(chain.get(), WalletLocation(), WalletDatabase::CreateDummy());
202202
LOCK(wallet->cs_wallet);
203203
wallet->mapKeyMetadata[coinbaseKey.GetPubKey().GetID()].nCreateTime = KEY_TIME;
204204
wallet->AddKeyPubKey(coinbaseKey, coinbaseKey.GetPubKey());
@@ -214,7 +214,7 @@ BOOST_FIXTURE_TEST_CASE(importwallet_rescan, TestChain100Setup)
214214
// Call importwallet RPC and verify all blocks with timestamps >= BLOCK_TIME
215215
// were scanned, and no prior blocks were scanned.
216216
{
217-
std::shared_ptr<CWallet> wallet = std::make_shared<CWallet>(*chain, WalletLocation(), WalletDatabase::CreateDummy());
217+
std::shared_ptr<CWallet> wallet = std::make_shared<CWallet>(chain.get(), WalletLocation(), WalletDatabase::CreateDummy());
218218

219219
JSONRPCRequest request;
220220
request.params.setArray();
@@ -245,7 +245,7 @@ BOOST_FIXTURE_TEST_CASE(importwallet_rescan, TestChain100Setup)
245245
BOOST_FIXTURE_TEST_CASE(coin_mark_dirty_immature_credit, TestChain100Setup)
246246
{
247247
auto chain = interfaces::MakeChain();
248-
CWallet wallet(*chain, WalletLocation(), WalletDatabase::CreateDummy());
248+
CWallet wallet(chain.get(), WalletLocation(), WalletDatabase::CreateDummy());
249249
CWalletTx wtx(&wallet, m_coinbase_txns.back());
250250
auto locked_chain = chain->lock();
251251
LOCK(wallet.cs_wallet);
@@ -340,7 +340,7 @@ class ListCoinsTestingSetup : public TestChain100Setup
340340
ListCoinsTestingSetup()
341341
{
342342
CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey()));
343-
wallet = MakeUnique<CWallet>(*m_chain, WalletLocation(), WalletDatabase::CreateMock());
343+
wallet = MakeUnique<CWallet>(m_chain.get(), WalletLocation(), WalletDatabase::CreateMock());
344344
bool firstRun;
345345
wallet->LoadWallet(firstRun);
346346
AddKey(*wallet, coinbaseKey);
@@ -451,7 +451,7 @@ BOOST_FIXTURE_TEST_CASE(ListCoins, ListCoinsTestingSetup)
451451
BOOST_FIXTURE_TEST_CASE(wallet_disableprivkeys, TestChain100Setup)
452452
{
453453
auto chain = interfaces::MakeChain();
454-
std::shared_ptr<CWallet> wallet = std::make_shared<CWallet>(*chain, WalletLocation(), WalletDatabase::CreateDummy());
454+
std::shared_ptr<CWallet> wallet = std::make_shared<CWallet>(chain.get(), WalletLocation(), WalletDatabase::CreateDummy());
455455
wallet->SetMinVersion(FEATURE_LATEST);
456456
wallet->SetWalletFlag(WALLET_FLAG_DISABLE_PRIVATE_KEYS);
457457
BOOST_CHECK(!wallet->TopUpKeyPool(1000));

src/wallet/wallet.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3112,7 +3112,6 @@ bool CWallet::CommitTransaction(CTransactionRef tx, mapValue_t mapValue, std::ve
31123112

31133113
DBErrors CWallet::LoadWallet(bool& fFirstRunRet)
31143114
{
3115-
auto locked_chain = chain().lock();
31163115
LOCK(cs_wallet);
31173116

31183117
fFirstRunRet = false;
@@ -3976,7 +3975,7 @@ bool CWallet::Verify(interfaces::Chain& chain, const WalletLocation& location, b
39763975

39773976
if (salvage_wallet) {
39783977
// Recover readable keypairs:
3979-
CWallet dummyWallet(chain, WalletLocation(), WalletDatabase::CreateDummy());
3978+
CWallet dummyWallet(&chain, WalletLocation(), WalletDatabase::CreateDummy());
39803979
std::string backup_filename;
39813980
if (!WalletBatch::Recover(wallet_path, (void *)&dummyWallet, WalletBatch::RecoverKeysOnlyFilter, backup_filename)) {
39823981
return false;
@@ -3996,7 +3995,7 @@ std::shared_ptr<CWallet> CWallet::CreateWalletFromFile(interfaces::Chain& chain,
39963995
if (gArgs.GetBoolArg("-zapwallettxes", false)) {
39973996
chain.initMessage(_("Zapping all transactions from wallet..."));
39983997

3999-
std::unique_ptr<CWallet> tempWallet = MakeUnique<CWallet>(chain, location, WalletDatabase::Create(location.GetPath()));
3998+
std::unique_ptr<CWallet> tempWallet = MakeUnique<CWallet>(&chain, location, WalletDatabase::Create(location.GetPath()));
40003999
DBErrors nZapWalletRet = tempWallet->ZapWalletTx(vWtx);
40014000
if (nZapWalletRet != DBErrors::LOAD_OK) {
40024001
chain.initError(strprintf(_("Error loading %s: Wallet corrupted"), walletFile));
@@ -4010,7 +4009,7 @@ std::shared_ptr<CWallet> CWallet::CreateWalletFromFile(interfaces::Chain& chain,
40104009
bool fFirstRun = true;
40114010
// TODO: Can't use std::make_shared because we need a custom deleter but
40124011
// should be possible to use std::allocate_shared.
4013-
std::shared_ptr<CWallet> walletInstance(new CWallet(chain, location, WalletDatabase::Create(location.GetPath())), ReleaseWallet);
4012+
std::shared_ptr<CWallet> walletInstance(new CWallet(&chain, location, WalletDatabase::Create(location.GetPath())), ReleaseWallet);
40144013
DBErrors nLoadWalletRet = walletInstance->LoadWallet(fFirstRun);
40154014
if (nLoadWalletRet != DBErrors::LOAD_OK)
40164015
{

src/wallet/wallet.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,7 @@ class CWallet final : public CCryptoKeyStore, private interfaces::Chain::Notific
704704
bool AddWatchOnly(const CScript& dest) override EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
705705

706706
/** Interface for accessing chain state. */
707-
interfaces::Chain& m_chain;
707+
interfaces::Chain* m_chain;
708708

709709
/** Wallet location which includes wallet name (see WalletLocation). */
710710
WalletLocation m_location;
@@ -767,7 +767,7 @@ class CWallet final : public CCryptoKeyStore, private interfaces::Chain::Notific
767767
unsigned int nMasterKeyMaxID = 0;
768768

769769
/** Construct wallet with specified name and database implementation. */
770-
CWallet(interfaces::Chain& chain, const WalletLocation& location, std::unique_ptr<WalletDatabase> database) : m_chain(chain), m_location(location), database(std::move(database))
770+
CWallet(interfaces::Chain* chain, const WalletLocation& location, std::unique_ptr<WalletDatabase> database) : m_chain(chain), m_location(location), database(std::move(database))
771771
{
772772
}
773773

@@ -795,7 +795,7 @@ class CWallet final : public CCryptoKeyStore, private interfaces::Chain::Notific
795795
std::unique_ptr<interfaces::Handler> m_chain_notifications_handler;
796796

797797
/** Interface for accessing chain state. */
798-
interfaces::Chain& chain() const { return m_chain; }
798+
interfaces::Chain& chain() const { assert(m_chain); return *m_chain; }
799799

800800
const CWalletTx* GetWalletTx(const uint256& hash) const;
801801

src/wallet/wallettool.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
#include <base58.h>
66
#include <fs.h>
7-
#include <interfaces/chain.h>
87
#include <util/system.h>
98
#include <wallet/wallet.h>
109
#include <wallet/walletutil.h>
@@ -28,8 +27,7 @@ static std::shared_ptr<CWallet> CreateWallet(const std::string& name, const fs::
2827
return nullptr;
2928
}
3029
// dummy chain interface
31-
auto chain = interfaces::MakeChain();
32-
std::shared_ptr<CWallet> wallet_instance(new CWallet(*chain, WalletLocation(name), WalletDatabase::Create(path)), WalletToolReleaseWallet);
30+
std::shared_ptr<CWallet> wallet_instance(new CWallet(nullptr /* chain */, WalletLocation(name), WalletDatabase::Create(path)), WalletToolReleaseWallet);
3331
bool first_run = true;
3432
DBErrors load_wallet_ret = wallet_instance->LoadWallet(first_run);
3533
if (load_wallet_ret != DBErrors::LOAD_OK) {
@@ -56,8 +54,7 @@ static std::shared_ptr<CWallet> LoadWallet(const std::string& name, const fs::pa
5654
}
5755

5856
// dummy chain interface
59-
auto chain = interfaces::MakeChain();
60-
std::shared_ptr<CWallet> wallet_instance(new CWallet(*chain, WalletLocation(name), WalletDatabase::Create(path)), WalletToolReleaseWallet);
57+
std::shared_ptr<CWallet> wallet_instance(new CWallet(nullptr /* chain */, WalletLocation(name), WalletDatabase::Create(path)), WalletToolReleaseWallet);
6158
DBErrors load_wallet_ret;
6259
try {
6360
bool first_run;

0 commit comments

Comments
 (0)