渡辺・松本・西村・清水(2017)『はじめてのブロックチェーンアプリケーション』翔泳社
を参考に、ブロックチェーンの実装を試している。
書籍とは異なったやり方を採用したり、試行錯誤した箇所があるので、実施した内容を記録する。
環境
VM
Google Cloud Platform で Compute Engine のインスタンスを用意する
- vCPU x 1
- Ubuntu 16.04LTS
- SSDディスク 10GB
- デフォルトのアクセス権
- HTTPトラフィックを許可する
- ネットワークは、ひとまずエフェメラル。必要になれば静的IPに変更
パッケージを更新/インストールする
パッケージをアップデート。git と tree あたりをインストールしておく。
$ sudo apt-get -y update
$ sudo apt-get install -y git tree
Go Ethereum の 安定版をインストールする
apt-get を用いて go-ethereum をインストール
リポジトリを設定。
$ sudo add-apt-repository -y ppa:ethereum/ethereum
Go Ethereum の 安定版をインストール
$ sudo apt-get update
$ sudo apt-get install ethereum
バージョンは、1.8.2-stable
$ geth version
Geth
Version: 1.8.2-stable
Git Commit: b8b9f7f4476a30a0aaf6077daade6ae77f969960
Architecture: amd64
Protocol Versions: [63 62]
Network Id: 1
Go Version: go1.9.4
Operating System: linux
GOPATH=
GOROOT=/usr/lib/go-1.9
参考:
https://ethereum.github.io/go-ethereum/install/#install-on-ubuntu-via-ppas
Geth を設定する
genesis ファイルを作成する
$ mkdir -p ~/eth/data_testnet
$ vi ~/eth/data_testnet/genesis.json
{
"config": {
"chainId" : 15,
"homesteadBlock" : 0,
"eip155Block" : 0,
"eip158Block" : 0
},
"nonce": "0x0000000000000042",
"timestamp": "0x00",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"extraData": "0x00",
"gasLimit": "0x8000000",
"difficulty": "0x4000",
"mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"coinbase": "0x3333333333333333333333333333333333333333",
"alloc": {}
}
参考:
- 書籍「はじめてのブロックチェーンアプリケーション」
- https://www.tdi.co.jp/miso/blockchain-geth
Geth を初期化する
$ geth --datadir ~/eth/data_testnet init ~/eth/data_testnet/genesis.json > ~/eth/
data_testnet/genesis.log
INFO [03-17|23:32:19] Maximum peer count ETH=25 LES=0 total=25
INFO [03-17|23:32:19] Allocated cache and file handles database=/home/a16z6rh/eth/data_testnet/geth/chaindata cache=16 handles=16
INFO [03-17|23:32:19] Writing custom genesis block
INFO [03-17|23:32:19] Persisted trie from memory database nodes=0 size=0.00B time=3.822µs gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [03-17|23:32:19] Successfully wrote genesis state database=chaindata hash=3b3326…f217d7
INFO [03-17|23:32:19] Allocated cache and file handles database=/home/a16z6rh/eth/data_testnet/geth/lightchaindata cache=16 handles=16
INFO [03-17|23:32:19] Writing custom genesis block
INFO [03-17|23:32:19] Persisted trie from memory database nodes=0 size=0.00B time=2.157µs gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [03-17|23:32:19] Successfully wrote genesis state database=lightchaindata hash=3b3326…f217d7
作成されたファイルを確認する
$ tree ~/eth/data_testnet
/home/hoge/eth/data_testnet
├── genesis.json
├── genesis.log
├── geth
│ ├── chaindata
│ │ ├── 000001.log
│ │ ├── CURRENT
│ │ ├── LOCK
│ │ ├── LOG
│ │ └── MANIFEST-000000
│ └── lightchaindata
│ ├── 000001.log
│ ├── CURRENT
│ ├── LOCK
│ ├── LOG
│ └── MANIFEST-000000
└── keystore
4 directories, 12 files
参考:初期化した DB をクリアするには
$ geth removedb
参考:https://ethereum.gitbooks.io/frontier-guide/content/backup_restore.html
テストネットワークで Geth を起動する
方法1:フォアグラウンドで起動し、コンソールで直接操作する
$ geth --networkid 4649 --nodiscover --maxpeers 0 --datadir ~/eth/data_testnet console 2>> ~/eth/data_testnet/geth.log
Welcome to the Geth JavaScript console!
instance: Geth/v1.8.2-stable-b8b9f7f4/linux-amd64/go1.9.4
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
>
方法2:バックグラウンドで起動し、RPC 接続する
Geth を起動
バックグランドで起動する。
HTTP endpoint opened というメッセージが表示されれば、RPC 接続可能な状態になっている。
なお、--mine オプションをつけたので、起動すれば自動的にマイニングが始まる。
$ nohup geth --networkid 4649 --nodiscover --maxpeers 0 --datadir ~/eth/data_testnet --mine --minerthreads 1 -rpc 2>> ~/eth/data_testnet/geth.log &
INFO [03-18|14:05:26] Maximum peer count ETH=0 LES=0 total=0
INFO [03-18|14:05:26] Starting peer-to-peer node instance=Geth/v1.8.2-stable-b8b9f7f4/linux-amd64/go1.9.4
INFO [03-18|14:05:26] Allocated cache and file handles database=/home/a16z6rh/eth/data_testnet/geth/chaindata cache=768 handles=512
INFO [03-18|14:05:26] Initialised chain configuration config="{ChainID: 15 Homestead: 0 DAO: <nil> DAOSupp ort: false EIP150: <nil> EIP155: 0 EIP158: 0 Byzantium: <nil> Constantinople: <nil> Engine: unknown}"
INFO [03-18|14:05:26] Disk storage enabled for ethash caches dir=/home/a16z6rh/eth/data_testnet/geth/ethash count=3
INFO [03-18|14:05:26] Disk storage enabled for ethash DAGs dir=/home/a16z6rh/.ethash count=2
INFO [03-18|14:05:26] Initialising Ethereum protocol versions="[63 62]" network=4649
INFO [03-18|14:05:26] Loaded most recent local header number=462 hash=26e7da…20c961 td=65697700
INFO [03-18|14:05:26] Loaded most recent local full block number=462 hash=26e7da…20c961 td=65697700
INFO [03-18|14:05:26] Loaded most recent local fast block number=462 hash=26e7da…20c961 td=65697700
INFO [03-18|14:05:26] Loaded local transaction journal transactions=0 dropped=0
INFO [03-18|14:05:26] Regenerated local transaction journal transactions=0 accounts=0
WARN [03-18|14:05:26] Blockchain not empty, fast sync disabled
INFO [03-18|14:05:26] Starting P2P networking
INFO [03-18|14:05:26] RLPx listener up self="enode://e0c74f29ac9ef17808cc914da35af1f6d1d0027bec85dbae6c07069769ab5a295b2cdd09cb883af764bfc33d2272149438798f8ff4d52901d13247f76de8829f@[::]:30303?discport=0"
INFO [03-18|14:05:26] HTTP endpoint opened url=http://127.0.0.1:8545 cors= vhosts=localhost
INFO [03-18|14:05:26] Transaction pool price threshold updated price=18000000000
INFO [03-18|14:05:26] Etherbase automatically configured address=0x9243861e2EAf17d07A4B7d3cEA8b6D36CAFDC107
INFO [03-18|14:05:26] Starting mining operation
INFO [03-18|14:05:26] Commit new mining work number=463 txs=0 uncles=0 elapsed=179.803µs
INFO [03-18|14:05:26] IPC endpoint opened url=/home/a16z6rh/eth/data_testnet/geth.ipc
INFO [03-18|14:05:39] Successfully sealed new block number=463 hash=5ac2ce…a9df3d
INFO [03-18|14:05:39] 🔨 mined potential block number=463 hash=5ac2ce…a9df3d
RPC 接続
$ geth attach rpc:http://localhost:8545
Welcome to the Geth JavaScript console!
instance: Geth/v1.8.2-stable-b8b9f7f4/linux-amd64/go1.9.4
coinbase: 0x9243861e2eaf17d07a4b7d3cea8b6d36cafdc107
at block: 471 (Sun, 18 Mar 2018 14:06:11 UTC)
modules: eth:1.0 net:1.0 rpc:1.0 web3:1.0
>
Geth を停止
プロセスを kill する
方法3:バックグラウンド起動し、JSON-RPC で接続する
Geth を起動
$ nohup geth --networkid 4649 --nodiscover --maxpeers 0 --datadir ~/eth/data_testnet --mine --minerthreads 1 -rpc -rpcaddr "0.0.0.0" --rpcport 8545 --rpccorsdomain "*" --rpcapi "admin, db, eth, debug, miner, net, shh, txpool, personal, web3" 2>> ~/eth/data_testnet/geth.log &
Geth のメソッドを呼び出す
アカウント一覧の取得を呼び出す例を挙げる。
$ curl -H "Content-Type: application/json" --data '{"jsonrpc" : "2.0", "method" : "personal_listAccounts", "params" : [], "id" : 10}' localhost:8545
{"jsonrpc":"2.0","id":10,"result":["0x9243861e2eaf17d07a4b7d3cea8b6d36cafdc107", "0xf0e88a84e2ec8cc0e97df28442c508fd8cad7ce2", "0xe75c9e894afa93d6957d4b0a8f8b87a9b21d68c6"]}
参考:http://alek3.hatenablog.com/entry/2017/12/19/210000
Geth を停止
プロセスを kill する
以上