From 099cd93cecbf079d7fd16a8d2ec8d84cc1d2351d Mon Sep 17 00:00:00 2001 From: Trevor Porter Date: Thu, 2 Jun 2022 20:01:51 +0100 Subject: [PATCH] Use OZ's ReentrancyGuardUpgradeable in Inbox (#499) * Put entered into its own slot to prevent SLOADing before each SSTORE * Use OZ ReentrancyGuardUpgradeable Co-authored-by: Yorke Rhodes --- solidity/core/contracts/Inbox.sol | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/solidity/core/contracts/Inbox.sol b/solidity/core/contracts/Inbox.sol index a24f30a07..93796b74b 100644 --- a/solidity/core/contracts/Inbox.sol +++ b/solidity/core/contracts/Inbox.sol @@ -10,13 +10,16 @@ import {TypeCasts} from "../libs/TypeCasts.sol"; import {IMessageRecipient} from "../interfaces/IMessageRecipient.sol"; import {IInbox} from "../interfaces/IInbox.sol"; +// ============ External Imports ============ +import {ReentrancyGuardUpgradeable} from "@openzeppelin/contracts-upgradeable/security/ReentrancyGuardUpgradeable.sol"; + /** * @title Inbox * @author Celo Labs Inc. * @notice Track root updates on Outbox, prove and dispatch messages to end * recipients. */ -contract Inbox is IInbox, Version0, Mailbox { +contract Inbox is IInbox, ReentrancyGuardUpgradeable, Version0, Mailbox { // ============ Libraries ============ using MerkleLib for MerkleLib.Tree; @@ -37,15 +40,13 @@ contract Inbox is IInbox, Version0, Mailbox { // Domain of outbox chain uint32 public override remoteDomain; - // re-entrancy guard - uint8 private entered; // Mapping of message leaves to MessageStatus mapping(bytes32 => MessageStatus) public messages; // ============ Upgrade Gap ============ // gap for upgrade safety - uint256[47] private __GAP; + uint256[48] private __GAP; // ============ Events ============ @@ -74,8 +75,8 @@ contract Inbox is IInbox, Version0, Mailbox { public initializer { + __ReentrancyGuard_init(); __Mailbox_initialize(_validatorManager); - entered = 1; remoteDomain = _remoteDomain; } @@ -114,11 +115,7 @@ contract Inbox is IInbox, Version0, Mailbox { bytes32[32] calldata _proof, uint256 _index, bytes calldata /* _sovereignData */ - ) external override { - // check re-entrancy guard - require(entered == 1, "!reentrant"); - entered = 0; - + ) external override nonReentrant { bytes32 _messageHash = _message.leaf(_index); // ensure that message has not been processed require( @@ -135,8 +132,6 @@ contract Inbox is IInbox, Version0, Mailbox { require(cachedCheckpoints[_calculatedRoot] >= _index, "!cache"); _process(_message, _messageHash); emit Process(_messageHash, _index, _proof); - // reset re-entrancy guard - entered = 1; } // ============ Internal Functions ============