以太坊搭建私链

525 阅读6分钟

安装geth

直接在geth.ethereum.org/下载安装即可

配置初始化json

在geth.exe所在目录下,新建genesis.json文件,配置如下:

  "config": {
    "chainId": 666,
    "homesteadBlock": 0,
    "eip150Block": 0,
    "eip150Hash": "0x0000000000000000000000000000000000000000000000000000000000000000",
    "eip155Block": 0,
    "eip158Block": 0,
    "byzantiumBlock": 0,
    "constantinopleBlock": 0,
    "petersburgBlock": 0,
    "istanbulBlock": 0,
    "ethash": {}
  },
  "nonce": "0x0",
  "timestamp": "0x5ddf8f3e",
  "extraData": "0x0000000000000000000000000000000000000000000000000000000000000000",
  "gasLimit": "0x47b760",
  "difficulty": "0x00002",
  "mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
  "coinbase": "0x0000000000000000000000000000000000000000",
  "alloc": {},
  "number": "0x0",
  "gasUsed": "0x0",
  "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000"
}

其中:

  • difficulty:表示挖矿难度,参数越小越容易
  • gasLimit:转账交易手续费
  • nonce: 一个64位随机数,用于挖矿
  • extraData: 附加信息,可以填你的个性信息
  • coinbase: 矿工账号
  • alloc: 用来预置账号以及账号的以太币数量,因为私有链挖矿比较容易,所以我们不需要预置有币的账号,需要的时候自己创建即可以
  • timestamp: 设置创世块的时间戳
  • parentHash: 上一个区块的hash值,因为是创世块,所以这个值是0
  • chainId: 区块链识别ID,不可与主网及其他测试网ID冲突

初始化私链\

  1. 选择一个目录作为数据存放目录,绝对路径与相对路径皆可,目录不存在则自动创建
  2. 执行以下指令: geth --datadir "testchain" init genesis.json
INFO [01-22|14:17:37.013] Maximum peer count                       ETH=50 LES=0 total=50
INFO [01-22|14:17:37.165] Set global gas cap                       cap=25000000
INFO [01-22|14:17:37.167] Allocated cache and file handles         database=E:\geth\testchain\geth\chaindata cache=16.00MiB handles=16
INFO [01-22|14:17:37.246] Writing custom genesis block
INFO [01-22|14:17:37.248] Persisted trie from memory database      nodes=0 size=0.00B time=0s gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [01-22|14:17:37.253] Successfully wrote genesis state         database=chaindata                        hash="d3d6bb…c5304a"
INFO [01-22|14:17:37.257] Allocated cache and file handles         database=E:\geth\testchain\geth\lightchaindata cache=16.00MiB handles=16
INFO [01-22|14:17:37.363] Writing custom genesis block
INFO [01-22|14:17:37.364] Persisted trie from memory database      nodes=0 size=0.00B time=0s gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [01-22|14:17:37.369] Successfully wrote genesis state         database=lightchaindata                        hash="d3d6bb…c5304a"

其中:

  • --datadir:指定数据文件夹
  • init:表示初始化
  • genesis.json: 表示创始区块配置文件路径

启动私链

执行以下指令: geth --identity chain --datadir testchain --networkid 666 --nodiscover console 2>log.txt

Welcome to the Geth JavaScript console!

instance: Geth/chain/v1.9.25-stable-e7872729/windows-amd64/go1.15.6
at block: 0 (Thu Nov 28 2019 17:11:26 GMT+0800 (CST))
 datadir: E:\geth\testchain
 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
>
```](https://juejin.cn/passport/web/logou/)

### 安装geth

直接在<https://geth.ethereum.org/>下载安装即可


### 配置初始化json

在geth.exe所在目录下,新建genesis.json文件,配置如下:
```{
  "config": {
    "chainId": 666,
    "homesteadBlock": 0,
    "eip150Block": 0,
    "eip150Hash": "0x0000000000000000000000000000000000000000000000000000000000000000",
    "eip155Block": 0,
    "eip158Block": 0,
    "byzantiumBlock": 0,
    "constantinopleBlock": 0,
    "petersburgBlock": 0,
    "istanbulBlock": 0,
    "ethash": {}
  },
  "nonce": "0x0",
  "timestamp": "0x5ddf8f3e",
  "extraData": "0x0000000000000000000000000000000000000000000000000000000000000000",
  "gasLimit": "0x47b760",
  "difficulty": "0x00002",
  "mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
  "coinbase": "0x0000000000000000000000000000000000000000",
  "alloc": {},
  "number": "0x0",
  "gasUsed": "0x0",
  "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000"
}

其中:

  • difficulty:表示挖矿难度,参数越小越容易
  • gasLimit:转账交易手续费
  • nonce: 一个64位随机数,用于挖矿
  • extraData: 附加信息,可以填你的个性信息
  • coinbase: 矿工账号
  • alloc: 用来预置账号以及账号的以太币数量,因为私有链挖矿比较容易,所以我们不需要预置有币的账号,需要的时候自己创建即可以
  • timestamp: 设置创世块的时间戳
  • parentHash: 上一个区块的hash值,因为是创世块,所以这个值是0
  • chainId: 区块链识别ID,不可与主网及其他测试网ID冲突

初始化私链\

  1. 选择一个目录作为数据存放目录,绝对路径与相对路径皆可,目录不存在则自动创建
  2. 执行以下指令: geth --datadir "testchain" init genesis.json
INFO [01-22|14:17:37.013] Maximum peer count                       ETH=50 LES=0 total=50
INFO [01-22|14:17:37.165] Set global gas cap                       cap=25000000
INFO [01-22|14:17:37.167] Allocated cache and file handles         database=E:\geth\testchain\geth\chaindata cache=16.00MiB handles=16
INFO [01-22|14:17:37.246] Writing custom genesis block
INFO [01-22|14:17:37.248] Persisted trie from memory database      nodes=0 size=0.00B time=0s gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [01-22|14:17:37.253] Successfully wrote genesis state         database=chaindata                        hash="d3d6bb…c5304a"
INFO [01-22|14:17:37.257] Allocated cache and file handles         database=E:\geth\testchain\geth\lightchaindata cache=16.00MiB handles=16
INFO [01-22|14:17:37.363] Writing custom genesis block
INFO [01-22|14:17:37.364] Persisted trie from memory database      nodes=0 size=0.00B time=0s gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [01-22|14:17:37.369] Successfully wrote genesis state         database=lightchaindata                        hash="d3d6bb…c5304a"

其中:

  • --datadir:指定数据文件夹
  • init:表示初始化
  • genesis.json: 表示创始区块配置文件路径

启动私链

执行以下指令: geth --identity chain --datadir testchain --networkid 666 --nodiscover console 2>log.txt

Welcome to the Geth JavaScript console!

instance: Geth/chain/v1.9.25-stable-e7872729/windows-amd64/go1.15.6
at block: 0 (Thu Nov 28 2019 17:11:26 GMT+0800 (CST))
 datadir: E:\geth\testchain
 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
>