diff --git a/web3sTests/Client/EthereumClientTests.swift b/web3sTests/Client/EthereumClientTests.swift index dfb6ad51..6225f5c7 100644 --- a/web3sTests/Client/EthereumClientTests.swift +++ b/web3sTests/Client/EthereumClientTests.swift @@ -149,11 +149,29 @@ class EthereumClientTests: XCTestCase { do { let logs = try await client?.eth_getLogs(addresses: ["0x23d0a442580c01e420270fba6ca836a8b2353acb"], topics: nil, fromBlock: .Earliest, toBlock: .Latest) XCTAssertNotNil(logs, "Logs not available") + XCTAssertEqual(logs?.count, 0) } catch { XCTFail("Expected logs but failed \(error).") } } + func testSimpleEthGetLogsWithFailingRequest() async { + guard let client = client else { + XCTFail() + return + } + await XCTAssertThrowsErrorAsync( + try await client.eth_getLogs( + addresses: ["0x3edf60dd017ace33a0220f78741b5581c385a1ba"], + topics: [try? ERC20Events.Transfer.signature()], + fromBlock: .Earliest, + toBlock: .Latest + ), + EthereumClientError.unexpectedReturnValue, + "Too many logs to receive. Should throw an error instead of defaulting to empty array" + ) + } + func testOrTopicsEthGetLogs() async { do { let logs = try await client?.eth_getLogs(addresses: nil, orTopics: [["0x8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925", "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"], ["0x00000000000000000000000061CA935f8b7847C4a1275814d8D88CDa8d406CC9"]], fromBlock: .Number(4902849), toBlock: .Number(4902849)) @@ -606,3 +624,18 @@ extension EthereumWebSocketClientTests: EthereumWebSocketClientDelegate { delegateExpectation = nil } } + +func XCTAssertThrowsErrorAsync( + _ expression: @autoclosure () async throws -> T, + _ errorThrown: @autoclosure () -> R, + _ message: @autoclosure () -> String = "This method should fail", + file: StaticString = #filePath, + line: UInt = #line +) async where R: Equatable, R: Error { + do { + let _ = try await expression() + XCTFail(message(), file: file, line: line) + } catch { + XCTAssertEqual(error as? R, errorThrown()) + } +} diff --git a/web3swift/src/Client/RecursiveLogCollector.swift b/web3swift/src/Client/RecursiveLogCollector.swift index a6489128..92bae571 100644 --- a/web3swift/src/Client/RecursiveLogCollector.swift +++ b/web3swift/src/Client/RecursiveLogCollector.swift @@ -37,9 +37,10 @@ struct RecursiveLogCollector { throw EthereumClientError.unexpectedReturnValue } return lhs + rhs + } else { + throw error } } - return [] } private func getLogs(addresses: [EthereumAddress]?, topics: Topics? = nil, from: EthereumBlock, to: EthereumBlock) async throws -> [EthereumLog] {