Skip to content

Commit 00588c3

Browse files
committed
Use boost signals for callbacks from main to wallet
1 parent e010af7 commit 00588c3

File tree

5 files changed

+77
-96
lines changed

5 files changed

+77
-96
lines changed

src/main.cpp

Lines changed: 46 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -74,75 +74,52 @@ int64 nTransactionFee = 0;
7474

7575
// These functions dispatch to one or all registered wallets
7676

77-
78-
void RegisterWallet(CWallet* pwalletIn)
79-
{
80-
{
81-
LOCK(cs_setpwalletRegistered);
82-
setpwalletRegistered.insert(pwalletIn);
83-
}
84-
}
85-
86-
void UnregisterWallet(CWallet* pwalletIn)
87-
{
88-
{
89-
LOCK(cs_setpwalletRegistered);
90-
setpwalletRegistered.erase(pwalletIn);
91-
}
92-
}
93-
94-
void UnregisterAllWallets()
95-
{
96-
LOCK(cs_setpwalletRegistered);
97-
setpwalletRegistered.clear();
77+
namespace {
78+
struct CMainSignals {
79+
// Notifies listeners of updated transaction data (passing hash, transaction, and optionally the block it is found in.
80+
boost::signals2::signal<void (const uint256 &, const CTransaction &, const CBlock *)> SyncTransaction;
81+
// Notifies listeners of an erased transaction (currently disabled, requires transaction replacement).
82+
boost::signals2::signal<void (const uint256 &)> EraseTransaction;
83+
// Notifies listeners of an updated transaction without new data (for now: a coinbase potentially becoming visible).
84+
boost::signals2::signal<void (const uint256 &)> UpdatedTransaction;
85+
// Notifies listeners of a new active block chain.
86+
boost::signals2::signal<void (const CBlockLocator &)> SetBestChain;
87+
// Notifies listeners about an inventory item being seen on the network.
88+
boost::signals2::signal<void (const uint256 &)> Inventory;
89+
// Tells listeners to broadcast their data.
90+
boost::signals2::signal<void ()> Broadcast;
91+
} g_signals;
9892
}
9993

100-
// erases transaction with the given hash from all wallets
101-
void static EraseFromWallets(uint256 hash)
102-
{
103-
LOCK(cs_setpwalletRegistered);
104-
BOOST_FOREACH(CWallet* pwallet, setpwalletRegistered)
105-
pwallet->EraseFromWallet(hash);
94+
void RegisterWallet(CWalletInterface* pwalletIn) {
95+
g_signals.SyncTransaction.connect(boost::bind(&CWalletInterface::SyncTransaction, pwalletIn, _1, _2, _3));
96+
g_signals.EraseTransaction.connect(boost::bind(&CWalletInterface::EraseFromWallet, pwalletIn, _1));
97+
g_signals.UpdatedTransaction.connect(boost::bind(&CWalletInterface::UpdatedTransaction, pwalletIn, _1));
98+
g_signals.SetBestChain.connect(boost::bind(&CWalletInterface::SetBestChain, pwalletIn, _1));
99+
g_signals.Inventory.connect(boost::bind(&CWalletInterface::Inventory, pwalletIn, _1));
100+
g_signals.Broadcast.connect(boost::bind(&CWalletInterface::ResendWalletTransactions, pwalletIn));
106101
}
107102

108-
// make sure all wallets know about the given transaction, in the given block
109-
void SyncWithWallets(const uint256 &hash, const CTransaction& tx, const CBlock* pblock, bool fUpdate)
110-
{
111-
LOCK(cs_setpwalletRegistered);
112-
BOOST_FOREACH(CWallet* pwallet, setpwalletRegistered)
113-
pwallet->AddToWalletIfInvolvingMe(hash, tx, pblock, fUpdate);
103+
void UnregisterWallet(CWalletInterface* pwalletIn) {
104+
g_signals.Broadcast.disconnect(boost::bind(&CWalletInterface::ResendWalletTransactions, pwalletIn));
105+
g_signals.Inventory.disconnect(boost::bind(&CWalletInterface::Inventory, pwalletIn, _1));
106+
g_signals.SetBestChain.disconnect(boost::bind(&CWalletInterface::SetBestChain, pwalletIn, _1));
107+
g_signals.UpdatedTransaction.disconnect(boost::bind(&CWalletInterface::UpdatedTransaction, pwalletIn, _1));
108+
g_signals.EraseTransaction.disconnect(boost::bind(&CWalletInterface::EraseFromWallet, pwalletIn, _1));
109+
g_signals.SyncTransaction.disconnect(boost::bind(&CWalletInterface::SyncTransaction, pwalletIn, _1, _2, _3));
114110
}
115111

116-
// notify wallets about a new best chain
117-
void static SetBestChain(const CBlockLocator& loc)
118-
{
119-
LOCK(cs_setpwalletRegistered);
120-
BOOST_FOREACH(CWallet* pwallet, setpwalletRegistered)
121-
pwallet->SetBestChain(loc);
112+
void UnregisterAllWallets() {
113+
g_signals.Broadcast.disconnect_all_slots();
114+
g_signals.Inventory.disconnect_all_slots();
115+
g_signals.SetBestChain.disconnect_all_slots();
116+
g_signals.UpdatedTransaction.disconnect_all_slots();
117+
g_signals.EraseTransaction.disconnect_all_slots();
118+
g_signals.SyncTransaction.disconnect_all_slots();
122119
}
123120

124-
// notify wallets about an updated transaction
125-
void static UpdatedTransaction(const uint256& hashTx)
126-
{
127-
LOCK(cs_setpwalletRegistered);
128-
BOOST_FOREACH(CWallet* pwallet, setpwalletRegistered)
129-
pwallet->UpdatedTransaction(hashTx);
130-
}
131-
132-
// notify wallets about an incoming inventory (for request counts)
133-
void static Inventory(const uint256& hash)
134-
{
135-
LOCK(cs_setpwalletRegistered);
136-
BOOST_FOREACH(CWallet* pwallet, setpwalletRegistered)
137-
pwallet->Inventory(hash);
138-
}
139-
140-
// ask wallets to resend their transactions
141-
void static ResendWalletTransactions()
142-
{
143-
LOCK(cs_setpwalletRegistered);
144-
BOOST_FOREACH(CWallet* pwallet, setpwalletRegistered)
145-
pwallet->ResendWalletTransactions();
121+
void SyncWithWallets(const uint256 &hash, const CTransaction &tx, const CBlock *pblock) {
122+
g_signals.SyncTransaction(hash, tx, pblock);
146123
}
147124

148125
//////////////////////////////////////////////////////////////////////////////
@@ -913,8 +890,8 @@ bool CTxMemPool::accept(CValidationState &state, const CTransaction &tx, bool fL
913890
///// are we sure this is ok when loading transactions or restoring block txes
914891
// If updated, erase old tx from wallet
915892
if (ptxOld)
916-
EraseFromWallets(ptxOld->GetHash());
917-
SyncWithWallets(hash, tx, NULL, true);
893+
g_signals.EraseTransaction(ptxOld->GetHash());
894+
g_signals.SyncTransaction(hash, tx, NULL);
918895

919896
LogPrint("mempool", "CTxMemPool::accept() : accepted %s (poolsz %"PRIszu")\n",
920897
hash.ToString().c_str(),
@@ -1974,7 +1951,7 @@ bool ConnectBlock(CBlock& block, CValidationState& state, CBlockIndex* pindex, C
19741951

19751952
// Watch for transactions paying to me
19761953
for (unsigned int i = 0; i < block.vtx.size(); i++)
1977-
SyncWithWallets(block.GetTxHash(i), block.vtx[i], &block, true);
1954+
g_signals.SyncTransaction(block.GetTxHash(i), block.vtx[i], &block);
19781955

19791956
return true;
19801957
}
@@ -2108,7 +2085,7 @@ bool SetBestChain(CValidationState &state, CBlockIndex* pindexNew)
21082085

21092086
// Update best block in wallet (so we can detect restored wallets)
21102087
if ((pindexNew->nHeight % 20160) == 0 || (!fIsInitialDownload && (pindexNew->nHeight % 144) == 0))
2111-
::SetBestChain(chainActive.GetLocator(pindexNew));
2088+
g_signals.SetBestChain(chainActive.GetLocator(pindexNew));
21122089

21132090
// New best block
21142091
nTimeBestReceived = GetTime();
@@ -2188,7 +2165,7 @@ bool AddToBlockIndex(CBlock& block, CValidationState& state, const CDiskBlockPos
21882165
CheckForkWarningConditions();
21892166
// Notify UI to display prev block's coinbase if it was ours
21902167
static uint256 hashPrevBestCoinBase;
2191-
UpdatedTransaction(hashPrevBestCoinBase);
2168+
g_signals.UpdatedTransaction(hashPrevBestCoinBase);
21922169
hashPrevBestCoinBase = block.GetTxHash(0);
21932170
} else
21942171
CheckForkWarningConditionsOnNewFork(pindexNew);
@@ -3311,7 +3288,7 @@ void static ProcessGetData(CNode* pfrom)
33113288
}
33123289

33133290
// Track requests for our stuff.
3314-
Inventory(inv.hash);
3291+
g_signals.Inventory(inv.hash);
33153292
}
33163293
}
33173294

@@ -3573,7 +3550,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)
35733550
}
35743551

35753552
// Track requests for our stuff
3576-
Inventory(inv.hash);
3553+
g_signals.Inventory(inv.hash);
35773554
}
35783555
}
35793556

