Skip to content

Commit

Permalink
Also call other wallet notify callbacks in scheduler thread
Browse files Browse the repository at this point in the history
This runs Block{Connected,Disconnected}, SetBestChain, Inventory,
and TransactionAddedToMempool on the background scheduler thread.

Of those, only BlockConnected is used outside of Wallet/ZMQ, and
is used only for orphan transaction removal in net_processing,
something which does not need to be synchronous with anything
else.

This partially reverts bitcoin#9583, re-enabling some of the gains from
bitcoin#7946. This does not, however, re-enable the gains achieved by
repeatedly releasing cs_main between each transaction processed.
  • Loading branch information
TheBlueMatt authored and furszy committed Feb 21, 2021
1 parent 31a8790 commit c7ab490
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2285,7 +2285,7 @@ bool ActivateBestChain(CValidationState& state, std::shared_ptr<const CBlock> pb

for (const PerBlockConnectTrace& trace : connectTrace.GetBlocksConnected()) {
assert(trace.pblock && trace.pindex);
GetMainSignals().BlockConnected(trace.pblock, trace.pindex, *trace.conflictedTxs);
GetMainSignals().BlockConnected(trace.pblock, trace.pindex, trace.conflictedTxs);
}

break;
Expand Down
24 changes: 16 additions & 8 deletions src/validationinterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,20 +129,28 @@ void CMainSignals::UpdatedBlockTip(const CBlockIndex* pindexNew, const CBlockInd
m_internals->UpdatedBlockTip(pindexNew, pindexFork, fInitialDownload);
}

void CMainSignals::TransactionAddedToMempool(const CTransactionRef &ptxn) {
m_internals->TransactionAddedToMempool(ptxn);
void CMainSignals::TransactionAddedToMempool(const CTransactionRef &ptx) {
m_internals->m_schedulerClient.AddToProcessQueue([ptx, this] {
m_internals->TransactionAddedToMempool(ptx);
});
}

void CMainSignals::BlockConnected(const std::shared_ptr<const CBlock> &block, const CBlockIndex *pindex, const std::vector<CTransactionRef> &txnConflicted) {
m_internals->BlockConnected(block, pindex, txnConflicted);
void CMainSignals::BlockConnected(const std::shared_ptr<const CBlock> &pblock, const CBlockIndex *pindex, const std::shared_ptr<const std::vector<CTransactionRef>>& pvtxConflicted) {
m_internals->m_schedulerClient.AddToProcessQueue([pblock, pindex, pvtxConflicted, this] {
m_internals->BlockConnected(pblock, pindex, *pvtxConflicted);
});
}

void CMainSignals::BlockDisconnected(const std::shared_ptr<const CBlock> &block, int nBlockHeight) {
m_internals->BlockDisconnected(block, nBlockHeight);
void CMainSignals::BlockDisconnected(const std::shared_ptr<const CBlock> &pblock, int nBlockHeight) {
m_internals->m_schedulerClient.AddToProcessQueue([pblock, nBlockHeight, this] {
m_internals->BlockDisconnected(pblock, nBlockHeight);
});
}

void CMainSignals::SetBestChain(const CBlockLocator& locator) {
m_internals->SetBestChain(locator);
void CMainSignals::SetBestChain(const CBlockLocator &locator) {
m_internals->m_schedulerClient.AddToProcessQueue([locator, this] {
m_internals->SetBestChain(locator);
});
}

void CMainSignals::Broadcast(CConnman* connman) {
Expand Down
24 changes: 22 additions & 2 deletions src/validationinterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ class CValidationInterface {
protected:
/** Notifies listeners of updated block chain tip */
virtual void UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload) {}
/**
* Notifies listeners of a transaction having been added to mempool.
*
* Called on a background thread.
*/
virtual void TransactionAddedToMempool(const CTransactionRef &ptxn) {}
/**
* Notifies listeners of a transaction leaving mempool.
Expand All @@ -62,9 +67,24 @@ class CValidationInterface {
* Called on a background thread.
*/
virtual void TransactionRemovedFromMempool(const CTransactionRef &ptx) {}
/**
* Notifies listeners of a block being connected.
* Provides a vector of transactions evicted from the mempool as a result.
*
* Called on a background thread.
*/
virtual void BlockConnected(const std::shared_ptr<const CBlock> &block, const CBlockIndex *pindex, const std::vector<CTransactionRef> &txnConflicted) {}
/**
* Notifies listeners of a block being disconnected
*
* Called on a background thread.
*/
virtual void BlockDisconnected(const std::shared_ptr<const CBlock> &block, int nBlockHeight) {}
/** Notifies listeners of the new active block chain on-disk. */
/**
* Notifies listeners of the new active block chain on-disk.
*
* Called on a background thread.
*/
virtual void SetBestChain(const CBlockLocator &locator) {}
/** Tells listeners to broadcast their data. */
virtual void ResendWalletTransactions(CConnman* connman) {}
Expand Down Expand Up @@ -101,7 +121,7 @@ class CMainSignals {

void UpdatedBlockTip(const CBlockIndex *, const CBlockIndex *, bool fInitialDownload);
void TransactionAddedToMempool(const CTransactionRef &ptxn);
void BlockConnected(const std::shared_ptr<const CBlock> &block, const CBlockIndex *pindex, const std::vector<CTransactionRef> &txnConflicted);
void BlockConnected(const std::shared_ptr<const CBlock> &block, const CBlockIndex *pindex, const std::shared_ptr<const std::vector<CTransactionRef>> &);
void BlockDisconnected(const std::shared_ptr<const CBlock> &block, int nBlockHeight);
void SetBestChain(const CBlockLocator &);
void Broadcast(CConnman* connman);
Expand Down

0 comments on commit c7ab490

Please sign in to comment.