μNEST vision| The Realization of a Minimal Blockchain

μNEST
μNEST
Sep 3, 2018 · 4 min read

A few days ago, I read such an article “Building a Blockchain (1) Basic Prototype with Go” in the WeChat public account “Learning and Learning”, which immediately aroused my interest. That’s why I chose to learn about the Go language and released the basic syntax of the Go language.

In fact, I always wanted to know more about the code of blockchain, but I didn’t have enough time to concentrate on the code of Bitcoin. This time I read this article and found that someone had explained the realization of the blockchain step by step, meanwhile the amount of code is not too much. Then I spent some time to learn.

The content of the article is really awesome, especially for beginners like me, and for code farmers who play with coding as a hobby. They can learn from the skin to deep. However, in the process of learning, I found that for a person who only wants to input two lines of commands to see the results of the operation, this article still lacks some specific experimental steps. Without these steps, it could be an obstacle for most readers, which may lead to loss of interest in further understanding and learning.

This article is a supplement to this series, which can be also regarded as the experimental manual of this series.

Prepare the environment

Preparing the environment is really nothing technical, but it will definitely be a tough barrier for 90% of the beginners. It can be said that one key content of this experimental manual is making preparations for the environment.

Install Go language

I tried to install Go on both two platforms.

Installation on the mac os is relatively simple, just execution a line of command.

brew install go

Installing on linux is a bit of a hassle. For detailed steps, please refer to the link How to Install Go 1.8 on Ubuntu.

(https://tecadmin.net/install-go-on-ubuntu/#)

If you don’t have a Linux environment, you can install a virtual machine instead. For the installation of the Linux virtual machine, please refer to my article — Programming Environment.

(https://www.jianshu.com/p/88fc2e0768d9)

Install dependent libraries

In addition to installing the go runtime environment, the blockchain prototype also requires the use of databases and encryption algorithms. However, the installation method of the dependent libraries is the same on each platform, so there is no need to distinguish it.

go get github.com/boltdb/bolt/…

go get golang.org/x/crypto/…

Because the original text is still being updated, the current environment is based on Part5 branch. Whether the follow-up needs other environments depends on original author’s article.

Download code

Once the environment is ready, you can download the code.

git clone https://github.com/Jeiwan/blockchain_go.git

Construct a project

The run command used in the original text is:

Blockchain_go createblockchain -address

But in fact blockchain_go is compiled, this is not given in the original text. Of course, the author maybe thinks this is too simple to mention it. I add this step.

The command to build is:

Go build -o blockchain_go *.go

If no error message is output and a blockchain_go file is generated in the current directory, the building is successful.

PS: One thing to remember is to rebuild it each time you update the source code.

Start experiments

All the content is ready, you can do some experiments, running to see the behavior of the so-called blockchain. Even you don’t fully understand the code, you can get an idea of what you have done.

Create an address

$./blockchain_go createwallet

Your new address: 159h84bcySKEjCz9PTqVjssWaUcp1qesYG

This way you get the address of a blockchain.

It can be considered as the bank account number of the blockchain world. If you want to buy or sell something, the transaction uses that address.

Create blockchain

$./blockchain_go createblockchain -address 159h84bcySKEjCz9PTqVjssWaUcp1qesYG

00380b95a840e2e1164b0b4859f24993ee95bb9caed5c97f52f759511e01bf54

Done!

A bunch of unreadable numbers and alphabets printed after running the command are the label of a blockchain.

First transfer

In general, blockchain is a public distributed ledger. One of its functions is to transfer ‘money’. Let’s take a look at it.

I have to create another address before the transfer.

$ ./blockchain_go createwallet

Your new address: 1EVnXorM3ZUrxtgavcAgu7y1U77aKo5R4J

Transfer

$./blockchain_go send -from 159h84bcySKEjCz9PTqVjssWaUcp1qesYG -to 1EVnXorM3ZUrxtgavcAgu7y1U77aKo5R4J -amount 3

0a213e7f892d240d2ab0a1d348e42653f92a9639a73bb636ddef9de5de2dc721

Success!

Check the account balance

$./blockchain_go getbalance -address

1EVnXorM3ZUrxtgavcAgu7y1U77aKo5R4J

Balance of ‘1EVnXorM3ZUrxtgavcAgu7y1U77aKo5R4J’: 3

Hey, under the address1EVnXorM3ZUrxtgavcAgu7y1U77aKo5R4J there are three tokens.

Explore the blockchain

$./blockchain_go printchain

============ Block 0a213e7f892d240d2ab0a1d348e42653f92a9639a73bb636ddef9de5de2dc721 ============

Prev. block: 00380b95a840e2e1164b0b4859f24993ee95bb9caed5c97f52f759511e01bf54

Created at : 2017–10–11 15:54:57 +0800 CST

PoW : true

— — Transaction c931a82af89b35613eaf75d7615969b45dd9d977ed588a6964428543c090f7bf:

Input 0:

TXID: ada9b6e85d682827b44968f396fb5878c70cd8d100af498a36fa9a7c8956651f

Out: 0

Signature: b4afe8ff2c7dff5a303ab17d537dc950e1d866b66e29d84ba9cc0854fc51ccf96cbaa32c7d6e4179dff068636f627da518dcccd41359d74b85f8ada45af475f9

PubKey: 3b000f08ad53bbfd4d011939b0199bdf56043c3cca5c73181b9a08c077da863ae337445a0113e3db3311f5afefc45464223d2372d1db8b28f8d6766617ce8f15

Output 0:

Value: 3

Script: 940b4a24d6bd8c474769f7e8382d572c1f20f4bb

Output 1:

Value: 7

Script: 2d8552a22cec6608c383145575884e03ce6b0fe8

============ Block 00380b95a840e2e1164b0b4859f24993ee95bb9caed5c97f52f759511e01bf54 ============

Prev. block:

Created at : 2017–10–11 15:41:32 +0800 CST

PoW : true

— — Transaction ada9b6e85d682827b44968f396fb5878c70cd8d100af498a36fa9a7c8956651f:

Input 0:

TXID:

Out: -1

Signature:

PubKey: 5468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73

Output 0:

Value: 10

Script: 2d8552a22cec6608c383145575884e03ce6b0fe8

Running this command will explore the block on the blockchain and also display information of two transactions. As for the specific meaning, you can refer to the original text.

Adjust the difficulty of PoW

PoW, the proof of work, is an important concept in Bitcoin. In order to coordinate the computing power of the entire network to calculate the block, the difficulty of PoW adjusts constantly. There was only the rational understanding before, now we have the code in hand to get emotional recognition through experiments.

In the proofofwork.go file, there is a variable

Const targetBits = 24

For the time being, it can be considered that this value is the size of the difficulty. The larger the value, the greater the difficulty it is. You can observe the running time of the command by reducing this value, generating a blockchain or transferring ‘money’. The length of time is used to understand the difficulty of PoW.

I hope this article will give you some fun in the world of blockchain.

μNEST

Written by

μNEST