新建一个区块链相关目录

mkdir chain
cd chain

新建一个目录data

mkdir data

创建一个账户

geth --datadir data  account new

输入两遍密码后即可生成一个账户

Your new account is locked with a password. Please give a password. Do not forget this 
password.
Password: 
Repeat password: 

Your new key was generated

Public address of the key:   XXXXXXXXXX
Path of the secret key file: data/keystore/UTC--2021-12-22T01-00-02.217462867Z--XXXXXXXXX

- You can share your public address with anyone. Others need it to interact with you.
- You must NEVER share the secret key with anyone! The key controls access to your funds!
- You must BACKUP your key file! Without the key, it's impossible to access account funds!
- You must REMEMBER your password! Without the password, it's impossible to decrypt the key!

创建成功后会在data目录下生成keystore文件,利用该文件可以查看账户

geth account list --keystore data/keystore

新建创世区块所需json配置

vi CustomGenesis.json

内容编辑如下

{
    "config": {
        "chainId": 3131,
        "homesteadBlock": 0,
        "eip150Block": 0,
        "eip155Block": 0,
        "eip158Block": 0
    },
    "difficulty": "0",
    "gasLimit": "2100000000",
    "alloc": {
          "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx": { "balance": "800" }
    }
}

其中xxxxxx为新建的账户地址,初始化给了800的余额

使用geth初始化创世区块

geth --datadir data init CustomGenesis.json

最后显示Successfully wrote genesis state database=lightchaindata即为成功

最后启动geth,注意现在geth中没有RPC相关的内容了,所以之前的--rpc --rpcport这种参数不再有效,使用会报以下错误

flag provided but not defined: -rpc

使用以下命令来启动geth进入控制台

geth --identity "Node40" --http --http.port "6060" --http.corsdomain "*" --datadir "data" --port "30303" --maxpeers 5 --http.api "admin,eth,debug,miner,net,txpool,personal,web3" --networkid 3131 console

这样就启动了一条私链并启动,显示如下

Welcome to the Geth JavaScript console!

instance: Geth/MX-Node40/v1.10.14-unstable-99be62a9-20211220/linux-amd64/go1.17.5
coinbase: 0xa15faa4e64df637947c63b37b69fcfc061fa78a7
at block: 0 (Thu Jan 01 1970 08:00:00 GMT+0800 (CST))
datadir: /home/mxchain_2/data
modules: admin:1.0 debug:1.0 eth:1.0 ethash:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 
txpool:1.0 web3:1.0

To exit, press ctrl-d or type exit

标签: golang, ETH, 私链, 以太坊, NFT, geth

添加新评论