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.cpp 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Copyright (c) 2009-2010 Satoshi Nakamoto
  2. // Copyright (c) 2009-2016 The Starwels developers
  3. // Distributed under the MIT software license, see the accompanying
  4. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  5. #include "validationinterface.h"
  6. #include "init.h"
  7. #include "primitives/block.h"
  8. #include "scheduler.h"
  9. #include "sync.h"
  10. #include "util.h"
  11. #include <list>
  12. #include <atomic>
  13. #include <boost/signals2/signal.hpp>
  14. struct MainSignalsInstance {
  15. boost::signals2::signal<void (const CBlockIndex *, const CBlockIndex *, bool fInitialDownload)> UpdatedBlockTip;
  16. boost::signals2::signal<void (const CTransactionRef &)> TransactionAddedToMempool;
  17. boost::signals2::signal<void (const std::shared_ptr<const CBlock> &, const CBlockIndex *pindex, const std::vector<CTransactionRef>&)> BlockConnected;
  18. boost::signals2::signal<void (const std::shared_ptr<const CBlock> &)> BlockDisconnected;
  19. boost::signals2::signal<void (const CBlockLocator &)> SetBestChain;
  20. boost::signals2::signal<void (const uint256 &)> Inventory;
  21. boost::signals2::signal<void (int64_t nBestBlockTime, CConnman* connman)> Broadcast;
  22. boost::signals2::signal<void (const CBlock&, const CValidationState&)> BlockChecked;
  23. boost::signals2::signal<void (const CBlockIndex *, const std::shared_ptr<const CBlock>&)> NewPoWValidBlock;
  24. // We are not allowed to assume the scheduler only runs in one thread,
  25. // but must ensure all callbacks happen in-order, so we end up creating
  26. // our own queue here :(
  27. SingleThreadedSchedulerClient m_schedulerClient;
  28. MainSignalsInstance(CScheduler *pscheduler) : m_schedulerClient(pscheduler) {}
  29. };
  30. static CMainSignals g_signals;
  31. void CMainSignals::RegisterBackgroundSignalScheduler(CScheduler& scheduler) {
  32. assert(!m_internals);
  33. m_internals.reset(new MainSignalsInstance(&scheduler));
  34. }
  35. void CMainSignals::UnregisterBackgroundSignalScheduler() {
  36. m_internals.reset(nullptr);
  37. }
  38. void CMainSignals::FlushBackgroundCallbacks() {
  39. m_internals->m_schedulerClient.EmptyQueue();
  40. }
  41. CMainSignals& GetMainSignals()
  42. {
  43. return g_signals;
  44. }
  45. void RegisterValidationInterface(CValidationInterface* pwalletIn) {
  46. g_signals.m_internals->UpdatedBlockTip.connect(boost::bind(&CValidationInterface::UpdatedBlockTip, pwalletIn, _1, _2, _3));
  47. g_signals.m_internals->TransactionAddedToMempool.connect(boost::bind(&CValidationInterface::TransactionAddedToMempool, pwalletIn, _1));
  48. g_signals.m_internals->BlockConnected.connect(boost::bind(&CValidationInterface::BlockConnected, pwalletIn, _1, _2, _3));
  49. g_signals.m_internals->BlockDisconnected.connect(boost::bind(&CValidationInterface::BlockDisconnected, pwalletIn, _1));
  50. g_signals.m_internals->SetBestChain.connect(boost::bind(&CValidationInterface::SetBestChain, pwalletIn, _1));
  51. g_signals.m_internals->Inventory.connect(boost::bind(&CValidationInterface::Inventory, pwalletIn, _1));
  52. g_signals.m_internals->Broadcast.connect(boost::bind(&CValidationInterface::ResendWalletTransactions, pwalletIn, _1, _2));
  53. g_signals.m_internals->BlockChecked.connect(boost::bind(&CValidationInterface::BlockChecked, pwalletIn, _1, _2));
  54. g_signals.m_internals->NewPoWValidBlock.connect(boost::bind(&CValidationInterface::NewPoWValidBlock, pwalletIn, _1, _2));
  55. }
  56. void UnregisterValidationInterface(CValidationInterface* pwalletIn) {
  57. g_signals.m_internals->BlockChecked.disconnect(boost::bind(&CValidationInterface::BlockChecked, pwalletIn, _1, _2));
  58. g_signals.m_internals->Broadcast.disconnect(boost::bind(&CValidationInterface::ResendWalletTransactions, pwalletIn, _1, _2));
  59. g_signals.m_internals->Inventory.disconnect(boost::bind(&CValidationInterface::Inventory, pwalletIn, _1));
  60. g_signals.m_internals->SetBestChain.disconnect(boost::bind(&CValidationInterface::SetBestChain, pwalletIn, _1));
  61. g_signals.m_internals->TransactionAddedToMempool.disconnect(boost::bind(&CValidationInterface::TransactionAddedToMempool, pwalletIn, _1));
  62. g_signals.m_internals->BlockConnected.disconnect(boost::bind(&CValidationInterface::BlockConnected, pwalletIn, _1, _2, _3));
  63. g_signals.m_internals->BlockDisconnected.disconnect(boost::bind(&CValidationInterface::BlockDisconnected, pwalletIn, _1));
  64. g_signals.m_internals->UpdatedBlockTip.disconnect(boost::bind(&CValidationInterface::UpdatedBlockTip, pwalletIn, _1, _2, _3));
  65. g_signals.m_internals->NewPoWValidBlock.disconnect(boost::bind(&CValidationInterface::NewPoWValidBlock, pwalletIn, _1, _2));
  66. }
  67. void UnregisterAllValidationInterfaces() {
  68. g_signals.m_internals->BlockChecked.disconnect_all_slots();
  69. g_signals.m_internals->Broadcast.disconnect_all_slots();
  70. g_signals.m_internals->Inventory.disconnect_all_slots();
  71. g_signals.m_internals->SetBestChain.disconnect_all_slots();
  72. g_signals.m_internals->TransactionAddedToMempool.disconnect_all_slots();
  73. g_signals.m_internals->BlockConnected.disconnect_all_slots();
  74. g_signals.m_internals->BlockDisconnected.disconnect_all_slots();
  75. g_signals.m_internals->UpdatedBlockTip.disconnect_all_slots();
  76. g_signals.m_internals->NewPoWValidBlock.disconnect_all_slots();
  77. }
  78. void CMainSignals::UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload) {
  79. m_internals->UpdatedBlockTip(pindexNew, pindexFork, fInitialDownload);
  80. }
  81. void CMainSignals::TransactionAddedToMempool(const CTransactionRef &ptx) {
  82. m_internals->TransactionAddedToMempool(ptx);
  83. }
  84. void CMainSignals::BlockConnected(const std::shared_ptr<const CBlock> &pblock, const CBlockIndex *pindex, const std::vector<CTransactionRef>& vtxConflicted) {
  85. m_internals->BlockConnected(pblock, pindex, vtxConflicted);
  86. }
  87. void CMainSignals::BlockDisconnected(const std::shared_ptr<const CBlock> &pblock) {
  88. m_internals->BlockDisconnected(pblock);
  89. }
  90. void CMainSignals::SetBestChain(const CBlockLocator &locator) {
  91. m_internals->SetBestChain(locator);
  92. }
  93. void CMainSignals::Inventory(const uint256 &hash) {
  94. m_internals->Inventory(hash);
  95. }
  96. void CMainSignals::Broadcast(int64_t nBestBlockTime, CConnman* connman) {
  97. m_internals->Broadcast(nBestBlockTime, connman);
  98. }
  99. void CMainSignals::BlockChecked(const CBlock& block, const CValidationState& state) {
  100. m_internals->BlockChecked(block, state);
  101. }
  102. void CMainSignals::NewPoWValidBlock(const CBlockIndex *pindex, const std::shared_ptr<const CBlock> &block) {
  103. m_internals->NewPoWValidBlock(pindex, block);
  104. }