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

in #utopian-io5 years ago

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

Sort:  

Hello @tensor,

Thank you for doing another great video on this series.

I like your flow charts as it is easy to follow.

I am amazed at the way you are able to continually lecture for 30 minutes non-stop showing the codes on the different commands. You are very knowledgeable and explain them very well.

I look forward to the next video where you will show us how to put all this together in action.

Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.


Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]

Thank you for moderating my contribution.

I do try to keep my tutorials shorter but unfortunately, there was no way to cut this one down. As a side note, it went from a 3 hour long live code session to a 30 minute tutorial haha.

Thank you for your review, @rosatravels! Keep up the good work!

Hi, @tensor!

You just got a 7.72% upvote from SteemPlus!
To get higher upvotes, earn more SteemPlus Points (SPP). On your Steemit wallet, check your SPP balance and click on "How to earn SPP?" to find out all the ways to earn.
If you're not using SteemPlus yet, please check our last posts in here to see the many ways in which SteemPlus can improve your Steem experience on Steemit and Busy.

Hi @tensor!

Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your post is eligible for our upvote, thanks to our collaboration with @utopian-io!
Feel free to join our @steem-ua Discord server

Hey, @tensor!

Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!

Get higher incentives and support Utopian.io!
Simply set @utopian.pay as a 5% (or higher) payout beneficiary on your contribution post (via SteemPlus or Steeditor).

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!

Coin Marketplace

STEEM 0.30
TRX 0.11
JST 0.033
BTC 64106.00
ETH 3129.71
USDT 1.00
SBD 4.16