Bitcoin Forum
December 03, 2016, 04:43:42 AM *
News: To be able to use the next phase of the beta forum software, please ensure that your email address is correct/functional.
 
   Home   Help Search Donate Login Register  
Pages: [1]
  Print  
Author Topic: Variable length integer - what the hell?  (Read 3160 times)
piotr_n
Legendary
*
Offline Offline

Activity: 1498


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 a bitcoin client written in Go, with some unique features.
PGP fingerprint: AB9E A551 E262 A87A 13BB  9059 1BE7 B545 CDF3 FD0E
1480740222
Hero Member
*
Offline Offline

Posts: 1480740222

View Profile Personal Message (Offline)

Ignore
1480740222
Reply with quote  #2

1480740222
Report to moderator
1480740222
Hero Member
*
Offline Offline

Posts: 1480740222

View Profile Personal Message (Offline)

Ignore
1480740222
Reply with quote  #2

1480740222
Report to moderator
1480740222
Hero Member
*
Offline Offline

Posts: 1480740222

View Profile Personal Message (Offline)

Ignore
1480740222
Reply with quote  #2

1480740222
Report to moderator
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction. Advertise here.
1480740222
Hero Member
*
Offline Offline

Posts: 1480740222

View Profile Personal Message (Offline)

Ignore
1480740222
Reply with quote  #2

1480740222
Report to moderator
1480740222
Hero Member
*
Offline Offline

Posts: 1480740222

View Profile Personal Message (Offline)

Ignore
1480740222
Reply with quote  #2

1480740222
Report to moderator
realnowhereman
Hero Member
*****
Offline Offline

Activity: 504



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

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: 574



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.

15UFyv6kfWgq83Pp3yhXPr8rknv9m6581W
JoelKatz
Legendary
*
Offline Offline

Activity: 1386


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.
1Joe1Katzci1rFcsr9HH7SLuHVnDy2aihZ BM-NBM3FRExVJSJJamV9ccgyWvQfratUHgN
2112
Legendary
*
Offline Offline

Activity: 1708



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:  

Sponsored by , a Bitcoin-accepting VPN.
Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!