Skip to content

Commit

Permalink
Bugfix: Wallet: Don't initialise "used" flag for wallet tool "info" c…
Browse files Browse the repository at this point in the history
…ommand

Initialising "used" would otherwise incorrectly inflate the address book count beyond what was actually in the file
  • Loading branch information
luke-jr committed Nov 24, 2023
1 parent ba55b09 commit 1c43cdc
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2359,7 +2359,7 @@ void CWallet::CommitTransaction(CTransactionRef tx, mapValue_t mapValue, std::ve
}
}

DBErrors CWallet::LoadWallet()
DBErrors CWallet::LoadWallet(const do_init_used_flag do_init_used_flag_val)
{
LOCK(cs_wallet);

Expand All @@ -2383,7 +2383,7 @@ DBErrors CWallet::LoadWallet()
return nLoadWalletRet;
}

InitialiseAddressBookUsed();
if (do_init_used_flag_val == do_init_used_flag::Init) InitialiseAddressBookUsed();

return DBErrors::LOAD_OK;
}
Expand Down
3 changes: 2 additions & 1 deletion src/wallet/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -791,7 +791,8 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
CAmount GetDebit(const CTransaction& tx, const isminefilter& filter) const;
void chainStateFlushed(ChainstateRole role, const CBlockLocator& loc) override;

DBErrors LoadWallet();
enum class do_init_used_flag { Init, Skip };
DBErrors LoadWallet(const do_init_used_flag do_init_used_flag_val = do_init_used_flag::Init);
DBErrors ZapSelectTx(std::vector<uint256>& vHashIn, std::vector<uint256>& vHashOut) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);

bool SetAddressBook(const CTxDestination& address, const std::string& strName, const std::optional<AddressPurpose>& purpose);
Expand Down
7 changes: 4 additions & 3 deletions src/wallet/wallettool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ static void WalletCreate(CWallet* wallet_instance, uint64_t wallet_creation_flag
wallet_instance->TopUpKeyPool();
}

static std::shared_ptr<CWallet> MakeWallet(const std::string& name, const fs::path& path, DatabaseOptions options)
static std::shared_ptr<CWallet> MakeWallet(const std::string& name, const fs::path& path, DatabaseOptions options, CWallet::do_init_used_flag do_init_used_flag_val = CWallet::do_init_used_flag::Init)
{
DatabaseStatus status;
bilingual_str error;
Expand All @@ -61,7 +61,7 @@ static std::shared_ptr<CWallet> MakeWallet(const std::string& name, const fs::pa
std::shared_ptr<CWallet> wallet_instance{new CWallet(/*chain=*/nullptr, name, std::move(database)), WalletToolReleaseWallet};
DBErrors load_wallet_ret;
try {
load_wallet_ret = wallet_instance->LoadWallet();
load_wallet_ret = wallet_instance->LoadWallet(do_init_used_flag_val);
} catch (const std::runtime_error&) {
tfm::format(std::cerr, "Error loading %s. Is wallet being used by another process?\n", name);
return nullptr;
Expand Down Expand Up @@ -168,7 +168,8 @@ bool ExecuteWalletToolFunc(const ArgsManager& args, const std::string& command)
DatabaseOptions options;
ReadDatabaseArgs(args, options);
options.require_existing = true;
const std::shared_ptr<CWallet> wallet_instance = MakeWallet(name, path, options);
// NOTE: We need to skip initialisation of the m_used flag, or else the address book count might be wrong
const std::shared_ptr<CWallet> wallet_instance = MakeWallet(name, path, options, CWallet::do_init_used_flag::Skip);
if (!wallet_instance) return false;
WalletShowInfo(wallet_instance.get());
wallet_instance->Close();
Expand Down

0 comments on commit 1c43cdc

Please sign in to comment.