Bitcoin Indexer with Extended btcd


Bitcoin Indexer

BTCのトランザクションで、特定のアドレスに対して入金があったかどうかを調べるために
Bitcoinのブロックデータをパースして mysql に入れられないか調査して見ました。

Golangで実装されているBitcoinのライブラリがうまく拡張できるように実装されていたので、
そちらを利用して追加実装して見ました。

内容

Bitcoinのブロックデータを保存する先を mysql にします。
以下のテーブルにデータを貯めることにします。

-- blocks
drop table if exists `blocks`;
create table `blocks` (
  `id` int(11) not null auto_increment,
  `network` int(11) not null,
  `block_len` int(11) not null,
  `checksum` varchar(8) character set utf8 not null default '',
  `raw_bytes` mediumblob not null,
  `hash` varchar(64) character set utf8 not null default '',
  `height` int(11) not null default 0,
  `block_time` timestamp not null default current_timestamp,
  `created` timestamp not null default current_timestamp,
  `updated` timestamp not null default current_timestamp on update current_timestamp,
  primary key (`id`),
  index idx_blocks_01 (`hash`),
  index idx_blocks_02 (`height`)
) engine=innodb default character set utf8;

-- transaction_outputs
drop table if exists `transaction_outputs`;
create table `transaction_outputs` (
  `id` int(11) not null auto_increment,
  `block_id` int(11) not null,
  `transaction_id` varchar(64) character set utf8 not null default '',
  `amount` bigint not null,
  `pk_script_bytes` mediumblob not null,
  `pk_script_class` tinyint unsigned not null,
  `created` timestamp not null default current_timestamp,
  `updated` timestamp not null default current_timestamp on update current_timestamp,
  primary key (`id`),
  index idx_transaction_outputs_01 (`block_id`),
  index idx_transaction_outputs_02 (`transaction_id`)
) engine=innodb default character set utf8;

-- transaction_output_addresses
drop table if exists `transaction_output_addresses`;
create table `transaction_output_addresses` (
  `id` int(11) not null auto_increment,
  `block_id` int(11) not null,
  `tx_out_id` int(11) not null,
  `address` varchar(255) character set utf8 not null default '',
  `created` timestamp not null default current_timestamp,
  `updated` timestamp not null default current_timestamp on update current_timestamp,
  primary key (`id`),
  index idx_transaction_output_addresses_01 (`address`)
) engine=innodb default character set utf8;

transaction_output_addresses に入金されたアドレスの情報が溜まって行き、そこを監視することで、
入金を確認することができるようになります。

ここに実装した内容が格納されています。

動作確認

build & install

$ go install github.com/btcsuite/btcd

run

docker で mysqlを構築し、user/pass/dbnameなどを適時設定しておく。
その後、btcd.conf を作成して、それをコマンドラインから指定して btcd を起動する

data_dir=`pwd`/data
./btcd --configfile=${data_dir}/btcd.conf --datadir=${data_dir} --logdir=${data_dir}/logs
[Application Options]
testnet=1
dbtype="mysqlldb"
dbrwconnection="user:pass@tcp(127.0.0.1:3306)/dbname"
dbroconnection="user:pass@tcp(127.0.0.1:3306)/dbname"

; ------------------------------------------------------------------------------
; Debug
; ------------------------------------------------------------------------------
debuglevel="debug"

こんな感じで実行すると・・
以下のような感じでデータが取得できる

注意事項

まだテスト段階のソースで、運用実績がありません。 利用は自己責任 でお願いします。また、バグ等の指摘は歓迎しますが、サポートは致しかねる場合があります。

以上になります。