Building Blockchain from scratch with Go (Part 3) – Monolithic Event vs Transaction

Tram Ho

Introduce

In the previous section, we learned the basics of Transaction & DB State. If you haven’t seen it yet, you can check it out.

Github: https://github.com/lequocbinh04/the-simple-blockchain

Continue the story

Tuesday evening, March 26.

On a chilly evening while John was happily stressing out with his favorite game, John accidentally typed rm -rf /. , everything on the computer has disappeared cleanly, including genesis.json and state.json have also been discolored.

John, a senior developer, is extremely angry and yells at the parents of his computer, but he doesn’t panic when he doesn’t have a backup for his computer, because he already has something better: a note the entire transaction. Now his job is to do the transactions again and his db will be restored.

He was very interested in this option, so he decided to develop his MVP database: For each bar activity (e.g. payment of water bill), it must be recorded inside the database. data of the blockchain.

Each customer recorded in the DB is an Account data type:

Each Transaction (TX – data change in DB) will have 4 attributes: from, to, value and data. The data attribute is a value that is the data of that tx (e.g. “reward” represents the transaction that is a reward to John for inventing TBB and increasing the initial total supply of TBB).

Genesis DB is still a json file:

And every transaction, formerly written on paper, he now writes in the file tx.db (one json file each line is the details of tx)

And the most important part, containing the entire business login will be State

State will save all user balances, transaction history, who transfers TBB tokens to whom. State starts by reading init data from genesis.json

After successfully loading the user’s initial balance from genesis.json we will sequentially execute the entire transaction in tx.db

State component is responsible for:

  • Add a new transaction to Mempool
  • Transaction validation (check user balance)
  • Change state
  • Persist state to file
  • Recalculate the entire transaction starting from genesis

Add a new transaction to Mempool :

Persist state to file

Transaction validation (check user balance)

Build a Command-Line-Interface (CLI)

Tuesday evening, March 26

John wanted a way to make it easier to add transactions to the DB and easily retrieve user balances on the system. Since go can build binary files, John decided to build a CLI for easy manipulation.

The easy way to build a CLI with go is to use the library github.com/spf13/cobra . Next he creates a new file cmd/tbb/main.go

He then proceeded to install the CLI

Go will automatically install it in $GOPATH/bin and you can run the tbb command directly in the terminal, but of course there won’t be anything at the moment since the run function in the main.go file is empty.

First John will make version for it, in the same directory as main.go we create version.go

Compile and run

Nude!

Similarly he created the file balances.go

The balance command will print the user’s balance to the screen

Next John tests the balance . command

Delicious! Next John will do the command to interact with the transaction, he creates the file ./cmd/tbb/tx.go

tbb tx add to add a new tx to the db

The command tbb tx add will have 3 required flags --from , --to and --value . CLI is complete!

John in turn added the tx he wrote on the paper

Get balance

The data has been written successfully!

Mempool

The mempool in this series is designed by the author in a very very simple way, for educational purposes. To know more about mempool in real projects, you can find out for yourself at the code of go-ethereum

summary

[🔑] ​​Block chain is a database.

The total token supply, user initial balance, and blockchain settings will be located in the Genesis file. The balance, the initial state of the blockchain recorded in the genesis file is never changed.

Changes to the database are called transactions (Transaction | TX). Transactions are events for actions in the system

Share the news now

Source : Viblo