POW NFT — Intro to Mining

This is an intro to mining written for POW NFT users who want to learn about mining. It explains what mining is in general, how it works, and how POW NFTs can be mined.

What is mining?

Mining is a basic process which is the foundation of all blockchains. It was first described in Satoshi Nakamoto’s Bitcoin whitepaper over a decade ago. Mining is essentially a process of running a relatively simple calculation called a hashing algorithm millions of times, with slightly different data each time, until you get a desired result.

What is a hashing algorithm?

In the simplest terms, a hashing algorithm is a set of instructions (algorithm) that can take any data and can scramble it all up together and spit out some other data that just looks like junk (we call this output a hash, and it isn’t actually junk — it can be quite useful!)

If you put the same data into the same hashing algorithm, you will always get the same hash, but if you change just one character of the original data the hash will be entirely different.

Here are three examples of three different (but very similar) pieces of data all hashed using the keccack256 hashing algorithm. As you can see, changing one character of the input data completely changes the hash.

Input hash



Notably, you also can’t look at a hash and work out what the original data was, it’s mathematically impossible. Think of a hash as a bit like a fingerprint for data — you can’t recreate a person from just their thumbprint, but if you compare a person’s thumbprint to one at a crime scene, you can see if they match.

Hashes are extremely useful and are used in most of the technologies and platforms we use every day. For example, when you create an account on a website/social media app/email provider, they ask you to make a password. Rather than storing this password, they just store the hash of the password. Then when you return to the site and log in with your password, they hash the password you just typed in and compare it with the hash they have stored. This means they know if your password is wrong, but they can’t accidentally leak it to anyone even if they get hacked — pretty clever stuff!

Note: Even though there are letters in the hashes above, these are actually numbers! Coders sometimes use base 16 math, where instead of adding a new digit at ten, we add one at sixteen. It means the above hashes can be written in 64 characters instead of about 78. If you’re wondering, base 16 counting goes 1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10, where 10 = 16 — it’s weird but you don’t have to learn this stuff so don’t worry.

Hashes in proof-of-work mining

Now that you know how hashes work, you might be wondering how this applies to mining. Ethereum, Bitcoin and many other blockchains all use a process called Proof-of-Work. POW mining basically means running a hash over and over again until the number it outputs is below a specific number called the block target.

To make the explanation easier, lets suppose we invented a hashing algorithm called WTFHash. It can take any data as an input and always outputs a number between 0 and 1,000,000. Because of the nature of hashing algorithms, any given hash can equally be any number in this range (we won’t know what number until we hash our data). This means there’s a 50% chance that any given hash will be at or below 500,000. If our block target was 500,000, then it would only take 2 hashes on average to get below the target. Of course, when you hash the same data twice, you always get the same hash. So if we are trying to reach this block target with the data “”, we might get a hash of 600,000. To try again we just add a number called a nonce and try again, “”. If this is also above 500,000 we try again with “” and repeat until it’s below 500,000.
If our block target was 1, there’s a 1/1,000,000 chance of being at or below the block target, which would take on average a million hashes to find. So the smaller the block target, the more hashes on average (and therefore more time) it takes to find a hash below that.

However, computers can run tens of thousands, if not millions of hashes per second. So hashing algorithms used for mining tend to output bigger numbers, like the keccak256 one we saw before. The data that miners hash is basically a combination of four things which together make a block:

  1. All the transactions that people currently want to make*
  2. An extra transaction with the miner’s address, saying “give me all the transaction fees as a prize”
  3. The hash of the previous block
  4. The nonce (the number they add to if the last hash wasn’t small enough)

*If a network is congested then only the transactions with high transaction fees get included, because there’s a limit to how many transactions can be included at once.

Every miner around the world hashes this data over and over, millions of times per second, adding 1 to the nonce each time. They all definitely have a different address as the recipient of the fees (point 2), so their hashes with the same nonce will be completely different. As soon as one of them gets a hash below the block target, their computer broadcasts a message across the network saying they mined the block. They tell everyone the data they used (the four points above), so anyone can make sure they aren’t cheating. It’s only one hash to check, so very easy for a miner to do. If it all checks out, the miners use this new hash as the previous block hash (point 3) and get to work on the next block.

Blockchains are designed to take the same amount of time to mine each block, regardless of how many people are mining (Ethereum has a new block every 20 seconds, Bitcoin has a new one ever 10 minutes). In order to achieve this, the block target automatically adjusts itself. If everyone is mining Ethereum and blocks are being mined every 10 seconds, the block target will get smaller to make it harder. But if suddenly a bunch of miners go offline, and its taking 30 seconds to mine a new block on average, the target will get larger. A larger target is easier to hit!

Mining NFTs

POW NFT is the first NFT ever to require mining to mint tokens. People hoping to get their hands on a new token need to run a hash over and over until they find one bellow the current target. The three things that go into the hash are:

  1. The hash of the previous token
  2. The Ethereum address of the person mining
  3. A nonce

When you successfully find a hash, you just submit a transaction to the NFT’s smart contract with the nonce, it will look at your address, at the previous token’s hash and if that all hashes below the current target, you will be able to mint your new token.

Because the hash includes the address of the miner (point 2), even if someone sees you making the transaction they can’t steal your nonce and cut in line, because their address (and hash) will be completely different.

The hash is also re-hashed with the current time when a token is minted to provide the next hash. This means someone can’t sneakily mine a bunch of tokens at home and then just submit all the transactions at once. The process is completely immune to front-running and can’t be gamed.

Because we are mining NFTs, the block target (in this case referred to as difficulty target) is designed to get smaller and smaller, even if it takes someone a year to mine a token, the difficulty target will never get bigger. This is an intentional scarcity mechanism which makes later tokens harder to mine. Rather than setting a hard cap, the idea is to reach a point where it’s so hard to mine new tokens that everybody stops — it lets the market decide on a cap, like a demand curve of computing power.

POW NFT has a miner built right into the website, connect your wallet with MetaMask (so it knows your address and can see the previous hash and current target on the blockchain), switch it on, and the website will do the rest (ie, hash over and over). Once the miner finds a hash below the current difficulty target, it will let you know and you can submit a transaction and mint your token.

You should be able to mine them in your browser without any hassle, but I’ve also published the algorithm used for mining. If you’re interested but don’t want to skip to the more techy writeup it’s (Solidity):

hash = uint(keccak256(abi.encodePacked(address miner,bytes32 previous_hash ,uint nonce)));

And the difficulty target is

difficulty = BASE_DIFFICULTY / (DIFFICULTY_RAMP**generation);
if(generation > 13){
difficulty /= (tokenId - 2**14 + 1);
}

where

uint BASE_DIFFICULTY = uint(0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff)/uint(300);
uint DIFFICULTY_RAMP = 3;

And generation is (pseudo-code):

generation = floor(log2(tokenId));

The Difficulty Bomb

From the start of generation 14, every time a token is mined the difficulty will increase, asymptotically approaching a level of infinite difficulty. This sets an approximate limit on how many tokens will ever be mined, without forcing hard caps on token supply.

Start mining NFTs in your browser

Codeslinger. Melbourne based Solidity developer for hire.