I've been studying the Bitcoin Core code, and I noticed that it uses a template READWRITE to serialize data structures for storing blocks. But it never seems to actually call "READWRITE(nTxs)", the VARINT (CompactSize) that encodes the number of transactions. It reads the transactions to a vector (primitives/block.h), and it almost seems like it just magically knows how many txs there are. What gives?


The transactions are passed in as a vector. Bitcoin Core's serialization framework understands that all vectors should be serialized with a compact sized unsigned integer preceding the serialization of the data within the vector itself.

  • Just want to add that "a compact sized unsigned integer preceding each object in the vector" might be confusing, as there is only one CompactSize int preceding the entire set. Nov 29 '17 at 7: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.