Skip to content

Commit

Permalink
backports: Use virtual transaction size in minimum fee calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
lateminer committed Jan 11, 2024
1 parent 2b57dbd commit fbce2fc
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 5 deletions.
14 changes: 13 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3810,7 +3810,7 @@ bool SignBlock(CBlock& block, CWallet& wallet, int64_t& nFees)
// Blackcoin: GetMinFee
CAmount GetMinFee(const CTransaction& tx, unsigned int nTimeTx)
{
size_t nBytes = ::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION);
size_t nBytes = GetVirtualTransactionSize(tx);
return GetMinFee(nBytes, nTimeTx);
}

Expand All @@ -3834,6 +3834,18 @@ CAmount GetMinFee(size_t nBytes, uint32_t nTime)
return nMinFee;
}

unsigned int nBytesPerSigOp = DEFAULT_BYTES_PER_SIGOP;

int64_t GetVirtualTransactionSize(int64_t nWeight, int64_t nSigOpCost)
{
return (std::max(nWeight, nSigOpCost * nBytesPerSigOp) + WITNESS_SCALE_FACTOR - 1) / WITNESS_SCALE_FACTOR;
}

int64_t GetVirtualTransactionSize(const CTransaction& tx, int64_t nSigOpCost)
{
return GetVirtualTransactionSize(GetTransactionWeight(tx), nSigOpCost);
}

static bool AcceptBlockHeader(const CBlockHeader& block, CValidationState& state, const CChainParams& chainparams, CBlockIndex** ppindex=NULL, bool fProofOfStake=true)
{
AssertLockHeld(cs_main);
Expand Down
6 changes: 6 additions & 0 deletions src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,12 @@ bool SignBlock(CBlock& block, CWallet& wallet, int64_t& nFees);
CAmount GetMinFee(const CTransaction& tx, unsigned int nTimeTx);
CAmount GetMinFee(size_t nBytes, uint32_t nTime);

/** Compute the virtual transaction size (weight reinterpreted as bytes). */
int64_t GetVirtualTransactionSize(int64_t nWeight, int64_t nSigOpCost);
int64_t GetVirtualTransactionSize(const CTransaction& tx, int64_t nSigOpCost = 0);

extern unsigned int nBytesPerSigOp;

/** Check a block is completely valid from start to finish (only works on top of our current best block, with cs_main held) */
bool TestBlockValidity(CValidationState& state, const CChainParams& chainparams, const CBlock& block, CBlockIndex* pindexPrev, bool fCheckPOW = true, bool fCheckMerkleRoot = true, bool fCheckSig = true);

Expand Down
2 changes: 0 additions & 2 deletions src/policy/policy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,5 +168,3 @@ bool AreInputsStandard(const CTransaction& tx, const CCoinsViewCache& mapInputs)

return true;
}

unsigned int nBytesPerSigOp = DEFAULT_BYTES_PER_SIGOP;
2 changes: 0 additions & 2 deletions src/policy/policy.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,4 @@ bool IsStandardTx(const CTransaction& tx, std::string& reason);
*/
bool AreInputsStandard(const CTransaction& tx, const CCoinsViewCache& mapInputs);

extern unsigned int nBytesPerSigOp;

#endif // BITCOIN_POLICY_POLICY_H
5 changes: 5 additions & 0 deletions src/primitives/transaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,8 @@ std::string CTransaction::ToString() const
str += " " + vout[i].ToString() + "\n";
return str;
}

int64_t GetTransactionWeight(const CTransaction& tx)
{
return ::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS) * (WITNESS_SCALE_FACTOR -1) + ::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION);
}
7 changes: 7 additions & 0 deletions src/primitives/transaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
#include "serialize.h"
#include "uint256.h"

static const int SERIALIZE_TRANSACTION_NO_WITNESS = 0x40000000;

static const int WITNESS_SCALE_FACTOR = 4;

/** An outpoint - a combination of a transaction hash and an index n into its vout */
class COutPoint
{
Expand Down Expand Up @@ -357,4 +361,7 @@ struct CMutableTransaction
uint256 GetNormalizedHash() const;
};

/** Compute the weight of a transaction, as defined by BIP 141 */
int64_t GetTransactionWeight(const CTransaction &tx);

#endif // BITCOIN_PRIMITIVES_TRANSACTION_H

0 comments on commit fbce2fc

Please sign in to comment.