From 514dcb5cee9c3c711ecb87c735bc016b04482072 Mon Sep 17 00:00:00 2001 From: "Du, Chengbin" Date: Fri, 6 May 2022 22:18:41 +0800 Subject: [PATCH] docs: Update Go gRPC client usage (#11884) ## Description Today, I'm going to use Go gRPC to interact with Cosmos SDK, but it is frustrating, the doc is totally outdated. I updated the doc while solving the problems. --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [x] added `!` to the type prefix if API or client breaking change - [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#pr-targeting)) - [x] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/main/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#testing) - [ ] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [x] updated the relevant documentation or specification - [x] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [x] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [x] confirmed `!` in the type prefix if API or client breaking change - [x] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [x] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [x] manually tested (if applicable) --- docs/run-node/interact-node.md | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/docs/run-node/interact-node.md b/docs/run-node/interact-node.md index 270326ad03..13cfb86d53 100644 --- a/docs/run-node/interact-node.md +++ b/docs/run-node/interact-node.md @@ -109,16 +109,28 @@ Assuming the state at that block has not yet been pruned by the node, this query The following snippet shows how to query the state using gRPC inside a Go program. The idea is to create a gRPC connection, and use the Protobuf-generated client code to query the gRPC server. +#### Install cosmos sdk + +Add below line to `go.mod` to replace protobuf, read more [#8469](https://github.com/cosmos/cosmos-sdk/issues/8469) + +``` +replace github.com/gogo/protobuf => github.com/regen-network/protobuf v1.3.3-alpha.regen.1 +``` + +```bash +$ go get github.com/cosmos/cosmos-sdk@main +``` + ```go import ( "context" "fmt" - "google.golang.org/grpc" + "google.golang.org/grpc" - "github.com/cosmos/cosmo-sdk/codec" + "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/types/tx" + banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" ) func queryState() error { @@ -128,20 +140,23 @@ func queryState() error { } // Create a connection to the gRPC server. - grpcConn := grpc.Dial( + grpcConn, err := grpc.Dial( "127.0.0.1:9090", // your gRPC server address. grpc.WithInsecure(), // The Cosmos SDK doesn't support any transport security mechanism. // This instantiates a general gRPC codec which handles proto bytes. We pass in a nil interface registry // if the request/response types contain interface instead of 'nil' you should pass the application specific codec. grpc.WithDefaultCallOptions(grpc.ForceCodec(codec.NewProtoCodec(nil).GRPCCodec())), ) + if err != nil { + return err + } defer grpcConn.Close() // This creates a gRPC client to query the x/bank service. bankClient := banktypes.NewQueryClient(grpcConn) bankRes, err := bankClient.Balance( context.Background(), - &banktypes.QueryBalanceRequest{Address: myAddress, Denom: "atom"}, + &banktypes.QueryBalanceRequest{Address: myAddress.String(), Denom: "atom"}, ) if err != nil { return err @@ -167,8 +182,10 @@ import ( "google.golang.org/grpc" "google.golang.org/grpc/metadata" + "github.com/cosmos/cosmos-sdk/codec" + sdk "github.com/cosmos/cosmos-sdk/types" grpctypes "github.com/cosmos/cosmos-sdk/types/grpc" - "github.com/cosmos/cosmos-sdk/types/tx" + banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" ) func queryState() error { @@ -177,13 +194,13 @@ func queryState() error { var header metadata.MD bankRes, err = bankClient.Balance( metadata.AppendToOutgoingContext(context.Background(), grpctypes.GRPCBlockHeightHeader, "12"), // Add metadata to request - &banktypes.QueryBalanceRequest{Address: myAddress, Denom: denom}, + &banktypes.QueryBalanceRequest{Address: myAddress.String(), Denom: "atom"}, grpc.Header(&header), // Retrieve header from response ) if err != nil { return err } - blockHeight = header.Get(grpctypes.GRPCBlockHeightHeader) + blockHeight := header.Get(grpctypes.GRPCBlockHeightHeader) fmt.Println(blockHeight) // Prints the block height (12)