LoginSignup
22
19

More than 5 years have passed since last update.

Ethereum入門 〜GethインストールからETH送金まで〜

Last updated at Posted at 2017-12-13

はじめに

Ethereumを使ったトークンの発行やスマートコントラクトに興味があり、手始めにGethのインストールからプライベートネット上でのETH送金までやってみました。

Gethのインストール

Homebrewを使ってEthereumのGethをインストールします。

$ brew tap ethereum/ethereum
$ brew install ethereum

Gethの起動

ディレクトリ作成

まずはローカルプライベートネットでGethを起動します。
ホームディレクトリに「eth_testnet」を作成します。

$ mkdir ~/eth_testnet
$ cd eth_testnet
$ pwd
/Users/home/eth_testnet

Genesisファイル作成

次にGenesisファイルを作成します。
Genesisファイルはブロックチェーンの0番目のブロックの情報を書いたjson形式のファイルになります。

$ vi genesis.json
genesis.json
{
  "config": {
        "chainId": 10,
        "homesteadBlock": 0,
        "eip155Block": 0,
        "eip158Block": 0
    },
  "alloc"      : {},
  "coinbase"   : "0x0000000000000000000000000000000000000000",
  "difficulty" : "0x20000",
  "extraData"  : "",
  "gasLimit"   : "0x2fefd8",
  "nonce"      : "0x0000000000000042",
  "mixhash"    : "0x0000000000000000000000000000000000000000000000000000000000000000",
  "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
  "timestamp"  : "0x00"
}

difficultyの値が大きすぎると採掘に時間がかかるので0x20000ぐらいが良いと思います。

viコマンドについてはこちらから
viコマンド(vimコマンド)一覧

Gnesisファイルの初期化

$ geth --datadir ~/eth_testnet init ~/eth_testnet/genesis.json

Gethの起動

geth --networkid 4649 --nodiscover --maxpeers 0 --datadir ~/eth_testnet console 2>> ~/eth_testnet/geth.log
Welcome to the Geth JavaScript console!
instance: Geth/v1.6.7-stable-ab5646c5/darwin-amd64/go1.9

このように表示されれば成功です。

マイニング

アカウントの作成

まずはアカウントを2つ作成します。
("pass1")はパスワードになります。プライベートネットなので何でも良いです。

> personal.newAccount("pass1")
"0xc61f64fdeea138f529fa4e2d10e5566819f7ed36"
> personal.newAccount("pass2")
"0xe972e5341a591150c9d38fe67ed2bcbb11eb71ab"

マイニング報酬を受け取るアカウントを確認します。
デフォルトは一番初めに作成したアカウントです。

> eth.coinbase
"0xc61f64fdeea138f529fa4e2d10e5566819f7ed36"

coinbaseを他のアカウントに切り替えることも出来ます。

> miner.setEtherbase(eth.accounts[1])
true
> eth.coinbase
"0xe972e5341a591150c9d38fe67ed2bcbb11eb71ab"

採掘前にブロックとアカウントの残高を確認します。

> eth.blockNumber
0
> eth.getBalance(eth.accounts[0])
0
> eth.getBalance(eth.accounts[1])
0

採掘していなければ0です。

マイニング

それではマイニングスタート!

> miner.start()
null

trueでなくてもOKです。
止める時はこちらのコマンド

> miner.stop()
true

ブロック数を確認します。

> eth.blockNumber
16

16個のブロックが

マイニング報酬を確認します。

> eth.getBalance(eth.accounts[0])
215000000000000000000

これは単位がwei(1eth=10^18wei)なのでとても大きい数字になってます。
eth換算してみます。

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

1ブロックで5ethの報酬なので、450ブロック生成されたことになります。
*現在の報酬は3eth

ETH送金

まずは送金するためにアカウントのロックを解除

> personal.unlockAccount(eth.accounts[0],"pass1")
true

eth.accounts[0]からeth.accounts[1]へ5eth送金します。

> eth.sendTransaction({from: eth.accounts[0], to: eth.accounts[1], value: web3.toWei(5, "ether")})
"0x3b8f105b6576af5777cf801e58b279171615007d319410e3fa32f1cc6ae8a475"

マイニングスタートします。
マイニング前はeth.accounts[1]のethは0ですが、マイニング後、5eth送金されました。

> eth.getBalance(eth.accounts[1])
0
> miner.start()
null
> eth.getBalance(eth.accounts[1])
5000000000000000000
> web3.fromWei(eth.getBalance(eth.accounts[1]),"ether")
5
> miner.stop()
true

