diff --git a/rpc/primitives/src/lib.rs b/rpc/primitives/src/lib.rs index fadbcbcea..59167598d 100644 --- a/rpc/primitives/src/lib.rs +++ b/rpc/primitives/src/lib.rs @@ -80,6 +80,12 @@ sp_api::decl_runtime_apis! { fn current_receipts() -> Option>; /// Return the current transaction status. fn current_transaction_statuses() -> Option>; + /// Return all the current data for a block in a single runtime call. + fn current_all() -> ( + Option, + Option>, + Option> + ); } } diff --git a/rpc/src/eth.rs b/rpc/src/eth.rs index 75831ee56..64fa5306e 100644 --- a/rpc/src/eth.rs +++ b/rpc/src/eth.rs @@ -719,7 +719,7 @@ impl EthApiT for EthApi where } fn logs(&self, filter: Filter) -> Result> { - let mut blocks_and_receipts = Vec::new(); + let mut blocks_and_statuses = Vec::new(); let mut ret = Vec::new(); if let Some(hash) = filter.block_hash { @@ -730,14 +730,12 @@ impl EthApiT for EthApi where _ => return Ok(Vec::new()), }; - let block = self.client.runtime_api() - .current_block(&id) + let (block, _, statuses) = self.client.runtime_api() + .current_all(&id) .map_err(|err| internal_err(format!("fetch runtime account basic failed: {:?}", err)))?; - let receipts = self.client.runtime_api().current_receipts(&id) - .map_err(|err| internal_err(format!("call runtime failed: {:?}", err)))?; - if let (Some(block), Some(receipts)) = (block, receipts) { - blocks_and_receipts.push((block, receipts)); + if let (Some(block), Some(statuses)) = (block, statuses) { + blocks_and_statuses.push((block, statuses)); } } else { let mut current_number = filter.to_block @@ -753,18 +751,15 @@ impl EthApiT for EthApi where .unwrap_or( self.client.info().best_number ); - while current_number >= from_number { let id = BlockId::Number(current_number); - let block = self.client.runtime_api() - .current_block(&id) + let (block, _, statuses) = self.client.runtime_api() + .current_all(&id) .map_err(|err| internal_err(format!("fetch runtime account basic failed: {:?}", err)))?; - let receipts = self.client.runtime_api().current_receipts(&id) - .map_err(|err| internal_err(format!("call runtime failed: {:?}", err)))?; - if let (Some(block), Some(receipts)) = (block, receipts) { - blocks_and_receipts.push((block, receipts)); + if let (Some(block), Some(statuses)) = (block, statuses) { + blocks_and_statuses.push((block, statuses)); } if current_number == Zero::zero() { @@ -775,15 +770,15 @@ impl EthApiT for EthApi where } } - for (block, receipts) in blocks_and_receipts { + for (block, statuses) in blocks_and_statuses { let mut block_log_index: u32 = 0; - for (index, receipt) in receipts.iter().enumerate() { - let logs = receipt.logs.clone(); + let block_hash = H256::from_slice( + Keccak256::digest(&rlp::encode(&block.header)).as_slice() + ); + for status in statuses.iter() { + let logs = status.logs.clone(); let mut transaction_log_index: u32 = 0; - let transaction = &block.transactions[index as usize]; - let transaction_hash = H256::from_slice( - Keccak256::digest(&rlp::encode(transaction)).as_slice() - ); + let transaction_hash = status.transaction_hash; for log in logs { let mut add: bool = false; if let ( @@ -810,12 +805,10 @@ impl EthApiT for EthApi where address: log.address.clone(), topics: log.topics.clone(), data: Bytes(log.data.clone()), - block_hash: Some(H256::from_slice( - Keccak256::digest(&rlp::encode(&block.header)).as_slice() - )), + block_hash: Some(block_hash), block_number: Some(block.header.number.clone()), transaction_hash: Some(transaction_hash), - transaction_index: Some(U256::from(index)), + transaction_index: Some(U256::from(status.transaction_index)), log_index: Some(U256::from(block_log_index)), transaction_log_index: Some(U256::from(transaction_log_index)), removed: false, diff --git a/template/runtime/src/lib.rs b/template/runtime/src/lib.rs index e558cfb5f..d34afa9d3 100644 --- a/template/runtime/src/lib.rs +++ b/template/runtime/src/lib.rs @@ -553,6 +553,18 @@ impl_runtime_apis! { fn current_receipts() -> Option> { Ethereum::current_receipts() } + + fn current_all() -> ( + Option, + Option>, + Option> + ) { + ( + Ethereum::current_block(), + Ethereum::current_receipts(), + Ethereum::current_transaction_statuses() + ) + } } impl pallet_transaction_payment_rpc_runtime_api::TransactionPaymentApi<