概要
本节介绍如何在公共环境中和/或在众多 Cosmos SDK 公有区块链之一的主网上,安全地运行一个节点。
服务器设置
用户
创建服务器时,大多数情况下默认用户是root。该用户在服务器上具有较高权限。运行节点时,建议不要使用 root 用户运行节点。
- 创建一个新用户
- 我们希望允许该用户执行 sudo 任务
root 用户。
Go
- 安装应用推荐的 Go 版本。
防火墙
节点不应将所有端口都暴露给公网;这会让你很容易遭受 DDoS 攻击。其次,CometBFT 也建议,绝不要暴露运行节点所不需要的端口。 配置防火墙时,运行 Cosmos SDK 节点有少量端口可以开放,包括 CometBFT JSON-RPC、Prometheus、p2p、远程签名器,以及 Cosmos SDK 的 gRPC 和 REST。如果该节点并不对外提供用于提交交易或查询的端点,那么最多只需要三个端点。 大多数服务器即使不是全部,也都预装了 ufw。本教程将使用ufw。
- 重置 UFW,禁止所有入站连接并允许出站连接
- 确保 22 端口(SSH)保持开放。
- 开放 26656 端口(CometBFT p2p 端口)。如果节点修改了 p2p 端口,则这里必须使用修改后的端口。
- 开放 26660 端口(CometBFT 的 Prometheus 端口)。该端口同时也是应用的监控端口。
- 如果正在配置的节点需要暴露 CometBFT 的 JSON-RPC 以及 Cosmos SDK 的 gRPC 和 REST,请执行此步骤。(可选)
CometBFT JSON-RPC
Cosmos SDK gRPC
Cosmos SDK REST
- 最后,启用 ufw
签名
如果正在启动的节点是验证者,验证者可以通过多种方式对区块进行签名。文件
基于文件的签名是最简单、也是默认的方式。该方式通过存储初始化时生成的共识密钥来对区块签名。它的安全性完全取决于你的服务器配置,因为一旦服务器被攻破,密钥也会随之泄露。该密钥文件位于初始化时生成的config/priv_val_key.json。
还需要注意第二个文件;该文件位于数据目录 data/priv_val_state.json。此文件用于保护你的节点避免双重签名。它会记录共识密钥上一次签名的高度、轮次以及最新签名。如果节点崩溃后需要恢复,必须保留此文件,以确保不会使用该共识密钥再次签署一个此前已经签过的区块。
远程签名器
远程签名器是独立于运行中节点的第二台服务器,它使用共识密钥对区块签名。这意味着共识密钥并不存放在节点本机上。这样可以提升安全性,因为连接到远程签名器的全节点可以被替换,而不会漏签区块。 目前最常用的两个远程签名器是 Iqlusion 的 tmkms 和 Strangelove 的 horcrux。TMKMS
依赖项
- 更新服务器依赖并安装所需的额外软件。
- 安装 Rust:
- 安装 Libusb:
设置
安装 tmkms 有两种方式:从源码安装或使用cargo install。以下示例将介绍如何下载或从源码构建,并使用 softsign。Softsign 表示软件签名;如果你愿意,也可以使用 yubihsm 作为签名密钥。
- 构建:
要让 tmkms 配合 yubikey 使用,请在安装二进制文件时加上
--features=yubihsm。- 将验证者密钥从全节点迁移到新的 tmkms 实例。
- 将验证者密钥导入 tmkms。
priv_validator_key.json。由于该密钥已经导入 tmkms(见上文),节点上不再需要保留它。可以将该密钥安全地离线保存。
- 修改
tmkms.toml。
123.456.12.345、端口 26659 和 chain_id test-chain-waSDSe。这些项目都必须根据 tmkms 的实际使用场景和网络进行修改。
- 设置 tmkms 实例的地址。
- 启动这两个进程。
Synopsis
This section describes how to securely run a node in a public setting and/or on a mainnet on one of the many Cosmos SDK public blockchains.
Server Setup
User
When creating a server most times it is created as userroot. This user has heightened privileges on the server. When operating a node, it is recommended to not run your node as the root user.
- Create a new user
- We want to allow this user to perform sudo tasks
root user can be used.
Go
- Install the Go version recommended by the application.
Firewall
Nodes should not have all ports open to the public; this is a simple way to get DDoS’d. Secondly, it is recommended by CometBFT to never expose ports that are not required to operate a node. When setting up a firewall, there are a few ports that can be open when operating a Cosmos SDK node. These include the CometBFT JSON-RPC, Prometheus, p2p, remote signer, and Cosmos SDK gRPC and REST. If the node is being operated as a node that does not offer endpoints to be used for submission or querying, then a maximum of three endpoints are needed. Most, if not all servers come equipped with ufw. Ufw will be used in this tutorial.- Reset UFW to disallow all incoming connections and allow outgoing
- Let’s make sure that port 22 (SSH) stays open.
- Allow Port 26656 (cometbft p2p port). If the node has a modified p2p port then that port must be used here.
- Allow port 26660 (CometBFT Prometheus). This acts as the application’s monitoring port as well.
- If the node which is being set up would like to expose CometBFT’s JSON-RPC and Cosmos SDK gRPC and REST, then follow this step. (Optional)
CometBFT JSON-RPC
Cosmos SDK gRPC
Cosmos SDK REST
- Lastly, enable ufw
Signing
If the node that is being started is a validator there are multiple ways a validator could sign blocks.File
File-based signing is the simplest and default approach. This approach works by storing the consensus key generated on initialization to sign blocks. This approach is only as safe as your server setup, as if the server is compromised, so is your key. This key is located in theconfig/priv_val_key.json directory generated on initialization.
A second file exists that users must be aware of; the file is located in the data directory data/priv_val_state.json. This file protects your node from double signing. It keeps track of the consensus key’s last sign height, round, and latest signature. If the node crashes and needs to be recovered, this file must be kept in order to ensure that the consensus key will not be used for signing a block that was previously signed.
Remote Signer
A remote signer is a secondary server that is separate from the running node that signs blocks with the consensus key. This means that the consensus key does not live on the node itself. This increases security because your full node which is connected to the remote signer can be swapped without missing blocks. The two most used remote signers are tmkms from Iqlusion and horcrux from Strangelove.TMKMS
Dependencies
- Update server dependencies and install extras needed.
- Install Rust:
- Install Libusb:
Setup
There are two ways to install tmkms, from source orcargo install. In the examples we will cover downloading or building from source and using softsign. Softsign stands for software signing, but you could use a yubihsm as your signing key if you wish.
- Build:
To use tmkms with a yubikey install the binary with
--features=yubihsm.- Migrate the validator key from the full node to the new tmkms instance.
- Import the validator key into tmkms.
priv_validator_key.json from the validator node and the tmkms node. Since the key has been imported into tmkms (above) it is no longer necessary on the nodes. The key can be safely stored offline.
- Modify the
tmkms.toml.
123.456.12.345 with a port of 26659 and a chain_id of test-chain-waSDSe. These are items that must be modified for the use case of tmkms and the network.
- Set the address of the tmkms instance.
- Start the two processes.