- Home
- Naivechainを用いたブロックチェーン動作の確認
Naivechainを用いたブロックチェーン動作の確認
Naivechain(200行のJavascriptで実装された超シンプルなブロックチェーン)を用いてブロックチェーンの同期までを確認する。
Naivechainの重要な概念
- ノードを制御するHTTPインターフェース
- Websocketを用いて他のノードと通信する(P2P)
- P2P通信による非常にシンプルな「プロトコル」
- ノードにデータが保持されない
- PoW、PoSを用いない(ブロック競合なし)
環境情報
この記事の環境は以下の通り。
$ cat /etc/os-release NAME="Ubuntu" VERSION="18.04.2 LTS (Bionic Beaver)" $ node -v v8.12.0 $ npm -v 6.9.0
Naivechainの導入
リポジトリのダウンロード
$ git clone https://github.com/lhartikk/naivechain.git Cloning into 'naivechain'... remote: Enumerating objects: 40, done. remote: Total 40 (delta 0), reused 0 (delta 0), pack-reused 40 Unpacking objects: 100% (40/40), done.
パッケージのインストール
$ cd naivechain $ npm install npm notice created a lockfile as package-lock.json. You should commit this file. npm WARN naivechain@1.0.0 No repository field. npm WARN naivechain@1.0.0 No license field. added 69 packages from 35 contributors and audited 107 packages in 1.683s found 17 vulnerabilities (4 low, 9 moderate, 4 high) run `npm audit fix` to fix them, or `npm audit` for details
naivechainの起動と同期
ピアA起動
ターミナル1(11.11.11.11)でピアAを起動する。
$ HTTP_PORT=3001 P2P_PORT=6001 npm start naivechain@1.0.0 start /home/ubuntu/blockchain/naivechain node main.js listening websocket p2p port on: 6001 Listening http on port: 3001
ピアBの追加
ターミナル2(22.22.22.22)でピアBを起動する。
$ HTTP_PORT=3002 P2P_PORT=6002 PEERS=ws://11.11.11.11:6001 npm start #ピアAのアドレス naivechain@1.0.0 start /home/ubuntu/blockchain/naivechain node main.js listening websocket p2p port on: 6002 Listening http on port: 3002
それぞれのピアに追加されたメッセージが表示される。
Received message{"type":0} Received message{"type":2,"data":"[{\"index\":0,\"previousHash\":\"0\",\"timestamp\":1465154705,\"data\":\"my genesis block!!\",\"hash\":\"816534932c2b7154836da6afc367695e6337db8a921823784c14378abed4f7d7\"}]"} received blockchain is not longer than current blockchain. Do nothing
ジェネシスブロックの確認
ターミナル3(11.11.11.11)でジェネシスブロックを確認する。
$ curl http://11.11.11.11:3001/blocks [{"index":0,"previousHash":"0","timestamp":1465154705,"data":"my genesis block!!","hash":"816534932c2b7154836da6afc367695e6337db8a921823784c14378abed4f7d7"}] $ curl http://22.22.22.22:3002/blocks [{"index":0,"previousHash":"0","timestamp":1465154705,"data":"my genesis block!!","hash":"816534932c2b7154836da6afc367695e6337db8a921823784c14378abed4f7d7"}]
ブロックの追加
$ curl -H "Content-type:application/json" --data '{"data" : "Some data to the first block"}' http://localhost:3001/mineBlock ▼ターミナルAで出力 block added: {"index":1,"previousHash":"816534932c2b7154836da6afc367695e6337db8a921823784c14378abed4f7d7","timestamp":1569893744.968,"data":"Some data to the first block","hash":"7e2258aca89030fa255e2b1eb8c37dbd94abd2e4ff962eee515e61d6f83d4e51"} Received message{"type":2,"data":"[{\"index\":1,\"previousHash\":\"816534932c2b7154836da6afc367695e6337db8a921823784c14378abed4f7d7\",\"timestamp\":1569893744.968,\"data\":\"Some data to the first block\",\"hash\":\"7e2258aca89030fa255e2b1eb8c37dbd94abd2e4ff962eee515e61d6f83d4e51\"}]"} received blockchain is not longer than current blockchain. Do nothing ▼ターミナルBで出力 Received message{"type":2,"data":"[{\"index\":1,\"previousHash\":\"816534932c2b7154836da6afc367695e6337db8a921823784c14378abed4f7d7\",\"timestamp\":1569893744.968,\"data\":\"Some data to the first block\",\"hash\":\"7e2258aca89030fa255e2b1eb8c37dbd94abd2e4ff962eee515e61d6f83d4e51\"}]"} blockchain possibly behind. We got: 0 Peer got: 1 We can append the received block to our chain
同期の確認
ターミナル3(11.11.11.11)で追加されたブロックが同期されていることを確認する。
$ curl http://11.11.11.11:3001/blocks [{"index":0,"previousHash":"0","timestamp":1465154705,"data":"my genesis block!!","hash":"816534932c2b7154836da6afc367695e6337db8a921823784c14378abed4f7d7"},{"index":1,"previousHash":"816534932c2b7154836da6afc367695e6337db8a921823784c14378abed4f7d7","timestamp":1569893744.968,"data":"Some data to the first block","hash":"7e2258aca89030fa255e2b1eb8c37dbd94abd2e4ff962eee515e61d6f83d4e51"}] $ curl http://22.22.22.22:3002/blocks [{"index":0,"previousHash":"0","timestamp":1465154705,"data":"my genesis block!!","hash":"816534932c2b7154836da6afc367695e6337db8a921823784c14378abed4f7d7"},{"index":1,"previousHash":"816534932c2b7154836da6afc367695e6337db8a921823784c14378abed4f7d7","timestamp":1569893744.968,"data":"Some data to the first block","hash":"7e2258aca89030fa255e2b1eb8c37dbd94abd2e4ff962eee515e61d6f83d4e51"}]
ここまででピアAおよびピアBの同期が確認できる。
ご相談・お見積もり
03-5207-2689