トランザクション情報を調べます。

> eth.getTransaction("0x3b8f105b6576af5777cf801e58b279171615007d319410e3fa32f1cc6ae8a475")
{
  blockHash: "0xa335f9055ea72d747071eb4fad80149705c9eeb1dd257c77aca4915b9ad1411c",
  blockNumber: 451,
  from: "0xc61f64fdeea138f529fa4e2d10e5566819f7ed36",
  gas: 90000,
  gasPrice: 18000000000,
  hash: "0x3b8f105b6576af5777cf801e58b279171615007d319410e3fa32f1cc6ae8a475",
  input: "0x",
  nonce: 0,
  r: "0x81570ca9a71932bb3fd3735e8f69e8dba33d74847dbc4dbca76ac9500fcc7e69",
  s: "0x1d537aafab0cb77bbb1015eb93aa206a917d803f2c3c12ae4f304d285622263e",
  to: "0xe972e5341a591150c9d38fe67ed2bcbb11eb71ab",
  transactionIndex: 0,
  v: "0x1b",
  value: 5000000000000000000
}

ブロック情報も確認出来ます。

> eth.getBlock(451)
{
  difficulty: 155045,
  extraData: "0xd683010607846765746885676f312e398664617277696e",
  gasLimit: 86385931,
  gasUsed: 21000,
  hash: "0xa335f9055ea72d747071eb4fad80149705c9eeb1dd257c77aca4915b9ad1411c",
  logsBloom: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
  miner: "0xc61f64fdeea138f529fa4e2d10e5566819f7ed36",
  mixHash: "0xc65fc923243b80465ca7815d89c10abb5b26c9ff1f783ad6528dec89a884ef5d",
  nonce: "0x23215e7e02013d7a",
  number: 451,
  parentHash: "0x1fb6338ba8140706a069f27ecc8aaf9eb2d80a28058bb63c1cf8b164b313cbea",
  receiptsRoot: "0xec39eb30bd96b653527f9b40319d935011cab5bb3a4139ed952b988427dc4b2b",
  sha3Uncles: "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
  size: 651,
  stateRoot: "0x9229bb9d647f17a796ddf02245cc865b035d7aa7a819201f6ec575dd81324e00",
  timestamp: 1504478592,
  totalDifficulty: 66032813,
  transactions: ["0x3b8f105b6576af5777cf801e58b279171615007d319410e3fa32f1cc6ae8a475"],
  transactionsRoot: "0xb6a33c35ae552a91d240a885f5e81885994ca5870747d6806c896e9c70647dbd",
  uncles: []
}

eth.accounts[0]はEtherbaseでもあるので、送金手数料がマイニング報酬として戻ってくるため実質手数料が無料です。

手数料の確認

次に手数料が発生するアカウントで送金してみます。
まず新しいアカウントを作成し、accounts[1]からaccounts[2]へ4eth送金してみます。

> personal.newAccount("pass3")
"0x5bdb4eca0cf7f5644be53def0a425033e6522550"
> eth.getBalance(eth.accounts[1])
5000000000000000000
> eth.getBalance(eth.accounts[2])
0
> personal.unlockAccount(eth.accounts[1],"pass2",0)
true
> eth.sendTransaction({from: eth.accounts[1], to: eth.accounts[2], value:web3.toWei(4,"ether")})
"0xf89e4c36a97e441a43ac43eaa6944ab913aaa8af9fd9a63ad832ca2a2fd01ed3"
> miner.start()
null
> eth.getBalance(eth.accounts[2])
4000000000000000000
> miner.stop()
true
> eth.getBalance(eth.accounts[1])
999622000000000000
> web3.fromWei(eth.getBalance(eth.accounts[1]),"ether")
0.999622

accounts[1]は5eth所有している状態からaccounts[2]へ4eth送金したところaccounts[1]の残高は0.999622ethとなりました。
1-0.999622=0.000378eth が手数料となっているのが分かります。

終わりに

プライベートネットですが、こんなにシンプルな作業で、しっかりマイニングされETHが送金出来るは感動です。

参考にさせて頂きました。

はじめてのブロックチェーン・アプリケーション Ethereumによるスマートコントラクト開発入門

「Ethereum入門」を Geth 1.6.7 で進めてみた(インストールからetherの送金まで)

22
19
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
22
19