From 9557a66dce6104e794a7ab20172738954d4315ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Bene=C5=A1?= Date: Thu, 23 Nov 2023 14:14:02 +0100 Subject: [PATCH] fix: race condition in `PXE.getTxReceipt(...)` (#3411) --- .../pxe/src/pxe_service/pxe_service.ts | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/yarn-project/pxe/src/pxe_service/pxe_service.ts b/yarn-project/pxe/src/pxe_service/pxe_service.ts index 851f2a53330..a977623e5ef 100644 --- a/yarn-project/pxe/src/pxe_service/pxe_service.ts +++ b/yarn-project/pxe/src/pxe_service/pxe_service.ts @@ -355,13 +355,23 @@ export class PXEService implements PXE { } public async getTxReceipt(txHash: TxHash): Promise { + let txReceipt = new TxReceipt(txHash, TxStatus.DROPPED, 'Tx dropped by P2P node.'); + + // We first check if the tx is in pending (instead of first checking if it is mined) because if we first check + // for mined and then for pending there could be a race condition where the tx is mined between the two checks + // and we would incorrectly return a TxReceipt with status DROPPED + const pendingTx = await this.node.getPendingTxByHash(txHash); + if (pendingTx) { + txReceipt = new TxReceipt(txHash, TxStatus.PENDING, ''); + } + const settledTx = await this.node.getTx(txHash); if (settledTx) { const deployedContractAddress = settledTx.newContractData.find( c => !c.contractAddress.equals(AztecAddress.ZERO), )?.contractAddress; - return new TxReceipt( + txReceipt = new TxReceipt( txHash, TxStatus.MINED, '', @@ -371,12 +381,7 @@ export class PXEService implements PXE { ); } - const pendingTx = await this.node.getPendingTxByHash(txHash); - if (pendingTx) { - return new TxReceipt(txHash, TxStatus.PENDING, ''); - } - - return new TxReceipt(txHash, TxStatus.DROPPED, 'Tx dropped by P2P node.'); + return txReceipt; } public async getTx(txHash: TxHash): Promise {