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
84e8b55b
Commit
84e8b55b
authored
Jun 15, 2023
by
clabby
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
`_in` & `out_` naming convention
fix
parent
d8cd3b8a
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
140 additions
and
128 deletions
+140
-128
FaultDisputeGame.sol
.../contracts-bedrock/contracts/dispute/FaultDisputeGame.sol
+42
-40
IDisputeGame.sol
...cts-bedrock/contracts/dispute/interfaces/IDisputeGame.sol
+20
-17
IFaultDisputeGame.sol
...edrock/contracts/dispute/interfaces/IFaultDisputeGame.sol
+16
-16
LibClock.sol
...ages/contracts-bedrock/contracts/dispute/lib/LibClock.sol
+9
-9
LibHashing.sol
...es/contracts-bedrock/contracts/dispute/lib/LibHashing.sol
+7
-7
LibPosition.sol
...s/contracts-bedrock/contracts/dispute/lib/LibPosition.sol
+46
-39
No files found.
packages/contracts-bedrock/contracts/dispute/FaultDisputeGame.sol
View file @
84e8b55b
...
@@ -77,25 +77,25 @@ contract FaultDisputeGame is IFaultDisputeGame, Clone {
...
@@ -77,25 +77,25 @@ contract FaultDisputeGame is IFaultDisputeGame, Clone {
/**
/**
* @inheritdoc IFaultDisputeGame
* @inheritdoc IFaultDisputeGame
*/
*/
function attack(uint256
parentIndex, Claim
pivot) external payable {
function attack(uint256
_parentIndex, Claim _
pivot) external payable {
_move(
parentIndex,
pivot, true);
_move(
_parentIndex, _
pivot, true);
}
}
/**
/**
* @inheritdoc IFaultDisputeGame
* @inheritdoc IFaultDisputeGame
*/
*/
function defend(uint256
parentIndex, Claim
pivot) external payable {
function defend(uint256
_parentIndex, Claim _
pivot) external payable {
_move(
parentIndex,
pivot, false);
_move(
_parentIndex, _
pivot, false);
}
}
/**
/**
* @inheritdoc IFaultDisputeGame
* @inheritdoc IFaultDisputeGame
*/
*/
function step(
function step(
uint256 prestateIndex,
uint256
_
prestateIndex,
uint256 parentIndex,
uint256
_
parentIndex,
bytes calldata stateData,
bytes calldata
_
stateData,
bytes calldata
bytes calldata
_proof
) external {
) external {
// TODO - Call the VM to perform the execution step.
// TODO - Call the VM to perform the execution step.
}
}
...
@@ -106,14 +106,14 @@ contract FaultDisputeGame is IFaultDisputeGame, Clone {
...
@@ -106,14 +106,14 @@ contract FaultDisputeGame is IFaultDisputeGame, Clone {
/**
/**
* @notice Internal move function, used by both `attack` and `defend`.
* @notice Internal move function, used by both `attack` and `defend`.
* @param challengeIndex The index of the claim being moved against.
* @param
_
challengeIndex The index of the claim being moved against.
* @param pivot The claim at the next logical position in the game.
* @param
_
pivot The claim at the next logical position in the game.
* @param isAttack Whether or not the move is an attack or defense.
* @param
_
isAttack Whether or not the move is an attack or defense.
*/
*/
function _move(
function _move(
uint256 challengeIndex,
uint256
_
challengeIndex,
Claim pivot,
Claim
_
pivot,
bool isAttack
bool
_
isAttack
) internal {
) internal {
// Moves cannot be made unless the game is currently in progress.
// Moves cannot be made unless the game is currently in progress.
if (status != GameStatus.IN_PROGRESS) {
if (status != GameStatus.IN_PROGRESS) {
...
@@ -121,19 +121,19 @@ contract FaultDisputeGame is IFaultDisputeGame, Clone {
...
@@ -121,19 +121,19 @@ contract FaultDisputeGame is IFaultDisputeGame, Clone {
}
}
// The zero hash is not a valid claim.
// The zero hash is not a valid claim.
if (Claim.unwrap(pivot) == bytes32(0)) {
if (Claim.unwrap(
_
pivot) == bytes32(0)) {
revert InvalidClaim();
revert InvalidClaim();
}
}
// The only move that can be made against a root claim is an attack. This is because the
// The only move that can be made against a root claim is an attack. This is because the
// root claim commits to the entire state; Therefore, the only valid defense is to do
// root claim commits to the entire state; Therefore, the only valid defense is to do
// nothing if it is agreed with.
// nothing if it is agreed with.
if (
challengeIndex == 0 && !
isAttack) {
if (
_challengeIndex == 0 && !_
isAttack) {
revert CannotDefendRootClaim();
revert CannotDefendRootClaim();
}
}
// Get the parent
// Get the parent
ClaimData memory parent = claimData[challengeIndex];
ClaimData memory parent = claimData[
_
challengeIndex];
// The parent must exist.
// The parent must exist.
if (Claim.unwrap(parent.claim) == bytes32(0)) {
if (Claim.unwrap(parent.claim) == bytes32(0)) {
...
@@ -141,12 +141,12 @@ contract FaultDisputeGame is IFaultDisputeGame, Clone {
...
@@ -141,12 +141,12 @@ contract FaultDisputeGame is IFaultDisputeGame, Clone {
}
}
// Set the parent claim as countered.
// Set the parent claim as countered.
claimData[challengeIndex].countered = true;
claimData[
_
challengeIndex].countered = true;
// Compute the position that the claim commits to. Because the parent's position is already
// Compute the position that the claim commits to. Because the parent's position is already
// known, we can compute the next position by moving left or right depending on whether
// known, we can compute the next position by moving left or right depending on whether
// or not the move is an attack or defense.
// or not the move is an attack or defense.
Position nextPosition = isAttack
Position nextPosition =
_
isAttack
? LibPosition.attack(parent.position)
? LibPosition.attack(parent.position)
: LibPosition.defend(parent.position);
: LibPosition.defend(parent.position);
...
@@ -188,7 +188,7 @@ contract FaultDisputeGame is IFaultDisputeGame, Clone {
...
@@ -188,7 +188,7 @@ contract FaultDisputeGame is IFaultDisputeGame, Clone {
Clock nextClock = LibClock.wrap(nextDuration, Timestamp.wrap(uint64(block.timestamp)));
Clock nextClock = LibClock.wrap(nextDuration, Timestamp.wrap(uint64(block.timestamp)));
// Do not allow for a duplicate claim to be made.
// Do not allow for a duplicate claim to be made.
ClaimHash claimHash = LibHashing.hashClaimPos(pivot, nextPosition);
ClaimHash claimHash = LibHashing.hashClaimPos(
_
pivot, nextPosition);
if (claims[claimHash]) {
if (claims[claimHash]) {
revert ClaimAlreadyExists();
revert ClaimAlreadyExists();
}
}
...
@@ -197,8 +197,8 @@ contract FaultDisputeGame is IFaultDisputeGame, Clone {
...
@@ -197,8 +197,8 @@ contract FaultDisputeGame is IFaultDisputeGame, Clone {
// Create the new claim.
// Create the new claim.
claimData.push(
claimData.push(
ClaimData({
ClaimData({
parentIndex: uint32(challengeIndex),
parentIndex: uint32(
_
challengeIndex),
claim: pivot,
claim:
_
pivot,
position: nextPosition,
position: nextPosition,
clock: nextClock,
clock: nextClock,
countered: false
countered: false
...
@@ -206,7 +206,7 @@ contract FaultDisputeGame is IFaultDisputeGame, Clone {
...
@@ -206,7 +206,7 @@ contract FaultDisputeGame is IFaultDisputeGame, Clone {
);
);
// Emit the appropriate event for the attack or defense.
// Emit the appropriate event for the attack or defense.
emit Move(
challengeIndex,
pivot, msg.sender);
emit Move(
_challengeIndex, _
pivot, msg.sender);
}
}
////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
...
@@ -216,39 +216,41 @@ contract FaultDisputeGame is IFaultDisputeGame, Clone {
...
@@ -216,39 +216,41 @@ contract FaultDisputeGame is IFaultDisputeGame, Clone {
/**
/**
* @inheritdoc IDisputeGame
* @inheritdoc IDisputeGame
*/
*/
function gameType() public pure override returns (GameType
_gameType
) {
function gameType() public pure override returns (GameType
gameType_
) {
_gameType
= GameTypes.FAULT;
gameType_
= GameTypes.FAULT;
}
}
/**
/**
* @inheritdoc IDisputeGame
* @inheritdoc IDisputeGame
*/
*/
function createdAt() external view returns (Timestamp
_createdAt
) {
function createdAt() external view returns (Timestamp
createdAt_
) {
_createdAt
= gameStart;
createdAt_
= gameStart;
}
}
/**
/**
* @inheritdoc IDisputeGame
* @inheritdoc IDisputeGame
*/
*/
function resolve() external returns (GameStatus
_status
) {
function resolve() external returns (GameStatus
status_
) {
// TODO - Resolve the game
// TODO - Resolve the game
status = GameStatus.IN_PROGRESS;
status_ = status;
}
}
/**
/**
* @inheritdoc IDisputeGame
* @inheritdoc IDisputeGame
*/
*/
function rootClaim() public pure returns (Claim
_rootClaim
) {
function rootClaim() public pure returns (Claim
rootClaim_
) {
_rootClaim
= Claim.wrap(_getArgFixedBytes(0x00));
rootClaim_
= Claim.wrap(_getArgFixedBytes(0x00));
}
}
/**
/**
* @inheritdoc IDisputeGame
* @inheritdoc IDisputeGame
*/
*/
function extraData() public pure returns (bytes memory
_extraData
) {
function extraData() public pure returns (bytes memory
extraData_
) {
// The extra data starts at the second word within the cwia calldata.
// The extra data starts at the second word within the cwia calldata.
// TODO: What data do we need to pass along to this contract from the factory?
// TODO: What data do we need to pass along to this contract from the factory?
// Block hash, preimage data, etc.?
// Block hash, preimage data, etc.?
_extraData
= _getArgDynBytes(0x20, 0x20);
extraData_
= _getArgDynBytes(0x20, 0x20);
}
}
/**
/**
...
@@ -258,14 +260,14 @@ contract FaultDisputeGame is IFaultDisputeGame, Clone {
...
@@ -258,14 +260,14 @@ contract FaultDisputeGame is IFaultDisputeGame, Clone {
external
external
pure
pure
returns (
returns (
GameType
_gameType
,
GameType
gameType_
,
Claim
_rootClaim
,
Claim
rootClaim_
,
bytes memory
_extraData
bytes memory
extraData_
)
)
{
{
_gameType
= gameType();
gameType_
= gameType();
_rootClaim
= rootClaim();
rootClaim_
= rootClaim();
_extraData
= extraData();
extraData_
= extraData();
}
}
/**
/**
...
@@ -292,7 +294,7 @@ contract FaultDisputeGame is IFaultDisputeGame, Clone {
...
@@ -292,7 +294,7 @@ contract FaultDisputeGame is IFaultDisputeGame, Clone {
/**
/**
* @inheritdoc IVersioned
* @inheritdoc IVersioned
*/
*/
function version() external pure override returns (string memory
_version
) {
function version() external pure override returns (string memory
version_
) {
_version
= VERSION;
version_
= VERSION;
}
}
}
}
packages/contracts-bedrock/contracts/dispute/interfaces/IDisputeGame.sol
View file @
84e8b55b
...
@@ -20,44 +20,44 @@ interface IDisputeGame is IInitializable, IVersioned {
...
@@ -20,44 +20,44 @@ interface IDisputeGame is IInitializable, IVersioned {
/**
/**
* @notice Returns the timestamp that the DisputeGame contract was created at.
* @notice Returns the timestamp that the DisputeGame contract was created at.
* @return
_createdAt
The timestamp that the DisputeGame contract was created at.
* @return
createdAt_
The timestamp that the DisputeGame contract was created at.
*/
*/
function createdAt() external view returns (Timestamp
_createdAt
);
function createdAt() external view returns (Timestamp
createdAt_
);
/**
/**
* @notice Returns the current status of the game.
* @notice Returns the current status of the game.
* @return
_status
The current status of the game.
* @return
status_
The current status of the game.
*/
*/
function status() external view returns (GameStatus
_status
);
function status() external view returns (GameStatus
status_
);
/**
/**
* @notice Getter for the game type.
* @notice Getter for the game type.
* @dev `clones-with-immutable-args` argument #1
* @dev `clones-with-immutable-args` argument #1
* @dev The reference impl should be entirely different depending on the type (fault, validity)
* @dev The reference impl should be entirely different depending on the type (fault, validity)
* i.e. The game type should indicate the security model.
* i.e. The game type should indicate the security model.
* @return
_gameType
The type of proof system being used.
* @return
gameType_
The type of proof system being used.
*/
*/
function gameType() external pure returns (GameType
_gameType
);
function gameType() external pure returns (GameType
gameType_
);
/**
/**
* @notice Getter for the root claim.
* @notice Getter for the root claim.
* @dev `clones-with-immutable-args` argument #2
* @dev `clones-with-immutable-args` argument #2
* @return
_rootClaim
The root claim of the DisputeGame.
* @return
rootClaim_
The root claim of the DisputeGame.
*/
*/
function rootClaim() external pure returns (Claim
_rootClaim
);
function rootClaim() external pure returns (Claim
rootClaim_
);
/**
/**
* @notice Getter for the extra data.
* @notice Getter for the extra data.
* @dev `clones-with-immutable-args` argument #3
* @dev `clones-with-immutable-args` argument #3
* @return
_extraData
Any extra data supplied to the dispute game contract by the creator.
* @return
extraData_
Any extra data supplied to the dispute game contract by the creator.
*/
*/
function extraData() external pure returns (bytes memory
_extraData
);
function extraData() external pure returns (bytes memory
extraData_
);
/**
/**
* @notice Returns the address of the `BondManager` used.
* @notice Returns the address of the `BondManager` used.
* @return
_bondManager
The address of the `BondManager` used.
* @return
bondManager_
The address of the `BondManager` used.
*/
*/
function bondManager() external view returns (IBondManager
_bondManager
);
function bondManager() external view returns (IBondManager
bondManager_
);
/**
/**
* @notice If all necessary information has been gathered, this function should mark the game
* @notice If all necessary information has been gathered, this function should mark the game
...
@@ -65,22 +65,25 @@ interface IDisputeGame is IInitializable, IVersioned {
...
@@ -65,22 +65,25 @@ interface IDisputeGame is IInitializable, IVersioned {
* the resolved game. It is at this stage that the bonds should be awarded to the
* the resolved game. It is at this stage that the bonds should be awarded to the
* necessary parties.
* necessary parties.
* @dev May only be called if the `status` is `IN_PROGRESS`.
* @dev May only be called if the `status` is `IN_PROGRESS`.
* @return
_status
The status of the game after resolution.
* @return
status_
The status of the game after resolution.
*/
*/
function resolve() external returns (GameStatus
_status
);
function resolve() external returns (GameStatus
status_
);
/**
/**
* @notice A compliant implementation of this interface should return the components of the
* @notice A compliant implementation of this interface should return the components of the
* game UUID's preimage provided in the cwia payload. The preimage of the UUID is
* game UUID's preimage provided in the cwia payload. The preimage of the UUID is
* constructed as `keccak256(gameType . rootClaim . extraData)` where `.` denotes
* constructed as `keccak256(gameType . rootClaim . extraData)` where `.` denotes
* concatenation.
* concatenation.
* @return gameType_ The type of proof system being used.
* @return rootClaim_ The root claim of the DisputeGame.
* @return extraData_ Any extra data supplied to the dispute game contract by the creator.
*/
*/
function gameData()
function gameData()
external
external
pure
pure
returns (
returns (
GameType
_gameType
,
GameType
gameType_
,
Claim
_rootClaim
,
Claim
rootClaim_
,
bytes memory
_extraData
bytes memory
extraData_
);
);
}
}
packages/contracts-bedrock/contracts/dispute/interfaces/IFaultDisputeGame.sol
View file @
84e8b55b
...
@@ -32,18 +32,18 @@ interface IFaultDisputeGame is IDisputeGame {
...
@@ -32,18 +32,18 @@ interface IFaultDisputeGame is IDisputeGame {
event Move(uint256 indexed parentIndex, Claim indexed pivot, address indexed claimant);
event Move(uint256 indexed parentIndex, Claim indexed pivot, address indexed claimant);
/**
/**
* Attack a disagreed upon `Claim`.
*
@notice
Attack a disagreed upon `Claim`.
* @param parentIndex Index of the `Claim` to attack in `claimData`.
* @param
_
parentIndex Index of the `Claim` to attack in `claimData`.
* @param pivot The `Claim` at the relative attack position.
* @param
_
pivot The `Claim` at the relative attack position.
*/
*/
function attack(uint256
parentIndex, Claim
pivot) external payable;
function attack(uint256
_parentIndex, Claim _
pivot) external payable;
/**
/**
* Defend an agreed upon `Claim`.
*
@notice
Defend an agreed upon `Claim`.
* @param parentIndex Index of the claim to defend in `claimData`.
* @param
_
parentIndex Index of the claim to defend in `claimData`.
* @param pivot The `Claim` at the relative defense position.
* @param
_
pivot The `Claim` at the relative defense position.
*/
*/
function defend(uint256
parentIndex, Claim
pivot) external payable;
function defend(uint256
_parentIndex, Claim _
pivot) external payable;
/**
/**
* @notice Perform the final step via an on-chain fault proof processor
* @notice Perform the final step via an on-chain fault proof processor
...
@@ -51,15 +51,15 @@ interface IFaultDisputeGame is IDisputeGame {
...
@@ -51,15 +51,15 @@ interface IFaultDisputeGame is IDisputeGame {
* a step in the fault proof program on-chain. The interface of the fault proof
* a step in the fault proof program on-chain. The interface of the fault proof
* processor contract should be generic enough such that we can use different
* processor contract should be generic enough such that we can use different
* fault proof VMs (MIPS, RiscV5, etc.)
* fault proof VMs (MIPS, RiscV5, etc.)
* @param prestateIndex The index of the prestate of the step within `claimData`.
* @param
_
prestateIndex The index of the prestate of the step within `claimData`.
* @param parentIndex The index of the parent claim within `claimData`.
* @param
_
parentIndex The index of the parent claim within `claimData`.
* @param stateData The stateData of the step is the preimage of the claim @ `prestateIndex`
* @param
_
stateData The stateData of the step is the preimage of the claim @ `prestateIndex`
* @param proof Proof to access memory leaf nodes in the VM.
* @param
_
proof Proof to access memory leaf nodes in the VM.
*/
*/
function step(
function step(
uint256 prestateIndex,
uint256
_
prestateIndex,
uint256 parentIndex,
uint256
_
parentIndex,
bytes calldata stateData,
bytes calldata
_
stateData,
bytes calldata proof
bytes calldata
_
proof
) external;
) external;
}
}
packages/contracts-bedrock/contracts/dispute/lib/LibClock.sol
View file @
84e8b55b
...
@@ -12,36 +12,36 @@ library LibClock {
...
@@ -12,36 +12,36 @@ library LibClock {
* @notice Packs a `Duration` and `Timestamp` into a `Clock` type.
* @notice Packs a `Duration` and `Timestamp` into a `Clock` type.
* @param _duration The `Duration` to pack into the `Clock` type.
* @param _duration The `Duration` to pack into the `Clock` type.
* @param _timestamp The `Timestamp` 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`.
* @return
clock_
The `Clock` containing the `_duration` and `_timestamp`.
*/
*/
function wrap(Duration _duration, Timestamp _timestamp) internal pure returns (Clock
_clock
) {
function wrap(Duration _duration, Timestamp _timestamp) internal pure returns (Clock
clock_
) {
assembly {
assembly {
_clock
:= or(shl(0x40, _duration), _timestamp)
clock_
:= or(shl(0x40, _duration), _timestamp)
}
}
}
}
/**
/**
* @notice Pull the `Duration` out of a `Clock` type.
* @notice Pull the `Duration` out of a `Clock` type.
* @param _clock The `Clock` type to pull the `Duration` out of.
* @param _clock The `Clock` type to pull the `Duration` out of.
* @return
_duration
The `Duration` pulled out of `_clock`.
* @return
duration_
The `Duration` pulled out of `_clock`.
*/
*/
function duration(Clock _clock) internal pure returns (Duration
_duration
) {
function duration(Clock _clock) internal pure returns (Duration
duration_
) {
// Shift the high-order 64 bits into the low-order 64 bits, leaving only the `duration`.
// Shift the high-order 64 bits into the low-order 64 bits, leaving only the `duration`.
assembly {
assembly {
_duration
:= shr(0x40, _clock)
duration_
:= shr(0x40, _clock)
}
}
}
}
/**
/**
* @notice Pull the `Timestamp` out of a `Clock` type.
* @notice Pull the `Timestamp` out of a `Clock` type.
* @param _clock The `Clock` type to pull the `Timestamp` out of.
* @param _clock The `Clock` type to pull the `Timestamp` out of.
* @return
_timestamp
The `Timestamp` pulled out of `_clock`.
* @return
timestamp_
The `Timestamp` pulled out of `_clock`.
*/
*/
function timestamp(Clock _clock) internal pure returns (Timestamp
_timestamp
) {
function timestamp(Clock _clock) internal pure returns (Timestamp
timestamp_
) {
// Clean the high-order 192 bits by shifting the clock left and then right again, leaving
// Clean the high-order 192 bits by shifting the clock left and then right again, leaving
// only the `timestamp`.
// only the `timestamp`.
assembly {
assembly {
_timestamp
:= shr(0xC0, shl(0xC0, _clock))
timestamp_
:= shr(0xC0, shl(0xC0, _clock))
}
}
}
}
}
}
packages/contracts-bedrock/contracts/dispute/lib/LibHashing.sol
View file @
84e8b55b
...
@@ -10,15 +10,15 @@ import "../../libraries/DisputeTypes.sol";
...
@@ -10,15 +10,15 @@ import "../../libraries/DisputeTypes.sol";
library LibHashing {
library LibHashing {
/**
/**
* @notice Hashes a claim and a position together.
* @notice Hashes a claim and a position together.
* @param claim A Claim type.
* @param
_
claim A Claim type.
* @param position The position of `claim`.
* @param
_
position The position of `claim`.
* @return claimHash A hash of abi.encodePacked(claim, position);
* @return claimHash
_
A hash of abi.encodePacked(claim, position);
*/
*/
function hashClaimPos(Claim
claim, Position position) internal pure returns (ClaimHash claimHash
) {
function hashClaimPos(Claim
_claim, Position _position) internal pure returns (ClaimHash claimHash_
) {
assembly {
assembly {
mstore(0x00, claim)
mstore(0x00,
_
claim)
mstore(0x20, position)
mstore(0x20,
_
position)
claimHash := keccak256(0x00, 0x40)
claimHash
_
:= keccak256(0x00, 0x40)
}
}
}
}
}
}
packages/contracts-bedrock/contracts/dispute/lib/LibPosition.sol
View file @
84e8b55b
...
@@ -8,113 +8,120 @@ import "../../libraries/DisputeTypes.sol";
...
@@ -8,113 +8,120 @@ import "../../libraries/DisputeTypes.sol";
* @notice This library contains helper functions for working with the `Position` type.
* @notice This library contains helper functions for working with the `Position` type.
*/
*/
library LibPosition {
library LibPosition {
function wrap(uint64 _depth, uint64 _indexAtDepth) internal pure returns (Position
_position
) {
function wrap(uint64 _depth, uint64 _indexAtDepth) internal pure returns (Position
position_
) {
assembly {
assembly {
_position
:= or(shl(0x40, _depth), _indexAtDepth)
position_
:= or(shl(0x40, _depth), _indexAtDepth)
}
}
}
}
/**
/**
* @notice Pulls the `depth` out of a packed `Position` type.
* @notice Pulls the `depth` out of a packed `Position` type.
* @param _position The position to get the `depth` of.
* @return depth_ The `depth` of the `position`.
*/
*/
function depth(Position
position) internal pure returns (uint64 _depth
) {
function depth(Position
_position) internal pure returns (uint64 depth_
) {
// Shift the high-order 64 bits into the low-order 64 bits, leaving only the `depth`.
// Shift the high-order 64 bits into the low-order 64 bits, leaving only the `depth`.
assembly {
assembly {
_depth := shr(0x40,
position)
depth_ := shr(0x40, _
position)
}
}
}
}
/**
/**
* @notice Pulls the `indexAtDepth` out of a packed `Position` type.
* @notice Pulls the `indexAtDepth` out of a packed `Position` type.
* @param _position The position to get the `indexAtDepth` of.
* @return indexAtDepth_ The `indexAtDepth` of the `position`.
*/
*/
function indexAtDepth(Position
position) internal pure returns (uint64 _indexAtDepth
) {
function indexAtDepth(Position
_position) internal pure returns (uint64 indexAtDepth_
) {
// Clean the high-order 192 bits by shifting the position left and then right again, leaving
// Clean the high-order 192 bits by shifting the position left and then right again, leaving
// only the `indexAtDepth`.
// only the `indexAtDepth`.
assembly {
assembly {
_indexAtDepth := shr(0xC0, shl(0xC0,
position))
indexAtDepth_ := shr(0xC0, shl(0xC0, _
position))
}
}
}
}
/**
/**
* @notice Get the position to the left of `position`.
* @notice Get the position to the left of `position`.
* @param position The position to get the left position of.
* @param
_
position The position to get the left position of.
* @return
_left
The position to the left of `position`.
* @return
left_
The position to the left of `position`.
*/
*/
function left(Position
position) internal pure returns (Position _left
) {
function left(Position
_position) internal pure returns (Position left_
) {
uint64 _depth = depth(position);
uint64 _depth = depth(
_
position);
uint64 _indexAtDepth = indexAtDepth(position);
uint64 _indexAtDepth = indexAtDepth(
_
position);
// Left = { depth: position.depth + 1, indexAtDepth: position.indexAtDepth * 2 }
// Left = { depth: position.depth + 1, indexAtDepth: position.indexAtDepth * 2 }
assembly {
assembly {
_left
:= or(shl(0x40, add(_depth, 0x01)), shl(0x01, _indexAtDepth))
left_
:= or(shl(0x40, add(_depth, 0x01)), shl(0x01, _indexAtDepth))
}
}
}
}
/**
/**
* @notice Get the position to the right of `position`.
* @notice Get the position to the right of `position`.
* @param position The position to get the right position of.
* @param
_
position The position to get the right position of.
* @return
_right
The position to the right of `position`.
* @return
right_
The position to the right of `position`.
*/
*/
function right(Position
position) internal pure returns (Position _right
) {
function right(Position
_position) internal pure returns (Position right_
) {
uint64 _depth = depth(position);
uint64 _depth = depth(
_
position);
uint64 _indexAtDepth = indexAtDepth(position);
uint64 _indexAtDepth = indexAtDepth(
_
position);
// Right = { depth: position.depth + 1, indexAtDepth: position.indexAtDepth * 2 + 1 }
// Right = { depth: position.depth + 1, indexAtDepth: position.indexAtDepth * 2 + 1 }
assembly {
assembly {
_right
:= or(shl(0x40, add(_depth, 0x01)), add(shl(0x01, _indexAtDepth), 0x01))
right_
:= or(shl(0x40, add(_depth, 0x01)), add(shl(0x01, _indexAtDepth), 0x01))
}
}
}
}
/**
/**
* @notice Get the parent position of `position`.
* @notice Get the parent position of `position`.
* @param position The position to get the parent position of.
* @param
_
position The position to get the parent position of.
* @return
_parent
The parent position of `position`.
* @return
parent_
The parent position of `position`.
*/
*/
function parent(Position
position) internal pure returns (Position _parent
) {
function parent(Position
_position) internal pure returns (Position parent_
) {
uint64 _depth = depth(position);
uint64 _depth = depth(
_
position);
uint64 _indexAtDepth = indexAtDepth(position);
uint64 _indexAtDepth = indexAtDepth(
_
position);
// Parent = { depth: position.depth - 1, indexAtDepth: position.indexAtDepth / 2 }
// Parent = { depth: position.depth - 1, indexAtDepth: position.indexAtDepth / 2 }
assembly {
assembly {
_parent
:= or(shl(0x40, sub(_depth, 0x01)), shr(0x01, _indexAtDepth))
parent_
:= or(shl(0x40, sub(_depth, 0x01)), shr(0x01, _indexAtDepth))
}
}
}
}
/**
/**
* @notice Get the deepest, right most index relative to the `position`.
* @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 _position The position to get the relative deepest, right most index of.
* @param maxDepth The maximum depth of the game.
* @param _maxDepth The maximum depth of the game.
* @return _rightIndex The deepest, right most index relative to the `position`.
* @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 (uint64 _rightIndex
) {
function rightIndex(Position
_position, uint256 _maxDepth) internal pure returns (uint64 rightIndex_
) {
assembly {
assembly {
_rightIndex := shr(0xC0, shl(0xC0,
position))
rightIndex_ := shr(0xC0, shl(0xC0, _
position))
// Walk down to the max depth by moving right
// Walk down to the max depth by moving right
for { let i := shr(0x40,
position) } lt(i, sub(
maxDepth, 0x01)) { i := add(i, 0x01) } {
for { let i := shr(0x40,
_position) } lt(i, sub(_
maxDepth, 0x01)) { i := add(i, 0x01) } {
_rightIndex := add(0x01, shl(0x01, _rightIndex
))
rightIndex_ := add(0x01, shl(0x01, rightIndex_
))
}
}
}
}
}
}
/**
/**
* @notice Get the attack position relative to `position`.
* @notice Get the attack position relative to `position`.
* @param _position The position to get the relative attack position of.
* @return attack_ The attack position relative to `position`.
*/
*/
function attack(Position
position) internal pure returns (Position _attack
) {
function attack(Position
_position) internal pure returns (Position attack_
) {
return left(position);
return left(
_
position);
}
}
/**
/**
* @notice Get the defend position of `position`.
* @notice Get the defense position relative to `position`.
* @param _position The position to get the relative defense position of.
* @return defense_ The defense position relative to `position`.
*/
*/
function defend(Position
position) internal pure returns (Position _defend
) {
function defend(Position
_position) internal pure returns (Position defense_
) {
uint64 _depth = depth(position);
uint64 _depth = depth(
_
position);
uint64 _indexAtDepth = indexAtDepth(position);
uint64 _indexAtDepth = indexAtDepth(
_
position);
// Defend = { depth: position.depth + 1, indexAtDepth: ((position.indexAtDepth / 2) * 2 + 1) * 2 }
// Defend = { depth: position.depth + 1, indexAtDepth: ((position.indexAtDepth / 2) * 2 + 1) * 2 }
assembly {
assembly {
_defend
:= or(shl(0x40, add(_depth, 0x01)), shl(0x01, add(0x01, shl(0x01, shr(0x01, _indexAtDepth)))))
defense_
:= or(shl(0x40, 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