Skip to content

Commit

Permalink
add checking ReceiptReceived event emission
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew Lam committed Dec 22, 2023
1 parent 9fde675 commit 3eab0a9
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 11 deletions.
18 changes: 9 additions & 9 deletions tests/flows/basic_send_receive.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 13 additions & 2 deletions tests/flows/send_specific_receipts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 :=
Expand All @@ -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 =
Expand All @@ -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.
Expand Down Expand Up @@ -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())
Expand Down
14 changes: 14 additions & 0 deletions tests/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 3eab0a9

Please sign in to comment.