2

I was wondering why the txn_count field in a block exists. The count of transactions could be determined by parsing the txns field in tx format, right? Same for 'tx_in count' and 'tx_out count' in a transaction. The tx_in and tx_out can be just parsed and the count can be determined based on that, right? Is this true? And if it's true, why do the txn_count, tx_in count and tx_out count fields exist?

0
3

I was wondering why the txn_count field in a block exists. The count of transactions could be determined by parsing the txns field in tx format, right?

Yes, it could, but it isn't. Ultimately serialization formats are a convention, and Satoshi picked one when he created the initial software which has persisted in the protocol. It could be changed, but why bother?

The tx_in and tx_out can be just parsed and the count can be determined based on that, right?

That's not quite true - you need some way of conveying at least where the inputs end and the outputs begin (which in the existing protocol is served by the txin count). When deserializing a list of transactions (like in a block message), you also need to know where each transaction ends and the next one begins (which in the existing protocol is served by the txout count).

Is it also used so you can reserve a certain amount of memory before parsing the transactions you think?

You could, but that actually isn't done (at least in Bitcoin Core). The problem is what if you receive a block that claims to contain a billion transactions? Instead, it will allocate buffer space in increments of at most a few MB, so an attacker would need to actually send megabytes of data to get you to allocate that much.

1
  • 1
    Thanks for the clear explanation! – Stijn de Vries Nov 20 '20 at 18:20
3

Serialization is certainly not my area of expertise, but I'd guess: one challenge with serialized data is to know how much data to read. Explicitly providing the count of objects (and their length) you're about to parse is likely simpler and safer than having multiple levels of terminators in a hierarchical data structure and may actually require less data overall.

1
  • Thanks! Is it also used so you can reserve a certain amount of memory before parsing the transactions you think? – Stijn de Vries Nov 20 '20 at 16:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.