> ## Documentation Index
> Fetch the complete documentation index at: https://injectivelabs-mintlify-25e241d4.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# 加入网络

在 Mainnet、Testnet 或本地开发网络上设置并同步 Injective 节点。

<Tabs>
  <Tab title="Mainnet">
    Injective Mainnet 的 chain-id 为 `injective-1`。

    <Columns cols={3}>
      <Card title="同步选项" href="#同步选项" arrow="true" cta="跳转到该部分" icon="panel-left-close">
        快照恢复、state sync 和区块同步
      </Card>

      <Card title="节点软件" href="#节点软件" arrow="true" cta="跳转到该部分" icon="panel-left-close">
        安装 injectived、配置并运行
      </Card>

      <Card title="硬件" href="#硬件" arrow="true" cta="跳转到该部分" icon="panel-left-close">
        最低和推荐规格
      </Card>
    </Columns>

    ***

    ### 同步选项

    选择如何为你的节点引导链数据。对大多数运营者来说，快照恢复是让节点最快运行起来的方式。

    **选项 1. 快照恢复（推荐）**

    下载预构建的快照并将其解压到你的数据目录。这是同步新节点最快、最可靠的方式。

    Injective 提供官方剪枝快照（goleveldb，约 2-3 GB），社区也维护其他选项。有关下载链接、使用说明和社区提供商，请参阅 [Chain Portal 上的快照页面](https://portal.injective.network/snapshots)。

    <Callout icon="warning" color="#FFA500" iconType="regular">
      **验证者：** 在快照恢复期间，你必须备份并还原 `priv_validator_state.json`。完整流程请参阅[验证者故障排查指南](/cn/infra/coordinated-upgrades#从快照恢复)。
    </Callout>

    ```bash theme={null}
    # Example: download and extract
    sudo systemctl stop injectived
    rm -rf ~/.injectived/data
    wget -O snapshot.lz4 <SNAPSHOT_URL>
    lz4 -d snapshot.lz4 | tar xf - -C ~/.injectived/
    sudo systemctl start injectived
    ```

    **选项 2. State Sync（CometBFT P2P）**

    State sync 通过 CometBFT P2P 层从对等节点下载状态机快照来引导节点，而不是重放所有历史区块。它需要两个可信的 RPC 服务器。有关可用端点，请参阅 [Chain Portal 上的 RPC 提供商页面](https://portal.injective.network/rpcs)。请在 `config.toml` 的 `[statesync]` 下进行配置：

    ```toml theme={null}
    [statesync]
    enable = true
    rpc_servers = "<rpc_server_1>,<rpc_server_2>"
    trust_height = <recent_height>
    trust_hash = "<block_hash_at_trust_height>"
    trust_period = "168h0m0s"
    ```

    获取信任高度和哈希：

    ```bash theme={null}
    # Get a recent height and its hash from a trusted RPC
    LATEST=$(curl -s https://sentry.tm.injective.network/block | jq -r '.result.block.header.height')
    TRUST_HEIGHT=$((LATEST - 2000))
    TRUST_HASH=$(curl -s "https://sentry.tm.injective.network/block?height=$TRUST_HEIGHT" | jq -r '.result.block_id.hash')
    echo "trust_height = $TRUST_HEIGHT"
    echo "trust_hash = \"$TRUST_HASH\""
    ```

    <Warning>
      **State sync 不会恢复 `wasm/` 目录。** CometBFT state sync 会恢复 IAVL 状态树，但不会恢复存储在 `~/.injectived/data/wasm/` 中的 CosmWasm 已编译合约字节码。缺少这些文件时，节点会在同步完成后立即出现 **AppHash 不匹配**。

      如果你遇到此问题，可以：

      1. 改用**快照恢复**（选项 1）。Injective 的官方快照包含 `wasm/` 目录
      2. 从可信来源获取 `wasm/` 目录，并在启动节点之前将其放置在 `~/.injectived/data/wasm/`

      **PebbleDB 用户：** Injective 的官方快照仅使用 goleveldb。官方不维护 PebbleDB 快照。如果你运行 PebbleDB 且无法使用快照恢复，则在使用 state sync 时必须单独获取 `wasm/` 目录。
    </Warning>

    **选项 3. 区块同步（完整重放）**

    这是默认行为。节点从对等节点下载并重放每一个区块。这是最慢的方法，但不需要外部快照。除非从最近的快照高度开始同步，否则对主网并不实用。

    **同步方法对比：**

    | 方法             | 速度     | 是否包含 wasm？                           | 说明                                  |
    | -------------- | ------ | ------------------------------------ | ----------------------------------- |
    | **快照恢复**       | 快（数分钟） | 是（官方快照）                              | 推荐给大多数运营者                           |
    | **State Sync** | 快（数分钟） | **否** —— 若不手动恢复 wasm 会导致 AppHash 不匹配 | 在 `config.toml` 的 `[statesync]` 中配置 |
    | **区块同步**       | 慢（数天）  | 不适用（通过执行构建）                          | 默认方式，从创世开始同步主网不实用                   |

    #### 对等节点发现

    你的节点需要对等节点才能与网络同步。请将种子节点或持久对等节点添加到 `~/.injectived/config/config.toml` 的 `[p2p]` 部分。社区提供商维护着最新的种子和对等节点列表：

    * [Polkachu Injective 种子节点](https://polkachu.com/seeds/injective)
    * [Decentrio Injective 基础设施](https://decentrio.ventures/infrastructure/injective)（种子节点、对等节点、地址簿）

    你也可以下载预构建的地址簿以加快对等节点发现：

    ```bash theme={null}
    # Decentrio address book
    wget -O ~/.injectived/config/addrbook.json \
      https://backend.decentrio.ventures/snapshots/injective/addrbook.json
    ```

    ***

    ### 节点软件

    #### 安装 `injectived`

    每个 [Injective core 发布版本](https://github.com/InjectiveFoundation/injective-core/releases/latest)都在单个 zip 中包含 `injectived`、[`peggo`](/cn/infra/validator-mainnet/peggo)（Peggy 跨链桥编排器）和 `libwasmvm`。验证者必须在其节点旁运行 `peggo`。非验证者节点运营者只需要 `injectived`。

    ```bash theme={null}
    # Download the latest release
    wget https://github.com/InjectiveFoundation/injective-core/releases/latest/download/linux-amd64.zip
    unzip linux-amd64.zip
    sudo mv injectived /usr/bin
    sudo mv libwasmvm.x86_64.so /usr/lib

    # Validators also need peggo
    sudo mv peggo /usr/bin
    ```

    #### 初始化新的 Injective 节点

    在运行 Injective 节点之前，请初始化链和 genesis 文件：

    ```bash theme={null}
    # <moniker> 参数是你节点的自定义用户名。它应该是人类可读的。
    export MONIKER=<moniker>
    injectived init $MONIKER --chain-id injective-1
    ```

    运行 `init` 命令将在 `~/.injectived` 创建 `injectived` 默认配置文件。

    #### 准备配置以加入 Mainnet

    下载主网 genesis 文件。Injective 将其与官方快照一起提供：

    ```bash theme={null}
    # Download the mainnet genesis file
    wget -O ~/.injectived/config/genesis.json \
      http://injective-mainnet-snapshots.s3-website.gra.io.cloud.ovh.net/genesis.json
    ```

    验证校验和：

    ```bash theme={null}
    sha256sum ~/.injectived/config/genesis.json
    # Expected: 573b89727e42b41d43156cd6605c0c8ad4a1ce16d9aad1e1604b02864015d528
    ```

    为对等节点发现配置种子节点（参见同步选项部分中的[对等节点发现](#对等节点发现)），并在 `~/.injectived/config/config.toml` 中将 `timeout_commit` 设置为 `300ms`。

    #### 使用 `app.toml` 和 `config.toml` 配置节点

    两个配置文件会自动生成在 `~/.injectived/config` 中：

    * `config.toml`：配置共识层（CometBFT）。[在共识层文档中了解更多](https://docs.cometbft.com/v0.38/core/configuration)。
    * `app.toml`：配置节点的应用和执行层，包括状态剪枝策略、遥测、gRPC 和 REST 服务器配置、状态同步等。

    #### 为 `injectived` 配置 `systemd` 服务（可选）

    编辑 `/etc/systemd/system/injectived.service` 的配置：

    ```bash theme={null}
    [Unit]
      Description=injectived

    [Service]
      WorkingDirectory=/usr/bin
      ExecStart=/bin/bash -c '/usr/bin/injectived --log-level=error start'
      Type=simple
      Restart=always
      RestartSec=5
      User=root

    [Install]
      WantedBy=multi-user.target
    ```

    启动和重启 systemd 服务：

    ```bash theme={null}
    sudo systemctl daemon-reload
    sudo systemctl restart injectived
    sudo systemctl status injectived

    # 启用系统启动时自动启动
    sudo systemctl enable injectived

    # 查看日志
    journalctl -u injectived -f
    ```

    在将快照数据加载到正确目录之前应停止服务，之后再启动。

    ```bash theme={null}
    # 停止节点
    sudo systemctl stop injectived

    # 启动节点
    sudo systemctl start injectived
    ```

    ***

    ### 硬件

    节点运营者应部署裸机服务器以实现最佳性能。
    验证者节点必须满足推荐的硬件规格，特别是 CPU 要求，
    以确保高正常运行时间。

    |      *最低要求*     |      *推荐配置*     |
    | :-------------: | :-------------: |
    |   RAM 内存 128GB  |   RAM 内存 128GB  |
    |     CPU 12 核    |     CPU 16 核    |
    | CPU 基础频率 3.7GHz | CPU 基础频率 4.2GHz |
    |   存储 2TB NVMe   |   存储 2TB NVMe   |
    |    网络 1Gbps+    |    网络 1Gbps+    |

    ***

    **支持**

    如有任何其他问题，你可以随时通过 [Discord](https://discord.gg/injective)、[Telegram](https://t.me/joininjective) 或[电子邮件](mailto:contact@injectivelabs.org)与 Injective 团队联系
  </Tab>

  <Tab title="Testnet">
    #### 硬件规格

    节点运营者应部署裸机服务器以实现最佳性能。此外，validator 节点必须满足推荐的硬件规格，特别是 CPU 要求，以确保高正常运行时间。

    |      *最低要求*     |      *推荐配置*     |
    | :-------------: | :-------------: |
    |   RAM 内存 128GB  |   RAM 内存 128GB  |
    |     CPU 12 核    |     CPU 16 核    |
    | CPU 基础频率 3.7GHz | CPU 基础频率 4.2GHz |
    |   存储 2TB NVMe   |   存储 2TB NVMe   |
    |    网络 1Gbps+    |    网络 1Gbps+    |

    #### 安装 `injectived` 和 `peggo`

    请参阅 [Injective releases repo](https://github.com/InjectiveLabs/testnet/releases) 获取最新版本。非 validator 节点运营者不需要安装 `peggo`。

    ```bash theme={null}
    wget https://github.com/InjectiveLabs/testnet/releases/latest/download/linux-amd64.zip
    unzip linux-amd64.zip
    sudo mv peggo /usr/bin
    sudo mv injectived /usr/bin
    sudo mv libwasmvm.x86_64.so /usr/lib 
    ```

    #### 初始化新的 Injective 链节点

    在运行 Injective 节点之前，我们需要初始化链以及节点的 genesis 文件：

    ```bash theme={null}
    # <moniker> 参数是你节点的自定义用户名，它应该是人类可读的。
    export MONIKER=<moniker>
    # Injective Testnet 的 chain-id 为 "injective-888"
    injectived init $MONIKER --chain-id injective-888
    ```

    运行 `init` 命令将在 `~/.injectived` 创建 `injectived` 默认配置文件。

    #### 准备配置以加入 Testnet

    你现在应该使用 Testnet 的 genesis 文件和应用程序配置文件更新默认配置，并使用种子节点配置你的持久对等节点。

    ```bash theme={null}
    git clone https://github.com/InjectiveLabs/testnet.git

    # 将 genesis 文件复制到配置目录
    aws s3 cp --no-sign-request s3://injective-snapshots/testnet/genesis.json .
    mv genesis.json ~/.injectived/config/

    # 将配置文件复制到配置目录
    cp testnet/corfu/70001/app.toml  ~/.injectived/config/app.toml
    cp testnet/corfu/70001/config.toml ~/.injectived/config/config.toml
    ```

    你还可以验证 genesis 校验和 - a4abe4e1f5511d4c2f821c1c05ecb44b493eec185c0eec13b1dcd03d36e1a779

    ```bash theme={null}
    sha256sum ~/.injectived/config/genesis.json
    ```

    #### 为 `injectived` 配置 `systemd` 服务

    编辑 `/etc/systemd/system/injectived.service` 的配置：

    ```bash theme={null}
    [Unit]
      Description=injectived

    [Service]
      WorkingDirectory=/usr/bin
      ExecStart=/bin/bash -c '/usr/bin/injectived --log-level=error start'
      Type=simple
      Restart=always
      RestartSec=5
      User=root

    [Install]
      WantedBy=multi-user.target
    ```

    启动和重启 systemd 服务

    ```bash theme={null}
    sudo systemctl daemon-reload
    sudo systemctl restart injectived
    sudo systemctl status injectived

    # 启用系统启动时自动启动
    sudo systemctl enable injectived

    # 查看日志
    journalctl -u injectived -f
    ```

    #### 与网络同步

    请参阅 [Polkachu Injective Testnet Node Snapshot](https://polkachu.com/testnets/injective/snapshots) 下载快照并与网络同步。

    **支持**

    如有任何其他问题，你可以随时通过 [Discord](https://discord.gg/injective)、[Telegram](https://t.me/joininjective) 或[电子邮件](mailto:contact@injectivelabs.org)与 Injective 团队联系。
  </Tab>

  <Tab title="本地网络">
    要轻松设置本地节点，请从 [injective-core](https://github.com/InjectiveFoundation/injective-core) 仓库下载并运行 `setup.sh` 脚本。请使用与已安装的 `injectived` 版本匹配的脚本版本。

    ```bash theme={null}
    # 将版本标签替换为与你的 injectived 二进制文件匹配的版本
    # 例如，使用 v1.18.3 的脚本：
    wget https://raw.githubusercontent.com/InjectiveFoundation/injective-core/v1.18.3/setup.sh
    chmod +x ./setup.sh # 使脚本可执行
    ./setup.sh
    ```

    <Callout icon="warning" color="#FFA500" iconType="regular">
      旧版 [injective-chain-releases](https://github.com/InjectiveLabs/injective-chain-releases) 仓库中的 `setup.sh` 脚本已过时，仅兼容 `injectived` v1.14.1 及更早版本。请始终使用 [InjectiveFoundation/injective-core](https://github.com/InjectiveFoundation/injective-core) 仓库中与你的链版本对应的脚本。
    </Callout>

    通过运行以下命令启动节点：

    ```bash theme={null}
    injectived start # 运行此命令后应该开始产生区块
    ```

    有关脚本执行内容的进一步说明以及对设置过程的更精细控制，请继续阅读下文。

    #### 初始化链

    在运行 Injective 节点之前，我们需要初始化链以及节点的 genesis 文件：

    ```bash theme={null}
    # <moniker> 参数是你节点的自定义用户名。它应该是人类可读的。
    injectived init <moniker> --chain-id=injective-1
    ```

    上述命令创建节点运行所需的所有配置文件以及定义网络初始状态的默认 genesis 文件。所有这些配置文件默认位于 `~/.injectived` 中，但你可以通过传递 `--home` 标志来覆盖此文件夹的位置。请注意，如果你选择使用 `~/.injectived` 以外的目录，则每次运行 `injectived` 命令时都必须使用 `--home` 标志指定位置。如果你已有 genesis 文件，可以使用 `--overwrite` 或 `-o` 标志覆盖它。

    `~/.injectived` 文件夹具有以下结构：

    ```bash theme={null}
    .                                   # ~/.injectived
      |- data                           # 包含节点使用的数据库。
      |- config/
          |- app.toml                   # 应用程序相关配置文件。
          |- config.toml                # Tendermint 相关配置文件。
          |- genesis.json               # genesis 文件。
          |- node_key.json              # 用于 p2p 协议中节点身份验证的私钥。
          |- priv_validator_key.json    # 用于共识协议中作为 validator 的私钥。
    ```

    #### 修改 `genesis.json` 文件

    此时，需要修改 `genesis.json` 文件：

    * 将 staking `bond_denom`、crisis `denom`、gov `denom` 和 mint `denom` 的值更改为 `"inj"`，因为这是 Injective 的原生 token。

    可以通过运行以下命令轻松完成：

    ```bash theme={null}
    cat $HOME/.injectived/config/genesis.json | jq '.app_state["staking"]["params"]["bond_denom"]="inj"' > $HOME/.injectived/config/tmp_genesis.json && mv $HOME/.injectived/config/tmp_genesis.json $HOME/.injectived/config/genesis.json
    cat $HOME/.injectived/config/genesis.json | jq '.app_state["crisis"]["constant_fee"]["denom"]="inj"' > $HOME/.injectived/config/tmp_genesis.json && mv $HOME/.injectived/config/tmp_genesis.json $HOME/.injectived/config/genesis.json
    cat $HOME/.injectived/config/genesis.json | jq '.app_state["gov"]["deposit_params"]["min_deposit"][0]["denom"]="inj"' > $HOME/.injectived/config/tmp_genesis.json && mv $HOME/.injectived/config/tmp_genesis.json $HOME/.injectived/config/genesis.json
    cat $HOME/.injectived/config/genesis.json | jq '.app_state["mint"]["params"]["mint_denom"]="inj"' > $HOME/.injectived/config/tmp_genesis.json && mv $HOME/.injectived/config/tmp_genesis.json $HOME/.injectived/config/genesis.json
    ```

    <Callout icon="info" color="#07C1FF" iconType="regular">
      上述命令仅在使用默认 `.injectived` 目录时有效。对于特定目录，请修改上述命令或手动编辑 `genesis.json` 文件以反映更改。
    </Callout>

    #### 为 Validator 账户创建密钥

    在启动链之前，你需要用至少一个账户填充状态。为此，首先在 `test` keyring backend 下创建一个名为 `my_validator` 的新账户（可以选择其他名称和其他 backend）：

    ```bash theme={null}
    injectived keys add my_validator --keyring-backend=test

    # 将生成的地址放入变量中以供后续使用。
    MY_VALIDATOR_ADDRESS=$(injectived keys show my_validator -a --keyring-backend=test)
    ```

    现在你已创建本地账户，继续在链的 genesis 文件中授予它一些 `inj` token。这样做还将确保你的链从创世开始就知道此账户的存在：

    ```bash theme={null}
    injectived add-genesis-account $MY_VALIDATOR_ADDRESS 100000000000000000000000000inj --chain-id=injective-1
    ```

    `$MY_VALIDATOR_ADDRESS` 是保存 keyring 中 `my_validator` 密钥地址的变量。Injective 中的 token 具有 `{amount}{denom}` 格式：`amount` 是 18 位精度的十进制数，`denom` 是带有其面额键的唯一 token 标识符（例如 `inj`）。这里我们授予 `inj` token，因为 `inj` 是 `injectived` 中用于 staking 的 token 标识符。

    #### 将 Validator 添加到链

    现在你的账户有了一些 token，你需要向链添加一个 validator。Validator 是参与共识过程以向链添加新区块的特殊全节点。任何账户都可以声明其成为 validator 运营者的意图，但只有获得足够委托的账户才能进入活跃集。在本指南中，你将把本地节点（通过上面的 `init` 命令创建）添加为链的 validator。可以在链首次启动之前通过包含在 genesis 文件中的特殊交易（称为 `gentx`）声明 validator：

    ```bash theme={null}
    # 创建 gentx。
    injectived genesis gentx my_validator 1000000000000000000000inj --chain-id=injective-1 --keyring-backend=test

    # 将 gentx 添加到 genesis 文件。
    injectived genesis collect-gentxs
    ```

    `gentx` 做三件事：

    1. 将你创建的 `validator` 账户注册为 validator 运营者账户（即控制 validator 的账户）。
    2. 自委托提供的 staking token `amount`。
    3. 将运营者账户与用于签署区块的 Tendermint 节点公钥链接。如果未提供 `--pubkey` 标志，则默认为通过上面的 `injectived init` 命令创建的本地节点公钥。

    有关 `gentx` 的更多信息，请使用以下命令：

    ```bash theme={null}
    injectived genesis gentx --help
    ```

    #### 使用 `app.toml` 和 `config.toml` 配置节点

    两个配置文件会自动生成在 `~/.injectived/config` 中：

    * `config.toml`：用于配置 Tendermint（在 [Tendermint 文档](https://docs.tendermint.com/v0.34/tendermint-core/configuration.html)中了解更多），以及
    * `app.toml`：由 Cosmos SDK（Injective 基于此构建）生成，用于状态修剪策略、遥测、gRPC 和 REST 服务器配置、状态同步等配置。

    两个文件都有详细注释——请直接参考它们来调整你的节点。

    一个需要调整的示例配置是 `app.toml` 中的 `minimum-gas-prices` 字段，它定义了 validator 节点愿意接受的处理交易的最低 gas 价格。如果为空，请确保编辑该字段并设置某个值，例如 `10inj`，否则节点将在启动时停止。在本教程中，让我们将最低 gas 价格设置为 0：

    ```toml theme={null}
     # validator 愿意接受的处理交易的最低 gas 价格。
     # 交易的费用必须满足此配置中指定的任何面额的最低要求
     # （例如 0.25token1;0.0001token2）。
     minimum-gas-prices = "0inj"
    ```

    #### 运行本地网络

    现在一切都已设置好，你终于可以启动节点了：

    ```bash theme={null}
    injectived start # 运行此命令后应该开始产生区块
    ```

    此命令允许你运行单个节点，这足以通过节点与链交互，但你可能希望同时运行多个节点以查看它们之间如何达成共识。
  </Tab>
</Tabs>
