2020/09/01

[ADA]Cardano DB Syncを起動するDockerfileを作りました

cardanodocker

やったこと

cardano-db-syncは、cardanoのブロックチェーンの情報をpostgresqlに同期してくれるサービスです。
以下のドキュメントを参考にしつつ
最新のソースコードからcardano-db-syncを起動するDocker imageを開発しました。

Motivation

inputoutput/cardano-db-sync - Docker Hubも準備されていたのですが、将来的に細かい中身の制御などをしたかったので、
debianベースのimageで作ることにしました。

Dockerfile

  • cardano-db-sync

がinstallされたdocker imageを作成しました。

FROM debian:stretch-slim

ENV CARDANO_DB_SYNC_VERSION=4.0.0

# Install dependencies
RUN apt-get update -y
RUN apt-get install -y --no-install-recommends \
      sudo \
      locales \
      git \
      curl \
      wget \
      gnupg \
      xz-utils

# avoid ssl error when wget https site
RUN apt-get install -y --no-install-recommends ca-certificates

# install postgreql client
RUN wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add - && \
    echo "deb http://apt.postgresql.org/pub/repos/apt/ stretch-pgdg main" | tee /etc/apt/sources.list.d/postgresql.list && \
    apt-get update -y && \
    apt-get install -y --no-install-recommends postgresql-client-11

RUN useradd -m -d /home/cardano -s /bin/bash cardano
RUN usermod -a -G sudo cardano
RUN echo " cardano      ALL=(ALL:ALL) NOPASSWD: ALL" >> /etc/sudoers
RUN echo "en_US.UTF-8 UTF-8" > /etc/locale.gen
RUN locale-gen en_US.UTF-8
ENV LANG en_US.UTF-8
ENV LC_ALL en_US.UTF-8

WORKDIR /home/cardano
USER cardano

# Install nix
RUN curl -L https://nixos.org/nix/install | sh
ENV PATH=/home/cardano/.nix-profile/bin:${PATH}
ENV NIX_PATH=/home/cardano/.nix-defexpr/channels
ENV NIX_PROFILES=/home/cardano/.nix-profile
ENV NIX_SSL_CERT_FILE=/etc/ssl/certs/ca-certificates.crt

# To improve build speed, it is possible to set up a binary cache maintained by IOHK (this is optional):
# https://github.com/input-output-hk/cardano-node/blob/468f52e5a6a2f18a2a89218a849d702481819f0b/doc/getting-started/building-the-node-using-nix.md#building-under-nix
# https://www.mankier.com/5/nix.conf
ADD nix.conf /home/cardano/.config/nix/

# Download the source code for cardano-wallet and Build and install the wallet
RUN git clone https://github.com/input-output-hk/cardano-db-sync.git && \
    cd cardano-db-sync && \
    git fetch --all --tags && \
    git checkout tags/${CARDANO_DB_SYNC_VERSION} && \
    nix-build -A cardano-db-sync-extended -o db-sync-node && \
    sudo mv db-sync-node/bin/cardano-db-sync-extended /usr/local/bin/

nix.conf の設定です。(これをやらないと、ビルドが数時間に及んでしまいます。これをやれば5分くらい)

substituters = https://cache.nixos.org https://hydra.iohk.io
trusted-substituters =
trusted-public-keys = iohk.cachix.org-1:DpRUyj7h7V830dp/i6Nti+NEO2/nhblbov/8MW7Rqoo= hydra.iohk.io:f/Ea+s+dFdN+3Y/G+FDgSq+a5NEWhJGzdjvKNGv0/EQ= cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY=

こちらを元に、起動スクリプトを足して完成になります。

(実際は)cardano-db-sync/config at master · input-output-hk/cardano-db-syncを参考にしてconfigファイルを設定します。
注意しなくてはいけないのは、上記のconfigファイルだとうまく動かない箇所があるので、そのままコピペだとダメでした。

testnet-config.yaml の以下の箇所を修正しました。

Protocol: Byron

ByronGenesisFile: ../cardano-node/configuration/defaults/byron-testnet/genesis.json
ByronGenesisHash: 96fceff972c2c06bd3bb5243c39215333be6d56aaf4823073dca31afe5038471

ShelleyGenesisFile: not/applicable
ShelleyGenesisHash: not/applicable

Protocol: Byron

ByronGenesisFile: /path/to/testnet-byron-genesis.json
ByronGenesisHash: 96fceff972c2c06bd3bb5243c39215333be6d56aaf4823073dca31afe5038471

ShelleyGenesisFile: /path/to/testnet-shelley-genesis.json
ShelleyGenesisHash: 849a1764f152e1b09c89c0dfdbcbdd38d711d1fec2db5dfa0f87cf2737a0eaf4
  • また、起動する際は、host上にcardano-nodeのsocketをvolumeで共有させて使用する必要があります。

起動後の確認

Log

正しく起動できると、以下のようなログが出力されます。

