Skip to content

Commit

Permalink
Raise exceptions in getBlock/getTransaction if result from rpc call i…
Browse files Browse the repository at this point in the history
…s none
  • Loading branch information
njgheorghita committed Feb 26, 2019
1 parent 07275e8 commit 5f3eb60
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,8 @@ def test_latest_block_based_cache_middleware_busts_cache(w3, mocker):
result = w3.manager.request_blocking('fake_endpoint', [])

assert w3.manager.request_blocking('fake_endpoint', []) == result
w3.testing.mine()
with pytest.raises(ValueError):
w3.testing.mine()

# should still be cached for at least 1 second. This also verifies that
# the middleware caches the latest block based on the block time.
Expand Down Expand Up @@ -211,8 +212,10 @@ def result_cb(method, params):
}))
w3.middleware_onion.add(latest_block_based_cache_middleware)

w3.manager.request_blocking('fake_endpoint', [])
w3.manager.request_blocking('fake_endpoint', [])
with pytest.raises(ValueError):
w3.manager.request_blocking('fake_endpoint', [])
with pytest.raises(ValueError):
w3.manager.request_blocking('fake_endpoint', [])

assert next(counter) == 2

Expand Down
6 changes: 4 additions & 2 deletions tests/core/middleware/test_simple_cache_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,10 @@ def result_cb(method, params):
rpc_whitelist={'fake_endpoint'},
))

w3.manager.request_blocking('fake_endpoint', [])
w3.manager.request_blocking('fake_endpoint', [])
with pytest.raises(ValueError):
w3.manager.request_blocking('fake_endpoint', [])
with pytest.raises(ValueError):
w3.manager.request_blocking('fake_endpoint', [])

assert next(counter) == 2

Expand Down
6 changes: 4 additions & 2 deletions tests/core/middleware/test_time_based_cache_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,10 @@ def mk_result(method, params):
w3.middleware_onion.add(construct_result_generator_middleware({'fake_endpoint': mk_result}))
w3.middleware_onion.add(time_cache_middleware)

w3.manager.request_blocking('fake_endpoint', [])
w3.manager.request_blocking('fake_endpoint', [])
with pytest.raises(ValueError):
w3.manager.request_blocking('fake_endpoint', [])
with pytest.raises(ValueError):
w3.manager.request_blocking('fake_endpoint', [])

assert next(counter) == 2

Expand Down
9 changes: 7 additions & 2 deletions tests/core/testing-module/test_testing_snapshot_and_revert.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import pytest


def test_snapshot_revert_to_latest_snapshot(web3):
web3.testing.mine(5)

Expand All @@ -11,7 +14,8 @@ def test_snapshot_revert_to_latest_snapshot(web3):

block_after_mining = web3.eth.getBlock("latest")

web3.testing.revert()
with pytest.raises(ValueError):
web3.testing.revert()

block_after_revert = web3.eth.getBlock("latest")

Expand All @@ -38,7 +42,8 @@ def test_snapshot_revert_to_specific(web3):

block_after_mining = web3.eth.getBlock("latest")

web3.testing.revert(snapshot_idx)
with pytest.raises(ValueError):
web3.testing.revert(snapshot_idx)

block_after_revert = web3.eth.getBlock("latest")

Expand Down
6 changes: 5 additions & 1 deletion tests/core/testing-module/test_testing_timeTravel.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import pytest


def test_time_traveling(web3):
current_block_time = web3.eth.getBlock("pending")['timestamp']

time_travel_to = current_block_time + 12345

web3.testing.timeTravel(time_travel_to)
with pytest.raises(ValueError):
web3.testing.timeTravel(time_travel_to)

latest_block_time = web3.eth.getBlock("pending")['timestamp']
assert latest_block_time >= time_travel_to
8 changes: 4 additions & 4 deletions web3/_utils/module_testing/eth_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,8 +485,8 @@ def test_eth_getBlockByHash(self, web3, empty_block):
assert block['hash'] == empty_block['hash']

def test_eth_getBlockByHash_not_found(self, web3, empty_block):
block = web3.eth.getBlock(UNKNOWN_HASH)
assert block is None
with pytest.raises(ValueError):
web3.eth.getBlock(UNKNOWN_HASH)

def test_eth_getBlockByNumber_with_integer(self, web3, empty_block):
block = web3.eth.getBlock(empty_block['number'])
Expand All @@ -498,8 +498,8 @@ def test_eth_getBlockByNumber_latest(self, web3, empty_block):
assert block['number'] == current_block_number

def test_eth_getBlockByNumber_not_found(self, web3, empty_block):
block = web3.eth.getBlock(12345)
assert block is None
with pytest.raises(ValueError):
web3.eth.getBlock(12345)

def test_eth_getBlockByNumber_pending(self, web3, empty_block):
current_block_number = web3.eth.blockNumber
Expand Down
4 changes: 4 additions & 0 deletions web3/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ def request_blocking(self, method, params):
if "error" in response:
raise ValueError(response["error"])

# Raise exception for all jsonrpc calls that return None except getTransactionReceipt
if method != 'eth_getTransactionReceipt' and response['result'] is None:
raise ValueError(f"The call to {method} did not return a value.")

return response['result']

async def coro_request(self, method, params):
Expand Down

0 comments on commit 5f3eb60

Please sign in to comment.