Skip to content

Commit

Permalink
Merge pull request #852 from eth-brownie/fix-chain-gettx
Browse files Browse the repository at this point in the history
Fix: TransactionReceipt for externally broadcasted tx's
  • Loading branch information
iamdefinitelyahuman committed Nov 21, 2020
2 parents b7c7253 + 0c90744 commit 4beaefa
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
29 changes: 18 additions & 11 deletions brownie/network/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,10 +310,12 @@ def wait(self, required_confs: int) -> None:
tx: Dict = web3.eth.getTransaction(self.txid)
break
except TransactionNotFound:
if self.sender.nonce > self.nonce: # type: ignore
self.status = Status(-2)
print("This transaction was replaced.")
return
if self.nonce is not None:
sender_nonce = web3.eth.getTransactionCount(str(self.sender))
if sender_nonce > self.nonce:
self.status = Status(-2)
print("This transaction was replaced.")
return
time.sleep(1)

self._await_confirmation(tx, required_confs)
Expand Down Expand Up @@ -343,10 +345,13 @@ def _await_transaction(self, required_confs: int, is_blocking: bool) -> None:
# if sender was not explicitly set, this transaction was
# not broadcasted locally and so likely doesn't exist
raise
if self.nonce is not None and self.sender.nonce > self.nonce:
self.status = Status(-2)
return
if self.nonce is not None:
sender_nonce = web3.eth.getTransactionCount(str(self.sender))
if sender_nonce > self.nonce:
self.status = Status(-2)
return
time.sleep(1)

self._set_from_tx(tx)

if not self._silent:
Expand All @@ -356,12 +361,13 @@ def _await_transaction(self, required_confs: int, is_blocking: bool) -> None:
f" Nonce: {color('bright blue')}{self.nonce}{color}"
)

# await confirmation of tx in a separate thread which is blocking if required_confs > 0
# await confirmation of tx in a separate thread which is blocking if
# required_confs > 0 or tx has already confirmed (`blockNumber` != None)
confirm_thread = threading.Thread(
target=self._await_confirmation, args=(tx, required_confs), daemon=True
)
confirm_thread.start()
if is_blocking and required_confs > 0:
if is_blocking and (required_confs > 0 or tx["blockNumber"]):
confirm_thread.join()

def _await_confirmation(self, tx: Dict, required_confs: int = 1) -> None:
Expand All @@ -378,10 +384,11 @@ def _await_confirmation(self, tx: Dict, required_confs: int = 1) -> None:
# await first confirmation
while True:
# if sender nonce is greater than tx nonce, the tx should be confirmed
expect_confirmed = bool(self.sender.nonce > self.nonce) # type: ignore
sender_nonce = web3.eth.getTransactionCount(str(self.sender))
expect_confirmed = bool(sender_nonce > self.nonce) # type: ignore
try:
receipt = web3.eth.waitForTransactionReceipt(
HexBytes(self.txid), timeout=30, poll_latency=1
HexBytes(self.txid), timeout=15, poll_latency=1
)
break
except TimeExhausted:
Expand Down
14 changes: 14 additions & 0 deletions tests/network/state/test_get_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,17 @@ def test_not_in_history(accounts, chain, history):
def test_unknown_tx(accounts, chain, history):
with pytest.raises(TransactionNotFound):
chain.get_transaction("0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421")


def test_external_tx(network, chain):
network.connect("mainnet")

tx = chain.get_transaction("0x1e0e3df9daa09d009185a1d009b905a9264e296f2d9c8cf6e8a2d0723df249a3")
assert tx.status == 1


def test_external_tx_reverted(network, chain):
network.connect("mainnet")

tx = chain.get_transaction("0x7c913d12a7692889c364913b7909806de05692abc9312b718f16f444e4a6b94b")
assert tx.status == 0

0 comments on commit 4beaefa

Please sign in to comment.