Skip to content

Commit

Permalink
cleaning up TimeLock and Transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
jac18281828 committed Oct 13, 2022
1 parent cc1a62f commit 1fcd740
Show file tree
Hide file tree
Showing 10 changed files with 393 additions and 31 deletions.
98 changes: 98 additions & 0 deletions abi/Governance.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@
"internalType": "uint256",
"name": "proposalId",
"type": "uint256"
},
{
"indexed": false,
"internalType": "uint256",
"name": "executedTransactionCount",
"type": "uint256"
}
],
"name": "ProposalExecuted",
Expand Down Expand Up @@ -72,6 +78,12 @@
"name": "proposalId",
"type": "uint256"
},
{
"indexed": false,
"internalType": "uint256",
"name": "transactionId",
"type": "uint256"
},
{
"indexed": false,
"internalType": "address",
Expand Down Expand Up @@ -100,6 +112,92 @@
"name": "ProposalTransactionAttached",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": false,
"internalType": "uint256",
"name": "proposalId",
"type": "uint256"
},
{
"indexed": false,
"internalType": "uint256",
"name": "transactionId",
"type": "uint256"
},
{
"indexed": false,
"internalType": "address",
"name": "target",
"type": "address"
},
{
"indexed": false,
"internalType": "uint256",
"name": "value",
"type": "uint256"
},
{
"indexed": false,
"internalType": "uint256",
"name": "scheduleTime",
"type": "uint256"
},
{
"indexed": false,
"internalType": "bytes32",
"name": "txHash",
"type": "bytes32"
}
],
"name": "ProposalTransactionCancelled",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": false,
"internalType": "uint256",
"name": "proposalId",
"type": "uint256"
},
{
"indexed": false,
"internalType": "uint256",
"name": "transactionId",
"type": "uint256"
},
{
"indexed": false,
"internalType": "address",
"name": "target",
"type": "address"
},
{
"indexed": false,
"internalType": "uint256",
"name": "value",
"type": "uint256"
},
{
"indexed": false,
"internalType": "uint256",
"name": "scheduleTime",
"type": "uint256"
},
{
"indexed": false,
"internalType": "bytes32",
"name": "txHash",
"type": "bytes32"
}
],
"name": "ProposalTransactionExecuted",
"type": "event"
},
{
"anonymous": false,
"inputs": [
Expand Down
33 changes: 33 additions & 0 deletions abi/Storage.json
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,11 @@
"name": "_scheduleTime",
"type": "uint256"
},
{
"internalType": "bytes32",
"name": "_txHash",
"type": "bytes32"
},
{
"internalType": "address",
"name": "_sender",
Expand Down Expand Up @@ -370,6 +375,29 @@
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "_proposalId",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "_transactionId",
"type": "uint256"
},
{
"internalType": "address",
"name": "_sender",
"type": "address"
}
],
"name": "clearTransaction",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
Expand Down Expand Up @@ -484,6 +512,11 @@
"internalType": "uint256",
"name": "_scheduleTime",
"type": "uint256"
},
{
"internalType": "bytes32",
"name": "_txHash",
"type": "bytes32"
}
],
"stateMutability": "view",
Expand Down
71 changes: 60 additions & 11 deletions contracts/CollectiveGovernance.sol
Original file line number Diff line number Diff line change
Expand Up @@ -158,17 +158,18 @@ contract CollectiveGovernance is Governance, VoteStrategy, ERC165 {
uint256 _scheduleTime
) external returns (uint256) {
require(_storage.getSender(_proposalId) == msg.sender, "Not sender");
bytes32 txHash = _timeLock.queueTransaction(_target, _value, _signature, _calldata, _scheduleTime);
uint256 transactionId = _storage.addTransaction(
_proposalId,
_target,
_value,
_signature,
_calldata,
_scheduleTime,
txHash,
msg.sender
);
bytes32 txHash = _timeLock.queueTransaction(_target, _value, _signature, _calldata, _scheduleTime);
emit ProposalTransactionAttached(msg.sender, _proposalId, _target, _value, _scheduleTime, txHash);
emit ProposalTransactionAttached(msg.sender, _proposalId, transactionId, _target, _value, _scheduleTime, txHash);
return transactionId;
}

Expand Down Expand Up @@ -222,6 +223,27 @@ contract CollectiveGovernance is Governance, VoteStrategy, ERC165 {
return isVoteOpenByProposalId[_proposalId] && getBlockTimestamp() < endTime && voteProceeding;
}

