Bitcoin Forum

Bitcoin => Development & Technical Discussion => Topic started by: piotr_n on July 29, 2011, 06:26:25 PM



Title: Variable length integer - what the hell?
Post by: piotr_n on July 29, 2011, 06:26:25 PM
Variable length integer, from the bitcoin protocol.

It states very clearly in the wiki (https://en.bitcoin.it/wiki/Protocol_Specification#Variable_length_integer):
Quote
<= 0xffff   3   0xfd + uint16_t
Here it makes sense.

But the problem is, when I do getblocks (https://en.bitcoin.it/wiki/Protocol_Specification#getblocks) with hash_stop set to zero, I get back inv (https://en.bitcoin.it/wiki/Protocol_Specification#inv) where the count field is "fd f4 01"
Now, if I parse it according to the spec, it give me value 753 (0xfd + 0x01f4). While in fact the proper value is 500 (0x01f4)

So which one is wrong; the spec or the value inside the message?
Or am I wrong somewhere?


Title: Re: Variable length integer - what the hell?
Post by: realnowhereman on July 29, 2011, 06:37:14 PM
You're wrong I'm afraid.

The way the variable length integer works is:
  • Look at the first byte
  • If that first byte is less than 253, use the byte literally
  • If that first byte is 253, read the next two bytes as a little endian 16-bit number (total bytes read = 3)
  • If that first byte is 254, read the next four bytes as a little endian 32-bit number (total bytes read = 5)
  • If that first byte is 255, read the next eight bytes as a little endian 64-bit number (total bytes read = 9)


Title: Re: Variable length integer - what the hell?
Post by: kokjo on July 29, 2011, 07:41:53 PM
i hope you do read python:
Code:
if ord(bdata[0]) <= 0xfc:
    return ord(bdata[0]), bdata[1:]
    if ord(bdata[0]) == 0xfd:
        return struct.unpack("<xH", bdata[:3])[0], bdata[3:]
    if ord(bdata[0]) == 0xfe:
        return struct.unpack("<xI", bdata[:5])[0], bdata[5:]
    if ord(bdata[0]) == 0xff:
        return struct.unpack("<xQ", bdata[:9])[0], bdata[9:]
the first byte describes how long the integer is.


Title: Re: Variable length integer - what the hell?
Post by: error on July 29, 2011, 08:18:08 PM
You missed the fact that Bitcoin uses little endian on the network. This is probably Bitcoin's biggest design mistake.


Title: Re: Variable length integer - what the hell?
Post by: JoelKatz on July 29, 2011, 08:27:54 PM
You missed the fact that Bitcoin uses little endian on the network. This is probably Bitcoin's biggest design mistake.
Here we see a master of the "praise with faint damn" technique. I bow to you, sir.


Title: Re: Variable length integer - what the hell?
Post by: 2112 on July 29, 2011, 09:49:52 PM
You missed the fact that Bitcoin uses little endian on the network. This is probably Bitcoin's biggest design mistake.
I wouldn't call it biggest, but probably one of the most difficult to rectify. The longer it stays this way the higher are the chances that people enshrine this mistake in the alternative clients. At present time the alternative block layout should be not too hard to implement. So long as it stays this way all the implementations will suffer problems related to those inconsistencies, e.g. ufsminer which was mining on the incorrect endianness.