Skip to content

Commit 44c430f

Browse files
ryanofskyS3RK
authored andcommitted
refactor: Add CWallet:::AttachChain method
This commit does not change behavior, it just moves code from CWallet::CreateWalletFromFile to CWallet:::AttachChain so it can be updated in the next commit. This commit is most easily reviewed with "git diff -w --color-moved=dimmed_zebra" or by diffing CWallet:::AttachChain against the previous code with an external diff tool.
1 parent e2a47ce commit 44c430f

File tree

2 files changed

+45
-24
lines changed

2 files changed

+45
-24
lines changed

src/wallet/wallet.cpp

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4089,6 +4089,35 @@ std::shared_ptr<CWallet> CWallet::Create(interfaces::Chain& chain, const std::st
40894089

40904090
LOCK(walletInstance->cs_wallet);
40914091

4092+
if (!AttachChain(walletInstance, chain, error, warnings)) {
4093+
return nullptr;
4094+
}
4095+
4096+
{
4097+
LOCK(cs_wallets);
4098+
for (auto& load_wallet : g_load_wallet_fns) {
4099+
load_wallet(interfaces::MakeWallet(walletInstance));
4100+
}
4101+
}
4102+
4103+
walletInstance->SetBroadcastTransactions(gArgs.GetBoolArg("-walletbroadcast", DEFAULT_WALLETBROADCAST));
4104+
4105+
{
4106+
walletInstance->WalletLogPrintf("setKeyPool.size() = %u\n", walletInstance->GetKeyPoolSize());
4107+
walletInstance->WalletLogPrintf("mapWallet.size() = %u\n", walletInstance->mapWallet.size());
4108+
walletInstance->WalletLogPrintf("m_address_book.size() = %u\n", walletInstance->m_address_book.size());
4109+
}
4110+
4111+
return walletInstance;
4112+
}
4113+
4114+
bool CWallet::AttachChain(const std::shared_ptr<CWallet>& walletInstance, interfaces::Chain& chain, bilingual_str& error, std::vector<bilingual_str>& warnings)
4115+
{
4116+
LOCK(walletInstance->cs_wallet);
4117+
// allow setting the chain if it hasn't been set already but prevent changing it
4118+
assert(!walletInstance->m_chain || walletInstance->m_chain == &chain);
4119+
walletInstance->m_chain = &chain;
4120+
40924121
// Register wallet with validationinterface. It's done before rescan to avoid
40934122
// missing block connections between end of rescan and validation subscribing.
40944123
// Because of wallet lock being hold, block connection notifications are going to
@@ -4122,21 +4151,21 @@ std::shared_ptr<CWallet> CWallet::Create(interfaces::Chain& chain, const std::st
41224151

41234152
if (tip_height && *tip_height != rescan_height)
41244153
{
4125-
// We can't rescan beyond non-pruned blocks, stop and throw an error.
4126-
// This might happen if a user uses an old wallet within a pruned node
4127-
// or if they ran -disablewallet for a longer time, then decided to re-enable
41284154
if (chain.havePruned()) {
4129-
// Exit early and print an error.
4130-
// If a block is pruned after this check, we will load the wallet,
4131-
// but fail the rescan with a generic error.
41324155
int block_height = *tip_height;
41334156
while (block_height > 0 && chain.haveBlockOnDisk(block_height - 1) && rescan_height != block_height) {
41344157
--block_height;
41354158
}
41364159

41374160
if (rescan_height != block_height) {
4161+
// We can't rescan beyond non-pruned blocks, stop and throw an error.
4162+
// This might happen if a user uses an old wallet within a pruned node
4163+
// or if they ran -disablewallet for a longer time, then decided to re-enable
4164+
// Exit early and print an error.
4165+
// If a block is pruned after this check, we will load the wallet,
4166+
// but fail the rescan with a generic error.
41384167
error = _("Prune: last wallet synchronisation goes beyond pruned data. You need to -reindex (download the whole blockchain again in case of pruned node)");
4139-
return nullptr;
4168+
return false;
41404169
}
41414170
}
41424171

@@ -4158,29 +4187,14 @@ std::shared_ptr<CWallet> CWallet::Create(interfaces::Chain& chain, const std::st
41584187
WalletRescanReserver reserver(*walletInstance);
41594188
if (!reserver.reserve() || (ScanResult::SUCCESS != walletInstance->ScanForWalletTransactions(chain.getBlockHash(rescan_height), rescan_height, {} /* max height */, reserver, true /* update */).status)) {
41604189
error = _("Failed to rescan the wallet during initialization");
4161-
return nullptr;
4190+
return false;
41624191
}
41634192
}
41644193
walletInstance->chainStateFlushed(chain.getTipLocator());
41654194
walletInstance->GetDatabase().IncrementUpdateCounter();
41664195
}
41674196

4168-
{
4169-
LOCK(cs_wallets);
4170-
for (auto& load_wallet : g_load_wallet_fns) {
4171-
load_wallet(interfaces::MakeWallet(walletInstance));
4172-
}
4173-
}
4174-
4175-
walletInstance->SetBroadcastTransactions(gArgs.GetBoolArg("-walletbroadcast", DEFAULT_WALLETBROADCAST));
4176-
4177-
{
4178-
walletInstance->WalletLogPrintf("setKeyPool.size() = %u\n", walletInstance->GetKeyPoolSize());
4179-
walletInstance->WalletLogPrintf("mapWallet.size() = %u\n", walletInstance->mapWallet.size());
4180-
walletInstance->WalletLogPrintf("m_address_book.size() = %u\n", walletInstance->m_address_book.size());
4181-
}
4182-
4183-
return walletInstance;
4197+
return true;
41844198
}
41854199

41864200
const CAddressBookData* CWallet::FindAddressBookEntry(const CTxDestination& dest, bool allow_change) const

src/wallet/wallet.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,13 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
763763

764764
bool CreateTransactionInternal(const std::vector<CRecipient>& vecSend, CTransactionRef& tx, CAmount& nFeeRet, int& nChangePosInOut, bilingual_str& error, const CCoinControl& coin_control, FeeCalculation& fee_calc_out, bool sign);
765765

766+
/**
767+
* Catch wallet up to current chain, scanning new blocks, updating the best
768+
* block locator and m_last_block_processed, and registering for
769+
* notifications about new blocks and transactions.
770+
*/
771+
static bool AttachChain(const std::shared_ptr<CWallet>& wallet, interfaces::Chain& chain, bilingual_str& error, std::vector<bilingual_str>& warnings);
772+
766773
public:
767774
/**
768775
* Main wallet lock.

0 commit comments

Comments
 (0)