In the bitcoin-book, the "Transaction Counter" part of a block is documented as being 1~9 bytes long instead of a fixed size.
How is this counter parsed from the raw data?
Bitcoin Stack Exchange is a question and answer site for Bitcoin crypto-currency enthusiasts. It only takes a minute to sign up.Sign up to join this community
The idea is the following:
marker < 0xFD
markeris the number
marker == 0xFD
marker == 0xFE
marker == 0xFF
Or, in Python:
def read_varint(byte_array): marker = byte_array if marker < 0xfd: return marker elif marker == 0xfd: return int.from_bytes(byte_array[1:3], 'little') elif marker == 0xfe: return int.from_bytes(byte_array[1:5], 'little') else: return int.from_bytes(byte_array[1:9], 'little')
See also the implementation in Bitcoin Core here.