forked from taikoxyz/taiko-mono
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(bridge): use deterministic message status slot (w/o mapping) (…
- Loading branch information
Showing
15 changed files
with
347 additions
and
210 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,69 +19,24 @@ import "../IBridge.sol"; | |
* @author dantaik <[email protected]> | ||
*/ | ||
library LibBridgeData { | ||
/********************* | ||
* Structs * | ||
*********************/ | ||
enum MessageStatus { | ||
NEW, | ||
RETRIABLE, | ||
DONE, | ||
FAILED | ||
} | ||
|
||
struct State { | ||
// chainId => isEnabled | ||
mapping(uint256 => bool) destChains; | ||
// message hash => status | ||
mapping(bytes32 => MessageStatus) messageStatus; | ||
uint256 nextMessageId; | ||
IBridge.Context ctx; // 3 slots | ||
uint256[44] __gap; | ||
uint256[45] __gap; | ||
} | ||
|
||
/********************* | ||
* Constants * | ||
*********************/ | ||
|
||
// TODO: figure out this value | ||
bytes32 internal constant SIGNAL_PLACEHOLDER = bytes32(uint256(1)); | ||
uint256 internal constant CHAINID_PLACEHOLDER = type(uint256).max; | ||
address internal constant SRC_CHAIN_SENDER_PLACEHOLDER = | ||
0x0000000000000000000000000000000000000001; | ||
|
||
/********************* | ||
* Events * | ||
*********************/ | ||
|
||
// Note: These events must match the ones defined in Bridge.sol. | ||
event MessageSent(bytes32 indexed signal, IBridge.Message message); | ||
|
||
event MessageStatusChanged(bytes32 indexed signal, MessageStatus status); | ||
|
||
event DestChainEnabled(uint256 indexed chainId, bool enabled); | ||
|
||
/********************* | ||
* Internal Functions* | ||
*********************/ | ||
|
||
/** | ||
* @dev If messageStatus is same as in the messageStatus mapping, | ||
* does nothing. | ||
* @param state The current bridge state. | ||
* @param signal The messageHash of the message. | ||
* @param status The status of the message. | ||
*/ | ||
function updateMessageStatus( | ||
State storage state, | ||
bytes32 signal, | ||
MessageStatus status | ||
) internal { | ||
if (state.messageStatus[signal] != status) { | ||
state.messageStatus[signal] = status; | ||
emit LibBridgeData.MessageStatusChanged(signal, status); | ||
} | ||
} | ||
|
||
/** | ||
* @dev Hashes messages and returns the hash signed with | ||
* "TAIKO_BRIDGE_MESSAGE" for verification. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
packages/protocol/contracts/bridge/libs/LibBridgeStatus.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// SPDX-License-Identifier: MIT | ||
// | ||
// ╭━━━━╮╱╱╭╮╱╱╱╱╱╭╮╱╱╱╱╱╭╮ | ||
// ┃╭╮╭╮┃╱╱┃┃╱╱╱╱╱┃┃╱╱╱╱╱┃┃ | ||
// ╰╯┃┃┣┻━┳┫┃╭┳━━╮┃┃╱╱╭━━┫╰━┳━━╮ | ||
// ╱╱┃┃┃╭╮┣┫╰╯┫╭╮┃┃┃╱╭┫╭╮┃╭╮┃━━┫ | ||
// ╱╱┃┃┃╭╮┃┃╭╮┫╰╯┃┃╰━╯┃╭╮┃╰╯┣━━┃ | ||
// ╱╱╰╯╰╯╰┻┻╯╰┻━━╯╰━━━┻╯╰┻━━┻━━╯ | ||
pragma solidity ^0.8.9; | ||
|
||
/** | ||
* @author dantaik <[email protected]> | ||
*/ | ||
library LibBridgeStatus { | ||
enum MessageStatus { | ||
NEW, | ||
RETRIABLE, | ||
DONE, | ||
FAILED | ||
} | ||
|
||
event MessageStatusChanged(bytes32 indexed signal, MessageStatus status); | ||
|
||
/** | ||
* @dev If messageStatus is same as in the messageStatus mapping, | ||
* does nothing. | ||
* @param signal The messageHash of the message. | ||
* @param status The status of the message. | ||
*/ | ||
function updateMessageStatus( | ||
bytes32 signal, | ||
MessageStatus status | ||
) internal { | ||
if (getMessageStatus(signal) != status) { | ||
_setMessageStatus(signal, status); | ||
emit LibBridgeStatus.MessageStatusChanged(signal, status); | ||
} | ||
} | ||
|
||
function getMessageStatus( | ||
bytes32 signal | ||
) internal view returns (MessageStatus) { | ||
bytes32 k = _statusSlot(signal); | ||
uint256 v; | ||
assembly { | ||
v := sload(k) | ||
} | ||
return MessageStatus(v); | ||
} | ||
|
||
function _setMessageStatus(bytes32 signal, MessageStatus status) private { | ||
bytes32 k = _statusSlot(signal); | ||
uint256 v = uint256(status); | ||
assembly { | ||
sstore(k, v) | ||
} | ||
} | ||
|
||
function _statusSlot(bytes32 signal) private pure returns (bytes32) { | ||
return keccak256(abi.encodePacked("MESSAGE_STATUS", signal)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.