Skip to content

Commit

Permalink
feat: merge defender logic, clean up sett forks and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
notbdu committed Mar 2, 2021
1 parent d21edd8 commit 02574ec
Show file tree
Hide file tree
Showing 17 changed files with 263 additions and 192 deletions.
5 changes: 2 additions & 3 deletions contracts/badger-remote/DefenderStorageless.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@ pragma solidity ^0.6.0;

import "interfaces/remote/IRemoteDefender.sol";

import "./PauseableStorageless.sol";

/*
DefenderStorageless is a no-storage required inheritable defender of unapproved contract access.
Contracts may safely inherit this w/o messing up their internal storage layout.
*/
contract DefenderStorageless is PauseableStorageless {
contract DefenderStorageless {
// Defend against access by unapproved contracts (EOAs are allowed access).
modifier defend(address defender) {
require(IRemoteDefender(defender).approved(msg.sender) || msg.sender == tx.origin, "Access denied for caller");
require(!IRemoteDefender(defender).frozen(msg.sender), "Caller frozen");
_;
}
}
12 changes: 6 additions & 6 deletions contracts/badger-remote/PauseableStorageless.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ contract PauseableStorageless {
*
* - The contract must not be paused.
*/
modifier whenNotPaused(address pauser) {
modifier whenNotPausedRemote(address pauser) {
require(!IRemotePauser(pauser).paused(), "Pausable: paused");
_;
}
Expand All @@ -38,7 +38,7 @@ contract PauseableStorageless {
*
* - The contract must be paused.
*/
modifier whenPaused(address pauser) {
modifier whenPausedRemote(address pauser) {
require(IRemotePauser(pauser).paused(), "Pausable: not paused");
_;
}
Expand All @@ -50,8 +50,8 @@ contract PauseableStorageless {
*
* - The contract must not be paused.
*/
function _pause(address pauser) internal virtual whenNotPaused {
IRemotePauser(pauser).pause();
function _pause(address pauser) internal virtual {
IRemotePauser(pauser).pause();
emit Paused(msg.sender);
}

Expand All @@ -62,8 +62,8 @@ contract PauseableStorageless {
*
* - The contract must be paused.
*/
function _unpause() internal virtual whenPaused {
IRemotePauser(pauser).unpause();
function _unpause(address pauser) internal virtual {
IRemotePauser(pauser).unpause();
emit Unpaused(msg.sender);
}
}
75 changes: 66 additions & 9 deletions contracts/badger-remote/RemoteDefenderUpgradeable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,87 @@ pragma solidity ^0.6.0;

import "deps/@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";

import "./RemotePauserUpgradeable.sol";
import "./RemoteFreezerUpgradeable.sol";

/*
RemoteDefenderUpgradeable defends against unapproved address access.
It also handles freezing/pausing of contract addresses and EOAs.
It handles the following protective functionality (of contract addresses and EOAs):
- approved for contract specific or global access
- frozen from global access
- paused specific contract or globally
*/
contract RemoteDefenderUpgradeable is OwnableUpgradeable, RemoteFreezerUpgradeable, RemotePauserUpgradeable {
mapping(address => bool) private _approved;
contract RemoteDefenderUpgradeable is OwnableUpgradeable {
// Is contract address approved for global access?
mapping(address => bool) private _approvedGlobal;
// Is contract address approved for targeted access to msg.sender?
mapping(address => mapping(address => bool)) private _approvedTargeted;

// Is account address frozen?
mapping(address => bool) private _frozen;

// Is contract address paused?
mapping(address => bool) private _paused;
// Is everything paused globally?
bool private _pausedGlobal;

function initialize() public initializer {
__Ownable_init();
}

// Access control functions.
function approved(address account) external view returns (bool) {
return _approved[account];
if (_approvedTargeted[account][msg.sender]) {
return true;
}
return _approvedGlobal[account];
}

function approve(address account) external onlyOwner {
_approved[account] = true;
_approvedGlobal[account] = true;
}

function revoke(address account) external onlyOwner {
_approved[account] = false;
_approvedGlobal[account] = false;
}

function approveFor(address account, address target) external onlyOwner {
_approvedTargeted[account][target] = true;
}

function revokeFor(address account, address target) external onlyOwner {
_approvedTargeted[account][target] = false;
}

// Freezer functions.
function frozen(address account) external view returns (bool) {
return _frozen[account];
}

function freeze(address account) external onlyOwner {
_frozen[account] = true;
}

function unfreeze(address account) external onlyOwner {
_frozen[account] = false;
}

// Pauser functions.
function paused() external view returns (bool) {
return _pausedGlobal || _paused[msg.sender];
}

function pauseGlobal() external onlyOwner {
_pausedGlobal = true;
}

function unpauseGlobal() external onlyOwner {
_pausedGlobal = false;
}

function pause() external {
_paused[msg.sender] = true;
}

function unpause() external {
_paused[msg.sender] = false;
}

// Reserve storage space for upgrades.
Expand Down
32 changes: 0 additions & 32 deletions contracts/badger-remote/RemoteFreezerUpgradeable.sol

This file was deleted.

28 changes: 0 additions & 28 deletions contracts/badger-remote/RemotePauserUpgradeable.sol

This file was deleted.

4 changes: 2 additions & 2 deletions contracts/badger-sett/DiggSett.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import "../../deps/@openzeppelin/contracts-upgradeable/token/ERC20/ERC20Upgradea
import "interfaces/badger/IController.sol";
import "interfaces/digg/IDigg.sol";
import "interfaces/digg/IDiggStrategy.sol";
import "../Sett_C.sol";
import "./Sett.sol";

/*
bDIGG is denominated in scaledShares.
Expand All @@ -32,7 +32,7 @@ import "../Sett_C.sol";
* Transfer functions are now pausable along with all other non-permissioned write functions
* All permissioned write functions, with the exception of pause() & unpause(), are pausable as well
*/
contract DiggSett is Sett_C {
contract DiggSett is Sett {
using SafeERC20Upgradeable for IERC20Upgradeable;
using AddressUpgradeable for address;
using SafeMathUpgradeable for uint256;
Expand Down
Loading

0 comments on commit 02574ec

Please sign in to comment.