Skip to content

Commit 077eb62

Browse files
MarcoFalkeComputerCraftr
MarcoFalke
authored andcommitted
Merge bitcoin#18982: wallet: Minimal fix to restore conflicted transaction notifications
7eaf86d trivial: Suggested cleanups to surrounding code (Russell Yanofsky) b604c5c wallet: Minimal fix to restore conflicted transaction notifications (Russell Yanofsky) Pull request description: This fix is a based on the fix by Antoine Riard (ariard) in 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#16624, causing issue 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 bitcoin#18325 Co-authored-by: Antoine Riard (ariard) ACKs for top commit: jonatack: Re-ACK 7eaf86d ariard: ACK 7eaf86d, reviewed, built and ran tests. MarcoFalke: ACK 7eaf86d dango Tree-SHA512: 9a1efe975969bb522a9dd73c41064a9348887cb67883cd92c6571fd2df4321b9f4568363891abdaae14a3b9b168ef8142e95c373fc04677e46289b251fb84689
1 parent 1cf6ab1 commit 077eb62

9 files changed

+71
-39
lines changed

doc/release-notes-18982.md

+8
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

+2-2
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,9 @@ class NotificationsProxy : public CValidationInterface
158158
{
159159
m_notifications->transactionAddedToMempool(tx);
160160
}
161-
void TransactionRemovedFromMempool(const CTransactionRef& tx) override
161+
void TransactionRemovedFromMempool(const CTransactionRef& tx, MemPoolRemovalReason reason) override
162162
{
163-
m_notifications->transactionRemovedFromMempool(tx);
163+
m_notifications->transactionRemovedFromMempool(tx, reason);
164164
}
165165
void BlockConnected(const std::shared_ptr<const CBlock>& block, const CBlockIndex* index) override
166166
{

src/interfaces/chain.h

+2-1
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 CBlockLocator;
2526
struct FeeCalculation;
@@ -221,7 +222,7 @@ class Chain
221222
public:
222223
virtual ~Notifications() {}
223224
virtual void transactionAddedToMempool(const CTransactionRef& tx) {}
224-
virtual void transactionRemovedFromMempool(const CTransactionRef& ptx) {}
225+
virtual void transactionRemovedFromMempool(const CTransactionRef& tx, MemPoolRemovalReason reason) {}
225226
virtual void blockConnected(const CBlock& block, int height) {}
226227
virtual void blockDisconnected(const CBlock& block, int height) {}
227228
virtual void updatedBlockTip() {}

src/txmempool.cpp

+1-1
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

+10-10
Original file line numberDiff line numberDiff line change
@@ -190,22 +190,22 @@ void CMainSignals::UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockInd
190190
fInitialDownload);
191191
}
192192

