Skip to content

Commit

Permalink
fix(protocol): tstore is not suppported on L2 now (#15802)
Browse files Browse the repository at this point in the history
Co-authored-by: Brechtpd <Brechtp.devos@gmail.com>
  • Loading branch information
dantaik and Brechtpd committed Feb 15, 2024
1 parent a652ae8 commit f44698e
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 25 deletions.
42 changes: 28 additions & 14 deletions packages/protocol/contracts/bridge/Bridge.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
}
}
Expand Down Expand Up @@ -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.
Expand Down
30 changes: 19 additions & 11 deletions packages/protocol/contracts/common/OwnerUUPSUpgradable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
}
5 changes: 5 additions & 0 deletions packages/protocol/utils/generate_genesis/taikoL2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ async function generateContractConfigs(
_initialized: 1,
_initializing: false,
// ReentrancyGuardUpgradeable
_reentry: 1, // _FALSE
_paused: 1, // _FALSE
// Ownable2Upgradeable
_owner: ownerSecurityCouncil,
Expand Down Expand Up @@ -330,6 +331,7 @@ async function generateContractConfigs(
_initialized: 1,
_initializing: false,
// ReentrancyGuardUpgradeable
_reentry: 1, // _FALSE
_paused: 1, // _FALSE
// Ownable2Upgradeable
_owner: ownerSecurityCouncil,
Expand Down Expand Up @@ -364,6 +366,7 @@ async function generateContractConfigs(
_initialized: 1,
_initializing: false,
// ReentrancyGuardUpgradeable
_reentry: 1, // _FALSE
_paused: 1, // _FALSE
// Ownable2Upgradeable
_owner: ownerSecurityCouncil,
Expand Down Expand Up @@ -398,6 +401,7 @@ async function generateContractConfigs(
_initialized: 1,
_initializing: false,
// ReentrancyGuardUpgradeable
_reentry: 1, // _FALSE
_paused: 1, // _FALSE
// Ownable2Upgradeable
_owner: ownerSecurityCouncil,
Expand Down Expand Up @@ -456,6 +460,7 @@ async function generateContractConfigs(
_initialized: 1,
_initializing: false,
// ReentrancyGuardUpgradeable
_reentry: 1, // _FALSE
_paused: 1, // _FALSE
// Ownable2Upgradeable
_owner: ownerSecurityCouncil,
Expand Down

0 comments on commit f44698e

Please sign in to comment.