refactor: introduce IsSpecialTxVersion()

Instead of hardcoding the tx version
This commit is contained in:
Alessandro Rezzi 2024-02-02 13:15:13 +01:00 committed by ale
parent 4301ab9dfb
commit 72d2008901
15 changed files with 38 additions and 29 deletions

View File

@ -121,7 +121,7 @@ bool CBloomFilter::CheckScript(const CScript &script) const
// wallets, etc.)
bool CBloomFilter::CheckSpecialTransactionMatchesAndUpdate(const CTransaction &tx)
{
if(tx.nVersion != 3 || tx.nType == TRANSACTION_NORMAL) {
if (!tx.IsSpecialTxVersion() || tx.nType == TRANSACTION_NORMAL) {
return false; // it is not a special transaction
}
switch(tx.nType) {

View File

@ -161,8 +161,7 @@ unsigned int GetTransactionSigOpCount(const CTransaction& tx, const CCoinsViewCa
bool Consensus::CheckTxInputs(const CTransaction& tx, TxValidationState& state, const CCoinsViewCache& inputs, int nSpendHeight, CAmount& txfee)
{
if (bool isAssetUnlockTx = (tx.nVersion == 3 && tx.nType == TRANSACTION_ASSET_UNLOCK); isAssetUnlockTx) {
if (bool isAssetUnlockTx = (tx.IsSpecialTxVersion() && tx.nType == TRANSACTION_ASSET_UNLOCK); isAssetUnlockTx) {
return GetAssetUnlockFee(tx, txfee, state);
}

View File

@ -261,7 +261,7 @@ bool CalcCbTxMerkleRootQuorums(const CBlock& block, const CBlockIndex* pindexPre
for (size_t i = 1; i < block.vtx.size(); i++) {
const auto& tx = block.vtx[i];
if (tx->nVersion == 3 && tx->nType == TRANSACTION_QUORUM_COMMITMENT) {
if (tx->IsSpecialTxVersion() && tx->nType == TRANSACTION_QUORUM_COMMITMENT) {
const auto opt_qc = GetTxPayload<llmq::CFinalCommitmentTxPayload>(*tx);
if (!opt_qc) {
return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-qc-payload-calc-cbtx-quorummerkleroot");

View File

@ -57,7 +57,7 @@ static UnlockDataPerBlock GetDataFromUnlockTxes(const std::vector<CTransactionRe
UnlockDataPerBlock blockData;
for (CTransactionRef tx : vtx) {
if (tx->nVersion != 3 || tx->nType != TRANSACTION_ASSET_UNLOCK) continue;
if (!tx->IsSpecialTxVersion() || tx->nType != TRANSACTION_ASSET_UNLOCK) continue;
CAmount unlocked{0};
TxValidationState tx_state;
@ -120,7 +120,7 @@ static std::optional<CBlock> GetBlockForCreditPool(const CBlockIndex* const bloc
assert(!block.vtx.empty());
// Should not fail if V20 (DIP0027) is active but it happens for RegChain (unit tests)
if (block.vtx[0]->nVersion != 3) return std::nullopt;
if (!block.vtx[0]->IsSpecialTxVersion()) return std::nullopt;
assert(!block.vtx[0]->vExtraPayload.empty());
@ -269,7 +269,7 @@ bool CCreditPoolDiff::Unlock(const CTransaction& tx, TxValidationState& state)
bool CCreditPoolDiff::ProcessLockUnlockTransaction(const CTransaction& tx, TxValidationState& state)
{
if (tx.nVersion != 3) return true;
if (!tx.IsSpecialTxVersion()) return true;
if (tx.nType != TRANSACTION_ASSET_LOCK && tx.nType != TRANSACTION_ASSET_UNLOCK) return true;
if (!CheckAssetLockUnlockTx(tx, pindexPrev, pool.indexes, state)) {

View File

@ -739,7 +739,7 @@ bool CDeterministicMNManager::BuildNewListFromBlock(const CBlock& block, gsl::no
for (int i = 1; i < (int)block.vtx.size(); i++) {
const CTransaction& tx = *block.vtx[i];
if (tx.nVersion != 3) {
if (!tx.IsSpecialTxVersion()) {
// only interested in special TXs
continue;
}
@ -1096,7 +1096,7 @@ CDeterministicMNList CDeterministicMNManager::GetListAtChainTip()
bool CDeterministicMNManager::IsProTxWithCollateral(const CTransactionRef& tx, uint32_t n)
{
if (tx->nVersion != 3 || tx->nType != TRANSACTION_PROVIDER_REGISTER) {
if (!tx->IsSpecialTxVersion() || tx->nType != TRANSACTION_PROVIDER_REGISTER) {
return false;
}
const auto opt_proTx = GetTxPayload<CProRegTx>(*tx);

View File

@ -102,7 +102,7 @@ bool MNHFTx::Verify(const uint256& quorumHash, const uint256& requestId, const u
bool CheckMNHFTx(const CTransaction& tx, const CBlockIndex* pindexPrev, TxValidationState& state)
{
if (tx.nVersion != 3 || tx.nType != TRANSACTION_MNHF_SIGNAL) {
if (!tx.IsSpecialTxVersion() || tx.nType != TRANSACTION_MNHF_SIGNAL) {
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-mnhf-type");
}
@ -147,7 +147,7 @@ bool CheckMNHFTx(const CTransaction& tx, const CBlockIndex* pindexPrev, TxValida
std::optional<uint8_t> extractEHFSignal(const CTransaction& tx)
{
if (tx.nVersion != 3 || tx.nType != TRANSACTION_MNHF_SIGNAL) {
if (!tx.IsSpecialTxVersion() || tx.nType != TRANSACTION_MNHF_SIGNAL) {
// only interested in special TXs 'TRANSACTION_MNHF_SIGNAL'
return std::nullopt;
}
@ -165,7 +165,7 @@ static bool extractSignals(const CBlock& block, const CBlockIndex* const pindex,
for (size_t i = 1; i < block.vtx.size(); ++i) {
const CTransaction& tx = *block.vtx[i];
if (tx.nVersion != 3 || tx.nType != TRANSACTION_MNHF_SIGNAL) {
if (!tx.IsSpecialTxVersion() || tx.nType != TRANSACTION_MNHF_SIGNAL) {
// only interested in special TXs 'TRANSACTION_MNHF_SIGNAL'
continue;
}

View File

@ -22,7 +22,7 @@ static bool CheckSpecialTxInner(CDeterministicMNManager& dmnman, const CTransact
{
AssertLockHeld(cs_main);
if (tx.nVersion != 3 || tx.nType == TRANSACTION_NORMAL)
if (!tx.IsSpecialTxVersion() || tx.nType == TRANSACTION_NORMAL)
return true;
const auto& consensusParams = Params().GetConsensus();
@ -80,7 +80,7 @@ bool CheckSpecialTx(CDeterministicMNManager& dmnman, const CTransaction& tx, con
[[nodiscard]] bool CSpecialTxProcessor::ProcessSpecialTx(const CTransaction& tx, const CBlockIndex* pindex, TxValidationState& state)
{
if (tx.nVersion != 3 || tx.nType == TRANSACTION_NORMAL) {
if (!tx.IsSpecialTxVersion() || tx.nType == TRANSACTION_NORMAL) {
return true;
}
@ -106,7 +106,7 @@ bool CheckSpecialTx(CDeterministicMNManager& dmnman, const CTransaction& tx, con
[[nodiscard]] bool CSpecialTxProcessor::UndoSpecialTx(const CTransaction& tx, const CBlockIndex* pindex)
{
if (tx.nVersion != 3 || tx.nType == TRANSACTION_NORMAL) {
if (!tx.IsSpecialTxVersion() || tx.nType == TRANSACTION_NORMAL) {
return true;
}
@ -281,7 +281,7 @@ bool CSpecialTxProcessor::CheckCreditPoolDiffForBlock(const CBlock& block, const
// If we get there we have v20 activated and credit pool amount must be included in block CbTx
const auto& tx = *block.vtx[0];
assert(tx.IsCoinBase());
assert(tx.nVersion == 3);
assert(tx.IsSpecialTxVersion());
assert(tx.nType == TRANSACTION_COINBASE);
const auto opt_cbTx = GetTxPayload<CCbTx>(tx);

View File

@ -52,7 +52,7 @@ CMerkleBlock::CMerkleBlock(const CBlock& block, CBloomFilter* filter, const std:
{
const auto& tx = *block.vtx[i];
const uint256& hash = tx.GetHash();
bool isAllowedType = tx.nVersion != 3 || allowedTxTypes.count(tx.nType) != 0;
bool isAllowedType = !tx.IsSpecialTxVersion() || allowedTxTypes.count(tx.nType) != 0;
if (txids && txids->count(hash)) {
vMatch.push_back(true);

View File

@ -224,7 +224,7 @@ public:
s << vin;
s << vout;
s << nLockTime;
if (this->nVersion == 3 && this->nType != TRANSACTION_NORMAL)
if (this->IsSpecialTxVersion() && this->nType != TRANSACTION_NORMAL)
s << vExtraPayload;
}
@ -265,6 +265,11 @@ public:
}
std::string ToString() const;
bool IsSpecialTxVersion() const noexcept
{
return nVersion >= 3;
}
};
/** A mutable version of CTransaction. */
@ -288,7 +293,7 @@ struct CMutableTransaction
SER_READ(obj, obj.nVersion = (int16_t) (n32bitVersion & 0xffff));
SER_READ(obj, obj.nType = (uint16_t) ((n32bitVersion >> 16) & 0xffff));
READWRITE(obj.vin, obj.vout, obj.nLockTime);
if (obj.nVersion == 3 && obj.nType != TRANSACTION_NORMAL) {
if (obj.IsSpecialTxVersion() && obj.nType != TRANSACTION_NORMAL) {
READWRITE(obj.vExtraPayload);
}
}
@ -304,6 +309,11 @@ struct CMutableTransaction
uint256 GetHash() const;
std::string ToString() const;
bool IsSpecialTxVersion() const
{
return nVersion >= 3;
}
};
typedef std::shared_ptr<const CTransaction> CTransactionRef;

View File

@ -2564,9 +2564,9 @@ static RPCHelpMan getspecialtxes()
for(const auto& tx : block.vtx)
{
if (tx->nVersion != 3 || tx->nType == TRANSACTION_NORMAL // ensure it's in fact a special tx
if (!tx->IsSpecialTxVersion() || tx->nType == TRANSACTION_NORMAL // ensure it's in fact a special tx
|| (nTxType != -1 && tx->nType != nTxType)) { // ensure special tx type matches filter, if given
continue;
continue;
}
nTxNum++;

View File

@ -1476,7 +1476,7 @@ public:
SerializeOutput(s, nOutput);
// Serialize nLockTime
::Serialize(s, txTo.nLockTime);
if (txTo.nVersion == 3 && txTo.nType != TRANSACTION_NORMAL)
if (txTo.IsSpecialTxVersion() && txTo.nType != TRANSACTION_NORMAL)
::Serialize(s, txTo.vExtraPayload);
}
};

View File

@ -149,7 +149,7 @@ BOOST_FIXTURE_TEST_CASE(evo_assetlock, TestChain100Setup)
// Check version
{
BOOST_CHECK(tx.nVersion == 3);
BOOST_CHECK(tx.IsSpecialTxVersion());
const auto opt_payload = GetTxPayload<CAssetLockPayload>(tx);
@ -350,7 +350,7 @@ BOOST_FIXTURE_TEST_CASE(evo_assetunlock, TestChain100Setup)
BOOST_CHECK(tx_state.GetRejectReason() == "bad-assetunlocktx-type");
// Check version of tx and payload
BOOST_CHECK(tx.nVersion == 3);
BOOST_CHECK(tx.IsSpecialTxVersion());
for (uint8_t payload_version : {0, 1, 2, 255}) {
CAssetUnlockPayload unlockPayload_tmp{payload_version,
unlockPayload->getIndex(),

View File

@ -387,7 +387,7 @@ CBlock TestChainSetup::CreateBlock(const std::vector<CMutableTransaction>& txns,
std::vector<CTransactionRef> llmqCommitments;
for (const auto& tx : block.vtx) {
if (tx->nVersion == 3 && tx->nType == TRANSACTION_QUORUM_COMMITMENT) {
if (tx->IsSpecialTxVersion() && tx->nType == TRANSACTION_QUORUM_COMMITMENT) {
llmqCommitments.emplace_back(tx);
}
}

View File

@ -376,7 +376,7 @@ static bool ContextualCheckTransaction(const CTransaction& tx, TxValidationState
if (fDIP0003Active_context) {
// check version 3 transaction types
if (tx.nVersion >= 3) {
if (tx.IsSpecialTxVersion()) {
if (tx.nType != TRANSACTION_NORMAL &&
tx.nType != TRANSACTION_PROVIDER_REGISTER &&
tx.nType != TRANSACTION_PROVIDER_UPDATE_SERVICE &&
@ -657,7 +657,7 @@ bool MemPoolAccept::PreChecks(ATMPArgs& args, Workspace& ws)
if (!ContextualCheckTransaction(tx, state, chainparams.GetConsensus(), m_active_chainstate.m_chain.Tip()))
return error("%s: ContextualCheckTransaction: %s, %s", __func__, hash.ToString(), state.ToString());
if (tx.nVersion == 3 && tx.nType == TRANSACTION_QUORUM_COMMITMENT) {
if (tx.IsSpecialTxVersion() && tx.nType == TRANSACTION_QUORUM_COMMITMENT) {
// quorum commitment is not allowed outside of blocks
return state.Invalid(TxValidationResult::TX_CONSENSUS, "qc-not-allowed");
}
@ -800,7 +800,7 @@ bool MemPoolAccept::PreChecks(ATMPArgs& args, Workspace& ws)
// blocks
// Checking of fee for MNHF_SIGNAL should be skipped: mnhf does not have
// inputs, outputs, or fee
if (tx.nVersion != 3 || tx.nType != TRANSACTION_MNHF_SIGNAL) {
if (!tx.IsSpecialTxVersion() || tx.nType != TRANSACTION_MNHF_SIGNAL) {
if (!bypass_limits && !CheckFeeRate(nSize, nModifiedFees, state)) return false;
}

View File

@ -3056,7 +3056,7 @@ bool CWallet::FundTransaction(CMutableTransaction& tx, CAmount& nFeeRet, int& nC
LOCK(cs_wallet);
int nExtraPayloadSize = 0;
if (tx.nVersion == 3 && tx.nType != TRANSACTION_NORMAL)
if (tx.IsSpecialTxVersion() && tx.nType != TRANSACTION_NORMAL)
nExtraPayloadSize = (int)tx.vExtraPayload.size();
CTransactionRef tx_new;