百问区块链中台:Geth以太坊搭建私链

2021-02-11  本文已影响0人  sknfie

概述

在安装好了Geth之后,我们需要知道如何启动它。
无论是主网、测试网还是私网,都可以使用Geth来启动。Geth直接运行,默认连接的就是以太坊主网,如果想要连接测试网可以连接Ropsten或rinkeby:

// Ropsten 测试网络
geth --testnet --fast --cache=512 console
// Rinkeby 测试网络
geth --rinkeby --fast --cache=512 console

创世块文件说明

将如下内容保存为genesis.json文件

{
  "config": {
        "chainId": 18,
        "homesteadBlock": 0,
        "eip150Block": 0,
        "eip155Block": 0,
        "eip158Block": 0
    },
  "alloc"      : {},
  "coinbase"   : "0x0000000000000000000000000000000000000000",
  "difficulty" : "0x2",
  "extraData"  : "",
  "gasLimit"   : "0xffffffff",
  "nonce"      : "0x0000000000000042",
  "mixhash"    : "0x0000000000000000000000000000000000000000000000000000000000000000",
  "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
  "timestamp"  : "0x00"
}

创世块文件的部分内容,我们可以简单了解一下:

Geth搭建私网

1.利用创世块文件初始化

[root@192 install]# geth init genesis.json --datadir ./data
INFO [02-11|18:17:57.325] Maximum peer count                       ETH=50 LES=0 total=50
INFO [02-11|18:17:57.325] Smartcard socket not found, disabling    err="stat /run/pcscd/pcscd.comm: no such file or directory"
INFO [02-11|18:17:57.326] Allocated cache and file handles         database=/root/install/data/geth/chaindata cache=16.00MiB handles=16
INFO [02-11|18:17:57.343] Writing custom genesis block 
INFO [02-11|18:17:57.343] Persisted trie from memory database      nodes=0 size=0.00B time=12.012µs gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [02-11|18:17:57.344] Successfully wrote genesis state         database=chaindata                         hash=c1d47d…d9ea3e
INFO [02-11|18:17:57.344] Allocated cache and file handles         database=/root/install/data/geth/lightchaindata cache=16.00MiB handles=16
INFO [02-11|18:17:57.364] Writing custom genesis block 
INFO [02-11|18:17:57.365] Persisted trie from memory database      nodes=0 size=0.00B time=155.78µs gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [02-11|18:17:57.370] Successfully wrote genesis state         database=lightchaindata                         hash=c1d47d…d9ea3e

此步骤主要是利用创世块进行文件初始化,指定一个数据目录,当看到类似上面的结果代表初始化成功。

[root@192 install]# tree data/
data/
|-- geth
|   |-- chaindata
|   |   |-- 000002.ldb
|   |   |-- 000003.log
|   |   |-- ancient
|   |   |   |-- bodies.0000.cdat
|   |   |   |-- bodies.cidx
|   |   |   |-- diffs.0000.rdat
|   |   |   |-- diffs.ridx
|   |   |   |-- FLOCK
|   |   |   |-- hashes.0000.rdat
|   |   |   |-- hashes.ridx
|   |   |   |-- headers.0000.cdat
|   |   |   |-- headers.cidx
|   |   |   |-- receipts.0000.cdat
|   |   |   `-- receipts.cidx
|   |   |-- CURRENT
|   |   |-- CURRENT.bak
|   |   |-- LOCK
|   |   |-- LOG
|   |   `-- MANIFEST-000004
|   |-- lightchaindata
|   |   |-- 000001.log
|   |   |-- CURRENT
|   |   |-- LOCK
|   |   |-- LOG
|   |   `-- MANIFEST-000000
|   |-- LOCK
|   |-- nodekey
|   |-- nodes
|   |   |-- 000001.log
|   |   |-- CURRENT
|   |   |-- LOCK
|   |   |-- LOG
|   |   `-- MANIFEST-000000
|   `-- transactions.rlp
|-- geth.ipc
`-- keystore

启动Geth节点

geth --datadir ./data --networkid 18 --port 30303 --rpc  --rpcport 8545 --rpcapi 'db,net,eth,web3,personal' --rpccorsdomain "*" --gasprice 0 --allow-insecure-unlock  console 2> 1.log

这个命令的启动参数比较长,我们也需要针对参数进行介绍:

连接

启动后,将看到类似下面的结果:

[root@192 ~]# geth attach --datadir ./data/
Welcome to the Geth JavaScript console!
instance: Geth/v1.9.10-stable-58cf5686/linux-amd64/go1.13.6
at block: 0 (Thu, 01 Jan 1970 08:00:00 CST)
 datadir: /root/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
> 
上一篇下一篇

猜你喜欢

热点阅读