こんにちは。
ブロックチェーンの開発作業を行う場合、gethを使用してプライベートネットなどを立ち上げる必要がありますが、 コマンドを毎回入力するのは結構大変だと思います。
ですので、gethの起動、終了を支援してくれるBashスクリプトを作成してみました。
https://github.com/PLUSPLUS-JP/geth-control
$ git clone git@github.com:PLUSPLUS-JP/geth-control.git
ぜひ clone してみてください。
リポジトリ内のドキュメントにも簡易説明を書いていますが、ブログにも補足としてスクリプトの初期設定と使い方を書きたいと思います。
動作環境について
gethのバージョンですが、私の環境ではこちらのバージョンを使用しています。1.8.x であれば動くと思います。
$ geth version Geth Version: 1.8.3-unstable Architecture: amd64 Protocol Versions: [63 62] Network Id: 1 Go Version: go1.9.2 Operating System: darwin GOPATH=/Users/o.sawada/go GOROOT=/usr/local/Cellar/go/1.9.2/libexec
どんなことができるのか
- プライベートネットを初期化
- プライベートネットの起動
- プライベートネットの停止
そのまんまですね。
それぞれ、
- 初期化 →
initialize-private-network.sh
- 起動 →
start-private-net.sh
- 停止 →
stop-private-net.sh
というスクリプトになっています。
$ cd geth-control/ $ tree . . ├── LICENSE ├── README.md ├── README.pdf └── private-net ├── _function.sh ├── initialize-private-network.sh ├── monitor-geth-log.sh ├── sample-private-net.cf ├── sample-genesis.json ├── start-private-net.sh └── stop-private-net.sh 1 directory, 10 files
初期設定
はじめに、private-net/sample-private-net.cf
をコピーしてprivate-net/private-net.cf
という設定ファイルを作成します。
この設定ファイルではRPC, websocket, gethが使うポートを設定します。デフォルトで使用するポートは次のようになっています。変更する必要がなければ、作成しなくても問題ありません。
- RPC 8545番
- Websokect 8546番
- GETH 30303番
$ vim private-net/private-net.cf
↓設定ファイルの中身
1 #↲ 2 # geth-control privete-net config file↲ 3 #↲ 4 ↲ 5 # for RPC connection↲ 6 OPT_RPC_PORT=8545↲ 7 ↲ 8 # for web socket↲ 9 OPT_WS_PORT=8546↲ 10 ↲ 11 # for geth↲ 12 OPT_GETH_PORT=30303↲
次に、genesis.json
を作成します。特にこだわりがなければ、sample-genesis.json
というファイルがありますので、それをコピーして使っても大丈夫だと思います。
$ cd private-net/ $ cp -a sample-genesis.json genesis.json
これで初期設定が終わりました。
初期化
ブロックチェーンを初期化してみます。
初期化の確認プロンプトが出ますので YES
と入力します。
下記が実行例です。ネットワークIDが 1526434664
として初期化されました。
$ ./initialize-private-network.sh Would you like to issue a network ID? [YES/*] YES >>> Created as network ID: 1526434664 >>> command [ SUCCESS ] >>> [HINT] Do you want to see more logs? >>> Please try use /Users/hoge/path/to/geth-control/private-net/start-private-net.sh
操作してみる
続いてブロックチェーンを起動してみます。
初めて起動するときは、ネットワークの初期化が行われ、その後にプライベートネットの起動が行われます。
$ ./start-private-net.sh Connect to private net? [YES/*] YES >>> Initialize the network INFO [05-16|10:39:42] Maximum peer count ETH=25 LES=0 total=25 INFO [05-16|10:39:42] Allocated cache and file handles database=/Users/o.sawada/blog/geth-control/private-net/geth/chaindata cache=16 handles=16 INFO [05-16|10:39:42] Writing custom genesis block INFO [05-16|10:39:42] Persisted trie from memory database nodes=0 size=0.00B time=8.452µs gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B INFO [05-16|10:39:42] Successfully wrote genesis state database=chaindata hash=4b7556…13cf12 INFO [05-16|10:39:42] Allocated cache and file handles database=/Users/o.sawada/blog/geth-control/private-net/geth/lightchaindata cache=16 handles=16 INFO [05-16|10:39:42] Writing custom genesis block INFO [05-16|10:39:42] Persisted trie from memory database nodes=0 size=0.00B time=1.435µs gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B INFO [05-16|10:39:42] Successfully wrote genesis state database=lightchaindata hash=4b7556…13cf12 >>> command [ SUCCESS ] >>> Startup success, enjoy with private net :-) DEBUG[05-16|10:39:42] WebSocket registered service=&{am:0xc420454680} namespace=eth DEBUG[05-16|10:39:42] WebSocket registered service=&{e:0xc4200be000} namespace=eth DEBUG[05-16|10:39:42] WebSocket registered service="&{e:0xc4200be000 agent:0xc420366000}" namespace=eth DEBUG[05-16|10:39:42] WebSocket registered service="&{d:0xc4204d9500 mux:0xc42041d110 installSyncSubscription:0xc4203420c0 uninstallSyncSubscription:0xc420342120}" namespace=eth DEBUG[05-16|10:39:42] WebSocket registered service="&{backend:0xc42045ac70 mux:0xc42041d110 quit:<nil> chainDb:0xc4202ea120 events:0xc42047c0c0 filtersMu:{state:0 sema:0} filters:map[]}" namespace=eth DEBUG[05-16|10:39:42] WebSocket registered service="&{net:0xc420476280 networkVersion:1526434664}" namespace=net DEBUG[05-16|10:39:42] Allowed origin(s) for WS RPC interface [*] INFO [05-16|10:39:42] WebSocket endpoint opened url=ws://[::]:8546 INFO [05-16|10:39:42] Mapped network port proto=tcp extport=30303 intport=30303 interface=NAT-PMP(192.168.xxx.xxx) >>> command [ SUCCESS ] >>> [HINT] Do you want to see more logs? >>> Please try use /Users/hoge/path/to/geth-control/private-net/monitor-geth-log.sh
どのように動いているか、ログを見続ける場合は、次のようにしてログを表示させます。
(内部的には tail -f
してます)
$ ./monitor-geth-log.sh DEBUG[05-16|10:39:42] WebSocket registered service="&{d:0xc4204d9500 mux:0xc42041d110 installSyncSubscription:0xc4203420c0 uninstallSyncSubscription:0xc420342120}" namespace=eth DEBUG[05-16|10:39:42] WebSocket registered service="&{backend:0xc42045ac70 mux:0xc42041d110 quit:<nil> chainDb:0xc4202ea120 events:0xc42047c0c0 filtersMu:{state:0 sema:0} filters:map[]}" namespace=eth DEBUG[05-16|10:39:42] WebSocket registered service="&{net:0xc420476280 networkVersion:1526434664}" namespace=net DEBUG[05-16|10:39:42] Allowed origin(s) for WS RPC interface [*] INFO [05-16|10:39:42] WebSocket endpoint opened url=ws://[::]:8546 INFO [05-16|10:39:42] Mapped network port proto=tcp extport=30303 intport=30303 interface=NAT-PMP(192.168.xxx.xxx) DEBUG[05-16|10:40:02] Recalculated downloader QoS values rtt=20s confidence=1.000 ttl=1m0s DEBUG[05-16|10:40:22] Recalculated downloader QoS values rtt=20s confidence=1.000 ttl=1m0s DEBUG[05-16|10:40:42] Recalculated downloader QoS values rtt=20s confidence=1.000 ttl=1m0s
ブロックチェーンを停止させたい場合は、次のようにして、停止のスクリプトを実行させます。
停止の確認プロンプトが表示されますので、YES
を入力し停止させます。
$ ./stop-private-net.sh Stop the private network? [YES/*] YES >>> command [ SUCCESS ]
使い方は簡単ですので、是非活用してみてください。