Install private Ethereum blockchain with 3 nodes tutorial, Biastek will show you 3 ways to setup private Ethereum. It depends on the using purpose, we should have suitable installation
- Install Private Ethereum Blockchain without docker
- Install Private Ethereum Blockchain with docker
- Install Private Ethereum Blokchain with OpenZeppelin
In this tutorial, we have 5 steps:
- Install prerequisites
- Set up Ethereum
- Run Ethereum
- Add Peers
- Testing Ethereum
Now let’s start from step 1
1. Set up prerequisites
Install Ubuntu 18.04 and install essential package
$ sudo apt-get update
$ sudo apt-get install build-essential
$ sudo apt-get upgrade
2. Set up Ethereum
Setup Geth
$ sudo add-apt-repository -y ppa:ethereum/ethereum
$ sudo apt-get update
$ sudo apt-get install ethereum
Create new accounts
- Create new account in 3 Peers with the below comand line
$ geth –datadir=./chaindata/ account new
- After create Account, it is will generate a folder “chaindata” which store data of Ethereum Blockchain. Besides, should save account and private key and put it to genesis.json file in next step.
- For example account in Peer 0
Public address of the key: 0xa352f2B91073472DF757248F9F06d8716d8b5123 Path of the secret key file: chaindata/keystore/UTC–2020-05-10T04-34-53.009776823Z–a352f2b91073472df757248f9f06d8716d8b5123
Create Genesis.json file
The Genesis File is the config file for creating/initializing a blockchain. Every (yes , every) new client joining an ethereum network must get a copy of the genesis file in order to properly initialize itself. Two clients with different genesis file will be able to “connect” to each other but not to work on the same blockchain
$vi genesis.json
{
“coinbase” : “0x0000000000000000000000000000000000000001”,
“difficulty” : “0x1”,
“extraData” : “”,
“gasLimit” : “0x47b760”,
“nonce” : “0x0000000000000042”,
“mixhash” : “0x0000000000000000000000000000000000000000000000000000000000000000”,
“parentHash” : “0x0000000000000000000000000000000000000000000000000000000000000000”,
“timestamp” : “0x00”,
“alloc”: {
“6cdbBD2e4DcbC0020b5D0Bb9B3500A001c14B078”: { “balance”: “0x1337000000000000000000” },// Account of Peer 0
“BdB8a18307931D887AA1F5609E6daD54c6004530”: { “balance”: “0x1337000000000000000000” }, //Account of Peer 1
“93AB0c01550DD1d505301dCD4B87932244608399”: { “balance”: “0x1337000000000000000000” }, //Account of Peer 2
//These accounts for re-funding
“4f1809cf76a8946d615cb546271cae3ea36328ed”: {
“balance”: “0x200000000000000000000000000000000000000000000000000000000000000”
},
“7d4684e8f1827832f13124b13874b00890a8a83f”: {
“balance”: “0x200000000000000000000000000000000000000000000000000000000000000”
},
“6206d535d4af9bd3327167027dbb2fffa070fc68”: {
“balance”: “0x200000000000000000000000000000000000000000000000000000000000000”
},
“362bfe247396ecc09d30091e3c5ca2d5f527bd06”: {
“balance”: “0x200000000000000000000000000000000000000000000000000000000000000”
},
“ca1c753ed477ac905f4ec6c9851bc81f0fb41512”: {
“balance”: “0x200000000000000000000000000000000000000000000000000000000000000”
},
“61cfb9bc71d6f2747598db3f767b4b8967f029ab”: {
“balance”: “0x200000000000000000000000000000000000000000000000000000000000000”
}
},
“config”: {
“chainId”: 22,
“homesteadBlock”: 0,
“eip155Block”: 0,
“eip158Block”: 0,
“eip150Block”: 0,
“byzantiumBlock”: 0,
“constantinopleBlock”: 0,
“petersburgBlock”: 0
}
}
Start Ethereum
$geth –datadir=./chaindata/ init ./genesis.json
- After start with above comand line, it will create geth.ipc file
3.Run Ethereum
Start Ethereum with mining process
$geth –identity “process-node” –datadir=./chaindata/ –mine –minerthreads=1 –allow-insecure-unlock –rpc –networkid 22 –rpcaddr “127.0.0.1” –rpcport 8545 –rpcapi=”db,eth,net,web3,personal” –rpccorsdomain “*”
$geth –identity “process-node” –datadir=./chaindata/ –mine –minerthreads=1 –allow-insecure-unlock –rpc –cache=1024 –syncmode “fast” –networkid 22 –rpcaddr “127.0.0.1” –rpcport 8545 –rpcapi=”db,eth,net,web3,personal” –rpccorsdomain “*”
- Start Ethereum without mining process, remove –mine –miner threads=1
$geth –identity “process-node” –datadir=./chaindata/ –allow-insecure-unlock –rpc –networkid 22 –rpcaddr “127.0.0.1” –rpcport 8545 –rpcapi=”db,eth,net,web3,personal” –rpccorsdomain “*”
- By default, account is locked after create, in order to unlock account using “allow-insecure-unlock” in command start Ethereum
- After run mining command line ethereum wil generate geth.ipc file.
- Unlock account
When deploy smart contract, account have to be unlock. Unlock account with below command line
$ personal.unlockAccount(eth.accounts[0])
4.Add Peers
Attach to blockchain with command
$ geth attach ~/chaindata/geth.ipc
Using command “admin.nodeInfo” to show enode
Add Peers use this command line with node info
admin.addPeer(“enode://da0111d59240f1d9930360625d274f0a655da64c9f16276eda63bb94e4e10d09f154c940308a71685248e29639027e70b9b965487f38f4cf032e7d27db22d99d@192.168.70.135:30303”)
5. Test Private Ethereum Blockchain
- Test with Web3
$geth attach ~/chaindata/geth.ipc
>web3.fromWei(eth.getBalance(eth.coinbase), “ether”)
- Test with JsonRpc by Postman
{“jsonrpc”:”2.0″,”method”:”eth_getBalance”,”params”:[“0x6cdbBD2e4DcbC0020b5D0Bb9B3500A001c14B078”, “latest”],”id”:1}
Connect MetaMask to Blockchain
- Install Metamask on chrome
- Choose network is localhost:8545
- Testing transfer ETH from Account2 to Accoutn1
Congratulations! We have completed private Ethereum installation
Blockchain Ethereum – Ví dụ về hàm Delegatecall trong solidity
Blockchain Ethereum (P4) – Kết Nối Các Node Sử Dụng Bootnode
Blockchain Ethereum (P3) – Cài Đặt Private Blockchain trên nền tảng Ethereum
Install web3 and nodejs
Sử dụng Virtual Box
Đọc sách 2021