Skip to content

Commit b1c28ce

Browse files
refactor: move CCoinJoinQueue implementation to separate files
The implementation of CCoinJoinQueue has been moved from coinjoin.cpp and coinjoin.h to new files queue.cpp and queue.h. Additionally, a new params.h file has been created to define CoinJoin-related timeout constants. This restructuring improves code organization and maintainability.
1 parent eb666d9 commit b1c28ce

File tree

10 files changed

+164
-99
lines changed

10 files changed

+164
-99
lines changed

src/Makefile.am

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,8 @@ BITCOIN_CORE_H = \
177177
checkqueue.h \
178178
clientversion.h \
179179
coinjoin/coinjoin.h \
180+
coinjoin/queue.h \
181+
coinjoin/params.h \
180182
coinjoin/client.h \
181183
coinjoin/common.h \
182184
coinjoin/options.h \
@@ -485,6 +487,7 @@ libbitcoin_node_a_SOURCES = \
485487
chainlock/clsig.cpp \
486488
chainlock/signing.cpp \
487489
coinjoin/coinjoin.cpp \
490+
coinjoin/queue.cpp \
488491
coinjoin/server.cpp \
489492
coinjoin/walletman.cpp \
490493
consensus/tx_verify.cpp \

src/coinjoin/coinjoin.cpp

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

55
#include <coinjoin/coinjoin.h>
6+
#include <coinjoin/queue.h>
67

78
#include <chain.h>
89
#include <chainparams.h>
@@ -39,45 +40,7 @@ bool CCoinJoinEntry::AddScriptSig(const CTxIn& txin)
3940
return false;
4041
}
4142

42-
uint256 CCoinJoinQueue::GetSignatureHash() const
43-
{
44-
return SerializeHash(*this, SER_GETHASH, PROTOCOL_VERSION);
45-
}
46-
uint256 CCoinJoinQueue::GetHash() const { return SerializeHash(*this, SER_NETWORK, PROTOCOL_VERSION); }
47-
48-
bool CCoinJoinQueue::Sign(const CActiveMasternodeManager& mn_activeman)
49-
{
50-
uint256 hash = GetSignatureHash();
51-
CBLSSignature sig = mn_activeman.Sign(hash, /*is_legacy=*/ false);
52-
if (!sig.IsValid()) {
53-
return false;
54-
}
55-
vchSig = sig.ToByteVector(false);
56-
57-
return true;
58-
}
59-
60-
bool CCoinJoinQueue::CheckSignature(const CBLSPublicKey& blsPubKey) const
61-
{
62-
if (!CBLSSignature(Span{vchSig}, false).VerifyInsecure(blsPubKey, GetSignatureHash(), false)) {
63-
LogPrint(BCLog::COINJOIN, "CCoinJoinQueue::CheckSignature -- VerifyInsecure() failed\n");
64-
return false;
65-
}
66-
67-
return true;
68-
}
69-
70-
bool CCoinJoinQueue::IsTimeOutOfBounds(int64_t current_time) const
71-
{
72-
return current_time - nTime > COINJOIN_QUEUE_TIMEOUT ||
73-
nTime - current_time > COINJOIN_QUEUE_TIMEOUT;
74-
}
75-
76-
[[nodiscard]] std::string CCoinJoinQueue::ToString() const
77-
{
78-
return strprintf("nDenom=%d, nTime=%lld, fReady=%s, fTried=%s, masternode=%s",
79-
nDenom, nTime, fReady ? "true" : "false", fTried ? "true" : "false", masternodeOutpoint.ToStringShort());
80-
}
43+
// CCoinJoinQueue implementation moved to coinjoin/queue.cpp
8144

8245
uint256 CCoinJoinBroadcastTx::GetSignatureHash() const
8346
{

src/coinjoin/coinjoin.h

Lines changed: 3 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#define BITCOIN_COINJOIN_COINJOIN_H
77

88
#include <coinjoin/common.h>
9+
#include <coinjoin/queue.h>
10+
#include <coinjoin/params.h>
911

1012
#include <core_io.h>
1113
#include <netaddress.h>
@@ -41,8 +43,6 @@ extern RecursiveMutex cs_main;
4143
// timeouts
4244
static constexpr int COINJOIN_AUTO_TIMEOUT_MIN = 5;
4345
static constexpr int COINJOIN_AUTO_TIMEOUT_MAX = 15;
44-
static constexpr int COINJOIN_QUEUE_TIMEOUT = 30;
45-
static constexpr int COINJOIN_SIGNING_TIMEOUT = 15;
4646

4747
static constexpr size_t COINJOIN_ENTRY_MAX_SIZE = 9;
4848

@@ -173,63 +173,7 @@ class CCoinJoinEntry
173173
};
174174

175175

