|
5 | 5 | The `cardano-rpc` package provides client and server haskell modules for gRPC interface of `cardano-node`. |
6 | 6 | It implements [UTxO RPC](https://utxorpc.org/introduction) protobuf communication protocol specification. |
7 | 7 |
|
| 8 | +## Quickstart |
| 9 | + |
| 10 | +cardano-rpc is cardano-node's built-in gRPC interface, implementing the [UTxO RPC](https://utxorpc.org) spec. |
| 11 | +It is part of cardano-node itself: you enable it with a configuration flag, there is no extra service to run, and it is built, tested, and released together with cardano-node. |
| 12 | +Because UTxO RPC is a standard, the same client code also works against other servers that implement it, such as Dolos. |
| 13 | +It serves live chain data: the tip, UTxO queries, protocol parameters, and transaction evaluation and submission. |
| 14 | +It is not an indexer: there are no address-history queries; see [UTxO RPC v1beta spec coverage](#utxo-rpc-v1beta-spec-coverage) below for the full method status. |
| 15 | +The same configuration works against any network, from the local cluster used here to mainnet. |
| 16 | + |
| 17 | +### Prerequisites |
| 18 | + |
| 19 | +1. [Nix](https://nixos.org/download/) with flakes enabled; every command below fetches its tools through it. |
| 20 | + The first invocations download the toolchain, which can take several minutes and a few gigabytes. |
| 21 | +2. A checkout of this repository, for the vendored proto files the CLI example and the TypeScript quickstart load: |
| 22 | + |
| 23 | + ```bash |
| 24 | + git clone https://github.com/IntersectMBO/cardano-api |
| 25 | + cd cardano-api |
| 26 | + ``` |
| 27 | + |
| 28 | +Work through "Start a local cluster" and "Make your first call" below, then follow whichever quickstart in "Language examples" matches your stack. |
| 29 | + |
| 30 | +### Start a local cluster |
| 31 | + |
| 32 | +The cardano-testnet command below bundles the matching cardano-node and cardano-cli binaries, so one command is enough: |
| 33 | + |
| 34 | +```bash |
| 35 | +nix run github:IntersectMBO/cardano-node#cardano-testnet -- \ |
| 36 | + cardano --num-pool-nodes 1 --enable-grpc --output-dir /tmp/demo-cluster |
| 37 | +``` |
| 38 | + |
| 39 | +This starts a testnet with a single block-producing cardano-node and its gRPC server enabled, and keeps running in the foreground until you press Ctrl+C. |
| 40 | +The cluster is ready once it logs `Testnet started`; open a second terminal for everything below. |
| 41 | +The cluster comes with funded test wallets, created at startup under `/tmp/demo-cluster/utxo-keys/utxo1` to `utxo3` (`utxo.skey`, `utxo.vkey`, `utxo.addr`); the transaction example below spends from `utxo1`. |
| 42 | + |
| 43 | +The RPC endpoint is a Unix socket at `/tmp/demo-cluster/socket/node1/rpc.sock`, next to cardano-node's IPC socket. |
| 44 | +Most gRPC tooling expects a TCP endpoint, so bridge the socket to `localhost:50051` with socat and leave it running (a second background process, alongside the cluster): |
| 45 | + |
| 46 | +```bash |
| 47 | +socat TCP-LISTEN:50051,fork,reuseaddr UNIX-CONNECT:/tmp/demo-cluster/socket/node1/rpc.sock |
| 48 | +``` |
| 49 | + |
| 50 | +Every example below talks to `localhost:50051`. |
| 51 | +Connecting to a cardano-node that listens on TCP directly (`--grpc-listen-port`, see the configuration reference) works the same way, without the bridge; cardano-testnet gets the same ability with `--enable-grpc-http` in [#6685](https://github.com/IntersectMBO/cardano-node/pull/6685). |
| 52 | + |
| 53 | +> [!TIP] |
| 54 | +> To use your own binaries instead of the bundled ones, export `CARDANO_NODE` and `CARDANO_CLI` with their paths before running. |
| 55 | +
|
| 56 | +> [!NOTE] |
| 57 | +> Flag names differ between the two CLIs: `cardano-testnet` takes `--enable-grpc`, `cardano-node` itself takes `--grpc-enable`. |
| 58 | +
|
| 59 | +> [!WARNING] |
| 60 | +> The output directory must not exist from a previous run; genesis creation fails on leftovers (`Genesis output directory already exists`). |
| 61 | +> Run `rm -rf /tmp/demo-cluster` first when retrying, and make sure no cardano-node processes from an earlier attempt are still alive. |
| 62 | +> |
| 63 | +> Unix socket paths are capped at 108 bytes on Linux, and cardano-testnet fails at startup (`pokeSockAddr: path is too long`) when the output directory is nested too deep. |
| 64 | +> Keep `--output-dir` shallow, e.g. under `/tmp`. |
| 65 | +
|
| 66 | +### Make your first call |
| 67 | + |
| 68 | +The CLI examples use the proto files vendored in this repository, so run them from the repository root. |
| 69 | +First enter a subshell that puts the tools on PATH (your prompt changes; run the commands below inside it). |
| 70 | +`.#quickstart` bundles every tool the whole quickstart needs, including both language examples below; the per-language shells (`.#quickstart-rust`, `.#quickstart-typescript`) are minimal alternatives if you only want one: |
| 71 | + |
| 72 | +```bash |
| 73 | +nix develop .#quickstart |
| 74 | +``` |
| 75 | + |
| 76 | +or with `nix-shell` (using `cardano-rpc/quickstart/shell.nix`): |
| 77 | + |
| 78 | +```bash |
| 79 | +nix-shell cardano-rpc/quickstart/shell.nix |
| 80 | +``` |
| 81 | + |
| 82 | +or fetch them ad hoc: |
| 83 | + |
| 84 | +```bash |
| 85 | +nix shell nixpkgs#buf nixpkgs#grpcurl nixpkgs#socat |
| 86 | +``` |
| 87 | + |
| 88 | +Read the chain tip (the most recently adopted block) with [buf](https://buf.build/docs/installation): |
| 89 | + |
| 90 | +```bash |
| 91 | +buf curl \ |
| 92 | + --schema cardano-rpc/proto --protocol grpc --http2-prior-knowledge \ |
| 93 | + http://localhost:50051/utxorpc.v1beta.sync.SyncService/ReadTip |
| 94 | +``` |
| 95 | + |
| 96 | +It prints the tip as JSON: slot, hash, height, and timestamp. |
| 97 | +Your values differ per run, and the tip advances between calls. |
| 98 | + |
| 99 | +Then read the protocol parameters (the chain's current fee, size, and cost limits): |
| 100 | + |
| 101 | +```bash |
| 102 | +buf curl \ |
| 103 | + --schema cardano-rpc/proto --protocol grpc --http2-prior-knowledge -d '{}' \ |
| 104 | + http://localhost:50051/utxorpc.v1beta.query.QueryService/ReadParams |
| 105 | +``` |
| 106 | + |
| 107 | +With grpcurl instead: |
| 108 | + |
| 109 | +```bash |
| 110 | +grpcurl -plaintext \ |
| 111 | + -import-path cardano-rpc/proto -proto utxorpc/v1beta/sync/sync.proto \ |
| 112 | + localhost:50051 \ |
| 113 | + utxorpc.v1beta.sync.SyncService/ReadTip |
| 114 | +``` |
| 115 | + |
| 116 | +### Language examples |
| 117 | + |
| 118 | +- **Rust**: first calls via the `utxorpc-spec` crate. See [quickstart/rust/README.md](quickstart/rust/README.md). |
| 119 | +- **TypeScript**: build and submit a transaction with MeshJS. See [quickstart/typescript/README.md](quickstart/typescript/README.md). |
| 120 | + |
| 121 | +### Clean up |
| 122 | + |
| 123 | +Stop the cluster and the socat bridge with Ctrl+C in their terminals, then remove the cluster directory: |
| 124 | + |
| 125 | +```bash |
| 126 | +rm -rf /tmp/demo-cluster |
| 127 | +``` |
| 128 | + |
| 129 | +### Configuration reference |
| 130 | + |
| 131 | +The gRPC server is off by default. |
| 132 | +Enable it with `--grpc-enable` or `EnableRpc: true` in the cardano-node configuration; a node socket path must also be configured. |
| 133 | + |
| 134 | +Exactly one transport is active at a time: |
| 135 | + |
| 136 | +1. Unix socket (default): `rpc.sock` next to the node socket, or `--grpc-socket-path` / `RpcSocketPath`. |
| 137 | +2. HTTP/2 cleartext: `--grpc-listen-port` / `RpcListenPort`, optionally `--grpc-listen-address` / `RpcListenAddress` (default `127.0.0.1`). |
| 138 | +3. HTTP/2 with TLS: add `--grpc-tls-certificate` and `--grpc-tls-private-key` (`RpcTlsCertificateFile`, `RpcTlsPrivateKeyFile`), optionally repeatable `--grpc-tls-chain-certificate` (`RpcTlsChainCertificateFiles`). |
| 139 | + |
| 140 | +The three transports are mutually exclusive. |
| 141 | +TLS additionally requires a listen port, and its certificate and key must be set together; see the [Security](#security) section below before exposing an endpoint beyond localhost. |
| 142 | + |
| 143 | +Clients connect over TCP the same way as in the examples above: replace the Unix-socket connector or `unix://` target with the node's address and port. |
| 144 | + |
8 | 145 | ## UTxO RPC v1beta spec coverage |
9 | 146 |
|
10 | 147 | Methods marked ⬜ or ❌ are exposed by the server but respond with the `UNIMPLEMENTED` gRPC status. |
|
0 commit comments