Macでethereumを動かしてみる


mac上でgo-ethereumのソースコードをビルドし、マイニング、送金までを動かしてみます.

# 環境

- macOS High Sierra
- Xcode インストール済み
- homebrew インストール済み

# Ethereumのビルド
### go言語環境のインストール
```
$ brew install go
```

### ソースコード取得
```
$ git clone -b release/1.3.6 https://github.com/ethereum/go-ethereum.git
```

### gethのビルド
```
$ make -C go-ethereum geth
```

### ビルド確認
```
$  ./go-ethereum/build/bin/geth version
Geth
Version: 1.3.6
Git Commit: bf324bd24be9036046e36c6248b2d7c31cce9eca
Protocol Versions: [63 62 61]
Network Id: 1
Go Version: go1.9.2
OS: darwin
GOPATH=
GOROOT=/usr/local/Cellar/go/1.9.2/libexec
$
```

gethにパスを通しておく

# テストネットワークの構築

### gethの起動

```
$ mkdir eth_data
$ geth --networkid "123" --datadir "eth_data" --olympic console
I1118 18:06:25.569527   83720 flags.go:425] WARNING: No etherbase set and no accounts found as default
I1118 18:06:25.569874   83720 database.go:71] Alloted 16MB cache to eth_data/chaindata
I1118 18:06:25.573234   83720 database.go:71] Alloted 16MB cache to eth_data/dapp
I1118 18:06:25.575204   83720 backend.go:314] Protocol Versions: [63 62 61], Network Id: 123
I1118 18:06:25.575485   83720 backend.go:332] Starting Olympic network
I1118 18:06:25.576533   83720 backend.go:362] Blockchain DB Version: 3
I1118 18:06:25.577099   83720 blockchain.go:214] Last header: #0 [fd4af92a…] TD=131072
I1118 18:06:25.577113   83720 blockchain.go:215] Last block: #0 [fd4af92a…] TD=131072
I1118 18:06:25.577119   83720 blockchain.go:216] Fast block: #0 [fd4af92a…] TD=131072
I1118 18:06:25.581048   83720 cmd.go:115] Starting Geth/v1.3.6-bf324bd2/darwin/go1.9.2
I1118 18:06:25.581197   83720 server.go:311] Starting Server
I1118 18:06:27.679335   83720 udp.go:212] Listening, enode://7425e50f0b94ee0552fb814272cea682e80e4ba05ab6d3cc40507b366dc5e275ab0219f06006d6fa4a0da409e41dd64f1d126de3061301f80f1c6310f3a5635a@[::]:30303
I1118 18:06:27.679612   83720 server.go:552] Listening on [::]:30303
I1118 18:06:27.679659   83720 backend.go:526] Server started
I1118 18:06:27.680075   83720 ipc.go:112] IPC service started (eth_data/geth.ipc)
instance: Geth/v1.3.6-bf324bd2/darwin/go1.9.2
 datadir: eth_data
coinbase: null
at block: 0 (Thu, 01 Jan 1970 09:00:00 JST)
modules: admin:1.0 db:1.0 debug:1.0 eth:1.0 miner:1.0 net:1.0 personal:1.0 shh:1.0 txpool:1.0 web3:1.0
>
```

# アカウント作成
### アカウント作成（送受信するため2つ作成）
```
> personal.newAccount("testuser1")
"0xe3b88b800ba2494219824863c96cf7c36e2903f5"
> personal.newAccount("testuser2")
"0x8f25b1d49fa60936b1b0b35d5d337a6966163b5c"
```

### アカウントの確認
```
> eth.accounts
["0xe3b88b800ba2494219824863c96cf7c36e2903f5", "0x8f25b1d49fa60936b1b0b35d5d337a6966163b5c"]
```

### 残高の確認
```
> eth.getBalance(eth.accounts[0])
0
```

# マイニング

### ブロック数の確認
```
> eth.blockNumber
0
```

### マイニング

