Create your own blockchain from scratch

Create your own blockchain from scratch

What is blockchain? Is it hard to build one? Where do you start? Which programming language should I use?

I get these questions quite often when I meet people interested in blockchain technology. You may also be one of those people, but don’t worry, I was too.

There are plenty of blockchain resources online, but understanding this booming technology as a beginner can be overwhelming and frustrating. However, this article is a little different than the other resources.

I will approach the topic in small steps with you, guiding you through the basic concepts of blockchain and how to program one using Golang (Go).

For speed, persistence and security, most blockchain core engines are embedded C/C++ (Bitcoin, EOS, etc…), Go (Hyperledger Fabric, Ethereum), Java (Ethereum), RustHaskell (Cardano) and/or Ruby (Ethereum), then provides bindings to other user-friendly programming languages.

Some blockchain engines also combine many programming languages ​​for robustness and ease of use for developers. Ethereum is the best example of this.

What Prerequisites Do I need to create a blockchain?

  • A network.
  • Cryptography.
  • A data structure and algorithms.
  • Decentralized systems.
  • Javascript, Go or Python knowledge.

You just need to understand the basic concepts to program your first blockchain prototype, so let’s start with some theory.

More about Blockchain: Sushiswap vs Uniswap: What are the differences?

What is a blockchain?

You can’t program something you don’t understand, right? Let’s break it down. Blockchain is not Bitcoin. Blockchain is not one digital currencyBlockchain is a set of different technologies that had already existed before it was created.

So, is this a blockchain?

No, but bear with me.

Let’s simplify things with an example and some figures.

Let’s take one MySQL database which stores some information in different tables.

An illustration of a MySQL database.
An illustration of a MySQL database. | Photo: Sinai Nday

With the database above, there are some limitations. It allows you to perform some tasks, including:

  1. Create, retrieve, update and delete (CRUD) operations.
  2. Save the same information twice.

But the limitations are many, including:

  1. We may drop the entire database either by mistake or on purpose.
  2. We cannot share sensitive information with others.
  3. It is centralized, which means there is a single point of failure and a security issue.
  4. There is no way to trust anything stored in it.
  5. People with bad intentions can make up the database.
  6. The database needs an administrator.
  7. Users do not have power over their own data

We need something different that is transparent, reliable and independent of people. Something that is automatic, immutable, decentralized and indestructible. This is where blockchain comes in.

A blockchain is a secure, reliable decentralized database and network in one.

How does Blockchain work?

In other words, a blockchain is a chain of blocks. These blocks are like tables in the database, but they cannot be deleted or updated. The blocks contain information such as transactions, nonce, target bit, difficulty, timestamp, previous block hash, current block hash, Markle tree and block ID, etc. and the blocks are cryptographically verified and linked together to form an immutable chain called a blockchain or a ledger .

See also  NFT lending allows NFT holders to maximize benefits | News
An illustration of a blockchain.
An illustration of a blockchain. | Photo: Sinai Nday

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

Illustration of a blockchain P2P network.
Illustration of a blockchain P2P network. | Photo: Sinai Nday

So instead of a centralized database, all the transactions (data) shared across the nodes are contained in blocks, which are linked together to make the ledger. This ledger represents all the data in the blockchain. All data in the ledger is secured with cryptographic hashing and digital signature and validated by consensus algorithm. Nodes on the network participate to ensure that all copies of data distributed across the network are the same.

5 Key Concepts in the Blockchain Ecosystem

  1. Cryptographic hash and digital signature.
  2. Immutable ledger.
  3. P2P networks.
  4. Consensus algorithm (PoW, PoS, PBFT, ETc …).
  5. Block validation (mining, forging, etc …).

I will explain these concepts in more detail later.

What are the benefits of using Blockchain?

There are a number benefits of using blockchainincluding:

  1. Remove intermediate organizations.
  2. An immutable ledger
  3. Transparency
  4. Safety
  5. Reliable

When should you use Blockchain?

Blockchain is not a silver bullet, so it is best to use it when:

  1. The data stored cannot be changed (proof of existence).
  2. The data cannot be denied by the owner (non-repudiation).
  3. You want decentralization.
  4. You want one source of truth.
  5. You want high security.
  6. You don’t care about speed. For example, Bitcoin takes an average of 10 minutes to validate a block of transactions. But some blockchains are faster because they use different consensus algorithms than PoW. We will talk about this later.

Blockchain Use Cases

