Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

archive/storage: Backport API from chainHead #94

Merged
merged 19 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
- [archive_unstable_hashByHeight](api/archive_unstable_hashByHeight.md)
- [archive_unstable_header](api/archive_unstable_header.md)
- [archive_unstable_storage](api/archive_unstable_storage.md)
- [archive_unstable_storageContinue](api/archive_unstable_storageContinue.md)
- [chainHead](api/chainHead.md)
- [chainHead_unstable_body](api/chainHead_unstable_body.md)
- [chainHead_unstable_call](api/chainHead_unstable_call.md)
Expand Down
122 changes: 75 additions & 47 deletions src/api/archive_unstable_storage.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,92 +2,120 @@

**Parameters**:

- `hash`: String containing a hexadecimal-encoded hash of the header of the block whose storage to fetch.
- `key`: String containing the hexadecimal-encoded key to fetch in the storage.
- `hash`: String containing an hexadecimal-encoded hash of the header of the block whose storage to fetch.
lexnv marked this conversation as resolved.
Show resolved Hide resolved
- `items`: Array of objects. The structure of these objects is found below.
- `childTrie`: `null` for main storage look-ups, or a string containing the hexadecimal-encoded key of the child trie of the "default" namespace.
- `includeDescendants`: Boolean indicating whether the key-values of all the descendants of the `key` should be returned as well.

**Return value**: String containing an opaque value representing the operation, or `null` if no block with that `hash` exists.
Each element in `items` must be an object containing the following fields:

- `key`: String containing the hexadecimal-encoded key to fetch in the storage.
- `type`: String equal to one of: `value`, `hash`, `closestDescendantMerkleValue`, `descendantsValues`, `descendantsHashes`.
- `paginationStartKey`: This parameter is optional and should be a string containing the hexadecimal-encoded key from which the storage iteration should resume. This parameter is only valid in the context of `descendantsValues` and `descendantsHashes`.

**Note**:
lexnv marked this conversation as resolved.
Show resolved Hide resolved

For each item in `items`, the JSON-RPC server must start obtaining the value of the entry with the given `key` from the storage, either from the main trie or from `childTrie`. If `type` is `descendantsValues` or `descendantsHashes`, then it must also obtain the values of all the descendants of the entry.

For the purpose of storage requests, the trie root hash of the child tries of the storage can be found in the main trie at keys starting the bytes of the ASCII string `:child_storage:`. This behaviour is consistent with all the other storage-request-alike mechanisms of Polkadot and Substrate-based chains, such as host functions or libp2p network requests.

If the height of the block hash provided is less than or equal to the current finalized block height (which can be obtained via archive_unstable_finalizedHeight), then calling this method with the same parameters will always return the same response.
If the height of the block hash provided is greater than the current finalized block height, then the block might be pruned at any time and calling this method may return null.

The JSON-RPC server must obtain the value of the entry with the given `key` from the storage, either from the main trie of from `childTrie`. If `includeDescendants` is `true`, then the values of all the descendants must be obtained as well.
This function should be used when the target block is older than the blocks reported by `chainHead_unstable_follow`.
Use `chainHead_unstable_storage` if instead you want to retrieve the storage of a block obtained by the `chainHead_unstable_follow`.

If the block was previously returned by `archive_unstable_hashByHeight` at a height inferior or equal to the current finalized block height (as indicated by `archive_unstable_finalizedHeight`), then calling this method multiple times is guaranteed to always return non-null and always the same results.
If `items` contains multiple identical or overlapping queries, the JSON-RPC server can choose whether to merge or not the items in the result. For example, if the request contains two items with the same key, one with `hash` and one with `value`, the JSON-RPC server can choose whether to generate two `item` objects, one with the value and one with the hash, or only a single `item` object with both `hash` and `value` set. The JSON-RPC server is encouraged to notify as soon as possible of the information at its disposal, without waiting for missing information.
lexnv marked this conversation as resolved.
Show resolved Hide resolved

If the block was previously returned by `archive_unstable_hashByHeight` at a height strictly superior to the current finalized block height (as indicated by `archive_unstable_finalizedHeight`), then the block might "disappear" and calling this function might return `null` at any point.
It is allowed (but discouraged) for the JSON-RPC server to provide the same information multiple times in the result, for example providing the `value` field of the same `key` twice. Forcing the JSON-RPC server to de-duplicate items in the result might lead to unnecessary overhead.

## Notifications format
**Return value**: A JSON object.

This function will later generate notifications in the following format:
The JSON object returned by this function has the following format:

