In a previous post you learned how to setup and run Toshi, a Bitcoin node built with Ruby, PostgreSQL and Redis. We walked through how to provision our AWS server, run our Docker containers and access Toshi via the built-in web interface.
As you many notice, it takes a very long time (~two weeks) for the blockchain to sync and for the Toshi database to be fully populated. However, Toshi sees new transactions in the Bitcoin network once it starts running which allows us to start playing with the data soon after it is up and running.
In this post we will walk through a few simple ways to analyze our new blockchain data to gain insights into and uncover some mysteries of the Blockchain.
The analysis here is very rudimentary and is only meant to give you a way to easily get your feet wet.
First we need a way to access our database in the toshi_db Docker container. I’ve found that the easiest way to do that is to setup a new container running psql, the PostgreSQL interactive terminal.
Run the command below, which will take you to the psql command line inside a new container.
Remember to replace my Postgres IP with your own.
Any commands run from here are actually being done IN the psql container. Once we quit psql (\q), the container will be stopped.
Let’s explore the data!
There are 37 tables in Toshi. Let’s find out what some of them have to offer:
Here we queried the first two entries in the addresses table. We can see that Toshi stores the total_received and total_sent values from each address. This allows us to calculate the balance of any bitcoin address with ease, something not possible with bitcoind.
We also queried the count of entries in the addresses table, this value will continually change as your data syncs and new transactions are created in the network.
Another interesting column in this table is address_type, which tells us whether an address is a traditional Pay to PubKey Hash (P2PKH) address or a Pay to Script Hash (P2SH) address. In the addresses table, P2PK addresses have address_type = 0 and P2SH addresses have address_type=1. Querying the amount of P2SH addresses can help us estimate the percentage of Bitcoin users taking advantage of multi-signature addresses. To learn more about different address types, look here.
If we want to learn more about a specific table in the Toshi DB, we can use the same \d command followed by the table name. For example:
This lists the columns, indices and relations of the unspent_outputs table.
Now, let’s look for something a bit more fun – hidden messages in the blockchain.
The op code OP_RETURN allows us to create bitcoin transactions with zero value outputs used to store a string of data. The hexadecimal code forOP_RETUR is \x6a.
Therefore, we can search for OP_RETURN outputs using the following query:
If we convert each of the script hex values to UTF-8 (online tool), we get some interesting results. Most of them are just noise (they may be encrypted), but there are a few decoded values that stand out:
The first result includes the French expression Ça marche, meaning “ok, that works”. The second result includes a URL that leads to the JSON description of a colored coin asset. To learn more about colored coins, check out Coinprism. The third result includes the text DOCPROOF, which indicates that the output was used for proof of existence, allowing a user to cryptographically prove existence of a document at a point in time. The last result looks like somebody just wanted to play around and test out OP_RETURN.
Lastly, if we want to export the results of a query from our container we can copy it to SDOUT and then export it from the container log afterwards.
If we quit psql (\q), we find the name of our previously used psql container (now stopped):
Then we can export the log into a CSV file:
You can now scp this CSV file back to your local machine for further analysis. Note that this file will include all commands and outputs from your psql container. It may require some manual touch-ups. You can always start a new psql container for a fresh log.
So, in just a few minutes we were able to create a new psql Docker container, allowing us to explore blockchain data in ways that are impossible or very difficult to do with bitcoind. We discovered messages that people have left in the blockchain and learned how to export any queries we make into a CSV file. We have only scratched the surface, there are many insights yet to be discovered.
There is a lot of complex analysis that can be done with this rich source of data. For example, if you look at the blockchain as a graph, you can find that you can cluster addresses into “Entities” by looking at any unspent outputs that have been used as inputs into a single transaction and assuming that the addresses owning each of those outputs belong to the same entity. Using only this rudimentary rule to cluster bitcoin addresses, you can begin to make out some interesting behavior and features in the blockchain. Perhaps that is a post best left for another day.