Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Fill transaction hash on ethGetLog of light client. #9938

Merged
merged 10 commits into from
Dec 17, 2018
35 changes: 33 additions & 2 deletions rpc/src/v1/helpers/light_fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ impl LightFetch {
const MAX_BLOCK_RANGE: u64 = 1000;

let fetcher = self.clone();
let fetcher_block = self.clone();
self.headers_range_by_block_id(filter.from_block, filter.to_block, MAX_BLOCK_RANGE)
.and_then(move |mut headers| {
if headers.is_empty() {
Expand Down Expand Up @@ -353,7 +354,7 @@ impl LightFetch {
data: log.data.into(),
block_hash: Some(hash.into()),
block_number: Some(num.into()),
// No way to easily retrieve transaction hash, so let's just skip it.
// No way to easily retrieve transaction hash.
transaction_hash: None,
transaction_index: Some(transaction_index.into()),
log_index: Some(block_index.into()),
Expand All @@ -367,8 +368,38 @@ impl LightFetch {
}
future::ok::<_,OnDemandError>(matches)
}) // and then collect them into a vector.
.map(|matches| matches.into_iter().map(|(_, v)| v).collect())
.map_err(errors::on_demand_error)
// retrieve transaction hash.
.and_then(move |matches| {
ordian marked this conversation as resolved.
Show resolved Hide resolved
let mut blocks = BTreeMap::new();
let mut result: Vec<Log> = matches.into_iter().map(|(_, v)| {
{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indentation is off here

let block_hash = v.block_hash.as_ref().expect("Previously initialized with value; qed");
blocks.entry(block_hash.clone()).or_insert_with(|| {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

block_hash is copy, so we don't need clone.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I think this is rpc::v1::types::hash which is not Copy but we should refactor that to be consistent in the codebase!

fetcher_block.block(BlockId::Hash(block_hash.clone().into()))
});
}
v
}).collect();
// future get blocks (unordered it)
stream::futures_unordered(blocks.into_iter().map(|(_,v)|v)).collect().map(move |blocks| {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: missing whitespace: .map(|(_, v)| v)

let transactions_per_block: BTreeMap<_, _> = blocks.iter()
.map(|block| (block.hash(), block.transactions())).collect();
for log in result.iter_mut() {
let log_index: U256 = log.transaction_index.expect("Previously initialized with value; qed").into();
if log_index < usize::max_value().into() {
let block_hash = log.block_hash.clone().expect("Previously initialized with value; qed").into();
let tx_hash = transactions_per_block.get(&block_hash)
.and_then(|txs| txs.get(log_index.as_usize()))
.map(|tr| tr.hash().into());
niklasad1 marked this conversation as resolved.
Show resolved Hide resolved
log.transaction_hash = tx_hash;
} else {
trace!(target: "light_fetch", "A received Receipts indexed other usize length ignored");
}
}
result
})
})
});

match maybe_future {
Expand Down