Building Blockchain from scratch with Go (Part 1)

Tram Ho

Introduce

  • This is a series to build the most basic Blockchain using Go
  • Reference series from EBook “Build a Blockchain from Scratch in Go”
  • [!] I’m also a newbie, so if you have any mistakes, please contribute by commenting

What will you learn from this series?

  • Build a simple p2p system
  • Event-based architecture
  • How the server can connect itself automatically (BTC, ETH, XRP)
  • Golang programming language
  • Basic encryption algorithms.

About Go

  • Go is a new programming language designed and developed by Google.
  • Concise language.
  • Has fast compilation speed. In this series, I will use Golang to code, so anyone who has basic knowledge of golang (syntax, …) can read it.

Also you can see more about Go here: https://go.dev/tour/

Learn about blockchain

  • For users: Blockchain is like a book, recording all transactions, anyone can own an identical copy
  • For programmers: Blockchain is like a distributed database on many nodes, each node contains exactly the same data. Surely all of you have heard or known about Bitcoin (BTC) and right, BTC is a product of blockchain.

image.png

  • Blockchain -> is a chain of concatenated blocks, each block is connected to the previous block via a “hash”.

-> To change data in Blockchain is an extremely difficult thing, almost impossible.

Learn the Genesis block

image.png Each block will have a header containing the information of that block (nonce, timestamp, parent_hash, … ) and will contain transactions (transaction) inside, and the first block of a chain is called Genesis block . Every block chain has a Genesis block

Story begins

Understanding such basic knowledge is enough, now let’s start the story, note that you have to go through all the parts to understand & accompany me through the story. ❤️ .

Repo used in the series: https://github.com/lequocbinh04/the-simple-blockchain

The story of John and his Bar begins:

In the 90s, John was a SQL database boss, he knew how to create databases with a high degree of difficulty, optimize queries, he could get billions of records in just under 0.1ms. But times changed, it was time for him to catch up with innovation and start building Web 3.0 and applying it to his bar, because in the early days John thought that the database didn’t need to be too complicated, so he He decided to just use a simple but effective json file for his bar.

Lesson learned: [🔑] Block chain is a database.

First User

Monday, March 18.

John created his first 1 million tokens.

In blockchain, a token is a unit inside the blockchain database, it can be converted into USD or VND, its conversion rate depends on the supply and demand of the token. Every blockchain has a “Genesis” file. This file is used to distribute the first tokens to early participants of that blockchain.

Let’s get started, let’s create the genesis.json file.

John starts with creating a genesis file, he creates a new file ./database/genesis.json , he defines the database of The Blockchain Bar will have 1 million tokens and all will belong to John:

And of course these tokens must have an accompanying application. So John decided, his bar will now pay with TBB (The blockchain bar) tokens. John will assign an initial price to TBB, to exchange them for USD, VND or other fiat. He then reprinted the menu and pasted it in front of the door:

John also decided to reward himself with 100 TBB per day, as money for a great idea, and to pay for TBB maintenance & upgrades.

Fun fact

The original ETH on the Ethereum network is now also created and distributed to early investors and developers in the same way that John did.

summary

Summary

[🔑] ​​Block chain is a database.

The total token supply, user initial balance, and blockchain settings will be located in the Genesis file.

End

Code of the series: https://github.com/lequocbinh04/the-simple-blockchain

If you find it interesting or have any questions, please leave them in the comment section. Also, please Up vote for me if you find the article really useful, thank you very much.

 

Share the news now

Source : Viblo