@@ -23,8 +23,9 @@ use crate::{
2323 graph:: ValidateTransactionPriority ,
2424 insert_and_log_throttled, LOG_TARGET , LOG_TARGET_STAT ,
2525} ;
26+ use async_trait:: async_trait;
2627use codec:: Encode ;
27- use futures:: future:: { ready , Future , FutureExt , Ready } ;
28+ use futures:: future:: { Future , FutureExt } ;
2829use prometheus_endpoint:: Registry as PrometheusRegistry ;
2930use sc_client_api:: { blockchain:: HeaderBackend , BlockBackend } ;
3031use sp_api:: { ApiExt , ProvideRuntimeApi } ;
@@ -181,6 +182,7 @@ impl<Client, Block> FullChainApi<Client, Block> {
181182 }
182183}
183184
185+ #[ async_trait]
184186impl < Client , Block > graph:: ChainApi for FullChainApi < Client , Block >
185187where
186188 Block : BlockT ,
@@ -194,21 +196,21 @@ where
194196{
195197 type Block = Block ;
196198 type Error = error:: Error ;
197- type ValidationFuture =
198- Pin < Box < dyn Future < Output = error:: Result < TransactionValidity > > + Send > > ;
199- type BodyFuture = Ready < error:: Result < Option < Vec < <Self :: Block as BlockT >:: Extrinsic > > > > ;
200199
201- fn block_body ( & self , hash : Block :: Hash ) -> Self :: BodyFuture {
202- ready ( self . client . block_body ( hash) . map_err ( error:: Error :: from) )
200+ async fn block_body (
201+ & self ,
202+ hash : Block :: Hash ,
203+ ) -> Result < Option < Vec < <Self :: Block as BlockT >:: Extrinsic > > , Self :: Error > {
204+ self . client . block_body ( hash) . map_err ( error:: Error :: from)
203205 }
204206
205- fn validate_transaction (
207+ async fn validate_transaction (
206208 & self ,
207209 at : <Self :: Block as BlockT >:: Hash ,
208210 source : TransactionSource ,
209211 uxt : graph:: ExtrinsicFor < Self > ,
210212 validation_priority : ValidateTransactionPriority ,
211- ) -> Self :: ValidationFuture {
213+ ) -> Result < TransactionValidity , Self :: Error > {
212214 let start = Instant :: now ( ) ;
213215 let ( tx, rx) = oneshot:: channel ( ) ;
214216 let client = self . client . clone ( ) ;
@@ -228,39 +230,36 @@ where
228230 } ;
229231 let metrics = self . metrics . clone ( ) ;
230232
231- async move {
232- metrics. report ( |m| m. validations_scheduled . inc ( ) ) ;
233-
234- {
235- validation_pool
236- . send (
237- async move {
238- let res = validate_transaction_blocking ( & * client, at, source, uxt) ;
239- let _ = tx. send ( res) ;
240- metrics. report ( |m| m. validations_finished . inc ( ) ) ;
241- }
242- . boxed ( ) ,
243- )
244- . await
245- . map_err ( |e| Error :: RuntimeApi ( format ! ( "Validation pool down: {:?}" , e) ) ) ?;
246- }
233+ metrics. report ( |m| m. validations_scheduled . inc ( ) ) ;
247234
248- let validity = match rx. await {
249- Ok ( r) => r,
250- Err ( _) => Err ( Error :: RuntimeApi ( "Validation was canceled" . into ( ) ) ) ,
251- } ;
235+ {
236+ validation_pool
237+ . send (
238+ async move {
239+ let res = validate_transaction_blocking ( & * client, at, source, uxt) ;
240+ let _ = tx. send ( res) ;
241+ metrics. report ( |m| m. validations_finished . inc ( ) ) ;
242+ }
243+ . boxed ( ) ,
244+ )
245+ . await
246+ . map_err ( |e| Error :: RuntimeApi ( format ! ( "Validation pool down: {:?}" , e) ) ) ?;
247+ }
252248
253- insert_and_log_throttled ! (
254- Level :: DEBUG ,
255- target: LOG_TARGET_STAT ,
256- prefix: prefix,
257- stats,
258- start. elapsed( ) . into( )
259- ) ;
249+ let validity = match rx. await {
250+ Ok ( r) => r,
251+ Err ( _) => Err ( Error :: RuntimeApi ( "Validation was canceled" . into ( ) ) ) ,
252+ } ;
253+
254+ insert_and_log_throttled ! (
255+ Level :: DEBUG ,
256+ target: LOG_TARGET_STAT ,
257+ prefix: prefix,
258+ stats,
259+ start. elapsed( ) . into( )
260+ ) ;
260261
261- validity
262- }
263- . boxed ( )
262+ validity
264263 }
265264
266265 /// Validates a transaction by calling into the runtime.
@@ -271,21 +270,21 @@ where
271270 at : Block :: Hash ,
272271 source : TransactionSource ,
273272 uxt : graph:: ExtrinsicFor < Self > ,
274- ) -> error :: Result < TransactionValidity > {
273+ ) -> Result < TransactionValidity , Self :: Error > {
275274 validate_transaction_blocking ( & * self . client , at, source, uxt)
276275 }
277276
278277 fn block_id_to_number (
279278 & self ,
280279 at : & BlockId < Self :: Block > ,
281- ) -> error :: Result < Option < graph:: NumberFor < Self > > > {
280+ ) -> Result < Option < graph:: NumberFor < Self > > , Self :: Error > {
282281 self . client . to_number ( at) . map_err ( |e| Error :: BlockIdConversion ( e. to_string ( ) ) )
283282 }
284283
285284 fn block_id_to_hash (
286285 & self ,
287286 at : & BlockId < Self :: Block > ,
288- ) -> error :: Result < Option < graph:: BlockHash < Self > > > {
287+ ) -> Result < Option < graph:: BlockHash < Self > > , Self :: Error > {
289288 self . client . to_hash ( at) . map_err ( |e| Error :: BlockIdConversion ( e. to_string ( ) ) )
290289 }
291290
0 commit comments