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.
- 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
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:
1 2 3 4 5 6 7 8 | <span class="token punctuation">{</span> <span class="token property">"chain_id"</span> <span class="token operator">:</span> <span class="token string">"the-simple-blockchain-ledger"</span> <span class="token punctuation">,</span> <span class="token property">"genesis_time"</span> <span class="token operator">:</span> <span class="token string">"2022-11-10T00:00:00.000000000Z"</span> <span class="token punctuation">,</span> <span class="token property">"balances"</span> <span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token property">"john"</span> <span class="token operator">:</span> <span class="token number">1000000</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> |
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:
1 2 3 4 5 6 7 8 9 | <span class="token number">1</span> TBB token <span class="token operator">=</span> <span class="token number">1</span> € <span class="token operator">|</span> Item <span class="token operator">|</span> Price <span class="token operator">|</span> <span class="token operator">|</span> ------------------------- <span class="token operator">|</span> ------- <span class="token operator">|</span> <span class="token operator">|</span> Vodka shot <span class="token operator">|</span> <span class="token number">1</span> TBB <span class="token operator">|</span> <span class="token operator">|</span> Orange juice <span class="token operator">|</span> <span class="token number">5</span> TBB <span class="token operator">|</span> <span class="token operator">|</span> Burger <span class="token operator">|</span> <span class="token number">2</span> TBB <span class="token operator">|</span> <span class="token operator">|</span> Crystal Head Vodka Bottle <span class="token operator">|</span> <span class="token number">950</span> TBB <span class="token operator">|</span> |
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.