Skip to content
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

Feat: Now proposals can be approved to bypass cooldown #10

Merged
merged 4 commits into from
Aug 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion contracts/SecretDelay.sol
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,17 @@ contract SecretDelay is Modifier {
uint256 indexed startingVetoedTrxNonce,
uint256 numberOfTrxVetoed
);
event TransactionsApproved(
uint256 indexed startingApprovedTrxNonce,
uint256 numberOfTrxApproved
);

CountersUpgradeable.Counter public salt;
uint256 public txCooldown;
uint256 public txExpiration;
uint256 public txNonce; // index of proposal in queue to be executed
uint256 public queuePointer; // index of last slot in queue where next proposal is added
uint256 public approved; // number of next transactions approved to be executed before cooldown
// Mapping of queue nonce to transaction hash.
mapping(uint256 => bytes32) public txHash;
// Mapping of queue nonce to creation timestamp.
Expand All @@ -45,7 +50,7 @@ contract SecretDelay is Modifier {
modifier isExecutable() {
require(txNonce < queuePointer, "Transaction queue is empty");
require(
block.timestamp - txCreatedAt[txNonce] >= txCooldown,
block.timestamp - txCreatedAt[txNonce] >= txCooldown || approved > 0,
"Transaction is still in cooldown"
);
if (txExpiration != 0) {
Expand All @@ -54,6 +59,7 @@ contract SecretDelay is Modifier {
"Transaction expired"
);
}
if (approved > 0) approved--;
_;
}

Expand Down Expand Up @@ -135,10 +141,30 @@ contract SecretDelay is Modifier {
function vetoNextTransactions(uint256 _trxsToVeto) public onlyOwner {
require(_trxsToVeto > 0, "Atleast veto one transaction");
require(_trxsToVeto + txNonce <= queuePointer, "Cannot be higher than queuePointer");
_adjustApprovals(txNonce+_trxsToVeto);
emit TransactionsVetoed(txNonce, _trxsToVeto);
txNonce += _trxsToVeto;
}

function vetoNextTransactionsAndApprove(uint256 _trxsToVeto, uint256 _transactions)
public
onlyOwner
{
// vetos transactions
vetoNextTransactions(_trxsToVeto);

// approves transactions
// note: unknown transactions won't be approved because if all transactions are vetoed
// and no transactions in queue, it will revert execution
approveNext(_transactions);
}

function approveNext(uint256 _transactions) public onlyOwner {
require(_transactions > 0, "Must approve at least one tx");
require(queuePointer - txNonce >= _transactions, "Cannot approve unknown tx");
approved = _transactions;
}

/// @dev Adds a transaction to the queue (same as avatar interface so that this can be placed between other modules and the avatar).
/// @param to Destination address of module transaction
/// @param value Ether value of module transaction
Expand Down Expand Up @@ -263,4 +289,14 @@ contract SecretDelay is Modifier {
function getTxCreatedAt(uint256 _nonce) public view returns (uint256) {
return (txCreatedAt[_nonce]);
}

function _adjustApprovals(uint256 _nonce) internal {
uint256 delta = _nonce - txNonce;

if (delta > approved) {
if (approved != 0) approved = 0;
} else {
approved -= delta;
}
}
}
13 changes: 13 additions & 0 deletions contracts/test/TestContract.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// SPDX-License-Identifier: GPL-3.0-or-later

pragma solidity >=0.8.0;

import "@openzeppelin/contracts/access/Ownable.sol";

contract TestContract is Ownable {
event ButtonPushed(address pusher);

function pushButton() public onlyOwner {
emit ButtonPushed(msg.sender);
}
}
Loading