I'm a freshman of bitcoin system and I'm trying to find out how to use bitcoin parser. I downloaded several from internet but they were not work out. Currently I am using Bitcoin Knots wallet and downloaded all blockchains (approx.223GB). I know those data are preserved in the file names blockchain and they are in the form like blk0xxxx.dat. But how to parse those files? I really want to know the answer!
5 Answers
But how to parse those files?
It is not too difficult to parse bitcoin blockchain if you have programming skills and know what you want to do. Have a look to my piece of code - this is just a working example that parsing is easy. Of course, you have to write code in your favorite language for your task
#include <QTimer>
#include "BlockChain.h"
#include "Util.h"
#include "MyByteArray.h"
#include "Target.h"
BlockChain::BlockChain ( QObject* parent ) : QFile ( parent ), blkFile ( START_BLOCK )
{
connect ( this, SIGNAL ( block ( const QByteArray& ) ), parent, SLOT ( block ( const QByteArray& ) ) );
connect ( this, SIGNAL ( doneFile ( ) ), parent, SLOT ( doneFile ( ) ) );
QTimer::singleShot ( 0, this, SLOT ( start ( ) ) );
}
void BlockChain::start ( )
{
setFileName ( blkFileName ( blkFile++ ) );
if ( !open ( QIODevice::ReadOnly ) )
{
_trace ( QString ( "cant open [%1]" ).arg ( fileName ( ) ) )
emit block ( QByteArray ( ) );
deleteLater ( );
}
else
{
_trace ( QString ( "processing [%1]" ).arg ( fileName ( ) ) )
QTimer::singleShot ( 0, this, SLOT ( next ( ) ) );
}
}
void BlockChain::next ( )
{
if ( pos ( ) < size ( ) )
{
quint32 magic;
quint32 sz ( read ( (char*)&magic, 4 ) );
while ( !magic && pos ( ) < size ( ) - 4 )
read ( (char*)&magic, 4 );
xassert ( ( ( magic == MAGIC_ID ) || !magic ) && ( sz == 4 ) )
if ( magic )
{
read ( (char*)&sz, 4 );
emit block ( read ( sz ) );
QTimer::singleShot ( 0, this, SLOT ( next ( ) ) );
return;
}
}
close ( );
emit doneFile ( );
QTimer::singleShot ( 0, this, SLOT ( start ( ) ) );
}
const QString BlockChain::blkFileName ( const int i ) const
{
return
( i < 10 ) ? QString ( DATA_ROOT "\\blk0000%1.dat" ).arg ( i ) :
( i < 100 ) ? QString ( DATA_ROOT "\\blk000%1.dat" ).arg ( i ) :
( i < 1000 ) ? QString ( DATA_ROOT "\\blk00%1.dat" ).arg ( i ) :
QString ( DATA_ROOT "\\blk0%1.dat" ).arg ( i );
}
Python 3 parser available here https://github.com/alecalve/python-bitcoin-blockchain-parser
The easiest way is to use my simple python script (blockchain parser) that can parse whole raw data from the blk*****.dat files into the human readable text view. This script is very simple to understand and it would be the best way to find out what hiding behind the blockchain database.
I want to update the status of the Bitcoin parser projects, one month ago I have released the new Bitcoin parser caller SpyCBlock, it resides to github at this link.
SpyCBlock is a simple parser multicore written with C++11, it parsing the file blk****.dat and produce different serialization, like:
- Serialization JSON Bitcoin blockchain.
- Serialization Graph of transaction.
- Serialization Graph of address (With version Bitcoin core <= 18.1)
You can see the demo graph with this web application.
Also, the very cool stuff is the JSON bitcoin blockchain because you can analyze it easier with your preferer language.
an example, with a Python demo I have to analyze the type of script used in Bitcoin blockchain, this is the result
also, you can see the result here too.
The demo analytics called AnalyticsPyBlock, written in Python3 and reside in Github ad this link https://github.com/vincenzopalazzo/AnalyticsPyBlock
The easiest way to parse the blockchain is to use the built-in JSON-RPC API instead of trying to get data directly from blk*.dat files. The official documentation is available here: https://bitcoin.org/en/developer-reference#bitcoin-core-apis