Geth的安装并简单使用篇

环境 Ubuntu20
geth : 1.10.5-stable
go: 1.17

前言

关于在Ubuntu对geth的安装方法有多种,可使用apt-get安装,用源代码构建和docker运行。
这里我选择使用源代码构建geth。

Geth对于硬件的要求:最好使用四核(或双核超线程)CPU,建议至少使用 16GB 内存。

下载go-ethereum的源代码

我选择构建1.10.5版本的geth

git clone -b v1.10.5 https://github.com/ethereum/go-ethereum.git

下载完毕当前目录下会有go-ethereum的文件夹

编译源代码

然后进入go-ethereum文件夹
然后执行命令

make geth

若在此步骤报错了,可能是你的go版本与go-ethereum不一致,导致编译失败,我是用1.17的golang
成功执行

编译完成可以检查是否编译成功,可以在其当前文件夹./build/bin文件夹的geth二进制文件查看版本
命令

./build/bin/geth version
Geth
Version: 1.10.5-stable
Git Commit: 33ca98ece985f5085ac8ec19a49239d726850004
Architecture: amd64
Go Version: go1.17.13
Operating System: linux
GOPATH=/usr/project/gowarehouse:/usr/project/goproject
GOROOT=/usr/local/go

配置全局变量

我们需要将其编译好的geth二进制文件进行全局应用

使用vim 在/etc/profile 文件最下面写上

export GETH=/media/rocfile/gethfile/go-ethereum  # 这里请写刚刚编译的 go-ethereum项目 文件夹
export PATH=$PATH:$GETH/build/bin

更新环境变量命令

source /etc/profile

查看geth版本

root@192-168-19-133:~# geth version
Geth
Version: 1.10.5-stable
Git Commit: 33ca98ece985f5085ac8ec19a49239d726850004
Architecture: amd64
Go Version: go1.17.13
Operating System: linux
GOPATH=/usr/project/gowarehouse:/usr/project/goproject
GOROOT=/usr/local/go

这样就可以全局应用geth啦

简单使用其geth[编写其genesis.json文件]

在某一个文件目录下创建一个data文件夹

mkdir data

然后在当前文件目录下vim 编译一个genesis.json配置文件
里面的内容是

{
"config" : { #配置段
"chainId": 122 #网络ID
},
"difficulty" : "1",#难度
"gasLimit" : "8000000", #gas限制
"alloc":{} #预分配帐号
}

之后的文章将会解释配置文件的参数用法和意思。

使用初始命令将创世信息写入

geth -datadir data init genesis.json

可以看到在data 生成了 geth和keystore文件夹

root@192-168-19-133:/yijiu/gethfile# geth -datadir data init genesis.json
INFO [09-11|09:45:38.957] Maximum peer count                       ETH=50 LES=0 total=50
INFO [09-11|09:45:38.957] Smartcard socket not found, disabling    err="stat /run/pcscd/pcscd.comm: no such file or directory"
INFO [09-11|09:45:38.957] Set global gas cap                       cap=50,000,000
INFO [09-11|09:45:38.957] Allocated cache and file handles         database=/yijiu/gethfile/data/geth/chaindata cache=16.00MiB handles=16
INFO [09-11|09:45:38.961] Writing custom genesis block
INFO [09-11|09:45:38.961] Persisted trie from memory database      nodes=0 size=0.00B time="29.853µs" gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [09-11|09:45:38.962] Successfully wrote genesis state         database=chaindata                           hash=567e85..683dd4
INFO [09-11|09:45:38.962] Allocated cache and file handles         database=/yijiu/gethfile/data/geth/lightchaindata cache=16.00MiB handles=16
INFO [09-11|09:45:38.964] Writing custom genesis block
INFO [09-11|09:45:38.964] Persisted trie from memory database      nodes=0 size=0.00B time="1.77µs"   gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [09-11|09:45:38.965] Successfully wrote genesis state         database=lightchaindata                           hash=567e85..683dd4
root@192-168-19-133:/yijiu/gethfile# ll
总用量 16
drwxr-xr-x 3 root root 4096 911 09:45 ./
drwxrwxrwx 9 root root 4096 911 09:44 ../
drwxr-xr-x 4 root root 4096 911 09:45 data/
-rw-r--r-- 1 root root   94 911 09:45 genesis.json
root@192-168-19-133:/yijiu/gethfile# cd data/
root@192-168-19-133:/yijiu/gethfile/data# ll
总用量 16
drwxr-xr-x 4 root root 4096 911 09:45 ./
drwxr-xr-x 3 root root 4096 911 09:45 ../
drwx------ 4 root root 4096 911 09:45 geth/
drwx------ 2 root root 4096 911 09:45 keystore/

执行启动geth命令并且进入geth的命令行界面

geth --networkid 122 --nodiscover --datadir data0 --rpc --rpcapi "net,eth,web3,personal" --rpcaddr 127.0.0.1 console

启动成功界面

