Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit f68d22f

Browse files
authored
Switch to new light client protocol (#5472)
* Switch to the new protocol * Oops, forgot to remove light_dispatch.rs * Fix tests * Address review
1 parent 905677d commit f68d22f

File tree

7 files changed

+481
-1682
lines changed

7 files changed

+481
-1682
lines changed

client/network/src/behaviour.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,6 @@ impl<B: BlockT, H: ExHashT> Behaviour<B, H> {
133133
}
134134

135135
/// Issue a light client request.
136-
#[allow(unused)]
137136
pub fn light_client_request(&mut self, r: light_client_handler::Request<B>) -> Result<(), light_client_handler::Error> {
138137
self.light_client_handler.request(r)
139138
}
@@ -175,6 +174,9 @@ Behaviour<B, H> {
175174
let ev = Event::NotificationsReceived { remote, messages };
176175
self.events.push(BehaviourOut::Event(ev));
177176
},
177+
CustomMessageOutcome::PeerNewBest(peer_id, number) => {
178+
self.light_client_handler.update_best_block(&peer_id, number);
179+
}
178180
CustomMessageOutcome::None => {}
179181
}
180182
}

client/network/src/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
//! See the documentation of [`Params`].
2121
2222
pub use crate::chain::{Client, FinalityProofProvider};
23-
pub use crate::on_demand_layer::OnDemand;
23+
pub use crate::on_demand_layer::{AlwaysBadChecker, OnDemand};
2424
pub use crate::service::{TransactionPool, EmptyTransactionPool};
2525
pub use libp2p::{identity, core::PublicKey, wasm_ext::ExtTransport, build_multiaddr};
2626

client/network/src/on_demand_layer.rs

Lines changed: 97 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,17 @@
1616

1717
//! On-demand requests service.
1818
19-
use crate::protocol::light_dispatch::RequestData;
20-
use std::{collections::HashMap, pin::Pin, sync::Arc, task::Context, task::Poll};
21-
use futures::{prelude::*, channel::mpsc, channel::oneshot};
19+
use crate::protocol::light_client_handler;
20+
21+
use futures::{channel::mpsc, channel::oneshot, prelude::*};
2222
use parking_lot::Mutex;
23-
use sp_blockchain::Error as ClientError;
2423
use sc_client_api::{
25-
Fetcher, FetchChecker, RemoteHeaderRequest, RemoteCallRequest, RemoteReadRequest,
26-
RemoteChangesRequest, RemoteReadChildRequest, RemoteBodyRequest,
24+
FetchChecker, Fetcher, RemoteBodyRequest, RemoteCallRequest, RemoteChangesRequest,
25+
RemoteHeaderRequest, RemoteReadChildRequest, RemoteReadRequest, StorageProof, ChangesProof,
2726
};
27+
use sp_blockchain::Error as ClientError;
2828
use sp_runtime::traits::{Block as BlockT, Header as HeaderT, NumberFor};
29+
use std::{collections::HashMap, pin::Pin, sync::Arc, task::Context, task::Poll};
2930

3031
/// Implements the `Fetcher` trait of the client. Makes it possible for the light client to perform
3132
/// network requests for some state.
@@ -41,13 +42,72 @@ pub struct OnDemand<B: BlockT> {
4142
/// Note that a better alternative would be to use a MPMC queue here, and add a `poll` method
4243
/// from the `OnDemand`. However there exists no popular implementation of MPMC channels in
4344
/// asynchronous Rust at the moment
44-
requests_queue: Mutex<Option<mpsc::UnboundedReceiver<RequestData<B>>>>,
45+
requests_queue: Mutex<Option<mpsc::UnboundedReceiver<light_client_handler::Request<B>>>>,
4546

4647
/// Sending side of `requests_queue`.
47-
requests_send: mpsc::UnboundedSender<RequestData<B>>,
48+
requests_send: mpsc::UnboundedSender<light_client_handler::Request<B>>,
49+
}
50+
51+
/// Dummy implementation of `FetchChecker` that always assumes that responses are bad.
52+
///
53+
/// Considering that it is the responsibility of the client to build the fetcher, it can use this
54+
/// implementation if it knows that it will never perform any request.
55+
#[derive(Default, Clone)]
56+
pub struct AlwaysBadChecker;
57+
58+
impl<Block: BlockT> FetchChecker<Block> for AlwaysBadChecker {
59+
fn check_header_proof(
60+
&self,
61+
_request: &RemoteHeaderRequest<Block::Header>,
62+
_remote_header: Option<Block::Header>,
63+
_remote_proof: StorageProof,
64+
) -> Result<Block::Header, ClientError> {
65+
Err(ClientError::Msg("AlwaysBadChecker".into()))
66+
}
67+
68+
fn check_read_proof(
69+
&self,
70+
_request: &RemoteReadRequest<Block::Header>,
71+
_remote_proof: StorageProof,
72+
) -> Result<HashMap<Vec<u8>,Option<Vec<u8>>>, ClientError> {
73+
Err(ClientError::Msg("AlwaysBadChecker".into()))
74+
}
75+
76+
fn check_read_child_proof(
77+
&self,
78+
_request: &RemoteReadChildRequest<Block::Header>,
79+
_remote_proof: StorageProof,
80+
) -> Result<HashMap<Vec<u8>, Option<Vec<u8>>>, ClientError> {
81+
Err(ClientError::Msg("AlwaysBadChecker".into()))
82+
}
83+
84+
fn check_execution_proof(
85+
&self,
86+
_request: &RemoteCallRequest<Block::Header>,
87+
_remote_proof: StorageProof,
88+
) -> Result<Vec<u8>, ClientError> {
89+
Err(ClientError::Msg("AlwaysBadChecker".into()))
90+
}
91+
92+
fn check_changes_proof(
93+
&self,
94+
_request: &RemoteChangesRequest<Block::Header>,
95+
_remote_proof: ChangesProof<Block::Header>
96+
) -> Result<Vec<(NumberFor<Block>, u32)>, ClientError> {
97+
Err(ClientError::Msg("AlwaysBadChecker".into()))
98+
}
99+
100+
fn check_body_proof(
101+
&self,
102+
_request: &RemoteBodyRequest<Block::Header>,
103+
_body: Vec<Block::Extrinsic>
104+
) -> Result<Vec<Block::Extrinsic>, ClientError> {
105+
Err(ClientError::Msg("AlwaysBadChecker".into()))
106+
}
48107
}
49108

