A fully decentralised blockchain over LAN, built from scratch in python
I set out to create a blockchain from scratch to fully understand the core principles of the technology. The project was a success with the creation of a decentralised peer to peer system without intermediaries. The program incorporated the concept of a proof of work as a consensus mechanism and digital signatures for authentication.
A blockchain is a way to create secure shared memory. The technology enables the creation of a decentralised system. A decentralised system has no central controlling agency and is opposite to conventional currency which are controlled by banks. The blockchain is made up of blocks chained together which are immutable - they can't be changed without an infeasible amount of computation. The main use at the moment of blockchain is in cryptocurrencies where it acts as a secure public ledger for transactions. The number of applications of blockchain is increasing.
When testing the program, I used two laptops over my LAN with known fixed IP addresses. The program has also been tested and works with three devices.
The program does not keep an account balance for the clients, nor do the transactions mean anything currently - they just contain a string with a random number representing the currency transaction. This is not included since it is just a proof of concept and I am not trying to make another bitcoin. The program does not exchange the verification keys in a secure way - this is a toy system.
How are we going to make the devices talk to each other? The solution that I used was sockets using Transmission Control Protocol (TCP). TCP was used in this case over UCP due to retransmission, in-order delivery and greater reliability. It is important that the messages are received to reduce the number of forks - although the slower message sending time over UCP could negatively affect this.
The program is going to have to receive data, send data, mine for keys, generate transaction, organise the blockchain and much more simultaneously. This can be achieved by multi-threading using the threading library in Python. The program uses both generic workers which can complete any task given put on the task queue and specialised worker threads which only complete one task e.g. receiving messages and putting them on the input data queue.
The program was a success and demonstrated the principle of a working blockchain for the use of transactions. This could easily be applied to many other areas with blockchain technology being used for things other than currency e.g. Ethereum's smart contracts.
This project is not scalable due to its communication technique and its restriction to the local area network. However, this was not the point of the project - it was to make a program using the core principles and learn the inner workings of blockchain. In this respect it was a success. Feel free to copy/edit/upgrade my code on Github.
created with
Website Builder Software .