Skip to content

Commit e13934c

Browse files
committed
Merge pull request #3115 from sipa/walletmain
Interaction cleanups between main and wallet
2 parents cde1060 + 722fa28 commit e13934c

File tree

10 files changed

+109
-178
lines changed

10 files changed

+109
-178
lines changed

src/bitcoind.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
// Distributed under the MIT/X11 software license, see the accompanying
44
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
55

6+
#include "ui_interface.h"
67
#include "init.h"
8+
#include "util.h"
9+
#include "main.h"
710
#include "bitcoinrpc.h"
811
#include <boost/algorithm/string/predicate.hpp>
912

src/bitcoinrpc.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
55

66
#include "chainparams.h"
7+
#include "main.h"
8+
#include "wallet.h"
79
#include "init.h"
810
#include "util.h"
911
#include "sync.h"

src/init.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
#ifndef BITCOIN_INIT_H
66
#define BITCOIN_INIT_H
77

8-
#include "wallet.h"
8+
#include <string>
9+
#include <boost/thread.hpp>
10+
11+
class CWallet;
912

1013
extern std::string strWalletFile;
1114
extern CWallet* pwalletMain;

src/main.cpp

Lines changed: 46 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -74,93 +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();
98-
}
99-
100-
// get the wallet transaction with the given hash (if it exists)
101-
bool static GetTransaction(const uint256& hashTx, CWalletTx& wtx)
102-
{
103-
LOCK(cs_setpwalletRegistered);
104-
BOOST_FOREACH(CWallet* pwallet, setpwalletRegistered)
105-
if (pwallet->GetTransaction(hashTx,wtx))
106-
return true;
107-
return false;
108-
}
109-
110-
// erases transaction with the given hash from all wallets
111-
void static EraseFromWallets(uint256 hash)
112-
{
113-
LOCK(cs_setpwalletRegistered);
114-
BOOST_FOREACH(CWallet* pwallet, setpwalletRegistered)
115-
pwallet->EraseFromWallet(hash);
116-
}
117-
118-
// make sure all wallets know about the given transaction, in the given block
119-
void SyncWithWallets(const uint256 &hash, const CTransaction& tx, const CBlock* pblock, bool fUpdate)
120-
{
121-
LOCK(cs_setpwalletRegistered);
122-
BOOST_FOREACH(CWallet* pwallet, setpwalletRegistered)
123-
pwallet->AddToWalletIfInvolvingMe(hash, tx, pblock, fUpdate);
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;
12492
}
12593

126-
// notify wallets about a new best chain
127-
void static SetBestChain(const CBlockLocator& loc)
128-
{
129-
LOCK(cs_setpwalletRegistered);
130-
BOOST_FOREACH(CWallet* pwallet, setpwalletRegistered)
131-
pwallet->SetBestChain(loc);
132-
}
133-
134-
// notify wallets about an updated transaction
135-
void static UpdatedTransaction(const uint256& hashTx)
136-
{
137-
LOCK(cs_setpwalletRegistered);
138-
BOOST_FOREACH(CWallet* pwallet, setpwalletRegistered)
139-
pwallet->UpdatedTransaction(hashTx);
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));
140101
}
141102

142-
// dump all wallets
143-
void static PrintWallets(const CBlock& block)
144-
{
145-
LOCK(cs_setpwalletRegistered);
146-
BOOST_FOREACH(CWallet* pwallet, setpwalletRegistered)
147-
pwallet->PrintWallet(block);
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));
148110
}
149111

150-
// notify wallets about an incoming inventory (for request counts)
151-
void static Inventory(const uint256& hash)
152-
{
153-
LOCK(cs_setpwalletRegistered);
154-
BOOST_FOREACH(CWallet* pwallet, setpwalletRegistered)
155-
pwallet->Inventory(hash);
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();
156119
}
157120

