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的各种功能和使用

一、问题描述 九宫重排问题是指将一个3×3的数字矩阵打乱后,通过移动数字,使其恢复成初始状态的过程。其中空格可以与其上下左右的数字交换位置。 二、问题分析 1.问题的可解性 对于九宫重排问题,我们可以通过数学方法证明:只有在初始状态与目标状态的逆序对数奇偶性相同时,问题才有解。若逆序对数为奇数,则问题无解;若逆序对数为偶数,则问题有解。其中逆序对数指的是所有数字中,前面比后面大的数的对数。 2.问题的解法 对于九宫重排问题的解法,我们可以使用启发式搜索算法中的A*算法。该算法使用估价函数对每个状态进行估价,从而选择当前最优的状态进行搜索。对于九宫重排问题,我们可以使用曼哈顿距离(Manhattan Distance)作为估价函数。曼哈顿距离指的是每个数字到目标位置的曼哈顿距离之和,即每个数字的水平和垂直距离之和。在A*算法中,我们需要计算每个状态的代价(g值)和估价值(h值),最终选择代价和估价值之和最小的状态进行搜索。 3.问题的实现 对于九宫重排问题的实现,我们可以使用C++语言进行编写。具体实现方式如下: (1)定义结构体State,用于存储每个状态的信息,包括当前状态的矩阵、代价和估价值等。 (2)定义函数getH,用于计算每个状态的估价值,即曼哈顿距离。 (3)定义函数getNext,用于获取当前状态的所有可能后继状态。 (4)定义函数Astar,用于使用A*算法搜索最优解。 (5)定义函数main,用于获取输入的初始状态调用Astar函数进行搜索。 三、代码实现 以下是C++语言实现九宫重排问题的代码: ``` #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<algorithm> #include<queue> #include<map> using namespace std; const int MAXN=3; const int MAXM=3; const int MAXNM=MAXN*MAXM; struct State{ int a[MAXN][MAXM]; int cost; int f; bool operator<(const State &rhs)const{ return f>rhs.f; } }; int h(State s){ int ans=0; for(int i=0;i<MAXN;i++){ for(int j=0;j<MAXM;j++){ if(s.a[i][j]==0)continue; ans+=abs(i-(s.a[i][j]-1)/MAXM)+abs(j-(s.a[i][j]-1)%MAXM); } } return ans; } State getNext(State s){ State ans; int x,y; for(int i=0;i<MAXN;i++){ for(int j=0;j<MAXM;j++){ if(s.a[i][j]==0){ x=i; y=j; break; } } } int dx[4]={-1,0,1,0}; int dy[4]={0,1,0,-1}; for(int i=0;i<4;i++){ int nx=x+dx[i]; int ny=y+dy[i]; if(nx>=0&&nx<MAXN&&ny>=0&&ny<MAXM){ ans=s; swap(ans.a[x][y],ans.a[nx][ny]); ans.cost=s.cost+1; ans.f=ans.cost+h(ans); return ans; } } } int Astar(State s){ priority_queue<State>q; map<State,int>mp; s.cost=0; s.f=h(s); q.push(s); mp[s]=1; while(!q.empty()){ State t=q.top(); q.pop(); if(h(t)==0)return t.cost; for(int i=0;i<4;i++){ State ns=getNext(t); if(!mp[ns]){ q.push(ns); mp[ns]=1; } } } return -1; } int main(){ State s; for(int i=0;i<MAXN;i++){ for(int j=0;j<MAXM;j++){ cin>>s.a[i][j]; } } int ans=Astar(s); cout<<ans<<endl; return 0; } ``` 四、总结 九宫重排问题虽然在计算机领域中属于比较简单的问题,但是其解法却涉及到了许多计算机算法和数据结构的知识。对于程序员来说,学习和掌握九宫重排问题的解法,不仅可以加深对于计算机算法和数据结构的理解,同时也可以提高程序员的编程能力和解决问题的能力。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

已久依依

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

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

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

打赏作者

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

抵扣说明:

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

余额充值