Ethereum private chain construction

Ethereum private chain construction (Windows+geth)

1.Geth download and installation (Geth is the go Ethereum client)

1.1Geth download

https://geth.ethereum.org/downloads/

(This is the version I downloaded)

1.2Geth installation

Foolish installation, just click Next (you can choose your own installation path), and finally configure the environment variables. The installation process should automatically configure the environment variables. If you don’t add them manually
Set the system environment variables/the path you just installed (mine was installed directly in the D:\ path)
Computer/Computer——> Properties——> Advanced System Settings——> Environment Variables——> System Variables——> path

[The external link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-lfgcNZz4-1683344995448) (assets/image-20220416002518947.png)]
Insert image description here

Enter geth -h in the command line window to return relevant information, indicating that the installation and configuration are successful.

2. Build a private chain

2.1 Genesis Blockchain Configuration

Create the prichain folder in the Geth directory, then create the data0 folder (used to save block information), and create the genesis.json file (the genesis block configuration information is placed in the json file)
Insert image description here
genesis.json file content:

{
    
    
  "config": {
    
    
        "chainId": 8, 
        "homesteadBlock": 0,
        "eip155Block": 0,
        "eip158Block": 0
    },
  "alloc"      : {
    
    },
  "coinbase"   : "0x0000000000000000000000000000000000000000",
  "difficulty" : "0x1",
  "extraData"  : "",
  "gasLimit"   : "0x2fefd8",
  "nonce"      : "0x0000000000000066",
  "mixhash"    : "0x0000000000000000000000000000000000000000000000000000000000000000",
  "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
  "timestamp"  : "0x00"
}

2.2 Genesis block initialization
Enter the command line window, switch to the path where genesis.json is located and execute the initialization command
geth --datadir data0 init genesis.json
Insert image description here

will return successfully worte genesis state to indicate successful initialization (private chain is successfully built)
geth … init …, indicating initialization of the blockchain
–datadir The option is followed by a directory name, specifying the data storage directory as data0
genesis.json is a parameter of the init command.
Running the above command will read the genesis.json file and write the genesis block to the blockchain.
View the initialization results in the file directory
[The external link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-elhqs99X-1683344995449) (assets/image-20220416095928117.png)]

3. Start the private chain and enter the geth console

Command line input geth --datadir data0 --networkid 8 console
geth console: Start the node and enter the interactive console
–datadir option Specify using data0 as the data directory
–The networkid option is followed by a number and specifies that the network ID of this private chain is 8.
note: The network ID will be used when connecting to other nodes. The network ID of the Ethereum public network is 1. In order not to conflict with the public chain network, it must be specified when running the private chain node. own network id
Insert image description here
Insert image description here

This is an interactive Javascript execution environment, where Javascript code can be executed, where > is the command prompt. Some Javascript objects used to operate Ethereum are also built into this environment, and these objects can be used directly. These objects mainly include:
eth: Contains some methods related to operating the blockchain
net: Contains the following methods to view the status of the p2p network< a i=3> admin: Contains some methods related to managing nodes miner: Contains some methods to start & stop mining web3: Contains the above Object, also contains some unit conversion methods


4. Create an account

[The external link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-KWgGC80T-1683344995450) (assets/image-20220416101011604.png)]

(The account password you created must be saved)

6.Common commands in geth console

personal.newAccount(): Create an account;
personal.unlockAccount(): Unlock the account;
eth.accounts: Enumerate the accounts in the system Account;
eth.getBalance(): View the account balance, the unit of the return value is Wei (Wei is the smallest currency denomination unit in Ethereum, similar to Satoshi in Bitcoin, 1 ether = 10^ 18 Wei);
eth.blockNumber: List the total number of blocks;
eth.getTransaction(): Get the transaction;
eth.getBlock(): Get the block;
miner.start(): Start;
miner.stop(): Stop;
web3.fromWei(): Wei is converted into Ether;
web3.toWei(): Ether is converted into Wei;
txpool.status: Transaction Status in the pool;
admin.addPeer(): Connect to other nodes;

Guess you like

Origin blog.csdn.net/xuanyitwo/article/details/130529694