eth2.0-specs/specs/networking/rpc-interface.md

284 lines
8.8 KiB
Markdown
Raw Normal View History

2019-05-06 15:30:32 +00:00
# Eth 2.0 Networking Spec - RPC Interface
2019-03-12 20:46:58 +00:00
2019-05-06 15:30:32 +00:00
## Abstract
2019-03-12 20:46:58 +00:00
The Ethereum 2.0 networking stack uses two modes of communication: a broadcast protocol that gossips information to interested parties via GossipSub, and an RPC protocol that retrieves information from specific clients. This specification defines the RPC protocol.
2019-05-06 15:30:32 +00:00
The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL", NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in [RFC 2119](https://tools.ietf.org/html/rfc2119).
2019-03-12 20:46:58 +00:00
2019-05-06 15:30:32 +00:00
## Dependencies
2019-03-12 20:46:58 +00:00
2019-04-12 23:32:36 +00:00
This specification assumes familiarity with the [Messaging](./messaging.md), [Node Identification](./node-identification.md), and [Beacon Chain](../core/0_beacon-chain.md) specifications.
2019-03-12 20:46:58 +00:00
# Specification
2019-05-06 15:30:32 +00:00
## Message schemas
2019-03-12 20:46:58 +00:00
Message body schemas are notated like this:
```
(
field_name_1: type
field_name_2: type
)
```
2019-03-18 04:19:12 +00:00
Embedded types are serialized as SSZ Containers unless otherwise noted.
2019-03-12 20:46:58 +00:00
2019-05-06 15:30:32 +00:00
All referenced data structures can be found in the [Beacon Chain](../core/0_beacon-chain.md#data-structures) specification.
2019-03-12 20:46:58 +00:00
2019-05-06 15:30:32 +00:00
## `libp2p` protocol names
2019-03-12 20:46:58 +00:00
2019-05-06 15:30:32 +00:00
A "Protocol ID" in `libp2p` parlance refers to a human-readable identifier `libp2p` uses in order to identify sub-protocols and stream messages of different types over the same connection. Peers exchange supported protocol IDs via the `Identify` protocol upon connection. When opening a new stream, peers pin a particular protocol ID to it, and the stream remains contextualized thereafter. Since messages are sent inside a stream, they do not need to bear the protocol ID.
2019-03-12 20:46:58 +00:00
2019-05-06 15:30:32 +00:00
## RPC-over-`libp2p`
2019-03-12 20:46:58 +00:00
2019-03-19 22:32:38 +00:00
To facilitate RPC-over-`libp2p`, a single protocol name is used: `/eth/serenity/beacon/rpc/1`. The version number in the protocol name is neither backwards or forwards compatible, and will be incremented whenever changes to the below structures are required.
Remote method calls are wrapped in a "request" structure:
2019-03-12 20:46:58 +00:00
```
(
id: uint64
method_id: uint16
2019-05-16 23:43:10 +00:00
body: (message_body...)
2019-03-12 20:46:58 +00:00
)
```
and their corresponding responses are wrapped in a "response" structure:
```
(
id: uint64
2019-03-21 02:04:04 +00:00
response_code: uint16
result: bytes
2019-03-12 20:46:58 +00:00
)
```
2019-05-16 23:43:10 +00:00
A union type is used to determine the contents of the `body` field in the request structure. Each "body" entry in the RPC calls below corresponds to one subtype in the `body` type union.
2019-03-12 20:46:58 +00:00
The details of the RPC-Over-`libp2p` protocol are similar to [JSON-RPC 2.0](https://www.jsonrpc.org/specification). Specifically:
1. The `id` member is REQUIRED.
2. The `id` member in the response MUST be the same as the value of the `id` in the request.
2019-03-18 04:19:12 +00:00
3. The `id` member MUST be unique within the context of a single connection. Monotonically increasing `id`s are RECOMMENDED.
4. The `method_id` member is REQUIRED.
2019-03-21 02:04:04 +00:00
5. The `result` member is REQUIRED on success.
6. The `result` member is OPTIONAL on errors, and MAY contain additional information about the error.
7. `response_code` MUST be `0` on success.
2019-03-12 20:46:58 +00:00
2019-03-18 04:19:12 +00:00
Structuring RPC requests in this manner allows multiple calls and responses to be multiplexed over the same stream without switching. Note that this implies that responses MAY arrive in a different order than requests.
2019-03-12 20:46:58 +00:00
The "method ID" fields in the below messages refer to the `method` field in the request structure above.
2019-03-21 02:04:04 +00:00
The first 1,000 values in `response_code` are reserved for system use. The following response codes are predefined:
2019-03-12 20:46:58 +00:00
2019-03-21 02:04:04 +00:00
1. `0`: No error.
2. `10`: Parse error.
2. `20`: Invalid request.
3. `30`: Method not found.
4. `40`: Server error.
2019-03-12 20:46:58 +00:00
2019-05-06 15:30:32 +00:00
### Alternative for non-`libp2p` clients
2019-03-19 22:32:38 +00:00
Since some clients are waiting for `libp2p` implementations in their respective languages. As such, they MAY listen for raw TCP messages on port `9000`. To distinguish RPC messages from other messages on that port, a byte prefix of `ETH` (`0x455448`) MUST be prepended to all messages. This option will be removed once `libp2p` is ready in all supported languages.
2019-03-12 20:46:58 +00:00
## Messages
### Hello
**Method ID:** `0`
**Body**:
```
(
network_id: uint8
2019-03-25 20:27:18 +00:00
chain_id: uint64
2019-03-12 20:46:58 +00:00
latest_finalized_root: bytes32
latest_finalized_epoch: uint64
best_root: bytes32
best_slot: uint64
)
```
Clients exchange `hello` messages upon connection, forming a two-phase handshake. The first message the initiating client sends MUST be the `hello` message. In response, the receiving client MUST respond with its own `hello` message.
Clients SHOULD immediately disconnect from one another following the handshake above under the following conditions:
1. If `network_id` belongs to a different chain, since the client definitionally cannot sync with this client.
2. If the `latest_finalized_root` shared by the peer is not in the client's chain at the expected epoch. For example, if Peer 1 in the diagram below has `(root, epoch)` of `(A, 5)` and Peer 2 has `(B, 3)`, Peer 1 would disconnect because it knows that `B` is not the root in their chain at epoch 3:
```
Root A
+---+
|xxx| +----+ Epoch 5
+-+-+
^
|
+-+-+
| | +----+ Epoch 4
+-+-+
Root B ^
|
+---+ +-+-+
|xxx+<---+--->+ | +----+ Epoch 3
+---+ | +---+
|
+-+-+
| | +-----------+ Epoch 2
+-+-+
^
|
+-+-+
| | +-----------+ Epoch 1
+---+
```
2019-05-06 15:30:32 +00:00
Once the handshake completes, the client with the higher `latest_finalized_epoch` or `best_slot` (if the clients have equal `latest_finalized_epoch`s) SHOULD request beacon block roots from its counterparty via `beacon_block_roots` (i.e. RPC method `10`).
2019-03-12 20:46:58 +00:00
### Goodbye
**Method ID:** `1`
**Body:**
```
(
reason: uint64
)
```
2019-03-19 22:32:38 +00:00
Client MAY send `goodbye` messages upon disconnection. The reason field MAY be one of the following values:
2019-03-12 20:46:58 +00:00
- `1`: Client shut down.
- `2`: Irrelevant network.
2019-03-19 22:32:38 +00:00
- `3`: Fault/error.
Clients MAY define custom goodbye reasons as long as the value is larger than `1000`.
2019-03-12 20:46:58 +00:00
2019-05-06 15:30:32 +00:00
### Get status
2019-03-21 02:04:04 +00:00
**Method ID:** `2`
2019-05-06 15:30:32 +00:00
**Request body:**
2019-03-21 02:04:04 +00:00
```
(
sha: bytes32
user_agent: bytes
timestamp: uint64
)
```
2019-05-06 15:30:32 +00:00
**Response body:**
2019-03-21 02:04:04 +00:00
```
(
sha: bytes32
user_agent: bytes
timestamp: uint64
)
```
Returns metadata about the remote node.
2019-05-06 15:30:32 +00:00
### Request beacon block roots
2019-03-12 20:46:58 +00:00
**Method ID:** `10`
2019-05-06 15:30:32 +00:00
**Request body**
2019-03-18 04:19:12 +00:00
```
2019-03-19 22:32:38 +00:00
(
start_slot: uint64
count: uint64
)
2019-03-18 04:19:12 +00:00
```
2019-05-06 15:30:32 +00:00
**Response body:**
2019-03-12 20:46:58 +00:00
```
# BlockRootSlot
(
block_root: bytes32
2019-03-12 20:46:58 +00:00
slot: uint64
)
(
roots: []BlockRootSlot
)
```
2019-03-21 02:04:04 +00:00
Requests a list of block roots and slots from the peer. The `count` parameter MUST be less than or equal to `32768`. The slots MUST be returned in ascending slot order.
2019-03-12 20:46:58 +00:00
2019-05-06 15:30:32 +00:00
### Beacon block headers
2019-03-12 20:46:58 +00:00
**Method ID:** `11`
2019-05-06 15:30:32 +00:00
**Request body**
2019-03-12 20:46:58 +00:00
```
(
start_root: HashTreeRoot
start_slot: uint64
max_headers: uint64
skip_slots: uint64
)
```
2019-05-06 15:30:32 +00:00
**Response body:**
2019-03-12 20:46:58 +00:00
```
(
2019-03-21 02:04:04 +00:00
headers: []BeaconBlockHeader
2019-03-12 20:46:58 +00:00
)
```
2019-05-06 15:30:32 +00:00
Requests beacon block headers from the peer starting from `(start_root, start_slot)`. The response MUST contain no more than `max_headers` headers. `skip_slots` defines the maximum number of slots to skip between blocks. For example, requesting blocks starting at slots `2` a `skip_slots` value of `1` would return the blocks at `[2, 4, 6, 8, 10]`. In cases where a slot is empty for a given slot number, the closest previous block MUST be returned. For example, if slot `4` were empty in the previous example, the returned array would contain `[2, 3, 6, 8, 10]`. If slot three were further empty, the array would contain `[2, 6, 8, 10]`—i.e. duplicate blocks MUST be collapsed. A `skip_slots` value of `0` returns all blocks.
2019-03-12 20:46:58 +00:00
2019-04-23 17:49:59 +00:00
The function of the `skip_slots` parameter helps facilitate light client sync - for example, in [#459](https://github.com/ethereum/eth2.0-specs/issues/459) - and allows clients to balance the peers from whom they request headers. Clients could, for instance, request every 10th block from a set of peers where each peer has a different starting block in order to populate block data.
2019-03-12 20:46:58 +00:00
2019-05-06 15:30:32 +00:00
### Beacon block bodies
2019-03-12 20:46:58 +00:00
**Method ID:** `12`
2019-05-06 15:30:32 +00:00
**Request body:**
2019-03-12 20:46:58 +00:00
```
(
block_roots: []HashTreeRoot
)
```
2019-05-06 15:30:32 +00:00
**Response body:**
2019-03-12 20:46:58 +00:00
```
(
block_bodies: []BeaconBlockBody
)
```
2019-05-06 15:30:32 +00:00
Requests the `block_bodies` associated with the provided `block_roots` from the peer. Responses MUST return `block_roots` in the order provided in the request. If the receiver does not have a particular `block_root`, it must return a zero-value `block_body` (i.e. a `block_body` container with all zero fields).
2019-03-12 20:46:58 +00:00
2019-05-06 15:30:32 +00:00
### Beacon chain state
2019-03-12 20:46:58 +00:00
2019-05-06 15:30:32 +00:00
*Note*: This section is preliminary, pending the definition of the data structures to be transferred over the wire during fast sync operations.
2019-03-12 20:46:58 +00:00
**Method ID:** `13`
2019-05-06 15:30:32 +00:00
**Request body:**
2019-03-12 20:46:58 +00:00
```
(
hashes: []HashTreeRoot
)
```
2019-05-06 15:30:32 +00:00
**Response body:** TBD
2019-03-12 20:46:58 +00:00
2019-04-23 17:49:59 +00:00
Requests contain the hashes of Merkle tree nodes that when merkleized yield the block's `state_root`.
2019-03-12 20:46:58 +00:00
The response will contain the values that, when hashed, yield the hashes inside the request body.