Industry Insights

Mining the Blockchain, Part III

2018-09-16  |  12 min read 

In the first two chapters of this series, we explored the core functionality of Blockchain: the ability to create and maintain a secure, distributed, trustless transaction ledger. In this chapter, we examine the work function that is fundamental to securing the ledger, once again using cryptocurrency to familiarize ourselves with the example.   

A Blockchain is secured by finding the solution to a very difficult puzzle. The process by which that solution is found is called “mining.”  Mining is computationally-intensive – potentially requiring thousands or millions of compute hours just to solve one puzzle.  The work is often done by “miners” – individuals or corporations that dedicate their resources to solving the puzzle.  We’ll explore the incentives that motivate that behavior and discover why there is an ever-increasing need for more compute power.  

A Puzzling Situation

Records on the blockchain are made permanent by finding the answer to a very difficult, computationally expensive puzzle.  Each block requires one such puzzle, and each subsequent block is dependent on the previous block’s puzzle, in effect “cementing” the previous block into place. Changing a block further back in time would require recomputing all the block puzzles from that point in time, forward to the current time.  For reference, a common block frequency is 10 minutes – thus every 10 minutes, another very difficult puzzle is solved by harnessing the power of millions of computers around the world.

The difficulty of finding the puzzle solution grows harder as the blockchain network gets bigger. As the number of participating nodes increases and the number of transactions on the system increases, more computational power is required to solve the puzzle.  The puzzle has a self-governing mechanism that automatically increases the difficulty if the number of “solvers,” or “miners,” increases.

Raffle Analogy

It is helpful to think about mining like a raffle.  In a raffle, many people participate, but only one winner is selected from the raffle pool.  In Blockchain, you earn a raffle ticket by doing some computationally difficult work related to a block of transactions.  The result of that work is like the number on your raffle ticket. It might be a winner, or it might not. In the Blockchain work function, the winner of the raffle is the first ticket that happens to be below a certain value.  When a person is issued a winning ticket, they call out to every that they’ve found the winning solution, and everyone rushes around to verify that they are indeed the winner.  It is important to note, only the first personto find a winning ticket gets the reward.  

This begs the question: what is the reward?  In cryptocurrencies, it is typically a number of coins in the currency being protected.  In an arbitrary blockchain, it could be digital currency, rights to physical resources, or any other asset of value.

The Purpose of Mining the Blockchain

In a central ledger, such as our banking system, end users assign trust to the bank to maintain the ledger.  Blockchain is different – it can be “trustless.” That means no one system has authority over the ledger.  A copy of the ledger is kept by all nodes, and massive amounts of compute power are harnessed to generate proof that the distributed ledger hasn’t been tampered with.  Trust in an individual entity is replaced by trust in a strong cryptographic function.  

To keep that trust secure, the entire chain must be hardened against tampering: entries recorded on the Blockchain must be immutable.  Blockchain accomplishes this by securing blocks of transactions with very difficult puzzles, and then linking those blocks together into a chain.  Re-writing any block other than the lead block would require re-computing all the puzzles between the altered block and the lead block. It is this chaining mechanism that makes historic entries in the Blockchain virtually immutable.

Understanding the Incentive

The process of mining is compute intensive.  It generates heat, consumes nearly all system resources on a computer system, and uses substantial amounts of electricity.   So why would anyone mine blocks on a Blockchain?  The answer is the incentive.  When the puzzle for a block is solved, the person that found the solution is awarded the bounty for that block.  In a crypto-currency like Bitcoin, the reward is paid in newly created currency. The individual with the winning solution earns these bounty coins (currently 12.5 BTC), and also all fees that were encoded in the transactions included in that block (0.5 BTC is common). Solving the puzzle is a winner-take-all approach.   When a winner is announced and the solution verified, any clients working on other possible solutions simply discard them.  Any orphaned transactions go back in the transaction pool to be picked up in a future block.

What is the puzzle?

Blockchain puzzles typically are secured by a class of compute challenges called proof-of-work functions.  For example, the computationally difficult puzzle for Bitcoin is a challenge to find a SHA256 hash which is arbitrarily close to zero.  If you have a background in IT security, you may recognize SHA256 – it is the same cryptographic hash function used in enterprise security applications, including SSL certificates.  

