美文网首页
百问区块链中台:Geth以太坊搭建私链

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

作者: sknfie | 来源:发表于2021-02-11 23:32 被阅读0次

概述

在安装好了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"
}

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

  • coinbase 挖矿后获得奖励的账户地址
  • difficulty 挖矿难度
  • gasLimit 一个区块所能容纳gas的上限
  • nonce 随机值
  • mixhash 一个256位的哈希证明,与nonce相结合,验证本块的有效性
  • extraData 附加信息,随意填写
  • parentHash 前一块的hash值,由于是创世块,所以为0

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

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

  • datadir 指定之前初始化的数据目录文件
  • networkid 配置成与配置文件config内的chainId相同值,代表加入哪个网络,私链就自己随意编号即可
  • port 传说中的p2p端口,也就是节点之间互相通信的端口
  • rpc 代表开启远程调用服务,这对我们很重要
  • rpcport 远程服务的端口,默认是8545
  • rpcapi 远程服务提供的远程调用函数集
  • rpccorsdomain 指定可以接收请求来源的域名列表(浏览器访问,必须开启)
  • gasprice gas的单价
  • allow-insecure-unlock 新版本增加的选项,允许在Geth命令窗口解锁账户
  • console 进入管理台
  • 2> 1.log Unix系统下的重定向,将Geth产生的日志输出都重定向到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
> 

相关文章

网友评论

      本文标题:百问区块链中台:Geth以太坊搭建私链

      本文链接:https://www.haomeiwen.com/subject/osgrxltx.html