Skip to content

Commit 6daf453

Browse files
authored
Merge pull request #94 from paritytech/lexnv/archive_backport
archive/storage: Backport API from chainHead
2 parents 458902a + e894512 commit 6daf453

File tree

3 files changed

+75
-63
lines changed

3 files changed

+75
-63
lines changed

src/SUMMARY.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
- [archive_unstable_hashByHeight](api/archive_unstable_hashByHeight.md)
1616
- [archive_unstable_header](api/archive_unstable_header.md)
1717
- [archive_unstable_storage](api/archive_unstable_storage.md)
18-
- [archive_unstable_storageContinue](api/archive_unstable_storageContinue.md)
1918
- [chainHead](api/chainHead.md)
2019
- [chainHead_unstable_body](api/chainHead_unstable_body.md)
2120
- [chainHead_unstable_call](api/chainHead_unstable_call.md)

src/api/archive_unstable_storage.md

Lines changed: 75 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -3,91 +3,119 @@
33
**Parameters**:
44

55
- `hash`: String containing a hexadecimal-encoded hash of the header of the block whose storage to fetch.
6-
- `key`: String containing the hexadecimal-encoded key to fetch in the storage.
6+
- `items`: Array of objects. The structure of these objects is found below.
77
- `childTrie`: `null` for main storage look-ups, or a string containing the hexadecimal-encoded key of the child trie of the "default" namespace.
8-
- `includeDescendants`: Boolean indicating whether the key-values of all the descendants of the `key` should be returned as well.
9-
10-
**Return value**: String containing an opaque value representing the operation, or `null` if no block with that `hash` exists.
118

12-
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.
9+
Each element in `items` must be an object containing the following fields:
1310

14-
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.
15-
16-
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.
11+
- `key`: String containing the hexadecimal-encoded key to fetch in the storage.
12+
- `type`: String equal to one of: `value`, `hash`, `closestDescendantMerkleValue`, `descendantsValues`, `descendantsHashes`.
13+
- `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`.
1714

18-
## Notifications format
15+
**Return value**: A JSON object.
1916

20-
This function will later generate notifications in the following format:
17+
The JSON object returned by this function has the following format:
2118

22-
```json
19+
```
2320
{
24-
"jsonrpc": "2.0",
25-
"method": "archive_unstable_storageEvent",
26-
"params": {
27-
"subscription": "...",
28-
"result": ...
29-
}
21+
"result": [
22+
{
23+
...
24+
},
25+
...
26+
],
27+
"discardedItems": ...
3028
}
3129
```
3230

33-
Where `subscription` is equal to the value returned by this function, and `result` can be one of:
31+
Where:
32+
33+
- `result` contains a vector of JSON objects (possibly empty) that were found in the storage.
34+
- `discardedItems` is an integer indicating the number of items at the back of the array of the `items` parameters that couldn't be processed.
35+
36+
The JSON objects in the `"result"` field can have one of the following formats based on their type:
3437

35-
### item
38+
### Value
3639

37-
```json
40+
```
3841
{
39-
"event": "item",
4042
"key": "0x0000000...",
4143
"value": "0x0000000...",
4244
}
4345
```
4446

45-
Yields an item that was found in the storage.
47+
The JSON object corresponds to one of the requested items whose `type` was `"value"` or `"descendantsValues"`.
48+
49+
If the `key` is not associated with a storage value in the trie, then no response is generated in the `"result"` vector for this item.
50+
51+
Returned when the `type` of the query was `"value"`:
52+
53+
- `key` is guaranteed to be equal to one of the `key`s provided.
54+
- `value` is a string containing the hexadecimal-encoded value of the storage entry.
4655

47-
The `key` field is a string containing the hexadecimal-encoded key of the value that was found.
48-
If the `includeDescendants` parameter was `true`, this `key` is guaranteed to start with the `key` provided as parameter.
49-
If the `includeDescendants` parameter was `false`, then it is also guaranteed to be equal to the `key` provided as parameter.
56+
Returned when the `type` of the query was `"descendantsValues"`:
5057

51-
The `value` field is a string containing the hexadecimal-encoded value of the storage item.
58+
- `key` is guaranteed to start with one of the `key`s provided.
59+
- `value` is a string containing the hexadecimal-encoded value of the storage entry.
5260

53-
### waiting-for-continue
61+
### Hash
5462

55-
```json
63+
```
5664
{
57-
"event": "waiting-for-continue"
65+
"key": "0x0000000...",
66+
"hash": "0x0000000...",
5867
}
5968
```
6069

61-
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.
70+
The JSON object corresponds to one of the requested items whose `type` was `"hash"` or `"descendantsHashes"`.
71+
72+
If the `key` is not associated with a storage value in the trie, then no response is generated in the `"result"` vector for this item.
6273

63-
This event only ever happens if the `includeDescendants` parameter was `true`.
74+
Returned when the `type` of the query was `"hash"`:
6475

65-
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.
76+
- `key` is guaranteed to be equal to one of the `key`s provided.
77+
- `hash` is a string containing the hexadecimal-encoded hash of the storage entry.
6678

67-
### done
79+
Returned when the `type` of the query was `"descendantsHashes"`:
6880

69-
```json
81+
- `key` is guaranteed to start with one of the `key`s provided.
82+
- `hash` is a string containing the hexadecimal-encoded cryptographic hash of the storage entry.
83+
84+
85+
### ClosestDescendantMerkleValue
86+
87+
```
7088
{
71-
"event": "done"
89+
"key": "0x0000000...",
90+
"closestDescendantMerkleValue": "0x000000..."
7291
}
7392
```
7493

75-
The `done` event indicates that everything went well and all values have been provided through `item` events in the past.
94+
The JSON object corresponds to one of the requested items whose `type` was `"closestDescendantMerkleValue"`.
7695

77-
If no `item` event was yielded, then the storage doesn't contain a value at the given key.
96+
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 in the `"result"` vector for this item.
7897

79-
No more event will be generated with this `subscription`.
98+
- `key` is guaranteed to be equal to one of the `key`s provided.
99+
- `closestDescendantMerkleValue` is the closest trie Merkle value of the `key`.
80100

81-
### stop
101+
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).
82102

83-
```json
84-
{
85-
"event": "stop"
86-
}
87-
```
103+
## Overview
104+
105+
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.
106+
107+
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.
108+
109+
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.
110+
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.
111+
112+
This function should be used when the target block is older than the blocks reported by `chainHead_unstable_follow`.
113+
Use `chainHead_unstable_storage` if instead you want to retrieve the storage of a block obtained by the `chainHead_unstable_follow`.
114+
115+
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.
88116

89-
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.
117+
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.
90118

91-
No more event will be generated with this `subscription`.
119+
## Possible errors
92120

93-
**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.
121+
- 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).

src/api/archive_unstable_storageContinue.md

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)