目录
创世块文件
{
"config": {
"chainId": 18,
"homesteadBlock": 0,
"eip150Block": 0,
"eip155Block": 0,
"eip158Block": 0
},
"alloc" : {},
"coinbase" : "0x0000000000000000000000000000000000000000",
"difficulty" : "0x2",
"extraData" : "",
"gasLimit" : "0xffffffff",
"nonce" : "0x0000000000000042",
"mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
"parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
"timestamp" : "0x00"
}
创世块中对应字段说明:
key | 说明 |
---|---|
chainId | 网络ID,区分不同的区块链网络,值为0代表以太坊主网 |
coinbase | 一个账户地址,挖矿收益最终归属的账户 |
difficulty | 挖矿难度 |
gasLimit | 创世块能够消耗gas的上限,即最多消耗的gas值;智能合约运行在EVM上,运行机器码指令,每个指令都会对应相应的gas消耗,gas与以太不是等价的,它们之前有换算关系,gas * gasPrice = ether, gasPrice是gas单价(单位wei),可以上下浮动(感觉跟市场油价一样会发生变动) |
nonce | 随机数,挖矿的时候寻找到符合条件的nonce值 |
mixhash | 本块的hash值,因为是创世块,所以没有hash值,初始化为0 |
parentHash | 父块hash值,因为是创世块,所以没有父块hash值,初始化为0 |
timestamp | 时间戳,是从1970-01-01 00:00:00开始计算以秒为单位 |
Geth常用参数说明
可以通过geth -h帮助指令查看所以指令及对应功能说明,以下常用指令说明
指令 | 说明 |
---|---|
--datadir | 指定之前初始化的数据目录文件 |
--networkid | 区分不同的区块链网络,与创世块chainId一样,0为以太坊主网 |
--port | 节点之间互相通信的端囗,默认是30303 |
--rpc | 开启远程调用服务,执行智能合约时连接的节点是借助于rpc服务 |
--rpcport | 远程服务端囗,默认是8545 |
--rpcapi | 远程服务提供的远程服务调用函数集(db、net、eth、web3、personal等) |
--rpccorsdomain | 指定可以接收请求来源的域名列表(浏览器访问时必须开启),默认为 “*” |
--gasprice | gas的单价,单位wei |
--allow-insecure-unlock | 允许在Geth命令窗囗解锁账户(新版本1.9.0+增加的选项) |
--console | 进入管理后台(如修改rpc端囗) |
Geth启动步骤
1、生成创世块文件
[root@localhost geth-alltools]# mkdir rungeth #创建目录,用于存储创世块及其他执行脚本
[root@localhost geth-alltools]# cd rungeth #进入目录
[root@localhost rungeth]# vi genesis.json #生成创世块文件
将上面的创世块文件内容拷贝到genesis.json文件,之后保存即可。
注:chainId=18不是以太坊主网,挖矿挖到的以太并不值钱,所以搭建的是私链,用于开发使用。
2、初始化
第一次安装完Geth需要初始化,或是需要修改创世块重新设置时需要初始化,之后再不需要初始化。
[root@localhost rungeth]# geth init genesis.json --datadir ./data
INFO [03-23|23:34:45.106] Maximum peer count ETH=50 LES=0 total=50
INFO [03-23|23:34:45.106] Smartcard socket not found, disabling err="stat /run/pcscd/pcscd.comm: no such file or directory"
INFO [03-23|23:34:45.109] Allocated cache and file handles database=/usr/local/geth-alltools/rungeth/data/geth/chaindata cache=16.00MiB handles=16
INFO [03-23|23:34:45.112] Writing custom genesis block
INFO [03-23|23:34:45.112] Persisted trie from memory database nodes=0 size=0.00B time=8.39µs gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [03-23|23:34:45.113] Successfully wrote genesis state database=chaindata hash=c1d47d…d9ea3e
INFO [03-23|23:34:45.113] Allocated cache and file handles database=/usr/local/geth-alltools/rungeth/data/geth/lightchaindata cache=16.00MiB handles=16
INFO [03-23|23:34:45.114] Writing custom genesis block
INFO [03-23|23:34:45.114] Persisted trie from memory database nodes=0 size=0.00B time=1.605µs gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [03-23|23:34:45.115] Successfully wrote genesis state database=lightchaindata hash=c1d47d…d9ea3e
查看生成的data目录结构
[root@localhost rungeth]# tree
.
├── data
│ ├── geth
│ │ ├── chaindata
│ │ │ ├── 000001.log
│ │ │ ├── CURRENT
│ │ │ ├── LOCK
│ │ │ ├── LOG
│ │ │ └── MANIFEST-000000
│ │ └── lightchaindata
│ │ ├── 000001.log
│ │ ├── CURRENT
│ │ ├── LOCK
│ │ ├── LOG
│ │ └── MANIFEST-000000
│ └── keystore
└── genesis.json
5 directories, 11 files
3、启动Geth
(1)最简模式开启
此方式启动看日志比较方便:
geth --datadir ./data --networkid 18 --rpc --rpccorsdomain "*" --allow-insecure-unlock
[root@localhost rungeth]# geth --datadir ./data --networkid 18 --rpc --rpccorsdomain "*" --allow-insecure-unlock
INFO [03-23|23:44:43.206] Maximum peer count ETH=50 LES=0 total=50
INFO [03-23|23:44:43.206] Smartcard socket not found, disabling err="stat /run/pcscd/pcscd.comm: no such file or directory"
INFO [03-23|23:44:43.209] Starting peer-to-peer node instance=Geth/v1.9.10-stable-58cf5686/linux-amd64/go1.13.6
INFO [03-23|23:44:43.209] Allocated trie memory caches clean=256.00MiB dirty=256.00MiB
INFO [03-23|23:44:43.209] Allocated cache and file handles database=/usr/local/geth-alltools/rungeth/data/geth/chaindata cache=512.00MiB handles=2048
INFO [03-23|23:44:43.219] Opened ancient database database=/usr/local/geth-alltools/rungeth/data/geth/chaindata/ancient
INFO [03-23|23:44:43.219] Initialised chain configuration config="{ChainID: 18 Homestead: 0 DAO: <nil> DAOSupport: false EIP150: 0 EIP155: 0 EIP158: 0 Byzantium: <nil> Constantinople: <nil> Petersburg: <nil> Istanbul: <nil>, Muir Glacier: <nil>, Engine: unknown}"
INFO [03-23|23:44:43.219] Disk storage enabled for ethash caches dir=/usr/local/geth-alltools/rungeth/data/geth/ethash count=3
INFO [03-23|23:44:43.219] Disk storage enabled for ethash DAGs dir=/root/.ethash count=2
INFO [03-23|23:44:43.219] Initialising Ethereum protocol versions="[64 63]" network=18 dbversion=<nil>
WARN [03-23|23:44:43.219] Upgrade blockchain database version from=<nil> to=7
INFO [03-23|23:44:43.220] Loaded most recent local header number=0 hash=c1d47d…d9ea3e td=2 age=52y11mo3w
INFO [03-23|23:44:43.220] Loaded most recent local full block number=0 hash=c1d47d…d9ea3e td=2 age=52y11mo3w
INFO [03-23|23:44:43.220] Loaded most recent local fast block number=0 hash=c1d47d…d9ea3e td=2 age=52y11mo3w
INFO [03-23|23:44:43.220] Regenerated local transaction journal transactions=0 accounts=0
INFO [03-23|23:44:43.230] Allocated fast sync bloom size=512.00MiB
INFO [03-23|23:44:43.230] Initialized fast sync bloom items=0 errorrate=0.000 elapsed=24.285µs
INFO [03-23|23:44:43.235] New local node record seq=1 id=c4badd74b6951c5c ip=127.0.0.1 udp=30303 tcp=30303
INFO [03-23|23:44:43.237] IPC endpoint opened url=/usr/local/geth-alltools/rungeth/data/geth.ipc
INFO [03-23|23:44:43.237] HTTP endpoint opened url=http://127.0.0.1:8545 cors=* vhosts=localhost
INFO [03-23|23:44:43.239] Started P2P networking self=enode://84cc775b989288ae3941448d04b873208b1578b44e0241f42b3532590253bc9b649a2c42bccfcc0a4536aa2a5140e2ad0d2bf2bbcd931d151246ae026093d9f0@127.0.0.1:30303
开启一个新的命令窗囗,查看端囗是否启动:
[root@localhost ~]# netstat -an | grep 8545
tcp 0 0 127.0.0.1:8545 0.0.0.0:* LISTEN
[root@localhost ~]# netstat -an | grep 30303
tcp 0 1 192.168.237.141:49550 172.105.175.190:30303 SYN_SENT
tcp 0 1 192.168.237.141:46690 104.53.12.218:30303 SYN_SENT
tcp 0 0 192.168.237.141:47820 52.231.165.108:30303 FIN_WAIT2
tcp 0 0 192.168.237.141:55828 52.205.246.91:30303 TIME_WAIT
tcp 0 1 192.168.237.141:39324 124.122.84.22:30303 SYN_SENT
tcp 0 1 192.168.237.141:40006 18.132.38.236:30303 SYN_SENT
tcp 0 1 192.168.237.141:53696 54.238.114.159:30303 SYN_SENT
tcp 0 0 192.168.237.141:38830 195.201.167.175:30303 ESTABLISHED
tcp 0 0 192.168.237.141:44082 35.158.244.151:30303 FIN_WAIT2
tcp 0 1 192.168.237.141:58038 3.209.45.79:30303 SYN_SENT
tcp 0 0 192.168.237.141:32990 18.138.108.67:30303 FIN_WAIT2
tcp 0 1 192.168.237.141:37970 159.89.20.6:30303 SYN_SENT
tcp 0 1 192.168.237.141:59454 99.234.211.9:30303 SYN_SENT
tcp 0 0 192.168.237.141:52138 65.21.77.248:30303 TIME_WAIT
tcp 0 1 192.168.237.141:43326 167.71.74.162:30303 SYN_SENT
tcp 0 0 192.168.237.141:56296 95.179.234.37:30303 ESTABLISHED
tcp 0 0 192.168.237.141:48674 13.59.52.19:30303 TIME_WAIT
tcp 0 1 192.168.237.141:48778 5.1.83.226:30303 SYN_SENT
tcp6 0 0 :::30303 :::* LISTEN
udp6 0 0 :::30303 :::*
(2)连接节点
此方式默认使用的是data目录中的geth.ipc文件协助访问,只有geth.ipc文件存在才可以使用这种方式。
[root@localhost rungeth]# geth attach --datadir ./data
Welcome to the Geth JavaScript console!
instance: Geth/v1.9.10-stable-58cf5686/linux-amd64/go1.13.6
at block: 0 (Thu, 01 Jan 1970 08:00:00 CST)
datadir: /usr/local/geth-alltools/rungeth/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
>
(3)使用脚本启动
将启动命令生成一个脚本.sh文件,脚本内容如下:
#!/bin/bash
geth --datadir ./data --networkid 18 --port 30303 --rpc --rpcport 8545 --rpcapi 'db,net,eth,web3,personal' --rpccorsdomain "*" --gasprice 0 --allow-insecure-unlock console 2>1.log
[root@localhost rungeth]# vi rungeth.sh #生成脚本文件,将上面内容拷贝到文件中
[root@localhost rungeth]# ls #查看当前目录是否生成了脚本文件
data genesis.json rungeth.sh
[root@localhost rungeth]# ./rungeth.sh #执行脚本
-bash: ./rungeth.sh: Permission denied #发现没有权限
[root@localhost rungeth]# chmod 777 rungeth.sh #为脚本设置可执行权限
[root@localhost rungeth]# ./rungeth.sh #再次执行脚本,成功启动Geth
Welcome to the Geth JavaScript console!
instance: Geth/v1.9.10-stable-58cf5686/linux-amd64/go1.13.6
at block: 0 (Thu, 01 Jan 1970 08:00:00 CST)
datadir: /usr/local/geth-alltools/rungeth/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
>
查看日志:打开另一个窗囗
[root@localhost rungeth]# ls #查看日志文件
1.log data genesis.json rungeth.sh
[root@localhost rungeth]# tail -f 1.log #查看日志内容
INFO [03-24|00:41:48.949] Loaded most recent local fast block number=0 hash=c1d47d…d9ea3e td=2 age=52y11mo3w
INFO [03-24|00:41:48.949] Loaded local transaction journal transactions=0 dropped=0
INFO [03-24|00:41:48.949] Regenerated local transaction journal transactions=0 accounts=0
INFO [03-24|00:41:48.955] Allocated fast sync bloom size=512.00MiB
INFO [03-24|00:41:48.955] Initialized fast sync bloom items=0 errorrate=0.000 elapsed=32.495µs
INFO [03-24|00:41:49.004] New local node record seq=2 id=c4badd74b6951c5c ip=127.0.0.1 udp=30303 tcp=30303
INFO [03-24|00:41:49.006] IPC endpoint opened url=/usr/local/geth-alltools/rungeth/data/geth.ipc
INFO [03-24|00:41:49.006] HTTP endpoint opened url=http://127.0.0.1:8545 cors=* vhosts=localhost
INFO [03-24|00:41:49.006] Started P2P networking self=enode://84cc775b989288ae3941448d04b873208b1578b44e0241f42b3532590253bc9b649a2c42bccfcc0a4536aa2a5140e2ad0d2bf2bbcd931d151246ae026093d9f0@127.0.0.1:30303
WARN [03-24|00:41:49.054] Served eth_coinbase reqid=3 t=14.01µs err="etherbase must be explicitly specified"
Geth操作
使用上面脚本开启Geth,并保留查看日志窗囗,在Geth管理后台中执行如下操作:
1、查看所有账户
> eth.accounts
[]
结果是一个数组。
2、创建账户
> personal.newAccount("abc")
"0x26eb81b7bb07a517b08833f16061cb62e061eb7d"
以上操作中"abc"是账户密码,第二行是生成的账户地址。
3、挖矿
默认使用账户数组中的第一个账户进行挖矿。
> miner.start(1) #1表示开户1个线程
null
查看日志窗囗是否挖矿成功,我的大概2分钟后挖矿成功的,需要等一等。
4、查询余额、设置变量
> eth.getBalance("0x26eb81b7bb07a517b08833f16061cb62e061eb7d") #(1)根据账户地址查询
400000000000000000000
> eth.getBalance(eth.accounts[0]) #(2)根据账户列表索引值查询
420000000000000000000
> acc0=eth.accounts[0] #设置变量
"0x26eb81b7bb07a517b08833f16061cb62e061eb7d"
> eth.getBalance(acc0) #(3)根据设置的变量查询
525000000000000000000
5、转账、解锁
> personal.newAccount("xyz") #创建第二个账户,密码为xyz
"0xa4cbd286bd1a043339fab472fe9ccdccb1d5b1dc"
> eth.accounts #查看所有账户
["0x26eb81b7bb07a517b08833f16061cb62e061eb7d", "0xa4cbd286bd1a043339fab472fe9ccdccb1d5b1dc"]
> acc1=eth.accounts[1] #将第二个账户赋值给一个变量
"0xa4cbd286bd1a043339fab472fe9ccdccb1d5b1dc"
> miner.stop() #停止挖矿
null
> eth.getBalance(acc1) #查看第二个账户余额
0
> eth.getBalance(acc0) #查看第一个账户余额
1.09e+21
> eth.sendTransaction({from:acc0,to:acc1,value:web3.toWei(8)}) #转账
Error: authentication needed: password or unlock #发现需要解锁
at web3.js:3143:20
at web3.js:6347:15
at web3.js:5081:36
at <anonymous>:1:1
> personal.unlockAccount(acc0) #为付款账户解锁
Unlock account 0x26eb81b7bb07a517b08833f16061cb62e061eb7d
Password: #输入付款账户密码abc
true
> eth.sendTransaction({from:acc0,to:acc1,value:web3.toWei(8)}) #转账
"0x7174c00488badee8a99a04f0337143225112604437073f37bf448ce3bf3fc760"
> eth.getBalance(acc1) #查看第二个账户余额,余额没变
0
> eth.getBalance(acc0) #查看第一个账户余额,余额没变
1.09e+21
以上发起的转账是一笔交易,需要矿工打包挖矿后,交易才会被确认
> miner.start(1) #挖矿
null
> eth.getBalance(acc1) #查看第二个账户余额,余额已多了
8000000000000000000
> eth.getBalance(acc0) #查看第二个账户余额,余额变少了
1.147e+21