Skip to content

Commit f26a6bf

Browse files
committed
SerializeTransaction
1 parent 0f3964f commit f26a6bf

1 file changed

Lines changed: 9 additions & 14 deletions

File tree

src/primitives/transaction.h

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -255,30 +255,25 @@ void UnserializeTransaction(TxType& tx, Stream& s, const TransactionSerParams& p
255255
template<typename Stream, typename TxType>
256256
void SerializeTransaction(const TxType& tx, Stream& s, const TransactionSerParams& params)
257257
{
258-
const bool fAllowWitness = params.allow_witness;
259-
260258
s << tx.version;
261-
unsigned char flags = 0;
262-
// Consistency check
263-
if (fAllowWitness) {
264-
/* Check whether witnesses need to be serialized. */
265-
if (tx.HasWitness()) {
266-
flags |= 1;
267-
}
268-
}
259+
260+
/* Check whether witnesses need to be serialized. */
261+
const unsigned char flags{static_cast<unsigned char>(params.allow_witness && tx.HasWitness())};
269262
if (flags) {
270263
/* Use extended format in case witnesses are to be serialized. */
271-
std::vector<CTxIn> vinDummy;
272-
s << vinDummy;
264+
WriteCompactSize(s, 0); // empty vector
273265
s << flags;
274266
}
267+
275268
s << tx.vin;
276269
s << tx.vout;
270+
277271
if (flags & 1) {
278-
for (size_t i = 0; i < tx.vin.size(); i++) {
279-
s << tx.vin[i].scriptWitness.stack;
272+
for (const auto& in : tx.vin) {
273+
s << in.scriptWitness.stack;
280274
}
281275
}
276+
282277
s << tx.nLockTime;
283278
}
284279

0 commit comments

Comments
 (0)