Skip to content

Commit 3a19fed

Browse files
committed
Make ValidationInterface signals-type-agnostic
(by hiding boost::signals stuff in the .cpp) This allows us to give it a bit more intelligence as we move forward, including routing some signals through CScheduler. While the introduction of a "internals" pointer in the class is pretty ugly, the fact that we no longer need to include boost/signals directly from validationinterface.h is very much worth the loss.
1 parent ff6a834 commit 3a19fed

File tree

2 files changed

+117
-53
lines changed

2 files changed

+117
-53
lines changed

src/validationinterface.cpp

Lines changed: 81 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,45 +5,99 @@
55

66
#include "validationinterface.h"
77

8+
#include <boost/signals2/signal.hpp>
9+
10+
struct MainSignalsInstance {
11+
boost::signals2::signal<void (const CBlockIndex *, const CBlockIndex *, bool fInitialDownload)> UpdatedBlockTip;
12+
boost::signals2::signal<void (const CTransactionRef &)> TransactionAddedToMempool;
13+
boost::signals2::signal<void (const std::shared_ptr<const CBlock> &, const CBlockIndex *pindex, const std::vector<CTransactionRef>&)> BlockConnected;
14+
boost::signals2::signal<void (const std::shared_ptr<const CBlock> &)> BlockDisconnected;
15+
boost::signals2::signal<void (const CBlockLocator &)> SetBestChain;
16+
boost::signals2::signal<void (const uint256 &)> Inventory;
17+
boost::signals2::signal<void (int64_t nBestBlockTime, CConnman* connman)> Broadcast;
18+
boost::signals2::signal<void (const CBlock&, const CValidationState&)> BlockChecked;
19+
boost::signals2::signal<void (const CBlockIndex *, const std::shared_ptr<const CBlock>&)> NewPoWValidBlock;
20+
};
21+
822
static CMainSignals g_signals;
923

24+
CMainSignals::CMainSignals() {
25+
m_internals.reset(new MainSignalsInstance());
26+
}
27+
1028
CMainSignals& GetMainSignals()
1129
{
1230
return g_signals;
1331
}
1432

1533
void RegisterValidationInterface(CValidationInterface* pwalletIn) {
16-
g_signals.UpdatedBlockTip.connect(boost::bind(&CValidationInterface::UpdatedBlockTip, pwalletIn, _1, _2, _3));
17-
g_signals.TransactionAddedToMempool.connect(boost::bind(&CValidationInterface::TransactionAddedToMempool, pwalletIn, _1));
18-
g_signals.BlockConnected.connect(boost::bind(&CValidationInterface::BlockConnected, pwalletIn, _1, _2, _3));
19-
g_signals.BlockDisconnected.connect(boost::bind(&CValidationInterface::BlockDisconnected, pwalletIn, _1));
20-
g_signals.SetBestChain.connect(boost::bind(&CValidationInterface::SetBestChain, pwalletIn, _1));
21-
g_signals.Inventory.connect(boost::bind(&CValidationInterface::Inventory, pwalletIn, _1));
22-
g_signals.Broadcast.connect(boost::bind(&CValidationInterface::ResendWalletTransactions, pwalletIn, _1, _2));
23-
g_signals.BlockChecked.connect(boost::bind(&CValidationInterface::BlockChecked, pwalletIn, _1, _2));
24-
g_signals.NewPoWValidBlock.connect(boost::bind(&CValidationInterface::NewPoWValidBlock, pwalletIn, _1, _2));
34+
g_signals.m_internals->UpdatedBlockTip.connect(boost::bind(&CValidationInterface::UpdatedBlockTip, pwalletIn, _1, _2, _3));
35+
g_signals.m_internals->TransactionAddedToMempool.connect(boost::bind(&CValidationInterface::TransactionAddedToMempool, pwalletIn, _1));
36+
g_signals.m_internals->BlockConnected.connect(boost::bind(&CValidationInterface::BlockConnected, pwalletIn, _1, _2, _3));
37+
g_signals.m_internals->BlockDisconnected.connect(boost::bind(&CValidationInterface::BlockDisconnected, pwalletIn, _1));
38+
g_signals.m_internals->SetBestChain.connect(boost::bind(&CValidationInterface::SetBestChain, pwalletIn, _1));
39+
g_signals.m_internals->Inventory.connect(boost::bind(&CValidationInterface::Inventory, pwalletIn, _1));
40+
g_signals.m_internals->Broadcast.connect(boost::bind(&CValidationInterface::ResendWalletTransactions, pwalletIn, _1, _2));
41+
g_signals.m_internals->BlockChecked.connect(boost::bind(&CValidationInterface::BlockChecked, pwalletIn, _1, _2));
42+
g_signals.m_internals->NewPoWValidBlock.connect(boost::bind(&CValidationInterface::NewPoWValidBlock, pwalletIn, _1, _2));
2543
}
2644

