目录
操作系统: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
}
2、初始化创世块
(1)生成创世块
创世块内容如下:
创世块中对应字段说明:
key | 说明 |
---|---|
chainId | 网络ID,区分不同的区块链网络,值为0代表以太坊主网 |
difficulty | 挖矿难度 |
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.threads | 设置挖矿的线程数量 |
--allow-insecure-unlock | 允许在Geth命令窗囗解锁账户(新版本1.9.0+增加的选项) |
--console | 进入管理后台(如修改rpc端囗) |
2>1.log | 将Geth产生的日志输出重定向到1.log文件中 0 标准输入 1 标准输出 2 标准错误 |
(2)启动Geth
命令如下:
[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
>
(3)查看账户
> eth.accounts
["0x654ffcc87b54280d5baa24ca7d366ad21e3a3c73"]
>
以上Geth私链搭建成功。