Is It Hard to Build a Blockchain From Scratch?
By: Sinai Nday
Tags: JavaScript, Blockchain, Distributed Ledgers, Python, Go

The same chain is then distributed to all the nodes (computers or miners) across the network via a P2P network.

By Nday Sinai

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 estates: 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 easier one is to use pre-built blockchain open-sources like Ethereum ( Create Distributed applications, altcoins, DEFI, NFT, etc.), Fabric (Configure a private blockchain), EOS, Cardano, etc. so that you don’t have to deal with the core engine which is a pain to implement.

If none of them do fit your requirements then build one from scratch or fork, modify and/or 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 package import ( "fmt" ) func main(){ fmt.Println("I am building my first blockchain") // print this CreateBlock("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 main import ( "fmt" // this will help us to print on the screen ) func CreateBlock(Header, Body string){ fmt.Println(Header ,"

", 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.

Nday Kabulo Sinai

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

Result: by Nday Sinai

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

", i) fmt.Printf("Timestamp : %d

", block.Timestamp+int64(i))

Let’s save the code and run it again, go build then ./go

Final result: by Nday Sinai

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 medium like button? so go ahead and smash it if you liked this post.👍

Click here for the Javascript version and here for the python one.