@@ -4193,7 +4170,7 @@ bool SendMessages(CNode* pto, bool fSendTrickle)
41934170
// transactions become unconfirmed and spams other nodes.
41944171
if (!fReindex && !fImporting && !IsInitialBlockDownload())
41954172
{
4196-
ResendWalletTransactions();
4173+
g_signals.Broadcast();
41974174
}
41984175

41994176
//

src/main.h

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
#include <list>
1919

20-
class CWallet;
2120
class CBlock;
2221
class CBlockIndex;
2322
class CKeyItem;
@@ -81,8 +80,6 @@ extern uint64 nLastBlockTx;
8180
extern uint64 nLastBlockSize;
8281
extern const std::string strMessageMagic;
8382
extern int64 nTimeBestReceived;
84-
extern CCriticalSection cs_setpwalletRegistered;
85-
extern std::set<CWallet*> setpwalletRegistered;
8683
extern bool fImporting;
8784
extern bool fReindex;
8885
extern bool fBenchmark;
@@ -108,17 +105,18 @@ class CCoinsView;
108105
class CCoinsViewCache;
109106
class CScriptCheck;
110107
class CValidationState;
108+
class CWalletInterface;
111109

112110
struct CBlockTemplate;
113111

114112
/** Register a wallet to receive updates from core */
115-
void RegisterWallet(CWallet* pwalletIn);
113+
void RegisterWallet(CWalletInterface* pwalletIn);
116114
/** Unregister a wallet from core */
117-
void UnregisterWallet(CWallet* pwalletIn);
115+
void UnregisterWallet(CWalletInterface* pwalletIn);
118116
/** Unregister all wallets from core */
119117
void UnregisterAllWallets();
120118
/** Push an updated transaction to all registered wallets */
121-
void SyncWithWallets(const uint256 &hash, const CTransaction& tx, const CBlock* pblock = NULL, bool fUpdate = false);
119+
void SyncWithWallets(const uint256 &hash, const CTransaction& tx, const CBlock* pblock = NULL);
122120

