准备创世纪区块
创建一个名为genesis.json的文件
{
"config": {
"chainId": 1234,
"homesteadBlock": 0,
"eip150Block": 0,
"eip155Block": 0,
"eip158Block": 0
},
"alloc" : {},
"coinbase" : "0x0000000000000000000000000000000000000000",
"difficulty" : "0x20000",
"extraData" : "",
"gasLimit" : "0x2fefd8",
"nonce" : "0x0000000000000042",
"mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
"parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
"timestamp" : "0x00"
}
参数说明
参数 | 说明 |
---|---|
chainId | 不同版本的eth的id,其中 1代表主网,2代表普通测试网络,3代表Ropsten测试网络,4代表Rinkeby测试网络,chainId的值要与在geth命令中的--networkid参数保持一致。更多ID可参考:ETH公链ID |
homesteadBlock | Homestead 硬分叉区块高度,默认值即可,后续硬分叉时才需要调整 |
eip150Block | EIP 150 硬分叉高度,默认值即可,后续硬分叉时才需要调整 |
eip155Block | EIP 155 硬分叉高度,默认值即可,后续硬分叉时才需要调整 |
eip158Block | EIP 158 硬分叉高度,默认值即可,后续硬分叉时才需要调整 |
alloc | 预设账号以及账号的以太币数量,私有链挖矿比较容易可以不配置 |
coinbase | 矿工账号 |
difficulty | 挖坑难度值,越大越难 |
extraData | 附加信息,以0x开头填写 |
gasLimit | gas 的消耗总量限制,用来限制区块能包含的交易信息总和,因为我们是私有链,所以填的很大 |
nonce | 64 位随机数,默认即可 |
mixhash | 与 nonce 配合用于挖矿,由上一个区块的一部分生成的 hash,默认即可。因为是第一个区块,因此,默认就是0 |
parentHash | 上一个区块的 hash 值,默认即可。因为是第一个区块,因此,默认就是0 |
初始化创世纪块
geth --datadir ./data init genesis.json
启动私有链
geth --datadir ./data --networkid 1234 --port 1234 --nodiscover --maxpeers 300 --http --http.addr 0.0.0.0 --http.vhosts "*" --http.api "db,net,eth,web3,personal" --http.corsdomain "*" --allow-insecure-unlock --verbosity 4 console 2>runGeth.log
- datadir 数据存放目录
- networkid network ID需要和初始化创世纪块的chainId保持一致
- prot 端口
- nodiscover 关闭节点自动发现
- maxpeers 最大节点连接数
- http 启动http
- allow-insecure-unlock 新版本geth,出于安全考虑,默认禁止了HTTP通道解锁账户,开启此功能
- verbosity 日志等级:0=silent, 1=error, 2=warn, 3=info, 4=debug, 5=detail (default: 3)
启动显示
Welcome to the Geth JavaScript console!
instance: Geth/v1.10.20-stable-8f2416a8/darwin-amd64/go1.18.1
coinbase: 0x0b11b803f6bd217885012d050a97e76b5a747ef9
at block: 0 (Thu Jan 01 1970 08:00:00 GMT+0800 (CST))
datadir: /Users/coder/install/geth-1.10.20/data
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 or type exit
eth相关命令
* 查看区块信息
eth.getBlock(0)
- 创建账号,密码为123455
personal.newAccount('123456')
* 查看账户
eth.accounts
eth.accounts[0]
* 查看余额
eth.getBalance(eth.accounts[0])
web3.fromWei(eth.getBalance(eth.accounts[0]),'ether')
* 开启,停止挖矿
miner.start()
miner.stop()
* 转账
要先开启挖矿,查看当前0账号的余额
web3.fromWei(eth.getBalance(eth.accounts[0]),'ether')
解锁账号
personal.unlockAccount(eth.accounts[0],'123456')
转账
eth.sendTransaction({from:eth.accounts[0],to:eth.accounts[1],value:web3.toWei(1,"ether")})
查看2账号的余额
web3.fromWei(eth.getBalance(eth.accounts[1]),'ether')