diff --git a/.gitbook/cn/infra/index.mdx b/.gitbook/cn/infra/index.mdx new file mode 100644 index 00000000..18a2d966 --- /dev/null +++ b/.gitbook/cn/infra/index.mdx @@ -0,0 +1,8 @@ +--- +title: 概述 +description: >- + 本节帮助节点运营者和验证者运行、升级和维护其 sentry/validator 节点。 +--- + +* [Mainnet Validator](/cn/infra/validator-mainnet/) +* [Testnet Validator](/cn/infra/validator-testnet/) diff --git a/.gitbook/cn/infra/interact-node/command-line.mdx b/.gitbook/cn/infra/interact-node/command-line.mdx new file mode 100644 index 00000000..b2d482a0 --- /dev/null +++ b/.gitbook/cn/infra/interact-node/command-line.mdx @@ -0,0 +1,7 @@ +--- +title: 使用 CLI 与节点交互 +--- + +你可以使用 `injectived` CLI 与节点交互。如果你正在与本地私有网络中的节点交互,请确保在使用 CLI 之前节点已在终端中运行。 + +有关如何使用 `injectived` 的更多详细信息,请参阅 [使用 injectived](/developers/injectived/use/ "mention")。 diff --git a/.gitbook/cn/infra/interact-node/go.mdx b/.gitbook/cn/infra/interact-node/go.mdx new file mode 100644 index 00000000..d9b568d3 --- /dev/null +++ b/.gitbook/cn/infra/interact-node/go.mdx @@ -0,0 +1,88 @@ +--- +title: 使用 Go 以编程方式与节点交互 +--- + + +以下示例使用 Go,但 Python 和 TS SDK 也可用于以编程方式与节点/Injective 交互。 + +* [TypeScript 示例](/developers-native/examples/) +* [Python 示例](https://github.com/InjectiveLabs/sdk-python/tree/master/examples) + + +以下代码片段展示了如何在 Go 程序中使用 gRPC 查询状态。其思路是创建一个 gRPC 连接,并使用 Protobuf 生成的客户端代码来查询 gRPC server。 + +```go +import ( + "context" + "fmt" + + "google.golang.org/grpc" + + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/tx" +) + +func queryState() error { + myAddress, err := sdk.AccAddressFromBech32("inj...") + if err != nil { + return err + } + + // 创建到 gRPC server 的连接。 + grpcConn := grpc.Dial( + "127.0.0.1:9090", // 你的 gRPC server 地址。 + grpc.WithInsecure(), // SDK 不支持任何传输安全机制。 + ) + defer grpcConn.Close() + + // 这将创建一个 gRPC 客户端来查询 x/bank 服务。 + bankClient := banktypes.NewQueryClient(grpcConn) + bankRes, err := bankClient.Balance( + context.Background(), + &banktypes.QueryBalanceRequest{Address: myAddress, Denom: "inj"}, + ) + if err != nil { + return err + } + + fmt.Println(bankRes.GetBalance()) // 打印账户余额 + + return nil +} +``` + +#### **使用 Go 查询历史状态** + +通过在 gRPC 请求中添加区块高度 metadata 来查询历史区块。 + +```go +import ( + "context" + "fmt" + + "google.golang.org/grpc" + "google.golang.org/grpc/metadata" + + grpctypes "github.com/cosmos/cosmos-sdk/types/grpc" + "github.com/cosmos/cosmos-sdk/types/tx" +) + +func queryState() error { + // --snip-- + + var header metadata.MD + bankRes, err = bankClient.Balance( + metadata.AppendToOutgoingContext(context.Background(), grpctypes.GRPCBlockHeightHeader, "12"), // 向请求添加 metadata + &banktypes.QueryBalanceRequest{Address: myAddress, Denom: denom}, + grpc.Header(&header), // 从响应中检索 header + ) + if err != nil { + return err + } + blockHeight = header.Get(grpctypes.GRPCBlockHeightHeader) + + fmt.Println(blockHeight) // 打印区块高度 (12) + + return nil +} +``` diff --git a/.gitbook/cn/infra/interact-node/grpc.mdx b/.gitbook/cn/infra/interact-node/grpc.mdx new file mode 100644 index 00000000..8dc1305f --- /dev/null +++ b/.gitbook/cn/infra/interact-node/grpc.mdx @@ -0,0 +1,62 @@ +--- +title: 使用 gRPC 与节点交互 +--- + +Protobuf 生态系统为不同用例开发了工具,包括从 `*.proto` 文件生成各种语言的代码。这些工具使客户端可以轻松构建。通常,客户端连接(即传输层)可以轻松插拔和替换。让我们探索一种流行的传输方法:gRPC。 + +由于代码生成库很大程度上取决于你自己的技术栈,我们只介绍两种替代方案: + +* `grpcurl` 用于通用调试和测试 +* 通过 Go、Python 或 TS 进行编程 + +## grpcurl + +[grpcurl](https://github.com/fullstorydev/grpcurl) 类似于 `curl`,但用于 gRPC。它也可作为 Go 库使用,但我们仅将其用作 CLI 命令进行调试和测试。按照上述链接中的说明进行安装。 + +假设你有一个本地节点正在运行(无论是 localnet 还是连接到实时网络),你应该能够运行以下命令来列出可用的 Protobuf 服务。你可以将 `localhost:9090` 替换为另一个节点的 gRPC server endpoint,该端点在 `app.toml` 中的 `grpc.address` 字段下配置: + +```bash +grpcurl -plaintext localhost:9090 list +``` + +你应该会看到 gRPC 服务列表,如 `cosmos.bank.v1beta1.Query`。这称为 reflection,是一个返回所有可用 endpoint 描述的 Protobuf endpoint。每个服务代表一个不同的 Protobuf 服务,每个服务公开多个可查询的 RPC 方法。 + +要获取服务的描述,你可以运行以下命令: + +```bash +# 我们要检查的服务 +grpcurl \ + localhost:9090 \ + describe cosmos.bank.v1beta1.Query +``` + +也可以执行 RPC 调用来查询节点信息: + +```bash +grpcurl \ + -plaintext + -d '{"address":"$MY_VALIDATOR"}' \ + localhost:9090 \ + cosmos.bank.v1beta1.Query/AllBalances +``` + +## 使用 grpcurl 查询历史状态 + +你还可以通过在 gRPC 请求中传递一些 [gRPC metadata](https://github.com/grpc/grpc-go/blob/master/Documentation/grpc-metadata.md) 来查询历史数据:`x-cosmos-block-height` metadata 应包含要查询的区块。使用上述 grpcurl,命令如下: + +```bash +grpcurl \ + -plaintext \ + -H "x-cosmos-block-height: 279256" \ + -d '{"address":"$MY_VALIDATOR"}' \ + localhost:9090 \ + cosmos.bank.v1beta1.Query/AllBalances +``` + +假设该区块的状态尚未被节点修剪,此查询应返回非空响应。 + +## 发送交易 + +使用 gRPC 和 REST 发送交易需要一些额外步骤:生成交易、签名,最后广播。 + +你可以在 [交易](/defi/transactions/ "mention") 中了解更多信息。 diff --git a/.gitbook/cn/infra/interact-node/index.mdx b/.gitbook/cn/infra/interact-node/index.mdx new file mode 100644 index 00000000..cee22adf --- /dev/null +++ b/.gitbook/cn/infra/interact-node/index.mdx @@ -0,0 +1,10 @@ +--- +title: 与节点交互 +--- + +本节介绍与 Injective 节点交互的不同方法。 + +- [命令行](/cn/infra/interact-node/command-line) +- [gRPC](/cn/infra/interact-node/grpc) +- [Go](/cn/infra/interact-node/go) +- [REST](/cn/infra/interact-node/rest) diff --git a/.gitbook/cn/infra/interact-node/rest.mdx b/.gitbook/cn/infra/interact-node/rest.mdx new file mode 100644 index 00000000..63cf9772 --- /dev/null +++ b/.gitbook/cn/infra/interact-node/rest.mdx @@ -0,0 +1,44 @@ +--- +title: 使用 REST Endpoints 与节点交互 +--- + +Cosmos SDK 上的所有 gRPC 服务都通过 gRPC-gateway 提供更便捷的基于 REST 的查询。URL 路径的格式基于 Protobuf 服务方法的完全限定名称,但可能包含一些小的自定义,以使最终 URL 看起来更符合惯例。例如,`cosmos.bank.v1beta1.Query/AllBalances` 方法的 REST endpoint 是 `GET /cosmos/bank/v1beta1/balances/{address}`。请求参数作为查询参数传递。 + +以下示例假设你使用 REST Endpoints 与本地私有网络中的节点交互。你可以将域名更改为公共网络。 + +作为具体示例,发出余额请求的 `curl` 命令是: + +```bash +curl \ + -X GET \ + -H "Content-Type: application/json" \ + http://localhost:1317/cosmos/bank/v1beta1/balances/$MY_VALIDATOR +``` + +确保将 `localhost:1317` 替换为你节点的 REST endpoint,该端点在 `api.address` 字段下配置。 + +所有可用 REST endpoint 的列表以 Swagger 规范文件的形式提供;可以在 `localhost:1317/swagger` 查看。确保你的 `app.toml` 文件中的 `api.swagger` 字段设置为 true。 + +## 使用 REST 查询历史状态 + +使用 HTTP header `x-cosmos-block-height` 查询历史状态。例如,curl 命令如下: + +```bash +curl \ + -X GET \ + -H "Content-Type: application/json" \ + -H "x-cosmos-block-height: 279256" \ + http://localhost:1317/cosmos/bank/v1beta1/balances/$MY_VALIDATOR +``` + +假设该区块的状态尚未被节点修剪,此查询应返回非空响应。 + +## 跨域资源共享 (CORS) + +默认情况下未启用 [CORS 策略](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS) 以帮助提高安全性。如果你想使用 rest-server,我们建议你提供反向代理。这可以使用 [nginx](https://www.nginx.com/) 完成。出于测试和开发目的,`app.toml` 中有一个 `enabled-unsafe-cors` 字段。 + +## 发送交易 + +使用 gRPC 和 REST 发送交易需要一些额外步骤:生成交易、签名,最后广播。 + +你可以在 [交易](/defi/transactions/ "mention") 中了解更多信息。 diff --git a/.gitbook/cn/infra/run-node.mdx b/.gitbook/cn/infra/run-node.mdx new file mode 100644 index 00000000..be955f43 --- /dev/null +++ b/.gitbook/cn/infra/run-node.mdx @@ -0,0 +1,56 @@ +--- +title: 运行节点 +--- + +强烈建议你在加入公共网络之前先设置本地私有网络。这将帮助你熟悉设置过程,并提供测试环境。 + +### **私有网络** + +* 通过在本地设置独立网络来加入 + +### **公共网络** + +* 通过公共 endpoints 使用网络;或 +* 通过运行节点加入 + +任何人都可以设置自己的节点,并配置 endpoints 与 Injective 区块链通信。为方便起见,还有一些公共 endpoints 可用于查询链。这些建议用于开发和测试目的。为了获得最大的控制和可靠性,建议运行自己的节点。 + +## 运行节点的准备工作 + +如果你选择运行节点(无论是设置私有网络还是加入公共网络),你必须设置 keyring。你还可以选择安装 Cosmovisor,它可以协助进行链升级以实现最小停机时间。 + +## 与节点交互 + +节点启动并运行后,有几种方式可以与节点交互,即使用 gRPC endpoints、REST endpoints 或 `injectived` CLI。你可以在 [与节点交互](/cn/infra/interact-node/) 部分了解更多信息。 + +## 运行节点指南 + + + + 了解如何设置你的 keyring + + + 了解如何加入网络 + + + 了解如何升级你的节点 + + diff --git a/.gitbook/docs.json b/.gitbook/docs.json index 8d080acc..0cf2a9b8 100644 --- a/.gitbook/docs.json +++ b/.gitbook/docs.json @@ -810,6 +810,26 @@ } ] }, + { + "group": "基础设施", + "icon": "network-wired", + "expanded": false, + "pages": [ + "cn/infra/index", + { + "group": "与节点交互", + "expanded": false, + "pages": [ + "cn/infra/interact-node/index", + "cn/infra/interact-node/command-line", + "cn/infra/interact-node/grpc", + "cn/infra/interact-node/go", + "cn/infra/interact-node/rest" + ] + }, + "cn/infra/run-node" + ] + }, { "group": "Native 开发者", "icon": "microchip",