123121
/** Register with a network node to receive its signals */
124122
void RegisterNodeSignals(CNodeSignals& nodeSignals);
@@ -190,9 +188,6 @@ bool AbortNode(const std::string &msg);
190188

191189

192190

193-
194-
bool GetWalletFile(CWallet* pwallet, std::string &strWalletFileOut);
195-
196191
struct CDiskBlockPos
197192
{
198193
int nFile;
@@ -1256,4 +1251,18 @@ class CMerkleBlock
12561251
)
12571252
};
12581253

1254+
1255+
class CWalletInterface {
1256+
protected:
1257+
virtual void SyncTransaction(const uint256 &hash, const CTransaction &tx, const CBlock *pblock) =0;
1258+
virtual void EraseFromWallet(const uint256 &hash) =0;
1259+
virtual void SetBestChain(const CBlockLocator &locator) =0;
1260+
virtual void UpdatedTransaction(const uint256 &hash) =0;
1261+
virtual void Inventory(const uint256 &hash) =0;
1262+
virtual void ResendWalletTransactions() =0;
1263+
friend void ::RegisterWallet(CWalletInterface*);
1264+
friend void ::UnregisterWallet(CWalletInterface*);
1265+
friend void ::UnregisterAllWallets();
1266+
};
1267+
12591268
#endif

