Line data Source code
1 : // Copyright (c) 2025 The Dash Core developers 2 : // Distributed under the MIT software license, see the accompanying 3 : // file COPYING or http://www.opensource.org/licenses/mit-license.php. 4 : 5 : #ifndef BITCOIN_TEST_UTIL_LLMQ_TESTS_H 6 : #define BITCOIN_TEST_UTIL_LLMQ_TESTS_H 7 : 8 : #include <test/util/random.h> 9 : #include <test/util/setup_common.h> 10 : 11 : #include <arith_uint256.h> 12 : #include <bls/bls.h> 13 : #include <consensus/params.h> 14 : #include <primitives/transaction.h> 15 : #include <random.h> 16 : #include <serialize.h> 17 : #include <streams.h> 18 : #include <uint256.h> 19 : 20 : #include <chainlock/chainlock.h> 21 : #include <llmq/commitment.h> 22 : #include <llmq/params.h> 23 : 24 : #include <vector> 25 : 26 : namespace llmq { 27 : namespace testutils { 28 : 29 : // Helper function to get LLMQ params from available_llmqs 30 585 : inline const Consensus::LLMQParams& GetLLMQParams(Consensus::LLMQType type) 31 : { 32 1461 : for (const auto& params : Consensus::available_llmqs) { 33 1461 : if (params.type == type) { 34 585 : return params; 35 : } 36 : } 37 0 : throw std::runtime_error("LLMQ type not found"); 38 0 : } 39 : 40 : // Helper functions to create test data 41 19 : inline CBLSPublicKey CreateRandomBLSPublicKey() 42 : { 43 19 : CBLSSecretKey sk; 44 19 : sk.MakeNewKey(); 45 19 : return sk.GetPublicKey(); 46 19 : } 47 : 48 16 : inline CBLSSignature CreateRandomBLSSignature() 49 : { 50 16 : CBLSSecretKey sk; 51 16 : sk.MakeNewKey(); 52 16 : uint256 hash = InsecureRand256(); 53 16 : return sk.Sign(hash, false); 54 16 : } 55 : 56 3 : inline CFinalCommitment CreateValidCommitment(const Consensus::LLMQParams& params, const uint256& quorumHash) 57 : { 58 3 : CFinalCommitment commitment; 59 3 : commitment.llmqType = params.type; 60 3 : commitment.quorumHash = quorumHash; 61 3 : commitment.validMembers.resize(params.size, true); 62 3 : commitment.signers.resize(params.size, true); 63 3 : commitment.quorumVvecHash = InsecureRand256(); 64 3 : commitment.quorumPublicKey = CreateRandomBLSPublicKey(); 65 3 : commitment.quorumSig = CreateRandomBLSSignature(); 66 3 : commitment.membersSig = CreateRandomBLSSignature(); 67 3 : return commitment; 68 3 : } 69 : 70 6 : inline chainlock::ChainLockSig CreateChainLock(int32_t height, const uint256& blockHash) 71 : { 72 6 : CBLSSignature sig = CreateRandomBLSSignature(); 73 6 : return chainlock::ChainLockSig(height, blockHash, sig); 74 : } 75 : 76 : // Helper to create bit vectors with specific patterns 77 18 : inline std::vector<bool> CreateBitVector(size_t size, const std::vector<size_t>& trueBits) 78 : { 79 18 : std::vector<bool> result(size, false); 80 76 : for (size_t idx : trueBits) { 81 58 : if (idx < size) { 82 58 : result[idx] = true; 83 58 : } 84 : } 85 18 : return result; 86 18 : } 87 : 88 : // Serialization round-trip test helper 89 : template <typename T> 90 13 : inline bool TestSerializationRoundtrip(const T& obj) 91 : { 92 : // Datastreams we will compare at the end 93 13 : CDataStream ss_obj(SER_NETWORK, PROTOCOL_VERSION); 94 13 : CDataStream ss_obj_rt(SER_NETWORK, PROTOCOL_VERSION); 95 : // A temporary datastream to perform serialization round-trip 96 13 : CDataStream ss_tmp(SER_NETWORK, PROTOCOL_VERSION); 97 13 : T obj_rt; 98 : 99 13 : ss_obj << obj; 100 13 : ss_tmp << obj; 101 13 : ss_tmp >> obj_rt; 102 13 : ss_obj_rt << obj_rt; 103 : 104 13 : return ss_obj.str() == ss_obj_rt.str(); 105 13 : } 106 : 107 : // Helper to create deterministic test data 108 46 : inline uint256 GetTestQuorumHash(uint32_t n) { return ArithToUint256(arith_uint256(n)); } 109 : 110 18 : inline uint256 GetTestBlockHash(uint32_t n) { return ArithToUint256(arith_uint256(n) << 32); } 111 : 112 : } // namespace testutils 113 : } // namespace llmq 114 : 115 : #endif // BITCOIN_TEST_UTIL_LLMQ_TESTS_H