Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/llmq/snapshot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

#include <univalue.h>

#include <algorithm>

namespace {
constexpr std::string_view DB_QUORUM_SNAPSHOT{"llmq_S"};

Expand Down Expand Up @@ -74,9 +76,15 @@ bool BuildQuorumRotationInfo(CDeterministicMNManager& dmnman, CQuorumSnapshotMan
}
baseBlockIndexes.push_back(blockIndex);
}
if (use_legacy_construction) {
std::sort(baseBlockIndexes.begin(), baseBlockIndexes.end(),
[](const CBlockIndex* a, const CBlockIndex* b) { return a->nHeight < b->nHeight; });
// Sort in all cases: the legacy path (served to peers < EFFICIENT_QRINFO_VERSION)
// relies on the order for baseBlockIndexes.back() and GetLastBaseBlockHash().
std::sort(baseBlockIndexes.begin(), baseBlockIndexes.end(),
[](const CBlockIndex* a, const CBlockIndex* b) { return a->nHeight < b->nHeight; });
if (!use_legacy_construction) {
// Only deduplicate on the non-legacy path; leave the legacy path untouched so the
// wire response to older peers stays bit-for-bit identical to the pre-fix behavior.
baseBlockIndexes.erase(std::unique(baseBlockIndexes.begin(), baseBlockIndexes.end()),
baseBlockIndexes.end());
}
}

Expand Down
49 changes: 49 additions & 0 deletions src/test/llmq_snapshot_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <test/util/llmq_tests.h>
#include <test/util/setup_common.h>

#include <chain.h>
#include <streams.h>
#include <univalue.h>

Expand Down Expand Up @@ -176,6 +177,54 @@ BOOST_AUTO_TEST_CASE(quorum_rotation_info_construction_test)
// Note: CQuorumRotationInfo serialization requires complex setup
// This is better tested in functional tests

BOOST_AUTO_TEST_CASE(get_last_base_block_hash_repeated_base_blocks_test)
{
std::vector<CBlockIndex> blocks(4);
std::vector<uint256> hashes{
GetTestBlockHash(10),
GetTestBlockHash(20),
GetTestBlockHash(30),
GetTestBlockHash(40),
};
for (size_t i{0}; i < blocks.size(); ++i) {
blocks[i].nHeight = static_cast<int>((i + 1) * 10);
blocks[i].phashBlock = &hashes[i];
}

// Non-legacy: sorts internally, so unsorted input with duplicates is fine.
std::vector<const CBlockIndex*> unsorted_repeated_base_blocks{
&blocks[2],
&blocks[0],
&blocks[1],
&blocks[1],
};
BOOST_CHECK(GetLastBaseBlockHash(unsorted_repeated_base_blocks, &blocks[3], false) == hashes[2]);
BOOST_CHECK(GetLastBaseBlockHash(unsorted_repeated_base_blocks, &blocks[1], false) == hashes[1]);

// Legacy: relies on caller-supplied sort and tolerates duplicates as a no-op.
// BuildQuorumRotationInfo deliberately does NOT deduplicate in the legacy path so
// the wire response to older peers stays bit-for-bit identical; these checks
// demonstrate that the duplicate is harmless to GetLastBaseBlockHash's output.
std::vector<const CBlockIndex*> sorted_repeated_base_blocks{
&blocks[0],
&blocks[1],
&blocks[1],
&blocks[2],
};
std::vector<const CBlockIndex*> sorted_unique_base_blocks{
&blocks[0],
&blocks[1],
&blocks[2],
};
BOOST_CHECK(GetLastBaseBlockHash(sorted_repeated_base_blocks, &blocks[3], true) == hashes[2]);
BOOST_CHECK(GetLastBaseBlockHash(sorted_repeated_base_blocks, &blocks[1], true) == hashes[1]);
// Legacy no-op proof: duplicate vs unique input produces the same hash.
BOOST_CHECK(GetLastBaseBlockHash(sorted_repeated_base_blocks, &blocks[3], true) ==
GetLastBaseBlockHash(sorted_unique_base_blocks, &blocks[3], true));
BOOST_CHECK(GetLastBaseBlockHash(sorted_repeated_base_blocks, &blocks[1], true) ==
GetLastBaseBlockHash(sorted_unique_base_blocks, &blocks[1], true));
}

BOOST_AUTO_TEST_CASE(get_quorum_rotation_info_serialization_test)
{
CGetQuorumRotationInfo getInfo;
Expand Down
3 changes: 3 additions & 0 deletions test/functional/feature_llmq_rotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,9 @@ def run_test(self):
hmc_base_blockhash = self.nodes[0].getblockhash(block_count - (block_count % 24) - 24 - 8)
best_block_hash = self.nodes[0].getbestblockhash()
rpc_qr_info = self.nodes[0].quorum("rotationinfo", best_block_hash, False, [hmc_base_blockhash])
rpc_qr_info_repeated_base = self.nodes[0].quorum("rotationinfo", best_block_hash, False,
[hmc_base_blockhash, hmc_base_blockhash])
assert_equal(rpc_qr_info_repeated_base, rpc_qr_info)
assert_equal(rpc_qr_info["mnListDiffTip"]["blockHash"], best_block_hash)
assert_equal(rpc_qr_info["mnListDiffTip"]["baseBlockHash"], rpc_qr_info["mnListDiffH"]["blockHash"])
assert_equal(rpc_qr_info["mnListDiffH"]["baseBlockHash"], rpc_qr_info["mnListDiffAtHMinusC"]["blockHash"])
Expand Down
Loading