Skip to content

Commit

Permalink
Merge bitcoin#23065: Allow UTXO locks to be written to wallet DB
Browse files Browse the repository at this point in the history
d96b000 Make GUI UTXO lock/unlock persistent (Samuel Dobson)
077154f Add release note for lockunspent change (Samuel Dobson)
719ae92 Update lockunspent tests for lock persistence (Samuel Dobson)
f13fc16 Allow lockunspent to store the lock in the wallet DB (Samuel Dobson)
c527893 Allow locked UTXOs to be store in the wallet database (Samuel Dobson)

Pull request description:

  Addresses and closes bitcoin#22368

  As per that issue (and its predecessor bitcoin#14907), there seems to be some interest in allowing unspent outputs to be locked persistently. This PR does so by adding a flag to lockunspent to store the change in the wallet database. Defaults to false, so there is no change in default behaviour.

  Edit: GUI commit changes default behaviour. UTXOs locked/unlocked via the GUI are now persistent.

ACKs for top commit:
  achow101:
    ACK d96b000
  kristapsk:
    ACK d96b000
  lsilva01:
    Tested ACK bitcoin@d96b000 on Ubuntu 20.04
  prayank23:
    ACK bitcoin@d96b000

Tree-SHA512: 957a5bbfe7f763036796906ccb1598feb6c14c5975838be1ba24a198840bf59e83233165cb112cebae909b6b25bf27275a4d7fa425923ef6c788ff671d7a89a8
  • Loading branch information
laanwj authored and vijaydasmp committed Nov 24, 2024
1 parent 2d0e5d7 commit a06c5b8
Show file tree
Hide file tree
Showing 11 changed files with 139 additions and 29 deletions.
15 changes: 15 additions & 0 deletions doc/release-notes-23065.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Notable changes
===============

Updated RPCs
------------

- `lockunspent` now optionally takes a third parameter, `persistent`, which
causes the lock to be written persistently to the wallet database. This
allows UTXOs to remain locked even after node restarts or crashes.

GUI changes
-----------

- UTXOs which are locked via the GUI are now stored persistently in the
wallet database, so are not lost on node shutdown or crash.
4 changes: 2 additions & 2 deletions src/interfaces/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,10 @@ class Wallet
virtual bool setAddressReceiveRequest(const CTxDestination& dest, const std::string& id, const std::string& value) = 0;

//! Lock coin.
virtual void lockCoin(const COutPoint& output) = 0;
virtual bool lockCoin(const COutPoint& output, const bool write_to_db) = 0;

//! Unlock coin.
virtual void unlockCoin(const COutPoint& output) = 0;
virtual bool unlockCoin(const COutPoint& output) = 0;

//! Return whether coin is locked.
virtual bool isLockedCoin(const COutPoint& output) = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/qt/coincontroldialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ void CoinControlDialog::lockCoin()
contextMenuItem->setCheckState(COLUMN_CHECKBOX, Qt::Unchecked);

COutPoint outpt(uint256S(contextMenuItem->data(COLUMN_ADDRESS, TxHashRole).toString().toStdString()), contextMenuItem->data(COLUMN_ADDRESS, VOutRole).toUInt());
model->wallet().lockCoin(outpt);
model->wallet().lockCoin(outpt, /* write_to_db = */ true);
contextMenuItem->setDisabled(true);
contextMenuItem->setIcon(COLUMN_CHECKBOX, GUIUtil::getIcon("lock_closed", GUIUtil::ThemedColor::RED));
updateLabelLocked();
Expand Down
1 change: 1 addition & 0 deletions src/rpc/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ static const CRPCConvertParam vRPCConvertParams[] =
{ "gettxoutsetinfo", 2, "use_index"},
{ "lockunspent", 0, "unlock" },
{ "lockunspent", 1, "transactions" },
{ "lockunspent", 2, "persistent" },
{ "send", 0, "outputs" },
{ "send", 1, "conf_target" },
{ "send", 3, "fee_rate"},
Expand Down
10 changes: 6 additions & 4 deletions src/wallet/interfaces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,15 +238,17 @@ class WalletImpl : public Wallet
WalletBatch batch{m_wallet->GetDatabase()};
return m_wallet->SetAddressReceiveRequest(batch, dest, id, value);
}
void lockCoin(const COutPoint& output) override
bool lockCoin(const COutPoint& output, const bool write_to_db) override
{
LOCK(m_wallet->cs_wallet);
return m_wallet->LockCoin(output);
std::unique_ptr<WalletBatch> batch = write_to_db ? std::make_unique<WalletBatch>(m_wallet->GetDatabase()) : nullptr;
return m_wallet->LockCoin(output, batch.get());
}
void unlockCoin(const COutPoint& output) override
bool unlockCoin(const COutPoint& output) override
{
LOCK(m_wallet->cs_wallet);
return m_wallet->UnlockCoin(output);
std::unique_ptr<WalletBatch> batch = std::make_unique<WalletBatch>(m_wallet->GetDatabase());
return m_wallet->UnlockCoin(output, batch.get());
}
bool isLockedCoin(const COutPoint& output) override
{
Expand Down
29 changes: 22 additions & 7 deletions src/wallet/rpcwallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2204,8 +2204,9 @@ static RPCHelpMan lockunspent()
"If no transaction outputs are specified when unlocking then all current locked transaction outputs are unlocked.\n"
"A locked transaction output will not be chosen by automatic coin selection, when spending Dash.\n"
"Manually selected coins are automatically unlocked.\n"
"Locks are stored in memory only. Nodes start with zero locked outputs, and the locked output list\n"
"is always cleared (by virtue of process exit) when a node stops or fails.\n"
"Locks are stored in memory only, unless persistent=true, in which case they will be written to the\n"
"wallet database and loaded on node start. Unwritten (persistent=false) locks are always cleared\n"
"(by virtue of process exit) when a node stops or fails. Unlocking will clear both persistent and not.\n"
"Also see the listunspent call\n",
{
{"unlock", RPCArg::Type::BOOL, RPCArg::Optional::NO, "Whether to unlock (true) or lock (false) the specified transactions"},
Expand All @@ -2217,6 +2218,7 @@ static RPCHelpMan lockunspent()
{"vout", RPCArg::Type::NUM, RPCArg::Optional::NO, "The output number"},
},
},
{"persistent", RPCArg::Type::BOOL, RPCArg::Default{false}, "Whether to write/erase this lock in the wallet database, or keep the change in memory only. Ignored for unlocking."},
},
},
},
Expand All @@ -2232,6 +2234,8 @@ static RPCHelpMan lockunspent()
+ HelpExampleCli("listlockunspent", "") +
"\nUnlock the transaction again\n"
+ HelpExampleCli("lockunspent", "true \"[{\\\"txid\\\":\\\"a08e6907dbbd3d809776dbfc5d82e371b764ed838b5655e72f463568df1aadf0\\\",\\\"vout\\\":1}]\"") +
"\nLock the transaction persistently in the wallet database\n"
+ HelpExampleCli("lockunspent", "false \"[{\\\"txid\\\":\\\"a08e6907dbbd3d809776dbfc5d82e371b764ed838b5655e72f463568df1aadf0\\\",\\\"vout\\\":1}]\" true") +
"\nAs a JSON-RPC call\n"
+ HelpExampleRpc("lockunspent", "false, \"[{\\\"txid\\\":\\\"a08e6907dbbd3d809776dbfc5d82e371b764ed838b5655e72f463568df1aadf0\\\",\\\"vout\\\":1}]\"")
},
Expand All @@ -2250,9 +2254,13 @@ static RPCHelpMan lockunspent()

bool fUnlock = request.params[0].get_bool();

const bool persistent{request.params[2].isNull() ? false : request.params[2].get_bool()};

if (request.params[1].isNull()) {
if (fUnlock)
pwallet->UnlockAllCoins();
if (fUnlock) {
if (!pwallet->UnlockAllCoins())
throw JSONRPCError(RPC_WALLET_ERROR, "Unlocking coins failed");
}
return true;
}

Expand Down Expand Up @@ -2303,17 +2311,24 @@ static RPCHelpMan lockunspent()
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, expected locked output");
}

if (!fUnlock && is_locked) {
if (!fUnlock && is_locked && !persistent) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, output already locked");
}

outputs.push_back(outpt);
}

std::unique_ptr<WalletBatch> batch = nullptr;
// Unlock is always persistent
if (fUnlock || persistent) batch = std::make_unique<WalletBatch>(pwallet->GetDatabase());

// Atomically set (un)locked status for the outputs.
for (const COutPoint& outpt : outputs) {
if (fUnlock) pwallet->UnlockCoin(outpt);
else pwallet->LockCoin(outpt);
if (fUnlock) {
if (!pwallet->UnlockCoin(outpt, batch.get())) throw JSONRPCError(RPC_WALLET_ERROR, "Unlocking coin failed");
} else {
if (!pwallet->LockCoin(outpt, batch.get())) throw JSONRPCError(RPC_WALLET_ERROR, "Locking coin failed");
}
}

