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 <yorke@useabacus.network>
pull/526/head
Trevor Porter 3 years ago committed by GitHub
parent 969c51bdcc
commit 099cd93cec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 19
      solidity/core/contracts/Inbox.sol

@ -10,13 +10,16 @@ import {TypeCasts} from "../libs/TypeCasts.sol";
import {IMessageRecipient} from "../interfaces/IMessageRecipient.sol"; import {IMessageRecipient} from "../interfaces/IMessageRecipient.sol";
import {IInbox} from "../interfaces/IInbox.sol"; import {IInbox} from "../interfaces/IInbox.sol";
// ============ External Imports ============
import {ReentrancyGuardUpgradeable} from "@openzeppelin/contracts-upgradeable/security/ReentrancyGuardUpgradeable.sol";
/** /**
* @title Inbox * @title Inbox
* @author Celo Labs Inc. * @author Celo Labs Inc.
* @notice Track root updates on Outbox, prove and dispatch messages to end * @notice Track root updates on Outbox, prove and dispatch messages to end
* recipients. * recipients.
*/ */
contract Inbox is IInbox, Version0, Mailbox { contract Inbox is IInbox, ReentrancyGuardUpgradeable, Version0, Mailbox {
// ============ Libraries ============ // ============ Libraries ============
using MerkleLib for MerkleLib.Tree; using MerkleLib for MerkleLib.Tree;
@ -37,15 +40,13 @@ contract Inbox is IInbox, Version0, Mailbox {
// Domain of outbox chain // Domain of outbox chain
uint32 public override remoteDomain; uint32 public override remoteDomain;
// re-entrancy guard
uint8 private entered;
// Mapping of message leaves to MessageStatus // Mapping of message leaves to MessageStatus
mapping(bytes32 => MessageStatus) public messages; mapping(bytes32 => MessageStatus) public messages;
// ============ Upgrade Gap ============ // ============ Upgrade Gap ============
// gap for upgrade safety // gap for upgrade safety
uint256[47] private __GAP; uint256[48] private __GAP;
// ============ Events ============ // ============ Events ============
@ -74,8 +75,8 @@ contract Inbox is IInbox, Version0, Mailbox {
public public
initializer initializer
{ {
__ReentrancyGuard_init();
__Mailbox_initialize(_validatorManager); __Mailbox_initialize(_validatorManager);
entered = 1;
remoteDomain = _remoteDomain; remoteDomain = _remoteDomain;
} }
@ -114,11 +115,7 @@ contract Inbox is IInbox, Version0, Mailbox {
bytes32[32] calldata _proof, bytes32[32] calldata _proof,
uint256 _index, uint256 _index,
bytes calldata /* _sovereignData */ bytes calldata /* _sovereignData */
) external override { ) external override nonReentrant {
// check re-entrancy guard
require(entered == 1, "!reentrant");
entered = 0;
bytes32 _messageHash = _message.leaf(_index); bytes32 _messageHash = _message.leaf(_index);
// ensure that message has not been processed // ensure that message has not been processed
require( require(
@ -135,8 +132,6 @@ contract Inbox is IInbox, Version0, Mailbox {
require(cachedCheckpoints[_calculatedRoot] >= _index, "!cache"); require(cachedCheckpoints[_calculatedRoot] >= _index, "!cache");
_process(_message, _messageHash); _process(_message, _messageHash);
emit Process(_messageHash, _index, _proof); emit Process(_messageHash, _index, _proof);
// reset re-entrancy guard
entered = 1;
} }
// ============ Internal Functions ============ // ============ Internal Functions ============

Loading…
Cancel
Save