> ## Documentation Index
> Fetch the complete documentation index at: https://docs.tenderly.co/llms.txt
> Use this file to discover all available pages before exploring further.

# 在 Node RPC 上运行 Graph Node

> 将自托管 Graph Node 指向 Tenderly Node RPC，在公共网络上索引 subgraph，包括 trace_filter 调用处理器。

Graph Node 可以从任何 Ethereum JSON-RPC 端点索引 subgraph。将其指向 [Node RPC](/node-rpc/overview) 端点，即可使用事件处理器、调用处理器以及映射中的合约调用来索引公共网络。

本页使用 Graph Node 0.45.0 在 Ethereum Mainnet 上测试：一个包含事件处理器、USDC `transfer(address,uint256)` 调用处理器以及映射中 `balanceOf` 调用的 subgraph 从最近的一个区块开始索引，并始终与链头保持在一个区块以内。要改为索引网络的 fork 副本，请参见[使用 The Graph 索引 Virtual Environment](/virtual-environments/develop/thegraph)。

## 开始之前

* 您所用网络的 Node RPC URL，可在 [Tenderly Dashboard](https://dashboard.tenderly.co) 中 Node RPC 的 **Integrate** 视图中获取
* 一个 subgraph 以及 `graph init` 为其生成的 `docker-compose.yml`（参见[设置 Graph Node 和 subgraph](/virtual-environments/develop/thegraph#set-up-graph-node-and-the-subgraph)；对于公共网络，脚手架生成步骤相同）

## 配置 Ethereum 提供程序

在生成的 `docker-compose.yml` 中，将 `graph-node` 服务中的 `ethereum` 设置为 `<network>:archive,traces:<Node RPC URL>`。第一个冒号之前的网络名称必须与 `subgraph.yaml` 中的 `network` 一致。

```yaml title="docker-compose.yml（graph-node 的 environment）" highlight={7} theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    environment:
      postgres_host: postgres
      postgres_user: graph-node
      postgres_pass: let-me-in
      postgres_db: graph-node
      ipfs: "ipfs:5001"
      ethereum: "mainnet:archive,traces:https://mainnet.gateway.tenderly.co/${TENDERLY_NODE_ACCESS_KEY}"
      GRAPH_LOG: info
```

Docker Compose 会从您的 shell 中替换 `${TENDERLY_NODE_ACCESS_KEY}`，因此访问密钥不会写入文件：

```bash theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
export TENDERLY_NODE_ACCESS_KEY=<your-access-key>
docker compose up -d
docker compose logs -f graph-node
```

启动时，Graph Node 会为该提供程序记录 `capabilities: archive, traces` 和 `Provider supports block receipts`，后者表示它通过一个 `eth_getBlockReceipts` 请求获取区块的所有收据。

| 能力 | 启用的功能 | Node RPC 方法 |
| - | - | - |
| `archive` | 映射中在历史区块上的合约调用（`ethereum.call`） | 在区块哈希上调用的 `eth_call`、`eth_getBalance`、`eth_getCode` |
| `traces` | 调用处理器（`callHandlers`）以及带有 `call` 过滤器的区块处理器 | `trace_filter` |

当 `ethereum` 值未声明任何能力时（`mainnet:<url>`），Graph Node 会假定两项能力都可用。请显式声明它们，使配置能够表明 subgraph 依赖哪些能力。

在公共网络上，请将 `ETHEREUM_REORG_THRESHOLD` 保持为默认值 `250`。Graph Node 使用它在网络发生重组时回退区块，因此不要照搬 Virtual Environment 设置中的值 `1`。

## 检查您的网络是否提供 trace\_filter

调用处理器需要 `trace_filter`，但并非 Node RPC 上的每个网络都提供该方法。请针对最近的一个区块发出请求：

```bash theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
URL=https://mainnet.gateway.tenderly.co/$TENDERLY_NODE_ACCESS_KEY
BLOCK=$(cast block-number --rpc-url $URL)
curl -s $URL \
  -H 'content-type: application/json' \
  -d "{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"trace_filter\",\"params\":[{\"fromBlock\":\"$(cast to-hex $BLOCK)\",\"toBlock\":\"$(cast to-hex $BLOCK)\"}]}"
```

如果 `result` 中返回数组，说明调用处理器可以在该网络上使用。如果返回 `the method trace_filter does not exist/is not available` 或 `Method not found` 之类的错误，说明无法使用；请改用 `eventHandlers` 索引合约的事件。请以十六进制传入区块号，因为 `trace_filter` 会以 `Invalid params` 拒绝 `latest` 标签。

## Graph Node 发送的请求

Graph Node 会持续轮询 Node RPC，因此即使没有 subgraph 出现新事件，每个被索引的网络也会产生请求。各类请求的成本请参见 [Node RPC 定价](/node-rpc/pricing)。

| Graph Node 活动 | 方法 |
| - | - |
| 每隔 `ETHEREUM_POLLING_INTERVAL`（默认 `1000` ms）轮询最新区块 | `eth_getBlockByNumber` |
| 加载新区块及其收据 | `eth_getBlockByHash`、`eth_getBlockReceipts` |
| 为事件处理器查找事件 | `eth_getLogs` |
| 映射中的合约调用 | `eth_call`（Compute） |
| 调用处理器和 `call` 区块过滤器 | `trace_filter`（Debug and Trace） |

在 Node RPC 上，调用处理器是典型 subgraph 中成本最高的部分，因为一次 trace 请求的成本高于一次读取或一次合约调用。如果合约针对同一操作会发出事件，请改为索引该事件。如果可以接受几秒的索引延迟，请调高 `ETHEREUM_POLLING_INTERVAL` 以降低轮询频率。

## 故障排除

| 症状 | 原因 | 修复 |
| - | - | - |
| 出现 `Trying again after trace_filter RPC call ... failed`，且 subgraph 停止前进 | 该网络不提供 `trace_filter`。 | [检查 trace 支持](#检查您的网络是否提供-trace_filter)，并在不支持的网络上使用事件处理器。 |
| 运行 `graph deploy` 时出现 `network not supported by registrar: no network base found on chain ethereum` | `subgraph.yaml` 中的 `network` 与 `ethereum` 中的网络名称不一致。 | 在两处使用相同的名称。 |
| 同一区块反复出现 `Receipt for tx ... unavailable, block was likely uncled` | Graph Node 无法加载该区块的收据。 | Graph Node 会自动重试。如果同一区块持续失败，请发送电子邮件至 `support@tenderly.co`，并附上网络和区块哈希。 |

## 相关内容

* [Node RPC 概览](/node-rpc/overview)
* [Node RPC 定价](/node-rpc/pricing)
* `trace_filter` 的 [RPC 参考](/node-rpc/rpc-reference?method=trace_filter)
* [使用 The Graph 索引 Virtual Environment](/virtual-environments/develop/thegraph)，用于索引 fork 出的网络


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.