2745
void UnregisterValidationInterface(CValidationInterface* pwalletIn) {
28-
g_signals.BlockChecked.disconnect(boost::bind(&CValidationInterface::BlockChecked, pwalletIn, _1, _2));
29-
g_signals.Broadcast.disconnect(boost::bind(&CValidationInterface::ResendWalletTransactions, pwalletIn, _1, _2));
30-
g_signals.Inventory.disconnect(boost::bind(&CValidationInterface::Inventory, pwalletIn, _1));
31-
g_signals.SetBestChain.disconnect(boost::bind(&CValidationInterface::SetBestChain, pwalletIn, _1));
32-
g_signals.TransactionAddedToMempool.disconnect(boost::bind(&CValidationInterface::TransactionAddedToMempool, pwalletIn, _1));
33-
g_signals.BlockConnected.disconnect(boost::bind(&CValidationInterface::BlockConnected, pwalletIn, _1, _2, _3));
34-
g_signals.BlockDisconnected.disconnect(boost::bind(&CValidationInterface::BlockDisconnected, pwalletIn, _1));
35-
g_signals.UpdatedBlockTip.disconnect(boost::bind(&CValidationInterface::UpdatedBlockTip, pwalletIn, _1, _2, _3));
36-
g_signals.NewPoWValidBlock.disconnect(boost::bind(&CValidationInterface::NewPoWValidBlock, pwalletIn, _1, _2));
46+
g_signals.m_internals->BlockChecked.disconnect(boost::bind(&CValidationInterface::BlockChecked, pwalletIn, _1, _2));
47+
g_signals.m_internals->Broadcast.disconnect(boost::bind(&CValidationInterface::ResendWalletTransactions, pwalletIn, _1, _2));
48+
g_signals.m_internals->Inventory.disconnect(boost::bind(&CValidationInterface::Inventory, pwalletIn, _1));
49+
g_signals.m_internals->SetBestChain.disconnect(boost::bind(&CValidationInterface::SetBestChain, pwalletIn, _1));
50+
g_signals.m_internals->TransactionAddedToMempool.disconnect(boost::bind(&CValidationInterface::TransactionAddedToMempool, pwalletIn, _1));
51+
g_signals.m_internals->BlockConnected.disconnect(boost::bind(&CValidationInterface::BlockConnected, pwalletIn, _1, _2, _3));
52+
g_signals.m_internals->BlockDisconnected.disconnect(boost::bind(&CValidationInterface::BlockDisconnected, pwalletIn, _1));
53+
g_signals.m_internals->UpdatedBlockTip.disconnect(boost::bind(&CValidationInterface::UpdatedBlockTip, pwalletIn, _1, _2, _3));
54+
g_signals.m_internals->NewPoWValidBlock.disconnect(boost::bind(&CValidationInterface::NewPoWValidBlock, pwalletIn, _1, _2));
3755
}
3856