src/rpcrawtransaction.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ Value sendrawtransaction(const Array& params, bool fHelp)
560560
// Not in block, but already in the memory pool; will drop
561561
// through to re-relay it.
562562
} else {
563-
SyncWithWallets(hashTx, tx, NULL, true);
563+
SyncWithWallets(hashTx, tx, NULL);
564564
}
565565
RelayTransaction(tx, hashTx);
566566

src/wallet.cpp

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ bool CWallet::AddToWallet(const CWalletTx& wtxIn)
505505
// Add a transaction to the wallet, or update it.
506506
// pblock is optional, but should be provided if the transaction is known to be in a block.
507507
// If fUpdate is true, existing transactions will be updated.
508-
bool CWallet::AddToWalletIfInvolvingMe(const uint256 &hash, const CTransaction& tx, const CBlock* pblock, bool fUpdate, bool fFindBlock)
508+
bool CWallet::AddToWalletIfInvolvingMe(const uint256 &hash, const CTransaction& tx, const CBlock* pblock, bool fUpdate)
509509
{
510510
{
511511
LOCK(cs_wallet);
@@ -525,16 +525,20 @@ bool CWallet::AddToWalletIfInvolvingMe(const uint256 &hash, const CTransaction&
525525
return false;
526526
}
527527

528-
bool CWallet::EraseFromWallet(uint256 hash)
528+
void CWallet::SyncTransaction(const uint256 &hash, const CTransaction& tx, const CBlock* pblock) {
529+
AddToWalletIfInvolvingMe(hash, tx, pblock, true);
530+
}
531+
532+
void CWallet::EraseFromWallet(const uint256 &hash)
529533
{
530534
if (!fFileBacked)
531-
return false;
535+
return;
532536
{
533537
LOCK(cs_wallet);
534538
if (mapWallet.erase(hash))
535539
CWalletDB(strWalletFile).EraseTx(hash);
536540
}
537-
return true;
541+
return;
538542
}
539543

540544

@@ -1496,14 +1500,6 @@ bool CWallet::SetDefaultKey(const CPubKey &vchPubKey)
14961500
return true;
14971501
}
14981502

1499-
bool GetWalletFile(CWallet* pwallet, string &strWalletFileOut)
1500-
{
1501-
if (!pwallet->fFileBacked)
1502-
return false;
1503-
strWalletFileOut = pwallet->strWalletFile;
1504-
return true;
1505-
}
1506-
15071503
//
15081504
// Mark old keypool keys as used,
15091505
// and generate all new keys

src/wallet.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ class CAddressBookData
8080
/** A CWallet is an extension of a keystore, which also maintains a set of transactions and balances,
8181
* and provides the ability to create new transactions.
8282
*/
83-
class CWallet : public CCryptoKeyStore
83+
class CWallet : public CCryptoKeyStore, public CWalletInterface
8484
{
8585
private:
8686
bool SelectCoins(int64 nTargetValue, std::set<std::pair<const CWalletTx*,unsigned int> >& setCoinsRet, int64& nValueRet) const;
@@ -197,8 +197,9 @@ class CWallet : public CCryptoKeyStore
197197

198198
void MarkDirty();
199199
bool AddToWallet(const CWalletTx& wtxIn);
200-
bool AddToWalletIfInvolvingMe(const uint256 &hash, const CTransaction& tx, const CBlock* pblock, bool fUpdate = false, bool fFindBlock = false);
201-
bool EraseFromWallet(uint256 hash);
200+
void SyncTransaction(const uint256 &hash, const CTransaction& tx, const CBlock* pblock);
201+
bool AddToWalletIfInvolvingMe(const uint256 &hash, const CTransaction& tx, const CBlock* pblock, bool fUpdate);
202+
void EraseFromWallet(const uint256 &hash);
202203
void WalletUpdateSpent(const CTransaction& prevout);
203204
int ScanForWalletTransactions(CBlockIndex* pindexStart, bool fUpdate = false);
204205
void ReacceptWalletTransactions();
@@ -887,6 +888,4 @@ class CAccountingEntry
887888
std::vector<char> _ssExtra;
888889
};
889890

890-
bool GetWalletFile(CWallet* pwallet, std::string &strWalletFileOut);
891-
892891
#endif

0 commit comments

Comments
 (0)