Bitcoin Forum

Bitcoin => Development & Technical Discussion => Topic started by: tinkert85 on October 02, 2014, 03:38:53 PM



Title: Why is VARINT not used in serialization of CBlockHeader and other core classes?
Post by: tinkert85 on October 02, 2014, 03:38:53 PM
What is the reason that CVarInt isn't used for serialization in the core classes in bitcoin?

Som examples would be:
CBlockHeader.nVersion
CTransaction.nVersion
CTransaction.nLockTime
COutPoint.n

I understand that CVarInt is only used for the "internal" storage types (CTxInUndo, CDiskBlockIndex and others) but I fail to see why.

Does it have something to do with that the classes that are serialized over network shouldn't use compressed forms, or is there a tradeoff with compressed storage space vs. computational overhead in compressing/decompressing?

Or is it just a legacy thing, that the protocol didn't compress from the start and the format has stuck around since then?





Title: Re: Why is VARINT not used in serialization of CBlockHeader and other core classes?
Post by: CIYAM on October 02, 2014, 03:49:09 PM
Or is it just a legacy thing, that the protocol didn't compress from the start and the format has stuck around since then?

My guess would be that - personally I really dislike "binary" protocols (and the internet mostly doesn't use them) as you just end up with a lot of headaches with serialization.


Title: Re: Why is VARINT not used in serialization of CBlockHeader and other core classes?
Post by: tinkert85 on October 02, 2014, 04:02:29 PM
Thank you for the quick reply!

That is a good reason and a good guess. But, the block format already uses some "byte compressed" integers, notably the size of all vectors, vtx, vin and vout. That format is as far as I understand called compact size and differs some from the CVarInt class.

Below is a snippet from the protocol specification where they explicitly say that the CVarInt only is used for local storage, but doesn't say why.

https://en.bitcoin.it/wiki/Protocol_specification#Variable_length_integer
"Modern BitcoinQT has also CVarInt class which implements even more compact integer for the purpose of local storage (which is incompatible with "CompactSize" described here). CVarInt is not a part of the protocol."

Any ideas regarding the above quote?


Title: Re: Why is VARINT not used in serialization of CBlockHeader and other core classes?
Post by: CIYAM on October 02, 2014, 04:42:27 PM
Any ideas regarding the above quote?

Sorry - I don't know exactly why that might be (endian issues perhaps?).


Title: Re: Why is VARINT not used in serialization of CBlockHeader and other core classes?
Post by: tacotime on October 02, 2014, 05:10:54 PM
My guess is because they make determinism easier in terms of the block header or tx structure. There have been some minor irritations around CryptoNote with using varint in the block headers. For instance, some programmers for miners presumed incoming header payload sizes of 80 bytes, but if the difficulty overflows you end up with 81 bytes. Thus when the header overflowed 80 bytes due to difficulty miners broke.