193-
void CMainSignals::TransactionAddedToMempool(const CTransactionRef &ptx) {
194-
auto event = [ptx, this] {
195-
m_internals->Iterate([&](CValidationInterface& callbacks) { callbacks.TransactionAddedToMempool(ptx); });
193+
void CMainSignals::TransactionAddedToMempool(const CTransactionRef& tx) {
194+
auto event = [tx, this] {
195+
m_internals->Iterate([&](CValidationInterface& callbacks) { callbacks.TransactionAddedToMempool(tx); });
196196
};
197197
ENQUEUE_AND_LOG_EVENT(event, "%s: txid=%s wtxid=%s", __func__,
198-
ptx->GetHash().ToString(),
199-
ptx->GetWitnessHash().ToString());
198+
tx->GetHash().ToString(),
199+
tx->GetWitnessHash().ToString());
200200
}
201201

202-
void CMainSignals::TransactionRemovedFromMempool(const CTransactionRef &ptx) {
203-
auto event = [ptx, this] {
204-
m_internals->Iterate([&](CValidationInterface& callbacks) { callbacks.TransactionRemovedFromMempool(ptx); });
202+
void CMainSignals::TransactionRemovedFromMempool(const CTransactionRef& tx, MemPoolRemovalReason reason) {
203+
auto event = [tx, reason, this] {
204+
m_internals->Iterate([&](CValidationInterface& callbacks) { callbacks.TransactionRemovedFromMempool(tx, reason); });
205205
};
206206
ENQUEUE_AND_LOG_EVENT(event, "%s: txid=%s wtxid=%s", __func__,
207-
ptx->GetHash().ToString(),
208-
ptx->GetWitnessHash().ToString());
207+
tx->GetHash().ToString(),
208+
tx->GetWitnessHash().ToString());
209209
}
210210

211211
void CMainSignals::BlockConnected(const std::shared_ptr<const CBlock> &pblock, const CBlockIndex *pindex) {

src/validationinterface.h

+5-4
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
// These functions dispatch to one or all registered wallets
2627

@@ -96,7 +97,7 @@ class CValidationInterface {
9697
*
9798
* Called on a background thread.
9899
*/
99-
virtual void TransactionAddedToMempool(const CTransactionRef &ptxn) {}
100+
virtual void TransactionAddedToMempool(const CTransactionRef& tx) {}
100101
/**
101102
* Notifies listeners of a transaction leaving mempool.
102103
*
@@ -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& tx, MemPoolRemovalReason reason) {}
133134
/**
134135
* Notifies listeners of a block being connected.
135136
* Provides a vector of transactions evicted from the mempool as a result.
@@ -196,8 +197,8 @@ class CMainSignals {
196197

197198

198199
void UpdatedBlockTip(const CBlockIndex *, const CBlockIndex *, bool fInitialDownload);
199-
void TransactionAddedToMempool(const CTransactionRef &);
200-
void TransactionRemovedFromMempool(const CTransactionRef &);
200+
void TransactionAddedToMempool(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

+40-13
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/error.h>
2627
#include <util/fees.h>
@@ -1093,24 +1094,53 @@ void CWallet::SyncTransaction(const CTransactionRef& ptx, CWalletTx::Confirmatio
10931094
MarkInputsDirty(ptx);
10941095
}
10951096

1096-
void CWallet::transactionAddedToMempool(const CTransactionRef& ptx) {
1097+
void CWallet::transactionAddedToMempool(const CTransactionRef& tx) {
10971098
auto locked_chain = chain().lock();
10981099
LOCK(cs_wallet);
1099-
CWalletTx::Confirmation confirm(CWalletTx::Status::UNCONFIRMED, /* block_height */ 0, {}, /* nIndex */ 0);
1100-
SyncTransaction(ptx, confirm);
1100+
SyncTransaction(tx, {CWalletTx::Status::UNCONFIRMED, /* block height */ 0, /* block hash */ {}, /* index */ 0});
11011101

1102-
auto it = mapWallet.find(ptx->GetHash());
1102+
auto it = mapWallet.find(tx->GetHash());
11031103
if (it != mapWallet.end()) {
11041104
it->second.fInMempool = true;
11051105
}
11061106
}
11071107

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

11161146
void CWallet::blockConnected(const CBlock& block, int height)
@@ -1122,9 +1152,8 @@ void CWallet::blockConnected(const CBlock& block, int height)
11221152
m_last_block_processed_height = height;
11231153
m_last_block_processed = block_hash;
11241154
for (size_t index = 0; index < block.vtx.size(); index++) {
1125-
CWalletTx::Confirmation confirm(CWalletTx::Status::CONFIRMED, height, block_hash, index);
1126-
SyncTransaction(block.vtx[index], confirm);
1127-
transactionRemovedFromMempool(block.vtx[index]);
1155+
SyncTransaction(block.vtx[index], {CWalletTx::Status::CONFIRMED, height, block_hash, (int)index});
1156+
transactionRemovedFromMempool(block.vtx[index], MemPoolRemovalReason::BLOCK);
11281157
}
11291158
}
11301159

@@ -1140,8 +1169,7 @@ void CWallet::blockDisconnected(const CBlock& block, int height)
11401169
m_last_block_processed_height = height - 1;
11411170
m_last_block_processed = block.hashPrevBlock;
11421171
for (const CTransactionRef& ptx : block.vtx) {
1143-
CWalletTx::Confirmation confirm(CWalletTx::Status::UNCONFIRMED, /* block_height */ 0, {}, /* nIndex */ 0);
1144-
SyncTransaction(ptx, confirm);
1172+
SyncTransaction(ptx, {CWalletTx::Status::UNCONFIRMED, /* block height */ 0, /* block hash */ {}, /* index */ 0});
11451173
}
11461174
}
11471175

@@ -1690,8 +1718,7 @@ CWallet::ScanResult CWallet::ScanForWalletTransactions(const uint256& start_bloc
16901718
break;
16911719
}
16921720
for (size_t posInBlock = 0; posInBlock < block.vtx.size(); ++posInBlock) {
1693-
CWalletTx::Confirmation confirm(CWalletTx::Status::CONFIRMED, *block_height, block_hash, posInBlock);
1694-
SyncTransaction(block.vtx[posInBlock], confirm, fUpdate);
1721+
SyncTransaction(block.vtx[posInBlock], {CWalletTx::Status::CONFIRMED, *block_height, block_hash, (int)posInBlock}, fUpdate);
16951722
}
16961723
// scan succeeded, record block as most recent successfully scanned
16971724
result.last_scanned_block = block_hash;

src/wallet/wallet.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -909,7 +909,7 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
909909
uint256 last_failed_block;
910910
};
911911
ScanResult ScanForWalletTransactions(const uint256& first_block, const uint256& last_block, const WalletRescanReserver& reserver, bool fUpdate);
912-
void transactionRemovedFromMempool(const CTransactionRef &ptx) override;
912+
void transactionRemovedFromMempool(const CTransactionRef& tx, MemPoolRemovalReason reason) override;
913913
void ReacceptWalletTransactions() EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
914914
void ResendWalletTransactions();
915915
struct Balance {

test/functional/feature_notifications.py

+2-7
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)