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
69 changes: 0 additions & 69 deletions src/test/llmq_chainlock_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#include <netaddress.h>
#include <spork.h>
#include <streams.h>
#include <util/strencodings.h>
#include <validation.h>
#include <version.h>

Expand Down Expand Up @@ -172,27 +171,6 @@ BOOST_FIXTURE_TEST_CASE(historical_coinbase_lookup_from_disk, RegTestingSetup)
BOOST_CHECK_THROW(unavailable.Read(activation + 30), std::runtime_error);
}

BOOST_AUTO_TEST_CASE(chainlock_construction_test)
{
// Test default constructor
ChainLockSig clsig1;
BOOST_CHECK(clsig1.IsNull());
BOOST_CHECK_EQUAL(clsig1.getHeight(), -1);
BOOST_CHECK(clsig1.getBlockHash().IsNull());
BOOST_CHECK(!clsig1.getSig().IsValid());

// Test parameterized constructor
int32_t height = 12345;
uint256 blockHash = GetTestBlockHash(1);
CBLSSignature sig = CreateRandomBLSSignature();

ChainLockSig clsig2(height, blockHash, sig);
BOOST_CHECK(!clsig2.IsNull());
BOOST_CHECK_EQUAL(clsig2.getHeight(), height);
BOOST_CHECK(clsig2.getBlockHash() == blockHash);
BOOST_CHECK(clsig2.getSig() == sig);
}

BOOST_AUTO_TEST_CASE(chainlock_null_test)
{
ChainLockSig clsig;
Expand Down Expand Up @@ -227,26 +205,6 @@ BOOST_AUTO_TEST_CASE(chainlock_serialization_test)
BOOST_CHECK_EQUAL(clsig.IsNull(), deserialized.IsNull());
}

BOOST_AUTO_TEST_CASE(chainlock_tostring_test)
{
// Test null chainlock
ChainLockSig nullClsig;
std::string nullStr = nullClsig.ToString();
BOOST_CHECK(!nullStr.empty());

// Test valid chainlock
int32_t height = 123456;
uint256 blockHash = GetTestBlockHash(789);
ChainLockSig clsig = CreateChainLock(height, blockHash);

std::string str = clsig.ToString();
BOOST_CHECK(!str.empty());

// ToString should contain height and hash info
BOOST_CHECK(str.find(strprintf("%d", height)) != std::string::npos);
BOOST_CHECK(str.find(blockHash.ToString().substr(0, 10)) != std::string::npos);
}

BOOST_AUTO_TEST_CASE(chainlock_edge_cases_test)
{
// Test with edge case heights
Expand All @@ -271,33 +229,6 @@ BOOST_AUTO_TEST_CASE(chainlock_edge_cases_test)
BOOST_CHECK_EQUAL(clsig2.getHeight(), clsig2_deserialized.getHeight());
}

BOOST_AUTO_TEST_CASE(chainlock_malformed_data_test)
{
// Test deserialization of truncated data
ChainLockSig clsig = CreateChainLock(1000, GetTestBlockHash(5));

CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
ss << clsig;

// Truncate the stream
std::string data = ss.str();
for (size_t truncateAt = 1; truncateAt < data.size(); truncateAt += 10) {
CDataStream truncated(std::vector<unsigned char>(data.begin(), data.begin() + truncateAt), SER_NETWORK,
PROTOCOL_VERSION);

ChainLockSig deserialized;
try {
truncated >> deserialized;
// If no exception, verify it's either complete or default
if (truncateAt < sizeof(int32_t)) {
BOOST_CHECK(deserialized.IsNull());
}
} catch (const std::exception&) {
// Expected for most truncation points
}
}
}

