You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

切换Hedera本地节点至Testnet时遇gRPC超时错误求助

问题描述

原本通过Docker部署并配置了Hedera本地节点,现在想切换连接到Hedera Testnet(而非私有网络),但执行代码时出现以下超时错误:

Error: MaxAttemptsOrTimeoutError: max attempts of 10 was reached for request with last error being: GrpcServiceError: gRPC service failed with: Status: TIMEOUT, Code: 17
at AccountCreateTransaction.execute (C:\Users\souha\OneDrive\Desktop\Souhail\Hedera\hello-hedera-js-sdk\node_modules@hashgraph\sdk\lib\Executable.cjs:677:11)
at runNextTicks (node:internal/process/task_queues:60:5)
at listOnTimeout (node:internal/timers:540:9)
at process.processTimers (node:internal/timers:514:7)
at async main (C:\Users\souha\OneDrive\Desktop\Souhail\Hedera\hello-hedera-js-sdk\index.js:25:22) {
nodeAccountId: '0.0.3'
}

运行的代码如下:

const {
  Client,
  PrivateKey,
  AccountId,
  AccountCreateTransaction,
  Hbar,
} = require("@hashgraph/sdk");

async function main() {
  // Operator credentials
  const operatorAccountId = "0.0.5163020"; // Default operator account
  const operatorPrivateKey = PrivateKey.fromString(
    "302e020100300506032b657004220420200c4b94c8449cb9b024fd9048c863245e5397d408771c7d86b58cb1ac602856" // Replace with private key from logs
  );

  // Node configuration
  const node = { "127.0.0.1:50211": new AccountId(3) }; // Node account ID is often `0.0.3`
  const client = Client.forNetwork(node).setMirrorNetwork("127.0.0.1:5600");

  // Set the operator
  client.setOperator(operatorAccountId, operatorPrivateKey);

  // Create a new account
  const newKey = PrivateKey.generateED25519();
  const newAccount = await new AccountCreateTransaction()
    .setKey(newKey.publicKey)
    .setInitialBalance(Hbar.fromTinybars(1000))
    .execute(client);

  const receipt = await newAccount.getReceipt(client);
  console.log("New Account ID:", receipt.accountId);
}

main().catch((err) => {
  console.error("Error:", err);
});
解决方案

超时错误的核心原因是代码仍在使用本地节点的连接配置,没有切换到Testnet官方节点。按以下步骤修改即可解决:

  • 替换节点与镜像网络配置
    删掉原本地节点配置,改用Hedera SDK提供的Testnet快捷初始化方式,或手动配置Testnet节点:
    推荐方式(快捷初始化):

    const client = Client.forTestnet();
    

    手动配置方式:

    const testnetNodes = {
      "testnet-node0.hedera.com:50211": new AccountId(3),
      "testnet-node1.hedera.com:50211": new AccountId(4),
      "testnet-node2.hedera.com:50211": new AccountId(5),
      "testnet-node3.hedera.com:50211": new AccountId(6),
      "testnet-node4.hedera.com:50211": new AccountId(7)
    };
    const client = Client.forNetwork(testnetNodes).setMirrorNetwork(["hcs.testnet.mirrornode.hedera.com:5600"]);
    
  • 更换有效操作账户
    代码里的0.0.5163020是本地节点默认账户,在Testnet中不存在。需要替换为你自己的Testnet账户ID,对应的私钥也要换成Testnet账户的私钥(Testnet账户可通过官方水龙头获取)。

  • 修改后的完整代码示例

    const {
      Client,
      PrivateKey,
      AccountId,
      AccountCreateTransaction,
      Hbar,
    } = require("@hashgraph/sdk");
    
    async function main() {
      // 替换为你的Testnet操作账户ID和私钥
      const operatorAccountId = "0.0.XXXXXXX"; 
      const operatorPrivateKey = PrivateKey.fromString("你的Testnet账户私钥");
    
      // 初始化Testnet客户端
      const client = Client.forTestnet();
    
      // 设置操作账户
      client.setOperator(operatorAccountId, operatorPrivateKey);
    
      // 创建新账户
      const newKey = PrivateKey.generateED25519();
      const newAccount = await new AccountCreateTransaction()
        .setKey(newKey.publicKey)
        .setInitialBalance(Hbar.fromTinybars(1000))
        .execute(client);
    
      const receipt = await newAccount.getReceipt(client);
      console.log("New Account ID:", receipt.accountId);
    }
    
    main().catch((err) => {
      console.error("Error:", err);
    });
    
  • 额外检查项

    1. 确保本地网络可正常访问Hedera Testnet节点(无防火墙或代理拦截gRPC流量)
    2. 确认操作账户有足够Hbar余额(Testnet账户初始有免费额度,不足可再次通过水龙头申领)

内容的提问来源于stack exchange,提问作者Souhail Gana

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 20:23:15