Skip to content

Commit 5a2a9b5

Browse files
author
MarcoFalke
committed
Merge #15652: wallet: Update transactions with current mempool after load
4bf1b1c qa: Check unconfirmed balance after loadwallet (João Barbosa) 2ebf650 wallet: Update transactions with current mempool after load (João Barbosa) 57908a7 interfaces: Add Chain::requestMempoolTransactions (João Barbosa) 0440481 wallet: Move CWallet::ReacceptWalletTransactions locks to callers (João Barbosa) Pull request description: Fixes #15591. ACKs for commit 4bf1b1: MarcoFalke: re-utACK 4bf1b1c jnewbery: utACK 4bf1b1c Tree-SHA512: 604b1057c7f9fc3772084bf6914e52dd1a68a1cfd365f907e8ec78f6f5f726bc56a3cad9f2b665642714fbf3d51e37c1184ac396460bddeafd918e8f9f7af392
2 parents 35477e9 + 4bf1b1c commit 5a2a9b5

File tree

6 files changed

+55
-10
lines changed

6 files changed

+55
-10
lines changed

src/interfaces/chain.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,13 @@ class ChainImpl : public Chain
367367
{
368368
return MakeUnique<RpcHandlerImpl>(command);
369369
}
370+
void requestMempoolTransactions(Notifications& notifications) override
371+
{
372+
LOCK2(::cs_main, ::mempool.cs);
373+
for (const CTxMemPoolEntry& entry : ::mempool.mapTx) {
374+
notifications.TransactionAddedToMempool(entry.GetSharedTx());
375+
}
376+
}
370377
};
371378
} // namespace
372379

src/interfaces/chain.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,16 @@ class Chain
269269
//! Register handler for RPC. Command is not copied, so reference
270270
//! needs to remain valid until Handler is disconnected.
271271
virtual std::unique_ptr<Handler> handleRpc(const CRPCCommand& command) = 0;
272+
273+
//! Synchronously send TransactionAddedToMempool notifications about all
274+
//! current mempool transactions to the specified handler and return after
275+
//! the last one is sent. These notifications aren't coordinated with async
276+
//! notifications sent by handleNotifications, so out of date async
277+
//! notifications from handleNotifications can arrive during and after
278+
//! synchronous notifications from requestMempoolTransactions. Clients need
279+
//! to be prepared to handle this by ignoring notifications about unknown
280+
//! removed transactions and already added new transactions.
281+
virtual void requestMempoolTransactions(Notifications& notifications) = 0;
272282
};
273283

274284
//! Interface to let node manage chain clients (wallets, or maybe tools for

src/wallet/rpcdump.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,11 @@ UniValue importaddress(const JSONRPCRequest& request)
348348
if (fRescan)
349349
{
350350
RescanWallet(*pwallet, reserver);
351-
pwallet->ReacceptWalletTransactions();
351+
{
352+
auto locked_chain = pwallet->chain().lock();
353+
LOCK(pwallet->cs_wallet);
354+
pwallet->ReacceptWalletTransactions(*locked_chain);
355+
}
352356
}
353357

354358
return NullUniValue;
@@ -532,7 +536,11 @@ UniValue importpubkey(const JSONRPCRequest& request)
532536
if (fRescan)
533537
{
534538
RescanWallet(*pwallet, reserver);
535-
pwallet->ReacceptWalletTransactions();
539+
{
540+
auto locked_chain = pwallet->chain().lock();
541+
LOCK(pwallet->cs_wallet);
542+
pwallet->ReacceptWalletTransactions(*locked_chain);
543+
}
536544
}
537545

