From a40ff770635b35dd3ef29b7e6df3d1e29e3b4a4c Mon Sep 17 00:00:00 2001 From: MaxMustermann2 <82761650+MaxMustermann2@users.noreply.github.com> Date: Thu, 27 Jun 2024 22:20:06 +0000 Subject: [PATCH] refactor: make `nextNonce` implementation common while the overrides still need to be present to inform the compiler about the base class whose function should be called, the implementation can still be made common in the base class. --- src/core/BootstrapLzReceiver.sol | 6 ++++-- src/core/ClientChainGateway.sol | 13 +++++++++++++ src/core/ClientGatewayLzReceiver.sol | 6 ++++-- src/core/ExocoreGateway.sol | 8 ++++++-- src/storage/GatewayStorage.sol | 4 ++++ test/mocks/ExocoreGatewayMock.sol | 6 ++++-- 6 files changed, 35 insertions(+), 8 deletions(-) diff --git a/src/core/BootstrapLzReceiver.sol b/src/core/BootstrapLzReceiver.sol index a27bc2bd..bd79fe82 100644 --- a/src/core/BootstrapLzReceiver.sol +++ b/src/core/BootstrapLzReceiver.sol @@ -1,7 +1,9 @@ pragma solidity ^0.8.19; import {OAppReceiverUpgradeable, Origin} from "../lzApp/OAppReceiverUpgradeable.sol"; + import {BootstrapStorage} from "../storage/BootstrapStorage.sol"; +import {GatewayStorage} from "../storage/GatewayStorage.sol"; import {PausableUpgradeable} from "@openzeppelin-upgradeable/contracts/utils/PausableUpgradeable.sol"; abstract contract BootstrapLzReceiver is PausableUpgradeable, OAppReceiverUpgradeable, BootstrapStorage { @@ -35,10 +37,10 @@ abstract contract BootstrapLzReceiver is PausableUpgradeable, OAppReceiverUpgrad public view virtual - override(OAppReceiverUpgradeable) + override(GatewayStorage, OAppReceiverUpgradeable) returns (uint64) { - return inboundNonce[srcEid][sender] + 1; + return GatewayStorage.nextNonce(srcEid, sender); } } diff --git a/src/core/ClientChainGateway.sol b/src/core/ClientChainGateway.sol index edd05f8a..18260cba 100644 --- a/src/core/ClientChainGateway.sol +++ b/src/core/ClientChainGateway.sol @@ -6,12 +6,15 @@ import {OAppReceiverUpgradeable} from "../lzApp/OAppReceiverUpgradeable.sol"; import {MessagingFee, OAppSenderUpgradeable} from "../lzApp/OAppSenderUpgradeable.sol"; import {ClientChainGatewayStorage} from "../storage/ClientChainGatewayStorage.sol"; + +import {GatewayStorage} from "../storage/GatewayStorage.sol"; import {ClientGatewayLzReceiver} from "./ClientGatewayLzReceiver.sol"; import {LSTRestakingController} from "./LSTRestakingController.sol"; import {NativeRestakingController} from "./NativeRestakingController.sol"; import {IOAppCore} from "@layerzero-v2/oapp/contracts/oapp/interfaces/IOAppCore.sol"; import {OptionsBuilder} from "@layerzero-v2/oapp/contracts/oapp/libs/OptionsBuilder.sol"; +import {ILayerZeroReceiver} from "@layerzero-v2/protocol/contracts/interfaces/ILayerZeroReceiver.sol"; import {OwnableUpgradeable} from "@openzeppelin-upgradeable/contracts/access/OwnableUpgradeable.sol"; import {Initializable} from "@openzeppelin-upgradeable/contracts/proxy/utils/Initializable.sol"; import {PausableUpgradeable} from "@openzeppelin-upgradeable/contracts/utils/PausableUpgradeable.sol"; @@ -167,4 +170,14 @@ contract ClientChainGateway is return (SENDER_VERSION, RECEIVER_VERSION); } + function nextNonce(uint32 srcEid, bytes32 sender) + public + view + virtual + override(ClientGatewayLzReceiver, GatewayStorage, ILayerZeroReceiver) + returns (uint64) + { + return GatewayStorage.nextNonce(srcEid, sender); + } + } diff --git a/src/core/ClientGatewayLzReceiver.sol b/src/core/ClientGatewayLzReceiver.sol index 354cf569..66c77e85 100644 --- a/src/core/ClientGatewayLzReceiver.sol +++ b/src/core/ClientGatewayLzReceiver.sol @@ -3,7 +3,9 @@ pragma solidity ^0.8.19; import {IExoCapsule} from "../interfaces/IExoCapsule.sol"; import {IVault} from "../interfaces/IVault.sol"; import {OAppReceiverUpgradeable, Origin} from "../lzApp/OAppReceiverUpgradeable.sol"; + import {ClientChainGatewayStorage} from "../storage/ClientChainGatewayStorage.sol"; +import {GatewayStorage} from "../storage/GatewayStorage.sol"; import {PausableUpgradeable} from "@openzeppelin-upgradeable/contracts/utils/PausableUpgradeable.sol"; @@ -69,10 +71,10 @@ abstract contract ClientGatewayLzReceiver is PausableUpgradeable, OAppReceiverUp public view virtual - override(OAppReceiverUpgradeable) + override(GatewayStorage, OAppReceiverUpgradeable) returns (uint64) { - return inboundNonce[srcEid][sender] + 1; + return GatewayStorage.nextNonce(srcEid, sender); } function afterReceiveDepositResponse(bytes memory requestPayload, bytes calldata responsePayload) diff --git a/src/core/ExocoreGateway.sol b/src/core/ExocoreGateway.sol index ac6a6036..42f2e3a6 100644 --- a/src/core/ExocoreGateway.sol +++ b/src/core/ExocoreGateway.sol @@ -13,7 +13,9 @@ import { OAppUpgradeable, Origin } from "../lzApp/OAppUpgradeable.sol"; + import {ExocoreGatewayStorage} from "../storage/ExocoreGatewayStorage.sol"; +import {GatewayStorage} from "../storage/GatewayStorage.sol"; import {OAppCoreUpgradeable} from "../lzApp/OAppCoreUpgradeable.sol"; import {IOAppCore} from "@layerzero-v2/oapp/contracts/oapp/interfaces/IOAppCore.sol"; @@ -344,10 +346,12 @@ contract ExocoreGateway is public view virtual - override(ILayerZeroReceiver, OAppReceiverUpgradeable) + // since there are 3 contracts with this function, it must be defined so that + // the compiler knows which one to call + override(GatewayStorage, ILayerZeroReceiver, OAppReceiverUpgradeable) returns (uint64) { - return inboundNonce[srcEid][sender] + 1; + return GatewayStorage.nextNonce(srcEid, sender); } } diff --git a/src/storage/GatewayStorage.sol b/src/storage/GatewayStorage.sol index c5d9e21c..ab306d5b 100644 --- a/src/storage/GatewayStorage.sol +++ b/src/storage/GatewayStorage.sol @@ -35,4 +35,8 @@ contract GatewayStorage { inboundNonce[srcChainId][srcAddress] = nonce; } + function nextNonce(uint32 srcEid, bytes32 sender) public view virtual returns (uint64) { + return inboundNonce[srcEid][sender] + 1; + } + } diff --git a/test/mocks/ExocoreGatewayMock.sol b/test/mocks/ExocoreGatewayMock.sol index 94842aa9..68ac2fc4 100644 --- a/test/mocks/ExocoreGatewayMock.sol +++ b/test/mocks/ExocoreGatewayMock.sol @@ -13,7 +13,9 @@ import { OAppUpgradeable, Origin } from "src/lzApp/OAppUpgradeable.sol"; + import {ExocoreGatewayStorage} from "src/storage/ExocoreGatewayStorage.sol"; +import {GatewayStorage} from "src/storage/GatewayStorage.sol"; import {IOAppCore} from "@layerzero-v2/oapp/contracts/oapp/interfaces/IOAppCore.sol"; import {OptionsBuilder} from "@layerzero-v2/oapp/contracts/oapp/libs/OptionsBuilder.sol"; @@ -335,10 +337,10 @@ contract ExocoreGatewayMock is public view virtual - override(ILayerZeroReceiver, OAppReceiverUpgradeable) + override(GatewayStorage, ILayerZeroReceiver, OAppReceiverUpgradeable) returns (uint64) { - return inboundNonce[srcEid][sender] + 1; + return GatewayStorage.nextNonce(srcEid, sender); } }