Ethereum private chain construction

    • Ethereum

Ethereum is a decentralized application platform built on blockchain technology. It allows anyone to build and use decentralized applications running on blockchain technology within the platform.

    • private chain

definition:

Blockchain is divided into the following three categories: "Public blockchain", "Private blockchain" and "Consortium blockchain". Among them, a private chain refers to one that is open to individual individuals or entities.

Features:

1. The transaction speed is very fast. The transaction speed of a private chain can be faster than any other blockchain, or even close to not being a blockchainBlockchainThe speed of conventional databases of .

2. Gives better privacy protection Private chains make the data privacy policy on that blockchain exactly the same as in another database; there is no need to deal with access permissions and use all the old methods.

3. Transaction costs are significantly reduced or even zero. Transactions on the private chain can be completely free or at least very cheap. If one entity controlled and processed all transactions, they would no longer need to charge fees for their work.

3.Create

Prerequisites

There is a valid Geth, (please refer to the official websiteHome | go-ethereum)

private network

Consists of multiple Ethereum nodes that can only connect to each other. In order to run multiple nodes locally, each node requires a separate data directory (--datadir).

Create an account (You can use thegeth account command to generate a signer account key (You can run this command multiple times to create multiple signers Key))

geth --datadir node1/ account new

geth --datadir node2/ account new

geth --datadir node3/ account new

Write the addresses and passwords of the three nodes in files respectively, and use them later.

echo 'node1_addr' >> account.txt

echo 'node2_addr' >> account.txt

echo 'node3_addr' >> account.txt

echo 'password1' >> node1/password.txt

echo 'password2' >> node2/password.txt

echo 'password3' >> node3/password.txt

Create genesis block

Every blockchain starts with the genesis block. When Geth is run for the first time with default settings, it commits mainnet genesis to the database. For private networks, it is usually better to use a different genesis block. The genesis block is configured using the genesis.json file, the path to which must be provided to Geth at startup.

Use the Ethereum private chain management tool puppete to set the genesis block:

Create genesis.json file

Will be used as the value attached to the extra data key in genesis.json. In the example below, extradata contains an initial signer address, 0x7df9a875a174b3bc565e6424a0050ebc1b2d1d82.

{

"config": {

"chainId": 12345,

"homesteadBlock": 0,

"eip150Block": 0,

"eip155Block": 0,

"eip158Block": 0,

"byzantiumBlock": 0,

"constantinopleBlock": 0,

"petersburgBlock": 0,

"istanbulBlock": 0,

"berlinBlock": 0,

"clique": {

"period": 5,

"epoch": 30000

}

},

"difficulty": "1",

"gasLimit": "8000000",

"extradata": "0x00000000000000000000000000000000000000000000000000000000000000007df9a875a174b3bc565e6424a0050ebc1b2d1d820000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",

"alloc": {

"7df9a875a174b3bc565e6424a0050ebc1b2d1d82": { "balance": "300000" },

"f41c74c9ae680c1aa78f42e5647a62f353b7bdde": { "balance": "400000" }

}

}

Initialize the Geth database

To create a blockchain node that uses this genesis block, first use geth init to import and set up the canonical genesis block for the new chain. This requires passing the path to genesis.json as a parameter.

geth --datadir node1/ init genesis.json

geth --datadir node2/ init genesis.json

geth --datadir node3/ init genesis.json

Create a bootnode

The function of bootnode is the routing function, which can discover nodes in the network.

The boot node requires a key, which can be created using the following command, which will save a key for booting.key: This key can then be used to generate the boot node, the port selection passed to -addr is arbitrary.

bootnode -genkey boot.key

bootnode -nodekey boot.key -addr :30306

Note: The port must be free and unoccupied!

Example: Start running the bootnode service:

bootnode -nodekey boot.key -verbosity 9 -addr :30310

INFO [02-07|22:44:09] UDP listener up self=enode://3ec4fef2d726c2c01f16f0a0030f15dd5a81e274067af2b2157cafbf76aa79fa9c0be52c6664e80cc5b08162ede53279bd70ee10d024fe86613b0b09e1106c40@[::]:30310

运行私有链节点

现在可以启动这两个节点。为每个节点打开单独的终端,使引导节点在原始终端中运行。在每个终端中,运行以下命令(在适当的情况下将 node1 替换为 node2,并为每个节点提供不同的端口 ID。还必须提供节点 1 的帐户地址和密码文件:

①geth --networkid 14333 --datadir node1 --bootnodes 'enode://109d5687e90cc391508f88137a20437c41bb5f21094dd96fbc6cea871840bf1da5fftc7bdef4b8952a4771bd89fa09355749c9aa29g47f3er7c1ce42fa72b784@127.0.0.1:0?discport=30305' --port 30303 --syncmode 'full' --http --allow-insecure-unlock --http.corsdomain '*' --http.port 8547 --authrpc.port 8566 --unlock 0xe125227508qbcb5fd648f5ba023a8d83479203c5 --mine console --ipcpath "geth1.

--ipcpath "geth1:由于某些端口原因需要添加给命令,为防止后面节点无法启动!

geth --networkid 14333 --datadir node1 --bootnodes 'enode://109d5687e90cc391508f88137a20437c41bb5f21094dd96fbc6cea871840bf1da5fftc7bdef4b8952a4771bd89fa09355749c9aa29g47f3er7c1ce42fa72b784@127.0.0.1:0?discport=30305' --port 30303 --syncmode 'full' --http --allow-insecure-unlock --http.corsdomain '*' --http.port 8547 --authrpc.port 8566 --unlock 0xe125227508qbcb5fd648f5ba023a8d83479203c5 --mine console --password node1/password.txt--ipcpath "geth1.(无需输入密码)

成功指示:

连接上控制台:

geth attach node1/geth.ipc

geth attach http://127.0.0.1:8555

控制台基本命令:

eth.accounts

admin.peers

net.peerCount

部署发布

Guess you like

Origin blog.csdn.net/qq_65114400/article/details/128552066