If you haven’t brushed up on cryptography, a hashing functionis simply a piece of code which takes an input of arbitrary length and returns an output of fixed length.  A stronghashing function is one which substantially changes the output based on minimal changes in the input, and which generates the widest possible set of outputs with minimal repeats. SHA256 is a strong hashing function.

Here is the actual puzzle used in Bitcoin, written as pseudocode:

<code>

Find a value of {x} such that SHA256(SHA256({x & block header})) < {difficulty}

Where {x} is a 32-bit integer between 0 and 4,294,967,295, called a nonce.

{Difficulty} is a hash value that has a large number of leading zeroes (currently 18 in Bitcoin)

</code>

To gain an appreciation for the difficult of the problem, we will use a highly simplified example:

Using an online SHA256 hash generator, we would search for a hash with a difficulty that it needs to have a leading zero.  We will look for permutations of the phrase “{n}The quick brown fox jumped over the lazy dog.”

<code>

Input: 1The quick brown fox jumped over the lazy dog.

Hash: DCA3EADFC80162A9053516CA309A5ED5BC3E145A64846F72B5FB950545339C3A

Input: 2The quick brown fox jumped over the lazy dog

Hash: 809C94A93820E6CF6FABDFE5CF9613C70BE07A52028AD0E227818249AEA280BE

.

.

Input: 23The quick brown fox jumped over the lazy dog.

Hash: 06A1A6A78111129954EC6B9C3509750E4503C02A039EAF230D368ADE93B8FF61

After 23 permutations, we discover a hash with a single leading zero.  This example is simple because in an actual blockchain search, the target would not be a single zero, but 18 zeros.  Such a hash value might look like this actual hash of bitcoin block #518078 from April 13, 2018:

00000000000000000014508941280216fa874a1210d4f4f4bebc40ef7c4e0ff0

These hashes are discovered by miners using brute force search, incrementing the value of {x} and computing the hashes to see if any generates a winning solution.  When a client has exhausted all values of {x}, it changes the header and tries again.  The volumes are enormous.  The Bitcoin network computes about 30 million million million hashes per second (30 Exahash/sec).  This is the amount of compute power required to provide a solution to this puzzle in around 10 minutes.  

Mining at Scale

Since the dawn of computing, large problems have been divided into smaller problems that can be solved in parallel, and the same approach sits at the core of blockchain networks. The problem space of the work function (or puzzle) can be divided up amongst a pool of clients, with each client attempting to find a solution by starting at a different point in the search space.  

When individuals pool their resources, they must find a way to divide the reward, should one of the pooled clients solve the problem, usually by splitting the reward based on the fractional amount of work contributed versus the total amount of work done to solve one problem.  

Some currencies actually prefer this approach, crafting puzzles that cannot be easily optimized by an ASIC, preventing centralization of large amounts of problem-solving power.  One of the core tenants blockchain is that a distributed system with many small nodes is less likely to be disrupted than a system with a smaller number of higher-performing nodes. 

Blockchain Transparency

It is surely counter-intuitive that the world’s most private and secure currency transfer system is publicly created, publicly readable, and only pseudo-anonymous (there are no names recorded in the blockchain, but the wallet ID of transacting parties is always visible).  Transactions amounts, fees, and wallet IDs of the sending and receiving are plainly readable in a web browser.  You can explore transactions in real-time on the Bitcoin network by visiting http://blockchain.info.

What makes this technology interesting to the industries beyond finance is the versatility of blockchain itself. By changing parameters such as the desired frequency of new blocks, the size of the blocks, or the rewards paid, the outcome can support applications needing high transaction rates, low latency, or untraceable exchanges. Blockchain also applies to smart contracts.  A smart contract adds the concept of conditional logic to the blockchain, allowing transactions to trigger other actions if specific conditions are met. We’ll explore this in a future blog post.

In our next post, we’ll look at Blockchain from an IT networking standpoint, to better understand what Blockchain traffic looks like on the LAN and WAN.