return true;
Expand Down
38 changes: 29 additions & 9 deletions src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -625,20 +625,25 @@ bool CWallet::IsSpent(const uint256& hash, unsigned int n) const
return false;
}

void CWallet::AddToSpends(const COutPoint& outpoint, const uint256& wtxid)
void CWallet::AddToSpends(const COutPoint& outpoint, const uint256& wtxid, WalletBatch* batch)
{
mapTxSpends.insert(std::make_pair(outpoint, wtxid));
setWalletUTXO.erase(outpoint);

setLockedCoins.erase(outpoint);
if (batch) {
UnlockCoin(outpoint, batch);
} else {
WalletBatch temp_batch(GetDatabase());
UnlockCoin(outpoint, &temp_batch);
}

std::pair<TxSpends::iterator, TxSpends::iterator> range;
range = mapTxSpends.equal_range(outpoint);
SyncMetaData(range);
}


void CWallet::AddToSpends(const uint256& wtxid)
void CWallet::AddToSpends(const uint256& wtxid, WalletBatch* batch)
{
auto it = mapWallet.find(wtxid);
assert(it != mapWallet.end());
Expand All @@ -647,7 +652,7 @@ void CWallet::AddToSpends(const uint256& wtxid)
return;

for (const CTxIn& txin : thisTx.tx->vin)
AddToSpends(txin.prevout, wtxid);
AddToSpends(txin.prevout, wtxid, batch);
}

