Bitcoin Cashのノードを立てる方法




環境

  • AWS Amazon linux t2.medium
  • NVM v0.33.8
  • Node.js v4.8.7

クライアント

Bitcoin Cashクライアントは/bitcoreを使っています。これはBitpayのBitcoreをフォークしてBitcon Cash用に修正を加えたものみたいです。

ちなみにsatoshilabsのbitcoreはTREZORで使われているとのことなので本当なら問題ないかと思います。

Node.jsとgitのインストール

Node.jsとgitのインストールは下記にまとめていますので参考にしてください。

参考記事:EC2にNode.jsとGitをインストールする最小手順

クライアントのインストール

インストールはgithubからリポジトリをクローンし、npm installします。ブランチはbcashというのがあるのでこれに切り替えます。

$ git clone https://github.com/satoshilabs/bitcore.git
$ cd bitcore
$ git fetch origin bcash
$ git checkout bcash
$ npm install

ノードの起動と同期

起動と同期は下記コマンド実行するだけです。

$ node bin/bitcored

しかし、実行するとこんなエラーが。。。

$ node bin/bitcored
/home/ec2-user/app/bitcore/node_modules/insight-api/node_modules/bitcore-lib/index.js:12
    throw new Error(message);
    ^

Error: More than one instance of bitcore-lib found. Please make sure to require bitcore-lib and check that submodules do not also include their own bitcore-lib dependency.
    at Object.bitcore.versionGuard (/home/ec2-user/app/bitcore/node_modules/insight-api/node_modules/bitcore-lib/index.js:12:11)
    at Object.<anonymous> (/home/ec2-user/app/bitcore/node_modules/insight-api/node_modules/bitcore-lib/index.js:15:9)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Module.require (module.js:353:17)
    at require (internal/module.js:12:17)
    at Object.<anonymous> (/home/ec2-user/app/bitcore/node_modules/insight-api/lib/blocks.js:4:15)
    at Module._compile (module.js:409:26)

このエラーはフォーク元のISSUEでも上がっていたので同じ対処すると解消します。ISSUEはこちら

$ vi node_modules/insight-api/node_modules/bitcore-lib/index.js

'use strict';

var bitcore = module.exports;

// module information
bitcore.version = 'v' + require('./package.json').version;
bitcore.versionGuard = function(version) {
  return; //この行を追加
  if (version !== undefined) {
    var message = 'More than one instance of bitcore-lib found. ' +
      'Please make sure to require bitcore-lib and check that submodules do' +
      ' not also include their own bitcore-lib dependency.';
    throw new Error(message);
  }
};

しかし、今度は別のところで同じエラーが出ます。

$ node bin/bitcored
/home/ec2-user/app/bitcore/node_modules/insight-api/node_modules/bitcore-message/node_modules/bitcore-lib/index.js:12
    throw new Error(message);
    ^

Error: More than one instance of bitcore-lib found. Please make sure to require bitcore-lib and check that submodules do not also include their own bitcore-lib dependency.
    at Object.bitcore.versionGuard (/home/ec2-user/app/bitcore/node_modules/insight-api/node_modules/bitcore-message/node_modules/bitcore-lib/index.js:12:11)
    at Object.<anonymous> (/home/ec2-user/app/bitcore/node_modules/insight-api/node_modules/bitcore-message/node_modules/bitcore-lib/index.js:15:9)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Module.require (module.js:353:17)
    at require (internal/module.js:12:17)
    at Object.<anonymous> (/home/ec2-user/app/bitcore/node_modules/insight-api/node_modules/bitcore-message/index.js:1:77)
    at Module._compile (module.js:409:26)

再度returnを追記します。

$ vi node_modules/insight-api/node_modules/bitcore-message/node_modules/bitcore-lib/index.js

'use strict';

var bitcore = module.exports;

// module information
bitcore.version = 'v' + require('./package.json').version;
bitcore.versionGuard = function(version) {
  return; // この行を追加
  if (version !== undefined) {
    var message = 'More than one instance of bitcore-lib found. ' +
      'Please make sure to require bitcore-lib and check that submodules do' +
      ' not also include their own bitcore-lib dependency.';
    throw new Error(message);
  }
};

