shuhai

shuhai

Build an Ethereum private chain, connect a visual wallet, and mine using the Geth client on Win10.

Download and Install the Geth Client#

Download Link: https://geth.ethereum.org/downloads/*

Download the latest version of Geth (Stable releases for Win10)
Choose the third option, download and install (select a convenient location during installation):

1
2

After the download is complete, open the folder:

3

Verify the installation:

Open cmd and enter the following command (to view the help documentation): geth -help

If the following content appears, the installation is successful

4

At this point, the Ethereum client has been installed

Test syncing with the main network using geth (this step is not necessary for configuring a private chain)

Simply open geth.exe

Geth will automatically start syncing, and the initial prompt will also indicate where the block storage path is located

My path is: C:\Users\Edward\AppData\Roaming\Ethereum (usually in the administrator account path)

5
6

Note: To view AppData, you need to check "Show hidden items" in the View options
7

Genesis Block Configuration File#

(Start building a private chain: make sure to close the geth syncing with the main network)
Create a new genesis block file

Create a new file named genesis.json in the Geth installation directory and enter the following content (and save):

{
  "nonce": "0x0000000000000042",
  "difficulty": "0x4",
  "mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
  "coinbase": "0x0000000000000000000000000000000000000000",
  "timestamp": "0x00",
  "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
  "gasLimit": "0xffffffffffffff",
  "alloc": {},
  "config": {
	"chainId": 666,
    "homesteadBlock": 0,
    "eip150Block": 0,
    "eip150Hash": "0x0000000000000000000000000000000000000000000000000000000000000000",
    "eip155Block": 0,
    "eip158Block": 0,
    "byzantiumBlock": 0,
    "constantinopleBlock": 0,
    "petersburgBlock": 0,
    "istanbulBlock": 0,
    "ethash": {}
   }
}
Parameter NameDescription
chainIDSpecifies the unique blockchain network ID. The network ID is used when connecting to other nodes. The network ID for the Ethereum public network is 1. To avoid conflicts with the public chain network, specify your own network ID when running a private chain node. Nodes on different ID networks cannot connect to each other.
homesteadBlockA value of 0 indicates that the homesteadBlock version is being used. Ethereum's development is divided into four stages: 1. Frontier 2. HomesteadBlock 3. Metropolis 4. Serenity
eip155Blockeip stands for Ethereum Improvement Proposal. Your chain will not fork due to these proposals, so set it to "0".
mixhashA 256-bit hash proof used in combination with the nonce to prove that sufficient calculations have been performed on the block: Proof of Work (PoW).
nonceA 64-bit hash proof used in combination with the mixhash to prove that sufficient calculations have been performed on the block: Proof of Work (PoW).
difficultySets the difficulty of the current block. If the difficulty is too high, CPU mining becomes difficult. Set a lower difficulty here.
allocEthereum account information. It can be left blank and accounts can be created after deployment is complete. Account balances can also be pre-configured. The account balance here is in wei.
coinbaseThe miner's account, fill in randomly.
timestampSet the timestamp of the genesis block.
parentHashThe hash value of the previous block. Since this is the genesis block, the value is 0.
extraDataAdditional information, fill in randomly, you can fill in your personal information.
gasLimitThis value sets the total consumption limit of GAS, which is used to limit the total sum of transaction information that a block can contain. Since we are using a private chain, fill in the maximum value.

Initialization#

Open cmd in the genesis.json folder path and execute the command: geth --datadir .\db init genesis.json
8

After executing this command, a db folder will appear in the Geth installation directory, with chaindata storing block data and keystore storing account data

9
10

Start the Node#

Command: geth --http --http.api db,eth,net,web3,personal --datadir .\db --networkid 666 console 2>> log2020526.log

Note: My geth has been running without being closed during the setup process. The networkid needs to be set to the specified chainId (configured in genesis.json).

Important parameter explanations:

port: Specifies the Ethereum network listening port, default is 30303

http: Enable HTTP-RPC service for deploying and testing smart contracts

http.api: API provided by HTTP-RPC

http.addr: Specifies the HTTP-RPC service listening address, default is localhost

http.port: Specifies the HTTP-RPC service listening port number, default is 8545

networkid: Specifies the Ethereum network ID, default is 1 (public chain). Since we are building a private chain, specify the chainId (configured in genesis.json)

console: Start command line mode

2>> log2020526.log: Redirect and record logs

After successful startup, it will look like the following:
11

You can verify node information with the command: admin.nodeInfo

12

Create an Account#

Enter eth.accounts to view existing accounts:

13
[] indicates no accounts yet

Enter the following command to create an account: personal.newAccount()

You will be prompted to enter a password, remember it, and keep it simple for testing purposes

14

The address is within the double quotes

After successful execution, a public key (address) for the account will be generated, and the corresponding account file will be created in the keystore folder:
15

You can check the balance of the corresponding account using the following command:

eth.accounts[0] // View the address of the first account

eth.getBalance(eth.accounts[0])

eth.getBalance("0x7326bc89fe5a1d593afb5fa02479974be776a081") // You can also directly enter the address in parentheses
16
The account balance is 0, so you need to mine to obtain Ether

Download the MetaMask Wallet and Import the Created Account#

MetaMask is a Chrome extension
After downloading, a small fox logo will appear in the upper right corner. Click on it, click on the network to display the hidden network localhost 8545 (and connect)

17

18

We will import the account we created on MetaMask into our local private chain

Reference: https://blog.csdn.net/weixin_43988498/article/details/108394012

19

Export the private key

20

Copy the private key and save it in a local txt file
21

Then enter

geth account import +your_private_key_file_path

You will be prompted to enter a password, which is the password used in the geth console

The generated account will be the same as the one in MetaMask.

Now check the current key file storage location:

geth account list

Find the storage location behind the corresponding account and place the file in the keystore folder of our private chain project.
22
23

Enter eth.accounts in geth to view the newly imported account

24

Mining#

The rewards from mining will go to the miner's account, called coinbase. By default, coinbase is the first account in the local accounts. As shown in the following image: command: eth.coinbase

25

You can set the mining account by entering miner.setEtherbase(account)

I will set the newly imported account as the mining address
26

Check the balance with eth.getBalance(account):
27

Start mining with the following command, the parameter is the number of threads, here set to 1 (ignore the null prompt)
miner.start(1)

28

You can see the account balance increasing by entering the command or opening the MetaMask wallet

29

30

Stop mining with the following command: miner.stop()

31

At this point, our private chain setup is complete

References#

https://blog.csdn.net/cjm083121/article/details/106319518

https://blog.csdn.net/weixin_43988498/article/details/108394012

Update#

  1. The chainid 666 in genesis.json and the --networkid 666 when starting the node must be consistent. Also, check if the id set in the localhost network in MetaMask is consistent, otherwise, transfers will not work.
    32
  2. Issue with importing local accounts

Change the file extension of the keystore file to json, import it with a password, and it will take a while to import.

  1. The previous genesis block file has been updated with new content.
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.