LoginSignup
6
6

More than 5 years have passed since last update.

DAppsを開発するための準備

Posted at

はじめに

前回、Gethをインストールしイーサリアムネットワークへ接続してみました。
https://qiita.com/tomo1026/items/912ec05eff02f7f76d30

前回の記事では実はメインネットへ接続しており、本番データを見ていたわけですが、開発時に間違って送金を行ったりすると大変な事になります。
そのため、イーサリアムではテストネットや個人的にネットワークを立ち上げられるような仕組みになっています。

システム開発のイメージとしては、以下のような住み分けでしょうか。

  • プライベートネット:開発環境
  • テストネット:検証環境
  • メインネット:本番環境

プライベートネット作成

早速プライベートネットを作成していきます。

genesisファイルの作成

好きなテキストエディタを使って、以下の内容でGenesisファイルというものを作ります。
ファイル名はgenesis.jsonにしました。

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

ファイルを作ったら適当な場所にフォルダを作って、その中に保存します。
MacでFinderからフォルダ作ってもいいですし、以下のようにコマンドで作成しても良いです。

フォルダの作成
$ mkdir private_net

プライベートネットの初期化

プライベートネットの初期化
geth --datadir private_net init private_net/genesis.json

プライベートネットワークへ接続

プライベートネットへ接続
geth --networkid "10" --nodiscover --datadir "private_net" console 2>>private_net/geth_err.log

以下のように表示されればOK。

Welcome to the Geth JavaScript console!

instance: Geth/v1.7.3-stable/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
>

ネットワーク内にアカウントを作成する

アカウント作成
> personal.newAccount("user01")
"0x5906149adae919fc15d865e34d0b755200e84e00"

> personal.newAccount("user02")
"0xe84d1e1e6e931de1f9df0cb20e49709eb721dd46"

ここで"0x5906149adae919fc15d865e34d0b755200e84e00"はユーザーアカウントのアドレスです。
送金する際などに使います。

ネットワーク内のアカウントを確認する

アカウントの確認は以下のコマンドでできます。

アカウントの確認
> eth.accounts
["0x5906149adae919fc15d865e34d0b755200e84e00", "0xe84d1e1e6e931de1f9df0cb20e49709eb721dd46"]

マイニングを開始する

coinbase(Etherbase)はマイニングをした際に報酬を受け取るアドレスです。

coinbaseを確認する
> eth.coinbase
"0x5906149adae919fc15d865e34d0b755200e84e00"
coinbaseを変更する
> miner.setEtherbase(eth.accounts[1])

上記を実行するとuser02(0xe84d1e1e6e931de1f9df0cb20e49709eb721dd46)が報酬を受け取れるようになります。ここでは実行しません。
実行してしまった人はminer.setEtherbase(eth.accounts[0])で元に戻せます。

マイニングを開始する
> miner.start()
null

私の環境では上記のようにnullが返ってきます。

現在のブロック番号、ハッシュレート、ブロックの内容を確認する
> eth.blockNumber //現在のブロック番号
1773
> eth.hashrate //ハッシュレートはマイニングする速度。0はマイニングしていない。
4191
> eth.getBlock(1773) //指定したブロック番号の中身を表示
{
  difficulty: 309596,
  extraData: "0xd883010703846765746887676f312e392e328664617277696e",
  gasLimit: 23741586,
  gasUsed: 0,
  hash: "0xe4d1a56047833191b2b8c686ebd54297a8da1db44aececb405cb92601220eec4",
  logsBloom: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
  miner: "0x5906149adae919fc15d865e34d0b755200e84e00",
  mixHash: "0x1b5f44800c7c6ec52a5e87fafddc6c57c0e65fbbda1c08bc0f4cbb7fe8533e8f",
  nonce: "0x131983b5d98a7ce6",
  number: 1773,
  parentHash: "0xf71805f818608f933d997429ed24d99cdbc868668fe83817e75604f1f8893096",
  receiptsRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
  sha3Uncles: "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
  size: 539,
  stateRoot: "0xc930d8629237cd84f9fbd189b78b1f278e231c9374d2ccc052ff351f2653481a",
  timestamp: 1517538011,
  totalDifficulty: 368187630,
  transactions: [],
  transactionsRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
  uncles: []
}

マイニングを停止する
> miner.stop()
true

マイニングを実施すると当たり前ですがイーサリアムが報酬としてもらえます。
以下のコマンドで獲得したイーサリアムを表示できます。
user01は獲得していますが、user02はマイニングをしていないので残高は0です。
6320イーサも持っててお金持ちですね。

念のためにプライベートネットで採掘したイーサはメインネットでは使えません。
余談ですがメインネットで最初にマイニングを始めた人たちは驚異的な数のイーサを持ってるはずです。

残高を確認する
> web3.fromWei(eth.getBalance(eth.accounts[0]),"ether")
6320
> web3.fromWei(eth.getBalance(eth.accounts[1]),"ether")
0

単純にeth.getBalance(account[0])とするとweiというEthereumの補助単位で表示されます。

送金する

当たり前ですが、マイニングが開始されていないと送金処理を実行しても送金されませんので事前にminer.start()をお忘れなく。

残高を確認する
> personal.unlockAccount(eth.accounts[0])
Unlock account 0x5906149adae919fc15d865e34d0b755200e84e00
Passphrase: 
true
> eth.sendTransaction({from: eth.accounts[0], to: eth.accounts[1], value: web3.toWei(1000, "ether")})

残高を確認するとuser01からuser02に1000eth移動したはずです。
user01はマイニングをし続けているので残高は増えているかもしれません。

6
6
0

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
6
6