# QUIC

> Transactions and bundles sent over one long-lived QUIC connection, with a path mask that picks the send paths.

Source: https://docs.constant-k.com/rpc/sending-transactions/quic/

Transactions and bundles are sent over one long-lived QUIC connection instead of HTTPS. The framing, reply codes and limits are shared with the zerocopy-swqos naming scheme, and constant-k adds one extension: the path mask, which picks the send paths for a transaction.

## Endpoints

| Region                                | Regular hostname                | Staked hostname                        |
| :------------------------------------ | :------------------------------ | :------------------------------------- |
| <Flag region="newyork" /> New York    | `quic-newyork.constant-k.com`   | `staked-quic-newyork.constant-k.com`   |
| <Flag region="frankfurt" /> Frankfurt | `quic-frankfurt.constant-k.com` | `staked-quic-frankfurt.constant-k.com` |
| <Flag region="amsterdam" /> Amsterdam | `quic-amsterdam.constant-k.com` | `staked-quic-amsterdam.constant-k.com` |

UDP port 443. The regular hostname routes like `rpc-<region>`: a tip picks Jito or Astralane, otherwise Network. The staked hostname routes like `staked-rpc-<region>`. A path mask on the request overrides both.

## Connection setup

1. Create a client certificate whose first subject CN is the API key. A self-signed certificate is enough, and its chain is not checked.
2. Connect with TLS 1.3, ALPN `ck-send/1`, and SNI set to the hostname.
3. Present the certificate. The key is read from it.

```bash
openssl req -x509 -newkey ec -pkeyopt ec_paramgen_curve:prime256v1 -nodes \
  -keyout client.key -out client.crt -days 365 -subj "/CN=YOUR_API_KEY"
```

### Naming schemes

ALPN `ck-send/1` is the default. `zerocopy-swqos/1` is also accepted, and a connection that offers it is answered in that scheme's reason names. The frames, codes, close codes, limits and path mask are the same on both.

A client that already speaks the zerocopy-swqos naming scheme connects with only the hostname and its API key changed.

### Limits

| Item                            | Value                                                                        |
| :------------------------------ | :--------------------------------------------------------------------------- |
| Connections per key, per region | 32                                                                           |
| Streams per connection          | 64 bidirectional and 64 unidirectional                                       |
| Idle timeout                    | 30 seconds. The server sends no keep-alive frames, so the client sends them. |
| Handshake                       | 3 seconds                                                                    |
| Each request                    | 2 seconds                                                                    |

Every plan connects. Each path a request uses counts against that path's plan limit, as on HTTPS.

### Close codes

| Code | Reason text        | Meaning                                                        |
| :--- | :----------------- | :------------------------------------------------------------- |
| 0    | `shutdown`         | The server is closing. Reconnect.                              |
| 1    | `connection limit` | The key already holds 32 connections in that region.           |
| 4    | `unknown host`     | The SNI is not one of this region's hostnames.                 |
| 8    | `unauthorized`     | No client certificate, or the key in it is missing or unknown. |

There is no paused key state, so `key_paused` is never sent.

## Streams and payload

| Stream                     | Request                                               | Reply                                         |
| :------------------------- | :---------------------------------------------------- | :-------------------------------------------- |
| Unidirectional             | One payload, no kind byte                             | None. The send still counts against the rate. |
| Bidirectional, kind `0x01` | `[0x01][flags][payload]`, one transaction             | Transaction frame                             |
| Bidirectional, kind `0x03` | `[0x03][payload]`, a bundle envelope with flag `0x02` | Bundle frame                                  |

On a kind `0x01` stream the flags byte is `0x00`, or `0x01` to run a preflight simulation at processed commitment. No other bit may be set. A stream that starts with any other byte is refused as malformed, problem `bad_stream_type`, and ends without a reply.

A payload whose first byte is not `0x00` is one signed transaction in wire format. A payload that starts with `0x00` is an envelope.

### Envelope

| Offset | Size | Field                                    |
| :----- | :--- | :--------------------------------------- |
| 0      | 1    | `0x00`                                   |
| 1      | 1    | Flags                                    |
| 2      | 1    | Path mask, when the flags include `0x04` |
| 2 or 3 | rest | Body: one transaction, or a bundle body  |

