0%

Geth搭建公有节点or私有链节点

Geth搭建公有节点

Geth搭建公有节点主要是因为实验室有获取区块数据这一需求,因此这里记录一下如何建立一个公有链节点并暴露其json-rpc供其他人调用:

1
geth --syncmode=light --datadir "./public_chain"  --rpc --rpcaddr "127.0.0.1"  --rpcport "8545"
  • —syncmode=light:仅获取当前状态,需要其他信息向其他full节点请求(其他模式还有:full——全节点模式,下载所有区块header&body,并且验证,fast——快速模式,下载所有区块,但不验证)

  • —datadir “./public_chain”:保存数据在”./public_chain”

  • —rpc -rpcaddr “127.0.0.1” —rpcport “8545”:开放127.0.0.1:8545的JSON-RPC访问

尝试RPC访问

查询版本:

1
2
$ curl -H "Content-Type: application/json" -X POST  --data '{"jsonrpc":"2.0","method":"web3_clientVersion","params":[],"id":67}' http://127.0.0.1:8545
{"jsonrpc":"2.0","id":67,"result":"Geth/v1.8.13-stable-225171a4/windows-amd64/go1.10.3"}

查询区块:

1
2
$ curl -H "Content-Type: application/json" -X POST  --data '{"jsonrpc":"2.0","method":"eth_syncing","params":[],"id":1}' http://127.0.0.1:8545
{"jsonrpc":"2.0","id":1,"result":{"currentBlock":"0x6a563f","highestBlock":"0x6a9987","knownStates":"0x0","pulledStates":"0x0","startingBlock":"0x687fff"}}

详细API请参阅https://github.com/ethereum/wiki/wiki/JSON-RPC#json-rpc-api

如果没有区块信息需要将static-nodes.json放在chain_data目录下:

https://ethfans.org/wikis/%E6%98%9F%E7%81%AB%E8%8A%82%E7%82%B9%E8%AE%A1%E5%88%92%E8%B6%85%E7%BA%A7%E8%8A%82%E7%82%B9%E5%88%97%E8%A1%A8

Geth搭建私有链

目前网上写的私有链搭建的步骤均没有介绍如何初始化账户及初始金额的问题,本文记录一下使用Geth构造一个私有链的完整步骤。

0x01 创建新账户

如果需要在私有链上初始化一些账户以及给这些账户发放一定数量的以太币,就需要新建一些账户。使用account new命令新建账户:

1
2
3
4
5
6
7
8
9
10
11
$ mkdir testchain	# 新建一个文件夹放置数据
$ geth --datadir "./testchain" account new # 新建账户,需要设置密码
WARN [10-23|20:08:07] No etherbase set and no accounts found as default
Your new account is locked with a password. Please give a password. Do not forget this password.
Passphrase:
Repeat passphrase:
Address: {28c67f4a957fcd814effce00863eb3aeb9e9884a}
$ ls ./testchain
keystore
$ ls ./testchain/keystore/ # 账户文件
UTC--2018-10-23T12-08-15.951611300Z--28c67f4a957fcd814effce00863eb3aeb9e9884a

0x02 编写配置文件

testchain目录下新建genesis.json文件,内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
{
"config":{
"homesteadBlock":0
},
"nonce": "0x0000000000000042",
"timestamp": "0x0",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"extraData": "0x",
"gasLimit": "0x8000000000000000",
"difficulty": "0x02",
"mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"coinbase": "0x28c67f4a957fcd814effce00863eb3aeb9e9884a",
"alloc": {
"0x28c67f4a957fcd814effce00863eb3aeb9e9884a":{"balance":"2000000000000000000000000"}
}
}

其中,coinbase指挖矿的报酬发送到的账户,alloc指初始化账户及其初始金额,注意这里的地址都需要是新建的账户;difficulty指挖矿难度,gasLimit指一个区块中的Gas限制,即打包区块时所有的交易Gas花费不得超过该值。

0x03 使用命令构造创始块

若文件夹下有旧区块信息需要先删除:

1
geth --datadir "./testchain" removedb

使用genesis.json创建创始块

1
geth --datadir "./testchain" init ./testchain/genesis.json

0x04 启动节点

创始块完成后可以使用geth启动节点并开始挖矿了,具体命令如下:

1
geth --syncmode=full  --identity "TestNode0" --rpc -rpcaddr "0.0.0.0"  --rpcport "8545" --rpccorsdomain "*" --port "30303" --nodiscover  --rpcapi "db,eth,net,web3,miner,net,personal,net,txpool,admin,debug"  --networkid 1900   --datadir "./testchain" --nat "any" --mine --minerthreads "1"

其中一些重要参数的含义为:

  • —identity:节点名称

  • —rpc -rpcaddr “0.0.0.0” —rpcport “8545”:节点开放rpc连接,监听地址为0.0.0.0:8545

  • —rpcapi:rpc提供的api类型

  • —rpccorsdomain:rpc允许接入本节点的网段,分号隔开

  • —port:节点间联系使用的端口号

  • —networkid:区块链的网络号,唯一的标识一条区块链,比如说1=主链, 3=Ropsten测试链, 4=Rinkeby测试链

  • —mine —minerthreads 1:启动挖矿,开放1个线程

启动节点后,一条私有链就可以使用了,即可以连接该节点发布智能合约,或者发起新的交易。

例如:连接节点,查看账户

1
2
3
4
5
6
7
8
9
10
11
12
$ geth attach http://localhost:8545
Welcome to the Geth JavaScript console!

instance: Geth/TestNode0/v1.7.0-unstable/linux-amd64/go1.10.1
coinbase: 0x28c67f4a957fcd814effce00863eb3aeb9e9884a
at block: 13 (Tue, 23 Oct 2018 20:55:04 DST)
datadir: /home/anemone/testchain
modules: admin:1.0 debug:1.0 eth:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0

> eth.accounts
["0x28c67f4a957fcd814effce00863eb3aeb9e9884a"]
>