Is It Hard to Build a Blockchain From Scratch?
Episode 1
In Golang
Website : https://open-blockchain-project.com/
Improved code (Frontend+backend) : https://github.com/ndaysinaiK/baby-blockchain
What is blockchain? is it hard to build one? where to start from? which programming language to use?
Trust me I got these questions quite often when meeting people who are interested in blockchain technology. You may also be part of them, don’t worry I was too.
At some point, it became tiring for me to answer the same questions repeatedly to every single person. Thus, I decided to start a series of articles so I can just share the link. Easy right?😊.
There are plenty of blockchain resources online, but it’s overwhelming and frustrated to understand them as a newbie to this booming technology. however, this article is a little different.
In this article, I will take a baby step with you, and you will understand the basic concepts of blockchain and how to program one using Python, Javascript, or Go (Golang).
I chose these languages because most people are familiar with them, especially JavaScript and python. But for speed, endurance, and security, most blockchain core engines are built in c/c++ (Bitcoin, EOS, etc.…), Go (Hyperledger Fabric, Ethereum), Java (Ethereum), Rust, Haskell (Cardano), and/or Ruby (Ethereum) then provide bindings to other easy-to-use programming languages.
Also, some blockchain engines combine many programming languages for robustness and easy-to-use for developers, Ethereum is the best example.
Prerequisites:
- Network
- Cryptography
- Data structure and algorithms
- Decentralized systems
- Javascript/ Go / Python
You only need basic concepts to program your first blockchain prototype, so let’s begin with some theories.
- What is Blockchain?
You can’t program something you don’t understand, right?. Let’s break things down. Blockchain is not Bitcoin, blockchain is not a digital currency, blockchain is a set of different technologies that already existed before.
So is this a blockchain man?
No, bear with me😁.
Let’s simplify things by giving an example since engineers understand better with figures.
Let’s take a MySQL database that stores some information in different tables.
With the above database:
- We can do some CRUD (Create, Retrieve, Update and Delete) operations,
- We can store the same information twice,
- We can drop the entire database ( mistakenly or intentionally),
- We cannot share sensitive information with others,
- The database is centralized ( Single point of failure, security issue),
- There is no way to trust everything that is stored in it.
- The database can be compromised by ill-intentioned people
- The database needs a manager ( He/She can alter or compromise the data)
- The users don’t have power over their own data
- and more…
So why if we need something different, transparent, reliable, independent from people, automatic, immutable, decentralized, and indestructible? that’s where blockchain kicks in.
A blockchain is a secure, trusted decentralized database and network all in one😎.
“Truth can only be found in one place: the code. ”
In other words, a blockchain is a chain of blocks, blocks are like tables in the database but they cannot be deleted or updated, blocks contain information (Transactions, nonce, target bit, difficulty, timestamp, previous block hash, current block hash, Markle tree, block ID, etc…). the blocks are cryptographically verified and chained up to form an immutable chain of blocks called a blockchain or a ledger.
The same chain is then distributed to all the nodes (computers or miners) across the network via a P2P network.
So, instead of a centralized database, all the transactions (data) that are shared across the nodes are contained in blocks, which are chained together to create the ledger. This ledger represents all the data in the blockchain. All the data in the ledger is secured by cryptographic hashing and digital signature and validated by a consensus algorithm. Nodes on the network participate to ensure that all copies of the data distributed across the network are the same.
5 key concepts to remember in the blockchain ecosystem:
- Cryptographic hash and digital signature
- Immutable ledger
- P2P network
- Consensus algorithm ( PoW, PoS, PBFT, ETc…)
- Block validation ( Mining, Forging, etc…)
I will explain these concepts more in detail as we go.
Benefits of using blockchain:
- Remove intermediary organizations
- Immutable ledger
- Transparency
- Security
- Reliability
- More benefits …
When to use blockchain?
blockchain is not a silver bullet, so use it when :
- The data stored cannot be modified ( proof of existence )
- The data cannot be denied by its owner ( non-repudiation)
- You want decentralization
- You want one source of truth
- You want high security
- You don’t care about speed ( eg. bitcoin takes 10 min on average to validate a block of transactions). But some blockchains are faster because they use different consensus algorithms other than PoW. We will talk about this later.
Blockchain use cases
blockchain goes beyond cryptocurrency and bitcoin, blockchain can be used in different sectors:
- Real estate: Land ownership
- Healthcare: Record securely the patient’s data
- Finance: Reduce the taxes and intermediaries, anti-money laundering, cross-border payment
- Supply chain: Track articles from the vendors to the customers ( Authenticity, original content creation)
- Cybersecurity: DDOS attacks
- Giving the power back to the user: Own your data and share it securely with whom you want ( DID)
- Crypto-currency
- Voting mechanism
Blockchain platforms and applications
- Bitcoin
- Ethereum
- Hyperledger Fabric
- EOS
- Chainlink
- Cardano
- Dogecoin ( meme coin 😁)
- Etc.
Blockchain types
There are 3 types :
- Private: Use only internally and when we know better the users (eg. Hyperledger Fabric)
- Public: Everyone can see what is going on (Bitcoin, Ethereum)
- Hybrid: In case you want to combine the first two.
“Talk is cheap. Show me the code.”😄
There are two ways to build a blockchain
2. How to create one? :
- The easiest way is to use pre-built open-sourced blockchains like Ethereum ( Create Distributed applications, altcoins, DEFI, NFT, etc.), Fabric (Configure a private blockchain), EOS, and Cardano, to mention a few. you just have to deploy decentralized applications and enjoy the blockchain features underneath. Using this method, you don’t have to deal with the blockchain’s core engine which is a pain to implement.
- If none of the existing blockchains do fit your requirements then build one from scratch or fork and improve an existing blockchain open-source code. For example, Litecoin and Bitcoin cash were forked from Bitcoin. This last method is tougher, time-consuming, and requires a lot of work and a strong team.
However, in this first article, We will build a blockchain prototype from scratch so that you can thoroughly understand the blockchain’s state machine.
3. How to build one from scratch?
Ok, let’s now create the first baby blockchain in three different programming languages, Go, Python, and Javascript. These prototypes can help you understand the concepts We described earlier.
First, We will create a block, second, We will add the data (header and body) to it, third, We will hash the block, and last but not least We will chain all the blocks up.
A block contains information mentioned earlier, but to simplify things We are going to remove some. Let’s delve into the specifics!
In Go
Hope you’re familiar with Go programming as I mentioned earlier, if not try to learn the basics: functions, methods, data types, structures, flow controls, iterations, etc… or if you like javascript and Python then you can skip and head to javascript or python implementation.
Let’s begin by creating a folder with 2 files in it, main.go and block.go
Folder structure:
go // the folder
main.go // file 1
block.go // file 2
main.go
// use the main package
package main//import the fmt packageimport ("fmt")func main(){fmt.Println("I am building my first blockchain") // print thisCreateBlock("The hearder will be shown here", "the body will be shown here") // call the function that will create the block and pass some parameters in it.}
If you run this program it will show an error because the CreateBlock
function is not defined yet, so go ahead and create it in block.go
block.go
package mainimport ("fmt" // this will help us to print on the screen)func CreateBlock(Header, Body string){fmt.Println(Header ,"\n", Body) // Show me the block content}
The beauty of Go is that you don’t have to import or export functions, just declare them with capital letters and Go will find them for you😃. now open a terminal and move to your created folder, and run go build
then run .\go
on windows, ./go
on Linux, and Macbook.
We just created a simple Go program that calls a function and passes some string data. Let’s go ahead and do more nice things. let’s add 2 more files, blockchain.go
and structures.go
so now we have 4 files: main.go, block.go, structures.go, and blockchain.go
I will add to each line of code some comments in order for you to understand what I am doing.
structures.go
block.go
blockchain.go
main.go
Let’s run it now, go build
then .\go
Voila! Easy right?
Oops, our blocks in the blockchain don’t have any IDs and timestamps, so let’s add that information by modifying the main.go
file, add these two lines in the for loop
:
fmt.Printf("Block ID : %d \n", i) fmt.Printf("Timestamp : %d \n", block.Timestamp+int64(i))
Let’s save the code and run it again, go build
then ./go
As you can see, the blockchain is well structured. Except for the genesis block, each block contains its hash and the hash of the previous block which makes it immutable, if the data in the block is altered, the hash will automatically change and the block will be discarded. The genesis block doesn’t have any previous hash because it’s the first one, there is no previous block.
All done! congratulations, you just created your baby blockchain in Go😉
In the next episodes, We will gradually fulfill the blockchain requirements. Blockchain is a masterpiece that needs to be engineered the way it’s supposed to be.
Do you know you can click 50 times the like button? so go ahead and smash it if you liked this post.👍
Some coffee will be much appreciated😉☕
Click here for the Javascript version and here for the Python one.
“A Blockchain Engineer is a professional who understands thoroughly the principles of blockchain, security, and software engineering for designing, developing, maintaining, testing and evaluating the blockchain core engines and software whereas Blockchain Developer is a professional who builds software on top of the blockchain called decentralized applications. Most blockchain developers use open blockchain platforms and frameworks like Ethereum, Hyperledger fabric, EOS, etc…”
Which one do you want to be?
In case you find any typos or error when running the code, reach out to me:
Let’s connect on LinkedIn: Sinai Nday
Or buy me some coffee 😉☕