geth (go-ethereum) をインストールしてプライベートネットワークに接続する手順 [MacOS 編]

MacOS に geth (go-ethereum) をインストールして、Ethereum node を立てて、プライベートネットワークに接続する方法をご紹介します。

Ethereum | イーサリアム

go-ethereum (geth) のインストール

下記の記事を参考に ethereum をインストールします。

まず、Homebrew をアップデートします。

brew update
brew upgrade

次に、ethereum をインストールします。

brew tap ethereum/ethereum
brew install ethereum
$ geth version  
Geth
Version: 1.8.13-stable
Architecture: amd64
Protocol Versions: [63 62]
Network Id: 1
Go Version: go1.10.3
Operating System: darwin
GOPATH=
GOROOT=/usr/local/Cellar/go/1.10.3/libexec

Ethereum プライベートネットワークに接続する

ここからは プライベート・ネットに接続する · Ethereum入門 の記事に沿って作業しました。

Genesis ファイルを作成する

eth_private_net ディレクトリを作成して、移動します。

ETH_PRIVATE_NET_DIR=~/works/eth_private_net
mkdir $ETH_PRIVATE_NET_DIR && cd $ETH_PRIVATE_NET_DIR

以下の内容の myGenesis.json ファイルを $ETH_PRIVATE_NET_DIR 以下に作成します。

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

Geth をプライベート・ネットで起動する

genesis ブロックの初期化

geth --datadir $ETH_PRIVATE_NET_DIR init $ETH_PRIVATE_NET_DIR/myGenesis.json

geth の起動

geth --networkid "15" --nodiscover --datadir $ETH_PRIVATE_NET_DIR console 2>> $ETH_PRIVATE_NET_DIR/geth_err.log

Geth プロンプト上で、

> eth.getBlock(0)

のコマンドを実行して、指定したブロック番号のブロック情報を表示できることを確認します。

以上、MacOS に geth で Ethereum node を立てた現場からお送りしました。

参考情報