| Flag   | Meaning                                                                                                                                |
| :----- | :------------------------------------------------------------------------------------------------------------------------------------- |
| `0x01` | Declared tip. constant-k has no declared-tip billing, so the request is refused with reason `unsupported` (code 4). Tips are on-chain. |
| `0x02` | The body is a bundle body                                                                                                              |
| `0x04` | A path mask byte follows the flags                                                                                                     |

An envelope with no flag set, or with a reserved flag, is refused as malformed, problem `bad_envelope`.

### Bundle body

`[count][length][transaction]…`, repeated once per member, with nothing after the last transaction.

| Size   | Field                                                         |
| :----- | :------------------------------------------------------------ |
| 1      | Transaction count, 1 to 5                                     |
| 2      | Length of the next transaction, u16 little endian, 1 to 4,096 |
| length | The transaction's wire bytes                                  |

### Size limits

| Item                            | Limit                                                            |
| :------------------------------ | :--------------------------------------------------------------- |
| Payload                         | 16,403 bytes, or 16,404 bytes when the path mask byte is present |
| Legacy or version 0 transaction | 1,232 bytes                                                      |
| Version 1 transaction           | 4,096 bytes                                                      |
| Each bundle member              | 1 to 4,096 bytes                                                 |

### Worked examples

| Request                         | Payload bytes                     |
| :------------------------------ | :-------------------------------- |
| One transaction                 | `TX`                              |
| One transaction, all four paths | `00 04 0F TX`                     |
| Bundle of two                   | `00 02 02 L1 L1 TX1 L2 L2 TX2`    |
| Bundle of two, Jito path        | `00 06 04 02 L1 L1 TX1 L2 L2 TX2` |

`L1 L1` is the little endian length of `TX1`.

## Choosing send paths

The path mask byte picks the paths a transaction is sent on. It is present when the envelope flags include `0x04`.

| Bit    | Path      |
| :----- | :-------- |
| `0x01` | Network   |
| `0x02` | SWQoS     |
| `0x04` | Jito      |
| `0x08` | Astralane |
| `0x0F` | All four  |

