Skip to content

Commit b427a05

Browse files
committed
Improvements in scanner
1 parent 5d4ee7c commit b427a05

File tree

2 files changed

+49
-39
lines changed

2 files changed

+49
-39
lines changed

crates/erc20_payment_lib/src/service.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ pub async fn transaction_from_chain_and_into_db(
8080
.map_err(|_err| ConversionError::from("Cannot parse tx_hash".to_string()))
8181
.map_err(err_from!())?;
8282

83-
if let Some(chain_tx) = get_chain_tx_hash(conn, tx_hash.to_string())
83+
if let Some(chain_tx) = get_chain_tx_hash(conn, format!("{:#x}", tx_hash))
8484
.await
8585
.map_err(err_from!())?
8686
{

src/actions/scan_chain.rs

Lines changed: 48 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -75,57 +75,64 @@ async fn scan_auto_step(
7575
web3: Arc<Web3RpcPool>,
7676
sender: Option<Address>,
7777
scan_info: &mut ScanDaoDbObj,
78-
) -> Result<(), PaymentError> {
78+
) -> Result<bool, PaymentError> {
7979
let current_block = web3
8080
.clone()
8181
.eth_block_number()
8282
.await
8383
.map_err(err_from!())?
8484
.as_u64() as i64;
8585

86-
let (start_block, end_block, is_forward) = if scan_info.start_block == -1 {
87-
let start_block = std::cmp::max(
88-
current_block as i64 - scan_blockchain_options.blocks_behind.unwrap_or(100) as i64,
89-
1,
90-
);
91-
let end_block = current_block;
92-
(start_block, end_block, true)
93-
} else if current_block - scan_info.last_block
94-
>= scan_blockchain_options.blocks_behind.unwrap_or(100) as i64
95-
+ scan_blockchain_options.forward_scan_buffer as i64
96-
{
97-
log::info!("Scan forward needed");
98-
let start_block = scan_info.last_block + 1;
86+
let scan_behind_needed =
87+
scan_info.start_block <= scan_blockchain_options.from_block.unwrap_or(1) as i64;
88+
89+
let (start_block, end_block, is_forward) = if scan_behind_needed {
90+
if current_block - scan_info.last_block
91+
>= scan_blockchain_options.blocks_behind.unwrap_or(100) as i64
92+
+ scan_blockchain_options.forward_scan_buffer as i64
93+
{
94+
log::info!("Scan forward needed");
95+
let start_block = scan_info.last_block + 1;
96+
if start_block > current_block {
97+
log::warn!(
98+
"Start block {} is higher than current block {}, no newer data on blockchain",
99+
start_block,
100+
current_block
101+
);
102+
return Ok(true);
103+
}
104+
let end_block = start_block + scan_blockchain_options.blocks_at_once as i64;
105+
let end_block = std::cmp::min(end_block, current_block);
106+
(start_block, end_block, true)
107+
} else {
108+
let end_block = scan_info.start_block;
109+
let start_block =
110+
std::cmp::max(end_block - scan_blockchain_options.blocks_at_once as i64, 1);
111+
if end_block - start_block > 0 {
112+
(start_block, end_block, false)
113+
} else {
114+
log::warn!(
115+
"Start block {} is higher than end block {}, no newer data on blockchain",
116+
start_block,
117+
end_block
118+
);
119+
return Ok(true);
120+
}
121+
}
122+
} else {
123+
// normal auto scan
124+
let start_block = scan_info.last_block - 100;
99125
if start_block > current_block {
100126
log::warn!(
101127
"Start block {} is higher than current block {}, no newer data on blockchain",
102128
start_block,
103129
current_block
104130
);
105-
return Ok(());
131+
return Ok(true);
106132
}
107133
let end_block = start_block + scan_blockchain_options.blocks_at_once as i64;
108-
let end_block = std::cmp::min(end_block, current_block);
134+
let end_block = std::cmp::min(end_block, current_block + 1);
109135
(start_block, end_block, true)
110-
} else {
111-
if scan_info.start_block <= scan_blockchain_options.from_block.unwrap_or(1) as i64 {
112-
tokio::time::sleep(std::time::Duration::from_secs(5)).await;
113-
return Ok(());
114-
}
115-
116-
let end_block = scan_info.start_block;
117-
let start_block =
118-
std::cmp::max(end_block - scan_blockchain_options.blocks_at_once as i64, 1);
119-
if end_block - start_block > 0 {
120-
(start_block, end_block, false)
121-
} else {
122-
log::warn!(
123-
"Start block {} is higher than end block {}, no newer data on blockchain",
124-
start_block,
125-
end_block
126-
);
127-
return Ok(());
128-
}
129136
};
130137

131138
log::info!(
@@ -174,7 +181,7 @@ async fn scan_auto_step(
174181
.await
175182
.map_err(err_from!())?;
176183

177-
Ok(())
184+
Ok(is_forward)
178185
}
179186

180187
pub async fn scan_blockchain_local(
@@ -264,14 +271,17 @@ pub async fn scan_blockchain_local(
264271
)
265272
.await
266273
{
267-
Ok(_) => {
274+
Ok(wait) => {
268275
log::info!("Scan step done");
276+
if wait {
277+
tokio::time::sleep(std::time::Duration::from_millis(2000)).await;
278+
}
269279
}
270280
Err(e) => {
271281
log::info!("Scan step failed - trying again: {}", e);
282+
tokio::time::sleep(std::time::Duration::from_millis(2000)).await;
272283
}
273284
}
274-
//tokio::time::sleep(std::time::Duration::from_millis(1000)).await;
275285
}
276286
} else {
277287
if current_block < scan_info.last_block {

0 commit comments

Comments
 (0)