Blockchain goes beyond cryptocurrency and bitcoin. It can be used in various sectors including:

  • Property: Land ownership.
  • Health Service: Record the patient’s data securely.
  • Finance: Reduce taxes and intermediaries, combating money laundering and cross-border payments.
  • Supply chain: Track goods from suppliers to customers, including verification of authenticity and original content.
  • Cyber ​​security: DDOS attack.
  • Give the power back to the user: Own your data and securely share it with whomever you want (DID).
  • Cryptocurrency.
  • Voting mechanism.

Blockchain platforms and applications to know

More about Blockchain: 22 crypto apps to know

Blockchain types

There are three types of blockchains:

  • Private: Use only internally and when we know the users (eg Hyperledger Fabric).
  • Public: Everyone can see what’s going on (Bitcoin, Ethereum).
  • Hybrid: In case you want to combine the first two.

Create your own blockchain

There are two ways to build a blockchain

  1. The easiest way is to use a pre-built blockchain open source like Ethereum (create distributed applications, altcoins, decentralized finance (DeFi) and non-fungible tokens (NFTs)etc.), Fabric (set up a private blockchain), EOS or Cardano etc. so you don’t have to deal with the core engine, which is difficult to implement.
  2. If it doesn’t suit your requirements, you can build one from scratch, modify and/or improve an existing open source blockchain. For example, Litecoin and Bitcoin cash was discarded from Bitcoin. This last method is tougher, more time-consuming and requires a lot of work and a strong team.

In this article, we will build a blockchain prototype from scratch so that you can thoroughly understand the blockchain state machine.

More about Blockchain: What is Layer 1 in Blockchain?

How to create a blockchain from scratch on the fly

We will create the first baby blockchain in Go. You can also learn how to create it in Pythonand JavaScript. These prototypes can help you understand the concepts we described earlier. We complete this in four steps:

  1. Create a block.
  2. Add the data (header and body) to the block.
  3. The hash block.
  4. Connect the blocks together.
See also  Business news LIVE today: Latest business news, share market news, economy and finance news

A block contains information mentioned earlier, but to simplify things we will remove some. Let’s delve into the details.

If you are not familiar with Go, try to familiarize yourself with basicincluding functions, methods, data types, structures, flow controls and iterations, etc.

A tutorial on how to build a blockchain in Golang. | Video: Tensor programming

1. Create a block

We start by creating a folder with two 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 CreateBlock the function is not defined yet, so go ahead and add it block.go.

package main
import (
"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
}

2. Add data to your blocks.

The beauty of Go is that you don’t need to import or export functions, just declare them in uppercase letters and Go will find them for you. Now open a terminal and move to the created folder, and run go buildthen run .\go on Windows, or ./go on Linux and Macbook.

Screenshot of the Go program calling a function and sending a data string.
Screenshot of the Go program calling a function and sending a data string. | Screenshot: Sinai Nday

We’ve just created a simple Go program that calls a function and sends some string data. Let’s add two more files, blockchain.go and structures.go. Now we have four files: main.go, block.go, structures.goand blockchain.go

3. Hash your block

I’ll add some comments to each line of code so you understand what I’m doing.

Structures.go

package main //Import the main package
// Create the Block data structure
// A block contains this info:
type Block struct {
	Timestamp         int64  // the time when the block was created
	PreviousBlockHash []byte // the hash of the previous block
	MyBlockHash       []byte // the hash of the current block
	AllData           []byte // the data or transactions (body info)
}

// Prepare the Blockchain data structure :
type Blockchain struct {
	Blocks []*Block // remember a blockchain is a series of blocks
}

Block.go

package main

import (
	// We will need these libraries:
	"bytes"         // need to convert data into byte in order to be sent on the network, computer understands better the byte(8bits)language
	"crypto/sha256" //crypto library to hash the data
	"strconv"       // for conversion
	"time"          // the time for our timestamp
)

// Now let's create a method for generating a hash of the block
// We will just concatenate all the data and hash it to obtain the block hash
func (block *Block) SetHash() {
	timestamp := []byte(strconv.FormatInt(block.Timestamp, 10))                                  // get the time and convert it into a unique series of digits
	headers := bytes.Join([][]byte{timestamp, block.PreviousBlockHash, block.AllData}, []byte{}) // concatenate all the block data
	hash := sha256.Sum256(headers)                                                               // hash the whole thing
	block.MyBlockHash = hash[:]                                                                  // now set the hash of the block
}

