Skip to content
This repository was archived by the owner on Mar 23, 2021. It is now read-only.

Commit 2271ca4

Browse files
committed
Find transaction in old ethereum block
1 parent d008447 commit 2271ca4

File tree

2 files changed

+64
-5
lines changed

2 files changed

+64
-5
lines changed

cnd/src/btsieve/ethereum/mod.rs

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ pub use self::{
77
};
88
use crate::{
99
btsieve::{BlockByHash, LatestBlock, MatchingTransactions, ReceiptByHash},
10-
ethereum::{Block, Transaction, TransactionAndReceipt, TransactionReceipt, H256},
10+
ethereum::{Block, Transaction, TransactionAndReceipt, TransactionReceipt, H256, U256},
1111
};
1212
use futures_core::{compat::Future01CompatExt, future::join, FutureExt, TryFutureExt};
1313
use std::{collections::HashSet, fmt::Debug, ops::Add};
@@ -30,15 +30,19 @@ where
3030
fn matching_transactions(
3131
&self,
3232
pattern: TransactionPattern,
33-
timestamp: Option<u32>,
33+
reference_timestamp: Option<u32>,
3434
) -> Box<dyn Stream<Item = Self::Transaction, Error = ()> + Send> {
3535
let (block_queue, next_block) = async_std::sync::channel(1);
3636
let (find_parent_queue, next_find_parent) = async_std::sync::channel(5);
37+
let (look_in_the_past_queue, next_look_in_the_past) = async_std::sync::channel(5);
38+
39+
let reference_timestamp = reference_timestamp.map(|timestamp| U256::from(timestamp));
3740

3841
spawn(self.clone(), {
3942
let mut connector = self.clone();
4043
let block_queue = block_queue.clone();
4144
let find_parent_queue = find_parent_queue.clone();
45+
let look_in_the_past_queue = look_in_the_past_queue.clone();
4246

4347
async move {
4448
let mut sent_blockhashes: HashSet<H256> = HashSet::new();
@@ -61,6 +65,10 @@ where
6165
find_parent_queue.send((blockhash, block.parent_hash)),
6266
)
6367
.await;
68+
69+
if sent_blockhashes.len() == 1 {
70+
look_in_the_past_queue.send(block.parent_hash).await
71+
};
6472
}
6573
}
6674
Ok(Some(_)) => {
@@ -117,9 +125,10 @@ where
117125
});
118126

119127
spawn(self.clone(), {
128+
let fetch_block_by_hash_queue = fetch_block_by_hash_queue.clone();
129+
120130
async move {
121131
let mut prev_blockhashes: HashSet<H256> = HashSet::new();
122-
let fetch_block_by_hash_queue = fetch_block_by_hash_queue.clone();
123132

124133
loop {
125134
match next_find_parent.recv().await {
@@ -138,6 +147,53 @@ where
138147
}
139148
});
140149

150+
spawn(self.clone(), {
151+
let connector = self.clone();
152+
let block_queue = block_queue.clone();
153+
let look_in_the_past_queue = look_in_the_past_queue.clone();
154+
155+
async move {
156+
loop {
157+
match next_look_in_the_past.recv().await {
158+
Some(parent_blockhash) => {
159+
match connector.block_by_hash(parent_blockhash).compat().await {
160+
Ok(Some(block)) => {
161+
if reference_timestamp
162+
.map(|reference_timestamp| {
163+
reference_timestamp <= block.timestamp
164+
})
165+
.unwrap_or(false)
166+
{
167+
join(
168+
block_queue.send(block.clone()),
169+
look_in_the_past_queue.send(block.parent_hash),
170+
)
171+
.await;
172+
}
173+
}
174+
Ok(None) => {
175+
log::warn!(
176+
"Block with hash {} does not exist",
177+
parent_blockhash
178+
);
179+
}
180+
Err(e) => {
181+
log::warn!(
182+
"Could not get block with hash {}: {:?}",
183+
parent_blockhash,
184+
e
185+
);
186+
187+
look_in_the_past_queue.send(parent_blockhash).await
188+
}
189+
}
190+
}
191+
None => unreachable!("senders cannot be dropped"),
192+
}
193+
}
194+
}
195+
});
196+
141197
let (matching_transaction_queue, matching_transaction) = async_std::sync::channel(1);
142198

143199
spawn(self.clone(), {

cnd/tests/ethereum_old_blocks.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
pub mod ethereum_helper;
22

3-
use btsieve::{ethereum::TransactionPattern, first_or_else::StreamExt, MatchingTransactions};
3+
use cnd::{
4+
btsieve::{ethereum::TransactionPattern, MatchingTransactions},
5+
ethereum::{Block, Transaction, TransactionAndReceipt, TransactionReceipt},
6+
first_or_else::StreamExt,
7+
};
48
use ethereum_helper::EthereumConnectorMock;
5-
use ethereum_support::{Block, Transaction, TransactionAndReceipt, TransactionReceipt};
69
use tokio::prelude::Future;
710

811
#[test]

0 commit comments

Comments
 (0)