Social media


How to extract transaction data from the bitcoin blockchain using bitcoin-cli?

Author: LB 2021-03-07 179

Bitcoin has been built on a permissionless blockchain, and this means that basically anyone can join its peer-to-peer network. Well, as long as you have 350 Gb of free space and this number keeps growing. If you want to use bitcoin-cli, you have to download the entire bitcoin blockchain and synchronize the transaction data starting from the genesis block. I didn’t have a huge hard drive in my PC, so I decided to compile a bitcoin client on a virtual machine rented from Amazon AWS. I know the bitcoin community dissuades users from using cloud providers to run bitcoin clients, so this guide is strictly for educational purposes. Let’s see some of the difficulties I faced!


You can download the bitcoin core source from github by executing $ git clone https://github.com/bitcoin/bitcoin.git. Enter the downloaded directory with $ cd bitcoin.


You need a few essential software to compile the bitcoin source and you can install these on Ubuntu with the following command: $ sudo apt-get install build-essential libtool autotools-dev automake pkg-config bsdmainutils python3 autoconf libboost-system-dev libboost-filesystem-dev libboost-chrono-dev libboost-program-options-dev libboost-test-dev libboost-thread-dev libboost-iostreams-dev libevent-dev


Now you can run $ ./autogen.sh.


And then $ ./configure.


If you want to generate a bitcoin wallet for yourself, be sure to run configure with wallet support, otherwise wallet functionality will be completely disabled in bitcoin-cli.


If ./configure was successful, you can compile the source code with $ make -j7 on 7 threads. Adjust this number according to the number of cores you have in your machine if you want fast compilation.


The compilation of bitcoin core takes a while, even on a few cores. As soon as it finishes, you can run the bitcoin daemon with $ ./bitcoind -daemon in the background.


This will start the synchronization process and it took a bit less than a day for me to download roughly 350 Gb of blockchain data.


You can check the progress of the blockchain synchronization in your home directory, under .bitcoin. The “debug.log” file will tell you the current blockheader the daemon is synchronizing. You can find the downloaded blockchain under the “blocks” directory.


This is how the synchronization log looks like:




As soon as you synchronized the blockchain, you can use $ ./bitcoin-cli createwallet "[YOUR WALLET NAME]" to create a new bitcoin wallet on your computer and you can use $ ./bitcoin-cli getwalletinfo to query your account balance. Unfortunately, you’re not able to query other wallets’ balance with this client. This is due to the fact that the bitcoin blockchain has been invented to store transaction data and not wallet balances. In order to retrieve the balance of a given address in a wallet, one would have to iterate through the entire chain and check for transaction that modify the balance of the given account. By summing all the incoming transactions and subtracting the outgoing transactions one can calculate the balance of the accounts, but this is a pretty lengthy process.


My goal was to access transaction data stored on the blockchain. By default, the bitcoin daemon downloads the entire blockchain, however it doesn’t build a transaction index, and you cannot search for a specific transaction using its transaction identifier. If you want to enable this functionality, you have to create a “bitcoin.conf” file to configure your client and set txindex=1. After this, make sure to reindex your local blockchain with $ ./bitcoind -rescan.


Now you can use $ ./bitcoin-cli getrawtransaction “[Transaction identifier hash]” true to extract transaction data in JSON format from your local synchronized blockchain.


Illustration: pixabay.com

Interesting entries


06.05.2021

Russian miners are already testing the Nvidia CMP 30HX chip for mining

05.05.2021

Technical details and the ROI of Chia Network Farming

30.04.2021

Investigating monero mining on a notebook

29.04.2021

Optimizing the mining of monero on a multithreaded CPU

28.04.2021

On game theory on cryptocurrency mining with increasing DAG size