1.前言
使用Geth同步以太坊主網(wǎng)區(qū)塊的文章已經(jīng)很多了,這篇文章不會講的太詳細了他爸。
我們將在Windows Server 2012 R2安裝Geth但金,啟動節(jié)點同步區(qū)塊友题,并且使用JSON RPC方式查看節(jié)點和同步情況。
2. 安裝Geth
2.1 安裝Go
首先需要安裝Go語言環(huán)境园爷。下載地址:https://golang.org/dl/宠蚂。
下載windows
版本然后安裝,我在D
盤根目錄下新建了一個文件夾go
童社,然后指定安裝在此文件夾中求厕。系統(tǒng)會默認設(shè)置好環(huán)境變量。
檢查是否成功安裝:
PS C:\Users\Administrator> go version
go version go1.11 windows/amd64
2.2 安裝Geth
下載地址:https://geth.ethereum.org/downloads/。下載windows
版本的Geth然后安裝呀癣,我在D
盤根目錄下新建了一個文件夾geth
美浦,然后指定安裝在此文件夾中。
檢查是否成功安裝:
PS C:\Users\Administrator> geth version
Geth
Version: 1.8.17-stable
Git Commit: 8bbe72075e4e16442c4e28d999edee12e294329e
Architecture: amd64
Protocol Versions: [63 62]
Network Id: 1
Go Version: go1.11.1
Operating System: windows
GOPATH=C:\Users\Administrator\go
GOROOT=D:\go\
3. 啟動Geth
安裝完成后项栏,在D
盤的geth
文件夾中有一個geth.exe
程序浦辨。如果直接雙擊,那么geth會直接啟動并開始同步區(qū)塊沼沈,并且會將區(qū)塊數(shù)據(jù)存儲在C:\Users\Administrator\AppData\Roaming\Ethereum\geth\chaindata
中荤牍。區(qū)塊數(shù)據(jù)很大,會占用系統(tǒng)盤庆冕,所以我們需要手動輸入指令啟動Geth康吵,并指定區(qū)塊數(shù)據(jù)的存儲位置。
PS D:\geth> geth --identity "mainnet etherum" --datadir "./chain" --rpc --rpcapi "admin,web3,eth,personal,miner,net" --rpcadd
r 0.0.0.0 --rpcport 8545 --cache=1024 console
參數(shù)解釋:
--identity "mainnet etherum"
給節(jié)點起名--datadir "./chain"
將區(qū)塊數(shù)據(jù)和keystore
文件保存在當前路徑的chain
文件下--rpc
開啟RPC服務(wù)--rpcapi "admin,web3,eth,personal,miner,net"
注明要開啟哪些RPC服務(wù)--rpcadd r 0.0.0.0 --rpcport 8545
指定RPC服務(wù)的IP和端口--cache=1024
分配給Geth的內(nèi)存访递,理論上越大同步速度越快晦嵌,根據(jù)自己的配置來console
開啟控制臺
如果一切正常,不出一會拷姿,命令行將顯示類似下面的輸出惭载,表示正在同步區(qū)塊:
INFO [10-18|12:18:42.057] Imported new block headers count=0 elapsed=142.004ms number=6296034 hash=0894db…
442bf0 age=1mo1w2d ignored=192
INFO [10-18|12:18:42.426] Imported new block headers count=0 elapsed=17.999ms number=6296226 hash=49de5c…
9fa22f age=1mo1w2d ignored=192
INFO [10-18|12:18:42.730] Imported new block headers count=0 elapsed=31.249ms number=6296418 hash=5025a3…
5a063f age=1mo1w2d ignored=192
INFO [10-18|12:18:43.070] Imported new block receipts count=2 elapsed=514.800ms number=6295844 hash=2b2caa…
80d5f8 age=1mo1w2d size=271.18kB
INFO [10-18|12:18:43.214] Imported new block receipts count=8 elapsed=30.999ms number=6295852 hash=80df4d…
b6c7ee age=1mo1w2d size=685.60kB
INFO [10-18|12:18:43.591] Imported new block headers count=0 elapsed=28.001ms number=6296610 hash=b704f8…
5f6dd8 age=1mo1w2d ignored=192
INFO [10-18|12:18:43.974] Imported new block receipts count=46 elapsed=208.011ms number=6295898 hash=c952be
…098c9a age=1mo1w2d size=3.51mB
INFO [10-18|12:18:44.163] Imported new block headers count=0 elapsed=250.013ms number=6296802 hash=42eea2
…2bc9c2 age=1mo1w2d ignored=192
INFO [10-18|12:18:44.690] Imported new block receipts count=6 elapsed=15.000ms number=6295904 hash=44c418
…cc1c93 age=1mo1w2d size=388.41kB
INFO [10-18|12:18:45.058] Imported new block receipts count=46 elapsed=172.009ms number=6295950 hash=76f6c7
…7c0269 age=1mo1w2d size=4.00mB
INFO [10-18|12:18:44.870] Imported new block headers count=0 elapsed=30.000ms number=6296994 hash=ecc2e9
…77e65b age=1mo1w2d ignored=192
4. 添加節(jié)點
可能由于網(wǎng)絡(luò)原因,啟動后會遲遲連接不上其他節(jié)點响巢,導(dǎo)致無法同步區(qū)塊描滔。可以添加星火節(jié)點踪古。有兩種方式可以添加節(jié)點含长。
-
https://ethfans.org/wikis/ 中找到
星火節(jié)點計劃超級節(jié)點列表
的頁面,下載static-nodes.json伏穆,保存在D:\geth\chain
中拘泞。如果已經(jīng)啟動了Geth,需要重啟下
- 在Geth命令行中輸入指令
admin.addPeer(節(jié)點信息)
枕扫,或者調(diào)用API請求(后面會講到)陪腌。返回true
表示添加成功
不過不管是上面哪種方式添加的節(jié)點,最終都沒有連接上烟瞧。一般能連上1-4個節(jié)點诗鸭。不過經(jīng)過查看,都不是導(dǎo)入的節(jié)點参滴。有人說在Linux上就可以强岸,windows不行,這個有待驗證
5. 通過RPC API查看節(jié)點和同步狀態(tài)
5.1 web3_clientVersion
獲取當前客戶端版本信息
post
http://0.0.0.0:8545
method:
web3_clientVersion
params:
{"jsonrpc":"2.0","method":"web3_clientVersion","params":[],"id":1}
returns:
{
"jsonrpc": "2.0",
"id": 1,
"result": "Geth/mainnet etherum/v1.8.17-stable-8bbe7207/windows-amd64/go1.11.1"
}
result:當前客戶端版本信息
5.2 web3_clientVersion
獲取同步狀態(tài)
post
http://0.0.0.0:8545
method:
eth_syncing
params:
{"jsonrpc":"2.0","method":"eth_syncing","params":[],"id":1}
returns:
{
"jsonrpc": "2.0",
"id": 1,
"result": {
"currentBlock": "0x566683",
"highestBlock": "0x63a229",
"knownStates": "0x6c4ea8",
"pulledStates": "0x6c3fc9",
"startingBlock": "0x2f3add"
}
}
currentBlock:已同步區(qū)塊號十六進制
highestBlock:最新區(qū)塊號十六進制
5.3 net_peerCount
獲取當前連接節(jié)點數(shù)
post
http://0.0.0.0:8545
method:
net_peerCount
params:
{"jsonrpc":"2.0","method":"net_peerCount","params":[],"id":1}
returns:
{
"jsonrpc": "2.0",
"id": 1,
"result": "0x4"
}
result:當前已連接節(jié)點數(shù)
5.4 net_version
獲取當前已連接網(wǎng)絡(luò)的ID
post
http://0.0.0.0:8545
method:
net_version
params:
{"jsonrpc":"2.0","method":"net_version","params":[],"id":1}
returns:
{
"jsonrpc": "2.0",
"id": 1,
"result": "1"
}
result:當前已連接網(wǎng)絡(luò)的ID卵洗。1為主網(wǎng)ID请唱。
5.5 net_version
獲取本地節(jié)點信息
post
http://0.0.0.0:8545
method:
admin_nodeInfo
params:
{"jsonrpc":"2.0","method":"admin_nodeInfo","params":[],"id":1}
returns:
{
"jsonrpc": "2.0",
"id": 1,
"result": {
"id": "64984e1c278b77993da1b08207837ba11d670c7e6d93aa6055890a94b62df5er",
"name": "Geth/mainnet etherum/v1.8.17-stable-8bbe7207/windows-amd64/go1.11.1",
"enode": "enode://d5529482408109611a2b9816d18cd39645b6361ecc69ed6cb84193bf87a2b4911e3cb78dc2968eac714007c9160835d096eab8f5c5ba238e6116b4bc365f09a2@181.223.111.118:30303",
"ip": "183.222.121.113",
"ports": {
"discovery": 30303,
"listener": 30303
},
"listenAddr": "[::]:30303",
"protocols": {
"eth": {
"network": 1,
"difficulty": 17179869184,
"genesis": "0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3",
"config": {
"chainId": 1,
"homesteadBlock": 1150000,
"daoForkBlock": 1920000,
"daoForkSupport": true,
"eip150Block": 2463000,
"eip150Hash": "0x2086799aeebeae135c246c65021c82b4e15a2c451340993aacfd2751886514f0",
"eip155Block": 2675000,
"eip158Block": 2675000,
"byzantiumBlock": 4370000,
"ethash": {}
},
"head": "0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3"
}
}
}
}
result:當前已連接網(wǎng)絡(luò)的ID弥咪。1為主網(wǎng)ID。
5.6 admin_peers
獲取已連接節(jié)點信息
post
http://0.0.0.0:8545
method:
admin_peers
params:
{"jsonrpc":"2.0","method":"admin_peers","params":[],"id":1}
returns:
{
"jsonrpc": "2.0",
"id": 1,
"result": [
{
"enode": "enode://8a5f77665507bf6991537f08f0cac248a9a0edf2c7276920ee781741947039e1ff4223a188f5643d077353865d30fb8da4bf94606122edb4ed9ba9e0037d9cd1@200.108.64.94:30303",
"id": "2eacb087ef9fea6d7945df6f6aedd608403c25fa725ac040d8f572f2e04e825c",
"name": "Geth/v1.8.16-stable-477eb093/linux-amd64/go1.10",
"caps": [
"eth/62",
"eth/63"
],
"network": {
"localAddress": "10.0.1.110:59556",
"remoteAddress": "200.108.64.94:30303",
"inbound": false,
"trusted": false,
"static": false
},
"protocols": {
"eth": {
"version": 63,
"difficulty": 102970212347,
"head": "0xf37c632d361e0a93f08ba29b1a2c708d9caa3ee19d1ee8d2a02612bffe49f0a9"
}
}
},
{
"enode": "enode://33c163eb46d4105606e275b2b867a49b6538e3681cf95b9990e7459293d4daa128bcb8f6fd0463b7fcacab55f36e987a06401d2999553eed93900ad640d818d0@124.248.48.105:35555",
"id": "64a4f1ca60aa7fd9b94a488501eb8cd19617c850a5946489ae16a05ea0017897",
"name": "Geth/v1.8.14-unstable-46d47215/linux-amd64/go1.9.4",
"caps": [
"eth/63"
],
"network": {
"localAddress": "10.0.1.110:59807",
"remoteAddress": "124.248.48.105:35555",
"inbound": false,
"trusted": false,
"static": false
},
"protocols": {
"eth": {
"version": 63,
"difficulty": 7.252307770791149416925e+21,
"head": "0xeb2182027c770464bcf9294ce6fc6c89f463fc527093215b3657f8ad8ad34159"
}
}
},
{
"enode": "enode://f6bc8c6c8bd6509b0e420222d318b561e84ec4585c9fc1e9a703d120f57e7502da3d940cb0044e1429db8898d75d49d22d69abe5aafb2b11043d8b55dd41ed61@34.239.162.121:30303",
"id": "80d75f668d9202865a5eef26c052161e9dec539d63b5901844bf2ae6aaeb2f25",
"name": "Geth/v1.8.15-omnibus-255989da/linux-amd64/go1.10.1",
"caps": [
"eth/62",
"eth/63"
],
"network": {
"localAddress": "10.0.1.110:59181",
"remoteAddress": "34.239.162.121:30303",
"inbound": false,
"trusted": false,
"static": false
},
"protocols": {
"eth": {
"version": 63,
"difficulty": 7.252333281014280363969e+21,
"head": "0x517d365fc194212befd49b050c0438a522498e7c6a594a37612155f5e30ee3e2"
}
}
},
{
"enode": "enode://bcdce0388737748e94758373b3f1316b320d7846c663ac8cc413aca0e2b3072db8b4cc91543261f29e9f6be479df2caf4d01e2c528aa4615d79caaddd256cffe@47.100.213.201:30303",
"id": "80fc3de11cfb64fd3e1f3fd811ec50edf4efc278658175c8cd3895d354600c1b",
"name": "Geth/v1.8.13-unstable-526abe27/linux-amd64/go1.10.3",
"caps": [
"eth/62",
"eth/63"
],
"network": {
"localAddress": "10.0.1.110:59173",
"remoteAddress": "47.100.213.201:30303",
"inbound": false,
"trusted": false,
"static": false
},
"protocols": {
"eth": {
"version": 63,
"difficulty": 7.252253534510701838913e+21,
"head": "0x17af0a63587777f7cc9d3053a3433d7135cad21f65f0ef0132e073efed686d17"
}
}
}
]
}
result:已連接節(jié)點信息
5.7 admin_datadir
獲取區(qū)塊數(shù)據(jù)的存儲路徑
post
http://0.0.0.0:8545
method:
admin_datadir
params:
{"jsonrpc":"2.0","method":"admin_datadir","params":[],"id":1}
returns:
{
"jsonrpc": "2.0",
"id": 1,
"result": "D:\\geth\\chain"
}
result:區(qū)塊數(shù)據(jù)存儲路徑
5.8 admin_addPeer
添加連接節(jié)點
post
http://0.0.0.0:8545
method:
admin_addPeer
params:
{"jsonrpc":"2.0","method":"admin_addPeer","params":["enode://6427b7e7446bb05f22fe7ce9ea175ec05858953d75a5a6e4f99a6aec0779a8bd6276f1959a42fe5948acbe14bcd0652082dc546d3b37ae8f2aea41eba4eca43b@121.202.15.182:30303"],"id":1}
returns:
{
"jsonrpc": "2.0",
"id": 1,
"result": true
}
result:true表示添加成功