Skip to content

Commit 5402147

Browse files
feat: only use efficient qrinfo construction for new shared protocol version
1 parent 542ac80 commit 5402147

File tree

5 files changed

+87
-42
lines changed

5 files changed

+87
-42
lines changed

src/llmq/snapshot.cpp

+79-38
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ UniValue CQuorumRotationInfo::ToJson() const
8787
bool BuildQuorumRotationInfo(CDeterministicMNManager& dmnman, CQuorumSnapshotManager& qsnapman,
8888
const ChainstateManager& chainman, const CQuorumManager& qman,
8989
const CQuorumBlockProcessor& qblockman, const CGetQuorumRotationInfo& request,
90-
CQuorumRotationInfo& response, std::string& errorRet)
90+
bool use_legacy_construction, CQuorumRotationInfo& response, std::string& errorRet)
9191
{
9292
AssertLockHeld(cs_main);
9393

@@ -112,13 +112,24 @@ bool BuildQuorumRotationInfo(CDeterministicMNManager& dmnman, CQuorumSnapshotMan
112112
}
113113
baseBlockIndexes.push_back(blockIndex);
114114
}
115+
if (use_legacy_construction) {
116+
std::sort(baseBlockIndexes.begin(), baseBlockIndexes.end(), [](const CBlockIndex* a, const CBlockIndex* b) {
117+
return a->nHeight < b->nHeight;
118+
});
119+
}
115120
}
116121

117122
const CBlockIndex* tipBlockIndex = chainman.ActiveChain().Tip();
118123
if (!tipBlockIndex) {
119124
errorRet = strprintf("tip block not found");
120125
return false;
121126
}
127+
if (use_legacy_construction) {
128+
//Build MN list Diff always with highest baseblock
129+
if (!BuildSimplifiedMNListDiff(dmnman, chainman, qblockman, qman, baseBlockIndexes.back()->GetBlockHash(), tipBlockIndex->GetBlockHash(), response.mnListDiffTip, errorRet)) {
130+
return false;
131+
}
132+
}
122133

