GethによるEthereumブロックチェーン入門の入門 (Gethセットアップ篇)
本記事の目的・対象読者
本記事ではEthereumのフルクライアントであるGethを使ったブロックチェーンの入門の入門を取り扱います。対象読者は下記のような方です。
- ブロックチェーンに技術的な興味がある
- 将来dAppsなどの分散型アプリケーションの開発をしてみたい
- ethereumを技術側から触ってみたい
- 詳しいことは置いといて、とりあえず自分のPCでethereumの送金を試したい
また、著者もブロックチェーンを勉強中です。自分へのメモとしても公開しています。
すでに詳しい方で「こんな情報あるよ」「ここが間違っているよ」などがありましたら、コメントに残していただけると幸いです。
また入門の入門なのでブロックチェーンの概念やEthereumの詳解などは致しません。
上記の項目を了承いただいた上で読み進めていただけると幸いです。
Gethとは
前述したように今回取り扱うGethは、Ethereumのフルクライアントになります。Gethを使うと下記のことができるようになります。
etherはEthereumの暗号通貨になります。各ノードでこのetherをやりとりします。また、トランザクションは1回の取引(送金)を指します。
トランザクションについて詳しく知りたい方は下記のブログが参考になると思います。
Gethをインストールする
Mac OSにインストールする方
Mac OSの方はhomebrewからのインストールが可能です。
まず、xcodeがインストールされていない方は、xcodeをインストールします。次にターミナルから下記のコマンドを実行します。
$ xcode-select --install
続いて、下記のコマンドを実行すればHomebrewのインストールができます。
$ ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)" ==> This script will install: /usr/local/bin/brew /usr/local/Library/... /usr/local/share/man/man1/brew.1 ==> The following directories will be made group writable: /usr/local/. /usr/local/bin /usr/local/lib /usr/local/share /usr/local/share/doc ==> The following directories will have their group set to admin: /usr/local/bin /usr/local/share /usr/local/share/doc Press RETURN to continue or any other key to abort ==> /usr/bin/sudo /bin/chmod g+rwx /usr/local/. /usr/local/bin /usr/local/lib /usr/local/share /usr/local/share/doc WARNING: Improper use of the sudo command could lead to data loss or the deletion of important system files. Please double-check your typing when using sudo. Type "man sudo" for more information. To proceed, enter your password, or type Ctrl-C to abort. Password: ==> /usr/bin/sudo /usr/bin/chgrp admin /usr/local/bin /usr/local/share /usr/local/share/doc ==> /usr/bin/sudo /bin/mkdir /Library/Caches/Homebrew ==> /usr/bin/sudo /bin/chmod g+rwx /Library/Caches/Homebrew ==> Downloading and installing Homebrew... remote: Counting objects: 184874, done. remote: Compressing objects: 100% (50748/50748), done. remote: Total 184874 (delta 133006), reused 184810 (delta 132957) Receiving objects: 100% (184874/184874), 36.91 MiB | 57.00 KiB/s, done. Resolving deltas: 100% (133006/133006), done. From https://github.com/Homebrew/homebrew * [new branch] master -> origin/master HEAD is now at 601338e Remove proc handling from BuildEnvironment ==> Installation successful! ==> Next steps Run `brew doctor` before you install anything Run `brew help` to get started
続いて、homebrewからgethをインストールします。
下記コマンドを実行して完了です。
$ brew tap ethereum/ethereum $ brew install ethereum
Ubuntuにインストールする方
Ubuntuにインストールする方は下記のコマンドを実行することでGethを自分のマシンに取り込むことができます。
$ sudo add-apt-repository -y ppa:ethereum/ethereum $ sudo apt-get update $ sudo apt-get install ethereum
Gethを動かしてみる
それでは実際にインストールしたGethを自分のマシンで動かしてみます。
genesisファイルを作成する
任意のディレクトリにブロック情報やgenesisファイルを格納するフォルダを作成します。
著者はデスクトップから動かしたかったので、下記のコマンドで実行しました。
$ mkdir ~/desktop/eth_private_net
上記のディレクトリに移動し、ディレクトリ内にmygenesis.jsonを作成してください。下記のjsonコードを入力して保存します。
{ "config":{ "chainId":15 }, "nonce":"0x0000000000000042", "timestamp":"0x0", "parentHash":"0x0000000000000000000000000000000000000000000000000000000000000000", "extraDate":"", "gasLimit":"0x8000000", "difficulty":"0x4000", "mixhash":"0x0000000000000000000000000000000000000000000000000000000000000000", "coinbase":"0x3333333333333333333333333333333333333333", "alloc":{} }
このjsonファイルには最初のブロック(genesisブロック)の情報が書かれています。主な情報は下記になります。
genesisブロックの初期化
下記のコマンドでgenesisブロックを初期化することができます。
$ geth --datadir ~/desktop/eth_private_net init ~/desktop/eth_private_net/myGenesis.json INFO [03-21|18:42:34] Maximum peer count ETH=25 LES=0 total=25 INFO [03-21|18:42:34] Allocated cache and file handles database=/Users/username/desktop/eth_private_net/geth/chaindata cache=16 handles=16 INFO [03-21|18:42:34] Writing custom genesis block INFO [03-21|18:42:34] Persisted trie from memory database nodes=0 size=0.00B time=17.266µs gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B INFO [03-21|18:42:34] Successfully wrote genesis state database=chaindata hash=7b2e8b…7e0432 INFO [03-21|18:42:34] Allocated cache and file handles database=/Users/username/desktop/eth_private_net/geth/lightchaindata cache=16 handles=16 INFO [03-21|18:42:34] Writing custom genesis block INFO [03-21|18:42:34] Persisted trie from memory database nodes=0 size=0.00B time=3.252µs gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B INFO [03-21|18:42:34] Successfully wrote genesis state database=lightchaindata hash=7b2e8b…7e0432
Geth起動
実際にGethを起動します。
$ geth --networkid "15" --nodiscover --datadir "~/desktop/eth_private_net" console 2>> ~/desktop/eth_private_net/geth_err.log
少し長いですが、一つずつ見ていきます。
- networkid
- ここで整数のIDを指定することで、ライブネットとは異なるネットワークを起動することが可能です。genesisブロックで指定したchainIdと同じものを指定しましょう。
- console
- Gethには採掘やトランザクションの生成などを対話的に進めることができるコンソールが用意されています。consoleオプションでそのコマンドを立ち上げることが可能です。
- geth_err.log
- cosoleで立ち上げるlogが格納されているファイルです。
下記のlogが出たら成功です。
Welcome to the Geth JavaScript console! instance: Geth/v1.8.2-stable/darwin-amd64/go1.10 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 >
実際にgenesisブロックを見てみましょう。
> eth.getBlock(0)
{ difficulty: 16384, extraData: "0x", gasLimit: 134217728, gasUsed: 0, <略> uncles: [] }
こんな感じにgenesisブロックの情報が表示されたら成功です!
下記のコマンドでgethのconsoleを終了できます。
>exit
さてgethのセットアップが完了したところで、次に実際にehtherの採掘をしてみます。
現在情報を整理している最中なので、少々お待ちください。
GethによるEthereumブロックチェーン入門の入門 その2
GethによるEthereumブロックチェーン入門の入門 その3