BOOST_FIXTURE_TEST_CASE(stale_chainlocks_are_remembered_for_duplicate_suppression, TestingSetup)
{
m_node.clhandler->CheckActiveState();
Expand Down
28 changes: 0 additions & 28 deletions src/test/llmq_commitment_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,6 @@ BOOST_FIXTURE_TEST_SUITE(llmq_commitment_tests, BasicTestingSetup)
// Get test params for use in tests
static const Consensus::LLMQParams& TEST_PARAMS = GetLLMQParams(Consensus::LLMQType::LLMQ_TEST_V17);

BOOST_AUTO_TEST_CASE(commitment_null_test)
{
CFinalCommitment commitment;

// Test default constructor creates null commitment
BOOST_CHECK(commitment.IsNull());
BOOST_CHECK(commitment.quorumHash.IsNull());
BOOST_CHECK(commitment.validMembers.empty());
BOOST_CHECK(commitment.signers.empty());
BOOST_CHECK(!commitment.quorumPublicKey.IsValid());
BOOST_CHECK(!commitment.quorumSig.IsValid());

// Note: VerifyNull requires valid LLMQ params which we can't test in unit tests
// It's tested in functional tests
}

BOOST_AUTO_TEST_CASE(commitment_counting_test)
{
CFinalCommitment commitment;
Expand Down Expand Up @@ -346,16 +330,4 @@ BOOST_AUTO_TEST_CASE(commitment_verify_null_edge_cases)
BOOST_CHECK(!commitment.IsNull());
}

BOOST_AUTO_TEST_CASE(commitment_tx_payload_test)
{
CFinalCommitmentTxPayload payload;
payload.nHeight = 12345;
payload.commitment = CreateValidCommitment(TEST_PARAMS, GetTestQuorumHash(1));

// Test basic construction
BOOST_CHECK_EQUAL(payload.nVersion, CFinalCommitmentTxPayload::CURRENT_VERSION);
BOOST_CHECK_EQUAL(payload.nHeight, 12345);
BOOST_CHECK(!payload.commitment.IsNull());
}

BOOST_AUTO_TEST_SUITE_END()
67 changes: 14 additions & 53 deletions src/test/llmq_hash_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,20 @@ BOOST_AUTO_TEST_CASE(build_commitment_hash_sensitivity_test)
GetTestQuorumHash(99) // Different vvec hash
);
BOOST_CHECK(baseHash != hashDiffVvec);

// Null quorum hash, null vvec hash, and an invalid pubkey still change the hash.
uint256 hashNullQuorum = BuildCommitmentHash(llmqType, uint256(), validMembers, quorumPubKey, vvecHash);
BOOST_CHECK(!hashNullQuorum.IsNull());
BOOST_CHECK(baseHash != hashNullQuorum);

CBLSPublicKey invalidPubKey;
uint256 hashInvalidKey = BuildCommitmentHash(llmqType, quorumHash, validMembers, invalidPubKey, vvecHash);
BOOST_CHECK(!hashInvalidKey.IsNull());
BOOST_CHECK(baseHash != hashInvalidKey);

uint256 hashNullVvec = BuildCommitmentHash(llmqType, quorumHash, validMembers, quorumPubKey, uint256());
BOOST_CHECK(!hashNullVvec.IsNull());
BOOST_CHECK(baseHash != hashNullVvec);
}

BOOST_AUTO_TEST_CASE(build_commitment_hash_edge_cases_test)
Expand Down Expand Up @@ -111,59 +125,6 @@ BOOST_AUTO_TEST_CASE(build_commitment_hash_edge_cases_test)
BOOST_CHECK(hashAllValid != hashNoneValid);
}

BOOST_AUTO_TEST_CASE(build_commitment_hash_null_inputs_test)
{
// Test with null quorum hash
uint256 hashNullQuorum = BuildCommitmentHash(TEST_PARAMS.type,
uint256(), // Null hash
CreateBitVector(5, {0, 1, 2}), CreateRandomBLSPublicKey(),
GetTestQuorumHash(1));
BOOST_CHECK(!hashNullQuorum.IsNull());

// Test with invalid (but serializable) public key
CBLSPublicKey invalidPubKey;
uint256 hashInvalidKey = BuildCommitmentHash(TEST_PARAMS.type, GetTestQuorumHash(1), CreateBitVector(5, {0, 1, 2}),
invalidPubKey, // Invalid key
GetTestQuorumHash(2));
BOOST_CHECK(!hashInvalidKey.IsNull());

// Test with null vvec hash
uint256 hashNullVvec = BuildCommitmentHash(TEST_PARAMS.type, GetTestQuorumHash(1), CreateBitVector(5, {0, 1, 2}),
CreateRandomBLSPublicKey(),
uint256() // Null hash
);
BOOST_CHECK(!hashNullVvec.IsNull());

// All should produce different hashes
BOOST_CHECK(hashNullQuorum != hashInvalidKey);
BOOST_CHECK(hashNullQuorum != hashNullVvec);
BOOST_CHECK(hashInvalidKey != hashNullVvec);
}

