Skip to content

Commit

Permalink
GH-600: Handle expected batch, got single response
Browse files Browse the repository at this point in the history
  • Loading branch information
masqrauder committed Jun 4, 2023
1 parent 30bee60 commit 86e4f9c
Showing 1 changed file with 99 additions and 3 deletions.
102 changes: 99 additions & 3 deletions node/src/blockchain/blockchain_bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ impl BlockchainBridge {
match retrieved_transactions {
Ok(transactions) => {
debug!(
logger,
self.logger,
"Write new start block: {}", transactions.new_start_block
);
if let Err(e) = self
Expand Down Expand Up @@ -524,7 +524,7 @@ impl BlockchainBridge {

pub fn extract_max_block_count(&self, error: BlockchainError) -> Option<u64> {
let regex_result =
Regex::new(r".* (max:|more than) (?P<max_block_count>\d+)(.*| blocks at once\.)")
Regex::new(r".* (max:|more than|allowed for your plan:|limited to) (?P<max_block_count>\d+)(.*| blocks at once\.)")
.expect("Invalid regex");
let max_block_count = match error {
BlockchainError::QueryFailed(msg) => match regex_result.captures(msg.as_str()) {
Expand All @@ -535,7 +535,10 @@ impl BlockchainBridge {
},
_ => None,
},
_ => None,
None => match msg.as_str() {
"Got invalid response: Expected batch, got single." => Some(1000),
_ => None,
},
},
_ => None,
};
Expand Down Expand Up @@ -2040,6 +2043,99 @@ mod tests {

assert_eq!(Some(100000u64), max_block_count);
}
/*
POKT (Polygon mainnet and mumbai)
{"jsonrpc":"2.0","id":7,"error":{"message":"You cannot query logs for more than 100000 blocks at once.","code":-32064}}
*/
/*
Ankr
{"jsonrpc":"2.0","error":{"code":-32600,"message":"block range is too wide"},"id":null}%
*/
#[test]
fn extract_max_block_range_for_ankr_error_response() {
let result = BlockchainError::QueryFailed("RPC error: Error { code: ServerError(-32600), message: \"block range is too wide\", data: None }".to_string());
let subject = BlockchainBridge::new(
Box::new(BlockchainInterfaceMock::default()),
Box::new(PersistentConfigurationMock::default()),
false,
None,
);
let max_block_count = subject.extract_max_block_count(result);

assert_eq!(None, max_block_count);
}

/*
MaticVigil
[{"error":{"message":"Blockheight too far in the past. Check params passed to eth_getLogs or eth_call requests.Range of blocks allowed for your plan: 1000","code":-32005},"jsonrpc":"2.0","id":7},{"error":{"message":"Blockheight too far in the past. Check params passed to eth_getLogs or eth_call requests.Range of blocks allowed for your plan: 1000","code":-32005},"jsonrpc":"2.0","id":8}]%
*/
#[test]
fn extract_max_block_range_for_matic_vigil_error_response() {
let result = BlockchainError::QueryFailed("RPC error: Error { code: ServerError(-32005), message: \"Blockheight too far in the past. Check params passed to eth_getLogs or eth_call requests.Range of blocks allowed for your plan: 1000\", data: None }".to_string());
let subject = BlockchainBridge::new(
Box::new(BlockchainInterfaceMock::default()),
Box::new(PersistentConfigurationMock::default()),
false,
None,
);
let max_block_count = subject.extract_max_block_count(result);

assert_eq!(Some(1000), max_block_count);
}

/*
Blockpi
[{"jsonrpc":"2.0","id":7,"result":"0x21db466"},{"jsonrpc":"2.0","id":8,"error":{"code":-32602,"message":"eth_getLogs is limited to 1024 block range. Please check the parameter requirements at https://docs.blockpi.io/documentations/api-reference"}}]
*/
#[test]
fn extract_max_block_range_for_blockpi_error_response() {
let result = BlockchainError::QueryFailed("RPC error: Error { code: ServerError(-32005), message: \"eth_getLogs is limited to 1024 block range. Please check the parameter requirements at https://docs.blockpi.io/documentations/api-reference\", data: None }".to_string());
let subject = BlockchainBridge::new(
Box::new(BlockchainInterfaceMock::default()),
Box::new(PersistentConfigurationMock::default()),
false,
None,
);
let max_block_count = subject.extract_max_block_count(result);

assert_eq!(Some(1024), max_block_count);
}

/*
blastapi - completely rejected call on Public endpoint as won't handle eth_getLogs method on public API
[{"jsonrpc":"2.0","id":2,"error":{"code":-32601,"message":"Method not found","data":{"method":""}}},{"jsonrpc":"2.0","id":1,"error":{"code":-32600,"message":"Invalid Request","data":{"message":"Cancelled due to validation errors in batch request"}}}] (edited)
[8:50 AM]
*/

#[test]
fn extract_max_block_range_for_blastapi_error_response() {
let result = BlockchainError::QueryFailed("RPC error: Error { code: ServerError(-32601), message: \"Method not found\", data: \"'eth_getLogs' is not available on our public API. Head over to https://docs.blastapi.io/blast-documentation/tutorials-and-guides/using-blast-to-get-a-blockchain-endpoint for more information\" }".to_string());
let subject = BlockchainBridge::new(
Box::new(BlockchainInterfaceMock::default()),
Box::new(PersistentConfigurationMock::default()),
false,
None,
);
let max_block_count = subject.extract_max_block_count(result);

assert_eq!(None, max_block_count);
}

#[test]
fn extract_max_block_range_for_expected_batch_got_single_error_response() {
let result = BlockchainError::QueryFailed(
"Got invalid response: Expected batch, got single.".to_string(),
);
let subject = BlockchainBridge::new(
Box::new(BlockchainInterfaceMock::default()),
Box::new(PersistentConfigurationMock::default()),
false,
None,
);
let max_block_count = subject.extract_max_block_count(result);

assert_eq!(Some(1000), max_block_count);
}

#[test]
fn make_connections_implements_panic_on_migration() {
Expand Down

0 comments on commit 86e4f9c

Please sign in to comment.