Skip to content

Commit

Permalink
PR review
Browse files Browse the repository at this point in the history
  • Loading branch information
palango committed Aug 8, 2019
1 parent 916dc67 commit 0399a04
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 33 deletions.
45 changes: 22 additions & 23 deletions src/monitoring_service/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,31 +178,30 @@ def _check_pending_transactions(self) -> None:
smallest nonce, and continue from there when this one is mined and confirmed. However,
as it is not expected that this list becomes to big this isn't optimized currently.
"""

for tx_hash in self.context.db.get_waiting_transactions():
receipt = self.web3.eth.getTransactionReceipt(tx_hash)
if receipt is not None:
tx_block = receipt.get("blockNumber")
if tx_block is None:
return

confirmation_block = tx_block + self.context.required_confirmations
if self.context.last_known_block < confirmation_block:
return

self.context.db.remove_waiting_transaction(tx_hash)
if receipt["status"] == 1:
log.info(
"Transaction was mined successfully",
transaction_hash=tx_hash,
receipt=receipt,
)
else:
log.error(
"Transaction was not mined successfully",
transaction_hash=tx_hash,
receipt=receipt,
)
if receipt is None:
continue

tx_block = receipt.get("blockNumber")
if tx_block is None:
continue

confirmation_block = tx_block + self.context.required_confirmations
if self.web3.eth.blockNumber < confirmation_block:
continue

self.context.db.remove_waiting_transaction(tx_hash)
if receipt["status"] == 1:
log.info(
"Transaction was mined successfully", transaction_hash=tx_hash, receipt=receipt
)
else:
log.error(
"Transaction was not mined successfully",
transaction_hash=tx_hash,
receipt=receipt,
)

def _purge_old_monitor_requests(self) -> None:
""" Delete all old MRs for which still no channel exists.
Expand Down
17 changes: 7 additions & 10 deletions tests/monitoring/monitoring_service/test_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,11 @@ def test_check_pending_transactions(web3, wait_for_blocks, monitoring_service: M

for tx_status in (0, 1):
tx_receipt = {"blockNumber": web3.eth.blockNumber, "status": tx_status}
with patch.object(web3.eth, "getTransactionReceipt", Mock(return_value=tx_receipt)):
with patch.object(
monitoring_service.database, "remove_waiting_transaction"
) as remove_mock:
with patch.object(
web3.eth, "getTransactionReceipt", Mock(return_value=tx_receipt)
), patch.object(monitoring_service.database, "remove_waiting_transaction") as remove_mock:
for should_call in (False, False, False, True):
monitoring_service._check_pending_transactions() # pylint: disable=protected-access # noqa

for should_call in (False, False, False, True):
monitoring_service.context.last_known_block = web3.eth.blockNumber
monitoring_service._check_pending_transactions() # pylint: disable=protected-access # noqa

assert remove_mock.called == should_call
wait_for_blocks(1)
assert remove_mock.called == should_call
wait_for_blocks(1)

0 comments on commit 0399a04

Please sign in to comment.