From 64cedb30bd126e48ef80ef9e92c90ce4c274d712 Mon Sep 17 00:00:00 2001 From: Konstantin Akimov Date: Wed, 21 Aug 2024 00:14:44 +0700 Subject: [PATCH] feat: actually test something EHF unit tests --- src/evo/mnhftx.cpp | 8 +++--- src/test/evo_mnhf_tests.cpp | 43 +++++++++++++++++--------------- test/functional/feature_mnehf.py | 2 -- 3 files changed, 27 insertions(+), 26 deletions(-) diff --git a/src/evo/mnhftx.cpp b/src/evo/mnhftx.cpp index 02f1839f08..c5c4cc9dd9 100644 --- a/src/evo/mnhftx.cpp +++ b/src/evo/mnhftx.cpp @@ -116,6 +116,10 @@ bool CheckMNHFTx(const ChainstateManager& chainman, const llmq::CQuorumManager& return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-mnhf-version"); } + if (!Params().IsValidMNActivation(mnhfTx.signal.versionBit, pindexPrev->GetMedianTimePast())) { + return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-mnhf-non-ehf"); + } + const CBlockIndex* pindexQuorum = WITH_LOCK(::cs_main, return chainman.m_blockman.LookupBlockIndex(mnhfTx.signal.quorumHash)); if (!pindexQuorum) { return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-mnhf-quorum-hash"); @@ -139,10 +143,6 @@ bool CheckMNHFTx(const ChainstateManager& chainman, const llmq::CQuorumManager& return false; } - if (!Params().IsValidMNActivation(mnhfTx.signal.versionBit, pindexPrev->GetMedianTimePast())) { - return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-mnhf-non-ehf"); - } - return true; } diff --git a/src/test/evo_mnhf_tests.cpp b/src/test/evo_mnhf_tests.cpp index 2be08a4804..7c1d299733 100644 --- a/src/test/evo_mnhf_tests.cpp +++ b/src/test/evo_mnhf_tests.cpp @@ -6,10 +6,12 @@ #include #include #include +#include #include #include #include #include +#include #include #include @@ -18,24 +20,12 @@ #include -bool VerifyMNHFTx(const CTransaction& tx, TxValidationState& state) -{ - if (const auto opt_mnhfTx_payload = GetTxPayload(tx); !opt_mnhfTx_payload) { - return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-mnhf-payload"); - } else if (opt_mnhfTx_payload->nVersion == 0 || - opt_mnhfTx_payload->nVersion > MNHFTxPayload::CURRENT_VERSION) { - return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-mnhf-version"); - } - - return true; -} - -static CMutableTransaction CreateMNHFTx(const uint256& mnhfTxHash, const CBLSSignature& cblSig, const uint16_t& versionBit) +static CMutableTransaction CreateMNHFTx(const uint256& quorumHash, const CBLSSignature& cblSig, const uint16_t& versionBit) { MNHFTxPayload extraPayload; extraPayload.nVersion = 1; extraPayload.signal.versionBit = versionBit; - extraPayload.signal.quorumHash = mnhfTxHash; + extraPayload.signal.quorumHash = quorumHash; extraPayload.signal.sig = cblSig; CMutableTransaction tx; @@ -46,19 +36,18 @@ static CMutableTransaction CreateMNHFTx(const uint256& mnhfTxHash, const CBLSSig return tx; } -BOOST_FIXTURE_TEST_SUITE(evo_mnhf_tests, BasicTestingSetup) +BOOST_FIXTURE_TEST_SUITE(evo_mnhf_tests, TestChain100Setup) BOOST_AUTO_TEST_CASE(verify_mnhf_specialtx_tests) { int count = 10; - uint16_t ver = 2; + uint16_t bit = 1; std::vector vec_sigs; std::vector vec_pks; std::vector vec_sks; CBLSSecretKey sk; - uint256 hash = GetRandHash(); for (int i = 0; i < count; i++) { sk.MakeNewKey(); vec_pks.push_back(sk.GetPublicKey()); @@ -71,13 +60,27 @@ BOOST_AUTO_TEST_CASE(verify_mnhf_specialtx_tests) BOOST_CHECK(ag_sk.IsValid()); BOOST_CHECK(ag_pk.IsValid()); - uint256 verHash = uint256S(ToString(ver)); + uint256 verHash = uint256S(ToString(bit)); auto sig = ag_sk.Sign(verHash); BOOST_CHECK(sig.VerifyInsecure(ag_pk, verHash)); - const CMutableTransaction tx = CreateMNHFTx(hash, sig, ver); + auto& chainman = Assert(m_node.chainman); + auto& qman = *Assert(m_node.llmq_ctx)->qman; + const CBlockIndex* pindex = chainman->ActiveChain().Tip(); + uint256 hash = GetRandHash(); TxValidationState state; - BOOST_CHECK(VerifyMNHFTx(CTransaction(tx), state)); + + { // wrong quorum (we don't have any indeed) + const CTransaction tx{CTransaction(CreateMNHFTx(hash, sig, bit))}; + CheckMNHFTx(*chainman, qman, CTransaction(tx), pindex, state); + BOOST_CHECK_EQUAL(state.ToString(), "bad-mnhf-quorum-hash"); + } + + { // non EHF fork + const CTransaction tx{CTransaction(CreateMNHFTx(hash, sig, 28))}; + CheckMNHFTx(*chainman, qman, CTransaction(tx), pindex, state); + BOOST_CHECK_EQUAL(state.ToString(), "bad-mnhf-non-ehf"); + } } BOOST_AUTO_TEST_SUITE_END() diff --git a/test/functional/feature_mnehf.py b/test/functional/feature_mnehf.py index a7406e7197..96c71a67e5 100755 --- a/test/functional/feature_mnehf.py +++ b/test/functional/feature_mnehf.py @@ -145,7 +145,6 @@ class MnehfTest(DashTestFramework): pubkey = key.get_pubkey().get_bytes() ehf_tx = self.create_mnehf(28, pubkey) ehf_unknown_tx = self.create_mnehf(27, pubkey) - ehf_invalid_tx = self.create_mnehf(9, pubkey) # deployment that is known as non-EHF self.log.info("Checking deserialization of CMnEhf by python's code") mnehf_payload = CMnEhf() @@ -166,7 +165,6 @@ class MnehfTest(DashTestFramework): self.log.info(f"ehf tx: {ehf_tx_sent}") ehf_unknown_tx_sent = self.send_tx(ehf_unknown_tx) self.log.info(f"unknown ehf tx: {ehf_unknown_tx_sent}") - self.send_tx(ehf_invalid_tx, expected_error='bad-mnhf-non-ehf') self.sync_all() ehf_blockhash = self.nodes[1].generate(1)[0] self.sync_blocks()