[db-sync-node:Info:32] [2020-09-01 12:27:13.94 UTC] Starting chainSyncClient
[db-sync-node:Info:32] [2020-09-01 12:27:14.01 UTC] Cardano.Db tip is at genesis
[db-sync-node:Info:37] [2020-09-01 12:27:14.01 UTC] Running DB thread
[db-sync-node:Info:37] [2020-09-01 12:27:14.16 UTC] Rolling back to slot 0, hash genesis
[db-sync-node:Info:41] [2020-09-01 12:27:14.16 UTC] getHistoryInterpreter: acquired
[db-sync-node:Info:37] [2020-09-01 12:27:14.17 UTC] Deleting blocks numbered: []
[db-sync-node:Info:37] [2020-09-01 12:27:14.76 UTC] insertABOBBoundary: epoch 0 hash 8f8602837f7c6f8b8867dd1cbc1842cf51a27eaed2c70ef48325d00f8efb320f
[db-sync-node:Info:37] [2020-09-01 12:27:46.38 UTC] insertByronBlock: slot 5000, block 3970, hash be7153b5c4c4dad4a489f70c8931a5f88bafff3ef88eba6ecad98a2f3485b3b3
[db-sync-node:Info:37] [2020-09-01 12:28:26.24 UTC] insertByronBlock: slot 10000, block 8970, hash 5c4bea2bbd58655dda8c02b21e0fc941e1191a04b5f0279842d8b93154a1919a
[db-sync-node:Info:37] [2020-09-01 12:29:05.73 UTC] insertByronBlock: slot 15000, block 13970, hash 6feee0376f082d2366ed792ba528ae43357ac8473c2bce4dad766e55c305cadb
[db-sync-node:Info:37] [2020-09-01 12:29:49.18 UTC] insertByronBlock: slot 20000, block 18970, hash 4cb95dd0cc7159ce82e3639d3f3ef2f2c4ebc8ca3a1510f67080fdf3662a9276

DBの確認

cardano-db-sync の書き込み先のDBにログインして確認します。

# \dt
                    List of relations
 Schema |         Name         | Type  |      Owner      
--------+----------------------+-------+-----------------
 public | block                | table | cardano_db_sync
 public | delegation           | table | cardano_db_sync
 public | epoch                | table | cardano_db_sync
 public | meta                 | table | cardano_db_sync
 public | param_update         | table | cardano_db_sync
 public | pool_hash            | table | cardano_db_sync
 public | pool_meta_data       | table | cardano_db_sync
 public | pool_owner           | table | cardano_db_sync
 public | pool_relay           | table | cardano_db_sync
 public | pool_retire          | table | cardano_db_sync
 public | pool_update          | table | cardano_db_sync
 public | reserve              | table | cardano_db_sync
 public | reward               | table | cardano_db_sync
 public | schema_version       | table | cardano_db_sync
 public | slot_leader          | table | cardano_db_sync
 public | stake                | table | cardano_db_sync
 public | stake_address        | table | cardano_db_sync
 public | stake_deregistration | table | cardano_db_sync
 public | stake_registration   | table | cardano_db_sync
 public | treasury             | table | cardano_db_sync
 public | tx                   | table | cardano_db_sync
 public | tx_in                | table | cardano_db_sync
 public | tx_metadata          | table | cardano_db_sync
 public | tx_out               | table | cardano_db_sync
 public | withdrawal           | table | cardano_db_sync
(25 rows)

# select count(*) from block;
 count  
--------
 140001
(1 row)

# select * from block order by id desc limit 5;
   id   |                                hash                                | epoch_no | slot_no | block_no | previous |                            merkel_root                             | slot_leader | size |        time         | tx_count | epoch_slot_no | vrf_key | op_cert | proto_version 
--------+--------------------------------------------------------------------+----------+---------+----------+----------+--------------------------------------------------------------------+-------------+------+---------------------+----------+---------------+---------+---------+---------------
 144001 | \x08c9a0425daf450709874349107dd87a6c846fa6f6adf560e82517a97ebd4843 |        6 |  145035 |   143993 |   144000 | \x0e5751c026e543b2e8ab2eb06099daa1d1e5df47778f7787faab45cdf12fe3a8 |           3 |  633 | 2019-08-27 10:05:16 |        0 |         15435 |         |         | 
 144000 | \x70c3e128818f19f92234c74fee1b86a64e06c0bf63e11a98d2cb5356a440ef4d |        6 |  145034 |   143992 |   143999 | \x0e5751c026e543b2e8ab2eb06099daa1d1e5df47778f7787faab45cdf12fe3a8 |           4 |  633 | 2019-08-27 10:04:56 |        0 |         15434 |         |         | 
 143999 | \x5d8340c8f9e24cff55c1553f2947c6dc04ec55f42999f866e36750a03508b153 |        6 |  145033 |   143991 |   143998 | \x0e5751c026e543b2e8ab2eb06099daa1d1e5df47778f7787faab45cdf12fe3a8 |           7 |  633 | 2019-08-27 10:04:36 |        0 |         15433 |         |         | 
 143998 | \xdc88ea8af4f80c0173b090a90af00823eea0290354971b76987a9fd78e4369ee |        6 |  145032 |   143990 |   143997 | \x0e5751c026e543b2e8ab2eb06099daa1d1e5df47778f7787faab45cdf12fe3a8 |           5 |  633 | 2019-08-27 10:04:16 |        0 |         15432 |         |         | 
 143997 | \x18f54e2e7bcb74c0dcff4e6eeaecb35cbc2910a5ee2b125efdbaa1d6ed01ac69 |        6 |  145031 |   143989 |   143996 | \x0e5751c026e543b2e8ab2eb06099daa1d1e5df47778f7787faab45cdf12fe3a8 |           7 |  633 | 2019-08-27 10:03:56 |        0 |         15431 |         |         | 
(5 rows)

ちゃんとデータが同期されていることが確認できました。

以上になります。