forked from bitcoin/bitcoin
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathgovernance_inv_tests.cpp
More file actions
474 lines (388 loc) · 22.5 KB
/
Copy pathgovernance_inv_tests.cpp
File metadata and controls
474 lines (388 loc) · 22.5 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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
// 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 <common/bloom.h>
#include <evo/chainhelper.h>
#include <governance/governance.h>
#include <governance/net_governance.h>
#include <governance/object.h>
#include <masternode/sync.h>
#include <net.h>
#include <net_processing.h>
#include <netfulfilledman.h>
#include <node/connection_types.h>
#include <protocol.h>
#include <streams.h>
#include <uint256.h>
#include <util/strencodings.h>
#include <util/time.h>
#include <version.h>
#include <test/util/net.h>
#include <test/util/setup_common.h>
#include <test/util/validation.h>
#include <boost/test/unit_test.hpp>
#include <atomic>
#include <chrono>
#include <memory>
#include <vector>
using namespace std::chrono_literals;
namespace {
struct GovernanceInvSetup : public TestingSetup {
GovernanceInvSetup() : TestingSetup{CBaseChainParams::MAIN}
{
// ConfirmInventoryRequest and the NetGovernance object/vote handlers
// short-circuit on !IsBlockchainSynced().
BOOST_REQUIRE(m_node.mn_sync);
m_node.mn_sync->SwitchToNextAsset();
BOOST_REQUIRE(m_node.mn_sync->IsBlockchainSynced());
BOOST_REQUIRE(m_node.mn_metaman);
// Note: mn_metaman is left unloaded. No test here reaches
// CGovernanceObject::ProcessVote, which asserts metaman.IsValid() -- a vote whose
// parent object exists would, and would need it loaded first.
m_node.govman = std::make_unique<CGovernanceManager>(*m_node.mn_metaman, *m_node.chainman, *m_node.chain_helper->superblocks, *m_node.dmnman, *m_node.mn_sync);
// Match runtime preconditions: NetGovernance::AlreadyHave claims we
// already have the inv when governance isn't loaded (e.g.
// -disablegovernance), so ConfirmInventoryRequest would never run.
BOOST_REQUIRE(m_node.govman->LoadCache(/*load_cache=*/false));
BOOST_REQUIRE(m_node.netfulfilledman);
// Loaded here for the later test that advances GOVERNANCE -> FINISHED;
// the sync notifier asserts netfulfilledman.IsValid().
BOOST_REQUIRE(m_node.netfulfilledman->LoadCache(/*load_cache=*/false));
BOOST_REQUIRE(m_node.connman);
BOOST_REQUIRE(m_node.peerman);
// Intentional unit-test boundary: TestingSetup does not run the
// init.cpp/AppInit startup path that registers the Dash-specific
// handlers, so the INV branch in PeerManagerImpl::AlreadyHave would not
// route MSG_GOVERNANCE_OBJECT[_VOTE] anywhere. Install the same
// NetGovernance handler init.cpp registers so a real INV reaches
// CGovernanceManager::ConfirmInventoryRequest; the startup registration
// itself stays outside this unit test.
m_node.peerman->AddExtraHandler(std::make_unique<NetGovernance>(
m_node.peerman.get(), *m_node.govman, *m_node.mn_sync,
*m_node.netfulfilledman, *m_node.connman));
// Anchor the clock so the object and vote timestamps the tests build
// from GetTime() are deterministic. Nothing advances it.
SetMockTime(1'700'000'000s);
}
~GovernanceInvSetup() {
// govman holds a reference to chain_helper->superblocks, so it must be reset before chain_helper (matches PrepareShutdown
// ordering in init.cpp).
m_node.peerman->RemoveHandlers();
m_node.govman.reset();
}
};
size_t CountQueuedMessages(const CNode& peer, const std::string& msg_type)
{
LOCK(peer.cs_vSend);
size_t count{0};
for (const auto& msg : peer.vSendMsg) {
if (msg.m_type == msg_type) {
++count;
}
}
return count;
}
size_t CountQueuedInventory(const CNode& peer, const CInv& expected_inv)
{
LOCK(peer.cs_vSend);
size_t count{0};
for (const auto& msg : peer.vSendMsg) {
if (msg.m_type != NetMsgType::INV) {
continue;
}
CDataStream stream{msg.data, SER_NETWORK, PROTOCOL_VERSION};
std::vector<CInv> invs;
stream >> invs;
for (const auto& inv : invs) {
if (inv.type == expected_inv.type && inv.hash == expected_inv.hash) {
++count;
}
}
}
return count;
}
std::unique_ptr<CNode> MakeGovernanceInvPeer(NodeId id)
{
in_addr peer_in_addr{};
peer_in_addr.s_addr = htonl(0x01020305 + id);
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);
peer->fSuccessfullyConnected = true;
return peer;
}
void ProcessInv(PeerManager& peerman, CNode& peer, const CInv& inv)
EXCLUSIVE_LOCKS_REQUIRED(NetEventsInterface::g_msgproc_mutex)
{
CDataStream inv_stream{SER_NETWORK, PROTOCOL_VERSION};
inv_stream << std::vector<CInv>{inv};
std::atomic<bool> interrupt_dummy{false};
peerman.ProcessMessage(peer, NetMsgType::INV, inv_stream, GetTime<std::chrono::microseconds>(), interrupt_dummy);
}
CGovernanceObject MakeGovernanceObject(int64_t creation_time, const uint256& collateral_hash)
{
const std::string data{
R"({"type":1,"name":"proposal","start_epoch":1700000000,"end_epoch":1700100000,"payment_amount":1.0,"payment_address":"XwnLY9Tf7Zsef8gMGL2fhWA9ZmMjt4KPwV","url":"https://dash.org"})"};
return CGovernanceObject{uint256{}, /*revision=*/1, creation_time, collateral_hash, HexStr(data)};
}
void ProcessGovernanceObject(NetGovernance& net_gov, CNode& peer, const CGovernanceObject& govobj)
{
CDataStream object_stream{SER_NETWORK, PROTOCOL_VERSION};
object_stream << govobj;
net_gov.ProcessMessage(peer, NetMsgType::MNGOVERNANCEOBJECT, object_stream);
}
CGovernanceVote MakeGovernanceVote(const uint256& parent_hash)
{
CGovernanceVote vote{COutPoint{uint256S("11"), 1}, parent_hash, VOTE_SIGNAL_FUNDING, VOTE_OUTCOME_YES};
vote.SetTime(GetTime<std::chrono::seconds>().count());
vote.SetSignature(std::vector<unsigned char>(CGovernanceVote::COMPACT_SIG_SIZE));
return vote;
}
void ProcessGovernanceVote(NetGovernance& net_gov, CNode& peer, const CGovernanceVote& vote)
{
CDataStream vote_stream{SER_NETWORK, PROTOCOL_VERSION};
vote_stream << vote;
net_gov.ProcessMessage(peer, NetMsgType::MNGOVERNANCEOBJECTVOTE, vote_stream);
}
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);
}
} // namespace
BOOST_FIXTURE_TEST_SUITE(governance_inv_tests, GovernanceInvSetup)
BOOST_AUTO_TEST_CASE(per_object_vote_sync_is_fulfilled_request_limited)
{
LOCK(NetEventsInterface::g_msgproc_mutex);
// NetGovernance::ProcessMessage ignores MNGOVERNANCESYNC until sync is
// fully finished; advance from GOVERNANCE to FINISHED.
m_node.mn_sync->SwitchToNextAsset();
BOOST_REQUIRE(m_node.mn_sync->IsSynced());
auto peer{MakeGovernanceInvPeer(/*id=*/1)};
m_node.peerman->InitializeNode(*peer, NODE_NETWORK);
auto make_request_stream = [](const uint256& object_hash, const CBloomFilter& filter) {
CDataStream stream{SER_NETWORK, PROTOCOL_VERSION};
stream << object_hash << filter;
return stream;
};
NetGovernance net_gov(m_node.peerman.get(), *m_node.govman, *m_node.mn_sync,
*m_node.netfulfilledman, *m_node.connman);
auto& connman = static_cast<ConnmanTestMsg&>(*m_node.connman);
AssertMisbehaviorScore(*m_node.peerman, *peer, 0);
const CBloomFilter vote_filter{1, 0.001, 0, BLOOM_UPDATE_NONE};
CGovernanceObject postponed_object{uint256(), /*revision=*/1, GetTime(), uint256::ONE, /*data=*/{}};
m_node.govman->AddPostponedObject(postponed_object);
const uint256 postponed_object_hash{postponed_object.GetHash()};
const std::string postponed_vote_sync_request{strprintf("%s-votes-%s", NetMsgType::MNGOVERNANCESYNC,
postponed_object_hash.ToString())};
auto postponed_stream = make_request_stream(postponed_object_hash, vote_filter);
net_gov.ProcessMessage(*peer, NetMsgType::MNGOVERNANCESYNC, postponed_stream);
BOOST_CHECK(!m_node.netfulfilledman->HasFulfilledRequest(peer->addr, postponed_vote_sync_request));
AssertMisbehaviorScore(*m_node.peerman, *peer, 0);
auto duplicate_postponed_stream = make_request_stream(postponed_object_hash, vote_filter);
net_gov.ProcessMessage(*peer, NetMsgType::MNGOVERNANCESYNC, duplicate_postponed_stream);
AssertMisbehaviorScore(*m_node.peerman, *peer, 0);
CGovernanceObject syncable_object{uint256(), /*revision=*/1, GetTime() + 1, uint256S("02"), /*data=*/{}};
m_node.govman->AddGovernanceObjectForTesting(syncable_object);
const uint256 syncable_object_hash{syncable_object.GetHash()};
const std::string syncable_vote_sync_request{strprintf("%s-votes-%s", NetMsgType::MNGOVERNANCESYNC,
syncable_object_hash.ToString())};
BOOST_CHECK(!m_node.netfulfilledman->HasFulfilledRequest(peer->addr, syncable_vote_sync_request));
connman.FlushSendBuffer(*peer);
auto syncable_object_fetch_stream = make_request_stream(syncable_object_hash, CBloomFilter{});
net_gov.ProcessMessage(*peer, NetMsgType::MNGOVERNANCESYNC, syncable_object_fetch_stream);
BOOST_CHECK_EQUAL(CountQueuedInventory(*peer, CInv{MSG_GOVERNANCE_OBJECT, syncable_object_hash}), 1U);
BOOST_CHECK(!m_node.netfulfilledman->HasFulfilledRequest(peer->addr, syncable_vote_sync_request));
BOOST_CHECK_EQUAL(CountQueuedMessages(*peer, NetMsgType::SYNCSTATUSCOUNT), 0U);
AssertMisbehaviorScore(*m_node.peerman, *peer, 0);
connman.FlushSendBuffer(*peer);
auto duplicate_syncable_object_fetch_stream = make_request_stream(syncable_object_hash, CBloomFilter{});
net_gov.ProcessMessage(*peer, NetMsgType::MNGOVERNANCESYNC, duplicate_syncable_object_fetch_stream);
BOOST_CHECK_EQUAL(CountQueuedInventory(*peer, CInv{MSG_GOVERNANCE_OBJECT, syncable_object_hash}), 1U);
BOOST_CHECK(!m_node.netfulfilledman->HasFulfilledRequest(peer->addr, syncable_vote_sync_request));
BOOST_CHECK_EQUAL(CountQueuedMessages(*peer, NetMsgType::SYNCSTATUSCOUNT), 0U);
AssertMisbehaviorScore(*m_node.peerman, *peer, 0);
connman.FlushSendBuffer(*peer);
auto syncable_stream = make_request_stream(syncable_object_hash, vote_filter);
net_gov.ProcessMessage(*peer, NetMsgType::MNGOVERNANCESYNC, syncable_stream);
BOOST_CHECK(m_node.netfulfilledman->HasFulfilledRequest(peer->addr, syncable_vote_sync_request));
BOOST_CHECK_EQUAL(CountQueuedMessages(*peer, NetMsgType::SYNCSTATUSCOUNT), 1U);
AssertMisbehaviorScore(*m_node.peerman, *peer, 0);
auto duplicate_syncable_stream = make_request_stream(syncable_object_hash, vote_filter);
net_gov.ProcessMessage(*peer, NetMsgType::MNGOVERNANCESYNC, duplicate_syncable_stream);
AssertMisbehaviorScore(*m_node.peerman, *peer, 20);
connman.FlushSendBuffer(*peer);
CGovernanceObject empty_filter_object{uint256(), /*revision=*/1, GetTime() + 2, uint256S("03"), /*data=*/{}};
m_node.govman->AddPostponedObject(empty_filter_object);
const uint256 empty_filter_object_hash{empty_filter_object.GetHash()};
const std::string empty_filter_vote_request{strprintf("%s-votes-%s", NetMsgType::MNGOVERNANCESYNC,
empty_filter_object_hash.ToString())};
auto empty_filter_stream = make_request_stream(empty_filter_object_hash, CBloomFilter{});
net_gov.ProcessMessage(*peer, NetMsgType::MNGOVERNANCESYNC, empty_filter_stream);
BOOST_CHECK_EQUAL(CountQueuedInventory(*peer, CInv{MSG_GOVERNANCE_OBJECT, empty_filter_object_hash}), 1U);
BOOST_CHECK(!m_node.netfulfilledman->HasFulfilledRequest(peer->addr, empty_filter_vote_request));
BOOST_CHECK_EQUAL(CountQueuedMessages(*peer, NetMsgType::SYNCSTATUSCOUNT), 0U);
AssertMisbehaviorScore(*m_node.peerman, *peer, 20);
connman.FlushSendBuffer(*peer);
auto duplicate_empty_filter_stream = make_request_stream(empty_filter_object_hash, CBloomFilter{});
net_gov.ProcessMessage(*peer, NetMsgType::MNGOVERNANCESYNC, duplicate_empty_filter_stream);
BOOST_CHECK_EQUAL(CountQueuedInventory(*peer, CInv{MSG_GOVERNANCE_OBJECT, empty_filter_object_hash}), 1U);
BOOST_CHECK(!m_node.netfulfilledman->HasFulfilledRequest(peer->addr, empty_filter_vote_request));
BOOST_CHECK_EQUAL(CountQueuedMessages(*peer, NetMsgType::SYNCSTATUSCOUNT), 0U);
AssertMisbehaviorScore(*m_node.peerman, *peer, 20);
const uint256 unknown_vote_hash{uint256S("09")};
const std::string unknown_vote_request{strprintf("%s-votes-%s", NetMsgType::MNGOVERNANCESYNC,
unknown_vote_hash.ToString())};
auto unknown_vote_stream = make_request_stream(unknown_vote_hash, vote_filter);
net_gov.ProcessMessage(*peer, NetMsgType::MNGOVERNANCESYNC, unknown_vote_stream);
BOOST_CHECK(!m_node.netfulfilledman->HasFulfilledRequest(peer->addr, unknown_vote_request));
AssertMisbehaviorScore(*m_node.peerman, *peer, 20);
auto duplicate_unknown_vote_stream = make_request_stream(unknown_vote_hash, vote_filter);
net_gov.ProcessMessage(*peer, NetMsgType::MNGOVERNANCESYNC, duplicate_unknown_vote_stream);
BOOST_CHECK(!m_node.netfulfilledman->HasFulfilledRequest(peer->addr, unknown_vote_request));
AssertMisbehaviorScore(*m_node.peerman, *peer, 20);
const uint256 object_fetch_hash{uint256S("0a")};
const std::string object_fetch_request{strprintf("%s-votes-%s", NetMsgType::MNGOVERNANCESYNC,
object_fetch_hash.ToString())};
auto object_fetch_stream = make_request_stream(object_fetch_hash, CBloomFilter{});
net_gov.ProcessMessage(*peer, NetMsgType::MNGOVERNANCESYNC, object_fetch_stream);
BOOST_CHECK(!m_node.netfulfilledman->HasFulfilledRequest(peer->addr, object_fetch_request));
AssertMisbehaviorScore(*m_node.peerman, *peer, 20);
auto duplicate_object_fetch_stream = make_request_stream(object_fetch_hash, CBloomFilter{});
net_gov.ProcessMessage(*peer, NetMsgType::MNGOVERNANCESYNC, duplicate_object_fetch_stream);
BOOST_CHECK(!m_node.netfulfilledman->HasFulfilledRequest(peer->addr, object_fetch_request));
AssertMisbehaviorScore(*m_node.peerman, *peer, 20);
m_node.peerman->FinalizeNode(*peer);
}
BOOST_AUTO_TEST_CASE(governance_objects_require_peer_announcement_or_request)
{
LOCK(NetEventsInterface::g_msgproc_mutex);
TestChainState& chainstate =
*static_cast<TestChainState*>(&m_node.chainman->ActiveChainstate());
chainstate.JumpOutOfIbd();
NetGovernance net_gov(m_node.peerman.get(), *m_node.govman, *m_node.mn_sync,
*m_node.netfulfilledman, *m_node.connman);
auto announcing_peer{MakeGovernanceInvPeer(/*id=*/11)};
auto second_announcing_peer{MakeGovernanceInvPeer(/*id=*/12)};
auto unsolicited_peer{MakeGovernanceInvPeer(/*id=*/13)};
m_node.peerman->InitializeNode(*announcing_peer, NODE_NETWORK);
m_node.peerman->InitializeNode(*second_announcing_peer, NODE_NETWORK);
m_node.peerman->InitializeNode(*unsolicited_peer, NODE_NETWORK);
AssertMisbehaviorScore(*m_node.peerman, *announcing_peer, 0);
const CGovernanceObject govobj{MakeGovernanceObject(GetTime<std::chrono::seconds>().count(), uint256S("21"))};
const CInv object_inv{MSG_GOVERNANCE_OBJECT, govobj.GetHash()};
ProcessInv(*m_node.peerman, *announcing_peer, object_inv);
ProcessInv(*m_node.peerman, *second_announcing_peer, object_inv);
// Pre-seed the object so ProcessObject short-circuits on "already seen" and the
// acceptance gate is exercised in isolation from collateral/chain validation. Both
// ProcessInv calls ran first, so the object was still absent at INV time and both
// peers registered a real pending request in the net-layer tracker.
m_node.govman->AddPostponedObject(govobj);
// Announcing peer: the gate accepts (it announced the inv) and consumes its per-peer
// request entry. Checking the entry is gone proves the gate actually consulted the
// net-layer tracker, independent of the pre-seed above (a rejected object would leave
// the entry untouched).
ProcessGovernanceObject(net_gov, *announcing_peer, govobj);
BOOST_CHECK(
!WITH_LOCK(::cs_main, return m_node.peerman->PeerConsumeObjectRequest(announcing_peer->GetId(), object_inv)));
AssertMisbehaviorScore(*m_node.peerman, *announcing_peer, 0);
ProcessGovernanceObject(net_gov, *second_announcing_peer, govobj);
// Consumption is per-peer: the second announcer's own entry is accepted and consumed,
// independent of the first peer's already-consumed entry.
BOOST_CHECK(!WITH_LOCK(::cs_main,
return m_node.peerman->PeerConsumeObjectRequest(second_announcing_peer->GetId(), object_inv)));
AssertMisbehaviorScore(*m_node.peerman, *second_announcing_peer, 0);
const CGovernanceObject unsolicited_govobj{MakeGovernanceObject(GetTime<std::chrono::seconds>().count() + 1, uint256S("22"))};
ProcessGovernanceObject(net_gov, *unsolicited_peer, unsolicited_govobj);
BOOST_CHECK(!m_node.govman->HaveObjectForHash(unsolicited_govobj.GetHash()));
AssertMisbehaviorScore(*m_node.peerman, *unsolicited_peer, 0);
m_node.peerman->FinalizeNode(*announcing_peer);
m_node.peerman->FinalizeNode(*second_announcing_peer);
m_node.peerman->FinalizeNode(*unsolicited_peer);
chainstate.ResetIbd();
}
BOOST_AUTO_TEST_CASE(governance_votes_require_peer_announcement_or_request)
{
LOCK(NetEventsInterface::g_msgproc_mutex);
TestChainState& chainstate =
*static_cast<TestChainState*>(&m_node.chainman->ActiveChainstate());
chainstate.JumpOutOfIbd();
NetGovernance net_gov(m_node.peerman.get(), *m_node.govman, *m_node.mn_sync,
*m_node.netfulfilledman, *m_node.connman);
auto announcing_peer{MakeGovernanceInvPeer(/*id=*/21)};
auto second_announcing_peer{MakeGovernanceInvPeer(/*id=*/22)};
auto unsolicited_peer{MakeGovernanceInvPeer(/*id=*/23)};
m_node.peerman->InitializeNode(*announcing_peer, NODE_NETWORK);
m_node.peerman->InitializeNode(*second_announcing_peer, NODE_NETWORK);
m_node.peerman->InitializeNode(*unsolicited_peer, NODE_NETWORK);
auto& connman = static_cast<ConnmanTestMsg&>(*m_node.connman);
const CGovernanceVote vote{MakeGovernanceVote(uint256S("31"))};
const CInv vote_inv{MSG_GOVERNANCE_OBJECT_VOTE, vote.GetHash()};
ProcessGovernanceVote(net_gov, *unsolicited_peer, vote);
BOOST_CHECK_EQUAL(CountQueuedMessages(*unsolicited_peer, NetMsgType::MNGOVERNANCESYNC), 0U);
AssertMisbehaviorScore(*m_node.peerman, *unsolicited_peer, 0);
ProcessInv(*m_node.peerman, *announcing_peer, vote_inv);
ProcessInv(*m_node.peerman, *second_announcing_peer, vote_inv);
connman.FlushSendBuffer(*announcing_peer);
ProcessGovernanceVote(net_gov, *announcing_peer, vote);
BOOST_CHECK_EQUAL(CountQueuedMessages(*announcing_peer, NetMsgType::MNGOVERNANCESYNC), 1U);
AssertMisbehaviorScore(*m_node.peerman, *announcing_peer, 0);
connman.FlushSendBuffer(*second_announcing_peer);
ProcessGovernanceVote(net_gov, *second_announcing_peer, vote);
// Second announcer: the gate accepts (it independently announced the vote) and consumes
// its per-peer request entry. A rejected vote would return before the gate consumes and
// leave the entry intact, so this proves the accept path independently of the (deduped)
// orphan-request side effect.
BOOST_CHECK(!WITH_LOCK(::cs_main,
return m_node.peerman->PeerConsumeObjectRequest(second_announcing_peer->GetId(), vote_inv)));
AssertMisbehaviorScore(*m_node.peerman, *second_announcing_peer, 0);
m_node.peerman->FinalizeNode(*announcing_peer);
m_node.peerman->FinalizeNode(*second_announcing_peer);
m_node.peerman->FinalizeNode(*unsolicited_peer);
chainstate.ResetIbd();
}
// A message received while not blockchain-synced is dropped, but the per-peer authorization must
// survive so a retransmit after sync is still accepted (the consume happens after the sync gate).
BOOST_AUTO_TEST_CASE(governance_vote_authorization_survives_unsynced_drop)
{
LOCK(NetEventsInterface::g_msgproc_mutex);
TestChainState& chainstate =
*static_cast<TestChainState*>(&m_node.chainman->ActiveChainstate());
chainstate.JumpOutOfIbd();
NetGovernance net_gov(m_node.peerman.get(), *m_node.govman, *m_node.mn_sync,
*m_node.netfulfilledman, *m_node.connman);
auto peer{MakeGovernanceInvPeer(/*id=*/31)};
m_node.peerman->InitializeNode(*peer, NODE_NETWORK);
auto& connman = static_cast<ConnmanTestMsg&>(*m_node.connman);
const CGovernanceVote vote{MakeGovernanceVote(uint256S("41"))};
const CInv vote_inv{MSG_GOVERNANCE_OBJECT_VOTE, vote.GetHash()};
// Synced: announce the vote so we hold a per-peer request for it.
BOOST_REQUIRE(m_node.mn_sync->IsBlockchainSynced());
ProcessInv(*m_node.peerman, *peer, vote_inv);
// Not synced: delivering the vote is dropped at the sync gate and must NOT consume the request.
m_node.mn_sync->Reset(/*fForce=*/true, /*fNotifyReset=*/false);
BOOST_REQUIRE(!m_node.mn_sync->IsBlockchainSynced());
connman.FlushSendBuffer(*peer);
ProcessGovernanceVote(net_gov, *peer, vote);
BOOST_CHECK_EQUAL(CountQueuedMessages(*peer, NetMsgType::MNGOVERNANCESYNC), 0U);
// Back in sync, the retransmit is still authorized: ProcessVote runs and (orphan parent)
// requests the missing object. Had the unsynced drop consumed the request, the gate would now
// reject the vote as unrequested and send no MNGOVERNANCESYNC.
m_node.mn_sync->SwitchToNextAsset();
BOOST_REQUIRE(m_node.mn_sync->IsBlockchainSynced());
connman.FlushSendBuffer(*peer);
ProcessGovernanceVote(net_gov, *peer, vote);
BOOST_CHECK_EQUAL(CountQueuedMessages(*peer, NetMsgType::MNGOVERNANCESYNC), 1U);
m_node.peerman->FinalizeNode(*peer);
chainstate.ResetIbd();
}
BOOST_AUTO_TEST_SUITE_END()