538546
return NullUniValue;
@@ -1468,7 +1476,11 @@ UniValue importmulti(const JSONRPCRequest& mainRequest)
14681476
}
14691477
if (fRescan && fRunScan && requests.size()) {
14701478
int64_t scannedTime = pwallet->RescanFromTime(nLowestTimestamp, reserver, true /* update */);
1471-
pwallet->ReacceptWalletTransactions();
1479+
{
1480+
auto locked_chain = pwallet->chain().lock();
1481+
LOCK(pwallet->cs_wallet);
1482+
pwallet->ReacceptWalletTransactions(*locked_chain);
1483+
}
14721484

14731485
if (pwallet->IsAbortingRescan()) {
14741486
throw JSONRPCError(RPC_MISC_ERROR, "Rescan aborted by user.");

src/wallet/wallet.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1861,13 +1861,11 @@ CWallet::ScanResult CWallet::ScanForWalletTransactions(const uint256& start_bloc
18611861
return result;
18621862
}
18631863

1864-
void CWallet::ReacceptWalletTransactions()
1864+
void CWallet::ReacceptWalletTransactions(interfaces::Chain::Lock& locked_chain)
18651865
{
18661866
// If transactions aren't being broadcasted, don't let them into local mempool either
18671867
if (!fBroadcastTransactions)
18681868
return;
1869-
auto locked_chain = chain().lock();
1870-
LOCK(cs_wallet);
18711869
std::map<int64_t, CWalletTx*> mapSorted;
18721870

18731871
// Sort pending wallet transactions based on their initial wallet insertion order
@@ -1877,7 +1875,7 @@ void CWallet::ReacceptWalletTransactions()
18771875
CWalletTx& wtx = item.second;
18781876
assert(wtx.GetHash() == wtxid);
18791877

1880-
int nDepth = wtx.GetDepthInMainChain(*locked_chain);
1878+
int nDepth = wtx.GetDepthInMainChain(locked_chain);
18811879

18821880
if (!wtx.IsCoinBase() && (nDepth == 0 && !wtx.isAbandoned())) {
18831881
mapSorted.insert(std::make_pair(wtx.nOrderPos, &wtx));
@@ -1888,7 +1886,7 @@ void CWallet::ReacceptWalletTransactions()
18881886
for (const std::pair<const int64_t, CWalletTx*>& item : mapSorted) {
18891887
CWalletTx& wtx = *(item.second);
18901888
CValidationState state;
1891-
wtx.AcceptToMemoryPool(*locked_chain, state);
1889+
wtx.AcceptToMemoryPool(locked_chain, state);
18921890
}
18931891
}
18941892

@@ -4398,9 +4396,15 @@ std::shared_ptr<CWallet> CWallet::CreateWalletFromFile(interfaces::Chain& chain,
43984396

43994397
void CWallet::postInitProcess()
44004398
{
4399+
auto locked_chain = chain().lock();
4400+
LOCK(cs_wallet);
4401+
44014402
// Add wallet transactions that aren't already in a block to mempool
44024403
// Do this here as mempool requires genesis block to be loaded
4403-
ReacceptWalletTransactions();
4404+
ReacceptWalletTransactions(*locked_chain);
4405+
4406+
// Update wallet transactions with current mempool transactions.
4407+
chain().requestMempoolTransactions(*this);
44044408
}
44054409

44064410
bool CWallet::BackupWallet(const std::string& strDest)

src/wallet/wallet.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -945,7 +945,7 @@ class CWallet final : public CCryptoKeyStore, private interfaces::Chain::Notific
945945
};
946946
ScanResult ScanForWalletTransactions(const uint256& first_block, const uint256& last_block, const WalletRescanReserver& reserver, bool fUpdate);
947947
void TransactionRemovedFromMempool(const CTransactionRef &ptx) override;
948-
void ReacceptWalletTransactions();
948+
void ReacceptWalletTransactions(interfaces::Chain::Lock& locked_chain) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
949949
void ResendWalletTransactions(interfaces::Chain::Lock& locked_chain, int64_t nBestBlockTime) override;
950950
// ResendWalletTransactionsBefore may only be called if fBroadcastTransactions!
951951
std::vector<uint256> ResendWalletTransactionsBefore(interfaces::Chain::Lock& locked_chain, int64_t nTime);

test/functional/wallet_balance.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,5 +129,17 @@ def run_test(self):
129129
# getbalance with minconf=2 will show the new balance.
130130
assert_equal(self.nodes[1].getbalance(minconf=2), Decimal('0'))
131131

132+
# check mempool transactions count for wallet unconfirmed balance after
133+
# dynamically loading the wallet.
134+
before = self.nodes[1].getunconfirmedbalance()
135+
dst = self.nodes[1].getnewaddress()
136+
self.nodes[1].unloadwallet('')
137+
self.nodes[0].sendtoaddress(dst, 0.1)
138+
self.sync_all()
139+
self.nodes[1].loadwallet('')
140+
after = self.nodes[1].getunconfirmedbalance()
141+
assert_equal(before + Decimal('0.1'), after)
142+
143+
132144
if __name__ == '__main__':
133145
WalletTest().main()

0 commit comments

Comments
 (0)