Changeset View
Changeset View
Standalone View
Standalone View
src/primitives/transaction.cpp
Show First 20 Lines • Show All 69 Lines • ▼ Show 20 Lines | CTransaction::CTransaction(CMutableTransaction &&tx) | ||||
: nVersion(tx.nVersion), vin(std::move(tx.vin)), vout(std::move(tx.vout)), | : nVersion(tx.nVersion), vin(std::move(tx.vin)), vout(std::move(tx.vout)), | ||||
nLockTime(tx.nLockTime), hash(ComputeHash()) {} | nLockTime(tx.nLockTime), hash(ComputeHash()) {} | ||||
Amount CTransaction::GetValueOut() const { | Amount CTransaction::GetValueOut() const { | ||||
Amount nValueOut(0); | Amount nValueOut(0); | ||||
for (std::vector<CTxOut>::const_iterator it(vout.begin()); it != vout.end(); | for (std::vector<CTxOut>::const_iterator it(vout.begin()); it != vout.end(); | ||||
++it) { | ++it) { | ||||
nValueOut += it->nValue; | nValueOut += it->nValue; | ||||
if (!MoneyRange(it->nValue) || !MoneyRange(nValueOut)) | if (!MoneyRange(it->nValue) || !MoneyRange(nValueOut)) { | ||||
throw std::runtime_error(std::string(__func__) + | throw std::runtime_error(std::string(__func__) + | ||||
": value out of range"); | ": value out of range"); | ||||
} | } | ||||
} | |||||
return nValueOut; | return nValueOut; | ||||
} | } | ||||
double CTransaction::ComputePriority(double dPriorityInputs, | double CTransaction::ComputePriority(double dPriorityInputs, | ||||
unsigned int nTxSize) const { | unsigned int nTxSize) const { | ||||
nTxSize = CalculateModifiedSize(nTxSize); | nTxSize = CalculateModifiedSize(nTxSize); | ||||
if (nTxSize == 0) return 0.0; | if (nTxSize == 0) { | ||||
return 0.0; | |||||
} | |||||
return dPriorityInputs / nTxSize; | return dPriorityInputs / nTxSize; | ||||
} | } | ||||
unsigned int CTransaction::CalculateModifiedSize(unsigned int nTxSize) const { | unsigned int CTransaction::CalculateModifiedSize(unsigned int nTxSize) const { | ||||
// In order to avoid disincentivizing cleaning up the UTXO set we don't | // In order to avoid disincentivizing cleaning up the UTXO set we don't | ||||
// count the constant overhead for each txin and up to 110 bytes of | // count the constant overhead for each txin and up to 110 bytes of | ||||
// scriptSig (which is enough to cover a compressed pubkey p2sh redemption) | // scriptSig (which is enough to cover a compressed pubkey p2sh redemption) | ||||
// for priority. Providing any more cleanup incentive than making additional | // for priority. Providing any more cleanup incentive than making additional | ||||
// inputs free would risk encouraging people to create junk outputs to | // inputs free would risk encouraging people to create junk outputs to | ||||
// redeem later. | // redeem later. | ||||
if (nTxSize == 0) nTxSize = GetTotalSize(); | if (nTxSize == 0) { | ||||
for (std::vector<CTxIn>::const_iterator it(vin.begin()); it != vin.end(); | nTxSize = GetTotalSize(); | ||||
++it) { | } | ||||
for (const auto &nVin : vin) { | |||||
unsigned int offset = | unsigned int offset = | ||||
41U + std::min(110U, (unsigned int)it->scriptSig.size()); | 41U + std::min(110U, (unsigned int)nVin.scriptSig.size()); | ||||
if (nTxSize > offset) nTxSize -= offset; | if (nTxSize > offset) { | ||||
nTxSize -= offset; | |||||
} | |||||
} | } | ||||
return nTxSize; | return nTxSize; | ||||
} | } | ||||
unsigned int CTransaction::GetTotalSize() const { | unsigned int CTransaction::GetTotalSize() const { | ||||
return ::GetSerializeSize(*this, SER_NETWORK, PROTOCOL_VERSION); | return ::GetSerializeSize(*this, SER_NETWORK, PROTOCOL_VERSION); | ||||
} | } | ||||
std::string CTransaction::ToString() const { | std::string CTransaction::ToString() const { | ||||
std::string str; | std::string str; | ||||
str += strprintf("CTransaction(txid=%s, ver=%d, vin.size=%u, vout.size=%u, " | str += strprintf("CTransaction(txid=%s, ver=%d, vin.size=%u, vout.size=%u, " | ||||
"nLockTime=%u)\n", | "nLockTime=%u)\n", | ||||
GetId().ToString().substr(0, 10), nVersion, vin.size(), | GetId().ToString().substr(0, 10), nVersion, vin.size(), | ||||
vout.size(), nLockTime); | vout.size(), nLockTime); | ||||
for (unsigned int i = 0; i < vin.size(); i++) | for (const auto &nVin : vin) { | ||||
str += " " + vin[i].ToString() + "\n"; | str += " " + nVin.ToString() + "\n"; | ||||
for (unsigned int i = 0; i < vout.size(); i++) | } | ||||
str += " " + vout[i].ToString() + "\n"; | for (const auto &nVout : vout) { | ||||
str += " " + nVout.ToString() + "\n"; | |||||
} | |||||
return str; | return str; | ||||
} | } |