Skip to content

Commit

Permalink
Implement block_transaction_count_by_hash for EthApi (#34)
Browse files Browse the repository at this point in the history
* Implement block_transaction_count_by_hash for EthApi

* Return Option for block_transaction_count_by_hash

* Fix 34#discussion_r438018163

* Remove sp-blockchain dependency
  • Loading branch information
tgmichel authored Jun 12, 2020
1 parent 6f4f4b4 commit 206eabb
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
2 changes: 1 addition & 1 deletion rpc/core/src/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ pub trait EthApi {

/// Returns the number of transactions in a block with given hash.
#[rpc(name = "eth_getBlockTransactionCountByHash")]
fn block_transaction_count_by_hash(&self, _: H256) -> BoxFuture<Option<U256>>;
fn block_transaction_count_by_hash(&self, _: H256) -> Result<Option<U256>>;

/// Returns the number of transactions in a block with given block number.
#[rpc(name = "eth_getBlockTransactionCountByNumber")]
Expand Down
1 change: 1 addition & 0 deletions rpc/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ sp_api::decl_runtime_apis! {
fn block_by_number(number: u32) -> Option<EthereumBlock>;
fn block_transaction_count_by_number(number: u32) -> Option<U256>;
fn block_by_hash(hash: H256) -> Option<EthereumBlock>;
fn block_transaction_count_by_hash(hash: H256) -> Option<U256>;
}
}

Expand Down
13 changes: 10 additions & 3 deletions rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ use sp_transaction_pool::TransactionPool;
use sc_client_api::backend::{StorageProvider, Backend, StateBackend};
use sha3::{Keccak256, Digest};
use sp_runtime::traits::BlakeTwo256;

use frontier_rpc_core::EthApi as EthApiT;
use frontier_rpc_core::types::{
BlockNumber, Bytes, CallRequest, EthAccount, Filter, Index, Log, Receipt, RichBlock,
Expand Down Expand Up @@ -246,8 +245,16 @@ impl<B, C, SC, P, CT, BE> EthApiT for EthApi<B, C, SC, P, CT, BE> where
.map_err(|_| internal_err("fetch runtime account basic failed"))?.nonce.into())
}

fn block_transaction_count_by_hash(&self, _: H256) -> BoxFuture<Option<U256>> {
unimplemented!("block_transaction_count_by_hash");
fn block_transaction_count_by_hash(&self, hash: H256) -> Result<Option<U256>> {
let header = self.select_chain.best_chain()
.map_err(|_| internal_err("fetch header failed"))?;

let result = match self.client.runtime_api()
.block_transaction_count_by_hash(&BlockId::Hash(header.hash()), hash) {
Ok(result) => result,
Err(_) => return Ok(None)
};
Ok(result)
}

fn block_transaction_count_by_number(&self, number: BlockNumber) -> Result<Option<U256>> {
Expand Down
7 changes: 7 additions & 0 deletions template/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,13 @@ impl_runtime_apis! {
None
}

fn block_transaction_count_by_hash(hash: H256) -> Option<U256> {
if let Some(block) = <ethereum::Module<Runtime>>::block_by_hash(hash) {
return Some(U256::from(block.transactions.len()))
}
None
}

fn block_by_hash(hash: H256) -> Option<EthereumBlock> {
<ethereum::Module<Runtime>>::block_by_hash(hash)
}
Expand Down

0 comments on commit 206eabb

Please sign in to comment.