2015-02-05 01:11:44 +01:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2018-01-02 18:12:05 +01:00
|
|
|
// Copyright (c) 2009-2017 The Bitcoin Core developers
|
2015-02-05 01:11:44 +01:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2017-11-10 01:57:53 +01:00
|
|
|
#include <validationinterface.h>
|
2017-10-03 00:24:59 +02:00
|
|
|
|
2017-11-10 01:57:53 +01:00
|
|
|
#include <init.h>
|
|
|
|
#include <primitives/block.h>
|
|
|
|
#include <scheduler.h>
|
|
|
|
#include <sync.h>
|
|
|
|
#include <txmempool.h>
|
|
|
|
#include <util.h>
|
2017-12-24 18:13:13 +01:00
|
|
|
#include <validation.h>
|
2017-04-10 20:55:49 +02:00
|
|
|
|
|
|
|
#include <list>
|
|
|
|
#include <atomic>
|
2017-12-24 18:13:13 +01:00
|
|
|
#include <future>
|
2015-02-05 01:11:44 +01:00
|
|
|
|
2017-01-19 22:17:14 +01:00
|
|
|
#include <boost/signals2/signal.hpp>
|
|
|
|
|
|
|
|
struct MainSignalsInstance {
|
|
|
|
boost::signals2::signal<void (const CBlockIndex *, const CBlockIndex *, bool fInitialDownload)> UpdatedBlockTip;
|
|
|
|
boost::signals2::signal<void (const CTransactionRef &)> TransactionAddedToMempool;
|
|
|
|
boost::signals2::signal<void (const std::shared_ptr<const CBlock> &, const CBlockIndex *pindex, const std::vector<CTransactionRef>&)> BlockConnected;
|
|
|
|
boost::signals2::signal<void (const std::shared_ptr<const CBlock> &)> BlockDisconnected;
|
2017-01-20 21:08:14 +01:00
|
|
|
boost::signals2::signal<void (const CTransactionRef &)> TransactionRemovedFromMempool;
|
2017-01-19 22:17:14 +01:00
|
|
|
boost::signals2::signal<void (const CBlockLocator &)> SetBestChain;
|
|
|
|
boost::signals2::signal<void (const uint256 &)> Inventory;
|
|
|
|
boost::signals2::signal<void (int64_t nBestBlockTime, CConnman* connman)> Broadcast;
|
|
|
|
boost::signals2::signal<void (const CBlock&, const CValidationState&)> BlockChecked;
|
|
|
|
boost::signals2::signal<void (const CBlockIndex *, const std::shared_ptr<const CBlock>&)> NewPoWValidBlock;
|
2017-01-19 22:49:22 +01:00
|
|
|
|
2017-04-10 20:55:49 +02:00
|
|
|
// We are not allowed to assume the scheduler only runs in one thread,
|
|
|
|
// but must ensure all callbacks happen in-order, so we end up creating
|
|
|
|
// our own queue here :(
|
|
|
|
SingleThreadedSchedulerClient m_schedulerClient;
|
|
|
|
|
2017-08-01 12:22:41 +02:00
|
|
|
explicit MainSignalsInstance(CScheduler *pscheduler) : m_schedulerClient(pscheduler) {}
|
2017-01-19 22:17:14 +01:00
|
|
|
};
|
|
|
|
|
2015-02-05 01:11:44 +01:00
|
|
|
static CMainSignals g_signals;
|
|
|
|
|
2017-01-19 22:49:22 +01:00
|
|
|
void CMainSignals::RegisterBackgroundSignalScheduler(CScheduler& scheduler) {
|
2017-04-10 20:55:49 +02:00
|
|
|
assert(!m_internals);
|
|
|
|
m_internals.reset(new MainSignalsInstance(&scheduler));
|
2017-01-19 22:49:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CMainSignals::UnregisterBackgroundSignalScheduler() {
|
2017-04-10 20:55:49 +02:00
|
|
|
m_internals.reset(nullptr);
|
2017-01-19 22:49:22 +01:00
|
|
|
}
|
|
|
|
|
2017-06-28 01:07:52 +02:00
|
|
|
void CMainSignals::FlushBackgroundCallbacks() {
|
2017-11-28 12:08:09 +01:00
|
|
|
if (m_internals) {
|
|
|
|
m_internals->m_schedulerClient.EmptyQueue();
|
|
|
|
}
|
2017-06-28 01:07:52 +02:00
|
|
|
}
|
|
|
|
|
2017-12-05 00:31:36 +01:00
|
|
|
size_t CMainSignals::CallbacksPending() {
|
|
|
|
if (!m_internals) return 0;
|
|
|
|
return m_internals->m_schedulerClient.CallbacksPending();
|
|
|
|
}
|
|
|
|
|
2017-01-20 21:08:14 +01:00
|
|
|
void CMainSignals::RegisterWithMempoolSignals(CTxMemPool& pool) {
|
|
|
|
pool.NotifyEntryRemoved.connect(boost::bind(&CMainSignals::MempoolEntryRemoved, this, _1, _2));
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMainSignals::UnregisterWithMempoolSignals(CTxMemPool& pool) {
|
|
|
|
pool.NotifyEntryRemoved.disconnect(boost::bind(&CMainSignals::MempoolEntryRemoved, this, _1, _2));
|
|
|
|
}
|
|
|
|
|
2015-02-05 01:11:44 +01:00
|
|
|
CMainSignals& GetMainSignals()
|
|
|
|
{
|
|
|
|
return g_signals;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RegisterValidationInterface(CValidationInterface* pwalletIn) {
|
2017-01-19 22:17:14 +01:00
|
|
|
g_signals.m_internals->UpdatedBlockTip.connect(boost::bind(&CValidationInterface::UpdatedBlockTip, pwalletIn, _1, _2, _3));
|
|
|
|
g_signals.m_internals->TransactionAddedToMempool.connect(boost::bind(&CValidationInterface::TransactionAddedToMempool, pwalletIn, _1));
|
|
|
|
g_signals.m_internals->BlockConnected.connect(boost::bind(&CValidationInterface::BlockConnected, pwalletIn, _1, _2, _3));
|
|
|
|
g_signals.m_internals->BlockDisconnected.connect(boost::bind(&CValidationInterface::BlockDisconnected, pwalletIn, _1));
|
2017-01-20 21:08:14 +01:00
|
|
|
g_signals.m_internals->TransactionRemovedFromMempool.connect(boost::bind(&CValidationInterface::TransactionRemovedFromMempool, pwalletIn, _1));
|
2017-01-19 22:17:14 +01:00
|
|
|
g_signals.m_internals->SetBestChain.connect(boost::bind(&CValidationInterface::SetBestChain, pwalletIn, _1));
|
|
|
|
g_signals.m_internals->Inventory.connect(boost::bind(&CValidationInterface::Inventory, pwalletIn, _1));
|
|
|
|
g_signals.m_internals->Broadcast.connect(boost::bind(&CValidationInterface::ResendWalletTransactions, pwalletIn, _1, _2));
|
|
|
|
g_signals.m_internals->BlockChecked.connect(boost::bind(&CValidationInterface::BlockChecked, pwalletIn, _1, _2));
|
|
|
|
g_signals.m_internals->NewPoWValidBlock.connect(boost::bind(&CValidationInterface::NewPoWValidBlock, pwalletIn, _1, _2));
|
2015-02-05 01:11:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void UnregisterValidationInterface(CValidationInterface* pwalletIn) {
|
2017-01-19 22:17:14 +01:00
|
|
|
g_signals.m_internals->BlockChecked.disconnect(boost::bind(&CValidationInterface::BlockChecked, pwalletIn, _1, _2));
|
|
|
|
g_signals.m_internals->Broadcast.disconnect(boost::bind(&CValidationInterface::ResendWalletTransactions, pwalletIn, _1, _2));
|
|
|
|
g_signals.m_internals->Inventory.disconnect(boost::bind(&CValidationInterface::Inventory, pwalletIn, _1));
|
|
|
|
g_signals.m_internals->SetBestChain.disconnect(boost::bind(&CValidationInterface::SetBestChain, pwalletIn, _1));
|
|
|
|
g_signals.m_internals->TransactionAddedToMempool.disconnect(boost::bind(&CValidationInterface::TransactionAddedToMempool, pwalletIn, _1));
|
|
|
|
g_signals.m_internals->BlockConnected.disconnect(boost::bind(&CValidationInterface::BlockConnected, pwalletIn, _1, _2, _3));
|
|
|
|
g_signals.m_internals->BlockDisconnected.disconnect(boost::bind(&CValidationInterface::BlockDisconnected, pwalletIn, _1));
|
2017-01-20 21:08:14 +01:00
|
|
|
g_signals.m_internals->TransactionRemovedFromMempool.disconnect(boost::bind(&CValidationInterface::TransactionRemovedFromMempool, pwalletIn, _1));
|
2017-01-19 22:17:14 +01:00
|
|
|
g_signals.m_internals->UpdatedBlockTip.disconnect(boost::bind(&CValidationInterface::UpdatedBlockTip, pwalletIn, _1, _2, _3));
|
|
|
|
g_signals.m_internals->NewPoWValidBlock.disconnect(boost::bind(&CValidationInterface::NewPoWValidBlock, pwalletIn, _1, _2));
|
2015-02-05 01:11:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void UnregisterAllValidationInterfaces() {
|
2017-11-28 12:08:09 +01:00
|
|
|
if (!g_signals.m_internals) {
|
|
|
|
return;
|
|
|
|
}
|
2017-01-19 22:17:14 +01:00
|
|
|
g_signals.m_internals->BlockChecked.disconnect_all_slots();
|
|
|
|
g_signals.m_internals->Broadcast.disconnect_all_slots();
|
|
|
|
g_signals.m_internals->Inventory.disconnect_all_slots();
|
|
|
|
g_signals.m_internals->SetBestChain.disconnect_all_slots();
|
|
|
|
g_signals.m_internals->TransactionAddedToMempool.disconnect_all_slots();
|
|
|
|
g_signals.m_internals->BlockConnected.disconnect_all_slots();
|
|
|
|
g_signals.m_internals->BlockDisconnected.disconnect_all_slots();
|
2017-01-20 21:08:14 +01:00
|
|
|
g_signals.m_internals->TransactionRemovedFromMempool.disconnect_all_slots();
|
2017-01-19 22:17:14 +01:00
|
|
|
g_signals.m_internals->UpdatedBlockTip.disconnect_all_slots();
|
|
|
|
g_signals.m_internals->NewPoWValidBlock.disconnect_all_slots();
|
|
|
|
}
|
|
|
|
|
2017-06-08 17:13:46 +02:00
|
|
|
void CallFunctionInValidationInterfaceQueue(std::function<void ()> func) {
|
|
|
|
g_signals.m_internals->m_schedulerClient.AddToProcessQueue(std::move(func));
|
|
|
|
}
|
|
|
|
|
2017-12-24 18:13:13 +01:00
|
|
|
void SyncWithValidationInterfaceQueue() {
|
|
|
|
AssertLockNotHeld(cs_main);
|
|
|
|
// Block until the validation queue drains
|
|
|
|
std::promise<void> promise;
|
|
|
|
CallFunctionInValidationInterfaceQueue([&promise] {
|
|
|
|
promise.set_value();
|
|
|
|
});
|
|
|
|
promise.get_future().wait();
|
|
|
|
}
|
|
|
|
|
2017-01-20 21:08:14 +01:00
|
|
|
void CMainSignals::MempoolEntryRemoved(CTransactionRef ptx, MemPoolRemovalReason reason) {
|
|
|
|
if (reason != MemPoolRemovalReason::BLOCK && reason != MemPoolRemovalReason::CONFLICT) {
|
2017-06-08 17:05:18 +02:00
|
|
|
m_internals->m_schedulerClient.AddToProcessQueue([ptx, this] {
|
|
|
|
m_internals->TransactionRemovedFromMempool(ptx);
|
|
|
|
});
|
2017-01-20 21:08:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-19 22:17:14 +01:00
|
|
|
void CMainSignals::UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload) {
|
2017-10-01 05:49:59 +02:00
|
|
|
m_internals->m_schedulerClient.AddToProcessQueue([pindexNew, pindexFork, fInitialDownload, this] {
|
|
|
|
m_internals->UpdatedBlockTip(pindexNew, pindexFork, fInitialDownload);
|
|
|
|
});
|
2017-01-19 22:17:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CMainSignals::TransactionAddedToMempool(const CTransactionRef &ptx) {
|
Also call other wallet notify callbacks in scheduler thread
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 #9583, re-enabling some of the gains from
#7946. This does not, however, re-enable the gains achieved by
repeatedly releasing cs_main between each transaction processed.
2017-06-08 17:08:53 +02:00
|
|
|
m_internals->m_schedulerClient.AddToProcessQueue([ptx, this] {
|
|
|
|
m_internals->TransactionAddedToMempool(ptx);
|
|
|
|
});
|
2017-01-19 22:17:14 +01:00
|
|
|
}
|
|
|
|
|
Also call other wallet notify callbacks in scheduler thread
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 #9583, re-enabling some of the gains from
#7946. This does not, however, re-enable the gains achieved by
repeatedly releasing cs_main between each transaction processed.
2017-06-08 17:08:53 +02:00
|
|
|
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);
|
|
|
|
});
|
2017-01-19 22:17:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CMainSignals::BlockDisconnected(const std::shared_ptr<const CBlock> &pblock) {
|
Also call other wallet notify callbacks in scheduler thread
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 #9583, re-enabling some of the gains from
#7946. This does not, however, re-enable the gains achieved by
repeatedly releasing cs_main between each transaction processed.
2017-06-08 17:08:53 +02:00
|
|
|
m_internals->m_schedulerClient.AddToProcessQueue([pblock, this] {
|
|
|
|
m_internals->BlockDisconnected(pblock);
|
|
|
|
});
|
2017-01-19 22:17:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CMainSignals::SetBestChain(const CBlockLocator &locator) {
|
Also call other wallet notify callbacks in scheduler thread
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 #9583, re-enabling some of the gains from
#7946. This does not, however, re-enable the gains achieved by
repeatedly releasing cs_main between each transaction processed.
2017-06-08 17:08:53 +02:00
|
|
|
m_internals->m_schedulerClient.AddToProcessQueue([locator, this] {
|
|
|
|
m_internals->SetBestChain(locator);
|
|
|
|
});
|
2017-01-19 22:17:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CMainSignals::Inventory(const uint256 &hash) {
|
Also call other wallet notify callbacks in scheduler thread
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 #9583, re-enabling some of the gains from
#7946. This does not, however, re-enable the gains achieved by
repeatedly releasing cs_main between each transaction processed.
2017-06-08 17:08:53 +02:00
|
|
|
m_internals->m_schedulerClient.AddToProcessQueue([hash, this] {
|
|
|
|
m_internals->Inventory(hash);
|
|
|
|
});
|
2017-01-19 22:17:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CMainSignals::Broadcast(int64_t nBestBlockTime, CConnman* connman) {
|
|
|
|
m_internals->Broadcast(nBestBlockTime, connman);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMainSignals::BlockChecked(const CBlock& block, const CValidationState& state) {
|
|
|
|
m_internals->BlockChecked(block, state);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMainSignals::NewPoWValidBlock(const CBlockIndex *pindex, const std::shared_ptr<const CBlock> &block) {
|
|
|
|
m_internals->NewPoWValidBlock(pindex, block);
|
2015-02-05 01:11:44 +01:00
|
|
|
}
|