安装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冲突
初始化私链
- 选择一个目录作为数据存放目录,绝对路径与相对路径皆可,目录不存在则自动创建
- 执行以下指令: geth —datadir “testchain” init genesis.json
其中:E:\geth>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
E:\geth>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
>
其中:
- —identity: 区块链的标示,随便填写,用于标示目前网络的名字
- —datadir:指定数据文件夹
- —networkid 666:指定连接哪个网络,此处666为genesis.json中配置的网络ID
- —nodiscover: 表示不搜索其他节点
- console: 表示启动后打开控制台
- 2>log.txt: 错误记录重定向到log.txt
测试私链
创建账户
> eth.accounts
[]
> personal.newAccount("test1")
"0x193784cea02443b5aeedc3514d3b5f0f01e77eac"
> personal.newAccount("test2")
"0xacc1f4deff6900965c02e3a773113dcb8699fcce"
> eth.accounts
["0x193784cea02443b5aeedc3514d3b5f0f01e77eac", "0xacc1f4deff6900965c02e3a773113dcb8699fcce"]
> eth.getBalance(eth.accounts[0])
0
> eth.getBalance(eth.accounts[1])
0
>
挖矿测试
> eth.coinbase
"0x193784cea02443b5aeedc3514d3b5f0f01e77eac"
> eth.getBalance(eth.accounts[0])
0
> eth.getBalance(eth.accounts[1])
0
> eth.accounts
["0x193784cea02443b5aeedc3514d3b5f0f01e77eac", "0xacc1f4deff6900965c02e3a773113dcb8699fcce"]
> miner.start()
null
> eth.mining
true
> miner.stop()
null
> eth.getBalance(eth.accounts[0])
20000000000000000000
>