主页 > imtoken转币到交易所 > 使用Geth搭建以太坊私链

使用Geth搭建以太坊私链

imtoken转币到交易所 2023-05-20 06:39:12

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

以太坊节点服务器搭建_以太坊节点搭建有什么用_ubuntu搭建以太坊环境

网络图与本文无关

1.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

2.源码安装

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/geth
go: downloading github.com/cespare/cp v0.1.0
go: downloading golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2
go: downloading github.com/Azure/azure-storage-blob-go v0.7.0
go: extracting github.com/cespare/cp v0.1.0
go: extracting github.com/Azure/azure-storage-blob-go v0.7.0
go: downloading github.com/Azure/azure-pipeline-go v0.2.2
go: extracting github.com/Azure/azure-pipeline-go v0.2.2
go: downloading github.com/mattn/go-ieproxy v0.0.0-20190702010315-6dee0af9227d
go: extracting golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2
go: extracting github.com/mattn/go-ieproxy v0.0.0-20190702010315-6dee0af9227d
go: finding github.com/cespare/cp v0.1.0
go: downloading github.com/elastic/gosigar v0.8.1-0.20180330100440-37f05ff46ffa
............................
github.com/naoina/go-stringutil
github.com/naoina/toml/ast
github.com/naoina/toml
github.com/ethereum/go-ethereum/eth/tracers
github.com/ethereum/go-ethereum/eth
github.com/ethereum/go-ethereum/les
github.com/ethereum/go-ethereum/ethstats
github.com/ethereum/go-ethereum/cmd/utils
github.com/ethereum/go-ethereum/cmd/geth
Done building.
Run "./build/bin/geth" to launch geth.

4.geth版本,实际运行前确保安装正常

$ ./build/bin/geth version
Geth
Version: 1.8.0-unstable
Git Commit: e37f7be97e47a032d723db16d8b195998547805a Architecture: amd64
Protocol Versions: [63 62] Network Id: 1
Go Version: go1.9
Operating System: linux 
GOPATH=/home/ubuntu/project 
GOROOT=/usr/local/go

3.启动节点同步

1. Geth已经安装好了,现在我们可以尝试运行一下。 执行以下命令,geth会开始同步block,并存储在当前目录下。 这里的 --syncmode fast 参数意味着我们将以“快速”模式同步块。 默认值为full,如果什么都不加,则下载全节点。 在这种模式下,我们只会下载每个区块头和区块体,但不会验证所有交易,直到所有区块都同步后才能获取系统的当前状态。 这节省了大量的交易验证时间。

$ geth --datadir . --syncmode fast

通常情况下,在同步以太坊区块链时,客户端会在最开始,即从创世块开始,下载并验证每一个区块和每笔交易。 毫无疑问,如果我们不加--syncmode fast参数,sync会耗时长,对资源要求高(会需要更多的RAM,如果你没有fast storage,会耗时很久时间)。 有些文章会把这个参数写成--fast以太坊节点搭建有什么用,这是之前快速同步模式的参数写法,现在已经被--syncmode fast代替了。

2、默认同步主网区块。 如果我们要同步测试网的区块,可以使用如下命令:

$ geth --testnet --datadir . --syncmode fast

--testnet 该参数将告诉 geth 启动并连接到最新的测试网络,即 Ropsten。 测试网的区块数和交易数会明显少于主网,所以速度会更快。 但即使以快速模式同步测试网也需要数小时。

4.建立自己的私有链

1、由于公网区块太多以太坊节点搭建有什么用,同步时间太长,为了快速了解Geth,我们可以尝试用它搭建一条只属于我们自己的私有链。

首先,我们需要创建网络的“创世纪”状态,它被写在一个小的 JSON 文件中(例如,我们称之为 genesis.json):

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

config:整个block的配置

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一致。 稍后,如果您带上控制台,则会出现一个交互式控制台。

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

私链搭建到这里,感谢大家的支持!