eth搭建私链(使用Geth搭建以太坊私有链)

  eth搭建私链(使用Geth搭建以太坊私有链)

作者:李鹏 2019/11/25 02!19

  

因为 Geth 安装有很多种方式,我这里主要就 Linux 环境给出两种!系统包管理器(apt-get)安装和源码安装。推荐大家用源码安装,因为在整个安装过程中可以看到 Geth 各组件的构建步骤。

  

网络配图与本文无关

  

一、apt-get方式

  

$ sudo apt-get install software-properties-common $ sudo add-apt-repository -y ppa!ethereum/ethereum $ sudo apt-get update $ sudo apt-get install ethereum

二、源码安装

  

1、克隆 github 仓库,取源代码

  

$ git clone https!//github。com/ethereum/go-ethereum。git

2、构建 Geth,切换到下载源代码的目录并使用 make 命令!

  

$ cd go-ethereum $ make geth

3、我们将看到 Go 编译器构建每个组件的构建信息(部分信息我这里已省略),直到它生成 geth 可执行文件

  

build/env。sh go run build/ci。go install 。/cmd/gethgo! downloading github。com/cespare/cp v0。1。0go! downloading golang。org/x/crypto v0。0。0-20190308221718-c2843e01d9a2go! downloading github。com/Azure/azure-storage-blob-go v0。7。0go! extracting github。com/cespare/cp v0。1。0go! extracting github。com/Azure/azure-storage-blob-go v0。7。0go! downloading github。com/Azure/azure-pipeline-go v0。2。2go! extracting github。com/Azure/azure-pipeline-go v0。2。2go! downloading github。com/mattn/go-ieproxy v0。0。0-20190702010315-6dee0af9227dgo! extracting golang。org/x/crypto v0。0。0-20190308221718-c2843e01d9a2go! extracting github。com/mattn/go-ieproxy v0。0。0-20190702010315-6dee0af9227dgo! finding github。com/cespare/cp v0。1。0go! downloading github。com/elastic/gosigar v0。8。1-0。20180330100440-37f05ff46ffa。。。。。。。。。。。。。。。。。。。。。。。。。。。。github。com/naoina/go-stringutilgithub。com/naoina/toml/astgithub。com/naoina/tomlgithub。com/ethereum/go-ethereum/eth/tracersgithub。com/ethereum/go-ethereum/ethgithub。com/ethereum/go-ethereum/lesgithub。com/ethereum/go-ethereum/ethstatsgithub。com/ethereum/go-ethereum/cmd/utilsgithub。com/ethereum/go-ethereum/cmd/gethDone building。Run ";。/build/bin/geth"; to launch geth。

4、geth version,确保在真正运行之前安装正常

  

$ 。/build/bin/geth versionGethVersion! 1。8。0-unstableGit Commit! e37f7be97e47a032d723db16d8b195998547805a Architecture! amd64Protocol Versions! [63 62] Network Id! 1Go Version! go1。9Operating System! linux GOPATH=/home/ubuntu/project GOROOT=/usr/local/go

三、启动节点同步

  

1、安装好了 Geth,现在我们可以尝试运行一下它。执行下面的命令,geth 就会开始同步区块,并存储在当前目录下。这里的 --syncmode fast 参数表示我们会以“快速”模式同步区块。默认值为full,如过什么不加就是下载全节点。在这种模式下,我 们只会下载每个区块头和区块体,但不会执行验证所有的交易,直到所有区块同步完毕再去获取一个系统当前的状态。这样就节省了很多交易验证的时间。

  

$ geth --datadir 。 --syncmode fast

  1. 通常,在同步以太坊区块链时,客户端会一开始就下载并验证每个块和每个交易,也就是说从创世区块开始。 毫无疑问,如果我们不加 --syncmode fast 参数,同步将花费很长时间并且具有很高的资源要求(它将需要更多的 RAM,如果你没有快速存储,则需要很长时间)。
  2. 有些文章会把这个参数写成 --fast,这是以前快速同步模式的参数写法,现在已经被 –syncmode fast取代。

2、默认是同步主网络的区块,如果我们想同步测试网络的区块,可以用下面的命令!

  

$ geth --testnet --datadir 。 --syncmode fast

--testnet 这个参数会告诉 geth 启动并连接到最新的测试网络,也就是 Ropsten。测试网络的区块和交易数量会明显少于主网,所以会更快一点。但即使是用快速模式同步测试网络,也会需要几个小时的时间。

  

四、搭建自己的私有链

  

1、因为公共网络的区块数量太多,同步耗时太长,我们为了方便快速了解 Geth,可以试着用它来搭一个只属于自己的私链。

  

  

首先,我们需要创建网络的“创世”(genesis)状态,这写在一个小小的 json 文件里(例如,我们将其命名为 genesis。json)!

  

{ ";config";! { ";chainId";! 15 }, ";difficulty";! ";2000";, ";gasLimit";! ";2100000";, ";alloc";! { ";0x4CB6940fEDD0EaBf33288DaB4C28E45796FD7D7f";! { ";balance";! ";300000000000000000"; }, ";f41c74c9ae680c1aa78f42e5647a62f353b7bdde";! { ";balance";! ";400000"; } }}

config:整个区块的配置

  

difficulty:难度系数(根据当前需求,难度系数越大,挖矿难度越大)

  

gasLimit:一个区块要求多少个gas

  

alloc:key:地址 value:账户余额 。

  

2、要创建一条以它作为创世块的区块链,我们可以使用下面的命令!

  

。/build/bin/geth --datadir /usr/local/alexlp/mychain/data init /usr/local/alexlp/mychain/genesis。json

3、在当前目录下运行 geth,就会启动这条私链,注意要将 networked 设置为与创世块配置里的chainId一致。后面如果带console会出现交互控制台。比特币走势

  

。/build/bin/geth --datadir /usr/local/alexlp/mychain/data --networkid 15 [console]

到这里私有链已经搭建完成,感谢支持!

  
","content_hash"!"bb8aceb8

版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

评论