Repository navigation
Expand file tree
/
Copy pathcoinjoin_inouts_tests.cpp
More file actions
287 lines (246 loc) · 11.2 KB
/
Copy pathcoinjoin_inouts_tests.cpp
File metadata and controls
287 lines (246 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// Copyright (c) 2025 The Dash Core developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <active/masternode.h>
#include <bls/bls.h>
#include <chain.h>
#include <coinjoin/coinjoin.h>
#include <coinjoin/common.h>
#include <coinjoin/server.h>
#include <evo/chainhelper.h>
#include <llmq/context.h>
#include <masternode/sync.h>
#include <net.h>
#include <node/connection_types.h>
#include <protocol.h>
#include <script/script.h>
#include <streams.h>
#include <test/util/setup_common.h>
#include <uint256.h>
#include <util/check.h>
#include <util/time.h>
#include <boost/test/unit_test.hpp>
#include <algorithm>
#include <array>
#include <cstdint>
#include <memory>
#include <vector>
BOOST_FIXTURE_TEST_SUITE(coinjoin_inouts_tests, TestingSetup)
static CBLSSecretKey MakeSecretKey()
{
CBLSSecretKey sk;
sk.MakeNewKey();
return sk;
}
static CScript P2PKHScript(uint8_t tag = 0x01)
{
// OP_DUP OP_HASH160 <20-byte-tag> OP_EQUALVERIFY OP_CHECKSIG
std::vector<unsigned char> hash(20, tag);
return CScript{} << OP_DUP << OP_HASH160 << hash << OP_EQUALVERIFY << OP_CHECKSIG;
}
BOOST_AUTO_TEST_CASE(broadcasttx_isvalidstructure_good_and_bad)
{
// Good: equal vin/vout sizes, vin count >= min participants, <= max*entry_size, P2PKH outputs with standard denominations
CCoinJoinBroadcastTx good;
{
CMutableTransaction mtx;
// Use min pool participants (e.g. 3). Build 3 inputs and 3 denominated outputs
const int participants = std::max(3, CoinJoin::GetMinPoolParticipants());
for (int i = 0; i < participants; ++i) {
CTxIn in;
in.prevout = COutPoint(uint256::ONE, static_cast<uint32_t>(i));
mtx.vin.push_back(in);
// Pick the smallest denomination
CTxOut out{CoinJoin::GetSmallestDenomination(), P2PKHScript(static_cast<uint8_t>(i))};
mtx.vout.push_back(out);
}
good.tx = MakeTransactionRef(mtx);
good.m_protxHash = uint256::ONE; // at least one of (outpoint, protxhash) must be set
}
BOOST_CHECK(good.IsValidStructure());
// Bad: both identifiers null
CCoinJoinBroadcastTx bad_ids = good;
bad_ids.m_protxHash = uint256{};
bad_ids.masternodeOutpoint.SetNull();
BOOST_CHECK(!bad_ids.IsValidStructure());
// Bad: vin/vout size mismatch
CCoinJoinBroadcastTx bad_sizes = good;
{
CMutableTransaction mtx(*good.tx);
mtx.vout.pop_back();
bad_sizes.tx = MakeTransactionRef(mtx);
}
BOOST_CHECK(!bad_sizes.IsValidStructure());
// Bad: non-P2PKH output
CCoinJoinBroadcastTx bad_script = good;
{
CMutableTransaction mtx(*good.tx);
mtx.vout[0].scriptPubKey = CScript() << OP_RETURN << std::vector<unsigned char>{'x'};
bad_script.tx = MakeTransactionRef(mtx);
}
BOOST_CHECK(!bad_script.IsValidStructure());
// Bad: non-denominated amount
CCoinJoinBroadcastTx bad_amount = good;
{
CMutableTransaction mtx(*good.tx);
mtx.vout[0].nValue = 42; // not a valid denom
bad_amount.tx = MakeTransactionRef(mtx);
}
BOOST_CHECK(!bad_amount.IsValidStructure());
}
// Test-only subclass exposing the minimal seams needed to observe how
// ProcessDSSIGNFINALTX treats messages from participants vs. non-participants
// without standing up a full DKG-backed signing session.
class TestableCoinJoinServer : public CCoinJoinServer
{
public:
using CCoinJoinServer::CCoinJoinServer;
void EnterSigningState() { nState = POOL_STATE_SIGNING; }
void SeedParticipant(const CService& addr) EXCLUSIVE_LOCKS_REQUIRED(!cs_coinjoin)
{
CCoinJoinEntry entry;
entry.addr = addr;
LOCK(cs_coinjoin);
vecEntries.push_back(std::move(entry));
}
};
static std::unique_ptr<CNode> MakePeer(NodeId id, uint32_t ipv4)
{
in_addr peer_in_addr{};
peer_in_addr.s_addr = htonl(ipv4);
auto peer = std::make_unique<CNode>(id,
/*sock=*/nullptr,
/*addrIn=*/CAddress{CService{peer_in_addr, 8333}, NODE_NETWORK},
/*nKeyedNetGroupIn=*/0,
/*nLocalHostNonceIn=*/0,
/*addrBindIn=*/CAddress{},
/*addrNameIn=*/std::string{},
/*conn_type_in=*/ConnectionType::INBOUND,
/*inbound_onion=*/false);
peer->nVersion = PROTOCOL_VERSION;
peer->SetCommonVersion(PROTOCOL_VERSION);
return peer;
}
BOOST_AUTO_TEST_CASE(server_signfinaltx_nonparticipant_cannot_abort_session)
{
BOOST_REQUIRE(m_node.mn_sync);
m_node.mn_sync->SwitchToNextAsset();
BOOST_REQUIRE(m_node.mn_sync->IsBlockchainSynced());
CActiveMasternodeManager mn_activeman(*Assert(m_node.connman), *Assert(m_node.dmnman), MakeSecretKey());
TestableCoinJoinServer server(m_node.peerman.get(), *Assert(m_node.chainman), *Assert(m_node.connman),
*Assert(m_node.dmnman), *Assert(m_node.dstxman), *Assert(m_node.mn_metaman),
*Assert(m_node.mempool), mn_activeman, *Assert(m_node.mn_sync),
*Assert(m_node.llmq_ctx->isman));
// Seed an active signing session with one participant. That participant's
// addr is deliberately not registered with connman -- a session-wide
// RelayStatus(REJECTED) would therefore see nDisconnected == vecEntries.size()
// and reset the pool state to POOL_STATE_IDLE via SetNull().
auto participant = MakePeer(/*id=*/7, /*ipv4=*/0x0a000001);
server.SeedParticipant(participant->addr);
server.EnterSigningState();
BOOST_REQUIRE_EQUAL(server.GetState(), int{POOL_STATE_SIGNING});
auto nonparticipant = MakePeer(/*id=*/42, /*ipv4=*/0x01020304);
BOOST_REQUIRE(!(nonparticipant->addr == participant->addr));
const size_t max_txins{CoinJoin::GetMaxPoolInputOutputCount()};
CDataStream stream{SER_NETWORK, PROTOCOL_VERSION};
WriteCompactSize(stream, max_txins + 1);
BOOST_CHECK_NO_THROW(server.ProcessMessage(*nonparticipant, NetMsgType::DSSIGNFINALTX, stream));
// The non-participant must be rejected without touching session-wide state:
// the stream body must not have been read past the compact-size prefix, and
// the pool must still be in POOL_STATE_SIGNING with its entry intact.
BOOST_CHECK_EQUAL(stream.size(), GetSizeOfCompactSize(max_txins + 1));
BOOST_CHECK_EQUAL(server.GetState(), int{POOL_STATE_SIGNING});
BOOST_CHECK_EQUAL(server.GetEntriesCount(), 1);
}
BOOST_AUTO_TEST_CASE(server_signfinaltx_participant_oversized_count_is_rejected_locally)
{
BOOST_REQUIRE(m_node.mn_sync);
m_node.mn_sync->SwitchToNextAsset();
BOOST_REQUIRE(m_node.mn_sync->IsBlockchainSynced());
CActiveMasternodeManager mn_activeman(*Assert(m_node.connman), *Assert(m_node.dmnman), MakeSecretKey());
TestableCoinJoinServer server(m_node.peerman.get(), *Assert(m_node.chainman), *Assert(m_node.connman),
*Assert(m_node.dmnman), *Assert(m_node.dstxman), *Assert(m_node.mn_metaman),
*Assert(m_node.mempool), mn_activeman, *Assert(m_node.mn_sync),
*Assert(m_node.llmq_ctx->isman));
// Same setup, but this time the oversized DSSIGNFINALTX comes from the
// session participant itself. It must still be rejected without materializing
// the txin vector and without collapsing the session for everyone else.
auto participant = MakePeer(/*id=*/7, /*ipv4=*/0x0a000001);
server.SeedParticipant(participant->addr);
server.EnterSigningState();
const size_t max_txins{CoinJoin::GetMaxPoolInputOutputCount()};
CDataStream stream{SER_NETWORK, PROTOCOL_VERSION};
WriteCompactSize(stream, max_txins + 1);
BOOST_CHECK_NO_THROW(server.ProcessMessage(*participant, NetMsgType::DSSIGNFINALTX, stream));
BOOST_CHECK_EQUAL(stream.size(), 0U);
BOOST_CHECK_EQUAL(server.GetState(), int{POOL_STATE_SIGNING});
BOOST_CHECK_EQUAL(server.GetEntriesCount(), 1);
// A count beyond the generic CompactSize cap is a malformed message, not a
// CoinJoin-level violation: it throws the standard deserialization error out
// to net processing. No txin is materialized and the session is left intact,
// so honest participants are unaffected.
CDataStream huge_stream{SER_NETWORK, PROTOCOL_VERSION};
WriteCompactSize(huge_stream, uint64_t{MAX_SIZE} + 1);
BOOST_CHECK_THROW(server.ProcessMessage(*participant, NetMsgType::DSSIGNFINALTX, huge_stream),
std::ios_base::failure);
BOOST_CHECK_EQUAL(server.GetState(), int{POOL_STATE_SIGNING});
BOOST_CHECK_EQUAL(server.GetEntriesCount(), 1);
}
BOOST_AUTO_TEST_CASE(entry_deserializes_vectors_through_wire_cap)
{
const size_t wire_cap{CoinJoin::GetMaxPoolInputOutputCount()};
BOOST_REQUIRE_GT(wire_cap, COINJOIN_ENTRY_MAX_SIZE);
for (const size_t count : {size_t{0}, COINJOIN_ENTRY_MAX_SIZE, COINJOIN_ENTRY_MAX_SIZE + 1, wire_cap}) {
BOOST_TEST_CONTEXT("count=" << count)
{
CCoinJoinEntry entry;
entry.vecTxDSIn.resize(count);
entry.vecTxOut.resize(count);
CDataStream stream{SER_NETWORK, PROTOCOL_VERSION};
stream << entry;
CCoinJoinEntry roundtripped;
BOOST_CHECK_NO_THROW(stream >> roundtripped);
BOOST_CHECK_EQUAL(roundtripped.vecTxDSIn.size(), count);
BOOST_CHECK_EQUAL(roundtripped.vecTxOut.size(), count);
}
}
}
BOOST_AUTO_TEST_CASE(entry_rejects_inputs_above_wire_cap_before_materializing)
{
const size_t wire_cap{CoinJoin::GetMaxPoolInputOutputCount()};
CDataStream stream{SER_NETWORK, PROTOCOL_VERSION};
WriteCompactSize(stream, wire_cap + 1);
CCoinJoinEntry entry;
BOOST_CHECK_THROW(stream >> entry, std::ios_base::failure);
BOOST_CHECK(entry.vecTxDSIn.empty());
}
BOOST_AUTO_TEST_CASE(entry_rejects_outputs_above_wire_cap_before_materializing)
{
const size_t wire_cap{CoinJoin::GetMaxPoolInputOutputCount()};
CDataStream stream{SER_NETWORK, PROTOCOL_VERSION};
WriteCompactSize(stream, 0);
stream << MakeTransactionRef(CMutableTransaction{});
WriteCompactSize(stream, wire_cap + 1);
CCoinJoinEntry entry;
BOOST_CHECK_THROW(stream >> entry, std::ios_base::failure);
BOOST_CHECK(entry.vecTxOut.empty());
}
BOOST_AUTO_TEST_CASE(queue_timeout_bounds)
{
CCoinJoinQueue dsq;
dsq.nDenom = CoinJoin::AmountToDenomination(CoinJoin::GetSmallestDenomination());
dsq.m_protxHash = uint256::ONE;
dsq.nTime = GetAdjustedTime();
// current time -> not out of bounds
BOOST_CHECK(!dsq.IsTimeOutOfBounds());
// Too old (beyond COINJOIN_QUEUE_TIMEOUT)
SetMockTime(GetTime() + (COINJOIN_QUEUE_TIMEOUT + 1));
BOOST_CHECK(dsq.IsTimeOutOfBounds());
// Too far in the future
SetMockTime(GetTime() - 2 * (COINJOIN_QUEUE_TIMEOUT + 1)); // move back to anchor baseline
dsq.nTime = GetAdjustedTime() + (COINJOIN_QUEUE_TIMEOUT + 1);
BOOST_CHECK(dsq.IsTimeOutOfBounds());
// Reset mock time
SetMockTime(0);
}
BOOST_AUTO_TEST_SUITE_END()