forked from bitcoin/bitcoin
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathcoinjoin_dstxmanager_tests.cpp
More file actions
192 lines (165 loc) · 6.4 KB
/
Copy pathcoinjoin_dstxmanager_tests.cpp
File metadata and controls
192 lines (165 loc) · 6.4 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
// 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 <test/util/setup_common.h>
#include <chain.h>
#include <coinjoin/coinjoin.h>
#include <coinjoin/common.h>
#include <script/script.h>
#include <uint256.h>
#include <algorithm>
#include <array>
#include <vector>
#include <boost/test/unit_test.hpp>
BOOST_FIXTURE_TEST_SUITE(coinjoin_dstxmanager_tests, TestingSetup)
static CCoinJoinBroadcastTx MakeDSTX(int vin_vout_count = 3)
{
CCoinJoinBroadcastTx dstx;
CMutableTransaction mtx;
const int count = std::max(vin_vout_count, 1);
for (int i = 0; i < count; ++i) {
mtx.vin.emplace_back(COutPoint(uint256S("0a"), i));
// Use denominated P2PKH outputs
CScript spk;
spk << OP_DUP << OP_HASH160 << std::vector<unsigned char>(20, i) << OP_EQUALVERIFY << OP_CHECKSIG;
mtx.vout.emplace_back(CoinJoin::GetSmallestDenomination(), spk);
}
dstx.tx = MakeTransactionRef(mtx);
dstx.m_protxHash = uint256::ONE;
return dstx;
}
BOOST_AUTO_TEST_CASE(add_get_dstx)
{
CCoinJoinBroadcastTx dstx = MakeDSTX();
auto& man = *Assert(m_node.dstxman);
// Not present initially
BOOST_CHECK(!man.GetDSTX(dstx.tx->GetHash()));
// Add
man.AddDSTX(dstx);
// Fetch back
auto got = man.GetDSTX(dstx.tx->GetHash());
BOOST_CHECK(static_cast<bool>(got));
BOOST_CHECK_EQUAL(got.tx->GetHash().ToString(), dstx.tx->GetHash().ToString());
}
BOOST_AUTO_TEST_CASE(broadcasttx_expiry_height_logic)
{
// Build a valid-looking CCoinJoinBroadcastTx with confirmed height
CCoinJoinBroadcastTx dstx;
{
CMutableTransaction mtx;
const int participants = std::max(3, CoinJoin::GetMinPoolParticipants());
for (int i = 0; i < participants; ++i) {
mtx.vin.emplace_back(COutPoint(uint256::TWO, i));
CScript spk;
spk << OP_DUP << OP_HASH160 << std::vector<unsigned char>(20, i) << OP_EQUALVERIFY << OP_CHECKSIG;
mtx.vout.emplace_back(CoinJoin::GetSmallestDenomination(), spk);
}
dstx.tx = MakeTransactionRef(mtx);
dstx.m_protxHash = uint256::ONE;
// mark as confirmed at height 100
dstx.SetConfirmedHeight(100);
}
int expiry_height = 124; // 125 - 100 == 25 > 24 → expired by height
// Minimal CBlockIndex with required fields
// Create a minimal block index to satisfy the interface
CBlockIndex index;
uint256 blk_hash = uint256S("03");
index.nHeight = expiry_height;
index.phashBlock = &blk_hash;
auto& man = *Assert(m_node.dstxman);
auto& hash = dstx.tx->GetHash();
man.AddDSTX(dstx);
BOOST_CHECK(static_cast<bool>(man.GetDSTX(hash)));
man.UpdatedBlockTip(&index);
BOOST_CHECK(static_cast<bool>(man.GetDSTX(hash)));
index.nHeight = expiry_height + 1;
man.UpdatedBlockTip(&index);
BOOST_CHECK(!static_cast<bool>(man.GetDSTX(hash)));
}
BOOST_AUTO_TEST_CASE(update_heights_block_connect_disconnect)
{
CCoinJoinBroadcastTx dstx = MakeDSTX();
auto& man = *Assert(m_node.dstxman);
man.AddDSTX(dstx);
// Create a fake block containing the tx
auto block = std::make_shared<CBlock>();
block->vtx.push_back(dstx.tx);
CBlockIndex index;
index.nHeight = 100;
uint256 bh = uint256S("0b");
index.phashBlock = &bh;
// Height should set to 100 on connect
man.BlockConnected(block, &index);
{
auto got = man.GetDSTX(dstx.tx->GetHash());
BOOST_CHECK(static_cast<bool>(got));
BOOST_CHECK(got.GetConfirmedHeight().has_value());
BOOST_CHECK_EQUAL(*got.GetConfirmedHeight(), 100);
}
// Height should clear on disconnect
man.BlockDisconnected(block, nullptr);
{
auto got = man.GetDSTX(dstx.tx->GetHash());
BOOST_CHECK(static_cast<bool>(got));
BOOST_CHECK(!got.GetConfirmedHeight().has_value());
}
}
BOOST_AUTO_TEST_CASE(mempool_reentry_clears_confirmed_height)
{
// After a block connect sets the confirmed height, the same tx being
// (re)added to the mempool, e.g. via a reorg, should clear it. This
// drives DSTX expiry logic that watches GetConfirmedHeight().
CCoinJoinBroadcastTx dstx = MakeDSTX();
auto& man = *Assert(m_node.dstxman);
man.AddDSTX(dstx);
auto block = std::make_shared<CBlock>();
block->vtx.push_back(dstx.tx);
CBlockIndex index;
index.nHeight = 200;
uint256 bh = uint256S("0c");
index.phashBlock = &bh;
man.BlockConnected(block, &index);
{
auto got = man.GetDSTX(dstx.tx->GetHash());
BOOST_REQUIRE(static_cast<bool>(got));
BOOST_REQUIRE(got.GetConfirmedHeight().has_value());
BOOST_CHECK_EQUAL(*got.GetConfirmedHeight(), 200);
}
// Re-entry to the mempool clears the confirmed height (without removing the entry).
man.TransactionAddedToMempool(dstx.tx);
{
auto got = man.GetDSTX(dstx.tx->GetHash());
BOOST_REQUIRE(static_cast<bool>(got));
BOOST_CHECK(!got.GetConfirmedHeight().has_value());
}
}
BOOST_AUTO_TEST_CASE(mempool_event_unrelated_tx_does_not_mutate_dstx)
{
// TransactionAddedToMempool for a transaction that is not a tracked DSTX
// must be a no-op for any stored DSTX state.
CCoinJoinBroadcastTx dstx = MakeDSTX();
auto& man = *Assert(m_node.dstxman);
man.AddDSTX(dstx);
// Establish a confirmed height to detect any unwanted mutation.
auto block = std::make_shared<CBlock>();
block->vtx.push_back(dstx.tx);
CBlockIndex index;
index.nHeight = 321;
uint256 bh = uint256S("0d");
index.phashBlock = &bh;
man.BlockConnected(block, &index);
// Build an unrelated transaction by mutating one input of the tracked tx so it hashes differently.
CMutableTransaction other_mtx(*dstx.tx);
other_mtx.vin[0].prevout = COutPoint(uint256S("ee"), 7);
auto other_tx = MakeTransactionRef(other_mtx);
BOOST_REQUIRE(other_tx->GetHash() != dstx.tx->GetHash());
man.TransactionAddedToMempool(other_tx);
// Stored DSTX still has its confirmed height.
auto got = man.GetDSTX(dstx.tx->GetHash());
BOOST_REQUIRE(static_cast<bool>(got));
BOOST_REQUIRE(got.GetConfirmedHeight().has_value());
BOOST_CHECK_EQUAL(*got.GetConfirmedHeight(), 321);
// Unrelated tx was not inserted as a DSTX.
BOOST_CHECK(!static_cast<bool>(man.GetDSTX(other_tx->GetHash())));
}
BOOST_AUTO_TEST_SUITE_END()