176-
/**
177-
* A currently in progress mixing merge and denomination information
178-
*/
179-
class CCoinJoinQueue
180-
{
181-
public:
182-
int nDenom{0};
183-
COutPoint masternodeOutpoint;
184-
uint256 m_protxHash;
185-
int64_t nTime{0};
186-
bool fReady{false}; //ready for submit
187-
std::vector<unsigned char> vchSig;
188-
// memory only
189-
bool fTried{false};
190-
191-
CCoinJoinQueue() = default;
192-
193-
CCoinJoinQueue(int nDenom, const COutPoint& outpoint, const uint256& proTxHash, int64_t nTime, bool fReady) :
194-
nDenom(nDenom),
195-
masternodeOutpoint(outpoint),
196-
m_protxHash(proTxHash),
197-
nTime(nTime),
198-
fReady(fReady)
199-
{
200-
}
201-
202-
SERIALIZE_METHODS(CCoinJoinQueue, obj)
203-
{
204-
READWRITE(obj.nDenom, obj.m_protxHash, obj.nTime, obj.fReady);
205-
if (!(s.GetType() & SER_GETHASH)) {
206-
READWRITE(obj.vchSig);
207-
}
208-
}
209-
210-
[[nodiscard]] uint256 GetHash() const;
211-
[[nodiscard]] uint256 GetSignatureHash() const;
212-
/** Sign this mixing transaction
213-
* return true if all conditions are met:
214-
* 1) we have an active Masternode,
215-
* 2) we have a valid Masternode private key,
216-
* 3) we signed the message successfully, and
217-
* 4) we verified the message successfully
218-
*/
219-
bool Sign(const CActiveMasternodeManager& mn_activeman);
220-
/// Check if we have a valid Masternode address
221-
[[nodiscard]] bool CheckSignature(const CBLSPublicKey& blsPubKey) const;
222-
223-
/// Check if a queue is too old or too far into the future
224-
[[nodiscard]] bool IsTimeOutOfBounds(int64_t current_time = GetAdjustedTime()) const;
225-
226-
[[nodiscard]] std::string ToString() const;
227-
228-
friend bool operator==(const CCoinJoinQueue& a, const CCoinJoinQueue& b)
229-
{
230-
return a.nDenom == b.nDenom && a.masternodeOutpoint == b.masternodeOutpoint && a.nTime == b.nTime && a.fReady == b.fReady;
231-
}
232-
};
176+
// CCoinJoinQueue moved to coinjoin/queue.h
233177

234178
/** Helper class to store mixing transaction (tx) information.
235179
*/

src/coinjoin/params.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright (c) 2014-2025 The Dash Core developers
2+
// Distributed under the MIT/X11 software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#ifndef BITCOIN_COINJOIN_PARAMS_H
6+
#define BITCOIN_COINJOIN_PARAMS_H
7+
8+
// Timeouts used by CoinJoin queue and signing, kept minimal to avoid heavy deps
9+
static constexpr int COINJOIN_QUEUE_TIMEOUT = 30;
10+
static constexpr int COINJOIN_SIGNING_TIMEOUT = 15;
11+
12+
#endif // BITCOIN_COINJOIN_PARAMS_H
13+
14+

