File size: 5,069 Bytes
8072076
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// SPDX-License-Identifier: MIT
pragma solidity 0.8.26;

import {Constitution} from "./Constitution.sol";

/// @title ProposalRegistry - the lifecycle ledger of the Ethan protocol (AEP-1)
/// @notice Anyone may submit a proposal that conforms to the Constitution.
///         One proposal is active at a time. After the timelock it becomes
///         executable (by the Router only) for a fixed window, then expires.
///         The guardian may only veto pending proposals, and can be renounced.
contract ProposalRegistry {
    enum Status {
        None,
        Pending,
        Executed,
        Vetoed
    }

    struct Proposal {
        address proposer;
        uint8 action;
        uint256 amount;
        uint64 eta;
        uint64 expiry;
        Status status;
    }

    Constitution public immutable CONSTITUTION;

    address public guardian;
    address public router;
    address private immutable _deployer;

    uint256 public proposalCount;
    uint256 public activeProposalId;

    event ProposalSubmitted(

        uint256 indexed id, address indexed proposer, uint8 action, uint256 amount, uint64 eta, uint64 expiry

    );
    event ProposalVetoed(uint256 indexed id, address indexed guardian);
    event ProposalExecuted(uint256 indexed id);
    event GuardianRenounced(address indexed formerGuardian);
    event RouterSet(address indexed router);

    error NotGuardian();
    error NotRouter();
    error NotDeployer();
    error RouterAlreadySet();
    error ZeroAddress();
    error InvalidAction();
    error InvalidAmount();
    error ProposalStillActive();
    error NotPending();
    error TimelockNotElapsed();
    error ProposalExpired();

    mapping(uint256 => Proposal) internal _proposals;

    constructor(Constitution constitution, address guardian_) {
        if (address(constitution) == address(0)) revert ZeroAddress();
        CONSTITUTION = constitution;
        guardian = guardian_;
        _deployer = msg.sender;
    }

    /// @notice One-time wiring of the Router, callable only by the deployer.
    function setRouter(address router_) external {
        if (msg.sender != _deployer) revert NotDeployer();
        if (router != address(0)) revert RouterAlreadySet();
        if (router_ == address(0)) revert ZeroAddress();
        router = router_;
        emit RouterSet(router_);
    }

    /// @notice Submit a proposal. Anyone may call. Constitution decides validity.
    function submit(uint8 action, uint256 amount) external returns (uint256 id) {
        if (!CONSTITUTION.isValidAction(action)) revert InvalidAction();
        if (amount == 0 || amount > CONSTITUTION.ABSOLUTE_CAP()) revert InvalidAmount();

        uint256 activeId = activeProposalId;
        if (activeId != 0) {
            Proposal storage active = _proposals[activeId];
            if (active.status == Status.Pending && block.timestamp < active.expiry) {
                revert ProposalStillActive();
            }
        }

        uint64 eta = uint64(block.timestamp + CONSTITUTION.TIMELOCK());
        uint64 expiry = uint64(uint256(eta) + CONSTITUTION.EXECUTION_WINDOW());

        id = ++proposalCount;
        _proposals[id] = Proposal({
            proposer: msg.sender,
            action: action,
            amount: amount,
            eta: eta,
            expiry: expiry,
            status: Status.Pending
        });
        activeProposalId = id;

        emit ProposalSubmitted(id, msg.sender, action, amount, eta, expiry);
    }

    /// @notice Guardian cancels a pending proposal. Its only power.
    function veto(uint256 id) external {
        if (msg.sender != guardian) revert NotGuardian();
        Proposal storage p = _proposals[id];
        if (p.status != Status.Pending) revert NotPending();
        p.status = Status.Vetoed;
        emit ProposalVetoed(id, msg.sender);
    }

    /// @notice Guardian gives up the veto power forever.
    function renounceGuardian() external {
        if (msg.sender != guardian) revert NotGuardian();
        guardian = address(0);
        emit GuardianRenounced(msg.sender);
    }

    /// @notice Router-only: atomically checks eligibility and consumes the proposal.
    function markExecuted(uint256 id) external {
        if (msg.sender != router) revert NotRouter();
        Proposal storage p = _proposals[id];
        if (p.status != Status.Pending) revert NotPending();
        if (block.timestamp < p.eta) revert TimelockNotElapsed();
        if (block.timestamp >= p.expiry) revert ProposalExpired();
        p.status = Status.Executed;
        emit ProposalExecuted(id);
    }

    function getProposal(uint256 id) external view returns (Proposal memory) {
        return _proposals[id];
    }

    function isExecutable(uint256 id) external view returns (bool) {
        Proposal storage p = _proposals[id];
        return p.status == Status.Pending && block.timestamp >= p.eta && block.timestamp < p.expiry;
    }
}