3957
void UnregisterAllValidationInterfaces() {
40-
g_signals.BlockChecked.disconnect_all_slots();
41-
g_signals.Broadcast.disconnect_all_slots();
42-
g_signals.Inventory.disconnect_all_slots();
43-
g_signals.SetBestChain.disconnect_all_slots();
44-
g_signals.TransactionAddedToMempool.disconnect_all_slots();
45-
g_signals.BlockConnected.disconnect_all_slots();
46-
g_signals.BlockDisconnected.disconnect_all_slots();
47-
g_signals.UpdatedBlockTip.disconnect_all_slots();
48-
g_signals.NewPoWValidBlock.disconnect_all_slots();
58+
g_signals.m_internals->BlockChecked.disconnect_all_slots();
59+
g_signals.m_internals->Broadcast.disconnect_all_slots();
60+
g_signals.m_internals->Inventory.disconnect_all_slots();
61+
g_signals.m_internals->SetBestChain.disconnect_all_slots();
62+
g_signals.m_internals->TransactionAddedToMempool.disconnect_all_slots();
63+
g_signals.m_internals->BlockConnected.disconnect_all_slots();
64+
g_signals.m_internals->BlockDisconnected.disconnect_all_slots();
65+
g_signals.m_internals->UpdatedBlockTip.disconnect_all_slots();
66+
g_signals.m_internals->NewPoWValidBlock.disconnect_all_slots();
67+
}
68+
69+
void CMainSignals::UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload) {
70+
m_internals->UpdatedBlockTip(pindexNew, pindexFork, fInitialDownload);
71+
}
72+
73+
void CMainSignals::TransactionAddedToMempool(const CTransactionRef &ptx) {
74+
m_internals->TransactionAddedToMempool(ptx);
75+
}
76+
77+
void CMainSignals::BlockConnected(const std::shared_ptr<const CBlock> &pblock, const CBlockIndex *pindex, const std::vector<CTransactionRef>& vtxConflicted) {
78+
m_internals->BlockConnected(pblock, pindex, vtxConflicted);
79+
}
80+
81+
void CMainSignals::BlockDisconnected(const std::shared_ptr<const CBlock> &pblock) {
82+
m_internals->BlockDisconnected(pblock);
83+
}
84+
85+
void CMainSignals::SetBestChain(const CBlockLocator &locator) {
86+
m_internals->SetBestChain(locator);
87+
}
88+
89+
void CMainSignals::Inventory(const uint256 &hash) {
90+
m_internals->Inventory(hash);
91+
}
92+
93+
void CMainSignals::Broadcast(int64_t nBestBlockTime, CConnman* connman) {
94+
m_internals->Broadcast(nBestBlockTime, connman);
95+
}
96+
97+
void CMainSignals::BlockChecked(const CBlock& block, const CValidationState& state) {
98+
m_internals->BlockChecked(block, state);
99+
}
100+
101+
void CMainSignals::NewPoWValidBlock(const CBlockIndex *pindex, const std::shared_ptr<const CBlock> &block) {
102+
m_internals->NewPoWValidBlock(pindex, block);
49103
}

src/validationinterface.h

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#ifndef BITCOIN_VALIDATIONINTERFACE_H
77
#define BITCOIN_VALIDATIONINTERFACE_H
88

9-
#include <boost/signals2/signal.hpp>
109
#include <memory>
1110

1211
#include "primitives/transaction.h" // CTransaction(Ref)
@@ -32,49 +31,60 @@ void UnregisterAllValidationInterfaces();
3231

