islishude / blog

my web notes
https://islishude.github.io/blog/
101 stars 15 forks source link

[DEPRECATED]Cardano SL 编译 #225

Closed islishude closed 4 years ago

islishude commented 4 years ago

请在生产环境使用 cardano-node 以及 cardano-reset(cardano-graphql)

安装 Nix

curl https://nixos.org/nix/install | sh
source $HOME/.nix-profile/etc/profile.d/nix.sh

默认nix以及后续的三方包会放在 /nix 目录下,如果根目录磁盘不够,尤其是三方包会逐渐占用磁盘,可以设置环境变量指定目录,如下所示的环境变量,其中数据量最大的是 NIX_STORE_DIR

NIX_STORE_DIR
Overrides the location of the Nix store (default prefix/store).

NIX_DATA_DIR
Overrides the location of the Nix static data directory (default prefix/share).

NIX_LOG_DIR
Overrides the location of the Nix log directory (default prefix/var/log/nix).

NIX_STATE_DIR
Overrides the location of the Nix state directory (default prefix/var/nix).

NIX_CONF_DIR
Overrides the location of the Nix configuration directory (default prefix/etc/nix).

配置缓存

sudo mkdir -p /etc/nix
cat <<EOF | sudo tee /etc/nix/nix.conf
substituters = https://hydra.iohk.io https://cache.nixos.org/
trusted-substituters =
trusted-public-keys = hydra.iohk.io:f/Ea+s+dFdN+3Y/G+FDgSq+a5NEWhJGzdjvKNGv0/EQ= cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY=
EOF

下载代码

git clone https://github.com/input-output-hk/cardano-sl.git
cd cardano-sl

切换到 master 分支或者某个 tag,默认情况下,master 分支为最新的 tag。

不过最新的版本不是master分支代码,最好使用 tag,这里使用 3.0.2

git checkout 3.0.2

编辑配置

在源码目录下新建文件 custom-wallet-config.nix ,然后根据需要填写下面内容。

默认情况下会根据下面的配置文件生成初始化脚本。

{
  ## Wallet API server.
  walletListen = "0.0.0.0:8090";

  ## Wallet Doc API server.
  walletDocListen = "0.0.0.0:8091";

  ## Runtime metrics server.
  ekgListen = "0.0.0.0:8000";

  ## 如果不需要认证或者为了方便可以打开下面配置
  # disableClientAuth = true;

  # 设置区块存储目录
  stateDir = "/data/cardano";

  ## Used to connect to a custom set of nodes on the network. When
  ## unspecified an appropriate default topology is generated.
  # topologyFile = ./topology.yaml;

  ## See https://downloads.haskell.org/~ghc/8.0.2/docs/html/users_guide/runtime_control.html#running-a-compiled-program
  ghcRuntimeArgs = "-N2 -qg -A1m -I0 -T";

  ## Primarily used for troubleshooting.
  #additionalNodeArgs = "";

  ## TLS configuration
  tlsConfig = {
    organization     = "Cardano Wallet";

    caCommonName     = "Cardano SSL CA";
    caEexpiryDays    = 3650;

    serverCommonName = "Cardano Server";
    serverExpiryDays = 3650;
    # 这里另外添加节点的ip和dns名称
    serverAltDNS     = [
      "localhost"
      "127.0.0.1"
      "::1"
    ];

    serverAltDNSExtra = [
      "8.8.8.8"
    ];

    clientCommonName = "Cardano Client";
    clientExpiryDays = 3650;
  };
}

编译代码

nix-build -A connectScripts.mainnet.wallet -o connect-to-mainnet

如果需要编译测试网

nix-build -A connectScripts.staging.wallet -o connect-to-stage-test

等待编译成功即可,编译后的文件是软链接的文件,而且是一个 shell 脚本,可以打开查看详情。

如果直接运行的话,会创建 stateDir ,然后生成自签名的证书,软链接一个类似 curl 但加入了客户端 TLS 认证功能的文件,最后运行 cardano-node。

