diff --git a/rpc/core/src/eth.rs b/rpc/core/src/eth.rs index f7bdc4c295..305700e877 100644 --- a/rpc/core/src/eth.rs +++ b/rpc/core/src/eth.rs @@ -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>; + fn block_transaction_count_by_hash(&self, _: H256) -> Result>; /// Returns the number of transactions in a block with given block number. #[rpc(name = "eth_getBlockTransactionCountByNumber")] diff --git a/rpc/primitives/src/lib.rs b/rpc/primitives/src/lib.rs index fc555d6740..181bb7fbd1 100644 --- a/rpc/primitives/src/lib.rs +++ b/rpc/primitives/src/lib.rs @@ -45,6 +45,7 @@ sp_api::decl_runtime_apis! { fn block_by_number(number: u32) -> Option; fn block_transaction_count_by_number(number: u32) -> Option; fn block_by_hash(hash: H256) -> Option; + fn block_transaction_count_by_hash(hash: H256) -> Option; } } diff --git a/rpc/src/lib.rs b/rpc/src/lib.rs index 907cfc9c47..98ca990a0e 100644 --- a/rpc/src/lib.rs +++ b/rpc/src/lib.rs @@ -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, @@ -246,8 +245,16 @@ impl EthApiT for EthApi where .map_err(|_| internal_err("fetch runtime account basic failed"))?.nonce.into()) } - fn block_transaction_count_by_hash(&self, _: H256) -> BoxFuture> { - unimplemented!("block_transaction_count_by_hash"); + fn block_transaction_count_by_hash(&self, hash: H256) -> Result> { + 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> { diff --git a/template/runtime/src/lib.rs b/template/runtime/src/lib.rs index a4ecc425ea..d0a298af4c 100644 --- a/template/runtime/src/lib.rs +++ b/template/runtime/src/lib.rs @@ -485,6 +485,13 @@ impl_runtime_apis! { None } + fn block_transaction_count_by_hash(hash: H256) -> Option { + if let Some(block) = >::block_by_hash(hash) { + return Some(U256::from(block.transactions.len())) + } + None + } + fn block_by_hash(hash: H256) -> Option { >::block_by_hash(hash) }