Skip to content

Commit

Permalink
pallet-ethereum StorageProvider access
Browse files Browse the repository at this point in the history
  • Loading branch information
tgmichel committed Oct 19, 2020
1 parent 3e6c866 commit 833758f
Showing 1 changed file with 59 additions and 38 deletions.
97 changes: 59 additions & 38 deletions rpc/src/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ use sc_client_api::backend::{StorageProvider, Backend, StateBackend, AuxStore};
use sha3::{Keccak256, Digest};
use sp_runtime::traits::BlakeTwo256;
use sp_blockchain::{Error as BlockChainError, HeaderMetadata, HeaderBackend};
use sp_storage::StorageKey;
use codec::Decode;
use sp_io::hashing::twox_128;
use frontier_rpc_core::{EthApi as EthApiT, NetApi as NetApiT};
use frontier_rpc_core::types::{
BlockNumber, Bytes, CallRequest, Filter, Index, Log, Receipt, RichBlock,
Expand Down Expand Up @@ -67,6 +70,10 @@ impl<B: BlockT, C, P, CT, BE, A: ChainApi> EthApi<B, C, P, CT, BE, A> {
}
}

fn storage_prefix_build(module: &[u8], storage: &[u8]) -> Vec<u8> {
[twox_128(module), twox_128(storage)].concat().to_vec()
}

fn rich_block_build(
block: ethereum::Block,
statuses: Vec<Option<TransactionStatus>>,
Expand Down Expand Up @@ -246,6 +253,39 @@ impl<B, C, P, CT, BE, A> EthApi<B, C, P, CT, BE, A> where
);
(best_number, header_number)
}

fn current_block(&self, id: &BlockId<B>) -> Option<ethereum::Block> {
if let Ok(Some(block_data)) = self.client.storage(
&id,
&StorageKey(
storage_prefix_build(b"Ethereum", b"CurrentBlock")
)
) {
return Decode::decode(&mut &block_data.0[..]).unwrap_or_else(|_| None);
} else { return None; };
}

fn current_statuses(&self, id: &BlockId<B>) -> Option<Vec<TransactionStatus>> {
if let Ok(Some(status_data)) = self.client.storage(
&id,
&StorageKey(
storage_prefix_build(b"Ethereum", b"CurrentTransactionStatuses")
)
) {
return Decode::decode(&mut &status_data.0[..]).unwrap_or_else(|_| None);
} else { return None; };
}

fn current_receipts(&self, id: &BlockId<B>) -> Option<Vec<ethereum::Receipt>> {
if let Ok(Some(status_data)) = self.client.storage(
&id,
&StorageKey(
storage_prefix_build(b"Ethereum", b"CurrentReceipts")
)
) {
return Decode::decode(&mut &status_data.0[..]).unwrap_or_else(|_| None);
} else { return None; };
}
}

impl<B, C, P, CT, BE, A> EthApiT for EthApi<B, C, P, CT, BE, A> where
Expand Down Expand Up @@ -358,10 +398,8 @@ impl<B, C, P, CT, BE, A> EthApiT for EthApi<B, C, P, CT, BE, A> where
return Ok(None);
}

let block = self.client.runtime_api().current_block(&id)
.map_err(|err| internal_err(format!("call runtime failed: {:?}", err)))?;
let statuses = self.client.runtime_api().current_transaction_statuses(&id)
.map_err(|err| internal_err(format!("call runtime failed: {:?}", err)))?;
let block: Option<ethereum::Block> = self.current_block(&id);
let statuses: Option<Vec<TransactionStatus>> = self.current_statuses(&id);

match (block, statuses) {
(Some(block), Some(statuses)) => {
Expand All @@ -384,10 +422,8 @@ impl<B, C, P, CT, BE, A> EthApiT for EthApi<B, C, P, CT, BE, A> where
None => return Ok(None),
};

let block = self.client.runtime_api().current_block(&id)
.map_err(|err| internal_err(format!("call runtime failed: {:?}", err)))?;
let statuses = self.client.runtime_api().current_transaction_statuses(&id)
.map_err(|err| internal_err(format!("call runtime failed: {:?}", err)))?;
let block: Option<ethereum::Block> = self.current_block(&id);
let statuses: Option<Vec<TransactionStatus>> = self.current_statuses(&id);

match (block, statuses) {
(Some(block), Some(statuses)) => {
Expand Down Expand Up @@ -434,9 +470,7 @@ impl<B, C, P, CT, BE, A> EthApiT for EthApi<B, C, P, CT, BE, A> where
return Ok(None);
}

let block = self.client.runtime_api()
.current_block(&id)
.map_err(|err| internal_err(format!("fetch runtime account basic failed: {:?}", err)))?;
let block: Option<ethereum::Block> = self.current_block(&id);

match block {
Some(block) => Ok(Some(U256::from(block.transactions.len()))),
Expand All @@ -450,9 +484,7 @@ impl<B, C, P, CT, BE, A> EthApiT for EthApi<B, C, P, CT, BE, A> where
None => return Ok(None),
};

let block = self.client.runtime_api()
.current_block(&id)
.map_err(|err| internal_err(format!("fetch runtime account basic failed: {:?}", err)))?;
let block: Option<ethereum::Block> = self.current_block(&id);

match block {
Some(block) => Ok(Some(U256::from(block.transactions.len()))),
Expand Down Expand Up @@ -600,10 +632,8 @@ impl<B, C, P, CT, BE, A> EthApiT for EthApi<B, C, P, CT, BE, A> where
return Ok(None);
}

let block = self.client.runtime_api().current_block(&id)
.map_err(|err| internal_err(format!("call runtime failed: {:?}", err)))?;
let statuses = self.client.runtime_api().current_transaction_statuses(&id)
.map_err(|err| internal_err(format!("call runtime failed: {:?}", err)))?;
let block: Option<ethereum::Block> = self.current_block(&id);
let statuses: Option<Vec<TransactionStatus>> = self.current_statuses(&id);

match (block, statuses) {
(Some(block), Some(statuses)) => {
Expand Down Expand Up @@ -634,10 +664,8 @@ impl<B, C, P, CT, BE, A> EthApiT for EthApi<B, C, P, CT, BE, A> where
}
let index = index.value();

let block = self.client.runtime_api().current_block(&id)
.map_err(|err| internal_err(format!("call runtime failed: {:?}", err)))?;
let statuses = self.client.runtime_api().current_transaction_statuses(&id)
.map_err(|err| internal_err(format!("call runtime failed: {:?}", err)))?;
let block: Option<ethereum::Block> = self.current_block(&id);
let statuses: Option<Vec<TransactionStatus>> = self.current_statuses(&id);

match (block, statuses) {
(Some(block), Some(statuses)) => {
Expand All @@ -662,10 +690,8 @@ impl<B, C, P, CT, BE, A> EthApiT for EthApi<B, C, P, CT, BE, A> where
};
let index = index.value();

let block = self.client.runtime_api().current_block(&id)
.map_err(|err| internal_err(format!("call runtime failed: {:?}", err)))?;
let statuses = self.client.runtime_api().current_transaction_statuses(&id)
.map_err(|err| internal_err(format!("call runtime failed: {:?}", err)))?;
let block: Option<ethereum::Block> = self.current_block(&id);
let statuses: Option<Vec<TransactionStatus>> = self.current_statuses(&id);

match (block, statuses) {
(Some(block), Some(statuses)) => {
Expand Down Expand Up @@ -699,12 +725,9 @@ impl<B, C, P, CT, BE, A> EthApiT for EthApi<B, C, P, CT, BE, A> where
return Ok(None);
}

let block = self.client.runtime_api().current_block(&id)
.map_err(|err| internal_err(format!("call runtime failed: {:?}", err)))?;
let receipts = self.client.runtime_api().current_receipts(&id)
.map_err(|err| internal_err(format!("call runtime failed: {:?}", err)))?;
let statuses = self.client.runtime_api().current_transaction_statuses(&id)
.map_err(|err| internal_err(format!("call runtime failed: {:?}", err)))?;
let block: Option<ethereum::Block> = self.current_block(&id);
let statuses: Option<Vec<TransactionStatus>> = self.current_statuses(&id);
let receipts: Option<Vec<ethereum::Receipt>> = self.current_receipts(&id);

match (block, statuses, receipts) {
(Some(block), Some(statuses), Some(receipts)) => {
Expand Down Expand Up @@ -804,9 +827,8 @@ impl<B, C, P, CT, BE, A> EthApiT for EthApi<B, C, P, CT, BE, A> where
return Ok(Vec::new());
}

let (block, _, statuses) = self.client.runtime_api()
.current_all(&id)
.map_err(|err| internal_err(format!("fetch runtime account basic failed: {:?}", err)))?;
let block: Option<ethereum::Block> = self.current_block(&id);
let statuses: Option<Vec<TransactionStatus>> = self.current_statuses(&id);

if let (Some(block), Some(statuses)) = (block, statuses) {
blocks_and_statuses.push((block, statuses));
Expand All @@ -828,9 +850,8 @@ impl<B, C, P, CT, BE, A> EthApiT for EthApi<B, C, P, CT, BE, A> where
while current_number >= from_number {
let id = BlockId::Number(current_number);

let (block, _, statuses) = self.client.runtime_api()
.current_all(&id)
.map_err(|err| internal_err(format!("fetch runtime account basic failed: {:?}", err)))?;
let block: Option<ethereum::Block> = self.current_block(&id);
let statuses: Option<Vec<TransactionStatus>> = self.current_statuses(&id);

if let (Some(block), Some(statuses)) = (block, statuses) {
blocks_and_statuses.push((block, statuses));
Expand Down

0 comments on commit 833758f

Please sign in to comment.