3332
class CValidationInterface {
3433
protected:
35-
virtual void UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload) {}
36-
virtual void TransactionAddedToMempool(const CTransactionRef &ptxn) {}
37-
virtual void BlockConnected(const std::shared_ptr<const CBlock> &block, const CBlockIndex *pindex, const std::vector<CTransactionRef> &txnConflicted) {}
38-
virtual void BlockDisconnected(const std::shared_ptr<const CBlock> &block) {}
39-
virtual void SetBestChain(const CBlockLocator &locator) {}
40-
virtual void Inventory(const uint256 &hash) {}
41-
virtual void ResendWalletTransactions(int64_t nBestBlockTime, CConnman* connman) {}
42-
virtual void BlockChecked(const CBlock&, const CValidationState&) {}
43-
virtual void NewPoWValidBlock(const CBlockIndex *pindex, const std::shared_ptr<const CBlock>& block) {};
44-
friend void ::RegisterValidationInterface(CValidationInterface*);
45-
friend void ::UnregisterValidationInterface(CValidationInterface*);
46-
friend void ::UnregisterAllValidationInterfaces();
47-
};
48-
49-
struct CMainSignals {
5034
/** Notifies listeners of updated block chain tip */
51-
boost::signals2::signal<void (const CBlockIndex *, const CBlockIndex *, bool fInitialDownload)> UpdatedBlockTip;
35+
virtual void UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload) {}
5236
/** Notifies listeners of a transaction having been added to mempool. */
53-
boost::signals2::signal<void (const CTransactionRef &)> TransactionAddedToMempool;
37+
virtual void TransactionAddedToMempool(const CTransactionRef &ptxn) {}
5438
/**
5539
* Notifies listeners of a block being connected.
5640
* Provides a vector of transactions evicted from the mempool as a result.
5741
*/
58-
boost::signals2::signal<void (const std::shared_ptr<const CBlock> &, const CBlockIndex *pindex, const std::vector<CTransactionRef> &)> BlockConnected;
42+
virtual void BlockConnected(const std::shared_ptr<const CBlock> &block, const CBlockIndex *pindex, const std::vector<CTransactionRef> &txnConflicted) {}
5943
/** Notifies listeners of a block being disconnected */
60-
boost::signals2::signal<void (const std::shared_ptr<const CBlock> &)> BlockDisconnected;
61-
/** Notifies listeners of a new active block chain. */
62-
boost::signals2::signal<void (const CBlockLocator &)> SetBestChain;
44+
virtual void BlockDisconnected(const std::shared_ptr<const CBlock> &block) {}
45+
/** Notifies listeners of the new active block chain on-disk. */
46+
virtual void SetBestChain(const CBlockLocator &locator) {}
6347
/** Notifies listeners about an inventory item being seen on the network. */
64-
boost::signals2::signal<void (const uint256 &)> Inventory;
48+
virtual void Inventory(const uint256 &hash) {}
6549
/** Tells listeners to broadcast their data. */
66-
boost::signals2::signal<void (int64_t nBestBlockTime, CConnman* connman)> Broadcast;
50+
virtual void ResendWalletTransactions(int64_t nBestBlockTime, CConnman* connman) {}
6751
/**
6852
* Notifies listeners of a block validation result.
6953
* If the provided CValidationState IsValid, the provided block
7054
* is guaranteed to be the current best block at the time the
7155
* callback was generated (not necessarily now)
7256
*/
73-
boost::signals2::signal<void (const CBlock&, const CValidationState&)> BlockChecked;
57+
virtual void BlockChecked(const CBlock&, const CValidationState&) {}
7458
/**
7559
* Notifies listeners that a block which builds directly on our current tip
7660
* has been received and connected to the headers tree, though not validated yet */
77-
boost::signals2::signal<void (const CBlockIndex *, const std::shared_ptr<const CBlock>&)> NewPoWValidBlock;
61+
virtual void NewPoWValidBlock(const CBlockIndex *pindex, const std::shared_ptr<const CBlock>& block) {};
62+
friend void ::RegisterValidationInterface(CValidationInterface*);
63+
friend void ::UnregisterValidationInterface(CValidationInterface*);
64+
friend void ::UnregisterAllValidationInterfaces();
65+
};
66+
67+
struct MainSignalsInstance;
68+
class CMainSignals {
69+
private:
70+
std::unique_ptr<MainSignalsInstance> m_internals;
71+
72+
friend void ::RegisterValidationInterface(CValidationInterface*);
73+
friend void ::UnregisterValidationInterface(CValidationInterface*);
74+
friend void ::UnregisterAllValidationInterfaces();
75+
public:
76+
CMainSignals();
77+
78+
void UpdatedBlockTip(const CBlockIndex *, const CBlockIndex *, bool fInitialDownload);
79+
void TransactionAddedToMempool(const CTransactionRef &);
80+
void BlockConnected(const std::shared_ptr<const CBlock> &, const CBlockIndex *pindex, const std::vector<CTransactionRef> &);
81+
void BlockDisconnected(const std::shared_ptr<const CBlock> &);
82+
void UpdatedTransaction(const uint256 &);
83+
void SetBestChain(const CBlockLocator &);
84+
void Inventory(const uint256 &);
85+
void Broadcast(int64_t nBestBlockTime, CConnman* connman);
86+
void BlockChecked(const CBlock&, const CValidationState&);
87+
void NewPoWValidBlock(const CBlockIndex *, const std::shared_ptr<const CBlock>&);
7888
};
7989

8090
CMainSignals& GetMainSignals();

0 commit comments

Comments
 (0)