Bitcoin Forum
May 11, 2024, 07:00:22 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Blockchain parsing  (Read 1815 times)
kerzane (OP)
Newbie
*
Offline Offline

Activity: 16
Merit: 0


View Profile
May 19, 2013, 02:40:00 PM
Last edit: May 19, 2013, 10:42:43 PM by kerzane
 #1


I'm writing a parser for the blockchain for my myself. I've been following the specs at

https://en.bitcoin.it/wiki/Genesis_block
https://en.bitcoin.it/wiki/Protocol_specification
and
https://en.bitcoin.it/wiki/Transactions

I've got to the point where I can follow the chain down to block #29664.

My problem is that I don't know how to infer the length of certain data entries, namely:

 - The number of transactions in a block
 - The number of inputs to a transaction
 - The number of outputs to a transaction
 - The length of the transaction input script
 - The length of the transaction output script

All of these entries are stated to be of length 1-9 bytes in the protocol specs.

But surely we need to have some way of predicting what length to read for these entries, in order to parse the chain successfully.

The (obviously flawed) assumption that they are always 1 byte long, fails at block #29664.

The only solution I can think of is trial and error until the block is parsed successfully (i.e. the magic bytes are correctly found at the start of the next block), but this seems really unsatisfactory.

Anyone know the solution to this problem?
1715454022
Hero Member
*
Offline Offline

Posts: 1715454022

View Profile Personal Message (Offline)

Ignore
1715454022
Reply with quote  #2

1715454022
Report to moderator
1715454022
Hero Member
*
Offline Offline

Posts: 1715454022

View Profile Personal Message (Offline)

Ignore
1715454022
Reply with quote  #2

1715454022
Report to moderator
1715454022
Hero Member
*
Offline Offline

Posts: 1715454022

View Profile Personal Message (Offline)

Ignore
1715454022
Reply with quote  #2

1715454022
Report to moderator
"In a nutshell, the network works like a distributed timestamp server, stamping the first transaction to spend a coin. It takes advantage of the nature of information being easy to spread but hard to stifle." -- Satoshi
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
1715454022
Hero Member
*
Offline Offline

Posts: 1715454022

View Profile Personal Message (Offline)

Ignore
1715454022
Reply with quote  #2

1715454022
Report to moderator
1715454022
Hero Member
*
Offline Offline

Posts: 1715454022

View Profile Personal Message (Offline)

Ignore
1715454022
Reply with quote  #2

1715454022
Report to moderator
1715454022
Hero Member
*
Offline Offline

Posts: 1715454022

View Profile Personal Message (Offline)

Ignore
1715454022
Reply with quote  #2

1715454022
Report to moderator
kerzane (OP)
Newbie
*
Offline Offline

Activity: 16
Merit: 0


View Profile
May 19, 2013, 02:59:20 PM
 #2

Bump, help a man out!
Nikinger
Full Member
***
Offline Offline

Activity: 141
Merit: 100



View Profile
May 19, 2013, 03:36:09 PM
 #3

Do you have taken a look on the source code of at least one random Bitcoin client?

1EwKrY5Bn3T47r4tYqSv6mMQkUyu7hZckV
kerzane (OP)
Newbie
*
Offline Offline

Activity: 16
Merit: 0


View Profile
May 19, 2013, 05:09:31 PM
 #4

Do you have taken a look on the source code of at least one random Bitcoin client?

Thanks for your reply!

I haven't, but I don't know what I would be looking for. I know what is in the data, I just don't know how long those fields are in any given block. I don't think a client code would help me.

I could have a look at another parser, but the whole reason I decided to write my own was because I couldn't find a clear simple one, and up to now it's been quite straightforward.

Any more help is welcome!
penguinn
Newbie
*
Offline Offline

Activity: 14
Merit: 0



View Profile
May 19, 2013, 06:37:50 PM
 #5

I want more!
No 1
Full Member
***
Offline Offline

Activity: 140
Merit: 100


Ad Infinitum Et Ultra


View Profile
May 19, 2013, 06:51:52 PM
 #6

looks interesting. ill be watching this

12wqXQuExLnWoWWQy7j35hzBEW91bUz1YS
LcbBQ5oXtTjyKK4V8iaDqgUAAtahv9nsHR
Zeilap
Full Member
***
Offline Offline

Activity: 154
Merit: 100


View Profile
May 19, 2013, 11:08:56 PM
Last edit: May 19, 2013, 11:21:48 PM by Zeilap
 #7

Anyone know the solution to this problem?

https://en.bitcoin.it/wiki/Protocol_specification#Variable_length_integer

Read the first byte as uint8 and check its value, you have 4 choices,

<0xFD:          use the value as it is
  0xFD:           read 2 more bytes as uint16
  0xFE:           read 4 more bytes as uint32
  0xFF:           read 8 more bytes as uint64
walf_man
Member
**
Offline Offline

Activity: 112
Merit: 10


View Profile
May 20, 2013, 07:09:23 AM
 #8

nice I need more
kerzane (OP)
Newbie
*
Offline Offline

Activity: 16
Merit: 0


View Profile
May 20, 2013, 12:27:00 PM
 #9

Anyone know the solution to this problem?

https://en.bitcoin.it/wiki/Protocol_specification#Variable_length_integer

Read the first byte as uint8 and check its value, you have 4 choices,

<0xFD:          use the value as it is
  0xFD:           read 2 more bytes as uint16
  0xFE:           read 4 more bytes as uint32
  0xFF:           read 8 more bytes as uint64


Thanks Zeilap, I think I follow, I hadn't found that entry in the specification before, I'll try it out when I get a chance.
kerzane (OP)
Newbie
*
Offline Offline

Activity: 16
Merit: 0


View Profile
May 20, 2013, 01:41:37 PM
 #10

Like a charm, cheers zeilap. I haz blockchain!!
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!