Skip to content

Commit 366fe0b

Browse files
committed
Add AddWatchOnlyWithDB, AddKeyOriginWithDB, AddCScriptWithDB functions
AddWatchOnlyWithDB, AddKeyOriginWithDB, and AddCScriptWithDB add their respective data to the wallet using the provided WalletBatch instead of creating a new WalletBatch object every time. This allows for batching writes to the database.
1 parent 56376f3 commit 366fe0b

File tree

2 files changed

+27
-14
lines changed

2 files changed

+27
-14
lines changed

src/wallet/wallet.cpp

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -362,12 +362,6 @@ void CWallet::LoadScriptMetadata(const CScriptID& script_id, const CKeyMetadata&
362362
m_script_metadata[script_id] = meta;
363363
}
364364

365-
// Writes a keymetadata for a public key. overwrite specifies whether to overwrite an existing metadata for that key if there exists one.
366-
bool CWallet::WriteKeyMetadata(const CKeyMetadata& meta, const CPubKey& pubkey, const bool overwrite)
367-
{
368-
return WalletBatch(*database).WriteKeyMetadata(meta, pubkey, overwrite);
369-
}
370-
371365
void CWallet::UpgradeKeyMetadata()
372366
{
373367
AssertLockHeld(cs_wallet);
@@ -432,10 +426,16 @@ void CWallet::UpdateTimeFirstKey(int64_t nCreateTime)
432426
}
433427

