Skip to content

Commit

Permalink
Code clean
Browse files Browse the repository at this point in the history
  • Loading branch information
boundless-forest committed Apr 24, 2024
1 parent 69ef658 commit 0b60350
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 105 deletions.
1 change: 0 additions & 1 deletion client/rpc/src/eth/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ where
) -> RpcResult<Option<RichBlock>> {
let client = Arc::clone(&self.client);
let block_data_cache = Arc::clone(&self.block_data_cache);
let backend = Arc::clone(&self.backend);
let graph = Arc::clone(&self.graph);

match self.backend.block_id(Some(number_or_hash)).await {
Expand Down
19 changes: 8 additions & 11 deletions client/rpc/src/eth/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -473,17 +473,14 @@ where

let mut ret: Vec<Log> = Vec::new();
if let Some(hash) = filter.block_hash {
let substrate_hash = match frontier_backend_client::load_hash::<B, C>(
client.as_ref(),
backend.as_ref(),
hash,
)
.await
.map_err(|err| internal_err(format!("{:?}", err)))?
{
Some(hash) => hash,
_ => return Err(crate::err(-32000, "unknown block", None)),
};
let substrate_hash =
match frontier_backend_client::load_hash::<B, C>(backend.as_ref(), hash)
.await
.map_err(|err| internal_err(format!("{:?}", err)))?
{
Some(hash) => hash,
_ => return Err(crate::err(-32000, "unknown block", None)),
};
let schema = fc_storage::onchain_storage_schema(client.as_ref(), substrate_hash);

let block = block_data_cache.current_block(schema, substrate_hash).await;
Expand Down
39 changes: 16 additions & 23 deletions client/rpc/src/eth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,17 +166,14 @@ where
&self,
eth_block_hash: H256,
) -> RpcResult<BlockInfo<B::Hash>> {
let substrate_hash = match frontier_backend_client::load_hash::<B, C>(
self.client.as_ref(),
self.backend.as_ref(),
eth_block_hash,
)
.await
.map_err(|err| internal_err(format!("{:?}", err)))?
{
Some(hash) => hash,
_ => return Ok(BlockInfo::default()),
};
let substrate_hash =
match frontier_backend_client::load_hash::<B, C>(self.backend.as_ref(), eth_block_hash)
.await
.map_err(|err| internal_err(format!("{:?}", err)))?
{
Some(hash) => hash,
_ => return Ok(BlockInfo::default()),
};

self.block_info_by_substrate_hash(substrate_hash).await
}
Expand All @@ -186,7 +183,6 @@ where
ethereum_tx_hash: H256,
) -> RpcResult<(BlockInfo<B::Hash>, usize)> {
let (eth_block_hash, index) = match frontier_backend_client::load_transactions::<B, C>(
self.client.as_ref(),
self.backend.as_ref(),
ethereum_tx_hash,
true,
Expand All @@ -198,17 +194,14 @@ where
None => return Ok((BlockInfo::default(), 0)),
};

let substrate_hash = match frontier_backend_client::load_hash::<B, C>(
self.client.as_ref(),
self.backend.as_ref(),
eth_block_hash,
)
.await
.map_err(|err| internal_err(format!("{:?}", err)))?
{
Some(hash) => hash,
_ => return Ok((BlockInfo::default(), 0)),
};
let substrate_hash =
match frontier_backend_client::load_hash::<B, C>(self.backend.as_ref(), eth_block_hash)
.await
.map_err(|err| internal_err(format!("{:?}", err)))?
{
Some(hash) => hash,
_ => return Ok((BlockInfo::default(), 0)),
};

Ok((
self.block_info_by_substrate_hash(substrate_hash).await?,
Expand Down
117 changes: 58 additions & 59 deletions client/rpc/src/eth/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,75 +53,74 @@ where
let backend = Arc::clone(&self.backend);
let graph = Arc::clone(&self.graph);

let (eth_block_hash, index) = match frontier_backend_client::load_transactions::<B, C>(
client.as_ref(),
backend.as_ref(),
hash,
true,
)
.await
.map_err(|err| internal_err(format!("{:?}", err)))?
{
Some((eth_block_hash, index)) => (eth_block_hash, index as usize),
None => {
let api = client.runtime_api();
let best_block = client.info().best_hash;
let (eth_block_hash, index) =
match frontier_backend_client::load_transactions::<B, C>(backend.as_ref(), hash, true)
.await
.map_err(|err| internal_err(format!("{:?}", err)))?
{
Some((eth_block_hash, index)) => (eth_block_hash, index as usize),
None => {
let api = client.runtime_api();
let best_block = client.info().best_hash;

let api_version = if let Ok(Some(api_version)) =
api.api_version::<dyn EthereumRuntimeRPCApi<B>>(best_block)
{
api_version
} else {
return Err(internal_err("failed to retrieve Runtime Api version"));
};
// If the transaction is not yet mapped in the frontier db,
// check for it in the transaction pool.
let mut xts: Vec<<B as BlockT>::Extrinsic> = Vec::new();
// Collect transactions in the ready validated pool.
xts.extend(
graph
.validated_pool()
.ready()
.map(|in_pool_tx| in_pool_tx.data().clone())
.collect::<Vec<<B as BlockT>::Extrinsic>>(),
);
let api_version = if let Ok(Some(api_version)) =
api.api_version::<dyn EthereumRuntimeRPCApi<B>>(best_block)
{
api_version
} else {
return Err(internal_err("failed to retrieve Runtime Api version"));
};
// If the transaction is not yet mapped in the frontier db,
// check for it in the transaction pool.
let mut xts: Vec<<B as BlockT>::Extrinsic> = Vec::new();
// Collect transactions in the ready validated pool.
xts.extend(
graph
.validated_pool()
.ready()
.map(|in_pool_tx| in_pool_tx.data().clone())
.collect::<Vec<<B as BlockT>::Extrinsic>>(),
);

// Collect transactions in the future validated pool.
xts.extend(
graph
.validated_pool()
.futures()
.iter()
.map(|(_hash, extrinsic)| extrinsic.clone())
.collect::<Vec<<B as BlockT>::Extrinsic>>(),
);
// Collect transactions in the future validated pool.
xts.extend(
graph
.validated_pool()
.futures()
.iter()
.map(|(_hash, extrinsic)| extrinsic.clone())
.collect::<Vec<<B as BlockT>::Extrinsic>>(),
);

let ethereum_transactions: Vec<EthereumTransaction> = if api_version > 1 {
api.extrinsic_filter(best_block, xts).map_err(|err| {
internal_err(format!("fetch runtime extrinsic filter failed: {:?}", err))
})?
} else {
#[allow(deprecated)]
let legacy = api.extrinsic_filter_before_version_2(best_block, xts)
.map_err(|err| {
let ethereum_transactions: Vec<EthereumTransaction> = if api_version > 1 {
api.extrinsic_filter(best_block, xts).map_err(|err| {
internal_err(format!(
"fetch runtime extrinsic filter failed: {:?}",
err
))
})?;
legacy.into_iter().map(|tx| tx.into()).collect()
};
})?
} else {
#[allow(deprecated)]
let legacy = api.extrinsic_filter_before_version_2(best_block, xts)
.map_err(|err| {
internal_err(format!(
"fetch runtime extrinsic filter failed: {:?}",
err
))
})?;
legacy.into_iter().map(|tx| tx.into()).collect()
};

for txn in ethereum_transactions {
let inner_hash = txn.hash();
if hash == inner_hash {
return Ok(Some(transaction_build(txn, None, None, None)));
for txn in ethereum_transactions {
let inner_hash = txn.hash();
if hash == inner_hash {
return Ok(Some(transaction_build(txn, None, None, None)));
}
}
// Unknown transaction.
return Ok(None);
}
// Unknown transaction.
return Ok(None);
}
};
};

let BlockInfo {
block,
Expand Down
12 changes: 1 addition & 11 deletions client/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,8 @@ pub mod frontier_backend_client {
};
use sp_blockchain::HeaderBackend;
use sp_io::hashing::{blake2_128, twox_128};
use sp_runtime::{
generic::BlockId,
traits::{Block as BlockT, HashingFor, UniqueSaturatedInto, Zero},
};
use sp_runtime::traits::{Block as BlockT, HashingFor};
use sp_state_machine::OverlayedChanges;
// Frontier
use fc_rpc_core::types::BlockNumberOrHash;

/// Implements a default runtime storage override.
/// It assumes that the balances and nonces are stored in pallet `system.account`, and
Expand Down Expand Up @@ -189,7 +184,6 @@ pub mod frontier_backend_client {
}

pub async fn load_hash<B: BlockT, C>(
client: &C,
backend: &dyn fc_api::Backend<B>,
hash: H256,
) -> RpcResult<Option<B::Hash>>
Expand All @@ -213,7 +207,6 @@ pub mod frontier_backend_client {
}

pub async fn load_transactions<B: BlockT, C>(
client: &C,
backend: &dyn fc_api::Backend<B>,
transaction_hash: H256,
only_canonical: bool,
Expand Down Expand Up @@ -375,7 +368,6 @@ mod tests {
// Expect B1 to be canon
assert_eq!(
futures::executor::block_on(super::frontier_backend_client::load_hash(
client.as_ref(),
backend.as_ref(),
ethereum_block_hash
))
Expand Down Expand Up @@ -404,7 +396,6 @@ mod tests {
// Still expect B1 to be canon
assert_eq!(
futures::executor::block_on(super::frontier_backend_client::load_hash(
client.as_ref(),
backend.as_ref(),
ethereum_block_hash
))
Expand All @@ -424,7 +415,6 @@ mod tests {
// Expect B2 to be new canon
assert_eq!(
futures::executor::block_on(super::frontier_backend_client::load_hash(
client.as_ref(),
backend.as_ref(),
ethereum_block_hash
))
Expand Down

0 comments on commit 0b60350

Please sign in to comment.