50-
impl<B: BlockT> OnDemand<B> where
109+
impl<B: BlockT> OnDemand<B>
110+
where
51111
B::Header: HeaderT,
52112
{
53113
/// Creates new on-demand service.
@@ -74,12 +134,15 @@ impl<B: BlockT> OnDemand<B> where
74134
///
75135
/// If this function returns `None`, that means that the receiver has already been extracted in
76136
/// the past, and therefore that something already handles the requests.
77-
pub(crate) fn extract_receiver(&self) -> Option<mpsc::UnboundedReceiver<RequestData<B>>> {
137+
pub(crate) fn extract_receiver(
138+
&self,
139+
) -> Option<mpsc::UnboundedReceiver<light_client_handler::Request<B>>> {
78140
self.requests_queue.lock().take()
79141
}
80142
}
81143

82-
impl<B> Fetcher<B> for OnDemand<B> where
144+
impl<B> Fetcher<B> for OnDemand<B>
145+
where
83146
B: BlockT,
84147
B::Header: HeaderT,
85148
{
@@ -91,40 +154,55 @@ impl<B> Fetcher<B> for OnDemand<B> where
91154

92155
fn remote_header(&self, request: RemoteHeaderRequest<B::Header>) -> Self::RemoteHeaderResult {
93156
let (sender, receiver) = oneshot::channel();
94-
let _ = self.requests_send.unbounded_send(RequestData::RemoteHeader(request, sender));
157+
let _ = self
158+
.requests_send
159+
.unbounded_send(light_client_handler::Request::Header { request, sender });
95160
RemoteResponse { receiver }
96161
}
97162

98163
fn remote_read(&self, request: RemoteReadRequest<B::Header>) -> Self::RemoteReadResult {
99164
let (sender, receiver) = oneshot::channel();
100-
let _ = self.requests_send.unbounded_send(RequestData::RemoteRead(request, sender));
165+
let _ = self
166+
.requests_send
167+
.unbounded_send(light_client_handler::Request::Read { request, sender });
101168
RemoteResponse { receiver }
102169
}
103170

104171
fn remote_read_child(
105172
&self,
106-
request: RemoteReadChildRequest<B::Header>
173+
request: RemoteReadChildRequest<B::Header>,
107174
) -> Self::RemoteReadResult {
108175
let (sender, receiver) = oneshot::channel();
109-
let _ = self.requests_send.unbounded_send(RequestData::RemoteReadChild(request, sender));
176+
let _ = self
177+
.requests_send
178+
.unbounded_send(light_client_handler::Request::ReadChild { request, sender });
110179
RemoteResponse { receiver }
111180
}
112181

113182
fn remote_call(&self, request: RemoteCallRequest<B::Header>) -> Self::RemoteCallResult {
114183
let (sender, receiver) = oneshot::channel();
115-
let _ = self.requests_send.unbounded_send(RequestData::RemoteCall(request, sender));
184+
let _ = self
185+
.requests_send
186+
.unbounded_send(light_client_handler::Request::Call { request, sender });
116187
RemoteResponse { receiver }
117188
}
118189

119-
fn remote_changes(&self, request: RemoteChangesRequest<B::Header>) -> Self::RemoteChangesResult {
190+
fn remote_changes(
191+
&self,
192+
request: RemoteChangesRequest<B::Header>,
193+
) -> Self::RemoteChangesResult {
120194
let (sender, receiver) = oneshot::channel();
121-
let _ = self.requests_send.unbounded_send(RequestData::RemoteChanges(request, sender));
195+
let _ = self
196+
.requests_send
197+
.unbounded_send(light_client_handler::Request::Changes { request, sender });
122198
RemoteResponse { receiver }
123199
}
124200

125201
fn remote_body(&self, request: RemoteBodyRequest<B::Header>) -> Self::RemoteBodyResult {
126202
let (sender, receiver) = oneshot::channel();
127-
let _ = self.requests_send.unbounded_send(RequestData::RemoteBody(request, sender));
203+
let _ = self
204+
.requests_send
205+
.unbounded_send(light_client_handler::Request::Body { request, sender });
128206
RemoteResponse { receiver }
129207
}
130208
}

0 commit comments

Comments
 (0)