Operating System:
Big library: https://golang.org/pkg/math/big/Bytes library: https://golang.org/pkg/bytes/In the past eight videos, we've built a mostly functional blockchain. We added a set of wallet modules, a transaction system, proof of work system and persistence to the blockchain; however, this is not enough. By definition, a blockchain is a distributed system and so we need to create a network interface which allows for peer to peer access of the chain. Without peer access, the blockchain is not secure nor is it public.
A blockchain network is decentralized by definition. This means that there are no servers nor are their clients that feed off of the servers. Instead, a blockchain's network is made up of various different nodes. The node acts as a server and a client, it takes care of message passing and routing and opens an interface to the blockchain.
This flowchart details and example of what a piece of the blockchain network might look like. There are three primary types of nodes in a typical blockchain. The full node is a node which contains a full copy of the blockchain, it preforms routing and peer discovery, validates blocks mined by miners and verifies transactions. There are also miner nodes; nodes which power and secure the network by running the proof of work algorithm to create new blocks as fast as possible. Finally, there are SPV or simplified payment verification nodes. These nodes do not store a full copy of the blockchain, instead they use a connection to a full node to verify transactions via merkle trees. These SPV nodes are generally used in wallet applications.
Our implementation of this system is fairly simplified. Because we don't have a cluster of computers to simulate the network on, we've got to make do with a single localhost. Each of the nodes in our implementation has a copy of the full blockchain and also can be a miner or a wallet node. The nodes connect to one another via a TCP connection and then communicate with one another by passing byte messages and data back and forth. These remote procedure calls allow the blockchains to sync with one another and properly manage different transactions and block actions depending on what the user does.
The main function of the network module contains a switch statement like the one above. This switch statement allows each of our peers to dispatch messages and commands to one another based on the messages that are being read from their connections. Each of these commands is read in as a slice of bytes and then converted back into a string. The string is matched against a case and a new function deploys another slice of bytes along with the appropriate data. A simple example of this is the version function. This function passes around the current length of each peer's blockchain. The one with the longest length is considered the main blockchain and all of the other nodes download the missing blocks to fill in their own blockchains.
The Source Code for this video may be found here: https://github.com/tensor-programming/golang-blockchain/tree/part_9