dash/src/test/specialtx_tests.cpp

86 lines
2.4 KiB
C++
Raw Normal View History

// Copyright (c) 2021-2023 The Dash Core developers
2021-12-11 21:00:27 +01:00
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <bls/bls.h>
#include <consensus/validation.h>
#include <evo/mnhftx.h>
#include <evo/specialtx.h>
#include <primitives/transaction.h>
#include <uint256.h>
#include <util/string.h>
2021-12-11 21:00:27 +01:00
#include <util/strencodings.h>
#include <boost/test/unit_test.hpp>
#include <test/util/setup_common.h>
2021-12-11 21:00:27 +01:00
#include <cstdint>
#include <vector>
bool VerifyMNHFTx(const CTransaction& tx, CValidationState& state)
{
MNHFTxPayload mnhfTx;
if (!GetTxPayload(tx, mnhfTx)) {
Merge #17004: validation: Remove REJECT code from CValidationState 9075d13153ce06cd59a45644831ecc43126e1e82 [docs] Add release notes for removal of REJECT reasons (John Newbery) 04a2f326ec0f06fb4fce1c4f93500752f05dede8 [validation] Fix REJECT message comments (John Newbery) e9d5a59e34ff2d538d8f5315efd9908bf24d0fdc [validation] Remove REJECT code from CValidationState (John Newbery) 0053e16714323c1694c834fdca74f064a1a33529 [logging] Don't log REJECT code when transaction is rejected (John Newbery) a1a07cfe99fc8cee30ba5976dc36b47b1f6532ab [validation] Fix peer punishment for bad blocks (John Newbery) Pull request description: We no longer send BIP 61 REJECT messages, so there's no need to set a REJECT code in the CValidationState object. Note that there is a minor bug fix in p2p behaviour here. Because the call to `MaybePunishNode()` in `PeerLogicValidation::BlockChecked()` only previously happened if the REJECT code was > 0 and < `REJECT_INTERNAL`, then there are cases were `MaybePunishNode()` can get called where it wasn't previously: - when `AcceptBlockHeader()` fails with `CACHED_INVALID`. - when `AcceptBlockHeader()` fails with `BLOCK_MISSING_PREV`. Note that `BlockChecked()` cannot fail with an 'internal' reject code. The only internal reject code was `REJECT_HIGHFEE`, which was only set in ATMP. This reverts a minor bug introduced in 5d08c9c579ba8cc7b684105c6a08263992b08d52. ACKs for top commit: ariard: ACK 9075d13, changes since last reviewed are splitting them in separate commits to ease understanding and fix nits fjahr: ACK 9075d13153ce06cd59a45644831ecc43126e1e82, confirmed diff to last review was fixing nits in docs/comments. ryanofsky: Code review ACK 9075d13153ce06cd59a45644831ecc43126e1e82. Only changes since last review are splitting the main commit and updating comments Tree-SHA512: 58e8a1a4d4e6f156da5d29fb6ad6a62fc9c594bbfc6432b3252e962d0e9e10149bf3035185dc5320c46c09f3e49662bc2973ec759679c0f3412232087cb8a3a7
2019-10-24 10:43:02 +02:00
return state.Invalid(ValidationInvalidReason::CONSENSUS, false, "bad-mnhf-payload");
2021-12-11 21:00:27 +01:00
}
if (mnhfTx.nVersion == 0 || mnhfTx.nVersion > MNHFTxPayload::CURRENT_VERSION) {
Merge #17004: validation: Remove REJECT code from CValidationState 9075d13153ce06cd59a45644831ecc43126e1e82 [docs] Add release notes for removal of REJECT reasons (John Newbery) 04a2f326ec0f06fb4fce1c4f93500752f05dede8 [validation] Fix REJECT message comments (John Newbery) e9d5a59e34ff2d538d8f5315efd9908bf24d0fdc [validation] Remove REJECT code from CValidationState (John Newbery) 0053e16714323c1694c834fdca74f064a1a33529 [logging] Don't log REJECT code when transaction is rejected (John Newbery) a1a07cfe99fc8cee30ba5976dc36b47b1f6532ab [validation] Fix peer punishment for bad blocks (John Newbery) Pull request description: We no longer send BIP 61 REJECT messages, so there's no need to set a REJECT code in the CValidationState object. Note that there is a minor bug fix in p2p behaviour here. Because the call to `MaybePunishNode()` in `PeerLogicValidation::BlockChecked()` only previously happened if the REJECT code was > 0 and < `REJECT_INTERNAL`, then there are cases were `MaybePunishNode()` can get called where it wasn't previously: - when `AcceptBlockHeader()` fails with `CACHED_INVALID`. - when `AcceptBlockHeader()` fails with `BLOCK_MISSING_PREV`. Note that `BlockChecked()` cannot fail with an 'internal' reject code. The only internal reject code was `REJECT_HIGHFEE`, which was only set in ATMP. This reverts a minor bug introduced in 5d08c9c579ba8cc7b684105c6a08263992b08d52. ACKs for top commit: ariard: ACK 9075d13, changes since last reviewed are splitting them in separate commits to ease understanding and fix nits fjahr: ACK 9075d13153ce06cd59a45644831ecc43126e1e82, confirmed diff to last review was fixing nits in docs/comments. ryanofsky: Code review ACK 9075d13153ce06cd59a45644831ecc43126e1e82. Only changes since last review are splitting the main commit and updating comments Tree-SHA512: 58e8a1a4d4e6f156da5d29fb6ad6a62fc9c594bbfc6432b3252e962d0e9e10149bf3035185dc5320c46c09f3e49662bc2973ec759679c0f3412232087cb8a3a7
2019-10-24 10:43:02 +02:00
return state.Invalid(ValidationInvalidReason::CONSENSUS, false, "bad-mnhf-version");
2021-12-11 21:00:27 +01:00
}
return true;
}
static CMutableTransaction CreateMNHFTx(const uint256& mnhfTxHash, const CBLSSignature& cblSig, const uint16_t& versionBit)
{
MNHFTxPayload extraPayload;
extraPayload.nVersion = 1;
extraPayload.signal.nVersion = versionBit;
extraPayload.signal.quorumHash = mnhfTxHash;
extraPayload.signal.sig = cblSig;
CMutableTransaction tx;
tx.nVersion = 3;
tx.nType = TRANSACTION_MNHF_SIGNAL;
SetTxPayload(tx, extraPayload);
return tx;
}
BOOST_FIXTURE_TEST_SUITE(specialtx_tests, BasicTestingSetup)
BOOST_AUTO_TEST_CASE(verify_mnhf_specialtx_tests)
{
int count = 10;
uint16_t ver = 2;
std::vector<CBLSSignature> vec_sigs;
std::vector<CBLSPublicKey> vec_pks;
std::vector<CBLSSecretKey> vec_sks;
CBLSSecretKey sk;
uint256 hash = GetRandHash();
for (int i = 0; i < count; i++) {
sk.MakeNewKey();
vec_pks.push_back(sk.GetPublicKey());
vec_sks.push_back(sk);
}
CBLSSecretKey ag_sk = CBLSSecretKey::AggregateInsecure(vec_sks);
CBLSPublicKey ag_pk = CBLSPublicKey::AggregateInsecure(vec_pks);
BOOST_CHECK(ag_sk.IsValid());
BOOST_CHECK(ag_pk.IsValid());
uint256 verHash = uint256S(ToString(ver));
2021-12-11 21:00:27 +01:00
auto sig = ag_sk.Sign(verHash);
BOOST_CHECK(sig.VerifyInsecure(ag_pk, verHash));
const CMutableTransaction tx = CreateMNHFTx(hash, sig, ver);
CValidationState state;
BOOST_CHECK(VerifyMNHFTx(CTransaction(tx), state));
}
BOOST_AUTO_TEST_SUITE_END()