src/coinjoin/queue.cpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright (c) 2014-2025 The Dash Core developers
2+
// Distributed under the MIT/X11 software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#include <coinjoin/queue.h>
6+
7+
#include <bls/bls.h>
8+
#include <logging.h>
9+
#include <masternode/node.h>
10+
#include <span.h>
11+
#include <tinyformat.h>
12+
#include <timedata.h>
13+
14+
#include <version.h>
15+
16+
uint256 CCoinJoinQueue::GetSignatureHash() const
17+
{
18+
return SerializeHash(*this, SER_GETHASH, PROTOCOL_VERSION);
19+
}
20+
21+
uint256 CCoinJoinQueue::GetHash() const
22+
{
23+
return SerializeHash(*this, SER_NETWORK, PROTOCOL_VERSION);
24+
}
25+
26+
bool CCoinJoinQueue::Sign(const CActiveMasternodeManager& mn_activeman)
27+
{
28+
uint256 hash = GetSignatureHash();
29+
CBLSSignature sig = mn_activeman.Sign(hash, /*is_legacy=*/ false);
30+
if (!sig.IsValid()) {
31+
return false;
32+
}
33+
vchSig = sig.ToByteVector(false);
34+
return true;
35+
}
36+
37+
bool CCoinJoinQueue::CheckSignature(const CBLSPublicKey& blsPubKey) const
38+
{
39+
if (!CBLSSignature(Span{vchSig}, false).VerifyInsecure(blsPubKey, GetSignatureHash(), false)) {
40+
LogPrint(BCLog::COINJOIN, "CCoinJoinQueue::CheckSignature -- VerifyInsecure() failed\n");
41+
return false;
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+
bool CCoinJoinQueue::IsTimeOutOfBounds() const
53+
{
54+
return IsTimeOutOfBounds(GetAdjustedTime());
55+
}
56+
57+
std::string CCoinJoinQueue::ToString() const
58+
{
59+
return strprintf("nDenom=%d, nTime=%lld, fReady=%s, fTried=%s, masternode=%s",
60+
nDenom, nTime, fReady ? "true" : "false", fTried ? "true" : "false", masternodeOutpoint.ToStringShort());
61+
}
62+
63+

src/coinjoin/queue.h

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Copyright (c) 2014-2025 The Dash Core developers
2+
// Distributed under the MIT/X11 software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#ifndef BITCOIN_COINJOIN_QUEUE_H
6+
#define BITCOIN_COINJOIN_QUEUE_H
7+
8+
#include <coinjoin/params.h>
9+
10+
#include <netaddress.h>
11+
#include <primitives/transaction.h>
12+
#include <serialize.h>
13+
#include <uint256.h>
14+
#include <util/time.h>
15+
16+
#include <string>
17+
#include <vector>
18+
19+
class CActiveMasternodeManager;
20+
class CBLSPublicKey;
21+
22+
/**
23+
* A currently in progress mixing merge and denomination information
24+
*/
25+
class CCoinJoinQueue
26+
{
27+
public:
28+
int nDenom{0};
29+
COutPoint masternodeOutpoint;
30+
uint256 m_protxHash;
31+
int64_t nTime{0};
32+
bool fReady{false}; // ready for submit
33+
std::vector<unsigned char> vchSig;
34+
// memory only
35+
bool fTried{false};
36+
37+
CCoinJoinQueue() = default;
38+
39+
CCoinJoinQueue(int nDenom, const COutPoint& outpoint, const uint256& proTxHash, int64_t nTime, bool fReady) :
40+
nDenom(nDenom),
41+
masternodeOutpoint(outpoint),
42+
m_protxHash(proTxHash),
43+
nTime(nTime),
44+
fReady(fReady)
45+
{
46+
}
47+
48+
SERIALIZE_METHODS(CCoinJoinQueue, obj)
49+
{
50+
READWRITE(obj.nDenom, obj.m_protxHash, obj.nTime, obj.fReady);
51+
if (!(s.GetType() & SER_GETHASH)) {
52+
READWRITE(obj.vchSig);
53+
}
54+
}
55+
56+
[[nodiscard]] uint256 GetHash() const;
57+
[[nodiscard]] uint256 GetSignatureHash() const;
58+
bool Sign(const CActiveMasternodeManager& mn_activeman);
59+
[[nodiscard]] bool CheckSignature(const CBLSPublicKey& blsPubKey) const;
60+
61+
/// Check if a queue is too old or too far into the future
62+
[[nodiscard]] bool IsTimeOutOfBounds(int64_t current_time) const;
63+
[[nodiscard]] bool IsTimeOutOfBounds() const;
64+
65+
[[nodiscard]] std::string ToString() const;
66+
67+
friend bool operator==(const CCoinJoinQueue& a, const CCoinJoinQueue& b)
68+
{
69+
return a.nDenom == b.nDenom && a.masternodeOutpoint == b.masternodeOutpoint && a.nTime == b.nTime && a.fReady == b.fReady;
70+
}
71+
};
72+
73+
#endif // BITCOIN_COINJOIN_QUEUE_H
74+
75+

src/msg_processing.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#ifndef BITCOIN_MSG_PROCESSING_H
66
#define BITCOIN_MSG_PROCESSING_H
77

8-
#include <coinjoin/coinjoin.h>
8+
#include <coinjoin/queue.h>
99

1010
#include <primitives/transaction.h>
1111
#include <protocol.h>

src/test/coinjoin_basemanager_tests.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <test/util/setup_common.h>
66

77
#include <coinjoin/coinjoin.h>
8+
#include <coinjoin/queue.h>
89
#include <cstddef>
910
#include <uint256.h>
1011

src/test/coinjoin_inouts_tests.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <chain.h>
1313
#include <chainlock/chainlock.h>
1414
#include <coinjoin/coinjoin.h>
15+
#include <coinjoin/queue.h>
1516
#include <coinjoin/common.h>
1617
#include <llmq/context.h>
1718
#include <script/script.h>

src/test/coinjoin_queue_tests.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <test/util/setup_common.h>
66

77
#include <bls/bls.h>
8+
#include <coinjoin/queue.h>
89
#include <coinjoin/coinjoin.h>
910
#include <masternode/node.h>
1011
#include <uint256.h>

0 commit comments

Comments
 (0)