Skip to content

Commit e38ffdc

Browse files
committed
refactor: move CCoinJoinQueue from coinjoin.h to common.h to drop dependency of msg_result on the heavy header
It's temporary refactoring; may be discarded later
1 parent 589a8fb commit e38ffdc

File tree

9 files changed

+95
-90
lines changed

9 files changed

+95
-90
lines changed

src/coinjoin/client.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ MessageProcessingResult CCoinJoinClientQueueManager::ProcessMessage(NodeId from,
135135
WITH_LOCK(cs_vecqueue, vecCoinJoinQueue.push_back(dsq));
136136
}
137137
} // cs_ProcessDSQueue
138-
ret.m_dsq.push_back(dsq);
138+
ret.m_dsq = dsq;
139139
return ret;
140140
}
141141

src/coinjoin/coinjoin.cpp

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -38,34 +38,6 @@ bool CCoinJoinEntry::AddScriptSig(const CTxIn& txin)
3838
return false;
3939
}
4040

41-
uint256 CCoinJoinQueue::GetSignatureHash() const
42-
{
43-
return SerializeHash(*this, SER_GETHASH, PROTOCOL_VERSION);
44-
}
45-
uint256 CCoinJoinQueue::GetHash() const { return SerializeHash(*this, SER_NETWORK, PROTOCOL_VERSION); }
46-
47-
bool CCoinJoinQueue::CheckSignature(const CBLSPublicKey& blsPubKey) const
48-
{
49-
if (!CBLSSignature(Span{vchSig}, false).VerifyInsecure(blsPubKey, GetSignatureHash(), false)) {
50-
LogPrint(BCLog::COINJOIN, "CCoinJoinQueue::CheckSignature -- VerifyInsecure() failed\n");
51-
return false;
52-
}
53-
54-
return true;
55-
}
56-
57-
bool CCoinJoinQueue::IsTimeOutOfBounds(int64_t current_time) const
58-
{
59-
return current_time - nTime > COINJOIN_QUEUE_TIMEOUT ||
60-
nTime - current_time > COINJOIN_QUEUE_TIMEOUT;
61-
}
62-
63-
[[nodiscard]] std::string CCoinJoinQueue::ToString() const
64-
{
65-
return strprintf("nDenom=%d, nTime=%lld, fReady=%s, fTried=%s, masternode=%s",
66-
nDenom, nTime, fReady ? "true" : "false", fTried ? "true" : "false", masternodeOutpoint.ToStringShort());
67-
}
68-
6941
uint256 CCoinJoinBroadcastTx::GetSignatureHash() const
7042
{
7143
return SerializeHash(*this, SER_GETHASH, PROTOCOL_VERSION);

src/coinjoin/coinjoin.h

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ extern RecursiveMutex cs_main;
3939
// timeouts
4040
static constexpr int COINJOIN_AUTO_TIMEOUT_MIN = 5;
4141
static constexpr int COINJOIN_AUTO_TIMEOUT_MAX = 15;
42-
static constexpr int COINJOIN_QUEUE_TIMEOUT = 30;
4342
static constexpr int COINJOIN_SIGNING_TIMEOUT = 15;
4443

4544
static constexpr size_t COINJOIN_ENTRY_MAX_SIZE = 9;
@@ -171,57 +170,6 @@ class CCoinJoinEntry
171170
};
172171

173172

174-
/**
175-
* A currently in progress mixing merge and denomination information
176-
*/
177-
class CCoinJoinQueue
178-
{
179-
public:
180-
int nDenom{0};
181-
COutPoint masternodeOutpoint;
182-
uint256 m_protxHash;
183-
int64_t nTime{0};
184-
bool fReady{false}; //ready for submit
185-
std::vector<unsigned char> vchSig;
186-
// memory only
187-
bool fTried{false};
188-
189-
CCoinJoinQueue() = default;
190-
191-
CCoinJoinQueue(int nDenom, const COutPoint& outpoint, const uint256& proTxHash, int64_t nTime, bool fReady) :
192-
nDenom(nDenom),
193-
masternodeOutpoint(outpoint),
194-
m_protxHash(proTxHash),
195-
nTime(nTime),
196-
fReady(fReady)
197-
{
198-
}
199-
200-
SERIALIZE_METHODS(CCoinJoinQueue, obj)
201-
{
202-
READWRITE(obj.nDenom, obj.m_protxHash, obj.nTime, obj.fReady);
203-
if (!(s.GetType() & SER_GETHASH)) {
204-
READWRITE(obj.vchSig);
205-
}
206-
}
207-
208-
[[nodiscard]] uint256 GetHash() const;
209-
[[nodiscard]] uint256 GetSignatureHash() const;
210-
211-
/// Check if we have a valid Masternode address
212-
[[nodiscard]] bool CheckSignature(const CBLSPublicKey& blsPubKey) const;
213-
214-
/// Check if a queue is too old or too far into the future
215-
[[nodiscard]] bool IsTimeOutOfBounds(int64_t current_time = GetAdjustedTime()) const;
216-
217-
[[nodiscard]] std::string ToString() const;
218-
219-
friend bool operator==(const CCoinJoinQueue& a, const CCoinJoinQueue& b)
220-
{
221-
return a.nDenom == b.nDenom && a.masternodeOutpoint == b.masternodeOutpoint && a.nTime == b.nTime && a.fReady == b.fReady;
222-
}
223-
};
224-
225173
/** Helper class to store mixing transaction (tx) information.
226174
*/
227175
class CCoinJoinBroadcastTx