BOOST_AUTO_TEST_CASE(build_commitment_hash_large_data_test)
{
// Test with maximum expected quorum size
std::vector<bool> largeValidMembers(400, true); // Max quorum size

// Create pattern in valid members
for (size_t i = 0; i < largeValidMembers.size(); i += 3) {
largeValidMembers[i] = false;
}

uint256 hashLarge = BuildCommitmentHash(TEST_PARAMS.type, GetTestQuorumHash(1), largeValidMembers,
CreateRandomBLSPublicKey(), GetTestQuorumHash(2));

BOOST_CHECK(!hashLarge.IsNull());

// Slightly different pattern should produce different hash
largeValidMembers[0] = !largeValidMembers[0];
uint256 hashLargeDiff = BuildCommitmentHash(TEST_PARAMS.type, GetTestQuorumHash(1), largeValidMembers,
CreateRandomBLSPublicKey(), GetTestQuorumHash(2));

BOOST_CHECK(!hashLargeDiff.IsNull());
BOOST_CHECK(hashLarge != hashLargeDiff);
}

BOOST_AUTO_TEST_CASE(build_commitment_hash_bit_pattern_test)
{
// Test that different bit patterns produce different hashes
Expand Down
106 changes: 26 additions & 80 deletions src/test/llmq_snapshot_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,33 +22,6 @@ using namespace llmq::testutils;

BOOST_FIXTURE_TEST_SUITE(llmq_snapshot_tests, BasicTestingSetup)

BOOST_AUTO_TEST_CASE(quorum_snapshot_construction_test)
{
// Test default constructor
CQuorumSnapshot snapshot1;
BOOST_CHECK(snapshot1.activeQuorumMembers.empty());
BOOST_CHECK_EQUAL(snapshot1.mnSkipListMode, SnapshotSkipMode::MODE_NO_SKIPPING);
BOOST_CHECK(snapshot1.mnSkipList.empty());

// Test parameterized constructor
std::vector<bool> activeMembers = {true, false, true, true, false};
auto skipMode = SnapshotSkipMode::MODE_SKIPPING_ENTRIES;
std::vector<int> skipList = {1, 3, 5, 7};

CQuorumSnapshot snapshot2(activeMembers, skipMode, skipList);
BOOST_CHECK(snapshot2.activeQuorumMembers == activeMembers);
BOOST_CHECK_EQUAL(snapshot2.mnSkipListMode, skipMode);
BOOST_CHECK(snapshot2.mnSkipList == skipList);

// Test move semantics
std::vector<bool> activeMembersCopy = activeMembers;
std::vector<int> skipListCopy = skipList;
CQuorumSnapshot snapshot3(std::move(activeMembersCopy), skipMode, std::move(skipListCopy));
BOOST_CHECK(snapshot3.activeQuorumMembers == activeMembers);
BOOST_CHECK_EQUAL(snapshot3.mnSkipListMode, skipMode);
BOOST_CHECK(snapshot3.mnSkipList == skipList);
}

BOOST_AUTO_TEST_CASE(quorum_snapshot_serialization_test)
{
std::vector<bool> activeMembers = CreateBitVector(10, {0, 2, 4, 6, 8});
Expand Down Expand Up @@ -108,21 +81,6 @@ BOOST_AUTO_TEST_CASE(quorum_snapshot_serialization_test)
BOOST_CHECK(TestSerializationRoundtrip(CQuorumSnapshot(alternating, SnapshotSkipMode::MODE_NO_SKIPPING, {})));
}

BOOST_AUTO_TEST_CASE(quorum_rotation_info_construction_test)
{
CQuorumRotationInfo rotInfo;

// Test default state
BOOST_CHECK(!rotInfo.extraShare);
BOOST_CHECK(!rotInfo.cycleHMinus4C.has_value());
BOOST_CHECK(rotInfo.lastCommitmentPerIndex.empty());
BOOST_CHECK(rotInfo.quorumSnapshotList.empty());
BOOST_CHECK(rotInfo.mnListDiffList.empty());
}

// 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);
Expand Down Expand Up @@ -251,51 +209,39 @@ BOOST_AUTO_TEST_CASE(quorum_rotation_info_serialization_test)
BOOST_CHECK(TestSerializationRoundtrip(rotInfo));
}

