Bitcoin Mining (Under The Hood) — An Example In Javascript
Intro
Bitcoin mining is a hot, but elusive concept in the software engineering world. As engineers we like to have our finger on just what a type of technology does, even if that means just having the 3000 foot overview of it. In this article I’ll breakdown the key points of Bitcoin mining, define what it means, and provide a simple mining example in Javascript.
Blocks
The underlying technology of Bitcoin and other cryptocurrencies is the blockchain technology. A block is an encrypted snapshot of the state of variables at a given moment. So, in the application of a cryptocurrency, this means a snapshot of balances in all accounts at a given moment.
A Chain of Blocks
If a block is an encrypted snapshot of balances, then a blockchain is a linking of these snapshots. Each snapshot has a pointer to the previous snapshot so we can trace full history of balances all the way back to the first one.
Encrypting in SHA256
To encrypt is to mutate a string of text. This often done for the purposes of security so that a set of information cannot be read. Bitcoin uses a method of encryption called SHA256 to make its data unreadable.
Before: ‘Patrick’
After: ‘2aba5dd8596b2dc0a86cc0fd514986ef09f279ee4cda6416ffa2dc51f2ed047e’
Go to the below link to produce the exact same result:
Moving from One Block to The Next
To create the next Bitcoin block, the snapshot, the previous block is grouped with transactions that have occurred since the previous block was created. This grouping is then hashed (encrypted) with SHA256.
Grouping Before:
{
previousBlock: ‘ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb’,
newTransactions: [
‘3e23e8160039594a33894f6564e1b1348bbd7a0088d42c4acb73eeaed59c009d’,
‘A3a5e715f0cc574a73c3f9bebb6bc24f32ffd5b67b387244c2c909da779a1478’,
‘18ac3e7343f016890c510e93f935261169d9e3f565436429830faf0934f4f8e4’
]
}
Grouping After:
f1bbbfae6397bdfb000c151c2a1a842a26e774354bb6d1109761e885544989b4
That’s it! That’s how we move from one block to the next! Except for one thing…. it’s too fast.
Difficulty Setting and “Mining”
Bitcoin, as an asset, aims to resemble the value stability of gold or other precious metals. To achieve this, it adopts the attributes of these precious metals that deliver this stable value. One attribute of these metals is that they are found over a period of time. To mimic this behavior the Bitcoin network releases a new Bitcoin every 10 minutes to the individual who produces the next block first.
The problem is that encrypting a block only takes a small amount a time, not 10 minutes. To slow down the calculation of the next Block, Bitcoin not only requires the SHA256 to be run, but to be created with a certain number of leading zeros. If an SHA256 hash is produced without the correct leading zeros, the grouping is updated slightly, hashed again, and checked. This happens over, and over, and over, and.. until a hash meeting this criteria is met.
Difficulty: Require one leading zero
No Good: ‘88561a75d1f047f8afa4630c43d82a9e6b08632117f90a8f18091963c9f9ee0b’
Good: ‘098057fbf586fd939a034da9b1175b2f06cb2b261c4879246688647d699ddde8’
If, on average, the blocks are being found too fast the Bitcoin network will increase the difficulty by requiring an additional leading zero. If blocks are being found too slowly the network will decrease the difficulty by requiring one less leading zero.
Example to hash:
{
last_name: ‘Divine’,
first_name: ‘Patrick’,
count: 0,
}
Difficulty 0 (Found on 1st Attempt):
88561a75d1f047f8afa4630c43d82a9e6b08632117f90a8f18091963c9f9ee0b
Difficulty 1 (Found on 33rd Attempt):
098057fbf586fd939a034da9b1175b2f06cb2b261c4879246688647d699ddde8
Difficulty 2 (Found on 231st Attempt):
00ebad5f73665c99422bd4fd160675090fcfa845b59aa04cb13de8c03c8da988
In the above, the count is incremented by one until the hash with the correct number of leading zeroes is found. The more leading zeroes the harder it is. Because the process of finding a hash is more or less random, it is called “mining.”
After A Block Is Found
After an acceptable Block is found, the finder distributes it throughout the system. The receivers can easily validate that the hash is correct by producing it themselves with the finder’s criteria. Once validated the finder is awarded Bitcoin!
Try It Yourself!
Go to the example on JSFiddle and view the hashing in the browser console.
Resources