Skip to content

Commit 4b81f22

Browse files
MarcoFalkeknst
authored andcommitted
Merge bitcoin#19556: Remove mempool global
fafb381 Remove mempool global (MarcoFalke) fa0359c Remove mempool global from p2p (MarcoFalke) eeee110 Remove mempool global from init (MarcoFalke) Pull request description: This refactor unlocks some nice potential features, such as, but not limited to: * Removing the fee estimates global (would avoid slightly fragile workarounds such as bitcoin#18766) * Making the mempool optional for a "blocksonly" operation mode Even absent those features, the new code without the global should be easier to maintain, read and write tests for. ACKs for top commit: jnewbery: utACK fafb381 hebasto: ACK fafb381, I have reviewed the code and it looks OK, I agree it can be merged. darosior: ACK fafb381 Tree-SHA512: a2e696dc377e2e81eaf9c389e6d13dde4a48d81f3538df88f4da502d3012dd61078495140ab5a5854f360a06249fe0e1f6a094c4e006d8b5cc2552a946becf26
1 parent be2d50b commit 4b81f22

26 files changed

+120
-85
lines changed

src/coinjoin/client.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ void CCoinJoinClientQueueManager::ProcessDSQueue(const CNode& peer, CDataStream&
134134
}
135135
}
136136

137-
void CCoinJoinClientManager::ProcessMessage(CNode& peer, std::string_view msg_type, CDataStream& vRecv, CConnman& connman)
137+
void CCoinJoinClientManager::ProcessMessage(const CTxMemPool& mempool, CNode& peer, std::string_view msg_type, CDataStream& vRecv, CConnman& connman)
138138
{
139139
if (fMasternodeMode) return;
140140
if (!CCoinJoinClientOptions::IsEnabled()) return;
@@ -153,12 +153,12 @@ void CCoinJoinClientManager::ProcessMessage(CNode& peer, std::string_view msg_ty
153153
AssertLockNotHeld(cs_deqsessions);
154154
LOCK(cs_deqsessions);
155155
for (auto& session : deqSessions) {
156-
session.ProcessMessage(peer, msg_type, vRecv, connman);
156+
session.ProcessMessage(mempool, peer, msg_type, vRecv, connman);
157157
}
158158
}
159159
}
160160