// Create a function for new block generation and return that block
func NewBlock(data string, prevBlockHash []byte) *Block {
	block := &Block{time.Now().Unix(), prevBlockHash, []byte{}, []byte(data)} // the block is received
	block.SetHash()                                                           // the block is hashed
	return block                                                              // the block is returned with all the information in it
}

/* let's now create the genesis block function that will return the first block. The genesis block is the first block on the chain */
func NewGenesisBlock() *Block {
	return NewBlock("Genesis Block", []byte{}) // the genesis block is made with some data in it
}

See also  Orbeon Protocol (ORBN) vs Polkadot (DOT) vs The Protocol (THE): Which Blockchain Technology Suits Your Needs?

Blockchain.go

package main

// create the method that adds a new block to a blockchain
func (blockchain *Blockchain) AddBlock(data string) {
	PreviousBlock := blockchain.Blocks[len(blockchain.Blocks)-1] // the previous block is needed, so let's get it
	newBlock := NewBlock(data, PreviousBlock.MyBlockHash)        // create a new block containing the data and the hash of the previous block
	blockchain.Blocks = append(blockchain.Blocks, newBlock)      // add that block to the chain to create a chain of blocks
}

/* Create the function that returns the whole blockchain and add the genesis to it first. the genesis block is the first ever mined block, so let's create a function that will return it since it does not exist yet */
func NewBlockchain() *Blockchain { // the function is created
	return &Blockchain{[]*Block{NewGenesisBlock()}} // the genesis block is added first to the chain
}

Main.go

//Time to put everything together and test
package main
import (
	"fmt" // just for printing something on the screen
)
func main() {
	newblockchain := NewBlockchain() // Initialize the blockchain
	// create 2 blocks and add 2 transactions
	newblockchain.AddBlock("first transaction")  // first block containing one tx
	newblockchain.AddBlock("Second transaction") // second block containing one tx
	// Now print all the blocks and their contents
	for _, block := range newblockchain.Blocks { // iterate on each block
		fmt.Printf("Hash of the block %x\n", block.MyBlockHash)                 // print the hash of the block
		fmt.Printf("Hash of the previous Block: %x\n", block.PreviousBlockHash) // print the hash of the previous block
		fmt.Printf("All the transactions: %s\n", block.AllData)                 // print the transactions
	} // our blockchain will be printed
}

Let’s run it now, go build thereafter .\go.

Result.
Result. | Screenshot: Sinai Nday

Voila. Easy, right?

4. Connect your blocks together

Oops, our blocks in the blockchain have no IDs and timestamps. So let’s add that information by changing main.go file, add these two lines in for loop:

fmt.Printf("Block ID : %d \n", i)                                        fmt.Printf("Timestamp : %d \n", block.Timestamp+int64(i))
//Time to put everything together and test
package main

import (
	"fmt" // just for printing something on the screen
)

func main() {
	newblockchain := NewBlockchain() // Initialize the blockchain
	// create 2 blocks and add 2 transactions
	newblockchain.AddBlock("first transaction")  // first block containing one tx
	newblockchain.AddBlock("Second transaction") // second block containing one tx
	// Now print all the blocks and their contents
	for i, block := range newblockchain.Blocks { // iterate on each block
		fmt.Printf("Block ID : %d \n", i)                                        // print the block ID
		fmt.Printf("Timestamp : %d \n", block.Timestamp+int64(i))                // print the timestamp of the block, to make them different, we just add a value i
		fmt.Printf("Hash of the block : %x\n", block.MyBlockHash)                // print the hash of the block
		fmt.Printf("Hash of the previous Block : %x\n", block.PreviousBlockHash) // print the hash of the previous block
		fmt.Printf("All the transactions : %s\n", block.AllData)                 // print the transactions
	} // our blockchain will be printed
}

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

Final result of your blockchain.
Final result of your blockchain. | Photo: Sinai Nday

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, making it immutable. If the data in the block changes, the hash will automatically change and the block will be discarded. The Genesis block has no previous hash because it is the first. There is no previous block.

And that’s it. Congratulations, you’ve just created your first baby blockchain in Go.

We will gradually meet the blockchain requirements. Blockchain is a masterpiece that must be constructed as it should be.

In blockchain, there are also two common roles. A blockchain engineer and a blockchain developer. A blockchain engineer is a professional who thoroughly understands the principles of blockchain, security, and software engineering to design, develop, maintain, test, and evaluate blockchain core engines and software. ONE 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. Now that you have the basics, it’s up to you to decide which one you want to be.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *