Go-ethereum 分析

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/makingLJ/article/details/86368526

Go-ethereum readme分析

区块链 Go-ethereum Geth


发布的可执行程序

编译好的可执行程序地址:

https://geth.ethereum.org/downloads/

编译源码

编译的先决条件及详细的编译指导可参见官网 Wiki

Building Ethereum

编译源码需要安装 GOC 编译器。

运行 make geth 可以编译 geth

或者,运行 make all 编译所有工具套件

可执行程序

cmd 目录中的程序

文件名 文件功能
geth 主要以太坊CLI客户端。包含了以太坊客户端的绝大部分功能
abigen 以太坊契约定义转换
bootnode 实现了对等网络的节点发现协议
evm EVM(以太坊虚拟机)的开发人员实用程序版本
gethrpctest rpc-test 测试套件的开发者实用工具
rlpdump 进行 RLP 编码转换的开发者实用工具
swarm Swarm 网络的守护进程和工具
puppeth 可帮助创建新的以太坊网络的CLI向导

运行以太坊程序

启动以太坊全节点

执行命令:

geth console

可以启动完整的以太坊节点,并同步以太坊公链的所有状态。

启动测试网络

执行命令:

geth --testnet console

启动以太坊程序,并使节点至连接到测试网络。该场景主要是用户只想了解以太坊程序的运行效果。该测试网络使用 POW 作为共识机制。

连接到知名的测试网络——Rinkeby

执行命令

geth --rinkeby console

该测试网络能避免重放攻击

配置

使用文件进行配置:

geth --config /path/to/your_config.toml

导出现有配置:

geth --your-favourite-flags dumpconfig

利用 Docker 启动程序

docker run -d --name ethereum-node -v /Users/alice/ethereum:/root \ -p 8545:8545 -p 30303:30303 \ ethereum/client-go

该命令在快速同步模式下启动 Geth,并配置 1G 持久存储

若要启动 RPC, 需要加上 -rpcaddr 0.0.0.0 才能使外部能够访问

Geth 节点的编程接口

Geth 提供了面向程序员的交互接口,其可以通过程序与以太坊进行交互。Geth 内置了基于 JSON-RPCWebSocketsIPC 的开放接口。IPC 是默认启动的并支持所有的 Geth 提供的 API,而 HTTPWebSocket 接口需要手动打开,并且由于安全原因(对互联网开发的 HTTP/WebSocket 接口可能会受到黑客的攻击),只打开部分接口。

HTTP based JSON-RPC API 选项:

  • --rpc 启动 the HTTP-RPC 服务器
  • --rpcaddr HTTP-RPC server listening interface (default: “localhost”)
  • --rpcport HTTP-RPC server listening port (default: 8545)
  • --rpcapi API’s offered over the HTTP-RPC interface (default: “eth,net,web3”)
  • --rpccorsdomain Comma separated list of domains from which to accept cross origin requests (browser enforced)
  • --ws Enable the WS-RPC server
  • --wsaddr WS-RPC server listening interface (default: “localhost”)
  • --wsport WS-RPC server listening port (default: 8546)
  • --wsapi API’s offered over the WS-RPC interface (default: “eth,net,web3”)
  • --wsorigins Origins from which to accept websockets requests
  • --ipcdisable Disable the IPC-RPC server
  • --ipcapi API’s offered over the IPC-RPC interface (default: “admin,debug,eth,miner,net,personal,shh,txpool,web3”)
  • --ipcpath Filename for IPC socket/pipe within the datadir (explicit paths escape it)

开发者可以使用自己的编程环境通过 HTTPWSIPC 等方式连接 Geth 节点,其中需要使用 JSON-RPC 作为数据传输格式。

操作私有网络(私有链)

维护私有网络比较复杂,因为需要手动配置官方网络中授予的许多配置

定义私有网络中的创世状态

通过编辑一个 JSON 文件进行配置创世状态,然后使用命令 geth init path/to/genesis.json 初始化

创建汇聚节点

将所有要运行的节点初始化为创世状态,然后启动一个引导节点以确保私有网络中的节点能够相互发现。

一个简洁的启动方法是通过命令:

$ bootnode --genkey=boot.key
$ bootnode --nodekey=boot.key

启动并配置引导节点。引导节点应该确保可以暴露一个其他节点都可以访问的 URL 。

启动私有网络的成员节点

启动汇聚节点后,可通过命令:

geth --datadir=path/to/custom/data/folder --bootnodes=<bootnode-enode-url-from-above>

启动私有成员节点。--datadir 选项指定私有连的数据存储位置,--bootnodes 指定汇聚节点的位置

运行私有网络的矿工

公有链的挖矿是一项需要 GPU 才能胜任的复杂任务。对于实践目的私有链,基于 CPU 的单线程程序足以胜任连续稳定产生块流的任务。

启动私有连的矿工命令:

geth <usual-flags> --mine --minerthreads=1 --etherbase=0x0000000000000000000000000000000000000000

其中的 <usual-flags> 保持跟公有链中的标志一致即可,挖矿产生的记录将写入到 --etherbas 指定的用户,也可以追加 --targetgaslimit 选项限制区块的 Gas 值限制,以及 --gasprice 指定交易价格

猜你喜欢

转载自blog.csdn.net/makingLJ/article/details/86368526