defer worksOperating System:
Big library: https://golang.org/pkg/math/big/Bytes library: https://golang.org/pkg/bytes/In this series, we have built a mostly functional blockchain thus far. However, blocks can only be added through hard coding them into the main function. Also, the blocks need to be re-signed upon every execution of the application. This also means that the blockchain can't be shared with other peers which defeats the purpose of the chain. To solve these problems, we need to build a persistence layer and a command line interface. Those are the main subjects of this Go video tutorial.
If you read the original Bitcoin whitepaper, there is nothing specific written regarding the type of database that should be used for the persistence layer. The current implementation of Bitcoin makes use of a low level key-value storage database called LevelDB. LevelDB along with another database derived from LevelDB called RocksDB are both fairly common in the cryptocurrency scene. In this application, we use a native Go Key-Value database called BadgerDB for our persistence layer.
Badger is an appropriate choice because it follows the conventions set up by LevelDB and RocksDB while also adding performance features to the architecture. One of the main design goals of BadgerDB is to write an extremely fast Key-Value database that can span across a very large amount of data. It is able to accomplish this task by using the latest SSD technologies and an ACID transaction technology called Serializable Snapshot Isolation.
With the addition of BadgerDB to our application, we need to refactor the Blockchain Structure from an array of blocks in memory to a pointer to the database and a cursor that signifies where in the chain we are. The BadgerDB API gives us a Database type to use as a pointer. Our current position in the Blockchain can be found using the block hashes. We can save the latest Hash value in memory and in the database for quick recall in a LastHash field.
The Database needs to store the blocks from our blockchain as well as the LastHash key. We have to serialize the Blocks into a slice of bytes before we can push them into the database. A simple serialization function can be written with the "encoding/gob" library. This same library can also be used to reclaim the data and deserialize it back into a block structure. A key called lhis also added to the database so that we can store and retrieve the LastHash.
We are now able to build a command line interface which lets a user access our blockchain. In this tutorial, we add two primary commands to the interface; add and print. Add allows a user to add a new block to the blockchain which also automatically runs the Proof of Work algorithm upon each addition. The Print command allows a user to print out all of the blocks in the blockchain in order from the latest back to the genesis block. This is achieved through the use of of a blockchain iterator struct.
As with most programming languages, Go treats the command line as an array of strings. Every item added to the command line after the initial execution is pushed into this array. We are able to control which items we want by adding flags and parsers to the values. We use a switch statement to determine if a user has typed in print or add and we execute the code based on this outcome.
Also, inside of our main function, we add two defer statements, one to properly close our BadgerDB to stop it from corrupting the data on the disk and one to properly close down the program when a Goexit command is issued. The Go os library can also be used to shut down a program prematurely but there is not guarantee that the defer statements will be honored.
The Source Code for this video may be found here: https://github.com/tensor-programming/golang-blockchain/tree/part_3
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