diff --git a/packages/protocol/contracts/bridge/Bridge.sol b/packages/protocol/contracts/bridge/Bridge.sol index 575a1d7573..684859555c 100644 --- a/packages/protocol/contracts/bridge/Bridge.sol +++ b/packages/protocol/contracts/bridge/Bridge.sol @@ -50,6 +50,8 @@ contract Bridge is EssentialContract, IBridge { // This is the keccak256 hash of "bridge.ctx_slot" bytes32 private constant _CTX_SLOT = 0xe4ece82196de19aabe639620d7f716c433d1348f96ce727c9989a982dbadc2b9; + // Place holder value when not using transient storage + uint256 internal constant PLACEHOLDER = type(uint256).max; uint128 public nextMessageId; // slot 1 mapping(bytes32 msgHash => bool recalled) private __isMessageRecalled; // slot 2, deprecated @@ -443,7 +445,7 @@ contract Bridge is EssentialContract, IBridge { /// @inheritdoc IBridge function context() public view returns (Context memory ctx) { ctx = _loadContext(); - if (ctx.msgHash == 0) { + if (ctx.msgHash == 0 || ctx.msgHash == bytes32(PLACEHOLDER)) { revert B_INVALID_CONTEXT(); } } @@ -543,29 +545,41 @@ contract Bridge is EssentialContract, IBridge { /// @notice Resets the call context function _resetContext() private { - _storeContext(bytes32(0), address(0), uint64(0)); + if (block.chainid == 1) { + _storeContext(bytes32(0), address(0), uint64(0)); + } else { + _storeContext(bytes32(PLACEHOLDER), address(uint160(PLACEHOLDER)), uint64(PLACEHOLDER)); + } } /// @notice Stores the call context function _storeContext(bytes32 msgHash, address from, uint64 srcChainId) private { - assembly { - tstore(_CTX_SLOT, msgHash) - tstore(add(_CTX_SLOT, 1), from) - tstore(add(_CTX_SLOT, 2), srcChainId) + if (block.chainid == 1) { + assembly { + tstore(_CTX_SLOT, msgHash) + tstore(add(_CTX_SLOT, 1), from) + tstore(add(_CTX_SLOT, 2), srcChainId) + } + } else { + _ctx = Context({ msgHash: msgHash, from: from, srcChainId: srcChainId }); } } /// @notice Loads the call context function _loadContext() private view returns (Context memory) { - bytes32 msgHash; - address from; - uint64 srcChainId; - assembly { - msgHash := tload(_CTX_SLOT) - from := tload(add(_CTX_SLOT, 1)) - srcChainId := tload(add(_CTX_SLOT, 2)) + if (block.chainid == 1) { + bytes32 msgHash; + address from; + uint64 srcChainId; + assembly { + msgHash := tload(_CTX_SLOT) + from := tload(add(_CTX_SLOT, 1)) + srcChainId := tload(add(_CTX_SLOT, 2)) + } + return Context({ msgHash: msgHash, from: from, srcChainId: srcChainId }); + } else { + return _ctx; } - return Context({ msgHash: msgHash, from: from, srcChainId: srcChainId }); } /// @notice Checks if the signal was received. diff --git a/packages/protocol/contracts/common/OwnerUUPSUpgradable.sol b/packages/protocol/contracts/common/OwnerUUPSUpgradable.sol index e976213840..37f3c1c47a 100644 --- a/packages/protocol/contracts/common/OwnerUUPSUpgradable.sol +++ b/packages/protocol/contracts/common/OwnerUUPSUpgradable.sol @@ -30,7 +30,7 @@ abstract contract OwnerUUPSUpgradable is UUPSUpgradeable, OwnableUpgradeable { bytes32 private constant _REENTRY_SLOT = 0xa5054f728453d3dbe953bdc43e4d0cb97e662ea32d7958190f3dc2da31d9721a; - uint8 private _reentryDeprecated; // slot 1 + uint8 private _reentry; // slot 1 uint8 private _paused; uint256[49] private __gap; @@ -88,21 +88,29 @@ abstract contract OwnerUUPSUpgradable is UUPSUpgradeable, OwnableUpgradeable { _paused = _FALSE; } - function _inNonReentrant() internal view returns (bool) { - return _loadReentryLock() == _TRUE; - } - // Stores the reentry lock - function _storeReentryLock(uint8 reentry) private { - assembly { - tstore(_REENTRY_SLOT, reentry) + function _storeReentryLock(uint8 reentry) internal virtual { + if (block.chainid == 1) { + assembly { + tstore(_REENTRY_SLOT, reentry) + } + } else { + _reentry = reentry; } } // Loads the reentry lock - function _loadReentryLock() private view returns (uint8 reentry) { - assembly { - reentry := tload(_REENTRY_SLOT) + function _loadReentryLock() internal view virtual returns (uint8 reentry) { + if (block.chainid == 1) { + assembly { + reentry := tload(_REENTRY_SLOT) + } + } else { + reentry = _reentry; } } + + function _inNonReentrant() internal view returns (bool) { + return _loadReentryLock() == _TRUE; + } } diff --git a/packages/protocol/utils/generate_genesis/taikoL2.ts b/packages/protocol/utils/generate_genesis/taikoL2.ts index c351ffa957..b9553bfe69 100644 --- a/packages/protocol/utils/generate_genesis/taikoL2.ts +++ b/packages/protocol/utils/generate_genesis/taikoL2.ts @@ -296,6 +296,7 @@ async function generateContractConfigs( _initialized: 1, _initializing: false, // ReentrancyGuardUpgradeable + _reentry: 1, // _FALSE _paused: 1, // _FALSE // Ownable2Upgradeable _owner: ownerSecurityCouncil, @@ -330,6 +331,7 @@ async function generateContractConfigs( _initialized: 1, _initializing: false, // ReentrancyGuardUpgradeable + _reentry: 1, // _FALSE _paused: 1, // _FALSE // Ownable2Upgradeable _owner: ownerSecurityCouncil, @@ -364,6 +366,7 @@ async function generateContractConfigs( _initialized: 1, _initializing: false, // ReentrancyGuardUpgradeable + _reentry: 1, // _FALSE _paused: 1, // _FALSE // Ownable2Upgradeable _owner: ownerSecurityCouncil, @@ -398,6 +401,7 @@ async function generateContractConfigs( _initialized: 1, _initializing: false, // ReentrancyGuardUpgradeable + _reentry: 1, // _FALSE _paused: 1, // _FALSE // Ownable2Upgradeable _owner: ownerSecurityCouncil, @@ -456,6 +460,7 @@ async function generateContractConfigs( _initialized: 1, _initializing: false, // ReentrancyGuardUpgradeable + _reentry: 1, // _FALSE _paused: 1, // _FALSE // Ownable2Upgradeable _owner: ownerSecurityCouncil,