Skip to content

Commit 806b9e7

Browse files
committed
Merge #8332: semi trivial: clarify witness branches in transaction.h serialization
e37b16a transaction: clarify witness branches (Daniel Cousens)
2 parents 30a87c0 + e37b16a commit 806b9e7

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

src/primitives/transaction.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,8 @@ struct CMutableTransaction;
290290
*/
291291
template<typename Stream, typename Operation, typename TxType>
292292
inline void SerializeTransaction(TxType& tx, Stream& s, Operation ser_action, int nType, int nVersion) {
293+
const bool fAllowWitness = !(nVersion & SERIALIZE_TRANSACTION_NO_WITNESS);
294+
293295
READWRITE(*const_cast<int32_t*>(&tx.nVersion));
294296
unsigned char flags = 0;
295297
if (ser_action.ForRead()) {
@@ -298,7 +300,7 @@ inline void SerializeTransaction(TxType& tx, Stream& s, Operation ser_action, in
298300
const_cast<CTxWitness*>(&tx.wit)->SetNull();
299301
/* Try to read the vin. In case the dummy is there, this will be read as an empty vector. */
300302
READWRITE(*const_cast<std::vector<CTxIn>*>(&tx.vin));
301-
if (tx.vin.size() == 0 && !(nVersion & SERIALIZE_TRANSACTION_NO_WITNESS)) {
303+
if (tx.vin.size() == 0 && fAllowWitness) {
302304
/* We read a dummy or an empty vin. */
303305
READWRITE(flags);
304306
if (flags != 0) {
@@ -309,7 +311,7 @@ inline void SerializeTransaction(TxType& tx, Stream& s, Operation ser_action, in
309311
/* We read a non-empty vin. Assume a normal vout follows. */
310312
READWRITE(*const_cast<std::vector<CTxOut>*>(&tx.vout));
311313
}
312-
if ((flags & 1) && !(nVersion & SERIALIZE_TRANSACTION_NO_WITNESS)) {
314+
if ((flags & 1) && fAllowWitness) {
313315
/* The witness flag is present, and we support witnesses. */
314316
flags ^= 1;
315317
const_cast<CTxWitness*>(&tx.wit)->vtxinwit.resize(tx.vin.size());
@@ -322,7 +324,7 @@ inline void SerializeTransaction(TxType& tx, Stream& s, Operation ser_action, in
322324
} else {
323325
// Consistency check
324326
assert(tx.wit.vtxinwit.size() <= tx.vin.size());
325-
if (!(nVersion & SERIALIZE_TRANSACTION_NO_WITNESS)) {
327+
if (fAllowWitness) {
326328
/* Check whether witnesses need to be serialized. */
327329
if (!tx.wit.IsNull()) {
328330
flags |= 1;

0 commit comments

Comments
 (0)