In this blog, I will attempt to do this. It is a work in progress, but the results are promising.
First, lets begin with a VERY brief introduction to the relevent portions of bitcoin and bitcoin mining to help understand the rest of the blog. Bitcoin is an online paymeny system, which operates in a completely decentralized manner. Bitcoins, the currency of the bitcoin system, are exchanged for goods and services. A good analogy to bitcoins in the conventional finalcial world is a cheque. Cheques can be used as payment for goods and services. However, the cheque has to be verified to ensure that the payor has sufficient funds to cover the cheque, a process that is performed by the banks. In the bitcoin system, the process of transaction verification (i.e., bitcoin mining) is performed by the bitcoin community (the equivelent of conventional banks, but completely decentralized).
Bitcoin mining, or transaction verification, is performed in chunks called "blocks". Successful verification of a block is rewarded by the bitcoin system with 25 bitcoins paid to the first person providing proof of verification. Each block is formed by collating all the transactions since the previous block, constructing a block header from these transactions, and then altering this header methodically until the SHA256 double hash of this header is less than the currently set "difficulty" target. Ken Shirriff's excellent blog post (http://www.righto.com/2014/02/bitcoin-mining-hard-way-algorithms.html) explains the specifics of this process well and is worth reading carefully.
For the purposes of this blog, here are the take-away: - if a bitcoin block is verified, the verifier gets paid 25 bitcoins (as of this writing) - the SHA256 double hash of the block header has to be below a predefined difficulty target for the verification to be deemed successful - bitcoin miner verify blocks and meet the difficulty threshold to get paid
SHA256 hashing function is a one-way hash function. The only way to know if the hash is below the difficulty target set by the bitcoin network is to alter the input one by one and compare the resulting hash to the target - i.e., by brute force. Since the size of the search space is very very large, a lot of computational power is necessary to be successful at this task.
The block header is an 80 byte string made of 1. version (4 bytes) 2. previous block's hash (32 bytes) 3. Merkle root (32 bytes) 4. timestamp (4 bytes) 5. bits (4 bytes) 6. nonce (4 bytes)
Of these, miners systematically alter the nonce (and timestamp with some constraints), compute the double SHA256 hash for each change, and check if the result is below the target. Merkle root can also be changed (by changing some of the coinbase transaction fields).
The idea is to take the previously solved block headers and learn where the nonce might be. To do this, take each solved header and build the training data table such as this:
----------------------------------------------------------------------- | ver | prev_hash | merkle_root | time | bits | candidate nonce | label| |----------------------------------------------------------------------| | 3 | 46ae7... | 8732ad... | 843..| 1d.. | 00000001 | 0 | | 3 | 46ae7... | 8732ad... | 843..| 1d.. | 00010001 | 0 | | . | ... | ... | .. | .. | . | . | | 3 | 46ae7... | 8732ad... | 843..| 1d.. | 10000001 | 1 | | 3 | 46ae7... | 8732ad... | 843..| 1d.. | 40000001 | 1 | -----------------------------------------------------------------------
The "candidate nonce" and "label" need explaining. The candidate nonces are numbers less than and greater than the true nonce - i.e., candidates that we are testing. The label is encoded as 0 if the candidate nonce is less than the true nonce, 1 if greater.
A machine learning classifier can be trained on this data. Once trained, the classifier will predict 0 if the true nonce of an unknown header is greater than the candidate nonce, and 1 if less. So for any given candidate nonce, this classifier will say if the true nonce is lower or higher.
Data for 49800 solved bitcoin headers is available here (python pickle format. See code below)
import hashlib
import struct
import pandas as pd
import random
import os.path
import pickle
import numpy as np
from sklearn.cross_validation import train_test_split
from sklearn.ensemble import RandomForestClassifier
import json
import matplotlib.pyplot as plt
%matplotlib inline
from matplotlib.legend_handler import HandlerLine2D
# Helper functions with some commenting
#############################################################
# load block header data
# Note: the data has been preprocessed and has been pickled
def load_data(filename):
with open(filename, 'rb') as f:
return pickle.load(f)
# Takes true nonce, returns random nonce value and label indicating
# above/below true nonce
# Used to make the training labels
def get_nonce(n):
r = random.randint(0, 4294967296)
while r == n:
r = random.randint(0, 4294967296)
if r < n:
l = 0
elif r > n:
l = 1
return (r, l)
# Makes a training set from the block header data by adding
# random nonce candidates and label
def make_df(data_dict):
ml_df = []
row = []
hex_int_dict = {'0':0, '1':1, '2':2, '3':3, '4':4, '5':5,
'6':6, '7':7, '8':8, '9':9, 'a': 10,
'b': 11, 'c':12, 'd':13, 'e':14, 'f':15}
for i in data_dict:
try:
header = [str(i['ver'])]
header.extend(list(i['prev_block']))
header.extend(list(i['mrkl_root']))
header.extend(list(str(i['time']).zfill(10)))
header.extend(list(str(i['bits'])))
nonce = int(i['nonce'])
for n in range(150):
rand_test_nonce, label = get_nonce(nonce)
row = list(header)
row = [hex_int_dict[r] for r in row]
row.extend([rand_test_nonce, label, nonce])
ml_df.append(row)
except:
continue
return ml_df
# The machine learning routine - uses a Random Forest classifier
# Runs 20 jobs - change this as necessary based on number of cores
# Warning - this takes time and memory to build the model
# About 5 minutes on a 32 core 32GB machine
# Decrease sample size to reduce memory usage. Accuracy does not seem to suffer.
def train_randomforest(X_train, Y_train):
clf = RandomForestClassifier(n_jobs=20)
clf = clf.fit(X_train, Y_train)
return clf
t = load_data('bitcoinheaders.pkl')
# use only 20000 + 4000 rows instead of the ~49000 - makes the process faster
train = t[0:20000]
test = t[21000:25000]
# Convert it to dataframe in a format that sklearn can use
ml_df = pd.DataFrame(make_df(train))
ml_df_test = pd.DataFrame(make_df(test))
# Split the raw data into test and training sets
X = ml_df.columns[0:148]
Y = ml_df.columns[148]
X_train = ml_df[X]
Y_train = ml_df[Y]
X_test = ml_df_test[X]
Y_test = ml_df_test[Y]
pd.DataFrame(X_train).head()
# Train the classifier on the training data
clf = train_randomforest(X_train, Y_train)
# Print the training set accuracy
print "Training set accuracy : " + str(clf.score(X_train, Y_train))
# Print the test set accuracy
print "Test set accuracy : " + str(clf.score(X_test, Y_test))
# Graph a prediction in relation to the true nonce
# rerun this cell to get another random selection
unique_nonces = ml_df[149].unique()
true_nonce = unique_nonces[random.randint(0, unique_nonces.shape[0])]
df_to_graph = ml_df.loc[ml_df[149] == true_nonce]
p = clf.predict(df_to_graph[X])
plt.scatter(df_to_graph[147], p, label='Candidate nonces')
plt.scatter(true_nonce, 0.5, c='red', label='True nonce for reference')
plt.xlabel('Nonce - 0 to 2^32')
plt.ylabel('Probability(true nonce < selected nonce)' )
plt.legend(loc=2)
clf.feature_importances_
Each byte in the header contributes equally (about 0.004) to the prediction, except for the candidate nonce which contributes a lot more. Makes sense.
As can be seen from the above test set accuracy and the graph, the classifier does a very good job of identifying the nonce. Also, "feature importance" shows that most of the bytes in the header contribute equally to the prediction - an observation that makes intuitive sense. There is a tremendous amount of interraction between the input variables (bytes) in the SHA hashing algorithms. The candidate nonce contributes a lot more, which also makes intuitive sense.
In this blog, I have trained a classifier to search for the nonce. A classifier can obviously also be trained to search for the correct timestamp, or a combination of the two, by changing the data encoding. The next step is to use the classifier on live data from bitcoind and try to mine the next block.
A pot of bitcoins await at the end of the random forest!
Silly bear, you left the labels in your training data. Good name for your blog.
ReplyDeleteThanks for visiting and for your thoughts.
DeleteThe data frame that I am showing in the blog is the entire data frame (ml_df). The model is trained on X_train and Y_train. X_train is the training data without the labels. Y_train has the labels. I double checked after I saw your comment ;-). Also, feature_importance_ would have ranked the label column very highly if it had been left in, which is not the case here.
Hello Arun! Thank you for the thought-provoking experiment. I really appreciate the amount of detail and repeatability you've provided.
ReplyDeleteHave you considered comparing your test set performance to the "naive baseline" that classifies a nonce based on whether its value is greater than or less than the average value of all "true" nonces in your training set?
After more testing, I found an error in the article. The learning algorithm does not learn to localize the nonce. All that it has done is learn the (somewhat skewed) distribution of the nonces in the dataset I used. It uses this information to "guess" the nonce's location. It does not really learn the nonce's location from the header.
ReplyDeleteWell... it was too good to be true anyway ;-)
This comment has been removed by a blog administrator.
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeleteWondering where did you download the blocks data from?
ReplyDeleteMasterfully done.
ReplyDeleteI’m going to read this. I’ll be sure to come back. thanks for sharing. and also This article gives the light in which we can observe the reality. this is very nice one and gives indepth information. thanks for this nice article... bitcoin profit app
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDelete