0、安装环境
虚拟机中安装的操作系统:Centos7
参考官网文档:Private Networks | Go Ethereum
1、创建账户
(1)创建目录rungeth并进入
[root@localhost local]# cd geth-alltools-1.10.16/ #进入geth目录
[root@localhost geth-alltools-1.10.16]# mkdir rungeth #创建目录rungeth
[root@localhost geth-alltools-1.10.16]# cd rungeth #进入rungeth目录
(2)创建账户
[root@localhost rungeth]# geth account new --datadir data
INFO [03-25|00:43:10.089] Maximum peer count ETH=50 LES=0 total=50
INFO [03-25|00:43:10.089] Smartcard socket not found, disabling err="stat /run/pcscd/pcscd.comm: no such file or directory"
Your new account is locked with a password. Please give a password. Do not forget this password.
Password: #输入密码(我输入的是abc)
Repeat password: #确认密码
Your new key was generated
Public address of the key: 0x654FFcC87B54280d5BAA24CA7D366AD21E3A3C73 #生成账户地址
Path of the secret key file: data/keystore/UTC--2022-03-24T16-43-17.897096805Z--654ffcc87b54280d5baa24ca7d366ad21e3a3c73
- You can share your public address with anyone. Others need it to interact with you.
- You must NEVER share the secret key with anyone! The key controls access to your funds!
- You must BACKUP your key file! Without the key, it's impossible to access account funds!
- You must REMEMBER your password! Without the password, it's impossible to decrypt the key!
(3)查看生成目录及文件
[root@localhost rungeth]# tree data
data
└── keystore
└── UTC--2022-03-24T16-43-17.897096805Z--654ffcc87b54280d5baa24ca7d366ad21e3a3c73
1 directory, 1 file
(4)查看账户信息
[root@localhost rungeth]# cat data/keystore/UTC--2022-03-24T16-43-17.897096805Z--654ffcc87b54280d5baa24ca7d366ad21e3a3c73 | jq .
{
"address": "654ffcc87b54280d5baa24ca7d366ad21e3a3c73",
"crypto": {
"cipher": "aes-128-ctr",
"ciphertext": "3928f93d9aafef698c5ce1fe199a0437b0ac5efc853c94886a22bbff826f6282",
"cipherparams": {
"iv": "8976121cedcef7ba44be2176ce58f569"
},
"kdf": "scrypt",
"kdfparams": {
"dklen": 32,
"n": 262144,
"p": 1,
"r": 8,
"salt": "5981b38618d601c62befd9e40f38ba7de7cc82c83fcdef4e0900ab9318db59be"
},
"mac": "921527ffdc1ffc752157b114397f7d869e69840d80be58daec80d4217d272530"
},
"id": "b0a493e2-6d39-43b6-ae8e-6a3874e7e7d9",
"version": 3
}
也可通过如下方式查看账户(此账户查看不是在同一环境下生成,因此账户信息与上面不同,只参考查询方法即可)
[root@localhost rungeth]# geth account list --datadir data
INFO [04-05|20:26:40.568] Maximum peer count ETH=50 LES=0 total=50
INFO [04-05|20:26:40.568] Smartcard socket not found, disabling err="stat /run/pcscd/pcscd.comm: no such file or directory"
INFO [04-05|20:26:40.569] Set global gas cap cap=50,000,000
Account #0: {b151a6dfa0f453402518bcd48adc876da4feb3f2} keystore:///usr/local/geth-alltools-1.10.16/rungeth/data/keystore/UTC--2022-04-05T12-22-54.238533327Z--b151a6dfa0f453402518bcd48adc876da4feb3f2
2、初始化创世块
(1)生成创世块
创世块内容如下:
{
"config": {
"chainId": 108,
"homesteadBlock": 0,
"eip150Block": 0,
"eip155Block": 0,
"eip158Block": 0,
"byzantiumBlock": 0,
"constantinopleBlock": 0,
"petersburgBlock": 0,
"ethash": {}
},
"difficulty": "1",
"gasLimit": "8000000",
"alloc": {
"7df9a875a174b3bc565e6424a0050ebc1b2d1d82": { "balance": "300000" },
"f41c74c9ae680c1aa78f42e5647a62f353b7bdde": { "balance": "400000" }
}
}
创世块中对应字段说明:
key | 说明 |
---|---|
chainId | 网络ID,区分不同的区块链网络,值为0代表以太坊主网 |
difficulty | mine难度 |
gasLimit | 创世块能够消耗gas的上限,即最多消耗的gas值;智能合约运行在EVM上,运行机器码指令,每个指令都会对应相应的gas消耗,gas与以太不是等价的,它们之前有换算关系,gas * gasPrice = ether, gasPrice是gas单价(单位wei),可以上下浮动(感觉跟市场油价一样会发生变动) |
[root@localhost rungeth]# vi genesis.json
将创世块内容拷贝到文件,按ESC,输入:wq保存退出。
(2)初始化创世块
[root@localhost rungeth]# geth init --datadir data genesis.json
INFO [03-25|01:15:46.766] Maximum peer count ETH=50 LES=0 total=50
INFO [03-25|01:15:46.766] Smartcard socket not found, disabling err="stat /run/pcscd/pcscd.comm: no such file or directory"
INFO [03-25|01:15:46.767] Set global gas cap cap=50,000,000
INFO [03-25|01:15:46.767] Allocated cache and file handles database=/usr/local/geth-alltools-1.10.16/rungeth/data/geth/chaindata cache=16.00MiB handles=16
INFO [03-25|01:15:46.771] Writing custom genesis block
INFO [03-25|01:15:46.772] Persisted trie from memory database nodes=3 size=397.00B time="342.191µs" gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [03-25|01:15:46.772] Successfully wrote genesis state database=chaindata hash=c3638c..f97051
INFO [03-25|01:15:46.772] Allocated cache and file handles database=/usr/local/geth-alltools-1.10.16/rungeth/data/geth/lightchaindata cache=16.00MiB handles=16
INFO [03-25|01:15:46.774] Writing custom genesis block
INFO [03-25|01:15:46.775] Persisted trie from memory database nodes=3 size=397.00B time="166.189µs" gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [03-25|01:15:46.775] Successfully wrote genesis state database=lightchaindata hash=c3638c..f97051
3、启动Geth
(1)常用命令说明
可以通过geth -h帮助指令查看所以指令及对应功能说明,以下常用指令说明
指令 | 说明 |
---|---|
--datadir | 指定之前初始化的数据目录文件 |
--networkid | 区分不同的区块链网络,与创世块chainId一样,0为以太坊主网,私链自己随意编号 |
--http | 开启远程调用服务,执行智能合约时连接的节点是借助于http服务 |
--http.addr | 远程服务地址 |
--http.port | 远程服务端囗,默认是8545 |
--http.api | 远程服务提供的远程服务调用函数集(db、net、eth、web3、personal等) |
--http.corsdomain | 指定可以接收请求来源的域名列表(浏览器访问时必须开启),默认为 “*” |
--snapshot | 是否发现其它节点 |
--mine | 开启出块(miner.start()) |
--miner.threads | 设置出块的线程数量 |
--allow-insecure-unlock | 允许在Geth命令窗囗解锁账户(新版本1.9.0+增加的选项) |
--dev | 开发者模式 |
--dev.period | 在开发者模式中,产生交易后是否自动mine,1 是,0 否(默认) |
--console | 进入管理后台(如修改rpc端囗) |
2>1.log | 将Geth产生的日志输出重定向到1.log文件中 0 标准输入 1 标准输出 2 标准错误 |
(2)启动Geth
命令启动
命令如下:
geth --datadir ./data --networkid 108 --http --http.addr 0.0.0.0 --http.vhosts "*" --http.api "db,net,eth,web3,personal" --http.corsdomain "*" --snapshot=false --mine --miner.threads 1 --allow-insecure-unlock console 2> 1.log
[root@localhost rungeth]# geth --datadir ./data --networkid 108 --http --http.addr 0.0.0.0 --http.vhosts "*" --http.api "db,net,eth,web3,personal" --http.corsdomain "*" --snapshot=false --mine --miner.threads 1 --allow-insecure-unlock console 2> 1.log
Welcome to the Geth JavaScript console!
instance: Geth/v1.10.16-stable-20356e57/linux-amd64/go1.17.5
coinbase: 0x654ffcc87b54280d5baa24ca7d366ad21e3a3c73
at block: 0 (Thu Jan 01 1970 08:00:00 GMT+0800 (CST))
datadir: /usr/local/geth-alltools-1.10.16/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
To exit, press ctrl-d or type exit
>
以上Geth私链搭建成功。也可将以上命令生成一个脚本,见下脚本启动。
脚本启动
可将启动命令生成一个脚本文件,以后每次执行脚本文件即可,脚本内容如下:
#!/bin/bash
geth --datadir ./data --networkid 108 --port 30303 --http --http.addr 0.0.0.0 --http.vhosts "*" --http.api "db,net,eth,web3,personal" --http.corsdomain "*" --snapshot=false --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.10.16-stable-20356e57/linux-amd64/go1.17.5
coinbase: 0x865e7972b3b253cb9b39f5723b3c3f4115fca6f9
at block: 0 (Thu Jan 01 1970 08:00:00 GMT+0800 (CST))
datadir: /usr/local/geth-alltools-1.10.16/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
To exit, press ctrl-d or type exit
>
查看日志:打开一个新的命令窗囗
[root@localhost rungeth]# ls #查看日志文件
1.log data genesis.json rungeth.sh
[root@localhost rungeth]# tail -f 1.log #查看日志内容
INFO [04-04|17:12:25.107] Looking for peers peercount=0 tried=91 static=0
INFO [04-04|17:12:45.248] Looking for peers peercount=2 tried=99 static=0
INFO [04-04|17:12:55.334] Looking for peers peercount=0 tried=47 static=0
INFO [04-04|17:13:05.350] Looking for peers peercount=2 tried=86 static=0
INFO [04-04|17:13:15.408] Looking for peers peercount=0 tried=46 static=0
INFO [04-04|17:13:25.712] Looking for peers peercount=0 tried=86 static=0
INFO [04-04|17:13:55.958] Looking for peers peercount=0 tried=48 static=0
INFO [04-04|17:14:05.993] Looking for peers peercount=2 tried=59 static=0
ERROR[04-04|17:14:07.299] Snapshot extension registration failed peer=04dfa983 err="peer connected on snap without compatible eth support"
INFO [04-04|17:14:16.163] Looking for peers peercount=0 tried=118 static=0
开发者模式启动
在启动命令中加上开发者模式即可,即加上如下粗体字部分:
geth --datadir ./data --networkid 108 --port 30303 --http --http.addr 0.0.0.0 --http.vhosts "*" --http.api "db,net,eth,web3,personal" --http.corsdomain "*" --snapshot=false --allow-insecure-unlock --dev --dev.period 1 console 2>1.log
如出现错误:Fatal: Failed to unlock developer account: could not decrypt key with given password 解决方案参见:Geth 使用dev模式启动报错:Fatal: Failed to unlock developer account: could not decrypt key with given pass_ling1998的博客-CSDN博客
当然,也可以把开发者模式命令保存成一个脚本文件,每次直接执行脚本文件即开启GETH。
进入已启动的Geth(通过IPC进入)
若已启动Geth,可通过如下命令进入控制台:
[root@localhost rungeth]# geth attach data/geth.ipc #加上geth.ipc所在路径即可
Welcome to the Geth JavaScript console!
instance: Geth/v1.10.16-stable-20356e57/linux-amd64/go1.17.5
coinbase: 0xb151a6dfa0f453402518bcd48adc876da4feb3f2
at block: 140 (Wed Apr 06 2022 00:09:46 GMT+0800 (CST))
datadir: /usr/local/geth-alltools-1.10.16/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
To exit, press ctrl-d or type exit
>
注:第一个区块出块时间较长,大概2~5分钟左右,耐心等待,确保已开启 miner.start()
4、常用命令操作
(1)查看账户
> eth.accounts
["0x654ffcc87b54280d5baa24ca7d366ad21e3a3c73"]
(2)赋值
> acc0=eth.accounts[0]
"0x654ffcc87b54280d5baa24ca7d366ad21e3a3c73"
(3)查询账户余额
> eth.getBalance(acc0)
396000000000000000000
> eth.getBalance(acc0)
562000000000000000000
一直在mining,所以余额会增加。