```json
```
{
"jsonrpc": "2.0",
"method": "archive_unstable_storageEvent",
"params": {
"subscription": "...",
"result": ...
}
"result": [
{
...
},
...
],
"discardedItems": ...
}
```

Where `subscription` is equal to the value returned by this function, and `result` can be one of:
Where:

- `result` contains a vector of JSON objects (possibly empty) that were found in the storage.
- `discardedItems` is an integer indicating the number of items at the back of the array of the `items` parameters that couldn't be processed.

The JSON objects in the `"result"` field can have one of the following formats based on their type:

### item
### Value

```json
```
{
"event": "item",
"key": "0x0000000...",
"value": "0x0000000...",
}
```

Yields an item that was found in the storage.
The JSON object corresponds to one of the requested items whose `type` was `"value"` or `"descendantsValues"`.

If the `key` is not associated with a storage value in the trie, then no response is generated.
Copy link
Member

@niklasad1 niklasad1 Nov 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this is fine but it would be easier for clients if the server would just send back null to indicate that key was not found.

For this API, a client probably needs to have a timeout for these request and after for instance 1 min the call is regarded as failed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will modify the phrasing of the spec. What I meant by this, given archive/storage is a plain method, no response will be generated for this "key" in the "result" array. Users will still get back once all queries are processed the responses back


Returned when the `type` of the query was `"value"`:

The `key` field is a string containing the hexadecimal-encoded key of the value that was found.
If the `includeDescendants` parameter was `true`, this `key` is guaranteed to start with the `key` provided as parameter.
If the `includeDescendants` parameter was `false`, then it is also guaranteed to be equal to the `key` provided as parameter.
- `key` is guaranteed to be equal to one of the `key`s provided.
- `value` is a string containing the hexadecimal-encoded value of the storage entry.

The `value` field is a string containing the hexadecimal-encoded value of the storage item.
Returned when the `type` of the query was `"descendantsValues"`:

### waiting-for-continue
- `key` is guaranteed to start with one of the `key`s provided.
- `value` is a string containing the hexadecimal-encoded value of the storage entry.

```json
### Hash

```
{
"event": "waiting-for-continue"
"key": "0x0000000...",
"hash": "0x0000000...",
}
```

The `waiting-for-continue` event is generated after at least one `"item"` event has been generated, and indicates that the JSON-RPC client must call `archive_unstable_storageContinue` before more events are generated.
The JSON object corresponds to one of the requested items whose `type` was `"hash"` or `"descendantsHashes"`.

This event only ever happens if the `includeDescendants` parameter was `true`.
If the `key` is not associated with a storage value in the trie, then no response is generated.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here 👆


While the JSON-RPC server is waiting for `archive_unstable_storageContinue` to be called, it can generate a `stop` event indicating that it can no longer proceed with that storage access.
Returned when the `type` of the query was `"hash"`:

### done

```json
{
"event": "done"
}
```
- `key` is guaranteed to be equal to one of the `key`s provided.
- `hash` is a string containing the hexadecimal-encoded cryptographic hash of the storage entry.
lexnv marked this conversation as resolved.
Show resolved Hide resolved

The `done` event indicates that everything went well and all values have been provided through `item` events in the past.
Returned when the `type` of the query was `"descendantsHashes"`:

If no `item` event was yielded, then the storage doesn't contain a value at the given key.
- `key` is guaranteed to start with one of the `key`s provided.
- `hash` is a string containing the hexadecimal-encoded cryptographic hash of the storage entry.

No more event will be generated with this `subscription`.

### stop
### ClosestDescendantMerkleValue

```json
```
{
"event": "stop"
"key": "0x0000000...",
"closestDescendantMerkleValue": "0x000000..."
}
```

The `stop` event can be generated after a `waiting-for-continue` event in order to indicate that the JSON-RPC server can't continue. The JSON-RPC client should simply try again.
The JSON object corresponds to one of the requested items whose `type` was `"closestDescendantMerkleValue"`.

If the `key` doesn't exist in the trie, then the Merkle value of the closest descendant of `key` (including branch nodes) is provided. If `key` doesn't have any descendant in the trie, then no response is generated.

- `key` is guaranteed to be equal to one of the `key`s provided.
- `closestDescendantMerkleValue` is the closest trie Merkle value of the `key`.

The trie node whose Merkle value is indicated in `closestDescendantMerkleValue` is not indicated, as determining the key of this node might incur an overhead for the JSON-RPC server. The Merkle value is equal to either the node value or the hash of the node value, as defined in the [Polkadot specification](https://spec.polkadot.network/chap-state#defn-merkle-value).

No more event will be generated with this `subscription`.
## Possible errors

**Note**: This event is generated in very niche situations, such as a node doing a clean shutdown of all its active subscriptions before shutting down.
- A JSON-RPC error is generated if `type` isn't one of the allowed values (similarly to a missing parameter or an invalid parameter type).