Bitcoin Forum
November 03, 2024, 04:25:01 PM *
News: Latest Bitcoin Core release: 28.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Variable length integer - what the hell?  (Read 3506 times)
piotr_n (OP)
Legendary
*
Offline Offline

Activity: 2055
Merit: 1359


aka tonikt


View Profile WWW
July 29, 2011, 06:26:25 PM
 #1

Variable length integer, from the bitcoin protocol.

It states very clearly in the wiki:
Quote
<= 0xffff   3   0xfd + uint16_t
Here it makes sense.

But the problem is, when I do getblocks with hash_stop set to zero, I get back 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?

Check out gocoin - my original project of full bitcoin node & cold wallet written in Go.
PGP fingerprint: AB9E A551 E262 A87A 13BB  9059 1BE7 B545 CDF3 FD0E
realnowhereman
Hero Member
*****
Offline Offline

Activity: 504
Merit: 502



View Profile
July 29, 2011, 06:37:14 PM
 #2

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)

1AAZ4xBHbiCr96nsZJ8jtPkSzsg1CqhwDa
kokjo
Legendary
*
Offline Offline

Activity: 1050
Merit: 1000

You are WRONG!


View Profile
July 29, 2011, 07:41:53 PM
 #3

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.

"The whole problem with the world is that fools and fanatics are always so certain of themselves and wiser people so full of doubts." -Bertrand Russell
error
Hero Member
*****
Offline Offline

Activity: 588
Merit: 500



View Profile
July 29, 2011, 08:18:08 PM
 #4

You missed the fact that Bitcoin uses little endian on the network. This is probably Bitcoin's biggest design mistake.

3KzNGwzRZ6SimWuFAgh4TnXzHpruHMZmV8
JoelKatz
Legendary
*
Offline Offline

Activity: 1596
Merit: 1012


Democracy is vulnerable to a 51% attack.


View Profile WWW
July 29, 2011, 08:27:54 PM
 #5

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.

I am an employee of Ripple. Follow me on Twitter @JoelKatz
1Joe1Katzci1rFcsr9HH7SLuHVnDy2aihZ BM-NBM3FRExVJSJJamV9ccgyWvQfratUHgN
2112
Legendary
*
Offline Offline

Activity: 2128
Merit: 1073



View Profile
July 29, 2011, 09:49:52 PM
 #6

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.

Please comment, critique, criticize or ridicule BIP 2112: https://bitcointalk.org/index.php?topic=54382.0
Long-term mining prognosis: https://bitcointalk.org/index.php?topic=91101.0
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!