Building a Blockchain with Go - Part 9 - The Network Module

Words
790
Reading
4 min
8y
Listen to this post
~5 min

golang.jpg

Repository

https://github.com/golang/go

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

Requirements

System Requirements:

Operating System:

  • FreeBSD 10.3 or later
  • Linux 2.6.23 or later with glibc
  • macOS 10.10 or later
  • Windows 7, Server 2008R2 or later

Required Knowledge

  • 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:

Credits/Sources:

Difficulty

  • Advanced

Description

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.

network.png

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.

command.png

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

Video Tutorial

Curriculum

Proof of Work Done

https://github.com/tensor-programming

Building a Blockchain with Go - Part 9 - The Network Module | Ecency