这里我们可以直接使用,也可以根据需要,把把重要的文件按照文件路径复制出来,然后自定义运行命令。

#!/nix/store/vlb7kcc1k035vpyrgsj9kk7380yh68wd-bash-4.4-p23/bin/bash

set -euo pipefail

if [[ "${1-}" == "--delete-state" ]]; then
    echo "Deleting /data/cardano ... "
    rm -Rf /data/cardano
    shift
fi
if [[ "${1-}" == "--runtime-args" ]]; then
    RUNTIME_ARGS="${2-}"
    shift 2
else
    RUNTIME_ARGS=""
fi

echo "Keeping state in /data/cardano"
mkdir -p /data/cardano/logs

echo "Launching a node connected to 'mainnet' ..."
export LC_ALL=en_GB.UTF-8
export LANG=en_GB.UTF-8

if [ ! -d /data/cardano/tls ]; then
    mkdir -p /data/cardano/tls/server && mkdir -p /data/cardano/tls/client
    /nix/store/qcb20qc9gfjrplva27s7fk000grfc6nd-cardano-sl-tools-3.2.0-exe-cardano-x509-certificates/bin/cardano-x509-certificates \
        --server-out-dir /data/cardano/tls/server \
        --clients-out-dir /data/cardano/tls/client \
        --configuration-file /nix/store/ynplcpmmfni2aw5i5j666v7jnwfwi09d-tls-config-mainnet.yaml \
        --configuration-key mainnet_full
fi
ln -sf /nix/store/kij2qsgdh76f0dh6kh02ffr3v4kdn6yz-curl-wallet-mainnet /data/cardano/curl

exec /nix/store/5hadsamqf02qfnbj2d7krv20zjns05f7-cardano-wallet-3.2.0-exe-cardano-node/bin/cardano-node \
    --configuration-file /nix/store/0r8bly8qny59b9isshg9q7jr89hdw55w-cardano-sl-config/lib/configuration.yaml \
    --configuration-key mainnet_full \
    --tlscert /data/cardano/tls/server/server.crt \
    --tlskey /data/cardano/tls/server/server.key \
    --tlsca /data/cardano/tls/server/ca.crt \
    --log-config /nix/store/0r8bly8qny59b9isshg9q7jr89hdw55w-cardano-sl-config/log-configs/connect-to-cluster.yaml \
    --topology "/nix/store/kiwxslk8q90j8rrjj4vqnnc9np5a9bhy-topology-mainnet" \
    --logs-prefix "/data/cardano/logs" \
    --db-path "/data/cardano/db" \
    --wallet-db-path '/data/cardano/wallet-db' \
    --keyfile /data/cardano/secret.key \
    --wallet-address 0.0.0.0:8090 \
    --wallet-doc-address 0.0.0.0:8091 \
    --ekg-server 0.0.0.0:8000 --metrics \
    +RTS -N2 -qg -A1m -I0 -T -RTS       \
    $RUNTIME_ARGS

这里我们使用自定义配置

# 创建数据目录,日志、配置等文件夹
mkdir -m 0775 -p /data/cardano/logs /data/cardano/lib
sudo chown -R $USER /data/cardano

# 复制节点程序到 $PATH
cp /nix/store/5hadsamqf02qfnbj2d7krv20zjns05f7-cardano-wallet-3.2.0-exe-cardano-node/bin/cardano-node /usr/local/bin/cardano-node
# 复制源码中配置文件到 /data/cardano/libs
cp /path/to/cardano-sl/lib/{configuration.yaml,mainnet-genesis.json} /data/cardano/lib/
# 复制 topology 配置文件到 /data/cardano/libs
cp /nix/store/kiwxslk8q90j8rrjj4vqnnc9np5a9bhy-topology-mainnet /data/cardano/lib/topology.yaml

topology 文件内容,基本不变

wallet:
  relays:
    - - host: relays.cardano-mainnet.iohk.io
  valency: 1
  fallbacks: 7

如果需要配置log,参考下面内容