161-
void CCoinJoinClientSession::ProcessMessage(CNode& peer, std::string_view msg_type, CDataStream& vRecv, CConnman& connman)
161+
void CCoinJoinClientSession::ProcessMessage(const CTxMemPool& mempool, CNode& peer, std::string_view msg_type, CDataStream& vRecv, CConnman& connman)
162162
{
163163
if (fMasternodeMode) return;
164164
if (!CCoinJoinClientOptions::IsEnabled()) return;
@@ -193,7 +193,7 @@ void CCoinJoinClientSession::ProcessMessage(CNode& peer, std::string_view msg_ty
193193
LogPrint(BCLog::COINJOIN, "DSFINALTX -- txNew %s", txNew.ToString()); /* Continued */
194194

195195
// check to see if input is spent already? (and probably not confirmed)
196-
SignFinalTransaction(txNew, peer, connman);
196+
SignFinalTransaction(mempool, txNew, peer, connman);
197197

198198
} else if (msg_type == NetMsgType::DSCOMPLETE) {
199199
if (!mixingMasternode) return;
@@ -539,7 +539,7 @@ void CCoinJoinClientSession::ProcessPoolStateUpdate(CCoinJoinStatusUpdate psssup
539539
// check it to make sure it's what we want, then sign it if we agree.
540540
// If we refuse to sign, it's possible we'll be charged collateral
541541
//
542-
bool CCoinJoinClientSession::SignFinalTransaction(const CTransaction& finalTransactionNew, CNode& peer, CConnman& connman)
542+
bool CCoinJoinClientSession::SignFinalTransaction(const CTxMemPool& mempool, const CTransaction& finalTransactionNew, CNode& peer, CConnman& connman)
543543
{
544544
if (!CCoinJoinClientOptions::IsEnabled()) return false;
545545

@@ -568,7 +568,7 @@ bool CCoinJoinClientSession::SignFinalTransaction(const CTransaction& finalTrans
568568

569569
// Make sure all inputs/outputs are valid
570570
PoolMessage nMessageID{MSG_NOERR};
571-
if (!IsValidInOuts(finalMutableTransaction.vin, finalMutableTransaction.vout, nMessageID, nullptr)) {
571+
if (!IsValidInOuts(mempool, finalMutableTransaction.vin, finalMutableTransaction.vout, nMessageID, nullptr)) {
572572
LogPrint(BCLog::COINJOIN, "CCoinJoinClientSession::%s -- ERROR! IsValidInOuts() failed: %s\n", __func__, CCoinJoin::GetMessageByID(nMessageID).translated);
573573
UnlockCoins();
574574
keyHolderStorage.ReturnAll();
@@ -756,7 +756,7 @@ bool CCoinJoinClientManager::CheckAutomaticBackup()
756756
//
757757
// Passively run mixing in the background to mix funds based on the given configuration.
758758
//
759-
bool CCoinJoinClientSession::DoAutomaticDenominating(CConnman& connman, bool fDryRun)
759+
bool CCoinJoinClientSession::DoAutomaticDenominating(CTxMemPool& mempool, CConnman& connman, bool fDryRun)
760760
{
761761
if (fMasternodeMode) return false; // no client-side mixing on masternodes
762762
if (nState != POOL_STATE_IDLE) return false;
@@ -909,7 +909,7 @@ bool CCoinJoinClientSession::DoAutomaticDenominating(CConnman& connman, bool fDr
909909
return false;
910910
}
911911
} else {
912-
if (!CCoinJoin::IsCollateralValid(CTransaction(txMyCollateral))) {
912+
if (!CCoinJoin::IsCollateralValid(mempool, CTransaction(txMyCollateral))) {
913913
LogPrint(BCLog::COINJOIN, "CCoinJoinClientSession::DoAutomaticDenominating -- invalid collateral, recreating...\n");
914914
if (!CreateCollateralTransaction(txMyCollateral, strReason)) {
915915
LogPrint(BCLog::COINJOIN, "CCoinJoinClientSession::DoAutomaticDenominating -- create collateral error: %s\n", strReason);
@@ -936,7 +936,7 @@ bool CCoinJoinClientSession::DoAutomaticDenominating(CConnman& connman, bool fDr
936936
return false;
937937
}
938938

939-
bool CCoinJoinClientManager::DoAutomaticDenominating(CConnman& connman, bool fDryRun)
939+
bool CCoinJoinClientManager::DoAutomaticDenominating(CTxMemPool& mempool, CConnman& connman, bool fDryRun)
940940
{
941941
if (fMasternodeMode) return false; // no client-side mixing on masternodes
942942
if (!CCoinJoinClientOptions::IsEnabled() || !IsMixing()) return false;
@@ -978,7 +978,7 @@ bool CCoinJoinClientManager::DoAutomaticDenominating(CConnman& connman, bool fDr
978978
return false;
979979
}
980980

981-
fResult &= session.DoAutomaticDenominating(connman, fDryRun);
981+
fResult &= session.DoAutomaticDenominating(mempool, connman, fDryRun);
982982
}
983983

984984
return fResult;
@@ -1815,7 +1815,7 @@ void CCoinJoinClientQueueManager::DoMaintenance()
18151815
CheckQueue();
18161816
}
18171817

1818-
void CCoinJoinClientManager::DoMaintenance(CConnman& connman)
1818+
void CCoinJoinClientManager::DoMaintenance(CTxMemPool& mempool, CConnman& connman)
18191819
{
18201820
if (!CCoinJoinClientOptions::IsEnabled()) return;
18211821
if (m_mn_sync == nullptr) return;
@@ -1830,7 +1830,7 @@ void CCoinJoinClientManager::DoMaintenance(CConnman& connman)
18301830
CheckTimeout();
18311831
ProcessPendingDsaRequest(connman);
18321832
if (nDoAutoNextRun == nTick) {
1833-
DoAutomaticDenominating(connman);
1833+
DoAutomaticDenominating(mempool, connman);
18341834
nDoAutoNextRun = nTick + COINJOIN_AUTO_TIMEOUT_MIN + GetRandInt(COINJOIN_AUTO_TIMEOUT_MAX - COINJOIN_AUTO_TIMEOUT_MIN);
18351835
}
18361836
}
@@ -1867,14 +1867,14 @@ void CCoinJoinClientManager::GetJsonInfo(UniValue& obj) const
18671867
obj.pushKV("sessions", arrSessions);
18681868
}
18691869

1870-
void DoCoinJoinMaintenance(CConnman& connman)
1870+
void DoCoinJoinMaintenance(CTxMemPool& mempool, CConnman& connman)
18711871
{
18721872
if (coinJoinClientQueueManager != nullptr) {
18731873
coinJoinClientQueueManager->DoMaintenance();
18741874
}
18751875

18761876
for (const auto& pair : coinJoinClientManagers) {
1877-
pair.second->DoMaintenance(connman);
1877+
pair.second->DoMaintenance(mempool, connman);
18781878
}
18791879
}
18801880

src/coinjoin/client.h

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class CCoinJoinClientQueueManager;
2020

2121
class CConnman;
2222
class CNode;
23+
class CTxMemPool;
2324

2425
class UniValue;
2526
class CMasternodeSync;
@@ -114,7 +115,7 @@ class CCoinJoinClientSession : public CCoinJoinBaseSession
114115
void CompletedTransaction(PoolMessage nMessageID);
115116

116117
/// As a client, check and sign the final transaction
117-
bool SignFinalTransaction(const CTransaction& finalTransactionNew, CNode& peer, CConnman& connman) LOCKS_EXCLUDED(cs_coinjoin);
118+
bool SignFinalTransaction(const CTxMemPool& mempool, const CTransaction& finalTransactionNew, CNode& peer, CConnman& connman) LOCKS_EXCLUDED(cs_coinjoin);
118119

119120
void RelayIn(const CCoinJoinEntry& entry, CConnman& connman) const;
120121

@@ -126,7 +127,7 @@ class CCoinJoinClientSession : public CCoinJoinBaseSession
126127
{
127128
}
128129

129-
void ProcessMessage(CNode& peer, std::string_view msg_type, CDataStream& vRecv, CConnman& connman);
130+
void ProcessMessage(const CTxMemPool& mempool, CNode& peer, std::string_view msg_type, CDataStream& vRecv, CConnman& connman);
130131

131132
void UnlockCoins();
132133

@@ -137,7 +138,7 @@ class CCoinJoinClientSession : public CCoinJoinBaseSession
137138
bool GetMixingMasternodeInfo(CDeterministicMNCPtr& ret) const;
138139

139140
/// Passively run mixing in the background according to the configuration in settings
140-
bool DoAutomaticDenominating(CConnman& connman, bool fDryRun = false) LOCKS_EXCLUDED(cs_coinjoin);
141+
bool DoAutomaticDenominating(CTxMemPool& mempool, CConnman& connman, bool fDryRun = false) LOCKS_EXCLUDED(cs_coinjoin);
141142

142143
/// As a client, submit part of a future mixing transaction to a Masternode to start the process
143144
bool SubmitDenominate(CConnman& connman);
@@ -187,6 +188,7 @@ class CCoinJoinClientManager
187188
bilingual_str strAutoDenomResult;
188189

189190
CWallet& mixingWallet;
191+
// CTxMemPool& mempool;
190192

191193
// Keep track of current block height
192194
int nCachedBlockHeight{0};
@@ -205,9 +207,10 @@ class CCoinJoinClientManager
205207
CCoinJoinClientManager& operator=(CCoinJoinClientManager const&) = delete;
206208

207209
explicit CCoinJoinClientManager(CWallet& wallet, const std::unique_ptr<CMasternodeSync>& mn_sync) :
208-
mixingWallet(wallet), m_mn_sync(mn_sync) {}
210+
mixingWallet(wallet),
211+
m_mn_sync(mn_sync) {}
209212

210-
void ProcessMessage(CNode& peer, std::string_view msg_type, CDataStream& vRecv, CConnman& connman) LOCKS_EXCLUDED(cs_deqsessions);
213+
void ProcessMessage(const CTxMemPool& mempool, CNode& peer, std::string_view msg_type, CDataStream& vRecv, CConnman& connman) LOCKS_EXCLUDED(cs_deqsessions);
211214

212215
bool StartMixing();
213216
void StopMixing();
@@ -220,7 +223,7 @@ class CCoinJoinClientManager
220223
bool GetMixingMasternodesInfo(std::vector<CDeterministicMNCPtr>& vecDmnsRet) const LOCKS_EXCLUDED(cs_deqsessions);
221224

222225
/// Passively run mixing in the background according to the configuration in settings
223-
bool DoAutomaticDenominating(CConnman& connman, bool fDryRun = false) LOCKS_EXCLUDED(cs_deqsessions);
226+
bool DoAutomaticDenominating(CTxMemPool& mempool, CConnman& connman, bool fDryRun = false) LOCKS_EXCLUDED(cs_deqsessions);
224227

225228
bool TrySubmitDenominate(const CService& mnAddr, CConnman& connman) LOCKS_EXCLUDED(cs_deqsessions);
226229
bool MarkAlreadyJoinedQueueAsTried(CCoinJoinQueue& dsq) const LOCKS_EXCLUDED(cs_deqsessions);
@@ -236,12 +239,12 @@ class CCoinJoinClientManager
236239

237240
void UpdatedBlockTip(const CBlockIndex* pindex);
238241

239-
void DoMaintenance(CConnman& connman);
242+
void DoMaintenance(CTxMemPool& mempool, CConnman& connman);
240243

241244
void GetJsonInfo(UniValue& obj) const LOCKS_EXCLUDED(cs_deqsessions);
242245
};
243246

244247

245-
void DoCoinJoinMaintenance(CConnman& connman);
248+
void DoCoinJoinMaintenance(CTxMemPool& mempool, CConnman& connman);
246249

247250
#endif // BITCOIN_COINJOIN_CLIENT_H

src/coinjoin/coinjoin.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ std::string CCoinJoinBaseSession::GetStateString() const
217217
}
218218
}
219219

220-
bool CCoinJoinBaseSession::IsValidInOuts(const std::vector<CTxIn>& vin, const std::vector<CTxOut>& vout, PoolMessage& nMessageIDRet, bool* fConsumeCollateralRet) const
220+
bool CCoinJoinBaseSession::IsValidInOuts(const CTxMemPool& mempool, const std::vector<CTxIn>& vin, const std::vector<CTxOut>& vout, PoolMessage& nMessageIDRet, bool* fConsumeCollateralRet) const
221221
{
222222
std::set<CScript> setScripPubKeys;
223223
nMessageIDRet = MSG_NOERR;
@@ -308,7 +308,7 @@ Mutex CCoinJoin::cs_mapdstx;
308308
std::map<uint256, CCoinJoinBroadcastTx> CCoinJoin::mapDSTX GUARDED_BY(CCoinJoin::cs_mapdstx);
309309

310310
// check to make sure the collateral provided by the client is valid
311-
bool CCoinJoin::IsCollateralValid(const CTransaction& txCollateral)
311+
bool CCoinJoin::IsCollateralValid(CTxMemPool& mempool, const CTransaction& txCollateral)
312312
{
313313
if (txCollateral.vout.empty()) return false;
314314
if (txCollateral.nLockTime != 0) return false;

src/coinjoin/coinjoin.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class CConnman;
2424
class CBLSPublicKey;
2525
class CBlockIndex;
2626
class CMasternodeSync;
27+
class CTxMemPool;
2728

2829
namespace llmq {
2930
class CChainLocksHandler;
@@ -343,7 +344,7 @@ class CCoinJoinBaseSession
343344

344345
void SetNull() EXCLUSIVE_LOCKS_REQUIRED(cs_coinjoin);
345346

346-
bool IsValidInOuts(const std::vector<CTxIn>& vin, const std::vector<CTxOut>& vout, PoolMessage& nMessageIDRet, bool* fConsumeCollateralRet) const;
347+
bool IsValidInOuts(const CTxMemPool& mempool, const std::vector<CTxIn>& vin, const std::vector<CTxOut>& vout, PoolMessage& nMessageIDRet, bool* fConsumeCollateralRet) const;
347348

348349
public:
349350
int nSessionDenom{0}; // Users must submit a denom matching this
@@ -472,7 +473,7 @@ class CCoinJoin
472473
static constexpr CAmount GetMaxPoolAmount() { return COINJOIN_ENTRY_MAX_SIZE * vecStandardDenominations.front(); }
473474

474475
/// If the collateral is valid given by a client
475-
static bool IsCollateralValid(const CTransaction& txCollateral);
476+
static bool IsCollateralValid(CTxMemPool& mempool, const CTransaction& txCollateral);
476477
static constexpr CAmount GetCollateralAmount() { return GetSmallestDenomination() / 10; }
477478
static constexpr CAmount GetMaxCollateralAmount() { return GetCollateralAmount() * 4; }
478479

src/coinjoin/server.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ bool CCoinJoinServer::AddEntry(const CCoinJoinEntry& entry, PoolMessage& nMessag
577577
return false;
578578
}
579579

580-
if (!CCoinJoin::IsCollateralValid(*entry.txCollateral)) {
580+
if (!CCoinJoin::IsCollateralValid(mempool, *entry.txCollateral)) {
581581
LogPrint(BCLog::COINJOIN, "CCoinJoinServer::%s -- ERROR: collateral not valid!\n", __func__);
582582
nMessageIDRet = ERR_INVALID_COLLATERAL;
583583
return false;
@@ -611,7 +611,7 @@ bool CCoinJoinServer::AddEntry(const CCoinJoinEntry& entry, PoolMessage& nMessag
611611
}
612612

613613
bool fConsumeCollateral{false};
614-
if (!IsValidInOuts(vin, entry.vecTxOut, nMessageIDRet, &fConsumeCollateral)) {
614+
if (!IsValidInOuts(mempool, vin, entry.vecTxOut, nMessageIDRet, &fConsumeCollateral)) {
615615
LogPrint(BCLog::COINJOIN, "CCoinJoinServer::%s -- ERROR! IsValidInOuts() failed: %s\n", __func__, CCoinJoin::GetMessageByID(nMessageIDRet).translated);
616616
if (fConsumeCollateral) {
617617
ConsumeCollateral(entry.txCollateral);
@@ -688,7 +688,7 @@ bool CCoinJoinServer::IsAcceptableDSA(const CCoinJoinAccept& dsa, PoolMessage& n
688688
}
689689

690690
// check collateral
691-
if (!fUnitTest && !CCoinJoin::IsCollateralValid(CTransaction(dsa.txCollateral))) {
691+
if (!fUnitTest && !CCoinJoin::IsCollateralValid(mempool, CTransaction(dsa.txCollateral))) {
692692
LogPrint(BCLog::COINJOIN, "CCoinJoinServer::%s -- collateral not valid!\n", __func__);
693693
nMessageIDRet = ERR_INVALID_COLLATERAL;
694694
return false;

src/coinjoin/server.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <net.h>
1010

1111
class CCoinJoinServer;
12+
class CTxMemPool;
1213
class UniValue;
1314

1415
// The main object for accessing mixing
@@ -19,6 +20,7 @@ extern std::unique_ptr<CCoinJoinServer> coinJoinServer;
1920
class CCoinJoinServer : public CCoinJoinBaseSession, public CCoinJoinBaseManager
2021
{
2122
private:
23+
CTxMemPool& mempool;
2224
CConnman& connman;
2325
const std::unique_ptr<CMasternodeSync>& m_mn_sync;
2426

@@ -75,10 +77,11 @@ class CCoinJoinServer : public CCoinJoinBaseSession, public CCoinJoinBaseManager
7577
void SetNull() EXCLUSIVE_LOCKS_REQUIRED(cs_coinjoin);
7678

7779
public:
78-
explicit CCoinJoinServer(CConnman& _connman, const std::unique_ptr<CMasternodeSync>& mn_sync) :
80+
explicit CCoinJoinServer(CTxMemPool& mempool, CConnman& _connman, const std::unique_ptr<CMasternodeSync>& mn_sync) :
7981
vecSessionCollaterals(),
8082
fUnitTest(false),
8183
connman(_connman),
84+
mempool(mempool),
8285
m_mn_sync(mn_sync) {};
8386

8487
void ProcessMessage(CNode& pfrom, std::string_view msg_type, CDataStream& vRecv);

0 commit comments

Comments
 (0)