158-
// ask wallets to resend their transactions
159-
void static ResendWalletTransactions()
160-
{
161-
LOCK(cs_setpwalletRegistered);
162-
BOOST_FOREACH(CWallet* pwallet, setpwalletRegistered)
163-
pwallet->ResendWalletTransactions();
121+
void SyncWithWallets(const uint256 &hash, const CTransaction &tx, const CBlock *pblock) {
122+
g_signals.SyncTransaction(hash, tx, pblock);
164123
}
165124

166125
//////////////////////////////////////////////////////////////////////////////
@@ -931,8 +890,8 @@ bool CTxMemPool::accept(CValidationState &state, const CTransaction &tx, bool fL
931890
///// are we sure this is ok when loading transactions or restoring block txes
932891
// If updated, erase old tx from wallet
933892
if (ptxOld)
934-
EraseFromWallets(ptxOld->GetHash());
935-
SyncWithWallets(hash, tx, NULL, true);
893+
g_signals.EraseTransaction(ptxOld->GetHash());
894+
g_signals.SyncTransaction(hash, tx, NULL);
936895

937896
LogPrint("mempool", "CTxMemPool::accept() : accepted %s (poolsz %"PRIszu")\n",
938897
hash.ToString().c_str(),
@@ -1095,27 +1054,6 @@ bool CMerkleTx::AcceptToMemoryPool(bool fLimitFree)
10951054
}
10961055

10971056

1098-
1099-
bool CWalletTx::AcceptWalletTransaction()
1100-
{
1101-
{
1102-
LOCK(mempool.cs);
1103-
// Add previous supporting transactions first
1104-
BOOST_FOREACH(CMerkleTx& tx, vtxPrev)
1105-
{
1106-
if (!tx.IsCoinBase())
1107-
{
1108-
uint256 hash = tx.GetHash();
1109-
if (!mempool.exists(hash) && pcoinsTip->HaveCoins(hash))
1110-
tx.AcceptToMemoryPool(false);
1111-
}
1112-
}
1113-
return AcceptToMemoryPool(false);
1114-
}
1115-
return false;
1116-
}
1117-
1118-
11191057
// Return transaction in tx, and if it was found inside a block, its hash is placed in hashBlock
11201058
bool GetTransaction(const uint256 &hash, CTransaction &txOut, uint256 &hashBlock, bool fAllowSlow)
11211059
{
@@ -1992,7 +1930,7 @@ bool ConnectBlock(CBlock& block, CValidationState& state, CBlockIndex* pindex, C
19921930

19931931
// Watch for transactions paying to me
19941932
for (unsigned int i = 0; i < block.vtx.size(); i++)
1995-
SyncWithWallets(block.GetTxHash(i), block.vtx[i], &block, true);
1933+
g_signals.SyncTransaction(block.GetTxHash(i), block.vtx[i], &block);
19961934

19971935
return true;
19981936
}
@@ -2126,7 +2064,7 @@ bool SetBestChain(CValidationState &state, CBlockIndex* pindexNew)
21262064

21272065
// Update best block in wallet (so we can detect restored wallets)
21282066
if ((pindexNew->nHeight % 20160) == 0 || (!fIsInitialDownload && (pindexNew->nHeight % 144) == 0))
2129-
::SetBestChain(chainActive.GetLocator(pindexNew));
2067+
g_signals.SetBestChain(chainActive.GetLocator(pindexNew));
21302068

21312069
// New best block
21322070
nTimeBestReceived = GetTime();
@@ -2206,7 +2144,7 @@ bool AddToBlockIndex(CBlock& block, CValidationState& state, const CDiskBlockPos
22062144
CheckForkWarningConditions();
22072145
// Notify UI to display prev block's coinbase if it was ours
22082146
static uint256 hashPrevBestCoinBase;
2209-
UpdatedTransaction(hashPrevBestCoinBase);
2147+
g_signals.UpdatedTransaction(hashPrevBestCoinBase);
22102148
hashPrevBestCoinBase = block.GetTxHash(0);
22112149
} else
22122150
CheckForkWarningConditionsOnNewFork(pindexNew);
@@ -3041,8 +2979,6 @@ void PrintBlockTree()
30412979
DateTimeStrFormat("%Y-%m-%d %H:%M:%S", block.GetBlockTime()).c_str(),
30422980
block.vtx.size());
30432981

3044-
PrintWallets(block);
3045-
30462982
// put the main time-chain first
30472983
vector<CBlockIndex*>& vNext = mapNext[pindex];
30482984
for (unsigned int i = 0; i < vNext.size(); i++)
@@ -3331,7 +3267,7 @@ void static ProcessGetData(CNode* pfrom)
33313267
}
33323268

33333269
// Track requests for our stuff.
3334-
Inventory(inv.hash);
3270+
g_signals.Inventory(inv.hash);
33353271
}
33363272
}
33373273

@@ -3593,7 +3529,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)
35933529
}
35943530

35953531
// Track requests for our stuff
3596-
Inventory(inv.hash);
3532+
g_signals.Inventory(inv.hash);
35973533
}
35983534
}
35993535

@@ -4215,7 +4151,7 @@ bool SendMessages(CNode* pto, bool fSendTrickle)
42154151
// transactions become unconfirmed and spams other nodes.
42164152
if (!fReindex && !fImporting && !IsInitialBlockDownload())
42174153
{
4218-
ResendWalletTransactions();
4154+
g_signals.Broadcast();
42194155
}
42204156

42214157
//
@@ -4243,15 +4179,6 @@ bool SendMessages(CNode* pto, bool fSendTrickle)
42434179
hashRand = Hash(BEGIN(hashRand), END(hashRand));
42444180
bool fTrickleWait = ((hashRand & 3) != 0);
42454181

4246-
// always trickle our own transactions
4247-
if (!fTrickleWait)
4248-
{
4249-
CWalletTx wtx;
4250-
if (GetTransaction(inv.hash, wtx))
4251-
if (wtx.fFromMe)
4252-
fTrickleWait = true;
4253-
}
4254-
42554182
if (fTrickleWait)
42564183
{
42574184
vInvWait.push_back(inv);

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;
@@ -1261,4 +1256,18 @@ class CMerkleBlock
12611256
)
12621257
};
12631258

1259+
1260+
class CWalletInterface {
1261+
protected:
1262+
virtual void SyncTransaction(const uint256 &hash, const CTransaction &tx, const CBlock *pblock) =0;
1263+
virtual void EraseFromWallet(const uint256 &hash) =0;
1264+
virtual void SetBestChain(const CBlockLocator &locator) =0;
1265+
virtual void UpdatedTransaction(const uint256 &hash) =0;
1266+
virtual void Inventory(const uint256 &hash) =0;
1267+
virtual void ResendWalletTransactions() =0;
1268+
friend void ::RegisterWallet(CWalletInterface*);
1269+
friend void ::UnregisterWallet(CWalletInterface*);
1270+
friend void ::UnregisterAllWallets();
1271+
};
1272+
12641273
#endif

src/qt/optionsmodel.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
#include "bitcoinunits.h"
88
#include "init.h"
9+
#include "core.h"
10+
#include "wallet.h"
11+
#include "netbase.h"
912
#include "walletdb.h"
1013
#include "guiutil.h"
1114

src/rpcdump.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <fstream>
77

88
#include "init.h" // for pwalletMain
9+
#include "wallet.h"
910
#include "bitcoinrpc.h"
1011
#include "ui_interface.h"
1112
#include "base58.h"

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

0 commit comments

Comments
 (0)