这篇文档,是记录自己搭建以太坊私链的过程。
安装
以太坊搭建私链的安装方式有很多,其中之一就是源码安装。
源码安装
下载go-ethereum 源码,编译:
git clone https://github.com/ethereum/go-ethereum.git
在目录下,编译:
make geth
显示:
env GO111MODULE=on go run build/ci.go install ./cmd/geth
......
......
github.com/ethereum/go-ethereum/cmd/geth
Done building.
Run "./build/bin/geth" to launch geth.
编译后的文件,在./build/bin/geth 下,会产生geth 二进制文件。
之后,配置环境变量:
export PATH=$PATH:/root/eth/go-ethereum/build/bin
即可在任意地方使用。
官网下载
在官网上,直接下载编译好的二进制文件:
Downloads | Go Ethereum
初始化
在开始搭建私链之前,需要配置一个创世文件:genesis.json ,一般的配置如下所示:
{
"config": {
"chainId": 10,
"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": "0x00200",
"mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"coinbase": "0x0000000000000000000000000000000000000000",
"alloc": { },
"number": "0x0",
"gasUsed": "0x0",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000"
}
配置好后,创建一个空的文件夹,比如datachain ,之后,一个节点的产生的数据都存储在这个目录文件下。
创建好后,开启初始化节点,运行命令:
geth --datadir ./datachain init genesis.json
显示日志:
root@cchui-virtual-machine:~/eth
INFO [07-26|20:32:06.444] Maximum peer count ETH=50 LES=0 total=50
INFO [07-26|20:32:06.444] Smartcard socket not found, disabling err="stat /run/pcscd/pcscd.comm: no such file or directory"
INFO [07-26|20:32:06.445] Set global gas cap cap=50,000,000
INFO [07-26|20:32:06.445] Allocated cache and file handles database=/root/eth/datachain/geth/chaindata cache=16.00MiB handles=16
INFO [07-26|20:32:06.449] Writing custom genesis block
INFO [07-26|20:32:06.449] Persisted trie from memory database nodes=0 size=0.00B time="8.341μs" gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [07-26|20:32:06.451] Successfully wrote genesis state database=chaindata hash=d3d6bb..c5304a
INFO [07-26|20:32:06.451] Allocated cache and file handles database=/root/eth/datachain/geth/lightchaindata cache=16.00MiB handles=16
INFO [07-26|20:32:06.454] Writing custom genesis block
INFO [07-26|20:32:06.454] Persisted trie from memory database nodes=0 size=0.00B time="1.497μs" gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [07-26|20:32:06.456] Successfully wrote genesis state database=lightchaindata hash=d3d6bb..c5304a
即,表示成功初始以太坊网络。
启动以太坊私链
启动
开启一个Geth 节点,输入下面的命令:
geth --identity "test Chain" --datadir ./datachain --rpc --rpcport 8547 --ws --ws.port=8546 --http.port=8548 --rpccorsdomain "*" --port 1718 --nodiscover --nat "any" --networkid 1108 --rpcapi "eth,net,web3,personal" console
启动节点后,即可进入节点控制台,和节点进行交互。
创建账户
> eth.accounts
["0x471144a5fec2d48450d6e14438a448be839dcd04"]
> personal.newAccount()
Passphrase:
Repeat passphrase:
INFO [07-26|20:46:17.128] Your new key was generated address=0x487366D8E76b5aD71dfA047997681F8A3AD8B75B
WARN [07-26|20:46:17.128] Please backup your key file! path=/root/eth/datachain/keystore/UTC--2021-07-26T12-46-15.974003905Z--487366d8e76b5ad71dfa047997681f8a3ad8b75b
WARN [07-26|20:46:17.128] Please remember your password!
"0x487366d8e76b5ad71dfa047997681f8a3ad8b75b"
> eth.accounts
["0x471144a5fec2d48450d6e14438a448be839dcd04", "0x487366d8e76b5ad71dfa047997681f8a3ad8b75b"]
账户余额
> eth.getBalance(eth.accounts[0])
0
挖矿
注意,在节点第一次挖矿的时候,时间会比较长。需要进行DAG的初始化,等到DAG 进程加载到 100 时,才会开始挖矿。
> miner.start(1)
INFO [07-26|20:49:45.475] Generating DAG in progress epoch=0 percentage=95 elapsed=1m13.544s
INFO [07-26|20:49:46.189] Generating DAG in progress epoch=0 percentage=96 elapsed=1m14.258s
INFO [07-26|20:49:46.901] Generating DAG in progress epoch=0 percentage=97 elapsed=1m14.969s
INFO [07-26|20:49:47.607] Generating DAG in progress epoch=0 percentage=98 elapsed=1m15.676s
INFO [07-26|20:49:48.368] Generating DAG in progress epoch=0 percentage=99 elapsed=1m16.437s
INFO [07-26|20:49:48.369] Generated ethash verification cache epoch=0 elapsed=1m16.438s
INFO [07-26|20:49:48.581] Successfully sealed new block number=1 sealhash=cfc590..c87647 hash=30c2a3..4ca68a elapsed=1m17.119s
INFO [07-26|20:49:48.581] 🔨 mined potential block number=1 hash=30c2a3..4ca68a
INFO [07-26|20:49:48.581] Commit new mining work number=2 sealhash=f57a2e..1f5426 uncles=0 txs=0 gas=0 fees=0 elapsed="137.234μs"
> miner.stop()
多节点组网
帮助
root@cchui-virtual-machine:~/eth
总结:
参考
|