Skip to content

Commit b604c5c

Browse files
ryanofskyAntoine Riard
andcommitted
wallet: Minimal fix to restore conflicted transaction notifications
This fix is a based on the fix by Antoine Riard <[email protected]> in bitcoin/bitcoin#18600. Unlike that PR, which implements some new behavior, this just restores previous wallet notification and status behavior for transactions removed from the mempool because they conflict with transactions in a block. The behavior was accidentally changed in two `CWallet::BlockConnected` updates: a31be09 and 7e89994 from bitcoin/bitcoin#16624, causing issue bitcoin/bitcoin#18325. The change here could be improved and replaced with a more comprehensive cleanup, so it includes a detailed comment explaining future considerations. Fixes #18325 Co-authored-by: Antoine Riard <[email protected]>
1 parent e2f6866 commit b604c5c

File tree

9 files changed

+55
-19
lines changed

9 files changed

+55
-19
lines changed

doc/release-notes-18982.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Notification changes
2+
--------------------
3+
4+
`-walletnotify` notifications are now sent for wallet transactions that are
5+
removed from the mempool because they conflict with a new block. These
6+
notifications were sent previously before the v0.19 release, but had been
7+
broken since that release (bug
8+
[#18325](https://github.com/bitcoin/bitcoin/issues/18325)).

src/interfaces/chain.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ class NotificationsProxy : public CValidationInterface
6363
{
6464
m_notifications->transactionAddedToMempool(tx);
6565
}
66-
void TransactionRemovedFromMempool(const CTransactionRef& tx) override
66+
void TransactionRemovedFromMempool(const CTransactionRef& tx, MemPoolRemovalReason reason) override
6767
{
68-
m_notifications->transactionRemovedFromMempool(tx);
68+
m_notifications->transactionRemovedFromMempool(tx, reason);
6969
}
7070
void BlockConnected(const std::shared_ptr<const CBlock>& block, const CBlockIndex* index) override
7171
{

src/interfaces/chain.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class CRPCCommand;
2020
class CScheduler;
2121
class Coin;
2222
class uint256;
23+
enum class MemPoolRemovalReason;
2324
enum class RBFTransactionState;
2425
struct bilingual_str;
2526
struct CBlockLocator;
@@ -239,7 +240,7 @@ class Chain
239240
public:
240241
virtual ~Notifications() {}
241242
virtual void transactionAddedToMempool(const CTransactionRef& tx) {}
242-
virtual void transactionRemovedFromMempool(const CTransactionRef& ptx) {}
243+
virtual void transactionRemovedFromMempool(const CTransactionRef& tx, MemPoolRemovalReason reason) {}
243244
virtual void blockConnected(const CBlock& block, int height) {}
244245
virtual void blockDisconnected(const CBlock& block, int height) {}
245246
virtual void updatedBlockTip() {}

src/txmempool.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ void CTxMemPool::removeUnchecked(txiter it, MemPoolRemovalReason reason)
410410
// for any reason except being included in a block. Clients interested
411411
// in transactions included in blocks can subscribe to the BlockConnected
412412
// notification.
413-
GetMainSignals().TransactionRemovedFromMempool(it->GetSharedTx());
413+
GetMainSignals().TransactionRemovedFromMempool(it->GetSharedTx(), reason);
414414
}
415415

416416
const uint256 hash = it->GetTx().GetHash();

src/validationinterface.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,9 @@ void CMainSignals::TransactionAddedToMempool(const CTransactionRef &ptx) {
208208
ptx->GetWitnessHash().ToString());
209209
}
210210

211-
void CMainSignals::TransactionRemovedFromMempool(const CTransactionRef &ptx) {
212-
auto event = [ptx, this] {
213-
m_internals->Iterate([&](CValidationInterface& callbacks) { callbacks.TransactionRemovedFromMempool(ptx); });
211+
void CMainSignals::TransactionRemovedFromMempool(const CTransactionRef &ptx, MemPoolRemovalReason reason) {
212+
auto event = [ptx, reason, this] {
213+
m_internals->Iterate([&](CValidationInterface& callbacks) { callbacks.TransactionRemovedFromMempool(ptx, reason); });
214214
};
215215
ENQUEUE_AND_LOG_EVENT(event, "%s: txid=%s wtxid=%s", __func__,
216216
ptx->GetHash().ToString(),

src/validationinterface.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class CConnman;
2121
class CValidationInterface;
2222
class uint256;
2323
class CScheduler;
24+
enum class MemPoolRemovalReason;
2425

2526
/** Register subscriber */
2627
void RegisterValidationInterface(CValidationInterface* callbacks);
@@ -129,7 +130,7 @@ class CValidationInterface {
129130
*
130131
* Called on a background thread.
131132
*/
132-
virtual void TransactionRemovedFromMempool(const CTransactionRef &ptx) {}
133+
virtual void TransactionRemovedFromMempool(const CTransactionRef &ptx, MemPoolRemovalReason reason) {}
133134
/**
134135
* Notifies listeners of a block being connected.
135136
* Provides a vector of transactions evicted from the mempool as a result.
@@ -197,7 +198,7 @@ class CMainSignals {
197198

198199
void UpdatedBlockTip(const CBlockIndex *, const CBlockIndex *, bool fInitialDownload);
199200
void TransactionAddedToMempool(const CTransactionRef &);
200-
void TransactionRemovedFromMempool(const CTransactionRef &);
201+
void TransactionRemovedFromMempool(const CTransactionRef &, MemPoolRemovalReason);
201202
void BlockConnected(const std::shared_ptr<const CBlock> &, const CBlockIndex *pindex);
202203
void BlockDisconnected(const std::shared_ptr<const CBlock> &, const CBlockIndex* pindex);
203204
void ChainStateFlushed(const CBlockLocator &);

src/wallet/wallet.cpp

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <script/descriptor.h>
2222
#include <script/script.h>
2323
#include <script/signingprovider.h>
24+
#include <txmempool.h>
2425
#include <util/bip32.h>
2526
#include <util/check.h>
2627
#include <util/error.h>
@@ -1111,12 +1112,42 @@ void CWallet::transactionAddedToMempool(const CTransactionRef& ptx) {
11111112
}
11121113
}
11131114

1114-
void CWallet::transactionRemovedFromMempool(const CTransactionRef &ptx) {
1115+
void CWallet::transactionRemovedFromMempool(const CTransactionRef &ptx, MemPoolRemovalReason reason) {
11151116
LOCK(cs_wallet);
11161117
auto it = mapWallet.find(ptx->GetHash());
11171118
if (it != mapWallet.end()) {
11181119
it->second.fInMempool = false;
11191120
}
1121+
// Handle transactions that were removed from the mempool because they
1122+
// conflict with transactions in a newly connected block.
1123+
if (reason == MemPoolRemovalReason::CONFLICT) {
1124+
// Call SyncNotifications, so external -walletnotify notifications will
1125+
// be triggered for these transactions. Set Status::UNCONFIRMED instead
1126+
// of Status::CONFLICTED for a few reasons:
1127+
//
1128+
// 1. The transactionRemovedFromMempool callback does not currently
1129+
// provide the conflicting block's hash and height, and for backwards
1130+
// compatibility reasons it may not be not safe to store conflicted
1131+
// wallet transactions with a null block hash. See
1132+
// https://github.com/bitcoin/bitcoin/pull/18600#discussion_r420195993.
1133+
// 2. For most of these transactions, the wallet's internal conflict
1134+
// detection in the blockConnected handler will subsequently call
1135+
// MarkConflicted and update them with CONFLICTED status anyway. This
1136+
// applies to any wallet transaction that has inputs spent in the
1137+
// block, or that has ancestors in the wallet with inputs spent by
1138+
// the block.
1139+
// 3. Longstanding behavior since the sync implementation in
1140+
// https://github.com/bitcoin/bitcoin/pull/9371 and the prior sync
1141+
// implementation before that was to mark these transactions
1142+
// unconfirmed rather than conflicted.
1143+
//
1144+
// Nothing described above should be seen as an unchangeable requirement
1145+
// when improving this code in the future. The wallet's heuristics for
1146+
// distinguishing between conflicted and unconfirmed transactions are
1147+
// imperfect, and could be improved in general, see
1148+
// https://github.com/bitcoin-core/bitcoin-devwiki/wiki/Wallet-Transaction-Conflict-Tracking
1149+
SyncTransaction(ptx, {CWalletTx::Status::UNCONFIRMED, /* block height */ 0, /* block hash */ {}, /* index */ 0});
1150+
}
11201151
}
11211152

11221153
void CWallet::blockConnected(const CBlock& block, int height)
@@ -1129,7 +1160,7 @@ void CWallet::blockConnected(const CBlock& block, int height)
11291160
for (size_t index = 0; index < block.vtx.size(); index++) {
11301161
CWalletTx::Confirmation confirm(CWalletTx::Status::CONFIRMED, height, block_hash, index);
11311162
SyncTransaction(block.vtx[index], confirm);
1132-
transactionRemovedFromMempool(block.vtx[index]);
1163+
transactionRemovedFromMempool(block.vtx[index], MemPoolRemovalReason::BLOCK);
11331164
}
11341165
}
11351166

src/wallet/wallet.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -923,7 +923,7 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
923923
uint256 last_failed_block;
924924
};
925925
ScanResult ScanForWalletTransactions(const uint256& start_block, int start_height, Optional<int> max_height, const WalletRescanReserver& reserver, bool fUpdate);
926-
void transactionRemovedFromMempool(const CTransactionRef &ptx) override;
926+
void transactionRemovedFromMempool(const CTransactionRef &ptx, MemPoolRemovalReason reason) override;
927927
void ReacceptWalletTransactions() EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
928928
void ResendWalletTransactions();
929929
struct Balance {

test/functional/feature_notifications.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -125,20 +125,15 @@ def run_test(self):
125125

126126
# Bump tx2 as bump2 and generate a block on node 0 while
127127
# disconnected, then reconnect and check for notifications on node 1
128-
# about newly confirmed bump2 and newly conflicted tx2. Currently
129-
# only the bump2 notification is sent. Ideally, notifications would
130-
# be sent both for bump2 and tx2, which was the previous behavior
131-
# before being broken by an accidental change in PR
132-
# https://github.com/bitcoin/bitcoin/pull/16624. The bug is reported
133-
# in issue https://github.com/bitcoin/bitcoin/issues/18325.
128+
# about newly confirmed bump2 and newly conflicted tx2.
134129
disconnect_nodes(self.nodes[0], 1)
135130
bump2 = self.nodes[0].bumpfee(tx2)["txid"]
136131
self.nodes[0].generatetoaddress(1, ADDRESS_BCRT1_UNSPENDABLE)
137132
assert_equal(self.nodes[0].gettransaction(bump2)["confirmations"], 1)
138133
assert_equal(tx2 in self.nodes[1].getrawmempool(), True)
139134
connect_nodes(self.nodes[0], 1)
140135
self.sync_blocks()
141-
self.expect_wallet_notify([bump2])
136+
self.expect_wallet_notify([bump2, tx2])
142137
assert_equal(self.nodes[1].gettransaction(bump2)["confirmations"], 1)
143138

144139
# TODO: add test for `-alertnotify` large fork notifications

0 commit comments

Comments
 (0)