Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
N
nebula
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
exchain
nebula
Commits
715df5da
Commit
715df5da
authored
Jun 09, 2023
by
clabby
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add libraries
parent
c7949cb3
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
194 additions
and
0 deletions
+194
-0
LibClock.sol
...ages/contracts-bedrock/contracts/dispute/lib/LibClock.sol
+48
-0
LibHashing.sol
...es/contracts-bedrock/contracts/dispute/lib/LibHashing.sol
+25
-0
LibPosition.sol
...s/contracts-bedrock/contracts/dispute/lib/LibPosition.sol
+121
-0
No files found.
packages/contracts-bedrock/contracts/dispute/lib/LibClock.sol
0 → 100644
View file @
715df5da
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.15;
import "../../libraries/DisputeTypes.sol";
/**
* @title LibClock
* @author clabby <https://github.com/clabby>
* @notice This library contains helper functions for working with the `Clock` type.
*/
library LibClock {
/**
* @notice Packs a `Duration` and `Timestamp` into a `Clock` type.
* @param _duration The `Duration` to pack into the `Clock` type.
* @param _timestamp The `Timestamp` to pack into the `Clock` type.
* @return _clock The `Clock` containing the `_duration` and `_timestamp`.
*/
function wrap(Duration _duration, Timestamp _timestamp) internal pure returns (Clock _clock) {
assembly {
_clock := or(shl(0x80, _duration), _timestamp)
}
}
/**
* @notice Pull the `Duration` out of a `Clock` type.
* @param _clock The `Clock` type to pull the `Duration` out of.
* @return _duration The `Duration` pulled out of `_clock`.
*/
function duration(Clock _clock) internal pure returns (Duration _duration) {
// Shift the high-order 128 bits into the low-order 128 bits, leaving only the `duration`.
assembly {
_duration := shr(0x80, _clock)
}
}
/**
* @notice Pull the `Timestamp` out of a `Clock` type.
* @param _clock The `Clock` type to pull the `Timestamp` out of.
* @return _timestamp The `Timestamp` pulled out of `_clock`.
*/
function timestamp(Clock _clock) internal pure returns (Timestamp _timestamp) {
// Clean the high-order 128 bits by shifting the clock left and then right again, leaving
// only the `timestamp`.
assembly {
_timestamp := shr(0x80, shl(0x80, _clock))
}
}
}
packages/contracts-bedrock/contracts/dispute/lib/LibHashing.sol
0 → 100644
View file @
715df5da
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.15;
import "../../libraries/DisputeTypes.sol";
/**
* @title Hashing
* @author clabby <https://github.com/clabby>
* @notice This library contains all of the hashing utilities used in the Cannon contracts.
*/
library LibHashing {
/**
* @notice Hashes a claim and a position together.
* @param claim A Claim type.
* @param position The position of `claim`.
* @return claimHash A hash of abi.encodePacked(claim, position);
*/
function hashClaimPos(Claim claim, Position position) internal pure returns (ClaimHash claimHash) {
assembly {
mstore(0x00, claim)
mstore(0x20, position)
claimHash := keccak256(0x00, 0x40)
}
}
}
packages/contracts-bedrock/contracts/dispute/lib/LibPosition.sol
0 → 100644
View file @
715df5da
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.15;
import "../../libraries/DisputeTypes.sol";
/**
* @title LibPosition
* @author clabby <https://github.com/clabby>
* @notice This library contains helper functions for working with the `Position` type.
*/
library LibPosition {
function wrap(uint128 _depth, uint128 _indexAtDepth) internal pure returns (Position _position) {
assembly {
_position := or(shl(0x80, _depth), _indexAtDepth)
}
}
/**
* @notice Pulls the `depth` out of a packed `Position` type.
*/
function depth(Position position) internal pure returns (uint128 _depth) {
// Shift the high-order 128 bits into the low-order 128 bits, leaving only the `depth`.
assembly {
_depth := shr(0x80, position)
}
}
/**
* @notice Pulls the `indexAtDepth` out of a packed `Position` type.
*/
function indexAtDepth(Position position) internal pure returns (uint128 _indexAtDepth) {
// Clean the high-order 128 bits by shifting the position left and then right again, leaving
// only the `indexAtDepth`.
assembly {
_indexAtDepth := shr(0x80, shl(0x80, position))
}
}
/**
* @notice Get the position to the left of `position`.
* @param position The position to get the left position of.
* @return _left The position to the left of `position`.
*/
function left(Position position) internal pure returns (Position _left) {
uint128 _depth = depth(position);
uint128 _indexAtDepth = indexAtDepth(position);
// Left = { depth: position.depth + 1, indexAtDepth: position.indexAtDepth * 2 }
assembly {
_left := or(shl(0x80, add(_depth, 0x01)), shl(0x01, _indexAtDepth))
}
}
/**
* @notice Get the position to the right of `position`.
* @param position The position to get the right position of.
* @return _right The position to the right of `position`.
*/
function right(Position position) internal pure returns (Position _right) {
uint128 _depth = depth(position);
uint128 _indexAtDepth = indexAtDepth(position);
// Right = { depth: position.depth + 1, indexAtDepth: position.indexAtDepth * 2 + 1 }
assembly {
_right := or(shl(0x80, add(_depth, 0x01)), add(shl(0x01, _indexAtDepth), 0x01))
}
}
/**
* @notice Get the parent position of `position`.
* @param position The position to get the parent position of.
* @return _parent The parent position of `position`.
*/
function parent(Position position) internal pure returns (Position _parent) {
uint128 _depth = depth(position);
uint128 _indexAtDepth = indexAtDepth(position);
// Parent = { depth: position.depth - 1, indexAtDepth: position.indexAtDepth / 2 }
assembly {
_parent := or(shl(0x80, sub(_depth, 0x01)), shr(0x01, _indexAtDepth))
}
}
/**
* @notice Get the deepest, right most index relative to the `position`.
* @param position The position to get the relative deepest, right most index of.
* @param maxDepth The maximum depth of the game.
* @return _rightIndex The deepest, right most index relative to the `position`.
* TODO: Optimize; No need to update the full position in the sub loop.
*/
function rightIndex(Position position, uint256 maxDepth) internal pure returns (uint128 _rightIndex) {
assembly {
_rightIndex := shr(0x80, shl(0x80, position))
// Walk down to the max depth by moving right
for { let i := shr(0x80, position) } lt(i, sub(maxDepth, 0x01)) { i := add(i, 0x01) } {
_rightIndex := add(0x01, shl(0x01, _rightIndex))
}
}
}
/**
* @notice Get the attack position relative to `position`.
*/
function attack(Position position) internal pure returns (Position _attack) {
return left(position);
}
/**
* @notice Get the defend position of `position`.
*/
function defend(Position position) internal pure returns (Position _defend) {
uint128 _depth = depth(position);
uint128 _indexAtDepth = indexAtDepth(position);
// Defend = { depth: position.depth + 1, indexAtDepth: ((position.indexAtDepth / 2) * 2 + 1) * 2 }
assembly {
_defend := or(shl(0x80, add(_depth, 0x01)), shl(0x01, add(0x01, shl(0x01, shr(0x01, _indexAtDepth)))))
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment