gethでEthereumのlocal cluster(複数ノードのあるprivate network)を作る方法

サイト: Overall: 

https://github.com/ethereum/go-ethereum/wiki/Setting-up-private-network-or-local-cluster

Connecting to the network · ethereum/go-ethereum Wiki · GitHub https://blog.coinfabrik.com/how-to-run-multiple-geth-instances-on-our-private-ethereum-blockchain/

1: Build geth tool https://github.com/ethereum/go-ethereum/wiki/Building-Ethereum

2: set up 1 node in private network https://qiita.com/oggata/items/eea4d5e37f38785f6079

3: Config nodes in network https://github.com/ethereum/go-ethereum/wiki/Setting-up-private-network-or-local-cluster


1: genesis.json作成

{
"nonce": "0x0000000000000042",
"mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"difficulty": "0x400",
"alloc": {},
"coinbase": "0x3333333333333333333333333333333333333333",
"timestamp": "0x0",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"extraData": "0x",
"gasLimit": "0x8000000",
"config": {}
} 

2: genesis init

2つのノードを立ち上げる。異なるデータディレクトリと同じgenesis.jsonファイルを用いる。

➜  go-ethereum git:(master) ✗ /Users/username/go-ethereum/build/bin/geth --datadir /Users/username/blockchain_projects/gethdir/miner01 init /Users/username/blockchain_projects/gethdir/customgenesis.json

3:ネットワーク立ち上げ それぞれのデータディレクトリを指定して以下のコマンドを打つ。 geth consoleが出てきたら成功。

➜  go-ethereum git:(master) ✗ /Users/username/go-ethereum/build/bin/geth --datadir /Users/username/blockchain_projects/gethdir/miner01 -verbosity 6 --ipcdisable --port 30301 --rpcport 8101 --rpc console 2>> /Users/username/blockchain_projects/gethdir/miner01.log

Welcome to the Geth JavaScript console!
instance: Geth/v1.8.0-unstable-908faf8c/darwin-amd64/go1.9.2
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
>

4: enodeURL確認

> admin.nodeInfo.enode
"enode://3524a52d94a5b5cdc698a5757447c5644d0e22049be81829720368c71f4fbfa7f6ef652cc06e77fc0b4fd0303d0590482be3bd988aac882439d6ac3c8878d65d@124.213.106.195:30301"

5: パソコン環境の中で使えるポート番号を確認 gethのconsoleではなくターミナルから:

➜  ~ ifconfig|grep netmask|awk '{print $2}'
127.0.0.1
192.168.0.4

4で確認した、「124.213.106.195」というポートは使えないことがわかる。 実際にこちらのenodeURLでpeerを追加しようとしても、peerCountは変わらない。

5: enodeURLによるpeerの追加 使えるポートの番号を用いて次のステップを実行。 片方のノードで、もう片方のノードを追加すると、相互に接続ができる。

> admin.addPeer("enode://3524a52d94a5b5cdc698a5757447c5644d0e22049be81829720368c71f4fbfa7f6ef652cc06e77fc0b4fd0303d0590482be3bd988aac882439d6ac3c8878d65d@127.0.0.1:30301")

*...127.0.0.1のポート番号は 4で確認したものを変更した。

6:追加を確認

geth console:
> net.peerCount
1