How to build a custom .NET core blockchain and PoW miner
Overview
My curiosity around all things crypto has been gaining some momentum recently. I’ve always had what I would describe as a surface level understanding of how things work, but I’ve never sat down to codify my thoughts - until today.
If you’re new to blockchain or also learning, maybe I can explain it in a way that resonates with how you think.
Please note, I am still learning, and this is very much a work in progress.
Therefore, there is a chance that some of my assumptions might not be correct, and I fully expect my thoughts, opinions and understanding to iterate and evolve.
The Blockchain
The blockchain aspect of what we broadly refer to as crypto is only one component of many components layered together to form a functional system.
For this project, I’ve only dealt with a some of the components.
- A basic blockchain
- A simple miner using a proof of work algorithm
I have not implemented
- Adaptive proof of work difficulty
- A decentralised peer to peer network
- A decentralised consensus mechanism
What is a Blockchain
In its most basic form, a blockchain is a collection of data blocks linked to the previous block using a cryptographic signature - thus, forming a verifiable chain of blocks.
This signature is called a hash
The Hash
The cryptographic signature for each block uses the previous block’s hash to generate its own hash. (among other inputs)
This hash signature chain means that if you try to modify any block in the chain, you automatically invalidate all successive blocks.
My mental model for this is like a linked list, but with added security and verifiable integrity.
The Miner
The miner is a program that adds new blocks to the blockchain.
In the real world of crypto, the miners compete to win the right to add the next block to the chain.
To Win
this right, the miners need to solve a cryptographic puzzle. The result of this puzzle is the SHA256 hash.
This hash is called the proof of work
or PoW
.
As more miners join the network, the overall network ability to solve the cryptographic hash becomes more likely.
The measure of this collective ability is called the network hash rate
.
Adaptive Hash Difficulty
The network adapts to the hash rate by varying the difficulty of the acceptable hash result.
The difficulty is varied by increasing or decreasing the number of zero’s at the start of the hash value.
More leading zero’s mean the miner has to iterate over many more hash variations to find a hash that meets the difficulty criteria.
Code Preview
I don’t think its going to be helpful to include all the code in this post, but here are some previews.
You can access the completed code here
The blockchain definition
|
|
The hasher
|
|
The miner (part)
|
|
The Example Solution
Requirements
- .NET 6 rc1 or above
The example I’ve created has the following components
- An ASP.NET 6 web API application
- Host the blockchain instance
- Enables interactions from the miner
- A miner console application
- Solves the hash algorithm based on the difficulty
- Submits the new block to the blockchain
To run the application
- Clone the Github repository
- Start up the web application
- Run the miner console application.
Running the web application will also open a swagger interface for you to interact with if you want.