123134
const CBlockIndex* blockIndex = chainman.m_blockman.LookupBlockIndex(request.blockRequestHash);
124135
if (!blockIndex) {
@@ -148,6 +159,13 @@ bool BuildQuorumRotationInfo(CDeterministicMNManager& dmnman, CQuorumSnapshotMan
148159
return false;
149160
}
150161

162+
if (use_legacy_construction) {
163+
//Build MN list Diff always with highest baseblock
164+
if (!BuildSimplifiedMNListDiff(dmnman, chainman, qblockman, qman, GetLastBaseBlockHash(baseBlockIndexes, pWorkBlockHIndex, use_legacy_construction), pWorkBlockHIndex->GetBlockHash(), response.mnListDiffH, errorRet)) {
165+
return false;
166+
}
167+
}
168+
151169
const CBlockIndex* pBlockHMinusCIndex = tipBlockIndex->GetAncestor(hBlockIndex->nHeight - cycleLength);
152170
if (!pBlockHMinusCIndex) {
153171
errorRet = strprintf("Can not find block H-C");
@@ -190,6 +208,12 @@ bool BuildQuorumRotationInfo(CDeterministicMNManager& dmnman, CQuorumSnapshotMan
190208
const CBlockIndex* pWorkBlockHMinus4CIndex = pBlockHMinus4CIndex->GetAncestor(pBlockHMinus4CIndex->nHeight - workDiff);
191209
//Checked later if extraShare is on
192210

211+
if (use_legacy_construction) {
212+
if (!BuildSimplifiedMNListDiff(dmnman, chainman, qblockman, qman, GetLastBaseBlockHash(baseBlockIndexes, pWorkBlockHMinusCIndex, use_legacy_construction), pWorkBlockHMinusCIndex->GetBlockHash(), response.mnListDiffAtHMinusC, errorRet)) {
213+
return false;
214+
}
215+
}
216+
193217
auto snapshotHMinusC = qsnapman.GetSnapshotForBlock(llmqType, pBlockHMinusCIndex);
194218
if (!snapshotHMinusC.has_value()) {
195219
errorRet = strprintf("Can not find quorum snapshot at H-C");
@@ -198,6 +222,12 @@ bool BuildQuorumRotationInfo(CDeterministicMNManager& dmnman, CQuorumSnapshotMan
198222
response.quorumSnapshotAtHMinusC = std::move(snapshotHMinusC.value());
199223
}
200224

225+
if (use_legacy_construction) {
226+
if (!BuildSimplifiedMNListDiff(dmnman, chainman, qblockman, qman, GetLastBaseBlockHash(baseBlockIndexes, pWorkBlockHMinus2CIndex, use_legacy_construction), pWorkBlockHMinus2CIndex->GetBlockHash(), response.mnListDiffAtHMinus2C, errorRet)) {
227+
return false;
228+
}
229+
}
230+
201231
auto snapshotHMinus2C = qsnapman.GetSnapshotForBlock(llmqType, pBlockHMinus2CIndex);
202232
if (!snapshotHMinus2C.has_value()) {
203233
errorRet = strprintf("Can not find quorum snapshot at H-2C");
@@ -206,6 +236,12 @@ bool BuildQuorumRotationInfo(CDeterministicMNManager& dmnman, CQuorumSnapshotMan
206236
response.quorumSnapshotAtHMinus2C = std::move(snapshotHMinus2C.value());
207237
}
208238

239+
if (use_legacy_construction) {
240+
if (!BuildSimplifiedMNListDiff(dmnman, chainman, qblockman, qman, GetLastBaseBlockHash(baseBlockIndexes, pWorkBlockHMinus3CIndex, use_legacy_construction), pWorkBlockHMinus3CIndex->GetBlockHash(), response.mnListDiffAtHMinus3C, errorRet)) {
241+
return false;
242+
}
243+
}
244+
209245
auto snapshotHMinus3C = qsnapman.GetSnapshotForBlock(llmqType, pBlockHMinus3CIndex);
210246
if (!snapshotHMinus3C.has_value()) {
211247
errorRet = strprintf("Can not find quorum snapshot at H-3C");
@@ -231,11 +267,12 @@ bool BuildQuorumRotationInfo(CDeterministicMNManager& dmnman, CQuorumSnapshotMan
231267
}
232268

233269
CSimplifiedMNListDiff mn4c;
234-
if (!BuildSimplifiedMNListDiff(dmnman, chainman, qblockman, qman, GetLastBaseBlockHash(baseBlockIndexes, pWorkBlockHMinus4CIndex), pWorkBlockHMinus4CIndex->GetBlockHash(), mn4c, errorRet)) {
270+
if (!BuildSimplifiedMNListDiff(dmnman, chainman, qblockman, qman, GetLastBaseBlockHash(baseBlockIndexes, pWorkBlockHMinus4CIndex, use_legacy_construction), pWorkBlockHMinus4CIndex->GetBlockHash(), mn4c, errorRet)) {
235271
return false;
236272
}
237-
baseBlockIndexes.push_back(pWorkBlockHMinus4CIndex);
238-
273+
if (!use_legacy_construction) {
274+
baseBlockIndexes.push_back(pWorkBlockHMinus4CIndex);
275+
}
239276
response.mnListDiffAtHMinus4C = std::move(mn4c);
240277
} else {
241278
response.extraShare = false;
@@ -288,55 +325,59 @@ bool BuildQuorumRotationInfo(CDeterministicMNManager& dmnman, CQuorumSnapshotMan
288325
}
289326

290327
CSimplifiedMNListDiff mnhneeded;
291-
if (!BuildSimplifiedMNListDiff(dmnman, chainman, qblockman, qman, GetLastBaseBlockHash(baseBlockIndexes, pNeededWorkBlockIndex), pNeededWorkBlockIndex->GetBlockHash(), mnhneeded, errorRet)) {
328+
if (!BuildSimplifiedMNListDiff(dmnman, chainman, qblockman, qman, GetLastBaseBlockHash(baseBlockIndexes, pNeededWorkBlockIndex, use_legacy_construction), pNeededWorkBlockIndex->GetBlockHash(), mnhneeded, errorRet)) {
292329
return false;
293330
}
294-
baseBlockIndexes.push_back(pNeededWorkBlockIndex);
295-
331+
if (!use_legacy_construction) {
332+
baseBlockIndexes.push_back(pNeededWorkBlockIndex);
333+
}
296334
response.mnListDiffList.push_back(mnhneeded);
297335
}
298336

299-
if (!BuildSimplifiedMNListDiff(dmnman, chainman, qblockman, qman,
300-
GetLastBaseBlockHash(baseBlockIndexes, pWorkBlockHMinus3CIndex),
301-
pWorkBlockHMinus3CIndex->GetBlockHash(), response.mnListDiffAtHMinus3C, errorRet)) {
302-
return false;
303-
}
304-
baseBlockIndexes.push_back(pWorkBlockHMinus3CIndex);
337+
if (!use_legacy_construction) {
338+
if (!BuildSimplifiedMNListDiff(dmnman, chainman, qblockman, qman,
339+
GetLastBaseBlockHash(baseBlockIndexes, pWorkBlockHMinus3CIndex, use_legacy_construction),
340+
pWorkBlockHMinus3CIndex->GetBlockHash(), response.mnListDiffAtHMinus3C, errorRet)) {
341+
return false;
342+
}
343+
baseBlockIndexes.push_back(pWorkBlockHMinus3CIndex);
305344

306-
if (!BuildSimplifiedMNListDiff(dmnman, chainman, qblockman, qman,
307-
GetLastBaseBlockHash(baseBlockIndexes, pWorkBlockHMinus2CIndex),
308-
pWorkBlockHMinus2CIndex->GetBlockHash(), response.mnListDiffAtHMinus2C, errorRet)) {
309-
return false;
310-
}
311-
baseBlockIndexes.push_back(pWorkBlockHMinus2CIndex);
345+
if (!BuildSimplifiedMNListDiff(dmnman, chainman, qblockman, qman,
346+
GetLastBaseBlockHash(baseBlockIndexes, pWorkBlockHMinus2CIndex, use_legacy_construction),
347+
pWorkBlockHMinus2CIndex->GetBlockHash(), response.mnListDiffAtHMinus2C, errorRet)) {
348+
return false;
349+
}
350+
baseBlockIndexes.push_back(pWorkBlockHMinus2CIndex);
312351

313-
if (!BuildSimplifiedMNListDiff(dmnman, chainman, qblockman, qman,
314-
GetLastBaseBlockHash(baseBlockIndexes, pWorkBlockHMinusCIndex),
315-
pWorkBlockHMinusCIndex->GetBlockHash(), response.mnListDiffAtHMinusC, errorRet)) {
316-
return false;
317-
}
318-
baseBlockIndexes.push_back(pWorkBlockHMinusCIndex);
352+
if (!BuildSimplifiedMNListDiff(dmnman, chainman, qblockman, qman,
353+
GetLastBaseBlockHash(baseBlockIndexes, pWorkBlockHMinusCIndex, use_legacy_construction),
354+
pWorkBlockHMinusCIndex->GetBlockHash(), response.mnListDiffAtHMinusC, errorRet)) {
355+
return false;
356+
}
357+
baseBlockIndexes.push_back(pWorkBlockHMinusCIndex);
319358

320-
if (!BuildSimplifiedMNListDiff(dmnman, chainman, qblockman, qman,
321-
GetLastBaseBlockHash(baseBlockIndexes, pWorkBlockHIndex),
322-
pWorkBlockHIndex->GetBlockHash(), response.mnListDiffH, errorRet)) {
323-
return false;
324-
}
325-
baseBlockIndexes.push_back(pWorkBlockHIndex);
359+
if (!BuildSimplifiedMNListDiff(dmnman, chainman, qblockman, qman,
360+
GetLastBaseBlockHash(baseBlockIndexes, pWorkBlockHIndex, use_legacy_construction),
361+
pWorkBlockHIndex->GetBlockHash(), response.mnListDiffH, errorRet)) {
362+
return false;
363+
}
364+
baseBlockIndexes.push_back(pWorkBlockHIndex);
326365

327-
if (!BuildSimplifiedMNListDiff(dmnman, chainman, qblockman, qman, GetLastBaseBlockHash(baseBlockIndexes, tipBlockIndex),
328-
tipBlockIndex->GetBlockHash(), response.mnListDiffTip, errorRet)) {
329-
return false;
366+
if (!BuildSimplifiedMNListDiff(dmnman, chainman, qblockman, qman, GetLastBaseBlockHash(baseBlockIndexes, tipBlockIndex, use_legacy_construction),
367+
tipBlockIndex->GetBlockHash(), response.mnListDiffTip, errorRet)) {
368+
return false;
369+
}
330370
}
331-
332371
return true;
333372
}
334373

335-
uint256 GetLastBaseBlockHash(Span<const CBlockIndex*> baseBlockIndexes, const CBlockIndex* blockIndex)
374+
uint256 GetLastBaseBlockHash(Span<const CBlockIndex*> baseBlockIndexes, const CBlockIndex* blockIndex, bool use_legacy_construction)
336375
{
337376
uint256 hash;
338-
std::sort(baseBlockIndexes.begin(), baseBlockIndexes.end(),
339-
[](const CBlockIndex* a, const CBlockIndex* b) { return a->nHeight < b->nHeight; });
377+
if (!use_legacy_construction) {
378+
std::sort(baseBlockIndexes.begin(), baseBlockIndexes.end(),
379+
[](const CBlockIndex* a, const CBlockIndex* b) { return a->nHeight < b->nHeight; });
380+
}
340381
for (const auto baseBlock : baseBlockIndexes) {
341382
if (baseBlock->nHeight >= blockIndex->nHeight)
342383
break;

src/llmq/snapshot.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,8 @@ class CQuorumRotationInfo
210210
bool BuildQuorumRotationInfo(CDeterministicMNManager& dmnman, CQuorumSnapshotManager& qsnapman,
211211
const ChainstateManager& chainman, const CQuorumManager& qman,
212212
const CQuorumBlockProcessor& qblockman, const CGetQuorumRotationInfo& request,
213-
CQuorumRotationInfo& response, std::string& errorRet) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
214-
uint256 GetLastBaseBlockHash(Span<const CBlockIndex*> baseBlockIndexes, const CBlockIndex* blockIndex);
213+
bool use_legacy_construction, CQuorumRotationInfo& response, std::string& errorRet) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
214+
uint256 GetLastBaseBlockHash(Span<const CBlockIndex*> baseBlockIndexes, const CBlockIndex* blockIndex, bool use_legacy_construction);
215215

216216
class CQuorumSnapshotManager
217217
{

src/net_processing.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -5200,7 +5200,8 @@ void PeerManagerImpl::ProcessMessage(
52005200

52015201
llmq::CQuorumRotationInfo quorumRotationInfoRet;
52025202
std::string strError;
5203-
if (BuildQuorumRotationInfo(*m_dmnman, *m_llmq_ctx->qsnapman, m_chainman, *m_llmq_ctx->qman, *m_llmq_ctx->quorum_block_processor, cmd, quorumRotationInfoRet, strError)) {
5203+
bool use_leagcy_construction = pfrom.GetCommonVersion() < EFFICIENT_QRINFO_VERSION;;
5204+
if (BuildQuorumRotationInfo(*m_dmnman, *m_llmq_ctx->qsnapman, m_chainman, *m_llmq_ctx->qman, *m_llmq_ctx->quorum_block_processor, cmd, use_leagcy_construction, quorumRotationInfoRet, strError)) {
52045205
m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::QUORUMROTATIONINFO, quorumRotationInfoRet));
52055206
} else {
52065207
strError = strprintf("getquorumrotationinfo failed for size(baseBlockHashes)=%d, blockRequestHash=%s. error=%s", cmd.baseBlockHashes.size(), cmd.blockRequestHash.ToString(), strError);

src/rpc/quorums.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -860,7 +860,7 @@ static RPCHelpMan quorum_rotationinfo()
860860
LOCK(cs_main);
861861

862862
if (!BuildQuorumRotationInfo(*CHECK_NONFATAL(node.dmnman), *llmq_ctx.qsnapman, chainman, *llmq_ctx.qman,
863-
*llmq_ctx.quorum_block_processor, cmd, quorumRotationInfoRet, strError)) {
863+
*llmq_ctx.quorum_block_processor, cmd, false, quorumRotationInfoRet, strError)) {
864864
throw JSONRPCError(RPC_INVALID_REQUEST, strError);
865865
}
866866

src/version.h

+3
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ static const int DSQ_INV_VERSION = 70234;
6161
//! Maximum header count for HEADRES2 message was increased from 2000 to 8000 in this version
6262
static const int INCREASE_MAX_HEADERS2_VERSION = 70235;
6363

64+
//! Behavior of QRINFO is changed in this protocol version
65+
static const int EFFICIENT_QRINFO_VERSION = 70236;
66+
6467
// Make sure that none of the values above collide with `ADDRV2_FORMAT`.
6568

6669
#endif // BITCOIN_VERSION_H

0 commit comments

Comments
 (0)