434428
bool CWallet::AddCScript(const CScript& redeemScript)
429+
{
430+
WalletBatch batch(*database);
431+
return AddCScriptWithDB(batch, redeemScript);
432+
}
433+
434+
bool CWallet::AddCScriptWithDB(WalletBatch& batch, const CScript& redeemScript)
435435
{
436436
if (!CCryptoKeyStore::AddCScript(redeemScript))
437437
return false;
438-
if (WalletBatch(*database).WriteCScript(Hash160(redeemScript), redeemScript)) {
438+
if (batch.WriteCScript(Hash160(redeemScript), redeemScript)) {
439439
UnsetWalletFlag(WALLET_FLAG_BLANK_WALLET);
440440
return true;
441441
}
@@ -457,20 +457,32 @@ bool CWallet::LoadCScript(const CScript& redeemScript)
457457
return CCryptoKeyStore::AddCScript(redeemScript);
458458
}
459459

460-
bool CWallet::AddWatchOnly(const CScript& dest)
460+
bool CWallet::AddWatchOnlyWithDB(WalletBatch &batch, const CScript& dest)
461461
{
462462
if (!CCryptoKeyStore::AddWatchOnly(dest))
463463
return false;
464464
const CKeyMetadata& meta = m_script_metadata[CScriptID(dest)];
465465
UpdateTimeFirstKey(meta.nCreateTime);
466466
NotifyWatchonlyChanged(true);
467-
if (WalletBatch(*database).WriteWatchOnly(dest, meta)) {
467+
if (batch.WriteWatchOnly(dest, meta)) {
468468
UnsetWalletFlag(WALLET_FLAG_BLANK_WALLET);
469469
return true;
470470
}
471471
return false;
472472
}
473473

474+
bool CWallet::AddWatchOnlyWithDB(WalletBatch &batch, const CScript& dest, int64_t create_time)
475+
{
476+
m_script_metadata[CScriptID(dest)].nCreateTime = create_time;
477+
return AddWatchOnlyWithDB(batch, dest);
478+
}
479+
480+
bool CWallet::AddWatchOnly(const CScript& dest)
481+
{
482+
WalletBatch batch(*database);
483+
return AddWatchOnlyWithDB(batch, dest);
484+
}
485+
474486
bool CWallet::AddWatchOnly(const CScript& dest, int64_t nCreateTime)
475487
{
476488
m_script_metadata[CScriptID(dest)].nCreateTime = nCreateTime;
@@ -4469,12 +4481,12 @@ bool CWallet::GetKeyOrigin(const CKeyID& keyID, KeyOriginInfo& info) const
44694481
return true;
44704482
}
44714483

4472-
bool CWallet::AddKeyOrigin(const CPubKey& pubkey, const KeyOriginInfo& info)
4484+
bool CWallet::AddKeyOriginWithDB(WalletBatch& batch, const CPubKey& pubkey, const KeyOriginInfo& info)
44734485
{
44744486
LOCK(cs_wallet);
44754487
std::copy(info.fingerprint, info.fingerprint + 4, mapKeyMetadata[pubkey.GetID()].key_origin.fingerprint);
44764488
mapKeyMetadata[pubkey.GetID()].key_origin.path = info.path;
44774489
mapKeyMetadata[pubkey.GetID()].has_key_origin = true;
44784490
mapKeyMetadata[pubkey.GetID()].hdKeypath = WriteHDKeypath(info.path);
4479-
return WriteKeyMetadata(mapKeyMetadata[pubkey.GetID()], pubkey, true);
4491+
return batch.WriteKeyMetadata(mapKeyMetadata[pubkey.GetID()], pubkey, true);
44804492
}

src/wallet/wallet.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,7 @@ class CWallet final : public CCryptoKeyStore, private interfaces::Chain::Notific
702702
* nTimeFirstKey more intelligently for more efficient rescans.
703703
*/
704704
bool AddWatchOnly(const CScript& dest) override EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
705+
bool AddWatchOnlyWithDB(WalletBatch &batch, const CScript& dest) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
705706

706707
/** Interface for accessing chain state. */
707708
interfaces::Chain* m_chain;
@@ -760,8 +761,6 @@ class CWallet final : public CCryptoKeyStore, private interfaces::Chain::Notific
760761
// Map from Script ID to key metadata (for watch-only keys).
761762
std::map<CScriptID, CKeyMetadata> m_script_metadata GUARDED_BY(cs_wallet);
762763

763-
bool WriteKeyMetadata(const CKeyMetadata& meta, const CPubKey& pubkey, bool overwrite);
764-
765764
typedef std::map<unsigned int, CMasterKey> MasterKeyMap;
766765
MasterKeyMap mapMasterKeys;
767766
unsigned int nMasterKeyMaxID = 0;
@@ -872,6 +871,7 @@ class CWallet final : public CCryptoKeyStore, private interfaces::Chain::Notific
872871
//! Adds an encrypted key to the store, without saving it to disk (used by LoadWallet)
873872
bool LoadCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret);
874873
bool AddCScript(const CScript& redeemScript) override;
874+
bool AddCScriptWithDB(WalletBatch& batch, const CScript& script);
875875
bool LoadCScript(const CScript& redeemScript);
876876

877877
//! Adds a destination data tuple to the store, and saves it to disk
@@ -887,6 +887,7 @@ class CWallet final : public CCryptoKeyStore, private interfaces::Chain::Notific
887887

888888
//! Adds a watch-only address to the store, and saves it to disk.
889889
bool AddWatchOnly(const CScript& dest, int64_t nCreateTime) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
890+
bool AddWatchOnlyWithDB(WalletBatch &batch, const CScript& dest, int64_t create_time) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
890891
bool RemoveWatchOnly(const CScript &dest) override EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
891892
//! Adds a watch-only address to the store, without saving it to disk (used by LoadWallet)
892893
bool LoadWatchOnly(const CScript &dest);
@@ -1213,7 +1214,7 @@ class CWallet final : public CCryptoKeyStore, private interfaces::Chain::Notific
12131214
bool GetKeyOrigin(const CKeyID& keyid, KeyOriginInfo& info) const override;
12141215

12151216
/** Add a KeyOriginInfo to the wallet */
1216-
bool AddKeyOrigin(const CPubKey& pubkey, const KeyOriginInfo& info);
1217+
bool AddKeyOriginWithDB(WalletBatch& batch, const CPubKey& pubkey, const KeyOriginInfo& info);
12171218
};
12181219

12191220
/**

0 commit comments

Comments
 (0)