すると今度はうまくいってノードが起動し、同期が始まります。かなり少しずつですがブロック高が積み上がっているのがわかります。

[ec2-user@ip-172-31-28-90 bitcore]$ node bin/bitcored
[2018-03-27T00:11:49.341Z] info: Using config: /home/ec2-user/.bitcore/bitcore-node.json
[2018-03-27T00:11:49.342Z] info: Using network: livenet
[2018-03-27T00:11:49.343Z] info: Starting bitcoind
[2018-03-27T00:11:49.345Z] info: Using bitcoin config file: /home/ec2-user/.bitcore/data/bitcoin.conf
[2018-03-27T00:11:49.348Z] info: Starting bitcoin process
[2018-03-27T00:11:54.390Z] info: Bitcoin Height: 0 Percentage: 0.00
[2018-03-27T00:11:54.395Z] info: Bitcoin Daemon Ready
[2018-03-27T00:11:54.395Z] info: Starting web
[2018-03-27T00:11:54.433Z] info: Starting insight-api
[2018-03-27T00:11:54.434Z] info: Starting insight-ui
[2018-03-27T00:11:54.436Z] info: Bitcore Node ready
[2018-03-27T00:11:54.880Z] warn: ZMQ connection delay: tcp://127.0.0.1:28332
[2018-03-27T00:11:54.881Z] info: ZMQ connected to: tcp://127.0.0.1:28332
[2018-03-27T00:12:54.404Z] info: Bitcoin Height: 192 Percentage: 0.00
[2018-03-27T00:13:09.401Z] info: Bitcoin Height: 26573 Percentage: 0.01
[2018-03-27T00:13:24.441Z] info: Bitcoin Height: 26573 Percentage: 0.02
[2018-03-27T00:13:39.402Z] info: Bitcoin Height: 79150 Percentage: 0.04
[2018-03-27T00:13:54.406Z] info: Bitcoin Height: 79150 Percentage: 0.07
[2018-03-27T00:14:09.408Z] info: Bitcoin Height: 113014 Percentage: 0.10

bitcoindのバージョン確認

実際にノードの裏側で起動しているbitcoindを確認します。bitcoinabcが起動していることが確認でき、期待通りの動作をしてくれていることが確認できます。

$ ps aux | grep bitcoind
uec2-user    4368  145  5.0 3391824 829232 ?      SLl  May12 2481:24 /home/ec2-user/bitcore/node_modules/bitcore-node/bin/bitcoind --conf=/home/ec2-user/.bitcore/data/bitcoin.conf --datadir=/home/ec2-user/.bitcore/data
$ /home/ec2-user/bitcore/node_modules/bitcore-node/bin/bitcoind --version
Bitcoin ABC Daemon version v0.16.0.0-4199db8-dirty
Copyright (C) 2009-2017 The Bitcoin developers
Copyright (C) 2009-2017 The Bitcoin Core developers

Please contribute if you find Bitcoin ABC useful. Visit
<https://www.bitcoinabc.org> for further information about the software.
The source code is available from <https://github.com/Bitcoin-ABC/bitcoin-abc>.

This is experimental software.
Distributed under the MIT software license, see the accompanying file COPYING
or <https://opensource.org/licenses/MIT>

This product includes software developed by the OpenSSL Project for use in the
OpenSSL Toolkit <https://www.openssl.org> and cryptographic software written by
Eric Young and UPnP software written by Thomas Bernard.

 

ネットワークの変更

接続先ネットワークを変更するには下記bitcore-node.jsonのnetworkを変更します。本番ネットワークはlivenet、テストネットワークはtestnetを指定するとそのネットワークのブロックを取得できます。

$ cat~/.bitcore/bitcore-node.json
{
  "network": "livenet",
  "port": 3001,
  "services": [
    "bitcoind",
    "web",
    "insight-api",
    "insight-ui"
  ],
  "servicesConfig": {
    "bitcoind": {
      "spawn": {
        "datadir": "/home/ec2-user/.bitcore/data",
        "exec": "/home/ec2-user/bitcore/node_modules/bitcore-node/bin/bitcoind"
      }
    }
  }
}

