In this article, we will see how to build our own blockchain in on our local computer using polygon edge technology.
First things first — installations
In order to build our own blockchain with a polygon edge, we need to first install the polygon edge on our system. We will also discuss all the dependencies and packages that are used in this course and how to install them.
So what are the prerequisites to get started with polygon edge? You should have some knowledge of the following topics -
Using the terminal or command prompt
How the blockchain works and its terminologies
Using Metamask wallet
And lastly, the following packages installed on your system
1.GO
If you choose to install polygon edge by cloning the repository and building it on the local system then you should have ‘go’ installed on your system. Now to install the go on your system you can visit -
https://go.dev/doc/install
Follow the steps to install the go according to your operating system. Once installed you can check if the go is installed properly or not by typing the following command in the command prompt or terminal -
$ go version
( Skip the ‘$’ it’s not the command, in each case from here onwards ) 😜, This will print the version of go installed on your system.
2.NodeJS
In the complete process of building the blockchain with polygon edge, we will be using some of the packages that use nodejs like truffle or hardhat libraries for deploying smart contracts on our custom blockchain or building the openzeppelin ERC20 tokens using their npm package, etc. So make sure you’ve NodeJS installed on the system.
To install the nodejs on your system you can visit -
Follow the steps to install the nodejs according to your operating system. Once installed you can check if the nodejs is installed properly or not by typing the following command in the command prompt or terminal -
$ node -v
This will print the current version of nodejs installed on your system.
It is advised to install nodejs with NVM(Node Version Manager), it lets you install and manage multiple versions of nodejs at the same time on a single system. Also in some cases, it might solve the permission error problems.
3.Truffle
You can skip this for now, but you still have to install the truffle when we will build the ‘Hello World’ smart contract on our edge chain. So it’s up to you, if you want to install it at this stage you can do that by following -
https://trufflesuite.com/docs/truffle/getting-started/installation/
This documentation also covers the installation of NodeJS with NVM.
4.Git(Optional)
We need the ‘git’ to clone the repository of polygon edge and later to clone the staking smart contracts. Basically, there are two ways to clone the repositories from GitHub -
We can use the ‘git clone ’ from the command prompt or terminal itself or
Just directly visit the GitHub repository URL and download the zip version of the URL on our system and then extract it.
If you want to install the git you can do so by visiting -
https://github.com/git-guides/install-git
5.Polygon Edge
There are three ways to install polygon edge on our system.
- Docker image
If you’re familiar with docker then you can go with this option otherwise skip this option.
Official Docker images are hosted under the hub.docker.com registry.
$ docker pull 0xpolygon/polygon-edge:latest
- Building from source
Now, this option requires the go installed on our system. make sure the installed version of go is >=1.18.
$ git clone https://github.com/0xPolygon/polygon-edge.git
$ cd polygon-edge/
$ go build -o polygon-edge main.go
$ sudo mv polygon-edge /usr/local/bin
Follow the above steps one by one and install the polygon edge.
- Using go install
This option also requires the go installed on our system. make sure the installed version of go is >=1.18.
$ go install github.com/0xPolygon/polygon-edge@develop
The binary will be available in your GOBIN environment variable and will include the latest changes from the mainline development branch.
If you ‘are facing any problems while installing any of the above packages on your system, you can ask for help from the DApp World community by joining the discord here -
Now once you’ve installed all the required dependencies we are good to go for building our own blockchain with polygon edge 🥳.
Now that you have completed the installations Let’s discuss what are the steps we are going to follow to build blockchain using polygon edge -
The first thing to consider here is that peer-to-peer networks are built of nodes that are nothing but computer machines, so to build that we need more than one computer machine here, but for now, we can make use of different ports available to run different nodes and create a network of nodes on one single system.
We’ve discussed that we are building a network of 4 nodes and why exactly 4 nodes in the last lesson. These 4 nodes will be the validator nodes and are eligible for both proposing the block and validating the blocks.
❗Why exactly 4 nodes?
There is no minimum to the number of nodes in a cluster, which means clusters with only 1 validator node are possible. Keep in mind that with a single node cluster, there is no crash tolerance and no BFT guarantee.
The minimum recommended number of nodes for achieving a BFT guarantee is 4 — since in a 4-node cluster, the failure of 1 node can be tolerated, with the remaining 3 functioning normally.
So each of these validator nodes will have a complete copy of the blockchain.
As all four nodes will be running on localhost, during the setup process it is expected that all the data directories for each of the nodes are in the same parent directory.
Steps to run a successful polygon edge chain on the local system -
- Generate data directories for each of the 4 nodes
- Prepare multiaddr string for bootnode
- Generate genesis file
- Running all servers and nodes.
Let’s see the steps one by one -
**
Step 1 : Initializing data directories
**-
Initialize the data directory with
$ polygon-edge secrets init - data-dir test-chain-1
This will generate the data directory for node one with name “test-chain-1”
And print the respective parameters -
Validator key
BLS public key
Node ID
Similarly for node 2 -
$ polygon-edge secrets init - data-dir test-chain-2
and node 3 -
$ polygon-edge secrets init - data-dir test-chain-3
and node 4 -
$ polygon-edge secrets init - data-dir test-chain-4
Step 2 : Preparing multiaddr string for bootnode
-
multiaddr format is as follows :
/ip4//tcp//p2p/
Here ip_address is 127.0.0.1, and the port will be 10001 in the case of node 1. The node_id is obtained while initializing the data directories.
So the complete multiaddr string for node 1 will look like -
/ip4/127.0.0.1/tcp/10001/p2p/16Uiu2HAmJxxH1tScDX2rLGSU9exnuvZKNM9SoK3v315azp68DLPW
Similarly, prepare the bootnode string for node2.
Step 3 : Generating genesis file
The basic command to generate the genesis file -
$ polygon-edge genesis
— consensus ibft
— ibft-validators-prefix-path test-chain-
— bootnode <01_multiaddr>
— bootnode <02_multiaddr>
Now here you should replace the <01_multiaddr> and <02_multiaddr> with the multiaddr string prepared in the last tutorial.
To premine the tokens in some account you should use the premine flag like -
- premine=<public_key_address>:1000000000000000000000
here replace the with the public key address in which you want to premine the tokens.
so the complete command will look like -
polygon-edge genesis
- consensus ibft
- ibft-validators-prefix-path test-chain-
- bootnode /ip4/127.0.0.1/tcp/10001/p2p/16Uiu2HAmJxxH1tScDX2rLGSU9exnuvZKNM9SoK3v315azp68DLPW
- bootnode /ip4/127.0.0.1/tcp/20001/p2p/16Uiu2HAmS9Nq4QAaEiogE4ieJFUYsoH28magT7wSvJPpfUGBj3Hq
- premine=0x3956E90e632AEbBF34DEB49b71c28A83Bc029862:1000000000000000000000
You can use as many as premine flags as you want.
To run the nodes -
For node 1 -
$ polygon-edge server
- data-dir ./test-chain-1
- chain genesis.json
- grpc-address :10000
- libp2p :10001
- jsonrpc :10002
- seal
For node 2 -
$ polygon-edge server
- data-dir ./test-chain-2
- chain genesis.json
- grpc-address :20000
- libp2p :20001
- jsonrpc :20002
- seal
Similarly, just replace the server port numbers and data directories for node 3 and node 4 respectively
Congratulations! You have now a blockchain running with 4 nodes on your local computer.
Now download and setup the meta mask wallet we will connect the wallet to the local blockchain.
Once the metamask account is set up now lets deploy the smart contract on our local blockchain -
The “Hello World” smart contract -
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
contract HelloWorld {
function helloworld() public pure returns(string memory){
return "Hello World";
}
}
Installing HDWalletProvider -
$ npm install @truffle/hdwallet-provider
Network configuration -
edgechain: {
provider: () =>
new HDWalletProvider(privatekey, `http://localhost:10002/`),
network_id: '*',
chain_id: 100,
},
Truffle commands -
Check version :
$ truffle version
Compile :
$ truffle compile
Migrate :
$ truffle migrate - network edgechain
Console :
$ truffle console - network edgechain
Getting contract instance in truffle console environment :
> let instance = await HelloWorld.deployed()
Fetching helloworld function :
> let msg = await instance.helloworld()
Congratulations! you have successfully create the blockchain on local computer and deployed the smart contract on it!
To get the complete guide with full Video+Reading+Quizzes and certification visit the DAppWorld and enroll in the course :
Complete course with videos+readings+quizzes+labs
Thanks for reading!
Top comments (0)