Skip to content

Commit 833758f

Browse files
committed
pallet-ethereum StorageProvider access
1 parent 3e6c866 commit 833758f

File tree

1 file changed

+59
-38
lines changed

1 file changed

+59
-38
lines changed

rpc/src/eth.rs

+59-38
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ use sc_client_api::backend::{StorageProvider, Backend, StateBackend, AuxStore};
2929
use sha3::{Keccak256, Digest};
3030
use sp_runtime::traits::BlakeTwo256;
3131
use sp_blockchain::{Error as BlockChainError, HeaderMetadata, HeaderBackend};
32+
use sp_storage::StorageKey;
33+
use codec::Decode;
34+
use sp_io::hashing::twox_128;
3235
use frontier_rpc_core::{EthApi as EthApiT, NetApi as NetApiT};
3336
use frontier_rpc_core::types::{
3437
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> {
6770
}
6871
}
6972

73+
fn storage_prefix_build(module: &[u8], storage: &[u8]) -> Vec<u8> {
74+
[twox_128(module), twox_128(storage)].concat().to_vec()
75+
}
76+
7077
fn rich_block_build(
7178
block: ethereum::Block,
7279
statuses: Vec<Option<TransactionStatus>>,
@@ -246,6 +253,39 @@ impl<B, C, P, CT, BE, A> EthApi<B, C, P, CT, BE, A> where
246253
);
247254
(best_number, header_number)
248255
}
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+
}
249289
}
250290

251291
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
358398
return Ok(None);
359399
}
360400

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);
365403

366404
match (block, statuses) {
367405
(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
384422
None => return Ok(None),
385423
};
386424

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);
391427

392428
match (block, statuses) {
393429
(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
434470
return Ok(None);
435471
}
436472

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);
440474

441475
match block {
442476
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
450484
None => return Ok(None),
451485
};
452486

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);
456488

457489
match block {
458490
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
600632
return Ok(None);
601633
}
602634

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);
607637

608638
match (block, statuses) {
609639
(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
634664
}
635665
let index = index.value();
636666

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);
641669

642670
match (block, statuses) {
643671
(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
662690
};
663691
let index = index.value();
664692

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);
669695

670696
match (block, statuses) {
671697
(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
699725
return Ok(None);
700726
}
701727

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);
708731

709732
match (block, statuses, receipts) {
710733
(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
804827
return Ok(Vec::new());
805828
}
806829

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);
810832

811833
if let (Some(block), Some(statuses)) = (block, statuses) {
812834
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
828850
while current_number >= from_number {
829851
let id = BlockId::Number(current_number);
830852

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);
834855

835856
if let (Some(block), Some(statuses)) = (block, statuses) {
836857
blocks_and_statuses.push((block, statuses));

0 commit comments

Comments
 (0)