Insight APIを叩く

ノードを立てることができたので今度はブロック情報を取得できるAPIを叩いてみます。Insight APIのドキュメントはこちらです。

まず初めはジェネシスブロックを取得してみました。ブロックハッシュが取得できたのでBlock Explorerなどで確認できます。

$ curl http://localhost:3001/insight-api/block-index/0
{"blockHash":"000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f"}

次はUTXOを取得してみます。ブロック高1にあるアドレスを指定してみました。かなり多くのUTXOが取得できたので前半部分のみ記載しています。

$ curl http://localhost:3001/insight-api/addr/12c6DSiU4Rq3P4ZxziKxzrL5LmMBrzjrJX/utxo
[{"address":"12c6DSiU4Rq3P4ZxziKxzrL5LmMBrzjrJX","txid":"268a648e76074ff6adff922100a72c908d33259295f10f4a5352020916ad4b83","vout":1,"scriptPubKey":"76a914119b098e2e980a229e139a9ed01a469e518e6f2688ac","amount":0.00000777,"satoshis":777,"height":358348,"confirmations":2552},{"address":"12c6DSiU4Rq3P4ZxziKxzrL5LmMBrzjrJX","txid":"571060bfdf239d7272415b502107f14a41e83834a762a38b6325e6217f48b97a","vout":0,"scriptPubKey":"76a914119b098e2e980a229e139a9ed01a469e518e6f2688ac","amount":0.00000777,"satoshis":777,"height":335380,"confirmations":25520},{"address":"12c6DSiU4Rq3P4ZxziKxzrL5LmMBrzjrJX","txid":"82e9cc08d424451e6ff1347a9ef62c88585fa658da3e36c7ce9a18de94726e8e","vout":0,"scriptPubKey":"76a914119b098e2e980a229e139a9ed01a469e518e6f2688ac","amount":0.00007578,"satoshis":7578,"height":333497,"confirmations":27403},,,,]

UTXOの各項目はこのようになっています。

{
    "address":"12c6DSiU4Rq3P4ZxziKxzrL5LmMBrzjrJX",
    "txid":"268a648e76074ff6adff922100a72c908d33259295f10f4a5352020916ad4b83",
    "vout":1,
    "scriptPubKey":"76a914119b098e2e980a229e139a9ed01a469e518e6f2688ac",
    "amount":0.00000777,
    "satoshis":777,
    "height":358348,
    "confirmations":2552
}

まとめ

Bitcoin Cashのノードの起動からInsight APIを使ったブロックチェーン内の情報取得を行いましたが意外と簡単にできました。

ノードを立てていじるだけでもかなりの勉強になったのでぜひ試してみてください。

追記

サーバをUbuntuで再度試してみたところ起動時のエラーが出なかったです。詳しい方がいましたらアドバイス頂けたらと思います。

最後に

最後にブロックチェーンエンジニアって何?どうやったらなれるの?という人向けに書いた下の記事も読んでみてください。

>>ブロックチェーンエンジニアになるには何をすべきか

おすすめ書籍

ビットコインとブロックチェーンの詳細をしっかりと学びたい方にはこちらの書籍が非常におすすめです。ウォレットの仕組み、楕円曲線暗号、P2Pプロトコル、公開鍵暗号などビットコインを支える技術について詳細に解説されています。また、サンプルコードを通して実際に手を動かして学べるので非常に濃い内容となっています。

こちらはブロックチェーンの初学者向けになります。ブロックチェーンの応用例の紹介から基礎的な理論まで分かりやすく図解で解説されています。また、ビットコインやEthereum、HyperLdger Fabricを実際に稼働させてみて動作を理解するためのサンプルコードの解説もあります。

The following two tabs change content below.

髙妻智一

2013年CyberAgent新卒入社 スマホゲームを作る子会社に所属し、サーバーサイドのエンジニアを担当。2年目の終わりから新規子会社の立ち上げに参加し、サーバーサイドのエンジニアリーダーとしてサービースのリリースから運用までを担当。 2018年仮想通貨のスマホウォレットを提供するGinco Incにブロックチェーンエンジニアとして入社。






よく読まれている関連記事はこちら




コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です