以太坊开发(二十八)在Windows服务器上使用Geth同步以太坊主网区块

yuyangray · · 1812 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

1.前言

使用Geth同步以太坊主网区块的文章已经很多了,这篇文章不会讲的太详细了。

我们将在Windows Server 2012 R2安装Geth,启动节点同步区块,并且使用JSON RPC方式查看节点和同步情况。

2. 安装Geth

2.1 安装Go

首先需要安装Go语言环境。下载地址:https://golang.org/dl/
下载windows版本然后安装,我在D盘根目录下新建了一个文件夹go,然后指定安装在此文件夹中。系统会默认设置好环境变量。

检查是否成功安装:

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会直接启动并开始同步区块,并且会将区块数据存储在C:\Users\Administrator\AppData\Roaming\Ethereum\geth\chaindata中。区块数据很大,会占用系统盘,所以我们需要手动输入指令启动Geth,并指定区块数据的存储位置。

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

参数解释:

  • --identity "mainnet etherum" 给节点起名

  • --datadir "./chain" 将区块数据和keystore文件保存在当前路径的chain文件下

  • --rpc 开启RPC服务

  • --rpcapi "admin,web3,eth,personal,miner,net" 注明要开启哪些RPC服务

  • --rpcadd r 0.0.0.0 --rpcport 8545 指定RPC服务的IP和端口

  • --cache=1024 分配给Geth的内存,理论上越大同步速度越快,根据自己的配置来

  • console 开启控制台

如果一切正常,不出一会,命令行将显示类似下面的输出,表示正在同步区块:

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. 添加节点

可能由于网络原因,启动后会迟迟连接不上其他节点,导致无法同步区块。可以添加星火节点。有两种方式可以添加节点。

  • 在Geth命令行中输入指令admin.addPeer(节点信息),或者调用API请求(后面会讲到)。返回true表示添加成功

不过不管是上面哪种方式添加的节点,最终都没有连接上。一般能连上1-4个节点。不过经过查看,都不是导入的节点。有人说在Linux上就可以,windows不行,这个有待验证

5. 通过RPC API查看节点和同步状态

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 获取同步状态

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:已同步区块号十六进制
highestBlock:最新区块号十六进制

5.3 net_peerCount 获取当前连接节点数

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:当前已连接节点数

5.4 net_version 获取当前已连接网络的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:当前已连接网络的ID。1为主网ID。

5.5 net_version 获取本地节点信息

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:当前已连接网络的ID。1为主网ID。

5.6 admin_peers 获取已连接节点信息

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:已连接节点信息

5.7 admin_datadir 获取区块数据的存储路径

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:区块数据存储路径

5.8 admin_addPeer 添加连接节点

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表示添加成功

有疑问加站长微信联系(非本文作者)

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

1812 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传