Line data Source code
1 : // Copyright (c) 2021-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 : #include <bls/bls.h> 6 : #include <consensus/validation.h> 7 : #include <evo/mnhftx.h> 8 : #include <evo/specialtx.h> 9 : #include <llmq/context.h> 10 : #include <primitives/transaction.h> 11 : #include <uint256.h> 12 : #include <util/string.h> 13 : #include <validation.h> 14 : 15 : #include <boost/test/unit_test.hpp> 16 : #include <test/util/setup_common.h> 17 : 18 : #include <cstdint> 19 : #include <vector> 20 : 21 : 22 2 : static CMutableTransaction CreateMNHFTx(const uint256& quorumHash, const CBLSSignature& cblSig, const uint16_t& versionBit) 23 : { 24 2 : MNHFTxPayload extraPayload; 25 2 : extraPayload.nVersion = 1; 26 2 : extraPayload.signal.versionBit = versionBit; 27 2 : extraPayload.signal.quorumHash = quorumHash; 28 2 : extraPayload.signal.sig = cblSig; 29 : 30 2 : CMutableTransaction tx; 31 2 : tx.nVersion = 3; 32 2 : tx.nType = TRANSACTION_MNHF_SIGNAL; 33 2 : SetTxPayload(tx, extraPayload); 34 : 35 2 : return tx; 36 2 : } 37 : 38 146 : BOOST_FIXTURE_TEST_SUITE(evo_mnhf_tests, TestChain100Setup) 39 : 40 149 : BOOST_AUTO_TEST_CASE(verify_mnhf_specialtx_tests) 41 : { 42 1 : int count = 10; 43 1 : uint16_t bit = 1; 44 : 45 1 : std::vector<CBLSSignature> vec_sigs; 46 1 : std::vector<CBLSPublicKey> vec_pks; 47 1 : std::vector<CBLSSecretKey> vec_sks; 48 : 49 1 : CBLSSecretKey sk; 50 11 : for (int i = 0; i < count; i++) { 51 10 : sk.MakeNewKey(); 52 10 : vec_pks.push_back(sk.GetPublicKey()); 53 10 : vec_sks.push_back(sk); 54 10 : } 55 : 56 1 : CBLSSecretKey ag_sk = CBLSSecretKey::AggregateInsecure(vec_sks); 57 1 : CBLSPublicKey ag_pk = CBLSPublicKey::AggregateInsecure(vec_pks); 58 : 59 1 : BOOST_CHECK(ag_sk.IsValid()); 60 1 : BOOST_CHECK(ag_pk.IsValid()); 61 : 62 1 : const bool use_legacy{false}; 63 1 : uint256 verHash = uint256S(ToString(bit)); 64 1 : auto sig = ag_sk.Sign(verHash, use_legacy); 65 1 : BOOST_CHECK(sig.VerifyInsecure(ag_pk, verHash, use_legacy)); 66 : 67 1 : auto& chainman = Assert(m_node.chainman); 68 1 : auto& qman = *Assert(m_node.llmq_ctx)->qman; 69 1 : const CBlockIndex* pindex = chainman->ActiveChain().Tip(); 70 1 : uint256 hash = GetRandHash(); 71 1 : TxValidationState state; 72 : 73 : { // wrong quorum (we don't have any indeed) 74 1 : const CTransaction tx{CTransaction(CreateMNHFTx(hash, sig, bit))}; 75 1 : CheckMNHFTx(*chainman, qman, CTransaction(tx), pindex, state); 76 1 : BOOST_CHECK_EQUAL(state.ToString(), "bad-mnhf-quorum-hash"); 77 1 : } 78 : 79 : { // non EHF fork 80 1 : const CTransaction tx{CTransaction(CreateMNHFTx(hash, sig, 28))}; 81 1 : CheckMNHFTx(*chainman, qman, CTransaction(tx), pindex, state); 82 1 : BOOST_CHECK_EQUAL(state.ToString(), "bad-mnhf-non-ehf"); 83 1 : } 84 1 : } 85 : 86 146 : BOOST_AUTO_TEST_SUITE_END()