Skip to content

Commit e82da77

Browse files
authored
Refine regex for max block count extraction and add test (#735)
* Refine regex for max block count extraction and add test Updated regex to capture maximum block count from error messages more accurately - namely update to Nodies provider. Added a new test case for extracting this specific error response. * Fix inclusive range calculation for block scanning in BlockchainInterfaceWeb3 Adjusted the end block marker calculation to correctly account for inclusive ranges by subtracting 1 from the scan range. T * Fix block marker calculations in tests for blockchain bridge and interface. Adjusted expected new start block values to ensure accurate transaction retrieval and logging.
1 parent 42eebcc commit e82da77

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

node/src/blockchain/blockchain_bridge.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -507,8 +507,8 @@ impl BlockchainBridge {
507507

508508
pub fn extract_max_block_count(error: BlockchainError) -> Option<u64> {
509509
let regex_result =
510-
Regex::new(r".* (max: |allowed for your plan: |is limited to |block range limit \(|exceeds max block range )(?P<max_block_count>\d+).*")
511-
.expect("Invalid regex");
510+
Regex::new(r".* (max: |allowed for your plan: |is limited to |block range limit \(|exceeds max block range |maximum allowed is )(?P<max_block_count>\d+).*")
511+
.expect("Invalid regex");
512512
let max_block_count = match error {
513513
BlockchainError::QueryFailed(msg) => match regex_result.captures(msg.as_str()) {
514514
Some(captures) => match captures.name("max_block_count") {
@@ -1541,7 +1541,7 @@ mod tests {
15411541
system.run();
15421542
let after = SystemTime::now();
15431543
let expected_transactions = RetrievedBlockchainTransactions {
1544-
new_start_block: BlockMarker::Value(42 + 9_000_000 + 1),
1544+
new_start_block: BlockMarker::Value(42 + 9_000_000),
15451545
transactions: vec![
15461546
BlockchainTransaction {
15471547
block_number: 6040059,
@@ -1742,7 +1742,7 @@ mod tests {
17421742
let received_payments_message = accountant_recording.get_record::<ReceivedPayments>(0);
17431743
check_timestamp(before, received_payments_message.timestamp, after);
17441744
let expected_transactions = RetrievedBlockchainTransactions {
1745-
new_start_block: BlockMarker::Value(6 + 5000 + 1),
1745+
new_start_block: BlockMarker::Value(6 + 5000),
17461746
transactions: vec![BlockchainTransaction {
17471747
block_number: 2000,
17481748
from: earning_wallet.clone(),
@@ -2201,6 +2201,15 @@ mod tests {
22012201
assert_eq!(Some(100000), max_block_count);
22022202
}
22032203

2204+
#[test]
2205+
fn extract_max_block_range_for_nodies_error_response_v2() {
2206+
let result = BlockchainError::QueryFailed("RPC error: Error { code: ServerError(-32001), message: \"Block range too large: maximum allowed is 20000 blocks\", data: None }".to_string());
2207+
2208+
let max_block_count = BlockchainBridge::extract_max_block_count(result);
2209+
2210+
assert_eq!(Some(20000), max_block_count);
2211+
}
2212+
22042213
#[test]
22052214
fn extract_max_block_range_for_expected_batch_got_single_error_response() {
22062215
let result = BlockchainError::QueryFailed(

node/src/blockchain/blockchain_interface/blockchain_interface_web3/mod.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,9 @@ impl BlockchainInterfaceWeb3 {
367367
) -> BlockMarker {
368368
let locally_determined_end_block_marker = match (start_block_marker, scan_range) {
369369
(BlockMarker::Value(start_block), BlockScanRange::Range(scan_range_number)) => {
370-
BlockMarker::Value(start_block + scan_range_number)
370+
// Subtract 1 because the range is inclusive: [start_block, end_block]
371+
// Example: If max range is 20000, we need start_block to start_block+20000-1 (ending up with 20000 blocks total)
372+
BlockMarker::Value(start_block + scan_range_number - 1)
371373
}
372374
(_, _) => BlockMarker::Uninitialized,
373375
};
@@ -515,8 +517,8 @@ mod tests {
515517
let start_block_marker = BlockMarker::Value(42);
516518
let scan_range = BlockScanRange::Range(1000);
517519
let block_response = "0x7d0"; // 2_000
518-
let expected_new_start_block = BlockMarker::Value(42 + 1000 + 1);
519-
let expected_log = "from start block: Number(42) to end block: Number(1042)";
520+
let expected_new_start_block = BlockMarker::Value(42 + 1000);
521+
let expected_log = "from start block: Number(42) to end block: Number(1041)";
520522
assert_on_retrieves_transactions(
521523
start_block_marker,
522524
scan_range,
@@ -1176,7 +1178,7 @@ mod tests {
11761178
Err(BlockchainError::InvalidResponse),
11771179
&logger
11781180
),
1179-
BlockMarker::Value(150)
1181+
BlockMarker::Value(149)
11801182
);
11811183
assert_eq!(
11821184
Subject::calculate_end_block_marker(
@@ -1194,7 +1196,7 @@ mod tests {
11941196
Ok(120.into()),
11951197
&logger
11961198
),
1197-
BlockMarker::Value(50 + 10)
1199+
BlockMarker::Value(59)
11981200
);
11991201
}
12001202

0 commit comments

Comments
 (0)