Skip to content

Commit

Permalink
Add custom exceptions for jsonrpc lookups that return none
Browse files Browse the repository at this point in the history
  • Loading branch information
njgheorghita committed Mar 6, 2019
1 parent 3db391e commit c2d716b
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 38 deletions.
3 changes: 2 additions & 1 deletion tests/core/eth-module/test_transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from web3.exceptions import (
TimeExhausted,
TransactionNotFound,
ValidationError,
)
from web3.middleware.simulate_unmined_transaction import (
Expand Down Expand Up @@ -53,7 +54,7 @@ def test_unmined_transaction_wait_for_receipt(web3):
'to': '0xd3CdA913deB6f67967B99D67aCDFa1712C293601',
'value': 123457
})
with pytest.raises(ValueError):
with pytest.raises(TransactionNotFound):
web3.eth.getTransactionReceipt(txn_hash)

txn_receipt = web3.eth.waitForTransactionReceipt(txn_hash)
Expand Down
10 changes: 6 additions & 4 deletions web3/_utils/module_testing/eth_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
)

from web3.exceptions import (
BlockNotFound,
InvalidAddress,
TransactionNotFound,
)

UNKNOWN_ADDRESS = '0xdEADBEeF00000000000000000000000000000000'
Expand Down Expand Up @@ -278,7 +280,7 @@ def test_eth_replaceTransaction_non_existing_transaction(
'gas': 21000,
'gasPrice': web3.eth.gasPrice,
}
with pytest.raises(ValueError):
with pytest.raises(TransactionNotFound):
web3.eth.replaceTransaction(
'0x98e8cc09b311583c5079fa600f6c2a3bea8611af168c52e4b60b5b243a441997',
txn_params
Expand Down Expand Up @@ -485,7 +487,7 @@ def test_eth_getBlockByHash(self, web3, empty_block):
assert block['hash'] == empty_block['hash']

def test_eth_getBlockByHash_not_found(self, web3, empty_block):
with pytest.raises(ValueError):
with pytest.raises(BlockNotFound):
web3.eth.getBlock(UNKNOWN_HASH)

def test_eth_getBlockByNumber_with_integer(self, web3, empty_block):
Expand All @@ -498,7 +500,7 @@ 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):
with pytest.raises(ValueError):
with pytest.raises(BlockNotFound):
web3.eth.getBlock(12345)

def test_eth_getBlockByNumber_pending(self, web3, empty_block):
Expand Down Expand Up @@ -565,7 +567,7 @@ def test_eth_getTransactionReceipt_unmined(self, web3, unlocked_account_dual_typ
'gas': 21000,
'gasPrice': web3.eth.gasPrice,
})
with pytest.raises(ValueError):
with pytest.raises(TransactionNotFound):
web3.eth.getTransactionReceipt(txn_hash)

def test_eth_getTransactionReceipt_with_log_entry(self,
Expand Down
5 changes: 4 additions & 1 deletion web3/_utils/transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
curry,
merge,
)
from web3.exceptions import (
TransactionNotFound,
)

VALID_TRANSACTION_PARAMS = [
'from',
Expand Down Expand Up @@ -66,7 +69,7 @@ def wait_for_transaction_receipt(web3, txn_hash, timeout=120, poll_latency=0.1):
while True:
try:
txn_receipt = web3.eth.getTransactionReceipt(txn_hash)
except ValueError:
except TransactionNotFound:
txn_receipt = None
# FIXME: The check for a null `blockHash` is due to parity's
# non-standard implementation of the JSON-RPC API and should
Expand Down
89 changes: 57 additions & 32 deletions web3/eth.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@
Contract,
)
from web3.exceptions import (
BlockNotFound,
TimeExhausted,
TransactionNotFound,
)
from web3.iban import (
Iban,
Expand Down Expand Up @@ -142,10 +144,13 @@ def getBlock(self, block_identifier, full_transactions=False):
if_number='eth_getBlockByNumber',
)

return self.web3.manager.request_blocking(
method,
[block_identifier, full_transactions],
)
try:
return self.web3.manager.request_blocking(
method,
[block_identifier, full_transactions],
)
except ValueError:
raise BlockNotFound(f"Block with id: {block_identifier} not found.")

