network: Use "one shot" protocol handler. (#3520)

* network: Use "one shot" protocol handler.

Add two new `NetworkBehaviour`s, one handling remote block requests
and another one to handle light client requests (both local and from
remote). The change is motivated by the desire to use multiple
substreams of a single connection for different protocols. To achieve
this, libp2p's `OneShotHandler` is used as a protocol handler in each
behaviour. It will open a fresh substream for the duration of the
request and close it afterwards. For block requests, we currently only
handle incoming requests from remote and tests are missing. For light
client handling we support incoming requests from remote and also
ported a substantial amount of functionality over from
`light_dispatch.rs` (including several tests). However the result lacks
in at least two aspects:

(1) We require external updates w.r.t. the best block per peer and
currently nothing updates this information.
(2) We carry a lot of peer-related state around.

Both aspects could be simplified by externalising peer selection and
just requiring a specific peer ID where the request should be sent to.
We still have to maintain some peer related state due to the way
libp2p's swarm and network behaviour work (e.g. we must make sure to
always issue `NetworkBehaviourAction::SendEvent`s to peers we are
connected to, otherwise the actions die a silent death.

Another change implemented here is the use of protocol buffers as the
encoding for network messages. Certain individual fields of messages
are still SCALE encoded. There has been some discussion about this
in another PR (https://github.com/paritytech/substrate/pull/3452), so
far without resolution.

* Uncomment `Behaviour::light_client_request`.

* Add license headers.
This commit is contained in:
Toralf Wittner
2020-02-12 11:50:52 +01:00
committed by GitHub
parent 173644c8b9
commit 51a45c5d9f
11 changed files with 2436 additions and 27 deletions
@@ -0,0 +1,59 @@
// Schema definition for block request/response messages.
syntax = "proto3";
package api.v1;
// Block enumeration direction.
enum Direction {
// Enumerate in ascending order (from child to parent).
Ascending = 0;
// Enumerate in descendfing order (from parent to canonical child).
Descending = 1;
}
// Request block data from a peer.
message BlockRequest {
// Unique request id.
uint64 id = 1;
// Bits of block data to request.
uint32 fields = 2;
// Start from this block.
oneof from_block {
// Start with given hash.
bytes hash = 3;
// Start with given block number.
bytes number = 4;
}
// End at this block. An implementation defined maximum is used when unspecified.
bytes to_block = 5; // optional
// Sequence direction.
Direction direction = 6;
// Maximum number of blocks to return. An implementation defined maximum is used when unspecified.
uint32 max_blocks = 7; // optional
}
// Response to `BlockRequest`
message BlockResponse {
// Id of a request this response was made for.
uint64 id = 1;
// Block data for the requested sequence.
repeated BlockData blocks = 2;
}
// Block data sent in the response.
message BlockData {
// Block header hash.
bytes hash = 1;
// Block header if requested.
bytes header = 2; // optional
// Block body if requested.
repeated bytes body = 3; // optional
// Block receipt if requested.
bytes receipt = 4; // optional
// Block message queue if requested.
bytes message_queue = 5; // optional
// Justification if requested.
bytes justification = 6; // optional
}
@@ -0,0 +1,128 @@
// Schema definition for light client messages.
syntax = "proto3";
package api.v1.light;
// A pair of arbitrary bytes.
message Pair {
// The first element of the pair.
bytes fst = 1;
// The second element of the pair.
bytes snd = 2;
}
// Enumerate all possible light client request messages.
message Request {
// Unique request id.
uint64 id = 1;
oneof request {
RemoteCallRequest remote_call_request = 2;
RemoteReadRequest remote_read_request = 3;
RemoteHeaderRequest remote_header_request = 4;
RemoteReadChildRequest remote_read_child_request = 5;
RemoteChangesRequest remote_changes_request = 6;
}
}
// Enumerate all possible light client response messages.
message Response {
/// Id of a request this response was made for.
uint64 id = 1;
oneof response {
RemoteCallResponse remote_call_response = 2;
RemoteReadResponse remote_read_response = 3;
RemoteHeaderResponse remote_header_response = 4;
RemoteChangesResponse remote_changes_response = 6;
}
}
// Remote call request.
message RemoteCallRequest {
// Block at which to perform call.
bytes block = 2;
// Method name.
string method = 3;
// Call data.
bytes data = 4;
}
// Remote call response.
message RemoteCallResponse {
// Execution proof.
bytes proof = 2;
}
// Remote storage read request.
message RemoteReadRequest {
// Block at which to perform call.
bytes block = 2;
// Storage keys.
repeated bytes keys = 3;
}
// Remote read response.
message RemoteReadResponse {
// Read proof.
bytes proof = 2;
}
// Remote storage read child request.
message RemoteReadChildRequest {
// Block at which to perform call.
bytes block = 2;
// Child Storage key.
bytes storage_key = 3;
// Child trie source information.
bytes child_info = 4;
/// Child type, its required to resolve `child_info`
/// content and choose child implementation.
uint32 child_type = 5;
// Storage keys.
repeated bytes keys = 6;
}
// Remote header request.
message RemoteHeaderRequest {
// Block number to request header for.
bytes block = 2;
}
// Remote header response.
message RemoteHeaderResponse {
// Header. None if proof generation has failed (e.g. header is unknown).
bytes header = 2; // optional
// Header proof.
bytes proof = 3;
}
/// Remote changes request.
message RemoteChangesRequest {
// Hash of the first block of the range (including first) where changes are requested.
bytes first = 2;
// Hash of the last block of the range (including last) where changes are requested.
bytes last = 3;
// Hash of the first block for which the requester has the changes trie root. All other
// affected roots must be proved.
bytes min = 4;
// Hash of the last block that we can use when querying changes.
bytes max = 5;
// Storage child node key which changes are requested.
bytes storage_key = 6; // optional
// Storage key which changes are requested.
bytes key = 7;
}
// Remote changes response.
message RemoteChangesResponse {
// Proof has been generated using block with this number as a max block. Should be
// less than or equal to the RemoteChangesRequest::max block number.
bytes max = 2;
// Changes proof.
repeated bytes proof = 3;
// Changes tries roots missing on the requester' node.
repeated Pair roots = 4;
// Missing changes tries roots proof.
bytes roots_proof = 5;
}