LoginSignup
5
4

More than 5 years have passed since last update.

Blockchainハッカソンの振り返り(その1)〜Etherを採掘!〜

Last updated at Posted at 2017-12-06

今回は機械学習コミュニティ「Team AI」が開催しているブロックチェーンハッカソンの振り返りを書こうと思います!
できる限り「Short & Simple」を心がけます。
なお、私もブロックチェーン初学者なので、内容に誤り等ありましたらご指摘いただけると幸いです。

今回やったこと

今回のハッカソンでは以下のことを行いました。

  1. Gethのインストール(Homebrewのインストールも含む)
  2. Genesisブロックの作成
  3. アカウントの作成
  4. Etherの採掘

一つずつ簡単に振り返っていきます。ちなみにパソコンはMac(OSバージョンは10.12.6)です。

Gethのインストール

まずはGethのインストールです。GethとはEthereumクライアントの一つです。Go言語で実装されたethereumクライアントということでGethという名前になっています。
Gethを使用することでEthereumネットワークにフルノードとして参加することができます。

チュートリアルによるとターミナルで以下のコマンドを打つとGethがインストールできるということで、早速打ってみました。

$ bash <(curl -L https://install-geth.ethereum.org)

・・・インストールできません。どうやら「https://install-geth.ethereum.org」 が現在は存在しないようです。他のインストール方法をグーグル先生で調べてみると、Homebrew(パッケージ管理ソフト)を使ってインストールする文献が多く見つかりました。なのでHomebrewを使ってインストールすることに。
HomebrewをインストールしていなかったのでまずはHomebrewのインストール。以下のコマンドでできます。

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

それではHomebrewを使ってGethをインストールします。

$ brew tap ethereum/ethereum
$ brew install ethereum

GitHubからGethをクローン、ビルドします。
ビルドにはGo言語のコンパイラが必要なので、もし入っていない場合は以下のコマンドでインストールしておきましょう。

Goコンパイラのインストール

$ brew install go

クローン、ビルド

$ mkdir ~/ether-practice
$ git clone https://github.com/ethereum/go-ethereum
$ cd go-ethereum
$ make geth

パスが通っていることを確認します。

$ which geth
/usr/local/bin/geth

これでGethを使用する準備が整いました。

Genesisブロックの作成

続いてGenesisブロックの作成です。まずはテストネットのディレクトリを作成。

$ cd ~/ether-practice
$ mkdir testnet
$ cd testnet
$ pwd
~/ether-practice/testnet

テストネットのディレクトリにGenesisファイルを作成します。

$ vi genesis.json
{
        "config": {
                "chainID": 15,
                "homesteadBlock": 0,
                "eip155Block" :0,
                "eip158Block":0
        },
        "nonce": "0x0000000000000042",
        "maxhash": "0x00000000000000000000000000000000000000000000000000000000000000000000000",
        "difficulty": "0x00",
        "alloc": {},
        "coinbase": "0x0000000000000000000000000000000000000000",
        "timestamp": "0x00",
        "parenthash": "0x0000000000000000000000000000000000000000000000000000000000000000",
        "extraData": "0x00",
        "gasLimit": "0x1312d00"
}

続いてGenesisブロックを初期化します。

$ geth --datadir ~/ether-practice/testnet init ~/ether-practice/testnet/genesis.json

Gethを起動しましょう!

$ geth --networkid 4649 --nodiscover --maxpeers 0 --datadir ~/ether-practice/testnet console 2>> ~/ether-practice/testnet/geth.log

以下のようにコンソールが起動すればOKです。

Welcome to the Geth JavaScript console!

instance: Geth/v1.7.3-stable/darwin-amd64/go1.9.2

>

今回立ち上げたテストネット(プライベートネット)のGenesisブロックがgenesis.jsonに記載されたものになっているか確認してみます。Gethプロンプト上で以下のコマンドを打つことで確認できます。

> eth.getBlock(0)

上記ではブロック番号"0"を指定してGenesisブロックの情報を表示します。genesis.jsonと同じ内容が出力されればOKです。

アカウントの作成

Gethのコンソール上で新規のアカウントを作成します。今回はEOA(Externally Owned Account)を作成します。(Ethereumにはもう一種類のアカウントContractというものもあるようですが、今回は触れません。)

このノードに登録されたアカウント(EOA)を表示させてみます。

> eth.accounts
[ ]

まだアカウントを作成していないので[]内には何も表示されません。
それではEOAを作成します。下記の「hogehoge」はアカウントのパスワードです。適宜変更してください。

> personal.newAccount("hogehoge")
'0xd7bd6786ced3bf0ece162db41c8579fb5ac08035'

出力された「0xd7bd6786ced3bf0ece162db41c8579fb5ac08035」は作成したアカウントのアドレスです。
採掘による報酬を紐づけるEOAのアドレスを確認しましょう。

> eth.coinbase
'0xd7bd6786ced3bf0ece162db41c8579fb5ac08035'

先ほど作成したアカウントのアドレスになっています。複数のアカウントがある場合は、採掘の報酬がどのアカウントに紐づいているか確認しておきましょう。なお、プライマリーアカウントはコマンドminer.setEtherbase(eth.accounts[x])(xはアカウントリストの番号)で変更できます。

Etherの採掘

作成したEOAのアドレスがプライマリーとしてセットされていれば、etherの採掘が可能です。以下のコマンドで採掘を開始します。なお、miner.start()の()内は採掘を何本のスレッドで同時実行するかを指定するパラメータです。指定しない場合は動作環境でのCPUコア数に設定されるようですが、CPU利用率が100%になってしまうようなことがないよう「1」などを指定しておけば無難なようです。

> miner.start()
true

この時trueではなくnullが出力されても問題ないようです。
採掘を停止する場合は以下のコマンドを打ってください。

> miner.stop()
true

採掘状況を確認しましょう。

> eth.blockNumber
275

上記の「275」はコマンド投入時の「ブロック高」です。この場合275個のブロックを採掘したことになります。
なお、採掘中に本当に採掘処理を行なっているかを確認したい場合は以下のコマンドでハッシュレートを確認してみましょう。

> eth.hashrate

「0」が出力されれば採掘停止中、「0以外」が出力されれば採掘中となります。ちなみにminer.stop()を打った後すぐに0になるわけではなく、少しずつレートが小さくなっていき、しばらくすると0になりました。

採掘したブロックの情報を見てみましょう。ブロック高が200のブロックの情報を見てみます。

> eth.getBlock(200)
{
  difficulty: 138988,
  extraData: "0xd883010703846765746887676f312e392e328664617277696e",
  gasLimit: 16450254,
  gasUsed: 0,
  hash: "0xec38d3b365fef7f2b5f8a66ef9765d76cc7c11e2258414579ea0de976c949c7f",
  logsBloom: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
  miner: "0xd7bd6786ced3bf0ece162db41c8579fb5ac08035",
  mixHash: "0xbdea73680393961f12624cf86e60e3c060082ba8a217737b0b4a996693f9a275",
  nonce: "0x7b7a4bf76eb9de21",
  number: 200,
  parentHash: "0x001850b35f2ba424b3c01cca24ae3b669ccd90bb293904f325eef12e32ee8015",
  receiptsRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
  sha3Uncles: "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
  size: 537,
  stateRoot: "0xc4af800d5c34b9ec103e73e8db7e4a723f2513d027675eebd2af7421d5aaf3d5",
  timestamp: 1512387514,
  totalDifficulty: 26889917,
  transactions: [],
  transactionsRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
  uncles: []
}

ふむふむ、今まで勉強してきたhashやnonceがちゃんとありますね。
アカウントの報酬を確認してみましょう。

> web3.fromWei(eth.getBalance(eth.accounts[0]),"ether")
950

なお、ログ(geth.log)を見ると、採掘に成功するとハンマーやチェーンが出力されています。採掘してる感がありますね笑

〜
INFO [12-04|20:21:10] Successfully sealed new block            number=1 hash=91bbba…617a4f
INFO [12-04|20:21:10] 🔨 mined potential block                  number=1 hash=91bbba…617a4f
INFO [12-04|20:21:10] Commit new mining work                   number=2 txs=0 uncles=0 elapsed=154.608µs
INFO [12-04|20:21:10] Successfully sealed new block            number=2 hash=8510bf…b1377a
INFO [12-04|20:21:10] 🔨 mined potential block                  number=2 hash=8510bf…b1377a
INFO [12-04|20:21:10] Commit new mining work                   number=3 txs=0 uncles=0 elapsed=108.132µs
INFO [12-04|20:21:11] Generating DAG in progress               epoch=1 percentage=0  elapsed=1.003s
INFO [12-04|20:21:11] Successfully sealed new block            number=3 hash=172921…1c80a6
INFO [12-04|20:21:11] 🔨 mined potential block                  number=3 hash=172921…1c80a6
INFO [12-04|20:21:11] Commit new mining work                   number=4 txs=0 uncles=0 elapsed=127.623µs
INFO [12-04|20:21:12] Successfully sealed new block            number=4 hash=5bc983…8ce2a1
INFO [12-04|20:21:12] 🔨 mined potential block                  number=4 hash=5bc983…8ce2a1
INFO [12-04|20:21:12] Commit new mining work                   number=5 txs=0 uncles=0 elapsed=127.34µs
INFO [12-04|20:21:12] Generating DAG in progress               epoch=1 percentage=1  elapsed=1.951s
INFO [12-04|20:21:13] Generating DAG in progress               epoch=1 percentage=2  elapsed=2.972s
INFO [12-04|20:21:13] Successfully sealed new block            number=5 hash=83a568…55942b
INFO [12-04|20:21:13] 🔨 mined potential block                  number=5 hash=83a568…55942b
INFO [12-04|20:21:13] Commit new mining work                   number=6 txs=0 uncles=0 elapsed=134.772µs
INFO [12-04|20:21:14] Successfully sealed new block            number=6 hash=0aedcd…4b4119
INFO [12-04|20:21:14] 🔗 block reached canonical chain          number=1 hash=91bbba…617a4f
INFO [12-04|20:21:14] 🔨 mined potential block                  number=6 hash=0aedcd…4b4119
INFO [12-04|20:21:14] Commit new mining work                   number=7 txs=0 uncles=0 elapsed=119.455µs
INFO [12-04|20:21:14] Generating DAG in progress               epoch=1 percentage=3  elapsed=3.939s
INFO [12-04|20:21:15] Successfully sealed new block            number=7 hash=79d662…04b133
INFO [12-04|20:21:15] 🔗 block reached canonical chain          number=2 hash=8510bf…b1377a
INFO [12-04|20:21:15] 🔨 mined potential block                  number=7 hash=79d662…04b133
INFO [12-04|20:21:15] Commit new mining work                   number=8 txs=0 uncles=0 elapsed=127.058µs
INFO [12-04|20:21:15] Generating DAG in progress               epoch=1 percentage=4  elapsed=4.905s
〜

ということで、今回のハッカソンではGethのインストールからEtherの採掘までを行いました。引き続き勉強していきます!

参考文献

5
4
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4