forked from dashpay/dash
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathllmq_qgetdata_tests.cpp
More file actions
241 lines (209 loc) · 11.1 KB
/
Copy pathllmq_qgetdata_tests.cpp
File metadata and controls
241 lines (209 loc) · 11.1 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
// Copyright (c) 2026 The Dash Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <arith_uint256.h>
#include <llmq/context.h>
#include <llmq/net_quorum.h>
#include <llmq/options.h>
#include <llmq/quorums.h>
#include <llmq/quorumsman.h>
#include <net.h>
#include <net_processing.h>
#include <node/connection_types.h>
#include <protocol.h>
#include <streams.h>
#include <uint256.h>
#include <validation.h>
#include <version.h>
#include <test/util/net.h>
#include <test/util/setup_common.h>
#include <boost/test/unit_test.hpp>
#include <memory>
#include <span>
using namespace llmq;
namespace {
//! Minimal QuorumRole so NetQuorum accepts QGETDATA as if we were a masternode.
struct MockMasternodeRole final : public QuorumRole {
explicit MockMasternodeRole(CQuorumManager& qman) : QuorumRole(qman) {}
bool IsMasternode() const override { return true; }
bool IsWatching() const override { return false; }
bool SetQuorumSecretKeyShare(CQuorum& /*quorum*/, Span<CBLSSecretKey> /*skContributions*/) const override
{
return false;
}
};
struct QGetDataSetup : public TestingSetup {
MockMasternodeRole m_role;
std::unique_ptr<NetQuorum> m_net_quorum;
QGetDataSetup() :
TestingSetup{CBaseChainParams::REGTEST},
m_role{*m_node.llmq_ctx->qman}
{
BOOST_REQUIRE(m_node.connman);
BOOST_REQUIRE(m_node.peerman);
BOOST_REQUIRE(m_node.dmnman);
BOOST_REQUIRE(m_node.llmq_ctx);
BOOST_REQUIRE(m_node.mn_sync);
BOOST_REQUIRE(m_node.sporkman);
BOOST_REQUIRE(m_node.chainman);
// Mirror init.cpp: TestingSetup does not register NetQuorum, so install
// one with a masternode role so the QGETDATA gate opens for qwatch peers.
m_net_quorum = std::make_unique<NetQuorum>(
m_node.peerman.get(), *m_node.llmq_ctx->bls_worker, *m_node.connman, *m_node.dmnman,
*m_node.llmq_ctx->qman, *m_node.llmq_ctx->qsnapman, *m_node.chainman, *m_node.mn_sync,
*m_node.sporkman, &m_role, /*nodeman=*/nullptr, DEFAULT_WORKER_COUNT, QvvecSyncModeMap{},
/*quorums_recovery=*/false);
}
~QGetDataSetup()
{
m_net_quorum.reset();
}
};
std::unique_ptr<CNode> MakePeer(NodeId id)
{
in_addr peer_in_addr{};
peer_in_addr.s_addr = htonl(0x0a000001 + static_cast<uint32_t>(id));
auto peer{std::make_unique<CNode>(id,
/*sock=*/nullptr,
/*addrIn=*/CAddress{CService{peer_in_addr, 9999}, 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);
peer->fSuccessfullyConnected = true;
// Unauthenticated QWATCH path: any peer can set this flag and then send QGETDATA.
peer->qwatch = true;
return peer;
}
void AssertMisbehaviorScore(PeerManager& peerman, const CNode& peer, int expected)
{
CNodeStateStats stats;
BOOST_REQUIRE(peerman.GetNodeStateStats(peer.GetId(), stats));
BOOST_CHECK_EQUAL(stats.m_misbehavior_score, expected);
}
CDataStream MakeQGetDataStream(Consensus::LLMQType llmq_type, const uint256& quorum_hash, uint16_t data_mask,
const uint256& protx_hash)
{
CDataStream stream{SER_NETWORK, PROTOCOL_VERSION};
stream << static_cast<uint8_t>(llmq_type);
stream << quorum_hash;
stream << data_mask;
stream << protx_hash;
return stream;
}
} // namespace
BOOST_FIXTURE_TEST_SUITE(llmq_qgetdata_tests, QGetDataSetup)
// An unknown block hash can mean the requester is ahead of us or on another fork, so it
// is answered with QUORUM_BLOCK_NOT_FOUND without scoring — but it must never create a
// tracking-map entry, or distinct hashes would grow the map without bound.
BOOST_AUTO_TEST_CASE(qgetdata_unknown_block_not_scored_not_tracked)
{
LOCK(NetEventsInterface::g_msgproc_mutex);
auto peer{MakePeer(/*id=*/2)};
m_node.peerman->InitializeNode(*peer, NODE_NETWORK);
AssertMisbehaviorScore(*m_node.peerman, *peer, 0);
const uint256 protx_hash{uint256S("0x44")};
for (const auto& hash_str : {"0x33", "0x55"}) {
const uint256 quorum_hash{uint256S(hash_str)};
auto stream = MakeQGetDataStream(Consensus::LLMQType::LLMQ_TEST, quorum_hash,
CQuorumDataRequest::QUORUM_VERIFICATION_VECTOR, protx_hash);
m_net_quorum->ProcessMessage(*peer, NetMsgType::QGETDATA, stream);
AssertMisbehaviorScore(*m_node.peerman, *peer, 0);
BOOST_CHECK(!m_node.llmq_ctx->qman->IsDataRequestPending(peer->GetVerifiedProRegTxHash(),
/*we_requested=*/false, quorum_hash,
Consensus::LLMQType::LLMQ_TEST));
}
}
// An active-chain block without a mined commitment is answered with QUORUM_NOT_FOUND and
// the first request is not scored: the commitment is mined after the quorum base block,
// so we can know the base block while still lagging behind the commitment the requester
// saw. Unlike the unknown-block miss, the key is bounded by real chain blocks, so the
// request is registered BEFORE the commitment lookup: the miss is an uncached EvoDB read,
// and repeats within the expiry window must be rate-limited and scored instead of
// re-running it for free.
BOOST_AUTO_TEST_CASE(qgetdata_active_non_quorum_unscored_once_then_rate_limited)
{
LOCK(NetEventsInterface::g_msgproc_mutex);
auto peer{MakePeer(/*id=*/3)};
m_node.peerman->InitializeNode(*peer, NODE_NETWORK);
AssertMisbehaviorScore(*m_node.peerman, *peer, 0);
const uint256 quorum_hash{
WITH_LOCK(::cs_main, return m_node.chainman->ActiveChain().Genesis()->GetBlockHash())};
const uint256 protx_hash{uint256S("0x66")};
auto send_request = [&]() {
auto stream = MakeQGetDataStream(Consensus::LLMQType::LLMQ_TEST, quorum_hash,
CQuorumDataRequest::QUORUM_VERIFICATION_VECTOR, protx_hash);
m_net_quorum->ProcessMessage(*peer, NetMsgType::QGETDATA, stream);
};
// First miss: registered, answered, unscored.
send_request();
AssertMisbehaviorScore(*m_node.peerman, *peer, 0);
BOOST_CHECK(m_node.llmq_ctx->qman->IsDataRequestPending(peer->GetVerifiedProRegTxHash(), /*we_requested=*/false,
quorum_hash, Consensus::LLMQType::LLMQ_TEST));
// Repeats hit the registered entry and score like any other rate-limited request.
for (int score = 25; score <= 75; score += 25) {
send_request();
AssertMisbehaviorScore(*m_node.peerman, *peer, score);
BOOST_CHECK(m_node.llmq_ctx->qman->IsDataRequestPending(peer->GetVerifiedProRegTxHash(),
/*we_requested=*/false, quorum_hash,
Consensus::LLMQType::LLMQ_TEST));
}
}
BOOST_AUTO_TEST_CASE(qgetdata_request_tracking_is_bounded)
{
const uint256 requester{uint256S("0x77")};
SetMockTime(100000);
for (size_t i = 0; i < MAX_INBOUND_DATA_REQUESTS_PER_REQUESTER; ++i) {
const uint256 hash{ArithToUint256(arith_uint256{i + 1})};
const CQuorumDataRequestKey key{requester, /*we_requested=*/false, hash, Consensus::LLMQType::LLMQ_TEST};
const CQuorumDataRequest request{Consensus::LLMQType::LLMQ_TEST, hash,
CQuorumDataRequest::QUORUM_VERIFICATION_VECTOR};
BOOST_CHECK(m_node.llmq_ctx->qman->RegisterDataRequest(key, request, /*add_expiry_bias=*/false) ==
DataRequestRegistration::Accepted);
}
const uint256 over_hash{uint256S("0xdead")};
const CQuorumDataRequestKey over_key{requester, /*we_requested=*/false, over_hash, Consensus::LLMQType::LLMQ_TEST};
const CQuorumDataRequest over_request{Consensus::LLMQType::LLMQ_TEST, over_hash,
CQuorumDataRequest::QUORUM_VERIFICATION_VECTOR};
BOOST_CHECK(m_node.llmq_ctx->qman->RegisterDataRequest(over_key, over_request, /*add_expiry_bias=*/false) ==
DataRequestRegistration::RequesterLimitExceeded);
BOOST_CHECK(!m_node.llmq_ctx->qman->IsDataRequestPending(requester, /*we_requested=*/false, over_hash,
Consensus::LLMQType::LLMQ_TEST));
// Timer-driven cleanup can recover capacity even when no block arrives.
SetMockTime(100000 + 361);
m_node.llmq_ctx->qman->CleanupExpiredDataRequests();
BOOST_CHECK(m_node.llmq_ctx->qman->RegisterDataRequest(over_key, over_request, /*add_expiry_bias=*/false) ==
DataRequestRegistration::Accepted);
SetMockTime(0);
}
BOOST_AUTO_TEST_CASE(qgetdata_global_capacity_does_not_limit_outbound_requests)
{
for (size_t i = 0; i < MAX_INBOUND_DATA_REQUESTS; ++i) {
const uint256 requester{ArithToUint256(arith_uint256{i / MAX_INBOUND_DATA_REQUESTS_PER_REQUESTER + 1})};
const uint256 hash{ArithToUint256(arith_uint256{i + 1})};
const CQuorumDataRequestKey key{requester, /*we_requested=*/false, hash, Consensus::LLMQType::LLMQ_TEST};
const CQuorumDataRequest request{Consensus::LLMQType::LLMQ_TEST, hash,
CQuorumDataRequest::QUORUM_VERIFICATION_VECTOR};
BOOST_REQUIRE(m_node.llmq_ctx->qman->RegisterDataRequest(key, request, /*add_expiry_bias=*/false) ==
DataRequestRegistration::Accepted);
}
const uint256 extra_requester{uint256S("0xbeef")};
const uint256 extra_hash{uint256S("0xfeed")};
const CQuorumDataRequest extra_request{Consensus::LLMQType::LLMQ_TEST, extra_hash,
CQuorumDataRequest::QUORUM_VERIFICATION_VECTOR};
BOOST_CHECK(m_node.llmq_ctx->qman->RegisterDataRequest({extra_requester, /*we_requested=*/false, extra_hash,
Consensus::LLMQType::LLMQ_TEST},
extra_request, /*add_expiry_bias=*/false) ==
DataRequestRegistration::CapacityExhausted);
BOOST_CHECK(!m_node.llmq_ctx->qman->IsDataRequestPending(extra_requester, /*we_requested=*/false, extra_hash,
Consensus::LLMQType::LLMQ_TEST));
BOOST_CHECK(m_node.llmq_ctx->qman->RegisterDataRequest({extra_requester, /*we_requested=*/true, extra_hash,
Consensus::LLMQType::LLMQ_TEST},
extra_request, /*add_expiry_bias=*/false) ==
DataRequestRegistration::Accepted);
}
BOOST_AUTO_TEST_SUITE_END()