src/coinjoin/common.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
#include <coinjoin/common.h>
66

7+
#include <bls/bls.h>
78
#include <core_io.h>
9+
#include <logging.h>
810
#include <univalue.h>
911

1012
namespace CoinJoin
@@ -24,3 +26,32 @@ std::string DenominationToString(int nDenom)
2426
}
2527

2628
} // namespace CoinJoin
29+
//
30+
uint256 CCoinJoinQueue::GetSignatureHash() const
31+
{
32+
return SerializeHash(*this, SER_GETHASH, PROTOCOL_VERSION);
33+
}
34+
uint256 CCoinJoinQueue::GetHash() const { return SerializeHash(*this, SER_NETWORK, PROTOCOL_VERSION); }
35+
36+
bool CCoinJoinQueue::CheckSignature(const CBLSPublicKey& blsPubKey) const
37+
{
38+
if (!CBLSSignature(Span{vchSig}, false).VerifyInsecure(blsPubKey, GetSignatureHash(), false)) {
39+
LogPrint(BCLog::COINJOIN, "CCoinJoinQueue::CheckSignature -- VerifyInsecure() failed\n");
40+
return false;
41+
}
42+
43+
return true;
44+
}
45+
46+
bool CCoinJoinQueue::IsTimeOutOfBounds(int64_t current_time) const
47+
{
48+
return current_time - nTime > COINJOIN_QUEUE_TIMEOUT ||
49+
nTime - current_time > COINJOIN_QUEUE_TIMEOUT;
50+
}
51+
52+
[[nodiscard]] std::string CCoinJoinQueue::ToString() const
53+
{
54+
return strprintf("nDenom=%d, nTime=%lld, fReady=%s, fTried=%s, masternode=%s",
55+
nDenom, nTime, fReady ? "true" : "false", fTried ? "true" : "false", masternodeOutpoint.ToStringShort());
56+
}
57+

src/coinjoin/common.h

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@
77

88
#include <consensus/amount.h>
99
#include <primitives/transaction.h>
10+
#include <timedata.h>
1011

1112
#include <array>
1213
#include <string>
1314
#include <util/ranges.h>
1415

16+
class CBLSPublicKey;
17+
1518
/** Holds a mixing input
1619
*/
1720
class CTxDSIn : public CTxIn
@@ -129,7 +132,59 @@ constexpr int CalculateAmountPriority(CAmount nInputAmount)
129132
//nondenom return largest first
130133
return -1 * (nInputAmount / COIN);
131134
}
132-
133135
} // namespace CoinJoin
134136

137+
/**
138+
* A currently in progress mixing merge and denomination information
139+
*/
140+
static constexpr int COINJOIN_QUEUE_TIMEOUT = 30;
141+
class CCoinJoinQueue
142+
// TODO: move it inside CoinJoin namespace
143+
{
144+
public:
145+
int nDenom{0};
146+
COutPoint masternodeOutpoint;
147+
uint256 m_protxHash;
148+
int64_t nTime{0};
149+
bool fReady{false}; //ready for submit
150+
std::vector<unsigned char> vchSig;
151+
// memory only
152+
bool fTried{false};
153+
154+
CCoinJoinQueue() = default;
155+
156+
CCoinJoinQueue(int nDenom, const COutPoint& outpoint, const uint256& proTxHash, int64_t nTime, bool fReady) :
157+
nDenom(nDenom),
158+
masternodeOutpoint(outpoint),
159+
m_protxHash(proTxHash),
160+
nTime(nTime),
161+
fReady(fReady)
162+
{
163+
}
164+
165+
SERIALIZE_METHODS(CCoinJoinQueue, obj)
166+
{
167+
READWRITE(obj.nDenom, obj.m_protxHash, obj.nTime, obj.fReady);
168+
if (!(s.GetType() & SER_GETHASH)) {
169+
READWRITE(obj.vchSig);
170+
}
171+
}
172+
173+
[[nodiscard]] uint256 GetHash() const;
174+
[[nodiscard]] uint256 GetSignatureHash() const;
175+
176+
/// Check if we have a valid Masternode address
177+
[[nodiscard]] bool CheckSignature(const CBLSPublicKey& blsPubKey) const;
178+
179+
/// Check if a queue is too old or too far into the future
180+
[[nodiscard]] bool IsTimeOutOfBounds(int64_t current_time = GetAdjustedTime()) const;
181+
182+
[[nodiscard]] std::string ToString() const;
183+
184+
friend bool operator==(const CCoinJoinQueue& a, const CCoinJoinQueue& b)
185+
{
186+
return a.nDenom == b.nDenom && a.masternodeOutpoint == b.masternodeOutpoint && a.nTime == b.nTime && a.fReady == b.fReady;
187+
}
188+
};
189+
135190
#endif // BITCOIN_COINJOIN_COMMON_H

