Building a simple blockchain in a P2P network
In Chapter 3, Cryptography in Blockchain, we explored how a consensus can be achieved in a decentralized network with the help of algorithms such as proof-of-work. Since consensus algorithms ensure that the Byzantine failure problem can be solved, a global truth can be maintained in a decentralized network in which there is no trust between the peers. Although consensus algorithms provide a convenient way to maintain a public ledger, each node has to perform a set of operations to maintain the ledger in a distributed network.
We have already created a simple blockchain application that can continuously enlarge its records whenever we have new data to be inserted. Because our blockchain application was deployed and the blocks were created in a single system, we have not yet added any mechanisms to validate the blocks. But when we deploy the blockchain as a public ledger, blocks need to be verified at each node whenever they arrive from the node's peers.
Each node has to perform the following processes in order to achieve a consensus in the network:
- Validate each incoming block for integrity so that it can be appended to the local blockchain
- Select the longest valid chain published by a peer in the decentralized network
- Create a valid block whenever there is some data that needs to be inserted into the public ledger
We're going to build a simple blockchain application that deploys the blockchain in a P2P network. This application is a reference implementation that will help us to understand the decentralization of the blockchain network. Although this application performs network discovery, block synchronization, and block validation, it doesn't follow all the required protocols for simplicity.
Let's now explore a few of the design considerations we need to think about in order to build the application before we dive into its implementation. The following sections cover validation, block synchronization, and the basic interface design used in this application.