Geth-1.10.16 私链搭建
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 generatedPublic 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--654ffcc87b54280d5baa24ca7d366ad21e3a3c731 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/datamodules: 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.0To 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/datamodules: 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.0To 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/datamodules: 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.0To 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,所以余额会增加。
Geth-1.10.16 私链搭建相关推荐
- 以太坊私链搭建、truffle项目开发
本文实现以下目标: 搭建一条以太坊私链 用企业级开发方式开发.部署一个项目 分析truffle执行过程 solidity.web3等的一些说明 Token ERC20标准 contract ERC20 ...
- conflux私链搭建教程
首先可以在conflux官方开发者文档中找到相应教程,只不过官方教程有点不详细,我摸了很久才摸出来:conflux开发者文档中文,然后我演示的环境是在windows环境,其实也是通过linux工具gi ...
- 【eos系列】账户 钱包 私链搭建
简介 本篇文章,将跟大家介绍eos的基本入门操作,包括私链的搭建.如何创建钱包.如何创建账户等.大家在看完本章之后,应该能在本机上搭建好eos私链,学会创建钱包和账户,为后面做质押资源.转账.投票等基 ...
- Geth-1.9.10私链搭建
目录 创世块文件 Geth常用参数说明 Geth启动步骤 1.生成创世块文件 2.初始化 3.启动Geth (1)最简模式开启 (2)连接节点 (3)使用脚本启动 Geth操作 1.查看所有账户 2. ...
- 以太坊私链搭建(Windows+geth)
文章目录 1. Geth下载与安装 1.1 Geth下载 1.2 Geth安装 2. 搭建私有链 2.1 创世区块配置 2.2 创世区块初始化 3 启动私链节点 / 进入geth控制台 4 创建账户 ...
- 以太坊联盟链-多节点私链搭建手册
修订日期 姓名 邮箱 2018-09-23 brucefeng brucefeng@brucefeng.com 一. 前言 这半个月都在处理其他的事情,没来得及更新博客,今天看了下最近的博客访问量增加 ...
- 区块链私链搭建及api调用
区块链私链的搭建以及web3j的API调用 (参考文档) (http://blog.hubwiz.com/2018/07/10/web3j-index/. https://github.com/eth ...
- 以太坊DPOS私链搭建--使用gttc,搭建一个可用于性能测试的区块链框架(1)
遇到的一些坑 bootnode 一直不起作用,所以决定不用它,改用static-nodes.json 在阿里云机器上编译gttc 系统Ubuntu16.04 想把eth服务部署到docker中方便移植 ...
- 以太坊私链搭建(二)——genesis.json字段解读
genesis.json文件用于配置生成以太坊私链网络的创世区块,当我们需要去创建一个创世区块时,我们可以通过修改genesis.json文件内的初始参数将这些数据写入创世区块.下面是以太坊官方文档给 ...
最新文章
- python26 调用mysql 5.1
- 拷贝eclipse 工作空间 workspace 步骤
- 你的微服务敢独立交付么?
- 机器学习中样本不平衡处理办法
- Android学习起步 - 新建工程及相关
- 关于RT-Thread的背景和成长
- 160 - 14 bjanes.1
- c# 扩展方法奇思妙用高级篇五:ToString(string format) 扩展
- 超值赛题分享大礼包,你的“六一”礼物来咯!
- 暖心!阿里安全白帽挖漏洞做公益 连收2462封山区小孩感谢信
- 关于MPU6050姿态解算的一阶互补滤波方法(从原理到代码实现)
- netware php_Linux中NetWare客户端简易安装说明(转)
- 1.6 判断一个字符串是否由重复子字符串组成
- mysql 裸设备_什么叫做裸设备
- 单片机推挽输出c语言,单片机IO口科普:推挽输出、开漏输出详解
- 使用docker安装mysql8及mysql5.7
- 基于STM32楼梯层控制系统
- [生存志] 第15节 历代大事件概览 东汉
- 平板一定要用原装电容笔吗?十大电容笔知名品牌
- Eigen类型与ROS中tf相关消息类型进行相互转换工具