Skip to content

Commit 489ebb7

Browse files
committed
wallet: make chain optional for CWallet::Create
1 parent d73ae93 commit 489ebb7

File tree

4 files changed

+30
-19
lines changed

4 files changed

+30
-19
lines changed

src/wallet/load.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ bool LoadWallets(interfaces::Chain& chain)
106106
continue;
107107
}
108108
chain.initMessage(_("Loading wallet...").translated);
109-
std::shared_ptr<CWallet> pwallet = database ? CWallet::Create(chain, name, std::move(database), options.create_flags, error, warnings) : nullptr;
109+
std::shared_ptr<CWallet> pwallet = database ? CWallet::Create(&chain, name, std::move(database), options.create_flags, error, warnings) : nullptr;
110110
if (!warnings.empty()) chain.initWarning(Join(warnings, Untranslated("\n")));
111111
if (!pwallet) {
112112
chain.initError(error);

src/wallet/test/wallet_tests.cpp

+15-6
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,17 @@ static_assert(WALLET_INCREMENTAL_RELAY_FEE >= DEFAULT_INCREMENTAL_RELAY_FEE, "wa
3838

3939
BOOST_FIXTURE_TEST_SUITE(wallet_tests, WalletTestingSetup)
4040

41-
static std::shared_ptr<CWallet> TestLoadWallet(interfaces::Chain& chain)
41+
static std::shared_ptr<CWallet> TestLoadWallet(interfaces::Chain* chain)
4242
{
4343
DatabaseOptions options;
4444
DatabaseStatus status;
4545
bilingual_str error;
4646
std::vector<bilingual_str> warnings;
4747
auto database = MakeWalletDatabase("", options, status, error);
4848
auto wallet = CWallet::Create(chain, "", std::move(database), options.create_flags, error, warnings);
49-
wallet->postInitProcess();
49+
if (chain) {
50+
wallet->postInitProcess();
51+
}
5052
return wallet;
5153
}
5254

@@ -689,7 +691,7 @@ BOOST_FIXTURE_TEST_CASE(CreateWallet, TestChain100Setup)
689691
{
690692
gArgs.ForceSetArg("-unsafesqlitesync", "1");
691693
// Create new wallet with known key and unload it.
692-
auto wallet = TestLoadWallet(*m_node.chain);
694+
auto wallet = TestLoadWallet(m_node.chain.get());
693695
CKey key;
694696
key.MakeNewKey(true);
695697
AddKey(*wallet, key);
@@ -729,7 +731,7 @@ BOOST_FIXTURE_TEST_CASE(CreateWallet, TestChain100Setup)
729731

730732
// Reload wallet and make sure new transactions are detected despite events
731733
// being blocked
732-
wallet = TestLoadWallet(*m_node.chain);
734+
wallet = TestLoadWallet(m_node.chain.get());
733735
BOOST_CHECK(rescan_completed);
734736
BOOST_CHECK_EQUAL(addtx_count, 2);
735737
{
@@ -769,7 +771,7 @@ BOOST_FIXTURE_TEST_CASE(CreateWallet, TestChain100Setup)
769771
ENTER_CRITICAL_SECTION(wallet->wallet()->cs_wallet);
770772
ENTER_CRITICAL_SECTION(cs_wallets);
771773
});
772-
wallet = TestLoadWallet(*m_node.chain);
774+
wallet = TestLoadWallet(m_node.chain.get());
773775
BOOST_CHECK_EQUAL(addtx_count, 4);
774776
{
775777
LOCK(wallet->cs_wallet);
@@ -781,10 +783,17 @@ BOOST_FIXTURE_TEST_CASE(CreateWallet, TestChain100Setup)
781783
TestUnloadWallet(std::move(wallet));
782784
}
783785

786+
BOOST_FIXTURE_TEST_CASE(CreateWalletWithoutChain, BasicTestingSetup)
787+
{
788+
auto wallet = TestLoadWallet(nullptr);
789+
BOOST_CHECK(wallet);
790+
UnloadWallet(std::move(wallet));
791+
}
792+
784793
BOOST_FIXTURE_TEST_CASE(ZapSelectTx, TestChain100Setup)
785794
{
786795
gArgs.ForceSetArg("-unsafesqlitesync", "1");
787-
auto wallet = TestLoadWallet(*m_node.chain);
796+
auto wallet = TestLoadWallet(m_node.chain.get());
788797
CKey key;
789798
key.MakeNewKey(true);
790799
AddKey(*wallet, key);

src/wallet/wallet.cpp

+13-11
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ std::shared_ptr<CWallet> LoadWalletInternal(interfaces::Chain& chain, const std:
214214
}
215215

216216
chain.initMessage(_("Loading wallet...").translated);
217-
std::shared_ptr<CWallet> wallet = CWallet::Create(chain, name, std::move(database), options.create_flags, error, warnings);
217+
std::shared_ptr<CWallet> wallet = CWallet::Create(&chain, name, std::move(database), options.create_flags, error, warnings);
218218
if (!wallet) {
219219
error = Untranslated("Wallet loading failed.") + Untranslated(" ") + error;
220220
status = DatabaseStatus::FAILED_LOAD;
@@ -294,7 +294,7 @@ std::shared_ptr<CWallet> CreateWallet(interfaces::Chain& chain, const std::strin
294294

295295
// Make the wallet
296296
chain.initMessage(_("Loading wallet...").translated);
297-
std::shared_ptr<CWallet> wallet = CWallet::Create(chain, name, std::move(database), wallet_creation_flags, error, warnings);
297+
std::shared_ptr<CWallet> wallet = CWallet::Create(&chain, name, std::move(database), wallet_creation_flags, error, warnings);
298298
if (!wallet) {
299299
error = Untranslated("Wallet creation failed.") + Untranslated(" ") + error;
300300
status = DatabaseStatus::FAILED_CREATE;
@@ -3885,14 +3885,14 @@ std::unique_ptr<WalletDatabase> MakeWalletDatabase(const std::string& name, cons
38853885
return MakeDatabase(wallet_path, options, status, error_string);
38863886
}
38873887

3888-
std::shared_ptr<CWallet> CWallet::Create(interfaces::Chain& chain, const std::string& name, std::unique_ptr<WalletDatabase> database, uint64_t wallet_creation_flags, bilingual_str& error, std::vector<bilingual_str>& warnings)
3888+
std::shared_ptr<CWallet> CWallet::Create(interfaces::Chain* chain, const std::string& name, std::unique_ptr<WalletDatabase> database, uint64_t wallet_creation_flags, bilingual_str& error, std::vector<bilingual_str>& warnings)
38893889
{
38903890
const std::string& walletFile = database->Filename();
38913891

38923892
int64_t nStart = GetTimeMillis();
38933893
// TODO: Can't use std::make_shared because we need a custom deleter but
38943894
// should be possible to use std::allocate_shared.
3895-
std::shared_ptr<CWallet> walletInstance(new CWallet(&chain, name, std::move(database)), ReleaseWallet);
3895+
std::shared_ptr<CWallet> walletInstance(new CWallet(chain, name, std::move(database)), ReleaseWallet);
38963896
DBErrors nLoadWalletRet = walletInstance->LoadWallet();
38973897
if (nLoadWalletRet != DBErrors::LOAD_OK) {
38983898
if (nLoadWalletRet == DBErrors::CORRUPT) {
@@ -3952,7 +3952,9 @@ std::shared_ptr<CWallet> CWallet::Create(interfaces::Chain& chain, const std::st
39523952
}
39533953
}
39543954

3955-
walletInstance->chainStateFlushed(chain.getTipLocator());
3955+
if (chain) {
3956+
walletInstance->chainStateFlushed(chain->getTipLocator());
3957+
}
39563958
} else if (wallet_creation_flags & WALLET_FLAG_DISABLE_PRIVATE_KEYS) {
39573959
// Make it impossible to disable private keys after creation
39583960
error = strprintf(_("Error loading %s: Private keys can only be disabled during creation"), walletFile);
@@ -4049,9 +4051,9 @@ std::shared_ptr<CWallet> CWallet::Create(interfaces::Chain& chain, const std::st
40494051
_("This is the transaction fee you will pay if you send a transaction."));
40504052
}
40514053
walletInstance->m_pay_tx_fee = CFeeRate(nFeePerK, 1000);
4052-
if (walletInstance->m_pay_tx_fee < chain.relayMinFee()) {
4054+
if (chain && walletInstance->m_pay_tx_fee < chain->relayMinFee()) {
40534055
error = strprintf(_("Invalid amount for -paytxfee=<amount>: '%s' (must be at least %s)"),
4054-
gArgs.GetArg("-paytxfee", ""), chain.relayMinFee().ToString());
4056+
gArgs.GetArg("-paytxfee", ""), chain->relayMinFee().ToString());
40554057
return nullptr;
40564058
}
40574059
}
@@ -4065,15 +4067,15 @@ std::shared_ptr<CWallet> CWallet::Create(interfaces::Chain& chain, const std::st
40654067
if (nMaxFee > HIGH_MAX_TX_FEE) {
40664068
warnings.push_back(_("-maxtxfee is set very high! Fees this large could be paid on a single transaction."));
40674069
}
4068-
if (CFeeRate(nMaxFee, 1000) < chain.relayMinFee()) {
4070+
if (chain && CFeeRate(nMaxFee, 1000) < chain->relayMinFee()) {
40694071
error = strprintf(_("Invalid amount for -maxtxfee=<amount>: '%s' (must be at least the minrelay fee of %s to prevent stuck transactions)"),
4070-
gArgs.GetArg("-maxtxfee", ""), chain.relayMinFee().ToString());
4072+
gArgs.GetArg("-maxtxfee", ""), chain->relayMinFee().ToString());
40714073
return nullptr;
40724074
}
40734075
walletInstance->m_default_max_tx_fee = nMaxFee;
40744076
}
40754077

4076-
if (chain.relayMinFee().GetFeePerK() > HIGH_TX_FEE_PER_KB) {
4078+
if (chain && chain->relayMinFee().GetFeePerK() > HIGH_TX_FEE_PER_KB) {
40774079
warnings.push_back(AmountHighWarn("-minrelaytxfee") + Untranslated(" ") +
40784080
_("The wallet will avoid paying less than the minimum relay fee."));
40794081
}
@@ -4089,7 +4091,7 @@ std::shared_ptr<CWallet> CWallet::Create(interfaces::Chain& chain, const std::st
40894091

40904092
LOCK(walletInstance->cs_wallet);
40914093

4092-
if (!AttachChain(walletInstance, chain, error, warnings)) {
4094+
if (chain && !AttachChain(walletInstance, *chain, error, warnings)) {
40934095
return nullptr;
40944096
}
40954097

src/wallet/wallet.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -1209,7 +1209,7 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
12091209
bool MarkReplaced(const uint256& originalHash, const uint256& newHash);
12101210

12111211
/* Initializes the wallet, returns a new CWallet instance or a null pointer in case of an error */
1212-
static std::shared_ptr<CWallet> Create(interfaces::Chain& chain, const std::string& name, std::unique_ptr<WalletDatabase> database, uint64_t wallet_creation_flags, bilingual_str& error, std::vector<bilingual_str>& warnings);
1212+
static std::shared_ptr<CWallet> Create(interfaces::Chain* chain, const std::string& name, std::unique_ptr<WalletDatabase> database, uint64_t wallet_creation_flags, bilingual_str& error, std::vector<bilingual_str>& warnings);
12131213

12141214
/**
12151215
* Wallet post-init setup

0 commit comments

Comments
 (0)