From 6d1ae8f4175e836b591de3b774dbd14b21913d7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Claudio=20Nale?= Date: Mon, 6 Feb 2023 18:00:47 -0300 Subject: [PATCH] Filter EVM logs in unit tests to just wormhole publish message logs. --- ethereum/forge-test/CoreRelayer.t.sol | 14 +++++++++----- ethereum/forge-test/WormholeSimulator.sol | 14 ++++++++++---- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/ethereum/forge-test/CoreRelayer.t.sol b/ethereum/forge-test/CoreRelayer.t.sol index db1df9b9..68d2ee67 100644 --- a/ethereum/forge-test/CoreRelayer.t.sol +++ b/ethereum/forge-test/CoreRelayer.t.sol @@ -1219,12 +1219,16 @@ contract TestCoreRelayer is Test { mapping(bytes32 => ICoreRelayer.TargetDeliveryParametersSingle) pastDeliveries; function genericRelayer(uint16 chainId, uint8 num) internal { - Vm.Log[] memory entries = vm.getRecordedLogs(); bytes[] memory encodedVMs = new bytes[](num); - for (uint256 i = 0; i < num; i++) { - encodedVMs[i] = relayerWormholeSimulator.fetchSignedMessageFromLogs( - entries[i], chainId, address(uint160(uint256(bytes32(entries[i].topics[1])))) - ); + { + // Filters all events to just the wormhole messages. + Vm.Log[] memory entries = relayerWormholeSimulator.fetchWormholeMessageFromLog(vm.getRecordedLogs()); + assertTrue(entries.length >= num); + for (uint256 i = 0; i < num; i++) { + encodedVMs[i] = relayerWormholeSimulator.fetchSignedMessageFromLogs( + entries[i], chainId, address(uint160(uint256(bytes32(entries[i].topics[1])))) + ); + } } IWormhole.VM[] memory parsed = new IWormhole.VM[](encodedVMs.length); diff --git a/ethereum/forge-test/WormholeSimulator.sol b/ethereum/forge-test/WormholeSimulator.sol index f313d905..2aa4fc3d 100644 --- a/ethereum/forge-test/WormholeSimulator.sol +++ b/ethereum/forge-test/WormholeSimulator.sol @@ -143,17 +143,23 @@ contract WormholeSimulator { /** * @notice Finds published Wormhole events in forge logs * @param logs The forge Vm.log captured when recording events during test execution - * @param numMessages The expected number of Wormhole events in the forge logs */ - function fetchWormholeMessageFromLog(Vm.Log[] memory logs, uint8 numMessages) + function fetchWormholeMessageFromLog(Vm.Log[] memory logs) public pure returns (Vm.Log[] memory) { + uint256 count = 0; + for (uint256 i = 0; i < logs.length; i++) { + if (logs[i].topics[0] == keccak256("LogMessagePublished(address,uint64,uint32,bytes,uint8)")) { + count += 1; + } + } + // create log array to save published messages - Vm.Log[] memory published = new Vm.Log[](numMessages); + Vm.Log[] memory published = new Vm.Log[](count); - uint8 publishedIndex = 0; + uint256 publishedIndex = 0; for (uint256 i = 0; i < logs.length; i++) { if (logs[i].topics[0] == keccak256("LogMessagePublished(address,uint64,uint32,bytes,uint8)")) { published[publishedIndex] = logs[i];