Have you researched blockchain, read a few theoretical explanations, and still couldn’t picture what the blocks look like in code? Or how a block’s hash actually gets mined?
Below is a simple, stripped-down, conceptual implementation of a chain of blocks, meant to complement the theory.
I’ll use C#, but the code is simple enough to port to your language of choice with little effort.
Before we start, the full source code is available in this repository:
Starting from the end
In short, each block in the blockchain contains:
- The data of interest to be signed
- A timestamp
- The hash of the previous block
- A sequential adjustment number for the hash (the nonce)
So adding a block to the chain means finding a new valid hash for it, attaching the data, and linking it to the previous hash.
Here is what the finished program looks like:
1 | static void Main(string[] args) |
Starting the chain
The blockchain starts from the ‘chain‘ object, an instance of the “BlockChain“ class:
1 | BlockChain chain = new BlockChain(difficulty, genesis); |
This class takes the difficulty, set to 2 bytes in this example, and the genesis block, which implements the block structure interface:
1 | //The genesis variable is of type 'IBlock' |
In the BlockChain constructor, a new hash is mined and assigned to the block being created.
Don’t worry about how the mining works just yet.
1 | public BlockChain(byte[] difficulty, IBlock genesis) |
In this example, the ‘BlockChain‘ class is instantiated only once. All subsequent blocks and mining go through the ‘Add‘ method, covered in more detail next.
Adding new blocks to the chain
Once the initial block exists, with the mining difficulty and hash signature defined, all that’s left is to add new blocks to the chain. The ‘Add‘ method handles this and holds the necessary logic.
1 | public void Add(IBlock item) |
Mining hashes
New hashes are mined by the ‘MineHash‘ method. At its core, it’s just a hash of the block’s concatenated information. In this example, we generate the hash with the GenerateHash method:
1 | public static byte[] GenerateHash(this IBlock block) |
The catch is that we’re looking for a hash with a specific prefix. The difficulty we set at the start determines which leading bytes the hash must have, and that’s where the “mining“ comes in.
We generate the hash and check its leading bytes. As long as it doesn’t have the prefix we’re after, we use the ‘Nonce‘ property as a counter: incrementing it changes the block’s signature completely.
We keep incrementing it and regenerating the hash until it has the desired prefix, a brute-force, trial-and-error search.
That’s why a higher difficulty means longer mining times.
1 | public static byte[] MineHash(this IBlock block, byte[] difficulty) |
Checking whether a block is valid
From here on, it’s simple. Each block holds the hash signature of its own contents and also carries the hash of the previous block, which locks the whole chain against tampering.
To validate a single block, just regenerate the hash from its information and compare it with the hash it carries. The same goes for the previous block’s hash.
1 | public static bool IsValid(this IBlock block) |
We can also validate the entire chain like this:
1 | public static bool IsValid(this IEnumerable<IBlock> items) |
Conclusion
Different people learn in different ways. When I built this example myself, I got a much clearer picture of how a blockchain works beyond the theory.
Plenty of problems can be solved with a homegrown implementation of this kind of algorithm, such as validating financial transactions, signing digital documents, and more. This basic model can serve as a starting point for any of them.
References
I based the code on a video series that starts with this one: