@@ -29,6 +29,9 @@ use sc_client_api::backend::{StorageProvider, Backend, StateBackend, AuxStore};
29
29
use sha3:: { Keccak256 , Digest } ;
30
30
use sp_runtime:: traits:: BlakeTwo256 ;
31
31
use sp_blockchain:: { Error as BlockChainError , HeaderMetadata , HeaderBackend } ;
32
+ use sp_storage:: StorageKey ;
33
+ use codec:: Decode ;
34
+ use sp_io:: hashing:: twox_128;
32
35
use frontier_rpc_core:: { EthApi as EthApiT , NetApi as NetApiT } ;
33
36
use frontier_rpc_core:: types:: {
34
37
BlockNumber , Bytes , CallRequest , Filter , Index , Log , Receipt , RichBlock ,
@@ -67,6 +70,10 @@ impl<B: BlockT, C, P, CT, BE, A: ChainApi> EthApi<B, C, P, CT, BE, A> {
67
70
}
68
71
}
69
72
73
+ fn storage_prefix_build ( module : & [ u8 ] , storage : & [ u8 ] ) -> Vec < u8 > {
74
+ [ twox_128 ( module) , twox_128 ( storage) ] . concat ( ) . to_vec ( )
75
+ }
76
+
70
77
fn rich_block_build (
71
78
block : ethereum:: Block ,
72
79
statuses : Vec < Option < TransactionStatus > > ,
@@ -246,6 +253,39 @@ impl<B, C, P, CT, BE, A> EthApi<B, C, P, CT, BE, A> where
246
253
) ;
247
254
( best_number, header_number)
248
255
}
256
+
257
+ fn current_block ( & self , id : & BlockId < B > ) -> Option < ethereum:: Block > {
258
+ if let Ok ( Some ( block_data) ) = self . client . storage (
259
+ & id,
260
+ & StorageKey (
261
+ storage_prefix_build ( b"Ethereum" , b"CurrentBlock" )
262
+ )
263
+ ) {
264
+ return Decode :: decode ( & mut & block_data. 0 [ ..] ) . unwrap_or_else ( |_| None ) ;
265
+ } else { return None ; } ;
266
+ }
267
+
268
+ fn current_statuses ( & self , id : & BlockId < B > ) -> Option < Vec < TransactionStatus > > {
269
+ if let Ok ( Some ( status_data) ) = self . client . storage (
270
+ & id,
271
+ & StorageKey (
272
+ storage_prefix_build ( b"Ethereum" , b"CurrentTransactionStatuses" )
273
+ )
274
+ ) {
275
+ return Decode :: decode ( & mut & status_data. 0 [ ..] ) . unwrap_or_else ( |_| None ) ;
276
+ } else { return None ; } ;
277
+ }
278
+
279
+ fn current_receipts ( & self , id : & BlockId < B > ) -> Option < Vec < ethereum:: Receipt > > {
280
+ if let Ok ( Some ( status_data) ) = self . client . storage (
281
+ & id,
282
+ & StorageKey (
283
+ storage_prefix_build ( b"Ethereum" , b"CurrentReceipts" )
284
+ )
285
+ ) {
286
+ return Decode :: decode ( & mut & status_data. 0 [ ..] ) . unwrap_or_else ( |_| None ) ;
287
+ } else { return None ; } ;
288
+ }
249
289
}
250
290
251
291
impl < B , C , P , CT , BE , A > EthApiT for EthApi < B , C , P , CT , BE , A > where
@@ -358,10 +398,8 @@ impl<B, C, P, CT, BE, A> EthApiT for EthApi<B, C, P, CT, BE, A> where
358
398
return Ok ( None ) ;
359
399
}
360
400
361
- let block = self . client . runtime_api ( ) . current_block ( & id)
362
- . map_err ( |err| internal_err ( format ! ( "call runtime failed: {:?}" , err) ) ) ?;
363
- let statuses = self . client . runtime_api ( ) . current_transaction_statuses ( & id)
364
- . map_err ( |err| internal_err ( format ! ( "call runtime failed: {:?}" , err) ) ) ?;
401
+ let block: Option < ethereum:: Block > = self . current_block ( & id) ;
402
+ let statuses: Option < Vec < TransactionStatus > > = self . current_statuses ( & id) ;
365
403
366
404
match ( block, statuses) {
367
405
( Some ( block) , Some ( statuses) ) => {
@@ -384,10 +422,8 @@ impl<B, C, P, CT, BE, A> EthApiT for EthApi<B, C, P, CT, BE, A> where
384
422
None => return Ok ( None ) ,
385
423
} ;
386
424
387
- let block = self . client . runtime_api ( ) . current_block ( & id)
388
- . map_err ( |err| internal_err ( format ! ( "call runtime failed: {:?}" , err) ) ) ?;
389
- let statuses = self . client . runtime_api ( ) . current_transaction_statuses ( & id)
390
- . map_err ( |err| internal_err ( format ! ( "call runtime failed: {:?}" , err) ) ) ?;
425
+ let block: Option < ethereum:: Block > = self . current_block ( & id) ;
426
+ let statuses: Option < Vec < TransactionStatus > > = self . current_statuses ( & id) ;
391
427
392
428
match ( block, statuses) {
393
429
( Some ( block) , Some ( statuses) ) => {
@@ -434,9 +470,7 @@ impl<B, C, P, CT, BE, A> EthApiT for EthApi<B, C, P, CT, BE, A> where
434
470
return Ok ( None ) ;
435
471
}
436
472
437
- let block = self . client . runtime_api ( )
438
- . current_block ( & id)
439
- . map_err ( |err| internal_err ( format ! ( "fetch runtime account basic failed: {:?}" , err) ) ) ?;
473
+ let block: Option < ethereum:: Block > = self . current_block ( & id) ;
440
474
441
475
match block {
442
476
Some ( block) => Ok ( Some ( U256 :: from ( block. transactions . len ( ) ) ) ) ,
@@ -450,9 +484,7 @@ impl<B, C, P, CT, BE, A> EthApiT for EthApi<B, C, P, CT, BE, A> where
450
484
None => return Ok ( None ) ,
451
485
} ;
452
486
453
- let block = self . client . runtime_api ( )
454
- . current_block ( & id)
455
- . map_err ( |err| internal_err ( format ! ( "fetch runtime account basic failed: {:?}" , err) ) ) ?;
487
+ let block: Option < ethereum:: Block > = self . current_block ( & id) ;
456
488
457
489
match block {
458
490
Some ( block) => Ok ( Some ( U256 :: from ( block. transactions . len ( ) ) ) ) ,
@@ -600,10 +632,8 @@ impl<B, C, P, CT, BE, A> EthApiT for EthApi<B, C, P, CT, BE, A> where
600
632
return Ok ( None ) ;
601
633
}
602
634
603
- let block = self . client . runtime_api ( ) . current_block ( & id)
604
- . map_err ( |err| internal_err ( format ! ( "call runtime failed: {:?}" , err) ) ) ?;
605
- let statuses = self . client . runtime_api ( ) . current_transaction_statuses ( & id)
606
- . map_err ( |err| internal_err ( format ! ( "call runtime failed: {:?}" , err) ) ) ?;
635
+ let block: Option < ethereum:: Block > = self . current_block ( & id) ;
636
+ let statuses: Option < Vec < TransactionStatus > > = self . current_statuses ( & id) ;
607
637
608
638
match ( block, statuses) {
609
639
( Some ( block) , Some ( statuses) ) => {
@@ -634,10 +664,8 @@ impl<B, C, P, CT, BE, A> EthApiT for EthApi<B, C, P, CT, BE, A> where
634
664
}
635
665
let index = index. value ( ) ;
636
666
637
- let block = self . client . runtime_api ( ) . current_block ( & id)
638
- . map_err ( |err| internal_err ( format ! ( "call runtime failed: {:?}" , err) ) ) ?;
639
- let statuses = self . client . runtime_api ( ) . current_transaction_statuses ( & id)
640
- . map_err ( |err| internal_err ( format ! ( "call runtime failed: {:?}" , err) ) ) ?;
667
+ let block: Option < ethereum:: Block > = self . current_block ( & id) ;
668
+ let statuses: Option < Vec < TransactionStatus > > = self . current_statuses ( & id) ;
641
669
642
670
match ( block, statuses) {
643
671
( Some ( block) , Some ( statuses) ) => {
@@ -662,10 +690,8 @@ impl<B, C, P, CT, BE, A> EthApiT for EthApi<B, C, P, CT, BE, A> where
662
690
} ;
663
691
let index = index. value ( ) ;
664
692
665
- let block = self . client . runtime_api ( ) . current_block ( & id)
666
- . map_err ( |err| internal_err ( format ! ( "call runtime failed: {:?}" , err) ) ) ?;
667
- let statuses = self . client . runtime_api ( ) . current_transaction_statuses ( & id)
668
- . map_err ( |err| internal_err ( format ! ( "call runtime failed: {:?}" , err) ) ) ?;
693
+ let block: Option < ethereum:: Block > = self . current_block ( & id) ;
694
+ let statuses: Option < Vec < TransactionStatus > > = self . current_statuses ( & id) ;
669
695
670
696
match ( block, statuses) {
671
697
( Some ( block) , Some ( statuses) ) => {
@@ -699,12 +725,9 @@ impl<B, C, P, CT, BE, A> EthApiT for EthApi<B, C, P, CT, BE, A> where
699
725
return Ok ( None ) ;
700
726
}
701
727
702
- let block = self . client . runtime_api ( ) . current_block ( & id)
703
- . map_err ( |err| internal_err ( format ! ( "call runtime failed: {:?}" , err) ) ) ?;
704
- let receipts = self . client . runtime_api ( ) . current_receipts ( & id)
705
- . map_err ( |err| internal_err ( format ! ( "call runtime failed: {:?}" , err) ) ) ?;
706
- let statuses = self . client . runtime_api ( ) . current_transaction_statuses ( & id)
707
- . map_err ( |err| internal_err ( format ! ( "call runtime failed: {:?}" , err) ) ) ?;
728
+ let block: Option < ethereum:: Block > = self . current_block ( & id) ;
729
+ let statuses: Option < Vec < TransactionStatus > > = self . current_statuses ( & id) ;
730
+ let receipts: Option < Vec < ethereum:: Receipt > > = self . current_receipts ( & id) ;
708
731
709
732
match ( block, statuses, receipts) {
710
733
( Some ( block) , Some ( statuses) , Some ( receipts) ) => {
@@ -804,9 +827,8 @@ impl<B, C, P, CT, BE, A> EthApiT for EthApi<B, C, P, CT, BE, A> where
804
827
return Ok ( Vec :: new ( ) ) ;
805
828
}
806
829
807
- let ( block, _, statuses) = self . client . runtime_api ( )
808
- . current_all ( & id)
809
- . map_err ( |err| internal_err ( format ! ( "fetch runtime account basic failed: {:?}" , err) ) ) ?;
830
+ let block: Option < ethereum:: Block > = self . current_block ( & id) ;
831
+ let statuses: Option < Vec < TransactionStatus > > = self . current_statuses ( & id) ;
810
832
811
833
if let ( Some ( block) , Some ( statuses) ) = ( block, statuses) {
812
834
blocks_and_statuses. push ( ( block, statuses) ) ;
@@ -828,9 +850,8 @@ impl<B, C, P, CT, BE, A> EthApiT for EthApi<B, C, P, CT, BE, A> where
828
850
while current_number >= from_number {
829
851
let id = BlockId :: Number ( current_number) ;
830
852
831
- let ( block, _, statuses) = self . client . runtime_api ( )
832
- . current_all ( & id)
833
- . map_err ( |err| internal_err ( format ! ( "fetch runtime account basic failed: {:?}" , err) ) ) ?;
853
+ let block: Option < ethereum:: Block > = self . current_block ( & id) ;
854
+ let statuses: Option < Vec < TransactionStatus > > = self . current_statuses ( & id) ;
834
855
835
856
if let ( Some ( block) , Some ( statuses) ) = ( block, statuses) {
836
857
blocks_and_statuses. push ( ( block, statuses) ) ;
0 commit comments