From 427b8199d7573afe7f505d026a52fc60c2c185dc Mon Sep 17 00:00:00 2001 From: gzeon <95478735+gzeoneth@users.noreply.github.com> Date: Fri, 24 Jun 2022 23:29:58 +0800 Subject: [PATCH 1/2] feat: expose isSpent in Outbox --- contracts/src/bridge/Outbox.sol | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/contracts/src/bridge/Outbox.sol b/contracts/src/bridge/Outbox.sol index 9dee5fab0f..329db58339 100644 --- a/contracts/src/bridge/Outbox.sol +++ b/contracts/src/bridge/Outbox.sol @@ -207,6 +207,30 @@ contract Outbox is DelegateCallAware, IOutbox { context = prevContext; } + function _calcSpentIndexOffset(uint256 index) + internal + view + returns ( + uint256, + uint256, + bytes32 + ) + { + uint256 spentIndex = index / 255; // Note: Reserves the MSB. + uint256 bitOffset = index % 255; + bytes32 replay = spent[spentIndex]; + return (spentIndex, bitOffset, replay); + } + + function _isSpent(uint256 bitOffset, bytes32 replay) internal pure returns (bool) { + return ((replay >> bitOffset) & bytes32(uint256(1))) != bytes32(0); + } + + function isSpent(uint256 index) public view returns (bool) { + (, uint256 bitOffset, bytes32 replay) = _calcSpentIndexOffset(index); + return _isSpent(bitOffset, replay); + } + function recordOutputAsSpent( bytes32[] memory proof, uint256 index, @@ -219,11 +243,9 @@ contract Outbox is DelegateCallAware, IOutbox { bytes32 calcRoot = calculateMerkleRoot(proof, index, item); if (roots[calcRoot] == bytes32(0)) revert UnknownRoot(calcRoot); - uint256 spentIndex = index / 255; // Note: Reserves the MSB. - uint256 bitOffset = index % 255; + (uint256 spentIndex, uint256 bitOffset, bytes32 replay) = _calcSpentIndexOffset(index); - bytes32 replay = spent[spentIndex]; - if (((replay >> bitOffset) & bytes32(uint256(1))) != bytes32(0)) revert AlreadySpent(index); + if (_isSpent(bitOffset, replay)) revert AlreadySpent(index); spent[spentIndex] = (replay | bytes32(1 << bitOffset)); } From f0aeeb93233b592104aa9a98b8333c486ba82568 Mon Sep 17 00:00:00 2001 From: gzeon <95478735+gzeoneth@users.noreply.github.com> Date: Sat, 25 Jun 2022 00:04:58 +0800 Subject: [PATCH 2/2] fix: change isSpent to external --- contracts/src/bridge/Outbox.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/src/bridge/Outbox.sol b/contracts/src/bridge/Outbox.sol index 329db58339..8ff52afa3c 100644 --- a/contracts/src/bridge/Outbox.sol +++ b/contracts/src/bridge/Outbox.sol @@ -226,7 +226,7 @@ contract Outbox is DelegateCallAware, IOutbox { return ((replay >> bitOffset) & bytes32(uint256(1))) != bytes32(0); } - function isSpent(uint256 index) public view returns (bool) { + function isSpent(uint256 index) external view returns (bool) { (, uint256 bitOffset, bytes32 replay) = _calcSpentIndexOffset(index); return _isSpent(bitOffset, replay); }