Merge #15652: wallet: Update transactions with current mempool after load
4bf1b1cefa
qa: Check unconfirmed balance after loadwallet (João Barbosa)2ebf650b2e
wallet: Update transactions with current mempool after load (João Barbosa)57908a739c
interfaces: Add Chain::requestMempoolTransactions (João Barbosa)0440481c6b
wallet: Move CWallet::ReacceptWalletTransactions locks to callers (João Barbosa) Pull request description: Fixes #15591. ACKs for commit 4bf1b1: MarcoFalke: re-utACK4bf1b1cefa
jnewbery: utACK4bf1b1cefa
Tree-SHA512: 604b1057c7f9fc3772084bf6914e52dd1a68a1cfd365f907e8ec78f6f5f726bc56a3cad9f2b665642714fbf3d51e37c1184ac396460bddeafd918e8f9f7af392
This commit is contained in:
commit
5a2a9b5b06
6 changed files with 55 additions and 10 deletions
|
@ -367,6 +367,13 @@ public:
|
||||||
{
|
{
|
||||||
return MakeUnique<RpcHandlerImpl>(command);
|
return MakeUnique<RpcHandlerImpl>(command);
|
||||||
}
|
}
|
||||||
|
void requestMempoolTransactions(Notifications& notifications) override
|
||||||
|
{
|
||||||
|
LOCK2(::cs_main, ::mempool.cs);
|
||||||
|
for (const CTxMemPoolEntry& entry : ::mempool.mapTx) {
|
||||||
|
notifications.TransactionAddedToMempool(entry.GetSharedTx());
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
|
|
|
@ -269,6 +269,16 @@ public:
|
||||||
//! Register handler for RPC. Command is not copied, so reference
|
//! Register handler for RPC. Command is not copied, so reference
|
||||||
//! needs to remain valid until Handler is disconnected.
|
//! needs to remain valid until Handler is disconnected.
|
||||||
virtual std::unique_ptr<Handler> handleRpc(const CRPCCommand& command) = 0;
|
virtual std::unique_ptr<Handler> handleRpc(const CRPCCommand& command) = 0;
|
||||||
|
|
||||||
|
//! Synchronously send TransactionAddedToMempool notifications about all
|
||||||
|
//! current mempool transactions to the specified handler and return after
|
||||||
|
//! the last one is sent. These notifications aren't coordinated with async
|
||||||
|
//! notifications sent by handleNotifications, so out of date async
|
||||||
|
//! notifications from handleNotifications can arrive during and after
|
||||||
|
//! synchronous notifications from requestMempoolTransactions. Clients need
|
||||||
|
//! to be prepared to handle this by ignoring notifications about unknown
|
||||||
|
//! removed transactions and already added new transactions.
|
||||||
|
virtual void requestMempoolTransactions(Notifications& notifications) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
//! Interface to let node manage chain clients (wallets, or maybe tools for
|
//! Interface to let node manage chain clients (wallets, or maybe tools for
|
||||||
|
|
|
@ -348,7 +348,11 @@ UniValue importaddress(const JSONRPCRequest& request)
|
||||||
if (fRescan)
|
if (fRescan)
|
||||||
{
|
{
|
||||||
RescanWallet(*pwallet, reserver);
|
RescanWallet(*pwallet, reserver);
|
||||||
pwallet->ReacceptWalletTransactions();
|
{
|
||||||
|
auto locked_chain = pwallet->chain().lock();
|
||||||
|
LOCK(pwallet->cs_wallet);
|
||||||
|
pwallet->ReacceptWalletTransactions(*locked_chain);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return NullUniValue;
|
return NullUniValue;
|
||||||
|
@ -532,7 +536,11 @@ UniValue importpubkey(const JSONRPCRequest& request)
|
||||||
if (fRescan)
|
if (fRescan)
|
||||||
{
|
{
|
||||||
RescanWallet(*pwallet, reserver);
|
RescanWallet(*pwallet, reserver);
|
||||||
pwallet->ReacceptWalletTransactions();
|
{
|
||||||
|
auto locked_chain = pwallet->chain().lock();
|
||||||
|
LOCK(pwallet->cs_wallet);
|
||||||
|
pwallet->ReacceptWalletTransactions(*locked_chain);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return NullUniValue;
|
return NullUniValue;
|
||||||
|
@ -1468,7 +1476,11 @@ UniValue importmulti(const JSONRPCRequest& mainRequest)
|
||||||
}
|
}
|
||||||
if (fRescan && fRunScan && requests.size()) {
|
if (fRescan && fRunScan && requests.size()) {
|
||||||
int64_t scannedTime = pwallet->RescanFromTime(nLowestTimestamp, reserver, true /* update */);
|
int64_t scannedTime = pwallet->RescanFromTime(nLowestTimestamp, reserver, true /* update */);
|
||||||
pwallet->ReacceptWalletTransactions();
|
{
|
||||||
|
auto locked_chain = pwallet->chain().lock();
|
||||||
|
LOCK(pwallet->cs_wallet);
|
||||||
|
pwallet->ReacceptWalletTransactions(*locked_chain);
|
||||||
|
}
|
||||||
|
|
||||||
if (pwallet->IsAbortingRescan()) {
|
if (pwallet->IsAbortingRescan()) {
|
||||||
throw JSONRPCError(RPC_MISC_ERROR, "Rescan aborted by user.");
|
throw JSONRPCError(RPC_MISC_ERROR, "Rescan aborted by user.");
|
||||||
|
|
|
@ -1861,13 +1861,11 @@ CWallet::ScanResult CWallet::ScanForWalletTransactions(const uint256& start_bloc
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
void CWallet::ReacceptWalletTransactions()
|
void CWallet::ReacceptWalletTransactions(interfaces::Chain::Lock& locked_chain)
|
||||||
{
|
{
|
||||||
// If transactions aren't being broadcasted, don't let them into local mempool either
|
// If transactions aren't being broadcasted, don't let them into local mempool either
|
||||||
if (!fBroadcastTransactions)
|
if (!fBroadcastTransactions)
|
||||||
return;
|
return;
|
||||||
auto locked_chain = chain().lock();
|
|
||||||
LOCK(cs_wallet);
|
|
||||||
std::map<int64_t, CWalletTx*> mapSorted;
|
std::map<int64_t, CWalletTx*> mapSorted;
|
||||||
|
|
||||||
// Sort pending wallet transactions based on their initial wallet insertion order
|
// Sort pending wallet transactions based on their initial wallet insertion order
|
||||||
|
@ -1877,7 +1875,7 @@ void CWallet::ReacceptWalletTransactions()
|
||||||
CWalletTx& wtx = item.second;
|
CWalletTx& wtx = item.second;
|
||||||
assert(wtx.GetHash() == wtxid);
|
assert(wtx.GetHash() == wtxid);
|
||||||
|
|
||||||
int nDepth = wtx.GetDepthInMainChain(*locked_chain);
|
int nDepth = wtx.GetDepthInMainChain(locked_chain);
|
||||||
|
|
||||||
if (!wtx.IsCoinBase() && (nDepth == 0 && !wtx.isAbandoned())) {
|
if (!wtx.IsCoinBase() && (nDepth == 0 && !wtx.isAbandoned())) {
|
||||||
mapSorted.insert(std::make_pair(wtx.nOrderPos, &wtx));
|
mapSorted.insert(std::make_pair(wtx.nOrderPos, &wtx));
|
||||||
|
@ -1888,7 +1886,7 @@ void CWallet::ReacceptWalletTransactions()
|
||||||
for (const std::pair<const int64_t, CWalletTx*>& item : mapSorted) {
|
for (const std::pair<const int64_t, CWalletTx*>& item : mapSorted) {
|
||||||
CWalletTx& wtx = *(item.second);
|
CWalletTx& wtx = *(item.second);
|
||||||
CValidationState state;
|
CValidationState state;
|
||||||
wtx.AcceptToMemoryPool(*locked_chain, state);
|
wtx.AcceptToMemoryPool(locked_chain, state);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4398,9 +4396,15 @@ std::shared_ptr<CWallet> CWallet::CreateWalletFromFile(interfaces::Chain& chain,
|
||||||
|
|
||||||
void CWallet::postInitProcess()
|
void CWallet::postInitProcess()
|
||||||
{
|
{
|
||||||
|
auto locked_chain = chain().lock();
|
||||||
|
LOCK(cs_wallet);
|
||||||
|
|
||||||
// Add wallet transactions that aren't already in a block to mempool
|
// Add wallet transactions that aren't already in a block to mempool
|
||||||
// Do this here as mempool requires genesis block to be loaded
|
// Do this here as mempool requires genesis block to be loaded
|
||||||
ReacceptWalletTransactions();
|
ReacceptWalletTransactions(*locked_chain);
|
||||||
|
|
||||||
|
// Update wallet transactions with current mempool transactions.
|
||||||
|
chain().requestMempoolTransactions(*this);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CWallet::BackupWallet(const std::string& strDest)
|
bool CWallet::BackupWallet(const std::string& strDest)
|
||||||
|
|
|
@ -945,7 +945,7 @@ public:
|
||||||
};
|
};
|
||||||
ScanResult ScanForWalletTransactions(const uint256& first_block, const uint256& last_block, const WalletRescanReserver& reserver, bool fUpdate);
|
ScanResult ScanForWalletTransactions(const uint256& first_block, const uint256& last_block, const WalletRescanReserver& reserver, bool fUpdate);
|
||||||
void TransactionRemovedFromMempool(const CTransactionRef &ptx) override;
|
void TransactionRemovedFromMempool(const CTransactionRef &ptx) override;
|
||||||
void ReacceptWalletTransactions();
|
void ReacceptWalletTransactions(interfaces::Chain::Lock& locked_chain) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
|
||||||
void ResendWalletTransactions(interfaces::Chain::Lock& locked_chain, int64_t nBestBlockTime) override;
|
void ResendWalletTransactions(interfaces::Chain::Lock& locked_chain, int64_t nBestBlockTime) override;
|
||||||
// ResendWalletTransactionsBefore may only be called if fBroadcastTransactions!
|
// ResendWalletTransactionsBefore may only be called if fBroadcastTransactions!
|
||||||
std::vector<uint256> ResendWalletTransactionsBefore(interfaces::Chain::Lock& locked_chain, int64_t nTime);
|
std::vector<uint256> ResendWalletTransactionsBefore(interfaces::Chain::Lock& locked_chain, int64_t nTime);
|
||||||
|
|
|
@ -129,5 +129,17 @@ class WalletTest(BitcoinTestFramework):
|
||||||
# getbalance with minconf=2 will show the new balance.
|
# getbalance with minconf=2 will show the new balance.
|
||||||
assert_equal(self.nodes[1].getbalance(minconf=2), Decimal('0'))
|
assert_equal(self.nodes[1].getbalance(minconf=2), Decimal('0'))
|
||||||
|
|
||||||
|
# check mempool transactions count for wallet unconfirmed balance after
|
||||||
|
# dynamically loading the wallet.
|
||||||
|
before = self.nodes[1].getunconfirmedbalance()
|
||||||
|
dst = self.nodes[1].getnewaddress()
|
||||||
|
self.nodes[1].unloadwallet('')
|
||||||
|
self.nodes[0].sendtoaddress(dst, 0.1)
|
||||||
|
self.sync_all()
|
||||||
|
self.nodes[1].loadwallet('')
|
||||||
|
after = self.nodes[1].getunconfirmedbalance()
|
||||||
|
assert_equal(before + Decimal('0.1'), after)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
WalletTest().main()
|
WalletTest().main()
|
||||||
|
|
Loading…
Reference in a new issue