-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
649 additions
and
126 deletions.
There are no files selected for viewing
Empty file.
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// SPDX-FileCopyrightText: 2023 Lido <[email protected]> | ||
// SPDX-License-Identifier: GPL-3.0 | ||
pragma solidity 0.8.21; | ||
|
||
/// @author madlabman | ||
library Batch { | ||
/// @notice Serialize node operator id, batch start and count of keys into a single bytes32 value | ||
function serialize( | ||
uint128 nodeOperatorId, | ||
uint64 start, | ||
uint64 count | ||
) internal pure returns (bytes32 s) { | ||
return bytes32(abi.encodePacked(nodeOperatorId, start, count)); | ||
} | ||
|
||
/// @notice Deserialize node operator id, batch start and count of keys from a single bytes32 value | ||
function deserialize( | ||
bytes32 b | ||
) internal pure returns (uint128 nodeOperatorId, uint64 start, uint64 count) { | ||
assembly { | ||
nodeOperatorId := shr(128, b) | ||
start := shr(64, b) | ||
count := b | ||
} | ||
} | ||
|
||
function isNil(bytes32 b) internal pure returns (bool) { | ||
return b == bytes32(0); | ||
} | ||
} |
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,81 @@ | ||
// SPDX-FileCopyrightText: 2023 Lido <[email protected]> | ||
// SPDX-License-Identifier: GPL-3.0 | ||
pragma solidity 0.8.21; | ||
|
||
|
||
/// @author madlabman | ||
library QueueLib { | ||
bytes32 public constant NULL_POINTER = bytes32(0); | ||
|
||
struct Queue { | ||
mapping(bytes32 => bytes32) queue; | ||
bytes32 front; | ||
bytes32 back; | ||
} | ||
|
||
function enqueue(Queue storage self, bytes32 item) internal { | ||
require(item != NULL_POINTER, "Queue: item is zero"); | ||
require(self.queue[item] == NULL_POINTER, "Queue: item already enqueued"); | ||
|
||
if (self.front == self.queue[self.front]) { | ||
self.queue[self.front] = item; | ||
} | ||
|
||
self.queue[self.back] = item; | ||
self.back = item; | ||
} | ||
|
||
function dequeue(Queue storage self) internal notEmpty(self) returns (bytes32 item) { | ||
item = self.queue[self.front]; | ||
self.front = item; | ||
} | ||
|
||
function peek(Queue storage self) internal view returns (bytes32) { | ||
return self.queue[self.front]; | ||
} | ||
|
||
function at(Queue storage self, bytes32 pointer) internal view returns (bytes32) { | ||
return self.queue[pointer]; | ||
} | ||
|
||
function list(Queue storage self, bytes32 pointer, uint256 limit) internal notEmpty(self) view returns ( | ||
bytes32[] memory items, | ||
bytes32 /* pointer */, | ||
uint256 /* count */ | ||
) { | ||
items = new bytes32[](limit); | ||
|
||
uint256 i; | ||
for (; i < limit; i++) { | ||
bytes32 item = self.queue[pointer]; | ||
if (item == NULL_POINTER) { | ||
break; | ||
} | ||
|
||
items[i] = item; | ||
pointer = item; | ||
} | ||
|
||
return (items, pointer, i); | ||
} | ||
|
||
function isEmpty(Queue storage self) internal view returns (bool) { | ||
return self.front == self.back; | ||
} | ||
|
||
function remove(Queue storage self, bytes32 pointerToItem, bytes32 item) internal { | ||
require(self.queue[pointerToItem] == item, "Queue: wrong pointer given"); | ||
|
||
self.queue[pointerToItem] = self.queue[item]; | ||
self.queue[item] = NULL_POINTER; | ||
|
||
if (self.back == item) { | ||
self.back = pointerToItem; | ||
} | ||
} | ||
|
||
modifier notEmpty(Queue storage self) { | ||
require(!isEmpty(self), "Queue: empty"); | ||
_; | ||
} | ||
} |
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,54 @@ | ||
// SPDX-FileCopyrightText: 2023 Lido <[email protected]> | ||
// SPDX-License-Identifier: GPL-3.0 | ||
pragma solidity 0.8.21; | ||
|
||
import "forge-std/Test.sol"; | ||
|
||
import { Batch } from "../src/lib/Batch.sol"; | ||
|
||
contract BatchTest is Test { | ||
function test_serialize() public { | ||
bytes32 b = Batch.serialize({ | ||
nodeOperatorId: 999, | ||
start: 3, | ||
count: 42 | ||
}); | ||
|
||
assertEq( | ||
b, | ||
// noIndex | start | count | | ||
0x000000000000000000000000000003e70000000000000003000000000000002a | ||
); | ||
} | ||
|
||
function test_deserialize() public { | ||
(uint128 nodeOperatorId, uint64 start, uint64 count) = Batch | ||
.deserialize( | ||
0x0000000000000000000000000000000000000000000000000000000000000000 | ||
); | ||
|
||
assertEq(nodeOperatorId, 0, "nodeOperatorId != 0"); | ||
assertEq(start, 0, "start != 0"); | ||
assertEq(count, 0, "count != 0"); | ||
|
||
(nodeOperatorId, start, count) = Batch.deserialize( | ||
0x000000000000000000000000000003e70000000000000003000000000000002a | ||
); | ||
|
||
assertEq(nodeOperatorId, 999, "nodeOperatorId != 999"); | ||
assertEq(start, 3, "start != 3"); | ||
assertEq(count, 42, "count != 42"); | ||
|
||
(nodeOperatorId, start, count) = Batch.deserialize( | ||
0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff | ||
); | ||
|
||
assertEq( | ||
nodeOperatorId, | ||
type(uint128).max, | ||
"nodeOperatorId != uint128.max" | ||
); | ||
assertEq(start, type(uint64).max, "start != uint64.max"); | ||
assertEq(count, type(uint64).max, "count != uint64.max"); | ||
} | ||
} |
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.