From 58bb7aeb5fa2543a558d33e0db66fd3d79fc759d Mon Sep 17 00:00:00 2001 From: Sam MacPherson Date: Thu, 2 Feb 2023 10:35:46 -0600 Subject: [PATCH] dont return all recorded logs for every relay --- src/domains/ArbitrumDomain.sol | 16 +++++++++------- src/domains/OptimismDomain.sol | 18 ++++++++++-------- src/domains/RecordedLogs.sol | 17 +++++++++++++++++ 3 files changed, 36 insertions(+), 15 deletions(-) diff --git a/src/domains/ArbitrumDomain.sol b/src/domains/ArbitrumDomain.sol index 3da2d90..809b826 100644 --- a/src/domains/ArbitrumDomain.sol +++ b/src/domains/ArbitrumDomain.sol @@ -116,12 +116,13 @@ contract ArbitrumDomain is BridgedDomain { selectFork(); // Read all L1 -> L2 messages and relay them under Arbitrum fork - Vm.Log[] memory logs = RecordedLogs.getLogs(); - for (; lastFromHostLogIndex < logs.length; lastFromHostLogIndex++) { - Vm.Log memory log = logs[lastFromHostLogIndex]; + Vm.Log[] memory logs = RecordedLogs.getLogs(lastFromHostLogIndex); + lastFromHostLogIndex += logs.length; + for (uint256 i = 0; i < logs.length; i++) { + Vm.Log memory log = logs[i]; if (log.topics[0] == MESSAGE_DELIVERED_TOPIC) { // We need both the current event and the one that follows for all the relevant data - Vm.Log memory logWithData = logs[lastFromHostLogIndex + 1]; + Vm.Log memory logWithData = logs[i + 1]; (,, address sender,,,) = abi.decode(log.data, (address, uint8, address, bytes32, uint256, uint64)); (address target, bytes memory message) = parseData(logWithData.data); vm.startPrank(sender); @@ -150,9 +151,10 @@ contract ArbitrumDomain is BridgedDomain { hostDomain.selectFork(); // Read all L2 -> L1 messages and relay them under host fork - Vm.Log[] memory logs = RecordedLogs.getLogs(); - for (; lastToHostLogIndex < logs.length; lastToHostLogIndex++) { - Vm.Log memory log = logs[lastToHostLogIndex]; + Vm.Log[] memory logs = RecordedLogs.getLogs(lastToHostLogIndex); + lastToHostLogIndex += logs.length; + for (uint256 i = 0; i < logs.length; i++) { + Vm.Log memory log = logs[i]; if (log.topics[0] == SEND_TO_L1_TOPIC) { (address sender, address target, bytes memory message) = abi.decode(log.data, (address, address, bytes)); l2ToL1Sender = sender; diff --git a/src/domains/OptimismDomain.sol b/src/domains/OptimismDomain.sol index aeff3ce..8e867be 100644 --- a/src/domains/OptimismDomain.sol +++ b/src/domains/OptimismDomain.sol @@ -77,11 +77,12 @@ contract OptimismDomain is BridgedDomain { } // Read all L1 -> L2 messages and relay them under Optimism fork - Vm.Log[] memory logs = RecordedLogs.getLogs(); if (!isGoerli() && lastToHostLogIndex > lastFromHostLogIndex) lastFromHostLogIndex = lastToHostLogIndex; - for (; lastFromHostLogIndex < logs.length; lastFromHostLogIndex++) { - Vm.Log memory log = logs[lastFromHostLogIndex]; - if (log.topics[0] == SENT_MESSAGE_TOPIC && (!isGoerli() || logs[lastFromHostLogIndex - 1].topics[0] == DEPOSIT_IDENTIFIER)) { + Vm.Log[] memory logs = RecordedLogs.getLogs(lastFromHostLogIndex); + lastFromHostLogIndex += logs.length; + for (uint256 i = 0; i < logs.length; i++) { + Vm.Log memory log = logs[i]; + if (log.topics[0] == SENT_MESSAGE_TOPIC && (!isGoerli() || logs[i - 1].topics[0] == DEPOSIT_IDENTIFIER)) { address target = address(uint160(uint256(log.topics[1]))); (address sender, bytes memory message, uint256 nonce, uint256 gasLimit) = abi.decode(log.data, (address, bytes, uint256, uint256)); vm.startPrank(malias); @@ -105,11 +106,12 @@ contract OptimismDomain is BridgedDomain { // Read all L2 -> L1 messages and relay them under Primary fork // Note: We bypass the L1 messenger relay here because it's easier to not have to generate valid state roots / merkle proofs - Vm.Log[] memory logs = RecordedLogs.getLogs(); if (!isGoerli() && lastFromHostLogIndex > lastToHostLogIndex) lastToHostLogIndex = lastFromHostLogIndex; - for (; lastToHostLogIndex < logs.length; lastToHostLogIndex++) { - Vm.Log memory log = logs[lastToHostLogIndex]; - if (log.topics[0] == SENT_MESSAGE_TOPIC && (!isGoerli() || logs[lastToHostLogIndex - 1].topics[0] == WITHDRAW_IDENTIFIER)) { + Vm.Log[] memory logs = RecordedLogs.getLogs(lastToHostLogIndex); + lastToHostLogIndex += logs.length; + for (uint256 i = 0; i < logs.length; i++) { + Vm.Log memory log = logs[i]; + if (log.topics[0] == SENT_MESSAGE_TOPIC && (!isGoerli() || logs[i - 1].topics[0] == WITHDRAW_IDENTIFIER)) { address target = address(uint160(uint256(log.topics[1]))); (address sender, bytes memory message,,) = abi.decode(log.data, (address, bytes, uint256, uint256)); // Set xDomainMessageSender diff --git a/src/domains/RecordedLogs.sol b/src/domains/RecordedLogs.sol index fdc80f5..c7121ad 100644 --- a/src/domains/RecordedLogs.sol +++ b/src/domains/RecordedLogs.sol @@ -34,6 +34,17 @@ contract RecordedLogsStorage { return _logs; } + function getLogs(uint256 startIndex) public view returns (Vm.Log[] memory) { + if (startIndex >= _logs.length) { + return new Vm.Log[](0); + } + Vm.Log[] memory logs = new Vm.Log[](_logs.length - startIndex); + for (uint256 i = startIndex; i < _logs.length; i++) { + logs[i - startIndex] = _logs[i]; + } + return logs; + } + } library RecordedLogs { @@ -68,4 +79,10 @@ library RecordedLogs { return STORAGE.getLogs(); } + function getLogs(uint256 startIndex) internal returns (Vm.Log[] memory) { + checkInitialized(); + STORAGE.addLogs(vm.getRecordedLogs()); + return STORAGE.getLogs(startIndex); + } + }