Repository
What Will I Learn?
- You will learn how to implement the network library into the blockchain
- You will learn how to properly close BadgerDB should your program crash
- You will learn how to truncate the BadgerDB database and remove a Lock file
- You will learn how to create multiple files and directories for multiple instances of a program
- You will learn how to check the diff of a blockchain against another node
- You will learn about block height and how it works to sync multiple blockchains
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:
- 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
Biglibrary: https://golang.org/pkg/math/big/ - Documentation about the
Byteslibrary: https://golang.org/pkg/bytes/
- 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/
Credits/Sources:
- Go Logo: https://golang.org/
Difficulty
- Advanced
Description
In this final tutorial video, we finish up the blockchain and produce a program that is near production level. By combining the network module with the code for our Command line interface, wallet module and blockchain module, this application now is able to simulate a blockchain network in real time on a single local machine. This also includes the creation of multiple databases and wallet files for each instance of the application along with the ability to quickly scan a blockchain for changes. We also look at how we can protect our BadgerDB against corruption should the program terminate prematurely.
Building a Blockchain Network on a Single Machine
In our network module, we've already specified the main way that we want our programs to talk with one another. The RPC or remote procedure calls are passed around via TCP with each instance of the client being represented by localhost machines listening on different ports. Using this structure, we can modify the existing Blockchain, Wallet and Command line interface code to account for multiple different local programs each with their own Node ID. This also means that we are able to run a miner, SPV and a full node all on the same computer.
In the image above are three terminals each running an instance of this blockchain. The top left terminal is connected to the main central node of the application. This node acts as a full node on the blockchain network; it preforms routing and tries to maintain the most consistent copy of the blockchain. The terminal on the bottom left is our wallet or SPV node. This node is responsible for sending transactions to the network. The terminal on the far right is the miner node of the network. When enough transactions are passed from the wallet node to the full node and then to the miner node, the miner node verifies the transactions and then runs the proof of work algorithm to create a new block. This new block is then sent to the main node which then proliferates the changes out to the other two nodes.
Network Simplification
This network system is a simplified version of a production blockchain. The central node acts somewhat like a server which goes against the idea of a decentralized architecture. Also, the SPV node and the miner node contain full copies of the blockchain. This was mainly done because the nodes do not have a message to discover each other on the network.
In a real blockchain, there can be multiple instances of the same chain which have different lengths or heights. In our example, we just take the new blocks and add them to our blockchain unconditionally. To save computing power, our network does not validate the block before adding it to the chain. The image above highlights the main differences between our version and a production level version. Each node routes through the Full Node where as in a real example, each node will communicate with one another independently.
The Source Code for this video may be found here: https://github.com/tensor-programming/golang-blockchain/tree/part_10
Video Tutorial
Curriculum
- 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
- Building a Blockchain with Go - Part 9 - The Network Module