Skip to content

Commit a612919

Browse files
committed
policy: Add CalculateExtraTxWeight to increase weight for datacarrier bytes
1 parent b27d987 commit a612919

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/policy/policy.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,3 +435,26 @@ size_t DatacarrierBytes(const CTransaction& tx, const CCoinsViewCache& view)
435435

436436
return ret;
437437
}
438+
439+
int32_t CalculateExtraTxWeight(const CTransaction& tx, const CCoinsViewCache& view, const unsigned int weight_per_data_byte)
440+
{
441+
int32_t mod_weight{0};
442+
443+
// Add in any extra weight for data bytes
444+
if (weight_per_data_byte > 1) {
445+
for (const CTxIn& txin : tx.vin) {
446+
const CTxOut &utxo = view.AccessCoin(txin.prevout).out;
447+
auto[script, consensus_weight_per_byte] = GetScriptForTransactionInput(utxo.scriptPubKey, txin);
448+
if (weight_per_data_byte > consensus_weight_per_byte) {
449+
mod_weight += script.DatacarrierBytes() * (weight_per_data_byte - consensus_weight_per_byte);
450+
}
451+
}
452+
if (weight_per_data_byte > WITNESS_SCALE_FACTOR) {
453+
for (const CTxOut& txout : tx.vout) {
454+
mod_weight += txout.scriptPubKey.DatacarrierBytes() * (weight_per_data_byte - WITNESS_SCALE_FACTOR);
455+
}
456+
}
457+
}
458+
459+
return mod_weight;
460+
}

src/policy/policy.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,4 +198,6 @@ std::pair<CScript, unsigned int> GetScriptForTransactionInput(CScript prevScript
198198

199199
size_t DatacarrierBytes(const CTransaction& tx, const CCoinsViewCache& view);
200200

201+
int32_t CalculateExtraTxWeight(const CTransaction& tx, const CCoinsViewCache& view, const unsigned int weight_per_data_byte);
202+
201203
#endif // BITCOIN_POLICY_POLICY_H

0 commit comments

Comments
 (0)