policy: Add CalculateExtraTxWeight to increase weight for datacarrier bytes

This commit is contained in:
Luke Dashjr 2023-09-04 22:23:06 +00:00
parent bd069a768c
commit eefda851c5
2 changed files with 25 additions and 0 deletions

View File

@ -429,3 +429,26 @@ size_t DatacarrierBytes(const CTransaction& tx, const CCoinsViewCache& view)
return ret;
}
int32_t CalculateExtraTxWeight(const CTransaction& tx, const CCoinsViewCache& view, const unsigned int weight_per_data_byte)
{
int32_t mod_weight{0};
// Add in any extra weight for data bytes
if (weight_per_data_byte > 1) {
for (const CTxIn& txin : tx.vin) {
const CTxOut &utxo = view.AccessCoin(txin.prevout).out;
auto[script, consensus_weight_per_byte] = GetScriptForTransactionInput(utxo.scriptPubKey, txin);
if (weight_per_data_byte > consensus_weight_per_byte) {
mod_weight += script.DatacarrierBytes() * (weight_per_data_byte - consensus_weight_per_byte);
}
}
if (weight_per_data_byte > WITNESS_SCALE_FACTOR) {
for (const CTxOut& txout : tx.vout) {
mod_weight += txout.scriptPubKey.DatacarrierBytes() * (weight_per_data_byte - WITNESS_SCALE_FACTOR);
}
}
}
return mod_weight;
}

View File

@ -195,4 +195,6 @@ std::pair<CScript, unsigned int> GetScriptForTransactionInput(CScript prevScript
size_t DatacarrierBytes(const CTransaction& tx, const CCoinsViewCache& view);
int32_t CalculateExtraTxWeight(const CTransaction& tx, const CCoinsViewCache& view, const unsigned int weight_per_data_byte);
#endif // BITCOIN_POLICY_POLICY_H