src/coinjoin/server.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ MessageProcessingResult CCoinJoinServer::ProcessDSQUEUE(NodeId from, CDataStream
188188
TRY_LOCK(cs_vecqueue, lockRecv);
189189
if (!lockRecv) return ret;
190190
vecCoinJoinQueue.push_back(dsq);
191-
ret.m_dsq.push_back(dsq);
191+
ret.m_dsq = dsq;
192192
}
193193
return ret;
194194
}

src/msg_result.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
#ifndef BITCOIN_MSG_RESULT_H
66
#define BITCOIN_MSG_RESULT_H
77

8-
#include <coinjoin/coinjoin.h>
9-
8+
#include <coinjoin/common.h>
109
#include <protocol.h>
1110
#include <uint256.h>
1211

@@ -50,7 +49,7 @@ struct MessageProcessingResult
5049
std::vector<CInv> m_inventory;
5150

5251
//! @m_dsq will relay DSQs to connected peers
53-
std::vector<CCoinJoinQueue> m_dsq;
52+
std::optional<CCoinJoinQueue> m_dsq;
5453

5554
//! @m_transactions will relay transactions to peers which is ready to accept it (some peers does not accept transactions)
5655
std::vector<uint256> m_transactions;

src/net_processing.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3567,8 +3567,8 @@ void PeerManagerImpl::PostProcessMessage(MessageProcessingResult&& result, NodeI
35673567
for (const auto& inv : result.m_inventory) {
35683568
RelayInv(inv);
35693569
}
3570-
for (const auto& dsq : result.m_dsq) {
3571-
RelayDSQ(dsq);
3570+
if (result.m_dsq.has_value()) {
3571+
RelayDSQ(result.m_dsq.value());
35723572
}
35733573
}
35743574

test/lint/lint-circular-dependencies.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@
2828
"chainlock/chainlock -> chainlock/signing -> llmq/signing_shares -> net_processing -> llmq/context -> chainlock/chainlock",
2929
"chainlock/chainlock -> chainlock/signing -> llmq/signing_shares -> net_processing -> masternode/active/context -> chainlock/chainlock",
3030
"chainlock/chainlock -> instantsend/instantsend -> instantsend/signing -> chainlock/chainlock",
31-
"chainlock/chainlock -> llmq/quorums -> msg_result -> coinjoin/coinjoin -> chainlock/chainlock",
31+
"chainlock/chainlock -> chainlock/signing -> llmq/signing_shares -> net_processing -> coinjoin/walletman -> coinjoin/client -> coinjoin/coinjoin -> chainlock/chainlock",
3232
"chainlock/chainlock -> validation -> chainlock/chainlock",
3333
"chainlock/chainlock -> validation -> evo/chainhelper -> chainlock/chainlock",
3434
"chainlock/signing -> llmq/signing_shares -> net_processing -> masternode/active/context -> chainlock/signing",
35-
"coinjoin/coinjoin -> instantsend/instantsend -> spork -> msg_result -> coinjoin/coinjoin",
35+
"coinjoin/common -> core_io -> evo/cbtx -> llmq/blockprocessor -> msg_result -> coinjoin/common",
3636
"coinjoin/client -> coinjoin/coinjoin -> instantsend/instantsend -> instantsend/signing -> llmq/signing_shares -> net_processing -> coinjoin/walletman -> coinjoin/client",
3737
"coinjoin/server -> net_processing -> coinjoin/server",
3838
"coinjoin/server -> net_processing -> masternode/active/context -> coinjoin/server",

0 commit comments

Comments
 (0)