/// @notice End voting on an existing proposal by id. All scheduled transactions are cancelled and nothing is executed.
/// @param _proposalId The numeric id of the proposed vote
/// @dev It is not possible to end voting until the required duration has elapsed.
function endVoteAndCancelTransaction(uint256 _proposalId)
external
requireSupervisor(_proposalId)
requireVoteOpen(_proposalId)
{
uint256 _endTime = _storage.endTime(_proposalId);
require(
_endTime <= getBlockTimestamp() || _storage.isVeto(_proposalId) || _storage.isCancel(_proposalId),
"Vote in progress"
);
isVoteOpenByProposalId[_proposalId] = false;

cancelTransaction(_proposalId);

emit VoteClosed(_proposalId);
emit ProposalClosed(_proposalId);
}

/// @notice end voting on an existing proposal by id
/// @param _proposalId The numeric id of the proposed vote
/// @dev it is not possible to end voting until the required duration has elapsed
Expand Down Expand Up @@ -417,34 +439,61 @@ contract CollectiveGovernance is Governance, VoteStrategy, ERC165 {
require(!isVoteOpenByProposalId[_proposalId] && getBlockTimestamp() <= _startTime, "Not possible");
uint256 transactionCount = _storage.transactionCount(_proposalId);
for (uint256 tid = 0; tid < transactionCount; tid++) {
(address target, uint256 value, string memory signature, bytes memory _calldata, uint256 scheduleTime) = _storage
.getTransaction(_proposalId, tid);
(
address target,
uint256 value,
string memory signature,
bytes memory _calldata,
uint256 scheduleTime,
bytes32 txHash
) = _storage.getTransaction(_proposalId, tid);
_timeLock.cancelTransaction(target, value, signature, _calldata, scheduleTime);
_storage.clearTransaction(_proposalId, tid, msg.sender);
emit ProposalTransactionCancelled(_proposalId, tid, target, value, scheduleTime, txHash);
}
_storage.cancel(_proposalId, msg.sender);
emit ProposalClosed(_proposalId);
}

function executeTransaction(uint256 _proposalId) private {
uint256 transactionCount = _storage.transactionCount(_proposalId);
uint256 executedCount = 0;
if (transactionCount > 0) {
_storage.setExecuted(_proposalId, msg.sender);
for (uint256 tid = 0; tid < transactionCount; tid++) {
(address target, uint256 value, string memory signature, bytes memory _calldata, uint256 scheduleTime) = _storage
.getTransaction(_proposalId, tid);
_timeLock.executeTransaction(target, value, signature, _calldata, scheduleTime);
(
address target,
uint256 value,
string memory signature,
bytes memory _calldata,
uint256 scheduleTime,
bytes32 txHash
) = _storage.getTransaction(_proposalId, tid);
if (txHash.length > 0 && _timeLock._queuedTransaction(txHash)) {
_timeLock.executeTransaction(target, value, signature, _calldata, scheduleTime);
emit ProposalTransactionExecuted(_proposalId, tid, target, value, scheduleTime, txHash);
executedCount++;
}
}
emit ProposalExecuted(_proposalId);
emit ProposalExecuted(_proposalId, executedCount);
}
}

function cancelTransaction(uint256 _proposalId) private {
uint256 transactionCount = _storage.transactionCount(_proposalId);
if (transactionCount > 0) {
for (uint256 tid = 0; tid < transactionCount; tid++) {
(address target, uint256 value, string memory signature, bytes memory _calldata, uint256 scheduleTime) = _storage
.getTransaction(_proposalId, tid);
_timeLock.cancelTransaction(target, value, signature, _calldata, scheduleTime);
(
address target,
uint256 value,
string memory signature,
bytes memory _calldata,
uint256 scheduleTime,
bytes32 txHash
) = _storage.getTransaction(_proposalId, tid);
if (txHash.length > 0 && _timeLock._queuedTransaction(txHash)) {
_timeLock.cancelTransaction(target, value, signature, _calldata, scheduleTime);
}
}
}
}
Expand Down
21 changes: 20 additions & 1 deletion contracts/Governance.sol
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,25 @@ interface Governance is IERC165 {
event ProposalTransactionAttached(
address creator,
uint256 proposalId,
uint256 transactionId,
address target,
uint256 value,
uint256 scheduleTime,
bytes32 txHash
);
/// @notice transaction canceled on proposal
event ProposalTransactionCancelled(
uint256 proposalId,
uint256 transactionId,
address target,
uint256 value,
uint256 scheduleTime,
bytes32 txHash
);
/// @notice transaction executed on proposal
event ProposalTransactionExecuted(
uint256 proposalId,
uint256 transactionId,
address target,
uint256 value,
uint256 scheduleTime,
Expand All @@ -68,7 +87,7 @@ interface Governance is IERC165 {
/// @notice Voting is now closed for voting
event ProposalClosed(uint256 proposalId);
/// @notice The attached transactions are executed
event ProposalExecuted(uint256 proposalId);
event ProposalExecuted(uint256 proposalId, uint256 executedTransactionCount);

/// @notice propose a vote for the community
/// @return uint256 The id of the new proposal
Expand Down
Loading

0 comments on commit 1fcd740

Please sign in to comment.