You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

validationinterface.h 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // Copyright (c) 2009-2010 Satoshi Nakamoto
  2. // Copyright (c) 2009-2017 The Bitcoin Core developers
  3. // Distributed under the MIT software license, see the accompanying
  4. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  5. #ifndef BITCOIN_VALIDATIONINTERFACE_H
  6. #define BITCOIN_VALIDATIONINTERFACE_H
  7. #include <primitives/transaction.h> // CTransaction(Ref)
  8. #include <functional>
  9. #include <memory>
  10. class CBlock;
  11. class CBlockIndex;
  12. struct CBlockLocator;
  13. class CBlockIndex;
  14. class CConnman;
  15. class CReserveScript;
  16. class CValidationInterface;
  17. class CValidationState;
  18. class uint256;
  19. class CScheduler;
  20. class CTxMemPool;
  21. enum class MemPoolRemovalReason;
  22. // These functions dispatch to one or all registered wallets
  23. /** Register a wallet to receive updates from core */
  24. void RegisterValidationInterface(CValidationInterface* pwalletIn);
  25. /** Unregister a wallet from core */
  26. void UnregisterValidationInterface(CValidationInterface* pwalletIn);
  27. /** Unregister all wallets from core */
  28. void UnregisterAllValidationInterfaces();
  29. /**
  30. * Pushes a function to callback onto the notification queue, guaranteeing any
  31. * callbacks generated prior to now are finished when the function is called.
  32. *
  33. * Be very careful blocking on func to be called if any locks are held -
  34. * validation interface clients may not be able to make progress as they often
  35. * wait for things like cs_main, so blocking until func is called with cs_main
  36. * will result in a deadlock (that DEBUG_LOCKORDER will miss).
  37. */
  38. void CallFunctionInValidationInterfaceQueue(std::function<void ()> func);
  39. /**
  40. * This is a synonym for the following, which asserts certain locks are not
  41. * held:
  42. * std::promise<void> promise;
  43. * CallFunctionInValidationInterfaceQueue([&promise] {
  44. * promise.set_value();
  45. * });
  46. * promise.get_future().wait();
  47. */
  48. void SyncWithValidationInterfaceQueue();
  49. class CValidationInterface {
  50. protected:
  51. /**
  52. * Notifies listeners when the block chain tip advances.
  53. *
  54. * When multiple blocks are connected at once, UpdatedBlockTip will be called on the final tip
  55. * but may not be called on every intermediate tip. If the latter behavior is desired,
  56. * subscribe to BlockConnected() instead.
  57. *
  58. * Called on a background thread.
  59. */
  60. virtual void UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload) {}
  61. /**
  62. * Notifies listeners of a transaction having been added to mempool.
  63. *
  64. * Called on a background thread.
  65. */
  66. virtual void TransactionAddedToMempool(const CTransactionRef &ptxn) {}
  67. /**
  68. * Notifies listeners of a transaction leaving mempool.
  69. *
  70. * This only fires for transactions which leave mempool because of expiry,
  71. * size limiting, reorg (changes in lock times/coinbase maturity), or
  72. * replacement. This does not include any transactions which are included
  73. * in BlockConnectedDisconnected either in block->vtx or in txnConflicted.
  74. *
  75. * Called on a background thread.
  76. */
  77. virtual void TransactionRemovedFromMempool(const CTransactionRef &ptx) {}
  78. /**
  79. * Notifies listeners of a block being connected.
  80. * Provides a vector of transactions evicted from the mempool as a result.
  81. *
  82. * Called on a background thread.
  83. */
  84. virtual void BlockConnected(const std::shared_ptr<const CBlock> &block, const CBlockIndex *pindex, const std::vector<CTransactionRef> &txnConflicted) {}
  85. /**
  86. * Notifies listeners of a block being disconnected
  87. *
  88. * Called on a background thread.
  89. */
  90. virtual void BlockDisconnected(const std::shared_ptr<const CBlock> &block) {}
  91. /**
  92. * Notifies listeners of the new active block chain on-disk.
  93. *
  94. * Called on a background thread.
  95. */
  96. virtual void SetBestChain(const CBlockLocator &locator) {}
  97. /**
  98. * Notifies listeners about an inventory item being seen on the network.
  99. *
  100. * Called on a background thread.
  101. */
  102. virtual void Inventory(const uint256 &hash) {}
  103. /** Tells listeners to broadcast their data. */
  104. virtual void ResendWalletTransactions(int64_t nBestBlockTime, CConnman* connman) {}
  105. /**
  106. * Notifies listeners of a block validation result.
  107. * If the provided CValidationState IsValid, the provided block
  108. * is guaranteed to be the current best block at the time the
  109. * callback was generated (not necessarily now)
  110. */
  111. virtual void BlockChecked(const CBlock&, const CValidationState&) {}
  112. /**
  113. * Notifies listeners that a block which builds directly on our current tip
  114. * has been received and connected to the headers tree, though not validated yet */
  115. virtual void NewPoWValidBlock(const CBlockIndex *pindex, const std::shared_ptr<const CBlock>& block) {};
  116. friend void ::RegisterValidationInterface(CValidationInterface*);
  117. friend void ::UnregisterValidationInterface(CValidationInterface*);
  118. friend void ::UnregisterAllValidationInterfaces();
  119. };
  120. struct MainSignalsInstance;
  121. class CMainSignals {
  122. private:
  123. std::unique_ptr<MainSignalsInstance> m_internals;
  124. friend void ::RegisterValidationInterface(CValidationInterface*);
  125. friend void ::UnregisterValidationInterface(CValidationInterface*);
  126. friend void ::UnregisterAllValidationInterfaces();
  127. friend void ::CallFunctionInValidationInterfaceQueue(std::function<void ()> func);
  128. void MempoolEntryRemoved(CTransactionRef tx, MemPoolRemovalReason reason);
  129. public:
  130. /** Register a CScheduler to give callbacks which should run in the background (may only be called once) */
  131. void RegisterBackgroundSignalScheduler(CScheduler& scheduler);
  132. /** Unregister a CScheduler to give callbacks which should run in the background - these callbacks will now be dropped! */
  133. void UnregisterBackgroundSignalScheduler();
  134. /** Call any remaining callbacks on the calling thread */
  135. void FlushBackgroundCallbacks();
  136. size_t CallbacksPending();
  137. /** Register with mempool to call TransactionRemovedFromMempool callbacks */
  138. void RegisterWithMempoolSignals(CTxMemPool& pool);
  139. /** Unregister with mempool */
  140. void UnregisterWithMempoolSignals(CTxMemPool& pool);
  141. void UpdatedBlockTip(const CBlockIndex *, const CBlockIndex *, bool fInitialDownload);
  142. void TransactionAddedToMempool(const CTransactionRef &);
  143. void BlockConnected(const std::shared_ptr<const CBlock> &, const CBlockIndex *pindex, const std::shared_ptr<const std::vector<CTransactionRef>> &);
  144. void BlockDisconnected(const std::shared_ptr<const CBlock> &);
  145. void SetBestChain(const CBlockLocator &);
  146. void Inventory(const uint256 &);
  147. void Broadcast(int64_t nBestBlockTime, CConnman* connman);
  148. void BlockChecked(const CBlock&, const CValidationState&);
  149. void NewPoWValidBlock(const CBlockIndex *, const std::shared_ptr<const CBlock>&);
  150. };
  151. CMainSignals& GetMainSignals();
  152. #endif // BITCOIN_VALIDATIONINTERFACE_H