From 3eab0a9518f2105efc6ff3c73998113f5220c474 Mon Sep 17 00:00:00 2001 From: Matthew Lam Date: Fri, 22 Dec 2023 20:21:22 +0000 Subject: [PATCH] add checking ReceiptReceived event emission --- tests/flows/basic_send_receive.go | 18 +++++++++--------- tests/flows/send_specific_receipts.go | 15 +++++++++++++-- tests/utils/utils.go | 14 ++++++++++++++ 3 files changed, 36 insertions(+), 11 deletions(-) diff --git a/tests/flows/basic_send_receive.go b/tests/flows/basic_send_receive.go index 803526ca2..df697920f 100644 --- a/tests/flows/basic_send_receive.go +++ b/tests/flows/basic_send_receive.go @@ -97,15 +97,15 @@ func BasicSendReceive(network interfaces.Network) { // deliveryReceipt = network.RelayMessage(ctx, receipt, subnetBInfo, subnetAInfo, true) - // Check that the receipt was received for expected Teleporter message id - receiptEvent, err := utils.GetEventFromLogs( - deliveryReceipt.Logs, - subnetAInfo.TeleporterMessenger.ParseReceiptReceived) - Expect(err).Should(BeNil()) - Expect(receiptEvent.MessageID).Should(Equal(expectedReceiptID)) - Expect(receiptEvent.DestinationBlockchainID).Should(Equal(subnetBInfo.BlockchainID)) - Expect(receiptEvent.FeeInfo.Amount).Should(Equal(feeAmount)) - Expect(receiptEvent.FeeInfo.FeeTokenAddress).Should(Equal(feeTokenAddress)) + // Check that the receipt was received for expected Teleporter message ID + // This check is not performed for external networks because unrelated messages may have already changed + // the state of the receipt queues. + if !network.IsExternalNetwork() { + Expect(utils.CheckReceiptReceived( + deliveryReceipt, + expectedReceiptID, + subnetAInfo.TeleporterMessenger)).Should(BeTrue()) + } // // Check Teleporter message received on the destination diff --git a/tests/flows/send_specific_receipts.go b/tests/flows/send_specific_receipts.go index 10c9092d1..07175d272 100644 --- a/tests/flows/send_specific_receipts.go +++ b/tests/flows/send_specific_receipts.go @@ -68,6 +68,7 @@ func SendSpecificReceipts(network interfaces.Network) { deliveryReceipt1.Logs, subnetBInfo.TeleporterMessenger.ParseReceiveCrossChainMessage) Expect(err).Should(BeNil()) + Expect(receiveEvent1.MessageID).Should(Equal(messageID1)) // Check that the first message was delivered delivered, err := @@ -85,6 +86,7 @@ func SendSpecificReceipts(network interfaces.Network) { deliveryReceipt2.Logs, subnetBInfo.TeleporterMessenger.ParseReceiveCrossChainMessage) Expect(err).Should(BeNil()) + Expect(receiveEvent2.MessageID).Should(Equal(messageID2)) // Check that the second message was delivered delivered, err = @@ -107,13 +109,17 @@ func SendSpecificReceipts(network interfaces.Network) { ) // Relay message from Subnet B to Subnet A - network.RelayMessage(ctx, receipt, subnetBInfo, subnetAInfo, true) + receipt = network.RelayMessage(ctx, receipt, subnetBInfo, subnetAInfo, true) // Check that the message back to Subnet A was delivered delivered, err = subnetAInfo.TeleporterMessenger.MessageReceived(&bind.CallOpts{}, subnetBInfo.BlockchainID, messageID) Expect(err).Should(BeNil()) Expect(delivered).Should(BeTrue()) + // Check that the expected receipts were received and emitted ReceiptReceived + Expect(utils.CheckReceiptReceived(receipt, messageID1, subnetAInfo.TeleporterMessenger)).Should(BeTrue()) + Expect(utils.CheckReceiptReceived(receipt, messageID2, subnetAInfo.TeleporterMessenger)).Should(BeTrue()) + // Check the reward amounts. // Even on external networks, the relayer should only have the expected fee amount // for this asset because the asset contract was newly deployed by this test. @@ -150,7 +156,12 @@ func SendSpecificReceipts(network interfaces.Network) { messageID) Expect(err).Should(BeNil()) Expect(delivered).Should(BeTrue()) - // Get the Teleporter message from receive event and confirm that the receipts are delivered again + + // Check that the expected receipts were included in the message but did not emit ReceiptReceived + // because they were previously received + Expect(utils.CheckReceiptReceived(receipt, messageID1, subnetAInfo.TeleporterMessenger)).Should(BeFalse()) + Expect(utils.CheckReceiptReceived(receipt, messageID2, subnetAInfo.TeleporterMessenger)).Should(BeFalse()) + receiveEvent, err := utils.GetEventFromLogs(receipt.Logs, subnetAInfo.TeleporterMessenger.ParseReceiveCrossChainMessage) Expect(err).Should(BeNil()) diff --git a/tests/utils/utils.go b/tests/utils/utils.go index 18427445d..fb954a9a9 100644 --- a/tests/utils/utils.go +++ b/tests/utils/utils.go @@ -465,6 +465,20 @@ func GetEventFromLogs[T any](logs []*types.Log, parser func(log types.Log) (T, e return *new(T), fmt.Errorf("failed to find %T event in receipt logs", *new(T)) } +// Returns true if the transaction receipt contains a ReceiptReceived log with the specified messageID +func CheckReceiptReceived( + receipt *types.Receipt, + messageID *big.Int, + transactor *teleportermessenger.TeleporterMessenger) bool { + for _, log := range receipt.Logs { + event, err := transactor.ParseReceiptReceived(*log) + if err == nil && event.MessageID.Cmp(messageID) == 0 { + return true + } + } + return false +} + // Signs a transaction using the provided key for the specified chainID func SignTransaction(tx *types.Transaction, key *ecdsa.PrivateKey, chainID *big.Int) *types.Transaction { txSigner := types.LatestSignerForChainID(chainID)