rotation:
    logLimit: 104857600 # 100MB
    keepFiles: 100

loggerTree:
  severity: Info+

  handlers:
    - { name: "Public"
      , filepath: "pub/node.log"
      , logsafety: PublicLogLevel
      , severity: Debug+
      , backend: FileTextBE }
    - { name: "Secret"
      , filepath: "node"
      , logsafety: SecretLogLevel
      , severity: Info+
      , backend: FileTextBE }

运行节点

如果使用初始化脚本运行节点钱包进程后,会在stateDir 创建数据,其中的客户端证书在 tls 目录下。

为了方便,我们去掉客户端认证和TLS选项,注意:生产环境请打开这些配置。

cardano-node \
  --configuration-file /data/cardano/lib/config.yaml \
  --topology /data/cardano/lib/topology.yaml \
  --configuration-key mainnet_full \
  --no-tls --no-client-auth \
  --db-path "/data/cardano/db" \
  --wallet-db-path '/data/cardano/wallet-db' \
  --keyfile /data/cardano/secret.key \
  --wallet-address 0.0.0.0:8090
$ curl -sS http://localhost:8090/api/v1/node-info | jq .
$ # 如果配置了客户端认证需要另外加上下面参数
$ # --cacert tls/client/ca.crt 
$ # --key tls/client/client.key --cert tls/client/client.crt
{
  "data": {
    "syncProgress": {
      "quantity": 1,
      "unit": "percent"
    },
    "blockchainHeight": {
      "quantity": 3868956,
      "unit": "blocks"
    },
    "localBlockchainHeight": {
      "quantity": 48958,
      "unit": "blocks"
    },
    "localTimeInformation": {
      "status": "available",
      "localTimeDifference": {
        "quantity": 4553,
        "unit": "microseconds"
      }
    },
    "subscriptionStatus": {
      "54.250.226.7:3000:0": "subscribed"
    }
  },
  "status": "success",
  "meta": {
    "pagination": {
      "totalPages": 1,
      "page": 1,
      "perPage": 1,
      "totalEntries": 1
    }
  }
}

使用 stack 进行构建

安装依赖,以 ubuntu18.04 为例

sudo apt-get update
sudo apt-get install libssl-dev liblzma-dev libtinfo-dev libsystemd-dev librocksdb-dev

下载 stack 工具链

wget -O stack.sh https://get.haskellstack.org

然后更改 STACK_VERSION 为 1.9.3,接着执行脚本继续安装 stack

sh stack.sh

初始化 haskell 编译环境

stack setup
stack install cpphs

下载源码,并切换到 3.2.0 最新版本

git clone https://github.com/input-output-hk/cardano-sl.git
cd cardano-sl
git checkout 3.2.0

执行编译脚本

./scripts/build/cardano-sl.sh $PARAM

PARAM 可选下面名称,多个编译项,按照逗号分割

networking binary util crypto core db chain infra lib node client generator auxx tools explorer wallet

等待下载依赖、编译完成后,所有可执行文件都在下面目录下

./.stack-work/install/x86_64-linux/lts-12.26/8.4.4/bin

新建数据存储目录

mkdir -p /data/cardano/lib

复制源码中配置文件到数据目录:

cp lib/configuration.yaml /data/cardano/lib
cp script-runner/topology-mainnet.yaml /data/cardano/lib

运行 explorer

cardano-explorer  --configuration-file /data/cardano/lib/configuration.yaml   --topology /data/cardano/lib/topology-mainnet.yaml   --configuration-key mainnet_full --db-path "/data/cardano/db"

或者运行带钱包的节点

cardano-node --configuration-file /data/cardano/lib/configuration.yaml   --topology /data/cardano/lib/topology-mainnet.yaml   --configuration-key mainnet_full   --no-tls --no-client-auth   --db-path "/data/cardano/db"   --wallet-db-path '/data/cardano/wallet-db'   --keyfile /data/cardano/secret.key   --wallet-address 0.0.0.0:8090