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 :: * } ;
2222use parking_lot:: Mutex ;
23- use sp_blockchain:: Error as ClientError ;
2423use 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 ;
2828use 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