* The chosen paths are sent at the same time, not one after another.
* A tipped path is used only when the transaction tips that path at or above its minimum. The minimums are on the [Jito](https://docs.constant-k.com/rpc/sending-transactions/jito/) and [Astralane](https://docs.constant-k.com/rpc/sending-transactions/astralane/) pages.
* Each chosen path counts as one send against that path's limit, exactly like an HTTPS send. A four-path send costs what four HTTPS sends cost. The limits are on the [Network](https://docs.constant-k.com/rpc/sending-transactions/network/), [SWQoS](https://docs.constant-k.com/rpc/sending-transactions/staked/), [Jito](https://docs.constant-k.com/rpc/sending-transactions/jito/) and [Astralane](https://docs.constant-k.com/rpc/sending-transactions/astralane/) pages.
* A path over its limit is skipped and named in the reply. The request is refused only when every chosen path is refused.
* A path that has not answered within 1.5 seconds is named `pending`. The reply is written then.
* A mask of `0x00`, or a mask with a bit above `0x08`, is refused as malformed, problem `bad_envelope`.
* Without a mask the request takes the route the matching HTTPS hostname would pick, and the reply carries no path list.

## Replies

| Frame       | Layout                                       |
| :---------- | :------------------------------------------- |
| Transaction | `0x01` code `LL LL` detail                   |
| Bundle      | `0x03` code 32-byte bundle id `LL LL` detail |

`LL LL` is the detail length, u16 little endian. The detail of a refusal is a UTF-8 JSON object with `reason`, its fields, `message` and `retryable`. An accepted request has an empty detail, except for a masked transaction, which reports its paths. A refused bundle carries an all-zero bundle id.

| Request              | Reply bytes                               |
| :------------------- | :---------------------------------------- |
| Accepted transaction | `01 00 00 00`                             |
| Refused transaction  | `01 03 LL LL {"reason":"tip_required",…}` |
| Accepted bundle      | `03 00 ID… (32 bytes) 00 00`              |

### Codes

| Code | Name            | Reasons                                                                   |
| :--- | :-------------- | :------------------------------------------------------------------------ |
| 0    | Accepted        | none                                                                      |
| 1    | RateLimited     | `rate_limited`                                                            |
| 2    | Unsigned        | `unsigned`                                                                |
| 3    | TipRequired     | `tip_required`                                                            |
| 4    | Malformed       | `malformed`, `unsupported`                                                |
| 5    | TooLarge        | `too_large`                                                               |
| 6    | SendFailed      | `send_failed`                                                             |
| 7    | NoSignatures    | `no_signatures`                                                           |
| 8    | Unauthorized    | `unauthorized`                                                            |
| 9    | Simulation      | `simulation_failed`                                                       |
| 10   | BundleInvalid   | `bundle_size`, `bundle_invalid`, `duplicate`, `superseded`, `unsupported` |
| 11   | NotReady        | `not_ready`                                                               |
| 12   | Internal        | `internal`                                                                |
| 13   | PaymentRequired | `payment_required`                                                        |

### Reason names

Only the names below change with the ALPN. Every other reason, code, frame, close code, limit and path value is the same in both schemes.

| Code | `ck-send/1`                | `zerocopy-swqos/1`          |
| :--- | :------------------------- | :-------------------------- |
| 3    | `tip_required`             | `missing_tip`               |
| 4    | `bad_stream_type`          | `invalid_stream_kind`       |
| 4    | `not_a_transaction`        | `expected_transaction`      |
| 4    | `not_a_bundle`             | `expected_bundle`           |
| 4    | `bad_envelope`             | `invalid_envelope`          |
| 6    | `send_failed`              | `forward_failed`            |
| 7    | `no_signatures`            | `signature_count`           |
| 11   | `retry_reason: relay_busy` | `waiting_for: block_engine` |

The code 4 names appear in the `problem` field of a `malformed` reply. The code 11 pair is a detail field and its value, not a reason.

### Path detail

An accepted masked transaction has code 0 and a detail that names every chosen path:

```json
{"paths":{"network":"network","swqos":"stake1","jito":"jito","astralane":"not_eligible:no_tip"}}
```

| Value                                                                  | Meaning                                              |
| :--------------------------------------------------------------------- | :--------------------------------------------------- |
| `network`, `stake1`, `jito`, `astralane`                               | The path was sent, and the value names the route     |
| `not_eligible:no_tip`                                                  | The transaction carries no tip for that path         |
| `not_eligible:tip_below_minimum`                                       | The tip for that path is under its minimum           |
| A refusal reason, such as `rate_limited`, `not_ready` or `send_failed` | The path refused the transaction                     |
| `pending`                                                              | The path had not answered when the reply was written |

`not_eligible:no_tip` and `not_eligible:tip_below_minimum` are the same in both naming schemes.

When no chosen path is eligible for the transaction's tip, nothing is sent and the reply is code 3:

```json
{"reason":"tip_required","paths":{"jito":"not_eligible:no_tip"},"retryable":false}
```

## Bundles

A bundle is a kind `0x03` stream, or a unidirectional stream whose envelope carries flag `0x02`. Bundles go to Jito. A 32-byte bundle id comes back in the frame.

* The bundle limits are the same as `sendBundle` over HTTPS; see [Jito](https://docs.constant-k.com/rpc/sending-transactions/jito/).
* A bundle with no mask goes to Jito by default.
* A mask on a bundle must include `0x04`. Other bits are ignored, and a mask without it is refused with reason `unsupported` (code 10).
* A bundle's tip is an on-chain transfer inside one of its transactions.

## Preflight

Flag `0x01` on a kind `0x01` stream runs a simulation at processed commitment before the transaction is sent. A transaction that fails it is answered with code 9 and is not sent. The detail carries `reason`, `err`, `logs`, `message` and `retryable`.

A live test of a transfer larger than the wallet's balance returned `InstructionError[0, Custom 1]` with the log `Transfer: insufficient lamports 99604169, need 1000000000000000`.

When the logs make the detail longer than 65,535 bytes, the logs are cut to their first 1,024 bytes and the error is kept whole.
