Skip to content

Commit

Permalink
feat: add grpcurl guide (#13)
Browse files Browse the repository at this point in the history
Co-authored-by: Santiago Carmuega <[email protected]>
  • Loading branch information
Mercurial and scarmuega authored Sep 5, 2024
1 parent 2d143c5 commit 13c0d72
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 1 deletion.
7 changes: 6 additions & 1 deletion pages/_meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,10 @@
},
"servers": "Servers",
"clients": "Clients",
"teams": "Teams"
"teams": "Teams",
"-- Guides --": {
"type": "separator",
"title": "Guides"
},
"grpcurl": "Using grpcurl"
}
130 changes: 130 additions & 0 deletions pages/grpcurl.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
# Using Grpcurl for Interacting with U5C

## Introduction to Grpcurl

Grpcurl is to gRPC what curl is to HTTP. While HTTP APIs are prevalent and familiar to most developers, gRPC is gradually gaining traction, especially for internal microservices communication due to its efficiency and performance. However, gRPC's binary protocol can make it seem less accessible, as traditional tools like curl don't work with it. Enter grpcurl - a command-line tool that brings the ease and familiarity of curl to gRPC.

## Installing Grpcurl

Grpcurl can be installed on various operating systems. Detailed installation instructions and binaries can be found on its official GitHub repository (https://github.com/fullstorydev/grpcurl#installation).

## Connecting to a Local [Dolos](https://github.com/txpipe/dolos) Node (U5C compliant node)

```sh
grpcurl -plaintext localhost:50051 list
```

This command will list the available services on the Dolos node running locally on port 50051.

## Listing Services and Methods

You can list the services or describe specific methods provided by the U5C endpoint:

```sh
grpcurl -plaintext localhost:50051 list utxorpc.sync.v1.ChainSyncService
```

## Executing Request/Reply Method

```sh
grpcurl -plaintext localhost:50051 describe utxorpc.sync.v1.ChainSyncService.FetchBlock
```

This command will describe the FetchBlock method provided by the ChainSyncService. The output will be similar to the following:

```sh
utxorpc.sync.v1.ChainSyncService.FetchBlock is a method:
rpc FetchBlock ( .utxorpc.sync.v1.FetchBlockRequest ) returns ( .utxorpc.sync.v1.FetchBlockResponse );
```

We can describe the FetchBlockRequest to see what parameters it takes:

```sh
grpcurl -plaintext localhost:50051 describe utxorpc.sync.v1.FetchBlockRequest
```

The output will be similar to the following:
```sh
utxorpc.sync.v1.FetchBlockRequest is a message:
// Request to fetch a block by its reference.
message FetchBlockRequest {
repeated .utxorpc.sync.v1.BlockRef ref = 1;
.google.protobuf.FieldMask field_mask = 2;
}
```

Once you understand the request format, you can execute a method. For instance, fetching a specific block:

```sh
grpcurl -plaintext -d '{"ref": [{"index": 300042, "hash": "LRptYoRi+RG+TmxUmSsoB3lr6BLEQ2pLA+UWhgwgiIA="}]}' localhost:50051 utxorpc.sync.v1.ChainSyncService.FetchBlock
```

In this example the hash is the `Block` hash bytes encoded in `base64`.

The output will be similar to the following:
```json
{
"block": [
{
"cardano": {
"header": {
"slot": "300042",
"hash": "LRptYoRi+RG+TmxUmSsoB3lr6BLEQ2pLA+UWhgwgiIA="
},
"body": {
"tx": [
{
"inputs": [
{
"txHash": "KzlvndWhqG23d2GdZjia94LcRDqwk3nCodeuuRZVI0M=",
"outputIndex": 1
},
{
"txHash": "aUNQxBqVlZOv/hAiL8ZqhCMSmJR4ZLiKlNzzpwot66w="
}
],
"outputs": [
{
"address": "gtgYWEKDWBzE+sU/eyXZ3mt9Cbzb6HmcdZYQo+ENEiYDV/oioQFYHlgc6aRDIU6NiYGVLgPdq1GS6ZbJI5WXLL2VV3O2ngAahdniww==",
"coin": "742008225111"
},
{
"address": "gtgYWEKDWBwQXB0gBCdfHgxlVZOr2iQa4eUdi3cANpMA3QyFoQFYHlgcxdlJsC4jI0tnCIRphMmNR3Oe+l2wxIEj+eCZxgAauVbtDw==",
"coin": "140000000000"
}
],
"witnesses": {},
"collateral": {},
"validity": {},
"successful": true,
"auxiliary": {}
}
]
}
}
}
]
}
```

## Executing a Streaming Method

Streaming methods are one of the core features of gRPC. To execute a streaming method, such as **FollowTip** in the ChainSyncService:

1. First, understand the method:

```sh
grpcurl -plaintext localhost:50051 describe utxorpc.sync.v1.ChainSyncService.FollowTip
```

2. Then execute the method, observing the streaming responses:

```sh
grpcurl -plaintext localhost:50051 utxorpc.sync.v1.ChainSyncService.FollowTip
```

## Conclusion

Grpcurl makes interacting with gRPC services as simple as using curl with HTTP APIs. It opens the door for developers to easily test and interact with gRPC services, making the transition from HTTP to gRPC smoother and more approachable.


0 comments on commit 13c0d72

Please sign in to comment.