What Will I Learn?
- You will learn about Nodes
- You will learn how to build a P2P network module
- You will learn about Byte based remote procedure calls
- You will learn how to use TCP in Go
- You will learn how to design a set of clients from scratch
- You will learn about Versions and Block Height
- FreeBSD 10.3 or later
- Linux 2.6.23 or later with glibc
- macOS 10.10 or later
- Windows 7, Server 2008R2 or later
- A little understanding of the Go programming language
- Go installed on your computer
- A text editor or IDE like Gogland (VS Code used)
Resources for Go and this Project:
- Awesome Go Github: https://github.com/avelino/awesome-go
- Golang Installation Page: https://golang.org/dl/
- Golang Home Page: https://golang.org/
- Golang Documentation Page: https://golang.org/doc/
- Documentation about the
- Documentation about the
- Information about Elliptic Curves: http://mathworld.wolfram.com/EllipticCurve.html
- Information about Base58: https://en.bitcoinwiki.org/wiki/Base58
- BadgerDB Documentation: https://godoc.org/github.com/dgraph-io/badger
- MermaidJS: https://mermaidjs.github.io/
- Go Logo: https://golang.org/
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.
Peer to Peer Networking
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.
Using Bytes to Automate Commands
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
- Building a Blockchain with Go - Go Modules and a Basic Blockchain - Part 1
- Building a Blockchain with Go - Refactor and Proof of Work - Part 2
- Building a Blockchain with Go - Persistence and Command Line - Part 3
- Building a Blockchain with Go - Adding Primitive Transactions - Part 4
- Building a Blockchain with Go - Part 5 - Building a Basic Wallet Module
- Building a Blockchain with Go - Part 6 - Adding Digital Signatures
- Building a Blockchain with Go - Part 7 - The UTXO Set and BadgerDB Iterators
- Building a Blockchain with Go - Part 8 - The Merkle Tree