Search code examples
blockchainbitcoin

How to read Blockchain Data ( .dat and .ldb files )?


Bitcoin Core downloads two directories : /blocks and /chainstate.

/block have a .dat file , /chainstate have a .ldb file.

How can i read these files?* I need to have info about all transactions and address amount.

  1. address 1 -> amount -> address 2

  2. address 3 : amount3

Other info in block don't interest me.


*API blockchain.info or other API don't intrest me.


Solution

  • One possible way is to use the Bitcoin RPC API:

    1. Configure the Bitcoin Core daemon by going to your Bitcoin Core app > Options > Open Configuration File.
    2. Inside the bitcoin.conf file, write (and change your_user and your_password):
    server=1
    rpcuser=your_user
    rpcpassword=your_password
    txindex=1
    
    1. Close and save the file.
    2. Reboot the Bitcoin Core app.

    Now you are able to use one API such as https://github.com/jgarzik/python-bitcoinrpc.

    To get block transaction hashes, you can use the command getblock.

    For example:

    rpc_connection = AuthServiceProxy("http://%s:%s@127.0.0.1:8332" % ("your_user", "your_password"), timeout=100000)
    block = rpc_connection.getblock("00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09")
    

    To get "all" the information about one transaction (addresses, fees, etc.), you can use getrawtransaction.

    For example:

    raw_transaction = rpc_connection.getrawtransaction("717722008a4e134f78910b37e12dd2cf6301cb8bd2d6278d3086035bfafd4e80", 2)
    

    For more information on how to use the Bitcoin RPC API, please visit https://developer.bitcoin.org/reference/rpc/index.html