How do I iterate through the entire Blockchain using RPC?

0

I'd like to iterate through the entire block chain using RPC. The process I intend to do is:

  1. getblockhash 1
  2. getblock (input of #1 here)
  3. iterate through each transaction with GetRawtransaction (input of tx field here)
  4. After getting all transactions, call getblockhash 2 (or goto nextblockhash in block)

I just want to make sure I'm enumerating the chain correctly

goodguys_activate

Posted 2014-03-13T19:23:09.707

Reputation: 11 898

The above seems correct. Most probably you will also want to DecodeRawTransaction after step 3. If performance is a requirement and you are already indexing all transactions (txindex=1) consider parsing your blk*.dat local files which will be way more faster than the RPC based approach. This should help: https://en.bitcoin.it/wiki/Data_directory#blocks_subdirectory

George Kimionis 2014-03-13T20:22:02.917

Answers

2

Your plan is correct (use getrawtransaction <hash> 1 to get JSON instead of HEX :D) but the RPC becomes slow > 60% of the blockchain because there's between 500 and 2k transactions to go through. Make sure txindex=1 is in your config so it has the rawTX data.

As suggested by George parsing the raw files stored will make a very significant difference, to save you a lot of work; this project (by bitpay) already does a that nicely:
https://github.com/bitpay/insight-api

Ruben de Vries

Posted 2014-03-13T19:23:09.707

Reputation: 546

bump for closing this ;)?Ruben de Vries 2014-06-24T07:37:55.623