2015-02-04 21:11:44 -03:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2020-01-14 16:17:38 -03:00
|
|
|
// Copyright (c) 2009-2020 The Bitcoin Core developers
|
2015-02-04 21:11:44 -03:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2017-11-09 21:57:53 -03:00
|
|
|
#include <validationinterface.h>
|
2017-10-02 19:24:59 -03:00
|
|
|
|
2019-08-21 20:48:52 -04:00
|
|
|
#include <chain.h>
|
|
|
|
#include <consensus/validation.h>
|
|
|
|
#include <logging.h>
|
2017-11-09 21:57:53 -03:00
|
|
|
#include <primitives/block.h>
|
2019-08-21 20:48:52 -04:00
|
|
|
#include <primitives/transaction.h>
|
2017-11-09 21:57:53 -03:00
|
|
|
#include <scheduler.h>
|
2017-04-10 15:55:49 -03:00
|
|
|
|
2017-12-24 14:13:13 -03:00
|
|
|
#include <future>
|
2019-07-23 17:47:17 -04:00
|
|
|
#include <unordered_map>
|
2018-12-30 19:19:14 -03:00
|
|
|
#include <utility>
|
2015-02-04 21:11:44 -03:00
|
|
|
|
2017-01-19 18:17:14 -03:00
|
|
|
#include <boost/signals2/signal.hpp>
|
|
|
|
|
2018-10-17 12:12:28 -03:00
|
|
|
struct ValidationInterfaceConnections {
|
|
|
|
boost::signals2::scoped_connection UpdatedBlockTip;
|
|
|
|
boost::signals2::scoped_connection TransactionAddedToMempool;
|
|
|
|
boost::signals2::scoped_connection BlockConnected;
|
|
|
|
boost::signals2::scoped_connection BlockDisconnected;
|
|
|
|
boost::signals2::scoped_connection TransactionRemovedFromMempool;
|
|
|
|
boost::signals2::scoped_connection ChainStateFlushed;
|
|
|
|
boost::signals2::scoped_connection BlockChecked;
|
|
|
|
boost::signals2::scoped_connection NewPoWValidBlock;
|
|
|
|
};
|
|
|
|
|
2017-01-19 18:17:14 -03:00
|
|
|
struct MainSignalsInstance {
|
|
|
|
boost::signals2::signal<void (const CBlockIndex *, const CBlockIndex *, bool fInitialDownload)> UpdatedBlockTip;
|
|
|
|
boost::signals2::signal<void (const CTransactionRef &)> TransactionAddedToMempool;
|
2019-11-11 12:43:34 -03:00
|
|
|
boost::signals2::signal<void (const std::shared_ptr<const CBlock> &, const CBlockIndex *pindex)> BlockConnected;
|
2019-07-24 15:41:41 -04:00
|
|
|
boost::signals2::signal<void (const std::shared_ptr<const CBlock>&, const CBlockIndex* pindex)> BlockDisconnected;
|
2017-01-20 17:08:14 -03:00
|
|
|
boost::signals2::signal<void (const CTransactionRef &)> TransactionRemovedFromMempool;
|
2018-04-27 15:01:02 -03:00
|
|
|
boost::signals2::signal<void (const CBlockLocator &)> ChainStateFlushed;
|
2019-10-24 12:35:42 -03:00
|
|
|
boost::signals2::signal<void (const CBlock&, const BlockValidationState&)> BlockChecked;
|
2017-01-19 18:17:14 -03:00
|
|
|
boost::signals2::signal<void (const CBlockIndex *, const std::shared_ptr<const CBlock>&)> NewPoWValidBlock;
|
2017-01-19 18:49:22 -03:00
|
|
|
|
2017-04-10 15:55:49 -03: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;
|
2018-10-17 12:12:28 -03:00
|
|
|
std::unordered_map<CValidationInterface*, ValidationInterfaceConnections> m_connMainSignals;
|
2017-04-10 15:55:49 -03:00
|
|
|
|
2017-08-01 06:22:41 -04:00
|
|
|
explicit MainSignalsInstance(CScheduler *pscheduler) : m_schedulerClient(pscheduler) {}
|
2017-01-19 18:17:14 -03:00
|
|
|
};
|
|
|
|
|
2015-02-04 21:11:44 -03:00
|
|
|
static CMainSignals g_signals;
|
|
|
|
|
2017-01-19 18:49:22 -03:00
|
|
|
void CMainSignals::RegisterBackgroundSignalScheduler(CScheduler& scheduler) {
|
2017-04-10 15:55:49 -03:00
|
|
|
assert(!m_internals);
|
|
|
|
m_internals.reset(new MainSignalsInstance(&scheduler));
|
2017-01-19 18:49:22 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CMainSignals::UnregisterBackgroundSignalScheduler() {
|
2017-04-10 15:55:49 -03:00
|
|
|
m_internals.reset(nullptr);
|
2017-01-19 18:49:22 -03:00
|
|
|
}
|
|
|
|
|
2017-06-27 19:07:52 -04:00
|
|
|
void CMainSignals::FlushBackgroundCallbacks() {
|
2017-11-28 08:08:09 -03:00
|
|
|
if (m_internals) {
|
|
|
|
m_internals->m_schedulerClient.EmptyQueue();
|
|
|
|
}
|
2017-06-27 19:07:52 -04:00
|
|
|
}
|
|
|
|
|
2017-12-04 20:31:36 -03:00
|
|
|
size_t CMainSignals::CallbacksPending() {
|
|
|
|
if (!m_internals) return 0;
|
|
|
|
return m_internals->m_schedulerClient.CallbacksPending();
|
|
|
|
}
|
|
|
|
|
2015-02-04 21:11:44 -03:00
|
|
|
CMainSignals& GetMainSignals()
|
|
|
|
{
|
|
|
|
return g_signals;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RegisterValidationInterface(CValidationInterface* pwalletIn) {
|
2018-10-17 12:12:28 -03:00
|
|
|
ValidationInterfaceConnections& conns = g_signals.m_internals->m_connMainSignals[pwalletIn];
|
2018-10-17 12:51:17 -03:00
|
|
|
conns.UpdatedBlockTip = g_signals.m_internals->UpdatedBlockTip.connect(std::bind(&CValidationInterface::UpdatedBlockTip, pwalletIn, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
|
|
|
|
conns.TransactionAddedToMempool = g_signals.m_internals->TransactionAddedToMempool.connect(std::bind(&CValidationInterface::TransactionAddedToMempool, pwalletIn, std::placeholders::_1));
|
2019-11-11 12:43:34 -03:00
|
|
|
conns.BlockConnected = g_signals.m_internals->BlockConnected.connect(std::bind(&CValidationInterface::BlockConnected, pwalletIn, std::placeholders::_1, std::placeholders::_2));
|
2019-07-24 15:41:41 -04:00
|
|
|
conns.BlockDisconnected = g_signals.m_internals->BlockDisconnected.connect(std::bind(&CValidationInterface::BlockDisconnected, pwalletIn, std::placeholders::_1, std::placeholders::_2));
|
2018-10-17 12:51:17 -03:00
|
|
|
conns.TransactionRemovedFromMempool = g_signals.m_internals->TransactionRemovedFromMempool.connect(std::bind(&CValidationInterface::TransactionRemovedFromMempool, pwalletIn, std::placeholders::_1));
|
|
|
|
conns.ChainStateFlushed = g_signals.m_internals->ChainStateFlushed.connect(std::bind(&CValidationInterface::ChainStateFlushed, pwalletIn, std::placeholders::_1));
|
|
|
|
conns.BlockChecked = g_signals.m_internals->BlockChecked.connect(std::bind(&CValidationInterface::BlockChecked, pwalletIn, std::placeholders::_1, std::placeholders::_2));
|
|
|
|
conns.NewPoWValidBlock = g_signals.m_internals->NewPoWValidBlock.connect(std::bind(&CValidationInterface::NewPoWValidBlock, pwalletIn, std::placeholders::_1, std::placeholders::_2));
|
2015-02-04 21:11:44 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void UnregisterValidationInterface(CValidationInterface* pwalletIn) {
|
2019-01-28 22:08:30 -03:00
|
|
|
if (g_signals.m_internals) {
|
|
|
|
g_signals.m_internals->m_connMainSignals.erase(pwalletIn);
|
|
|
|
}
|
2015-02-04 21:11:44 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void UnregisterAllValidationInterfaces() {
|
2017-11-28 08:08:09 -03:00
|
|
|
if (!g_signals.m_internals) {
|
|
|
|
return;
|
|
|
|
}
|
2018-10-17 12:12:28 -03:00
|
|
|
g_signals.m_internals->m_connMainSignals.clear();
|
2017-01-19 18:17:14 -03:00
|
|
|
}
|
|
|
|
|
2017-06-08 11:13:46 -04:00
|
|
|
void CallFunctionInValidationInterfaceQueue(std::function<void ()> func) {
|
|
|
|
g_signals.m_internals->m_schedulerClient.AddToProcessQueue(std::move(func));
|
|
|
|
}
|
|
|
|
|
2017-12-24 14:13:13 -03: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();
|
|
|
|
}
|
|
|
|
|
2019-08-21 20:48:52 -04:00
|
|
|
// Use a macro instead of a function for conditional logging to prevent
|
|
|
|
// evaluating arguments when logging is not enabled.
|
|
|
|
//
|
|
|
|
// NOTE: The lambda captures all local variables by value.
|
|
|
|
#define ENQUEUE_AND_LOG_EVENT(event, fmt, name, ...) \
|
|
|
|
do { \
|
|
|
|
auto local_name = (name); \
|
|
|
|
LOG_EVENT("Enqueuing " fmt, local_name, __VA_ARGS__); \
|
|
|
|
m_internals->m_schedulerClient.AddToProcessQueue([=] { \
|
|
|
|
LOG_EVENT(fmt, local_name, __VA_ARGS__); \
|
|
|
|
event(); \
|
|
|
|
}); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define LOG_EVENT(fmt, ...) \
|
|
|
|
LogPrint(BCLog::VALIDATION, fmt "\n", __VA_ARGS__)
|
2017-01-20 17:08:14 -03:00
|
|
|
|
2017-01-19 18:17:14 -03:00
|
|
|
void CMainSignals::UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload) {
|
2018-04-15 12:22:28 -03:00
|
|
|
// Dependencies exist that require UpdatedBlockTip events to be delivered in the order in which
|
|
|
|
// the chain actually updates. One way to ensure this is for the caller to invoke this signal
|
|
|
|
// in the same critical section where the chain is updated
|
|
|
|
|
2019-08-21 20:48:52 -04:00
|
|
|
auto event = [pindexNew, pindexFork, fInitialDownload, this] {
|
2017-10-01 00:49:59 -03:00
|
|
|
m_internals->UpdatedBlockTip(pindexNew, pindexFork, fInitialDownload);
|
2019-08-21 20:48:52 -04:00
|
|
|
};
|
|
|
|
ENQUEUE_AND_LOG_EVENT(event, "%s: new block hash=%s fork block hash=%s (in IBD=%s)", __func__,
|
|
|
|
pindexNew->GetBlockHash().ToString(),
|
|
|
|
pindexFork ? pindexFork->GetBlockHash().ToString() : "null",
|
|
|
|
fInitialDownload);
|
2017-01-19 18:17:14 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CMainSignals::TransactionAddedToMempool(const CTransactionRef &ptx) {
|
2019-08-21 20:48:52 -04:00
|
|
|
auto event = [ptx, this] {
|
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 11:08:53 -04:00
|
|
|
m_internals->TransactionAddedToMempool(ptx);
|
2019-08-21 20:48:52 -04:00
|
|
|
};
|
|
|
|
ENQUEUE_AND_LOG_EVENT(event, "%s: txid=%s wtxid=%s", __func__,
|
|
|
|
ptx->GetHash().ToString(),
|
|
|
|
ptx->GetWitnessHash().ToString());
|
2017-01-19 18:17:14 -03:00
|
|
|
}
|
|
|
|
|
2019-07-23 17:47:17 -04:00
|
|
|
void CMainSignals::TransactionRemovedFromMempool(const CTransactionRef &ptx) {
|
2019-08-21 20:48:52 -04:00
|
|
|
auto event = [ptx, this] {
|
2019-07-23 17:47:17 -04:00
|
|
|
m_internals->TransactionRemovedFromMempool(ptx);
|
2019-08-21 20:48:52 -04:00
|
|
|
};
|
|
|
|
ENQUEUE_AND_LOG_EVENT(event, "%s: txid=%s wtxid=%s", __func__,
|
|
|
|
ptx->GetHash().ToString(),
|
|
|
|
ptx->GetWitnessHash().ToString());
|
2019-07-23 17:47:17 -04:00
|
|
|
}
|
|
|
|
|
2019-11-11 12:43:34 -03:00
|
|
|
void CMainSignals::BlockConnected(const std::shared_ptr<const CBlock> &pblock, const CBlockIndex *pindex) {
|
|
|
|
auto event = [pblock, pindex, this] {
|
|
|
|
m_internals->BlockConnected(pblock, pindex);
|
2019-08-21 20:48:52 -04:00
|
|
|
};
|
|
|
|
ENQUEUE_AND_LOG_EVENT(event, "%s: block hash=%s block height=%d", __func__,
|
|
|
|
pblock->GetHash().ToString(),
|
|
|
|
pindex->nHeight);
|
2017-01-19 18:17:14 -03:00
|
|
|
}
|
|
|
|
|
2019-07-24 15:41:41 -04:00
|
|
|
void CMainSignals::BlockDisconnected(const std::shared_ptr<const CBlock>& pblock, const CBlockIndex* pindex)
|
|
|
|
{
|
2019-08-21 20:48:52 -04:00
|
|
|
auto event = [pblock, pindex, this] {
|
2019-07-24 15:41:41 -04:00
|
|
|
m_internals->BlockDisconnected(pblock, pindex);
|
2019-08-21 20:48:52 -04:00
|
|
|
};
|
|
|
|
ENQUEUE_AND_LOG_EVENT(event, "%s: block hash=%s block height=%d", __func__,
|
|
|
|
pblock->GetHash().ToString(),
|
|
|
|
pindex->nHeight);
|
2017-01-19 18:17:14 -03:00
|
|
|
}
|
|
|
|
|
2018-04-27 15:01:02 -03:00
|
|
|
void CMainSignals::ChainStateFlushed(const CBlockLocator &locator) {
|
2019-08-21 20:48:52 -04:00
|
|
|
auto event = [locator, this] {
|
2018-04-27 15:01:02 -03:00
|
|
|
m_internals->ChainStateFlushed(locator);
|
2019-08-21 20:48:52 -04:00
|
|
|
};
|
|
|
|
ENQUEUE_AND_LOG_EVENT(event, "%s: block hash=%s", __func__,
|
|
|
|
locator.IsNull() ? "null" : locator.vHave.front().ToString());
|
2017-01-19 18:17:14 -03:00
|
|
|
}
|
|
|
|
|
2019-10-24 12:35:42 -03:00
|
|
|
void CMainSignals::BlockChecked(const CBlock& block, const BlockValidationState& state) {
|
2019-08-21 20:48:52 -04:00
|
|
|
LOG_EVENT("%s: block hash=%s state=%s", __func__,
|
2019-11-08 18:22:36 -03:00
|
|
|
block.GetHash().ToString(), state.ToString());
|
2017-01-19 18:17:14 -03:00
|
|
|
m_internals->BlockChecked(block, state);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMainSignals::NewPoWValidBlock(const CBlockIndex *pindex, const std::shared_ptr<const CBlock> &block) {
|
2019-08-21 20:48:52 -04:00
|
|
|
LOG_EVENT("%s: block hash=%s", __func__, block->GetHash().ToString());
|
2017-01-19 18:17:14 -03:00
|
|
|
m_internals->NewPoWValidBlock(pindex, block);
|
2015-02-04 21:11:44 -03:00
|
|
|
}
|