Add sigops adjusted weight calculator
What changed, and why it matters
This commit is a small, clean code refactor. It extracts an existing calculation—taking the larger of a transaction's weight and its sigop-based adjusted weight—into a new reusable helper function, and adds a related utility for converting fee rates. There is no change in behavior or security fix visible in the diff.
No security action required. Treat as routine refactoring; review in normal code-review flow if consuming the new helper elsewhere.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch introduces GetSigOpsAdjustedWeight(int64_t weight, int64_t sigop_cost, unsigned int bytes_per_sigop) in src/policy/policy.cpp and declares it in src/policy/policy.h. It replaces the inline std::max(nWeight, nSigOpCost * bytes_per_sigop) expression inside GetVirtualTransactionSize with a call to this new helper, preserving identical arithmetic. A new inline helper ToFeePerVSize(FeePerWeight) is also added, relying on the newly included util/feefrac.h header. The change is purely structural and exposes existing logic for reuse.
Changed components
src/policy/policy.cppsrc/policy/policy.hInspect captured patch +11 / −1
diff --git a/src/policy/policy.cpp b/src/policy/policy.cpp
index 3da6cb74..b159d616 100644
--- a/src/policy/policy.cpp
+++ b/src/policy/policy.cpp
@@ -373,9 +373,14 @@ bool SpendsNonAnchorWitnessProg(const CTransaction& tx, const CCoinsViewCache& p
return false;
}
+int64_t GetSigOpsAdjustedWeight(int64_t weight, int64_t sigop_cost, unsigned int bytes_per_sigop)
+{
+ return std::max(weight, sigop_cost * bytes_per_sigop);
+}
+
int64_t GetVirtualTransactionSize(int64_t nWeight, int64_t nSigOpCost, unsigned int bytes_per_sigop)
{
- return (std::max(nWeight, nSigOpCost * bytes_per_sigop) + WITNESS_SCALE_FACTOR - 1) / WITNESS_SCALE_FACTOR;
+ return (GetSigOpsAdjustedWeight(nWeight, nSigOpCost, bytes_per_sigop) + WITNESS_SCALE_FACTOR - 1) / WITNESS_SCALE_FACTOR;
}
int64_t GetVirtualTransactionSize(const CTransaction& tx, int64_t nSigOpCost, unsigned int bytes_per_sigop)
diff --git a/src/policy/policy.h b/src/policy/policy.h
index 0131b56b..0e4314ea 100644
--- a/src/policy/policy.h
+++ b/src/policy/policy.h
@@ -11,6 +11,7 @@
#include <primitives/transaction.h>
#include <script/interpreter.h>
#include <script/solver.h>
+#include <util/feefrac.h>
#include <cstdint>
#include <string>
@@ -188,4 +189,8 @@ static inline int64_t GetVirtualTransactionInputSize(const CTxIn& tx)
return GetVirtualTransactionInputSize(tx, 0, 0);
}
+int64_t GetSigOpsAdjustedWeight(int64_t weight, int64_t sigop_cost, unsigned int bytes_per_sigop);
+
+static inline FeePerVSize ToFeePerVSize(FeePerWeight feerate) { return {feerate.fee, (feerate.size + WITNESS_SCALE_FACTOR - 1) / WITNESS_SCALE_FACTOR}; }
+
#endif // BITCOIN_POLICY_POLICY_H
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.