root@192-168-19-133:/yijiu/gethfile/data# geth --networkid 122 --nodiscover --datadir data0 --rpc --rpcapi "net,eth,web3,personal" --rpcaddr 127.0.0.1 console
INFO [09-11|09:57:19.109] Maximum peer count                       ETH=50 LES=0 total=50
WARN [09-11|09:57:19.109] The flag --rpc is deprecated and will be removed June 2021, please use --http
WARN [09-11|09:57:19.109] The flag --rpcaddr is deprecated and will be removed June 2021, please use --http.addr
WARN [09-11|09:57:19.109] The flag --rpcapi is deprecated and will be removed June 2021, please use --http.api
INFO [09-11|09:57:19.109] Smartcard socket not found, disabling    err="stat /run/pcscd/pcscd.comm: no such file or directory"
INFO [09-11|09:57:19.109] Set global gas cap                       cap=50,000,000
INFO [09-11|09:57:19.109] Allocated trie memory caches             clean=154.00MiB dirty=256.00MiB
INFO [09-11|09:57:19.109] Allocated cache and file handles         database=/yijiu/gethfile/data/data0/geth/chaindata cache=512.00MiB handles=524,288
INFO [09-11|09:57:19.119] Opened ancient database                  database=/yijiu/gethfile/data/data0/geth/chaindata/ancient readonly=false
INFO [09-11|09:57:19.119] Writing default main-net genesis block
INFO [09-11|09:57:19.258] Persisted trie from memory database      nodes=12356 size=1.78MiB time=30.206371ms gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [09-11|09:57:19.259] Initialised chain configuration          config="{ChainID: 1 Homestead: 1150000 DAO: 1920000 DAOSupport: true EIP150: 2463000 EIP155: 2675000 EIP158: 2675000 Byzantium: 4370000 Constantinople: 7280000 Petersburg: 7280000 Istanbul: 9069000, Muir Glacier: 9200000, Berlin: 12244000, London: 12965000, Engine: ethash}"
INFO [09-11|09:57:19.259] Disk storage enabled for ethash caches   dir=/yijiu/gethfile/data/data0/geth/ethash count=3
INFO [09-11|09:57:19.259] Disk storage enabled for ethash DAGs     dir=/root/.ethash                          count=2
INFO [09-11|09:57:19.259] Initialising Ethereum protocol           network=122 dbversion=<nil>
INFO [09-11|09:57:19.259] Loaded most recent local header          number=0 hash=d4e567..cb8fa3 td=17,179,869,184 age=54y5mo3w
INFO [09-11|09:57:19.259] Loaded most recent local full block      number=0 hash=d4e567..cb8fa3 td=17,179,869,184 age=54y5mo3w
INFO [09-11|09:57:19.259] Loaded most recent local fast block      number=0 hash=d4e567..cb8fa3 td=17,179,869,184 age=54y5mo3w
WARN [09-11|09:57:19.259] Failed to load snapshot, regenerating    err="missing or corrupted snapshot"
INFO [09-11|09:57:19.259] Rebuilding state snapshot
INFO [09-11|09:57:19.260] Regenerated local transaction journal    transactions=0 accounts=0
INFO [09-11|09:57:19.260] Gasprice oracle is ignoring threshold set threshold=2
INFO [09-11|09:57:19.260] Resuming state snapshot generation       root=d7f897..0f0544 accounts=0 slots=0 storage=0.00B elapsed="492.609µs"
WARN [09-11|09:57:19.260] Error reading unclean shutdown markers   error="leveldb: not found"
INFO [09-11|09:57:19.261] Starting peer-to-peer node               instance=Geth/v1.10.5-stable-33ca98ec/linux-amd64/go1.17.13
INFO [09-11|09:57:19.266] IPC endpoint opened                      url=/yijiu/gethfile/data/data0/geth.ipc
INFO [09-11|09:57:19.266] New local node record                    seq=1 id=7a053ad755e70a5b ip=127.0.0.1 udp=0 tcp=30303
INFO [09-11|09:57:19.266] Started P2P networking                   self="enode://98597b60a94ce61e200eb3c0b0db5092207782645a6fcc23974637dadf6bdc4ad5b9b2038b7344409ee211f66ac091e419a3c2b0091f69008d558f758d9ca4d3@127.0.0.1:30303?discport=0"
INFO [09-11|09:57:19.267] HTTP server started                      endpoint=127.0.0.1:8545 prefix= cors= vhosts=localhost
WARN [09-11|09:57:19.311] Served eth_coinbase                      reqid=3 t="73.484µs" err="etherbase must be explicitly specified"
Welcome to the Geth JavaScript console!

instance: Geth/v1.10.5-stable-33ca98ec/linux-amd64/go1.17.13
at block: 0 (Thu Jan 01 1970 08:00:00 GMT+0800 (CST))
 datadir: /yijiu/gethfile/data/data0
 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
> INFO [09-11|09:57:19.357] Gen

创建新账号

> personal.newAccount()
Passphrase:   # 这里需要输入自定义的password
Repeat passphrase: # 重复输入password
INFO [09-11|09:59:20.841] Your new key was generated               address=0x443c2170A627e030d9e2e33084E391e30Ec450fB
WARN [09-11|09:59:20.841] Please backup your key file!             path=/yijiu/gethfile/data/data0/keystore/UTC--2023-09-11T01-59-19.392323977Z--443c2170a627e030d9e2e33084e391e30ec450fb
WARN [09-11|09:59:20.842] Please remember your password!
"0x443c2170a627e030d9e2e33084e391e30ec450fb"

查看链子的账户

> eth.accounts
["0x443c2170a627e030d9e2e33084e391e30ec450fb"]
>

结语

geth的搭建和简单配置使用已经讲解完毕,接下来的文章将详细说明geth的各种功能和使用

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

已久依依

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值