bool CWallet::EncryptWallet(const SecureString& strWalletPassphrase)
Expand Down Expand Up @@ -915,7 +920,7 @@ CWalletTx* CWallet::AddToWallet(CTransactionRef tx, const CWalletTx::Confirmatio
wtx.nOrderPos = IncOrderPosNext(&batch);
wtx.m_it_wtxOrdered = wtxOrdered.insert(std::make_pair(wtx.nOrderPos, &wtx));
wtx.nTimeSmart = ComputeTimeSmart(wtx);
AddToSpends(hash);
AddToSpends(hash, &batch);

std::vector<std::pair<const CTransactionRef&, unsigned int>> outputs;
for(unsigned int i = 0; i < wtx.tx->vout.size(); ++i) {
Expand Down Expand Up @@ -4443,7 +4448,7 @@ void ReserveDestination::ReturnDestination()
address = CNoDestination();
}

void CWallet::LockCoin(const COutPoint& output)
bool CWallet::LockCoin(const COutPoint& output, WalletBatch* batch)
{
AssertLockHeld(cs_wallet);
setLockedCoins.insert(output);
Expand All @@ -4452,23 +4457,38 @@ void CWallet::LockCoin(const COutPoint& output)

fAnonymizableTallyCached = false;
fAnonymizableTallyCachedNonDenom = false;
if (batch) {
return batch->WriteLockedUTXO(output);
}
return true;
}

void CWallet::UnlockCoin(const COutPoint& output)
bool CWallet::UnlockCoin(const COutPoint& output, WalletBatch* batch)
{
AssertLockHeld(cs_wallet);
setLockedCoins.erase(output);
std::map<uint256, CWalletTx>::iterator it = mapWallet.find(output.hash);
if (it != mapWallet.end()) it->second.MarkDirty(); // recalculate all credits for this tx

fAnonymizableTallyCached = false;
fAnonymizableTallyCachedNonDenom = false;

bool was_locked = setLockedCoins.erase(output);
if (batch && was_locked) {
return batch->EraseLockedUTXO(output);
}
return true;
}

void CWallet::UnlockAllCoins()
bool CWallet::UnlockAllCoins()
{
AssertLockHeld(cs_wallet);
bool success = true;
WalletBatch batch(GetDatabase());
for (auto it = setLockedCoins.begin(); it != setLockedCoins.end(); ++it) {
success &= batch.EraseLockedUTXO(*it);
}
setLockedCoins.clear();
return success;
}

bool CWallet::IsLockedCoin(uint256 hash, unsigned int n) const
Expand Down
10 changes: 5 additions & 5 deletions src/wallet/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -759,8 +759,8 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
*/
typedef std::multimap<COutPoint, uint256> TxSpends;
TxSpends mapTxSpends GUARDED_BY(cs_wallet);
void AddToSpends(const COutPoint& outpoint, const uint256& wtxid) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
void AddToSpends(const uint256& wtxid) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
void AddToSpends(const COutPoint& outpoint, const uint256& wtxid, WalletBatch* batch = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
void AddToSpends(const uint256& wtxid, WalletBatch* batch = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);

std::set<COutPoint> setWalletUTXO;
mutable std::map<COutPoint, int> mapOutpointRoundsCache GUARDED_BY(cs_wallet);
Expand Down Expand Up @@ -1033,9 +1033,9 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
std::vector<OutputGroup> GroupOutputs(const std::vector<COutput>& outputs, bool separate_coins, const CFeeRate& effective_feerate, const CFeeRate& long_term_feerate, const CoinEligibilityFilter& filter, bool positive_only) const;

bool IsLockedCoin(uint256 hash, unsigned int n) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
void LockCoin(const COutPoint& output) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
void UnlockCoin(const COutPoint& output) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
void UnlockAllCoins() EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
bool LockCoin(const COutPoint& output, WalletBatch* batch = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
bool UnlockCoin(const COutPoint& output, WalletBatch* batch = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
bool UnlockAllCoins() EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
void ListLockedCoins(std::vector<COutPoint>& vOutpts) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
void ListProTxCoins(std::vector<COutPoint>& vOutpts) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);

Expand Down
17 changes: 17 additions & 0 deletions src/wallet/walletdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const std::string HDCHAIN{"hdchain"};
const std::string HDPUBKEY{"hdpubkey"};
const std::string KEYMETA{"keymeta"};
const std::string KEY{"key"};
const std::string LOCKED_UTXO{"lockedutxo"};
const std::string MASTER_KEY{"mkey"};
const std::string MINVERSION{"minversion"};
const std::string NAME{"name"};
Expand Down Expand Up @@ -308,6 +309,16 @@ bool WalletBatch::WriteDescriptorCacheItems(const uint256& desc_id, const Descri
return true;
}

bool WalletBatch::WriteLockedUTXO(const COutPoint& output)
{
return WriteIC(std::make_pair(DBKeys::LOCKED_UTXO, std::make_pair(output.hash, output.n)), uint8_t{'1'});
}

bool WalletBatch::EraseLockedUTXO(const COutPoint& output)
{
return EraseIC(std::make_pair(DBKeys::LOCKED_UTXO, std::make_pair(output.hash, output.n)));
}

class CWalletScanState {
public:
unsigned int nKeys{0};
Expand Down Expand Up @@ -709,6 +720,12 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue,

wss.m_descriptor_crypt_keys.insert(std::make_pair(std::make_pair(desc_id, pubkey.GetID()), std::make_pair(pubkey, privkey)));
wss.fIsEncrypted = true;
} else if (strType == DBKeys::LOCKED_UTXO) {
uint256 hash;
uint32_t n;
ssKey >> hash;
ssKey >> n;
pwallet->LockCoin(COutPoint(hash, n));
} else if (strType != DBKeys::BESTBLOCK && strType != DBKeys::BESTBLOCK_NOMERKLE &&
strType != DBKeys::MINVERSION && strType != DBKeys::ACENTRY &&
strType != DBKeys::VERSION && strType != DBKeys::SETTINGS &&
Expand Down
4 changes: 4 additions & 0 deletions src/wallet/walletdb.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ extern const std::string HDCHAIN;
extern const std::string HDPUBKEY;
extern const std::string KEY;
extern const std::string KEYMETA;
extern const std::string LOCKED_UTXO;
extern const std::string MASTER_KEY;
extern const std::string MINVERSION;
extern const std::string NAME;
Expand Down Expand Up @@ -219,6 +220,9 @@ class WalletBatch
bool WriteDescriptorLastHardenedCache(const CExtPubKey& xpub, const uint256& desc_id, uint32_t key_exp_index);
bool WriteDescriptorCacheItems(const uint256& desc_id, const DescriptorCache& cache);

bool WriteLockedUTXO(const COutPoint& output);
bool EraseLockedUTXO(const COutPoint& output);

/// Write destination data key,value tuple to database
bool WriteDestData(const std::string &address, const std::string &key, const std::string &value);
/// Erase destination data tuple from wallet database
Expand Down
38 changes: 37 additions & 1 deletion test/functional/wallet_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,49 @@ def run_test(self):
# Exercise locking of unspent outputs
unspent_0 = self.nodes[2].listunspent()[0]
unspent_0 = {"txid": unspent_0["txid"], "vout": unspent_0["vout"]}
# Trying to unlock an output which isn't locked should error
assert_raises_rpc_error(-8, "Invalid parameter, expected locked output", self.nodes[2].lockunspent, True, [unspent_0])

# Locking an already-locked output should error
self.nodes[2].lockunspent(False, [unspent_0])
assert_raises_rpc_error(-8, "Invalid parameter, output already locked", self.nodes[2].lockunspent, False, [unspent_0])
assert_raises_rpc_error(-6, "Insufficient funds", self.nodes[2].sendtoaddress, self.nodes[2].getnewaddress(), 200)

# Restarting the node should clear the lock
self.restart_node(2)
self.nodes[2].lockunspent(False, [unspent_0])

# Unloading and reloating the wallet should clear the lock
assert_equal(self.nodes[0].listwallets(), [self.default_wallet_name])
self.nodes[2].unloadwallet(self.default_wallet_name)
self.nodes[2].loadwallet(self.default_wallet_name)
assert_equal(len(self.nodes[2].listlockunspent()), 0)

# Locking non-persistently, then re-locking persistently, is allowed
self.nodes[2].lockunspent(False, [unspent_0])
self.nodes[2].lockunspent(False, [unspent_0], True)

# Restarting the node with the lock written to the wallet should keep the lock
self.restart_node(2)
assert_raises_rpc_error(-8, "Invalid parameter, output already locked", self.nodes[2].lockunspent, False, [unspent_0])

# Unloading and reloading the wallet with a persistent lock should keep the lock
self.nodes[2].unloadwallet(self.default_wallet_name)
self.nodes[2].loadwallet(self.default_wallet_name)
assert_raises_rpc_error(-8, "Invalid parameter, output already locked", self.nodes[2].lockunspent, False, [unspent_0])

# Locked outputs should not be used, even if they are the only available funds
assert_raises_rpc_error(-6, "Insufficient funds", self.nodes[2].sendtoaddress, self.nodes[2].getnewaddress(), 20)
assert_equal([unspent_0], self.nodes[2].listlockunspent())

# Unlocking should remove the persistent lock
self.nodes[2].lockunspent(True, [unspent_0])
self.restart_node(2)
assert_equal(len(self.nodes[2].listlockunspent()), 0)

# Reconnect node 2 after restarts
self.connect_nodes(1, 2)
self.connect_nodes(0, 2)

assert_raises_rpc_error(-8, "txid must be of length 64 (not 34, for '0000000000000000000000000000000000')",
self.nodes[2].lockunspent, False,
[{"txid": "0000000000000000000000000000000000", "vout": 0}])
Expand Down

0 comments on commit a06c5b8

Please sign in to comment.