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

Change vow to use a proxy #241

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 5 additions & 3 deletions src/test/clip.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import "ds-value/value.sol";

import {Vat} from "../vat.sol";
import {Spotter} from "../spot.sol";
import {Vow} from "../vow.sol";
import {Vow, VowProxy} from "../vow.sol";
import {GemJoin, DaiJoin} from "../join.sol";

import {Clipper} from "../clip.sol";
Expand Down Expand Up @@ -339,7 +339,9 @@ contract ClipperTest is DSTest {
spot = new Spotter(address(vat));
vat.rely(address(spot));

vow = new Vow(address(vat), address(0), address(0));
vow = Vow(address(new VowProxy()));
address vowImp = address(new Vow(address(vat)));
VowProxy(address(vow)).setImplementation(vowImp);
gold = new DSToken("GLD");
goldJoin = new GemJoin(address(vat), ilk, address(gold));
vat.rely(address(goldJoin));
Expand All @@ -357,7 +359,7 @@ contract ClipperTest is DSTest {
dog = new Dog(address(vat));
dog.file("vow", address(vow));
vat.rely(address(dog));
vow.rely(address(dog));
VowProxy(address(vow)).rely(address(dog));

vat.init(ilk);

Expand Down
14 changes: 9 additions & 5 deletions src/test/end.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import "ds-value/value.sol";
import {Vat} from '../vat.sol';
import {Cat} from '../cat.sol';
import {Dog} from '../dog.sol';
import {Vow} from '../vow.sol';
import {Vow, VowProxy} from '../vow.sol';
import {Pot} from '../pot.sol';
import {Flipper} from '../flip.sol';
import {Clipper} from '../clip.sol';
Expand Down Expand Up @@ -208,7 +208,11 @@ contract EndTest is DSTest {
flop = new Flopper(address(vat), address(gov));
gov.setOwner(address(flop));

vow = new Vow(address(vat), address(flap), address(flop));
vow = Vow(address(new VowProxy()));
address vowImp = address(new Vow(address(vat)));
VowProxy(address(vow)).setImplementation(vowImp);
vow.file("flapper", address(flap));
vow.file("flopper", address(flop));

pot = new Pot(address(vat));
vat.rely(address(pot));
Expand All @@ -217,12 +221,12 @@ contract EndTest is DSTest {
cat = new Cat(address(vat));
cat.file("vow", address(vow));
vat.rely(address(cat));
vow.rely(address(cat));
VowProxy(address(vow)).rely(address(cat));

dog = new Dog(address(vat));
dog.file("vow", address(vow));
vat.rely(address(dog));
vow.rely(address(dog));
VowProxy(address(vow)).rely(address(dog));

spot = new Spotter(address(vat));
vat.file("Line", rad(1_000_000 ether));
Expand All @@ -237,7 +241,7 @@ contract EndTest is DSTest {
end.file("spot", address(spot));
end.file("wait", 1 hours);
vat.rely(address(end));
vow.rely(address(end));
VowProxy(address(vow)).rely(address(end));
spot.rely(address(end));
pot.rely(address(end));
cat.rely(address(end));
Expand Down
14 changes: 9 additions & 5 deletions src/test/vat.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import "ds-token/token.sol";

import {Vat} from '../vat.sol';
import {Cat} from '../cat.sol';
import {Vow} from '../vow.sol';
import {Vow, VowProxy} from '../vow.sol';
import {Jug} from '../jug.sol';
import {GemJoin, DaiJoin} from '../join.sol';

Expand All @@ -30,8 +30,8 @@ contract TestVat is Vat {
}

contract TestVow is Vow {
constructor(address vat, address flapper, address flopper)
public Vow(vat, flapper, flopper) {}
constructor(address vat)
public Vow(vat) {}
// Total deficit
function Awe() public view returns (uint) {
return vat.sin(address(this));
Expand Down Expand Up @@ -484,7 +484,11 @@ contract BiteTest is DSTest {
flap = new Flapper(address(vat), address(gov));
flop = new Flopper(address(vat), address(gov));

vow = new TestVow(address(vat), address(flap), address(flop));
vow = TestVow(address(new VowProxy()));
address vowImp = address(new TestVow(address(vat)));
VowProxy(address(vow)).setImplementation(vowImp);
vow.file("flapper", address(flap));
vow.file("flopper", address(flop));
flap.rely(address(vow));
flop.rely(address(vow));

Expand All @@ -497,7 +501,7 @@ contract BiteTest is DSTest {
cat.file("vow", address(vow));
cat.file("box", rad((10 ether) * MLN));
vat.rely(address(cat));
vow.rely(address(cat));
VowProxy(address(vow)).rely(address(cat));

gold = new DSToken("GEM");
gold.mint(1000 ether);
Expand Down
8 changes: 6 additions & 2 deletions src/test/vow.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import "ds-test/test.sol";
import {Flopper as Flop} from './flop.t.sol';
import {Flapper as Flap} from './flap.t.sol';
import {TestVat as Vat} from './vat.t.sol';
import {Vow} from '../vow.sol';
import {Vow, VowProxy} from '../vow.sol';

interface Hevm {
function warp(uint256) external;
Expand Down Expand Up @@ -39,7 +39,11 @@ contract VowTest is DSTest {
flop = new Flop(address(vat), address(gov));
flap = new Flap(address(vat), address(gov));

vow = new Vow(address(vat), address(flap), address(flop));
vow = Vow(address(new VowProxy()));
address vowImp = address(new Vow(address(vat)));
VowProxy(address(vow)).setImplementation(vowImp);
vow.file("flapper", address(flap));
vow.file("flopper", address(flop));
flap.rely(address(vow));
flop.rely(address(vow));

Expand Down
89 changes: 69 additions & 20 deletions src/vow.sol
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,67 @@ interface VatLike {
function nope(address) external;
}

contract Vow {
// --- Auth ---
mapping (address => uint) public wards;
function rely(address usr) external auth { require(live == 1, "Vow/not-live"); wards[usr] = 1; }
function deny(address usr) external auth { wards[usr] = 0; }
contract VowProxy {
address public implementation;
uint256 public live; // Active Flag
mapping (address => uint256) public wards;

event Rely(address indexed usr);
event Deny(address indexed usr);
event SetImplementation(address indexed);

constructor() public {
wards[msg.sender] = 1;
emit Rely(msg.sender);
live = 1;
}

function rely(address usr) external auth {
require(live == 1, "Vow/not-live");
wards[usr] = 1;
emit Rely(usr);
}

function deny(address usr) external auth {
wards[usr] = 0;
emit Deny(usr);
}

modifier auth {
require(wards[msg.sender] == 1, "Vow/not-authorized");
_;
}

// --- Data ---
VatLike public vat; // CDP Engine
FlapLike public flapper; // Surplus Auction House
FlopLike public flopper; // Debt Auction House
function setImplementation(address implementation_) external auth {
implementation = implementation_;
emit SetImplementation(implementation_);
}

fallback() external {
address _impl = implementation;
require(_impl != address(0));

assembly {
let ptr := mload(0x40)
calldatacopy(ptr, 0, calldatasize())
let result := delegatecall(gas(), _impl, ptr, calldatasize(), 0, 0)
let size := returndatasize()
returndatacopy(ptr, 0, size)

switch result
case 0 { revert(ptr, size) }
default { return(ptr, size) }
}
}
}

contract Vow {
// --- Proxy Storage ---
bytes32 slot0; // avoid collision with proxy's implementation field
uint256 public live; // Active Flag
mapping (address => uint256) public wards;

// --- Implementation Storage ---
mapping (uint256 => uint256) public sin; // debt queue
uint256 public Sin; // Queued debt [rad]
uint256 public Ash; // On-auction debt [rad]
Expand All @@ -69,16 +115,19 @@ contract Vow {
uint256 public bump; // Flap fixed lot size [rad]
uint256 public hump; // Surplus buffer [rad]

uint256 public live; // Active Flag
VatLike public immutable vat; // CDP Engine
FlapLike public flapper; // Surplus Auction House
FlopLike public flopper; // Debt Auction House

// --- Auth ---
modifier auth {
require(wards[msg.sender] == 1, "Vow/not-authorized");
_;
}

// --- Init ---
constructor(address vat_, address flapper_, address flopper_) public {
wards[msg.sender] = 1;
vat = VatLike(vat_);
flapper = FlapLike(flapper_);
flopper = FlopLike(flopper_);
vat.hope(flapper_);
live = 1;
constructor(address vat_) public {
vat = VatLike(vat_);
}

// --- Math ---
Expand All @@ -104,7 +153,7 @@ contract Vow {

function file(bytes32 what, address data) external auth {
if (what == "flapper") {
vat.nope(address(flapper));
if (address(flapper) != address(0)) vat.nope(address(flapper));
flapper = FlapLike(data);
vat.hope(data);
}
Expand All @@ -114,12 +163,12 @@ contract Vow {

// Push to debt-queue
function fess(uint tab) external auth {
sin[now] = add(sin[now], tab);
sin[block.timestamp] = add(sin[block.timestamp], tab);
Sin = add(Sin, tab);
}
// Pop from debt-queue
function flog(uint era) external {
require(add(era, wait) <= now, "Vow/wait-not-finished");
require(add(era, wait) <= block.timestamp, "Vow/wait-not-finished");
Sin = sub(Sin, sin[era]);
sin[era] = 0;
}
Expand Down