BOOST_AUTO_TEST_CASE(quorum_snapshot_json_test)
BOOST_AUTO_TEST_CASE(quorum_snapshot_json_values)
{
// Create snapshot with test data
std::vector<bool> activeMembers = {true, false, true, true, false, false, true};
auto skipMode = SnapshotSkipMode::MODE_SKIPPING_ENTRIES;
std::vector<int> skipList = {10, 20, 30, 40};

CQuorumSnapshot snapshot(activeMembers, skipMode, skipList);

// Test JSON conversion
UniValue json = snapshot.ToJson();

// Verify JSON structure
BOOST_CHECK(json.isObject());
BOOST_CHECK(json.exists("activeQuorumMembers"));
BOOST_CHECK(json.exists("mnSkipListMode"));
BOOST_CHECK(json.exists("mnSkipList"));

// Verify skip list is array
BOOST_CHECK(json["mnSkipList"].isArray());
BOOST_CHECK_EQUAL(json["mnSkipList"].size(), skipList.size());
const std::vector<bool> activeMembers = {true, false, true};
const std::vector<int> skipList = {10, 20};
CQuorumSnapshot snapshot(activeMembers, SnapshotSkipMode::MODE_SKIPPING_ENTRIES, skipList);

const UniValue json = snapshot.ToJson();
BOOST_CHECK_EQUAL(json["mnSkipListMode"].getInt<int>(),
static_cast<int>(SnapshotSkipMode::MODE_SKIPPING_ENTRIES));
BOOST_REQUIRE_EQUAL(json["activeQuorumMembers"].size(), activeMembers.size());
BOOST_CHECK_EQUAL(json["activeQuorumMembers"][0].get_bool(), true);
BOOST_CHECK_EQUAL(json["activeQuorumMembers"][1].get_bool(), false);
BOOST_CHECK_EQUAL(json["activeQuorumMembers"][2].get_bool(), true);
BOOST_REQUIRE_EQUAL(json["mnSkipList"].size(), skipList.size());
BOOST_CHECK_EQUAL(json["mnSkipList"][0].getInt<int>(), 10);
BOOST_CHECK_EQUAL(json["mnSkipList"][1].getInt<int>(), 20);
}

BOOST_AUTO_TEST_CASE(quorum_snapshot_malformed_data_test)
BOOST_AUTO_TEST_CASE(quorum_snapshot_truncated_wire_is_rejected)
{
// Create valid snapshot
CQuorumSnapshot snapshot({true, false, true}, SnapshotSkipMode::MODE_SKIPPING_ENTRIES, {1, 2, 3});

CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
ss << snapshot;

// Test truncated data deserialization
std::string data = ss.str();
for (size_t truncateAt = 1; truncateAt < data.size(); truncateAt += 5) {
// Every proper prefix of a populated snapshot is shorter than the length the
// compact-size fields claim, so Unserialize must fail rather than return a
// partial object. One-byte steps, not a stride, so a prefix the stride skipped
// cannot be accepted.
const CQuorumSnapshot snapshot({true, false, true}, SnapshotSkipMode::MODE_SKIPPING_ENTRIES, {1, 2, 3});
CDataStream full(SER_NETWORK, PROTOCOL_VERSION);
full << snapshot;
const std::string data = full.str();
for (size_t truncateAt = 1; truncateAt < data.size(); ++truncateAt) {
CDataStream truncated(std::vector<unsigned char>(data.begin(), data.begin() + truncateAt), SER_NETWORK,
PROTOCOL_VERSION);

CQuorumSnapshot deserialized;
try {
truncated >> deserialized;
// If no exception, it might be a valid partial deserialization
// (though unlikely for complex structures)
} catch (const std::exception&) {
// Expected for most truncation points
}
BOOST_CHECK_THROW(truncated >> deserialized, std::ios_base::failure);
}
}

Expand Down
Loading