# Miners, blocks and transactions oh my!

The Bitcoin network’s ingenuity is solving the double spend problem or put another way, creating a monetary system that does not require any third-party (banks) to verify transactions. The system is said to be ‘trustless’. How does the network achieve such a lofty feat? The miners.

On a high-level, miners are computers dedicated to the network to validate all transactions and prohibit any bad actors. As discussed in my last post, users create cryptographically secure transactions and broadcast them to the network of miners. The miners gather up as many transactions as can fit into a block, and go through a mathematical process to verify the block and add it to the chain of past blocks. Miners are rewarded in freshly minted bitcoin for contributing their computing resources to the network.

So what are these ‘math problems’? Let’s break it down.

**A Block and its Hash**

The two main components in a block are the list of transactions and the Block Header. The Block Header consists of the hash of the current block, the hash of the previous block, timestamp of when the current block was hashed, the target difficulty of the block (more on this later), and the nonce (more on this later).

Remember that Bitcoin uses the SHA256 algorithm to ‘hash’ data into a 256-bit number. This means you can put any data into the algorithm and it will spit out a 256-bit number that uniquely identifies that data. If you put your name through the algorithm, you can receive your SHA256 hash ID. However if you change anything about your name (like the spelling or capitalization) and hash it again, you will get a completely different number unrelated to the other hash. You cannot use the SHA256 hash to work backwards and figure out the data behind it, but you can always take the same piece of data and get the same SHA256 hash/256-bit number.

This is the entire concept behind a blockchain. Take two blocks, A and B. First A is added to the network. The miners collect transactions into Block A and hash all of that date to generate a 256-bit number that uniquely identifies Block A. Call it Hash A. Then, miners work to create Block B to add on top of Block A. Miners collect another set of transactions and add Hash A to the Block B. Miners then hash the new set of transactions + Hash A to get Hash B.

Now, if a malicious actor were to go back to Block A’s transactions and change even the smallest detail, then Hash A would completely change, which in turn would change the subsequent Hash B because you just altered the data that Block B hashed. And since it requires a lot of work (computing resources) to create a block and add it to the network, it would require even more work to alter a previous block, say Block A, add that to the network and then recreate Block B with your new Hash A and add that to the network all before the rest of the mining network has moved on to Block C, D, E and beyond.

**The Math Problem**

You can’t just add any hash to a block and add it to the network. In order to be the miner to add the next block, you have to win a competition to find a *correct hash* that solves a difficult math problem. Since there is no way to start with a resulting hash and work backwards to figure out what piece of data gave that hash, the Bitcoin protocol uses this feature to create its difficult math problem. The math problem stipulates that the first miner to produce a hash with a certain amount of leading 0s will be the winner of that block and be able to add it to the network. Remember, a hash is just a 256-bit string of numbers and letters (its actually a hexadecimal number).

93ef6f358fbb998c60802496863052290d4c63735b7fe5bdaac821de96a53a9a

This number can start with any number 0–9 or letter A-F, so finding a hash that starts with say 17 leading 0s would take a lot of work.

**The Nonce**

If we take a look back at our picture of blocks above, we are missing some other data that is also included in the hash calculation, particularly the Nonce. The Nonce is a number added to each block as the variable that miners can continuously change until the miner finds a Nonce that solves the math problem above. Essentially, you can continuously change the Nonce until the SHA256 hash function results in a hash with a certain amount of leading 0s. This has the added benefit that when a miner broadcasts the block to the network, everyone else can take the block and the nonce and perform the SHA256 hash of this data and see that indeed this gives you a hash with the correct number of leading 0s.

**Difficulty**

Inevitably, just like any other competition with a monetary prize, there will be increasing demand attempting to win these precious bitcoin. This means more people will add their computing resources to the network in the hopes that they can choose a Nonce that gives them a hash with the correct leading 0s. The only way to gain an advantage over your competition is to use computing resources that have a higher hash rate — hashes per second — called hashing power. As more hashing power attempts to solve the puzzle, then inevitably the puzzle will be solved faster and faster. This is where the Difficulty or Target comes in. The Bitcoin protocol has the explicit goal to add a block 6 times per hour or once every 10 minutes over the long run. In order to maintain this orderly format, the protocol has a Difficulty that adjusts up or down depending on how quickly blocks are added to the network. Every 2016 blocks (about 2 weeks), the protocol looks back at the last 2016 blocks and measures how long it took to solve them and then adjusts the Difficulty accordingly. The Difficulty is simply how many leading 0s miners will need in their hash to claim a valid block.

**The Block Reward and Fees**

The incentive for dedicating computing resources to the network and continuously expending energy to verify transactions is the block reward and transaction fees. For every block that a miner successfully wins with the appropriate Nonce and corresponding hash, the miner receives bitcoin as compensation. In the beginning, the block reward was 50BTC per block, but every 4 years, the block reward halves until there are no more block rewards. Today the block reward has halved twice, so it is down to 12.5BTC per block.

The second incentive are the fees associated with each transaction. The Bitcoin protocol has specifically hardcoded in that each block can only fit a maximum of 1MB of transaction data. Of course, if demand to use the network increases while the supply of available space stays fixed, then you need a way to prioritize who gets in to each block. This is where the transaction fee comes in. The higher the transaction fee you are willing to pay to fit into a block, the more miners will prioritize adding your transaction to the next block thereby increasing the overall block reward beyond the 12.5BTC.

**Single vs. Pool Mining**

As the network has evolved and the popularity and demand to mine grows over time, it has become nearly impossible for one individual miner to win a block and receive the block reward. The only way to compete today as a single miner is to purchase a lot of hashing power, but this is quite expensive. However, for the hobbyists of the world that still want to mine and be apart of the network, but also want to make some money (or at least cover their costs), they can join a mining pool.

A mining pool is exactly as it sounds, a group of miners who ban together to collectively contribute their computing resources to win blocks. By banning together, the entire pool will win blocks more frequently and then divide the reward amongst the participants of that pool. This allows hobbyists to join a pool and make a little bit of bitcoin everyday over time.

Each member of the pool will act individually attempting to find the correct Nonce and corresponding hash to win the next block. However there is a sub-game going on at the same time in order to track each members contribution to the pool, so that the pool can appropriately pay out the members based on their proportional contribution.

Let’s say the Difficulty for the network is to try and find 17 leading 0s. Well a pool would have a mini version of that same competition amongst the members of the pool but the Difficulty would be much lower say 3 leading 0s. Therefore, every time one of the members finds a Nonce and corresponding hash that has between 3 and 17 leading 0s, that member would receive a share. This share is just proof to the pool that the member is trying to find the real solution to the next block. Once a member of the pool actually finds the solution to the block, the pool can look at every members’ shares and provide them with their proportional contribution of the block reward and fees.

Credit again goes to: