Three easy and quick steps to build an Ethereum private chain test

Environment build

First of all, there are many ways to build an Ethereum private chain. This article introduces how to quickly build a test chain with docker, which can be used to test contracts and do experiments. Not suitable for production environments.
(Note in advance that openethereum will no longer be updated in May 2022, and only the existing version will be kept.)
To use docker to build an Ethereum private chain, of course you need to install docker and docker compose. For specific docker installation, it is recommended to go to the official website. The installation is described in detail in my other article ( hyperledger fabric 2.4 installation and running test ).
The docker image used in this article is openethereum/openethereum, and those who are interested can learn about the official website by themselves.

openethereum源码官网
https://github.com/openethereum/openethereum
openethereum文档详解
https://openethereum.github.io/Configuring-OpenEthereum

The detailed installation link is attached below
Note, please install docker, docker-compose and go from the official website, not through apt. (may have an impact)
docker

configuration file directory

First create our new chain directory, and create the corresponding data directory and key directory. Among them, newethchain is the main project root directory, and the key folder is used to store the corresponding account key file.

mkdir newethchain
cd newethchain
mkdir chaindata
mkdir key

Next is the key point, we need to create the corresponding configuration file to start and configure OpenEthereum.

Ethereum configuration

1. The first one is the configuration file for starting the Ethereum node, that is, the configuration file for us to start the Ethereum to generate the genesis block. Here we use a startup configuration that we often use. If there is no one, please refer to the one below. Just configure it.
A simple configuration is attached as follows:

{
    
    
  "name": "newethchain",
  "engine": {
    
    
    "instantSeal": {
    
    
      "params": {
    
    }
    }
  },
  "params": {
    
    
    "gasLimitBoundDivisor": "0x0400",
    "accountStartNonce": "0x0",
    "maximumExtraDataSize": "0x20",
    "minGasLimit": "0x1388",
    "networkID": "0x11",
    "registrar": "0x0000000000000000000000000000000000001337",
    "eip150Transition": "0x0",
    "eip160Transition": "0x0",
    "eip161abcTransition": "0x0",
    "eip161dTransition": "0x0",
    "eip155Transition": "0x0",
    "eip98Transition": "0x7fffffffffffff",
    "maxCodeSize": 24576,
    "maxCodeSizeTransition": "0x0",
    "eip140Transition": "0x0",
    "eip211Transition": "0x0",
    "eip214Transition": "0x0",
    "eip658Transition": "0x0",
    "eip145Transition": "0x0",
    "eip1014Transition": "0x0",
    "eip1052Transition": "0x0",
    "wasmActivationTransition": "0x0"
  },
  "genesis": {
    
    
    "seal": {
    
    
      "generic": "0x0"
    },
    "difficulty": "0x20000",
    "author": "0x0000000000000000000000000000000000000000",
    "timestamp": "0x00",
    "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
    "extraData": "0x",
    "gasLimit": "0x7A1200"
  },
  "accounts": {
    
    
    "E2612d75e9BEBe8924d1d3e92a0003876eF113fa": {
    
     "balance": "1000000000000000000000000000" },
    "3e8230dfc38Fb145E10F9484AFdD823DD079FdF2": {
    
     "balance": "5000000000000000000" }
  }
}

OpenEthereum configuration

2. The second step is to configure OpenEthereum, and introduce the configuration of the Ethereum creation block in the first step into openethereum. openethereum is started in the form of docker, and does not start with the command line like geth, so we need to configure the configuration file of the first step in the configuration file in the form of a path

First we create the configuration file config.toml of openethereum. Enter the following configuration, see the official website for detailed configuration, and configure according to your needs.
For detailed configuration, please refer to the website https://openethereum.github.io/Configuring-OpenEthereum
For the full version configuration toml file, please refer to https://github.com/openethereum/openethereum/blob/main/bin/oe/cli/ In tests/config.full.toml
, it is worth noting that the first configuration is the creation configuration of the first step, which should be written in the path in docker. For the path here, see the third step for details.

[parity]
# 创世配置文件,指定相关的创世配置信息,第一步的配置文件的文件名
chain = "/home/openethereum/.local/share/openethereum/neweth.json"
#base_path = "./chain-data"
#keys_path = "./keys"


[rpc]
interface = "all"
apis = ["all"]
hosts = ["all"]
cors = ["all"]
# necessary for EIP1186 eth_getProof
experimental_rpcs = true
port = 8845

[websockets]
interface = "all"
apis = ["all"]
hosts = ["all"]
origins = ["all"]
port = 8846

[mining]
reseal_min_period = 0
min_gas_price = 0

[footprint]
# Enables Fat DB
fat_db = "on"

docker compose startup configuration

3. This step is mainly to configure dockercompose. We use the docker compose command to arrange and start the docker image of openethereum. It mainly needs to configure the configuration file config.toml (the file name of the configuration file in the second step), and the chain data directory chain-data , the account secret key directory key. A simple configuration is provided here as a reference. What needs special attention here is that the configuration file in this step and the configuration file in the second step must have a one-to-one correspondence with respect to the open port numbers .

version: '3.3'
services:
  openethereum:
    container_name: newethchain
    image: openethereum/openethereum
    volumes:
      - ./:/home/openethereum/.local/share/openethereum
    command: >
      --config /home/openethereum/.local/share/openethereum/config.toml
      --base-path /home/openethereum/.local/share/openethereum/chain-data
      --keys-path /home/openethereum/.local/share/openethereum/key
    ports:
      - 8845:8845
      - 8846:8846
#      - 30002:30002
#      - 30002:30002/udp

This configuration is complete

test

Start the docker container

cd newethchain
sudo docker compose up
添加-d参数可使容器在后台运行

It can be seen that the container starts normally
insert image description here
. We can allocate the initial ETH to our account in metamask when configuring the genesis block.

"accounts": {
    
    
    "E2612d75e9BEBe8924d1d3e92a0003876eF113fa": {
    
     "balance": "1000000000000000000000000000" },
    "3e8230dfc38Fb145E10F9484AFdD823DD079FdF2": {
    
     "balance": "5000000000000000000" }
  }

At this point, we can see the effect by connecting our private chain through metamask.
insert image description here

If necessary, you can use web3.js or ether.js for more detailed testing

Summarize

The fast Ethereum construction method in this article is mainly used for experiments or testing contracts. If there are special requirements for the version of the chain, it is recommended to start a latest version of the Ethereum private chain through geth or other methods.
If you have any questions, welcome to communicate.

Guess you like

Origin blog.csdn.net/qq_40482198/article/details/121793072