-
Notifications
You must be signed in to change notification settings - Fork 28
Add TimelockControllerEnumerable
#214
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
aviggiano
wants to merge
30
commits into
OpenZeppelin:master
Choose a base branch
from
aviggiano:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
91c8d32
Fix https://github.com/OpenZeppelin/openzeppelin-community-contracts/…
aviggiano d715512
Improve NatSpec
aviggiano a0144bf
Update tests
aviggiano f52ccdd
Update docs
aviggiano cc6c8ec
Update docs
aviggiano 4202f5b
Update contracts/governance/TimelockControllerEnumerable.sol
aviggiano 03ba004
Update contracts/governance/TimelockControllerEnumerable.sol
aviggiano 06db9b1
Update contracts/governance/TimelockControllerEnumerable.sol
aviggiano 1de67ee
Update contracts/governance/TimelockControllerEnumerable.sol
aviggiano 2c4c809
Update contracts/governance/TimelockControllerEnumerable.sol
aviggiano 31a68c9
Update contracts/governance/TimelockControllerEnumerable.sol
aviggiano d82ab45
Update contracts/governance/TimelockControllerEnumerable.sol
aviggiano 370e71f
Update contracts/governance/TimelockControllerEnumerable.sol
aviggiano 5e25a62
Update contracts/governance/TimelockControllerEnumerable.sol
aviggiano 4a2fa88
Update contracts/governance/TimelockControllerEnumerable.sol
aviggiano d8c165e
Update contracts/governance/TimelockControllerEnumerable.sol
aviggiano 79d5df3
Update contracts/governance/TimelockControllerEnumerable.sol
aviggiano 6bdb6a5
Update contracts/governance/TimelockControllerEnumerable.sol
aviggiano a77ba8e
Update contracts/governance/TimelockControllerEnumerable.sol
aviggiano a53e607
Update contracts/governance/TimelockControllerEnumerable.sol
aviggiano 9469bbb
Update contracts/governance/TimelockControllerEnumerable.sol
aviggiano ce6e936
Update contracts/governance/TimelockControllerEnumerable.sol
aviggiano 59cacbb
Update contracts/governance/TimelockControllerEnumerable.sol
aviggiano 6fe8afe
Update contracts/governance/TimelockControllerEnumerable.sol
aviggiano 9e373ca
Update contracts/governance/TimelockControllerEnumerable.sol
aviggiano 857c063
Merge branch 'master' of github.com:OpenZeppelin/openzeppelin-communi…
aviggiano 464e3f8
Merge branch 'master' of github.com:aviggiano/openzeppelin-community-…
aviggiano e47ba01
Add WARNING to operationsBatch
aviggiano 6f13062
Make TimelockControllerEnumerable abstract
aviggiano ede0719
Add range functions
aviggiano File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,196 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.20; | ||
|
||
import {TimelockController} from "@openzeppelin/contracts/governance/TimelockController.sol"; | ||
import {EnumerableSet} from "@openzeppelin/contracts/utils/structs/EnumerableSet.sol"; | ||
|
||
/// @dev Extends the TimelockController to allow for enumerable operations | ||
contract TimelockControllerEnumerable is TimelockController { | ||
using EnumerableSet for EnumerableSet.Bytes32Set; | ||
|
||
/// @notice The operation struct | ||
struct Operation { | ||
address target; | ||
uint256 value; | ||
bytes data; | ||
bytes32 predecessor; | ||
bytes32 salt; | ||
uint256 delay; | ||
} | ||
|
||
/// @notice The operation batch struct | ||
struct OperationBatch { | ||
address[] targets; | ||
uint256[] values; | ||
bytes[] payloads; | ||
bytes32 predecessor; | ||
bytes32 salt; | ||
uint256 delay; | ||
} | ||
|
||
/// @notice The error when the operation index is not found | ||
error OperationIndexNotFound(uint256 index); | ||
/// @notice The error when the operation id is not found | ||
aviggiano marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
error OperationIdNotFound(bytes32 id); | ||
/// @notice The error when the operation batch index is not found | ||
aviggiano marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
error OperationBatchIndexNotFound(uint256 index); | ||
/// @notice The error when the operation batch id is not found | ||
aviggiano marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
error OperationBatchIdNotFound(bytes32 id); | ||
|
||
/// @notice The operations id set | ||
EnumerableSet.Bytes32Set private _operationsIdSet; | ||
/// @notice The operations map | ||
mapping(bytes32 id => Operation operation) private _operationsMap; | ||
|
||
/// @notice The operations batch id set | ||
EnumerableSet.Bytes32Set private _operationsBatchIdSet; | ||
/// @notice The operations batch map | ||
mapping(bytes32 id => OperationBatch operationBatch) private _operationsBatchMap; | ||
|
||
constructor( | ||
uint256 minDelay, | ||
address[] memory proposers, | ||
address[] memory executors, | ||
address canceller | ||
) TimelockController(minDelay, proposers, executors, canceller) {} | ||
|
||
/// @inheritdoc TimelockController | ||
/// @dev Store the operation the mapping and set | ||
function schedule( | ||
address target, | ||
uint256 value, | ||
bytes calldata data, | ||
bytes32 predecessor, | ||
bytes32 salt, | ||
uint256 delay | ||
) public virtual override { | ||
super.schedule(target, value, data, predecessor, salt, delay); | ||
bytes32 id = hashOperation(target, value, data, predecessor, salt); | ||
_operationsIdSet.add(id); | ||
_operationsMap[id] = Operation({ | ||
target: target, | ||
value: value, | ||
data: data, | ||
predecessor: predecessor, | ||
salt: salt, | ||
delay: delay | ||
}); | ||
} | ||
|
||
/// @inheritdoc TimelockController | ||
/// @dev Store the operationBatch the mapping and set | ||
function scheduleBatch( | ||
address[] calldata targets, | ||
uint256[] calldata values, | ||
bytes[] calldata payloads, | ||
bytes32 predecessor, | ||
bytes32 salt, | ||
uint256 delay | ||
) public virtual override { | ||
super.scheduleBatch(targets, values, payloads, predecessor, salt, delay); | ||
bytes32 id = hashOperationBatch(targets, values, payloads, predecessor, salt); | ||
_operationsBatchIdSet.add(id); | ||
_operationsBatchMap[id] = OperationBatch({ | ||
targets: targets, | ||
values: values, | ||
payloads: payloads, | ||
predecessor: predecessor, | ||
salt: salt, | ||
delay: delay | ||
}); | ||
} | ||
|
||
/// @inheritdoc TimelockController | ||
/// @dev Remove the operation from the mapping and set | ||
function cancel(bytes32 id) public virtual override { | ||
super.cancel(id); | ||
if (_operationsIdSet.contains(id)) { | ||
_operationsIdSet.remove(id); | ||
delete _operationsMap[id]; | ||
} | ||
if (_operationsBatchIdSet.contains(id)) { | ||
_operationsBatchIdSet.remove(id); | ||
delete _operationsBatchMap[id]; | ||
} | ||
} | ||
aviggiano marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/// @dev Return the operations from the mapping and set | ||
/// @return operations_ The operations array | ||
aviggiano marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
function operations() public view returns (Operation[] memory operations_) { | ||
aviggiano marked this conversation as resolved.
Show resolved
Hide resolved
aviggiano marked this conversation as resolved.
Show resolved
Hide resolved
|
||
uint256 operationsCount_ = _operationsIdSet.length(); | ||
operations_ = new Operation[](operationsCount_); | ||
for (uint256 i = 0; i < operationsCount_; i++) { | ||
operations_[i] = _operationsMap[_operationsIdSet.at(i)]; | ||
} | ||
return operations_; | ||
} | ||
|
||
/// @dev Return the number of operations from the set | ||
/// @return operationsCount_ The number of operations | ||
aviggiano marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
function operationsCount() public view returns (uint256 operationsCount_) { | ||
operationsCount_ = _operationsIdSet.length(); | ||
return operationsCount_; | ||
} | ||
|
||
/// @dev Return the operation at the given index from the mapping and set | ||
/// @param index The index of the operation | ||
/// @return operation_ The operation | ||
aviggiano marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
function operation(uint256 index) public view returns (Operation memory operation_) { | ||
if (index >= _operationsIdSet.length()) { | ||
revert OperationIndexNotFound(index); | ||
} | ||
operation_ = _operationsMap[_operationsIdSet.at(index)]; | ||
return operation_; | ||
} | ||
|
||
/// @dev Return the operation with the given id from the mapping and set | ||
/// @param id The id of the operation | ||
/// @return operation_ The operation | ||
aviggiano marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
function operation(bytes32 id) public view returns (Operation memory operation_) { | ||
if (!_operationsIdSet.contains(id)) { | ||
revert OperationIdNotFound(id); | ||
} | ||
operation_ = _operationsMap[id]; | ||
return operation_; | ||
} | ||
|
||
/// @dev Return the operationsBatch from the mapping and set | ||
/// @return operationsBatch_ The operationsBatch array | ||
function operationsBatch() public view returns (OperationBatch[] memory operationsBatch_) { | ||
aviggiano marked this conversation as resolved.
Show resolved
Hide resolved
|
||
uint256 operationsBatchCount_ = _operationsBatchIdSet.length(); | ||
operationsBatch_ = new OperationBatch[](operationsBatchCount_); | ||
for (uint256 i = 0; i < operationsBatchCount_; i++) { | ||
operationsBatch_[i] = _operationsBatchMap[_operationsBatchIdSet.at(i)]; | ||
} | ||
return operationsBatch_; | ||
} | ||
|
||
/// @dev Return the number of operationsBatch from the set | ||
/// @return operationsBatchCount_ The number of operationsBatch | ||
function operationsBatchCount() public view returns (uint256 operationsBatchCount_) { | ||
operationsBatchCount_ = _operationsBatchIdSet.length(); | ||
return operationsBatchCount_; | ||
} | ||
|
||
/// @dev Return the operationsBatch at the given index from the mapping and set | ||
/// @param index The index of the operationsBatch | ||
/// @return operationBatch_ The operationsBatch | ||
function operationBatch(uint256 index) public view returns (OperationBatch memory operationBatch_) { | ||
if (index >= _operationsBatchIdSet.length()) { | ||
revert OperationBatchIndexNotFound(index); | ||
} | ||
operationBatch_ = _operationsBatchMap[_operationsBatchIdSet.at(index)]; | ||
return operationBatch_; | ||
} | ||
|
||
/// @dev Return the operationsBatch with the given id from the mapping and set | ||
/// @param id The id of the operationsBatch | ||
/// @return operationBatch_ The operationsBatch | ||
function operationBatch(bytes32 id) public view returns (OperationBatch memory operationBatch_) { | ||
if (!_operationsBatchIdSet.contains(id)) { | ||
revert OperationBatchIdNotFound(id); | ||
} | ||
operationBatch_ = _operationsBatchMap[id]; | ||
return operationBatch_; | ||
} | ||
} |
This file contains hidden or 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,177 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.8.20; | ||
|
||
import {Test} from "forge-std/Test.sol"; | ||
import { | ||
TimelockControllerEnumerable | ||
} from "@openzeppelin/community-contracts/governance/TimelockControllerEnumerable.sol"; | ||
|
||
contract TimelockControllerEnumerableTest is Test { | ||
TimelockControllerEnumerable public timelockControllerEnumerable; | ||
|
||
event Call(); | ||
|
||
function setUp() public { | ||
address[] memory proposers = new address[](1); | ||
address[] memory executors = new address[](1); | ||
proposers[0] = address(this); | ||
executors[0] = address(this); | ||
uint256 minDelay = 1 days; | ||
timelockControllerEnumerable = new TimelockControllerEnumerable(minDelay, proposers, executors, address(0)); | ||
} | ||
|
||
function call() external { | ||
emit Call(); | ||
} | ||
|
||
function test_schedule() public { | ||
timelockControllerEnumerable.schedule( | ||
address(this), | ||
0, | ||
abi.encodeCall(this.call, ()), | ||
bytes32(0), | ||
bytes32(0), | ||
1 days | ||
); | ||
assertEq(timelockControllerEnumerable.operationsCount(), 1); | ||
TimelockControllerEnumerable.Operation memory operation = timelockControllerEnumerable.operation(uint256(0)); | ||
assertEq(operation.target, address(this)); | ||
assertEq(operation.value, 0); | ||
assertEq(operation.data, abi.encodeCall(this.call, ())); | ||
assertEq(operation.predecessor, bytes32(0)); | ||
assertEq(operation.salt, bytes32(0)); | ||
assertEq(operation.delay, 1 days); | ||
bytes32 id = timelockControllerEnumerable.hashOperation( | ||
address(this), | ||
0, | ||
abi.encodeCall(this.call, ()), | ||
bytes32(0), | ||
bytes32(0) | ||
); | ||
operation = timelockControllerEnumerable.operation(id); | ||
assertEq(operation.target, address(this)); | ||
assertEq(operation.value, 0); | ||
assertEq(operation.data, abi.encodeCall(this.call, ())); | ||
assertEq(operation.predecessor, bytes32(0)); | ||
assertEq(operation.salt, bytes32(0)); | ||
assertEq(operation.delay, 1 days); | ||
} | ||
|
||
function test_schedule_execute() public { | ||
test_schedule(); | ||
TimelockControllerEnumerable.Operation memory operation = timelockControllerEnumerable.operation(uint256(0)); | ||
bytes32 id = timelockControllerEnumerable.hashOperation( | ||
operation.target, | ||
operation.value, | ||
operation.data, | ||
operation.predecessor, | ||
operation.salt | ||
); | ||
assertEq(timelockControllerEnumerable.isOperationPending(id), true); | ||
vm.warp(block.timestamp + operation.delay); | ||
timelockControllerEnumerable.execute( | ||
operation.target, | ||
operation.value, | ||
operation.data, | ||
operation.predecessor, | ||
operation.salt | ||
); | ||
assertEq(timelockControllerEnumerable.isOperationPending(id), false); | ||
} | ||
|
||
function test_scheduleBatch() public { | ||
address[] memory targets = new address[](1); | ||
uint256[] memory values = new uint256[](1); | ||
bytes[] memory payloads = new bytes[](1); | ||
targets[0] = address(this); | ||
values[0] = 0; | ||
payloads[0] = abi.encodeCall(this.call, ()); | ||
timelockControllerEnumerable.scheduleBatch(targets, values, payloads, bytes32(0), bytes32(0), 1 days); | ||
assertEq(timelockControllerEnumerable.operationsBatchCount(), 1); | ||
TimelockControllerEnumerable.OperationBatch memory operationBatch = timelockControllerEnumerable.operationBatch( | ||
uint256(0) | ||
); | ||
assertEq(operationBatch.targets[0], address(this)); | ||
assertEq(operationBatch.values[0], 0); | ||
assertEq(operationBatch.payloads[0], abi.encodeCall(this.call, ())); | ||
assertEq(operationBatch.predecessor, bytes32(0)); | ||
assertEq(operationBatch.salt, bytes32(0)); | ||
assertEq(operationBatch.delay, 1 days); | ||
bytes32 id = timelockControllerEnumerable.hashOperationBatch(targets, values, payloads, bytes32(0), bytes32(0)); | ||
operationBatch = timelockControllerEnumerable.operationBatch(id); | ||
assertEq(operationBatch.targets[0], address(this)); | ||
assertEq(operationBatch.values[0], 0); | ||
assertEq(operationBatch.payloads[0], abi.encodeCall(this.call, ())); | ||
assertEq(operationBatch.predecessor, bytes32(0)); | ||
assertEq(operationBatch.salt, bytes32(0)); | ||
assertEq(operationBatch.delay, 1 days); | ||
} | ||
|
||
function test_scheduleBatch_execute() public { | ||
test_scheduleBatch(); | ||
TimelockControllerEnumerable.OperationBatch memory operationBatch = timelockControllerEnumerable.operationBatch( | ||
uint256(0) | ||
); | ||
bytes32 id = timelockControllerEnumerable.hashOperationBatch( | ||
operationBatch.targets, | ||
operationBatch.values, | ||
operationBatch.payloads, | ||
operationBatch.predecessor, | ||
operationBatch.salt | ||
); | ||
assertEq(timelockControllerEnumerable.isOperationPending(id), true); | ||
vm.warp(block.timestamp + operationBatch.delay); | ||
timelockControllerEnumerable.executeBatch( | ||
operationBatch.targets, | ||
operationBatch.values, | ||
operationBatch.payloads, | ||
operationBatch.predecessor, | ||
operationBatch.salt | ||
); | ||
assertEq(timelockControllerEnumerable.isOperationPending(id), false); | ||
} | ||
|
||
function test_cancel_schedule() public { | ||
timelockControllerEnumerable.schedule( | ||
address(this), | ||
0, | ||
abi.encodeCall(this.call, ()), | ||
bytes32(0), | ||
bytes32(0), | ||
1 days | ||
); | ||
assertEq(timelockControllerEnumerable.operationsCount(), 1); | ||
bytes32 id = timelockControllerEnumerable.hashOperation( | ||
address(this), | ||
0, | ||
abi.encodeCall(this.call, ()), | ||
bytes32(0), | ||
bytes32(0) | ||
); | ||
timelockControllerEnumerable.cancel(id); | ||
assertEq(timelockControllerEnumerable.operationsCount(), 0); | ||
vm.expectRevert(abi.encodeWithSelector(TimelockControllerEnumerable.OperationIdNotFound.selector, id)); | ||
timelockControllerEnumerable.operation(id); | ||
vm.expectRevert(abi.encodeWithSelector(TimelockControllerEnumerable.OperationIndexNotFound.selector, 0)); | ||
timelockControllerEnumerable.operation(uint256(0)); | ||
} | ||
|
||
function test_cancel_scheduleBatch() public { | ||
address[] memory targets = new address[](1); | ||
uint256[] memory values = new uint256[](1); | ||
bytes[] memory payloads = new bytes[](1); | ||
targets[0] = address(this); | ||
values[0] = 0; | ||
payloads[0] = abi.encodeCall(this.call, ()); | ||
timelockControllerEnumerable.scheduleBatch(targets, values, payloads, bytes32(0), bytes32(0), 1 days); | ||
assertEq(timelockControllerEnumerable.operationsBatchCount(), 1); | ||
bytes32 id = timelockControllerEnumerable.hashOperationBatch(targets, values, payloads, bytes32(0), bytes32(0)); | ||
timelockControllerEnumerable.cancel(id); | ||
assertEq(timelockControllerEnumerable.operationsBatchCount(), 0); | ||
vm.expectRevert(abi.encodeWithSelector(TimelockControllerEnumerable.OperationBatchIdNotFound.selector, id)); | ||
timelockControllerEnumerable.operationBatch(id); | ||
vm.expectRevert(abi.encodeWithSelector(TimelockControllerEnumerable.OperationBatchIndexNotFound.selector, 0)); | ||
timelockControllerEnumerable.operationBatch(uint256(0)); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.