Decode blocks received and Store in another file for analyzing

0

I am working on a bitcoin research project. I want to decode receiving blocks from the block chain and store decoded data in a separate file for analyzing purposes.

For this purpose I digs the bitcoin c++ code and find the exact place, which use to write the blocks into blkXXX.dat file.

Could you please suggest a solution for this matter.

Thank you

Rasela Don

Posted 2014-10-27T06:28:34.057

Reputation: 319

Answers

2

#include <QTimer>
#include <QFile>

#include "BlockChain.h"

BlockChain::BlockChain ( const int start, QObject* parent ) : QFile ( parent ), blkFile ( start + 1 )
{
  QTimer::singleShot ( 0, this, SLOT ( start ( ) ) );
}
//--------------------------------------------------------------
void BlockChain::start ( )
{
  setFileName ( blkFileName ( blkFile++ ) );
  if ( !open ( QIODevice::ReadOnly ) )
  {
    _trace ( QString ( "cant open [%1]" ).arg ( fileName ( ) ) );
    getParent ( ).block ( QByteArray ( ) ); // finish signal
    deleteLater ( );
  }
  else
  {
    _trace ( QString ( "processing [%1]" ).arg ( fileName ( ) ) );
    QTimer::singleShot ( 0, this, SLOT ( next ( ) ) );
  }
}
//--------------------------------------------------------------
void BlockChain::next ( )
{
  bool lock ( true );
  if ( pos ( ) < size ( ) )
  {
    quint32 magic;
    quint32 size ( read ( (char*)&magic, 4 ) );
    xassert ( ( ( magic == MAGIC_ID ) || !magic ) && ( size == 4 ) );
    if ( magic )
    {
      read ( (char*)&size, 4 );
      xassert ( size > HEADER_SIZE && size <= MAX_BLOCK_SIZE );
      getParent ( ).block ( read ( size ) ); // process data
      QTimer::singleShot ( 0, this, SLOT ( next ( ) ) );
      return;
    }
    else
      lock = false;
  }
  close ( );
  getParent ( ).doneFile ( lock, blkFile - 1 );
  QTimer::singleShot ( 0, this, SLOT ( start ( ) ) );
}
//--------------------------------------------------------------
const QString BlockChain::blkFileName ( const int i )
{
  return
    ( i < 10 ) ? QString ( DATA_ROOT "\\blk0000%1.dat" ).arg ( i ) :
    ( i < 100 ) ? QString ( DATA_ROOT "\\blk000%1.dat" ).arg ( i ) :
    QString ( DATA_ROOT "\\blk00%1.dat" ).arg ( i );
}

amaclin

Posted 2014-10-27T06:28:34.057

Reputation: 5 763

Thank you very much for take your time to reply me. Actually I am trying to find a way to do the enhancement in C++ core, not the qt-client. Because we dont use qt environment for development.Rasela Don 2014-10-27T08:01:07.417

This is just a working example which shows how to parse existing blk-files and store data somewhere elseamaclin 2014-10-27T08:50:27.247