# 运行全节点（官方教程）

您可以运行 [FullNodes](/ji-chu/quan-jie-dian.md) 来验证状态并同步到 Aptos 区块链。 FullNodes 可以由任何人运行。 FullNodes 通过相互查询或直接查询验证者来复制区块链的完整状态。

本教程详细介绍了如何配置公共 FullNode 以连接到 Aptos 开发网。 Aptos Labs 提供的 FullNode 具有速率限制，可能会阻碍在测试网上的开发。 运行全节点将直接为您提供数据以避免此类速率限制。&#x20;

> **注意：**&#x60A8;的公共 FullNode 将通过 REST 端点连接到 devnet，可在您的计算机上的 localhost:8080 来访问该端点。

## **前提条件**[**​**](https://aptos.dev/tutorials/run-a-fullnode#prerequisites)

在开始学习本教程之前，我们建议您熟悉以下内容：

* [验证者节点概念](/ji-chu/yan-zheng-zhe-jie-dian.md)
* [全节点概念](/ji-chu/quan-jie-dian.md)
* [REST 规范](https://github.com/aptos-labs/aptos-core/tree/main/api)

### 硬件要求[​](https://aptos.dev/tutorials/run-a-fullnode#hardware-requirement) <a href="#hardware-requirement" id="hardware-requirement"></a>

为了运行生产级的全节点，我们建议使用以下硬件：

* CPU: Intel Xeon Skylake 或更新，4 核
* 内存：8GiB RAM

如果运行全节点用于开发或测试目的：

* CPU: 2 核
* 内存：4GiB RAM

### 存储要求

注意：Aptos 存储的数据量取决于区块链的账本历史（长度）和链上状态（例如账户）的数量。 这可能受到许多因素的影响，包括：区块链的年龄、平均交易率和账本修剪器的配置。

鉴于 DevNet 目前每周都会重置（请参阅 Discord 上的 #devnet-release 频道），我们估计 Aptos 需要的存储空间不会超过几 GB。

## 开始[​](https://aptos.dev/tutorials/run-a-fullnode#getting-started) <a href="#getting-started" id="getting-started"></a>

您可以通过两种方式配置公共 FullNode：使用 Aptos-core 源码或 Docker。

### 使用 Aptos-core 源码[​](https://aptos.dev/tutorials/run-a-fullnode#using-aptos-core-source-code) <a href="#using-aptos-core-source-code" id="using-aptos-core-source-code"></a>

1. 从 GitHub 下载并克隆 Aptos-core 项目，并通过运行以下命令准备您的开发环境：

   ```
   git clone https://github.com/aptos-labs/aptos-core.git
   cd aptos-core
   ./scripts/dev_setup.sh
   source ~/.cargo/env
   ```
2. 检出 devnet 分支：`git checkout origin/devnet`
3. 准备你的配置文件：
   * 将 `config/src/config/test_data/public_full_node.yaml`拷贝到你的工作目录。
   * 下载 devnet 的 [genesis](https://devnet.aptoslabs.com/genesis.blob) 和 [waypoint](https://devnet.aptoslabs.com/waypoint.txt) 文件。
   * 在你当前的工作目录中更新 public\_full\_node.yaml 文件：
     * 指定你想存储 devet 的数据目录到 `base:data_dir`；例如： `./data`。
     * 拷贝和粘贴 waypoint 文件内容到 `waypoint` 区域。
     * 通读配置并进行任何其他所需的更改。 您可以通过检查以下文件作为示例来查看 `public_full_node.yaml` 文件应该具有哪些配置： `docker/compose/public_full_node/public_full_node.yaml`
4. 运行 aptos-node：`cargo run -p aptos-node --release -- -f ./public_full_node.yaml`

您现在已成功配置并开始运行连接到 Aptos devnet 的全节点。

注意：这将在 `target/release/aptos-node` 下构建一个发布二进制文件。发布二进制文件往往比调试二进制文件快得多，但缺少对开发有用的调试信息。 只需省略 `--release` 标志即可构建调试二进制文件。

### 使用 Docker[​](https://aptos.dev/tutorials/run-a-fullnode#using-docker) <a href="#using-docker" id="using-docker"></a>

您还可以使用 Docker 来配置和运行您的全节点。

1. 安装 Docker 和 Docker-Compose。
2. 为您的公共全节点创建一个目录。
3. 将公共全节点的 [docker compose](https://github.com/aptos-labs/aptos-core/tree/main/docker/compose/public_full_node/docker-compose.yaml) 和 [aptos-core](https://github.com/aptos-labs/aptos-core/tree/main/docker/compose/public_full_node/public_full_node.yaml) 配置文件下载到此目录中。
4. 将 devnet 的 [genesis](https://devnet.aptoslabs.com/genesis.blob) 和 [waypoint](https://devnet.aptoslabs.com/waypoint.txt) 文件下载到该目录中。
5. 运行 docker-compose： `docker-compose up`.

## 验证您的全节点的正确性[​](https://aptos.dev/tutorials/run-a-fullnode#understand-and-verify-the-correctness-of-your-fullnode) <a href="#understand-and-verify-the-correctness-of-your-fullnode" id="understand-and-verify-the-correctness-of-your-fullnode"></a>

### **验证初始化同步**[**​**](https://aptos.dev/tutorials/run-a-fullnode#initial-synchronization)

在您的 FullNode 的初始同步期间，可能需要传输大量数据。 可以通过查询指标端口来监控进度，以查看您的节点当前同步到的版本。 运行以下命令：

```
curl 127.0.0.1:9101/metrics 2> /dev/null | grep "aptos_state_sync_version{.*\"synced\"}" | awk '{print $2}'
```

该命令将输出节点的当前同步版本。 例如：

```
71000
```

将此命令返回的同步版本（例如 71000）与 [Aptos 状态页面](https://status.devnet.aptos.dev/)上显示的当前版本（最新）进行比较。 如果您的节点正在赶上当前版本，则它正在正确同步。 注意：节点和状态页面之间存在一些版本差异是可以的，因为状态页面不会自动刷新。

### （可选）验证出站网络连接​[​](https://aptos.dev/tutorials/run-a-fullnode#optional-verify-outbound-network-connections) <a href="#optional-verify-outbound-network-connections" id="optional-verify-outbound-network-connections"></a>

出站网络连接数应大于0。运行以下命令：

```
curl 127.0.0.1:9101/metrics 2> /dev/null | grep "aptos_connections{direction=\"outbound\""
```

该命令将输出节点的出站网络连接数。 例如：

```
aptos_connections{direction="outbound",network_id="Public",peer_id="aabd651f",role_type="full_node"} 3
```

如果返回的出站连接数（例如 3）不超过 0，则意味着您的节点无法连接到 Aptos 区块链。 如果您遇到这种情况，请按照以下步骤操作，看看它们是否能解决问题：

1. 按照以下步骤将您的节点更新到最新版本：[更新步骤](#update-fullnode-with-new-release).
2. 删除您可能已添加到配置文件（例如，public\_full\_node.yaml）中的任何种子节点。 种子可能会阻止您连接到网络。种子对等点在[添加上游种子对等点](#tian-jia-shang-you-zhong-zi-jie-dian)部分中讨论。

### （可选）检查 Docker 分类帐大小[​](https://aptos.dev/tutorials/run-a-fullnode#optional-examine-docker-ledger-size) <a href="#optional-examine-docker-ledger-size" id="optional-examine-docker-ledger-size"></a>

可以通过进入 Docker 容器并检查大小来监控 DevNet 的区块链账本的体积。 这将允许您查看区块链分类帐当前消耗了多少存储空间。 运行这些命令来检查：

```
# 获得容器id:
id=$(docker container ls | grep public_full_node_fullnode_1 | grep -oE "^[0-9a-zA-Z]+")
# 进入容器：
docker exec -it $id /bin/bash
# 查看卷(ledger)大小:
du -cs -BM /opt/aptos/data
```

## **添加上游种子节点**[**​**](https://aptos.dev/tutorials/run-a-fullnode#add-upstream-seed-peers)

注意：您可能会在节点的错误消息中看到  `NoAvailablePeers` 。 这在节点首次启动时是正常的。 等待节点运行几分钟，看看它是否连接到对等点。 如果没有，请按照以下步骤操作：

Devnet 验证器全节点最多只能接受约 5000 个连接。 如果我们的网络容量很大，您的全节点可能无法连接。 您可能会在节点的错误消息中不断看到  `NoAvailablePeers` 。 如果发生这种情况，您可以在 FullNode 配置文件中设置种子以添加要连接的新对等点。 我们准备了一些 FullNode 地址供您使用，如下所示。

此外，请随意使用社区提供的那些（任何已经运行全节点的人都可以提供他们的地址供您连接）。 将这些添加到您的 `discovery_method` 下的配置文件中：

```
...
full_node_networks:
    - discovery_method: "onchain"
      # The network must have a listen address to specify protocols. This runs it locally to
      # prevent remote, incoming connections.
      listen_address: ...
      seeds:
        bb14af025d226288a3488b4433cf5cb54d6a710365a2d95ac6ffbd9b9198a86a:
            addresses:
            - "/dns4/pfn0.node.devnet.aptoslabs.com/tcp/6182/ln-noise-ik/bb14af025d226288a3488b4433cf5cb54d6a710365a2d95ac6ffbd9b9198a86a/ln-handshake/0"
            role: "Upstream"
        7fe8523388084607cdf78ff40e3e717652173b436ae1809df4a5fcfc67f8fc61:
            addresses:
            - "/dns4/pfn1.node.devnet.aptoslabs.com/tcp/6182/ln-noise-ik/7fe8523388084607cdf78ff40e3e717652173b436ae1809df4a5fcfc67f8fc61/ln-handshake/0"
            role: "Upstream"
        f6b135a59591677afc98168791551a0a476222516fdc55869d2b649c614d965b:
            addresses:
            - "/dns4/pfn2.node.devnet.aptoslabs.com/tcp/6182/ln-noise-ik/f6b135a59591677afc98168791551a0a476222516fdc55869d2b649c614d965b/ln-handshake/0"
            role: "Upstream"
...
```

## 高级指南[​](https://aptos.dev/tutorials/run-a-fullnode#advanced-guide) <a href="#advanced-guide" id="advanced-guide"></a>

如果您想深入了解节点配置的更多自定义，本高级指南将向您展示：&#x20;

* 为您的新全节点创建静态网络身份
* 检索其他节点许可列表的公共网络身份
* 启动带有或不带有静态网络标识的节点

### 为全节点创建静态身份[​](https://aptos.dev/tutorials/run-a-fullnode#create-a-static-identity-for-a-fullnode) <a href="#create-a-static-identity-for-a-fullnode" id="create-a-static-identity-for-a-fullnode"></a>

全节点将自动以随机生成的网络身份（PeerId 和公钥对）启动。 这对于常规的全节点非常有用，但如果您需要另一个节点将您列入白名单或提供特定权限，或者如果您希望始终以相同的身份运行您的全节点，则可以创建静态网络身份。

1. 使用 [aptos-labs/aptos-core](https://github.com/aptos-labs/aptos-core.git) 项目构建 `aptos-operational-tool` ，我们可以使用 cargo 构建来运行这些工具。 例如：

   ```
   $ git clone https://github.com/aptos-labs/aptos-core.git
   $ cd aptos-core
   $ ./scripts/dev_setup.sh
   $ source ~/.cargo/env
   $ cargo run -p aptos-operational-tool -- <command> <args>
   ```

   或者，您可以使用我们的 docker 镜像。 使用最新的工具版本启动一个 docker 容器，例如：

   ```
   $ docker run -i aptoslab/tools:devnet sh -x
   $ aptos-operational-tool <command> <arg>
   ```
2. 运行密钥生成器，输出十六进制编码的静态 x25519 PrivateKey。 这将是您网络身份的私钥。

   ```
    $ cargo run -p aptos-operational-tool -- generate-key --encoding hex --key-type x25519 --key-file /path/to/private-key.txt
   ```

   或者在 aptoslab/tools docker 容器内执行：

   ```
   $ aptos-operational-tool generate-key --encoding hex --key-type x25519 --key-file /path/to/private-key.txt
   ```

   key 文件示例：

   ```
   $ cat /path/to/private-key.txt
   B8BD811A91D8E6E0C6DAC991009F189337378760B55F3AD05580235325615C74
   ```

### 检索公共网络身份[​](https://aptos.dev/tutorials/run-a-fullnode#retrieve-public-network-identity) <a href="#retrieve-public-network-identity" id="retrieve-public-network-identity"></a>

1. 在前一个密钥文件上运行 peer 生成器

   ```
    $ cargo run -p aptos-operational-tool -- extract-peer-from-file --encoding hex --key-file /path/to/private-key.txt --output-file /path/to/peer-info.yaml
   ```

   或者在 aptoslab/tools docker 容器内执行：

   ```
   $ aptos-operational-tool extract-peer-from-file --encoding hex --key-file /path/to/private-key.txt --output-file /path/to/peer-info.yaml
   ```

   yaml 输出示例：

   ```
    ---
    ca3579457555c80fc7bb39964eb298c414fd60f81a2f8eedb0244ec07a26e575:
      addresses: []
      keys:
        - ca3579457555c80fc7bb39964eb298c414fd60f81a2f8eedb0244ec07a26e575
      role: Downstream
   ```

   在这个示例中， `ca3579457555c80fc7bb39964eb298c414fd60f81a2f8eedb0244ec07a26e575` 是 peer id， 也是从您在上一步生成的私钥派生的公钥。
2. 这将创建一个 yaml 文件，其中包含您的公共身份，以提供给上游完整节点。 如果您想通过特定的上游完整节点连接您的完整节点，并且该完整节点仅允许已知身份连接到它们，这将非常有用。

### 使用静态网络身份启动节点[​](https://aptos.dev/tutorials/run-a-fullnode#start-a-node-with-the-static-network-identity) <a href="#start-a-node-with-the-static-network-identity" id="start-a-node-with-the-static-network-identity"></a>

一旦我们有了静态身份，我们可以用它启动一个节点。

```
full_node_networks:
- network_id: "public"
  discovery_method: "onchain"
  identity:
    type: "from_config"
    key: "<PRIVATE_KEY>"
    peer_id: "<PEER_ID>"
```

示例：

```
full_node_networks:
- network_id: "public"
  discovery_method: "onchain"
  identity:
    type: "from_config"
    key: "B8BD811A91D8E6E0C6DAC991009F189337378760B55F3AD05580235325615C74"
    peer_id: "ca3579457555c80fc7bb39964eb298c414fd60f81a2f8eedb0244ec07a26e575"
```

### 允许其他全节点连接[​](https://aptos.dev/tutorials/run-a-fullnode#allow-other-fullnode-to-connect) <a href="#allow-other-fullnode-to-connect" id="allow-other-fullnode-to-connect"></a>

使用静态身份启动全节点后，您可以打开节点以允许其他人通过您的节点连接到 devnet。 确保在防火墙上打开端口 6180（或 6182，取决于您的节点正在侦听的端口，可以在 fullnode config yaml 文件中配置），并共享您的节点信息以供其他人用作 `seeds`。

```
<Peer_ID>:
  addresses:
  # with DNS
  - "/dns4/<DNS_Name>/tcp/<Port_Number>/ln-noise-ik/<Public_Key>/ln-handshake/0"
  role: Upstream
<Peer_ID>:
  addresses:
  # with IP
  - "/ip4/<IP_Address>/tcp/<Port_Number>/ln-noise-ik/<Public_Key>/ln-handshake/0"
  role: Upstream
```

确保您在地址中输入的端口号与您在全节点配置中的端口号（6180 或 6182）相匹配。 例子：

```
bb14af025d226288a3488b4433cf5cb54d6a710365a2d95ac6ffbd9b9198a86a:
  addresses:
  - "/dns4/pfn0.node.devnet.aptoslabs.com/tcp/6182/ln-noise-ik/bb14af025d226288a3488b4433cf5cb54d6a710365a2d95ac6ffbd9b9198a86a/ln-handshake/0"
  role: "Upstream"
bb14af025d226288a3488b4433cf5cb54d6a710365a2d95ac6ffbd9b9198a86a:
  addresses:
  - "/ip4/100.20.221.187/tcp/6182/ln-noise-ik/bb14af025d226288a3488b4433cf5cb54d6a710365a2d95ac6ffbd9b9198a86a/ln-handshake/0"
  role: "Upstream"
```

## 更新全节点新版本 <a href="#update-fullnode-with-new-release" id="update-fullnode-with-new-release"></a>

{% hint style="info" %}
✨ DEVNET 新地址格式 ✨

Aptos 地址现在是 32 字节而不是 16 字节。 如果您之前添加了种子对等点，请确保将它们更新为新的 32 字节格式。 对于使用静态身份的人，您可以按照相同的说明[重新生成您的身份](#create-a-static-identity-for-a-fullnode)，为上面的 FullNode 创建一个静态身份。
{% endhint %}

[​](https://aptos.dev/tutorials/run-a-fullnode/#update-fullnode-with-new-release)当 devnet 被擦除并使用更新版本发布时，您还需要更新 fullnode，以确保它可以与新网络保持同步。 一般步骤如下：

1. 关闭您的全节点
2. 删除数据文件（目录路径是您在 `public_full_node.yaml` 文件中指定的）， 默认是 `/opt/aptos/data`
3. 删除 `genesis.blob` 和 `waypoint.txt` 文件（取决于您怎么配置，你也可能没有这个文件）
4. 获取最新的 [genesis](https://devnet.aptoslabs.com/genesis.blob) 和 [waypoint](https://devnet.aptoslabs.com/waypoint.txt) 文件
5. 使用新的 waypoint 更新 `public_full_node.yaml` （如果你直接将 waypoint 配置在这里）
6. 重启全节点
7. 请参阅上面的 [验证初始同步](#yan-zheng-chu-shi-hua-tong-bu) 部分以检查节点是否再次进行状态同步。&#x20;


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://wiki.aptos.movemove.org/jiao-cheng/kai-fa-wang-devnet/yun-xing-quan-jie-dian-guan-fang-jiao-cheng.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