```
> miner.start()
I1118 18:10:04.793788   83720 backend.go:584] Automatic pregeneration of ethash DAG ON (ethash dir: /Users/hashizume/.ethash)
I1118 18:10:04.793843   83720 backend.go:591] checking DAG (ethash dir: /Users/hashizume/.ethash)
I1118 18:10:04.793922   83720 miner.go:119] Starting mining operation (CPU=4 TOT=5)
true
> I1118 18:10:04.796171   83720 worker.go:569] commit new work on block 1 with 0 txs & 0 uncles. Took 2.216442ms
I1118 18:10:04.796271   83720 ethash.go:220] Generating DAG for epoch 0 (size 1073739904) (0000000000000000000000000000000000000000000000000000000000000000)
I1118 18:10:05.528098   83720 ethash.go:252] Generating DAG: 0%
I1118 18:10:09.470387   83720 ethash.go:252] Generating DAG: 1%
...
I1118 18:16:43.767504   83720 ethash.go:252] Generating DAG: 98%
I1118 18:16:48.587833   83720 ethash.go:252] Generating DAG: 99%
I1118 18:16:52.545269   83720 ethash.go:252] Generating DAG: 100%
I1118 18:16:52.547874   83720 ethash.go:237] Done generating DAG for epoch 0, it took 6m47.752030111s
I1118 18:16:55.442401   83720 worker.go:348] 🔨  Mined block (#1 / 9a5b753e). Wait 5 blocks for confirmation
I1118 18:16:55.444259   83720 worker.go:569] commit new work on block 2 with 0 txs & 0 uncles. Took 1.823975ms

...
```

ずっとマイニングし続けるので適当なところで`miner.stop()`と打って止める

```
I1118 18:20:49.618426   83720 worker.go:569] commit new work on block 223 with 0 txs & 0 uncles. Took 316.812µs
I1118 18:20:49.618450   83720 worker.go:447] 🔨 🔗  Mined 5 blocks back: block #217
I1118 18:20:49.620637   83720 worker.go:569] commit new work on block 223 with 0 txs & 0 uncles. Took 365.96µs
I1118 18:20:49.805888   83720 worker.go:348] 🔨  Mined block (#223 / cc113f58). Wait 5 blocks for confirmation
I1118 18:20:49.806210   83720 worker.go:569] commit new work on block 224 with 0 txs & 0 uncles. Took 285.018µs
I1118 18:20:49.806234   83720 worker.go:447] 🔨 🔗  Mined 5 blocks back: block #218
I1118 18:20:49.808358   83720 worker.go:569] commit new work on block 224 with 0 txs & 0 uncles. Took 215.045µs

true
>
```

# 送金

### 残高確認
```
> eth.getBalance(eth.accounts[0])
334500000000000000000
```

大金持ち！

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

貧乏！

### 送金

```
> eth.sendTransaction({from: eth.accounts[0], to: eth.accounts[1], value: web3.toWei(1, "ether")})
Please unlock account e3b88b800ba2494219824863c96cf7c36e2903f5.
Passphrase:
```

パスフレーズを求められるので適当な文字列を入力. ここでは"testuser1"とした.

```
Account is now unlocked for this session.
I1118 18:28:07.461451   83720 xeth.go:1028] Tx(0x13298e3e6904a874690ee5cb0e76cafa8bd141998070b8114289699f2e87bd36) to: 0x8f25b1d49fa60936b1b0b35d5d337a6966163b5c
"0x13298e3e6904a874690ee5cb0e76cafa8bd141998070b8114289699f2e87bd36"
>
```

### 未確定トランザクションの確認

```
> eth.pendingTransactions
[{
    data: "0x",
    from: "0xe3b88b800ba2494219824863c96cf7c36e2903f5",
    gas: "90000",
    gasPrice: "20000000000",
    hash: "0x13298e3e6904a874690ee5cb0e76cafa8bd141998070b8114289699f2e87bd36",
    nonce: "0",
    to: "0x8f25b1d49fa60936b1b0b35d5d337a6966163b5c",
    value: "1000000000000000000"
}]
```

### マイニングの開始
```
> miner.start()
```

`miner.stop()`で止める.

### 未確定トランザクションが消えていることを確認.
```
> eth.pendingTransactions
null
```

### 送金の確認
```
> eth.getBalance(eth.accounts[1])
1000000000000000000
>
```

増えてる！送金成功！

### gethの停止
```
> exit
```