def getBlockTransactionCount(self, block_identifier):
"""
Expand All @@ -158,10 +163,13 @@ def getBlockTransactionCount(self, block_identifier):
if_hash='eth_getBlockTransactionCountByHash',
if_number='eth_getBlockTransactionCountByNumber',
)
return self.web3.manager.request_blocking(
method,
[block_identifier],
)
try:
return self.web3.manager.request_blocking(
method,
[block_identifier],
)
except ValueError:
raise BlockNotFound(f"Block with id: {block_identifier} not found.")

def getUncleCount(self, block_identifier):
"""
Expand All @@ -174,10 +182,13 @@ def getUncleCount(self, block_identifier):
if_hash='eth_getUncleCountByBlockHash',
if_number='eth_getUncleCountByBlockNumber',
)
return self.web3.manager.request_blocking(
method,
[block_identifier],
)
try:
return self.web3.manager.request_blocking(
method,
[block_identifier],
)
except ValueError:
raise BlockNotFound(f"Block with id: {block_identifier} not found.")

def getUncleByBlock(self, block_identifier, uncle_index):
"""
Expand All @@ -190,16 +201,24 @@ def getUncleByBlock(self, block_identifier, uncle_index):
if_hash='eth_getUncleByBlockHashAndIndex',
if_number='eth_getUncleByBlockNumberAndIndex',
)
return self.web3.manager.request_blocking(
method,
[block_identifier, uncle_index],
)
try:
return self.web3.manager.request_blocking(
method,
[block_identifier, uncle_index],
)
except ValueError:
raise BlockNotFound(
f"Uncle at index: {uncle_index} of block with id: {block_identifier} not found."
)

def getTransaction(self, transaction_hash):
return self.web3.manager.request_blocking(
"eth_getTransactionByHash",
[transaction_hash],
)
try:
return self.web3.manager.request_blocking(
"eth_getTransactionByHash",
[transaction_hash],
)
except ValueError:
raise TransactionNotFound(f"Transaction with hash: {transaction_hash} not found.")

@deprecated_for("w3.eth.getTransactionByBlock")
def getTransactionFromBlock(self, block_identifier, transaction_index):
Expand All @@ -220,10 +239,16 @@ def getTransactionByBlock(self, block_identifier, transaction_index):
if_hash='eth_getTransactionByBlockHashAndIndex',
if_number='eth_getTransactionByBlockNumberAndIndex',
)
return self.web3.manager.request_blocking(
method,
[block_identifier, transaction_index],
)
try:
return self.web3.manager.request_blocking(
method,
[block_identifier, transaction_index],
)
except ValueError:
raise TransactionNotFound(
f"Transaction index: {transaction_index} "
f"on block id: {block_identifier} not found."
)

def waitForTransactionReceipt(self, transaction_hash, timeout=120):
try:
Expand All @@ -237,20 +262,20 @@ def waitForTransactionReceipt(self, transaction_hash, timeout=120):
)

def getTransactionReceipt(self, transaction_hash):
return self.web3.manager.request_blocking(
"eth_getTransactionReceipt",
[transaction_hash],
)
try:
return self.web3.manager.request_blocking(
"eth_getTransactionReceipt",
[transaction_hash],
)
except ValueError:
raise TransactionNotFound(f"Transaction with hash: {transaction_hash} not found.")

def getTransactionCount(self, account, block_identifier=None):
if block_identifier is None:
block_identifier = self.defaultBlock
return self.web3.manager.request_blocking(
"eth_getTransactionCount",
[
account,
block_identifier,
],
[account, block_identifier],
)

def replaceTransaction(self, transaction_hash, new_transaction):
Expand Down
14 changes: 14 additions & 0 deletions web3/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,17 @@ class ManifestValidationError(PMError):
Raised when a provided manifest cannot be published, since it's invalid.
"""
pass


class TransactionNotFound(Exception):
"""
Raised when a tx hash used to lookup a tx in a jsonrpc call cannot be found.
"""
pass


class BlockNotFound(Exception):
"""
Raised when a block id used to lookup a block in a jsonrpc call cannot be found.
"""
pass

0 comments on commit c2d716b

Please sign in to comment.