TimelockController.spec 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. import "helpers/helpers.spec";
  2. import "methods/IAccessControl.spec";
  3. methods {
  4. function PROPOSER_ROLE() external returns (bytes32) envfree;
  5. function EXECUTOR_ROLE() external returns (bytes32) envfree;
  6. function CANCELLER_ROLE() external returns (bytes32) envfree;
  7. function isOperation(bytes32) external returns (bool);
  8. function isOperationPending(bytes32) external returns (bool);
  9. function isOperationReady(bytes32) external returns (bool);
  10. function isOperationDone(bytes32) external returns (bool);
  11. function getTimestamp(bytes32) external returns (uint256) envfree;
  12. function getMinDelay() external returns (uint256) envfree;
  13. function hashOperation(address, uint256, bytes, bytes32, bytes32) external returns(bytes32) envfree;
  14. function hashOperationBatch(address[], uint256[], bytes[], bytes32, bytes32) external returns(bytes32) envfree;
  15. function schedule(address, uint256, bytes, bytes32, bytes32, uint256) external;
  16. function scheduleBatch(address[], uint256[], bytes[], bytes32, bytes32, uint256) external;
  17. function execute(address, uint256, bytes, bytes32, bytes32) external;
  18. function executeBatch(address[], uint256[], bytes[], bytes32, bytes32) external;
  19. function cancel(bytes32) external;
  20. function updateDelay(uint256) external;
  21. }
  22. /*
  23. ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
  24. │ Helpers │
  25. └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
  26. */
  27. // Uniformly handle scheduling of batched and non-batched operations.
  28. function helperScheduleWithRevert(env e, method f, bytes32 id, uint256 delay) returns bool {
  29. if (f.selector == sig:schedule(address, uint256, bytes, bytes32, bytes32, uint256).selector) {
  30. address target; uint256 value; bytes data; bytes32 predecessor; bytes32 salt;
  31. require hashOperation(target, value, data, predecessor, salt) == id; // Correlation
  32. schedule@withrevert(e, target, value, data, predecessor, salt, delay);
  33. } else if (f.selector == sig:scheduleBatch(address[], uint256[], bytes[], bytes32, bytes32, uint256).selector) {
  34. // NOTE: while the "single" correlation requirement works, the prover is not able to deal with the the "batch"
  35. // correlation requirement. This requirement is necessary to ensure that the call arguments correspond to the
  36. // operation ID that we are observing. This failure, from the prover, to "identify" a set of arguments that
  37. // correspond to the operation ID causes vacuity.
  38. //
  39. // Therefore, this path should not be used for now. Using it will cause the sanity check to fail.
  40. address[] targets; uint256[] values; bytes[] payloads; bytes32 predecessor; bytes32 salt;
  41. require hashOperationBatch(targets, values, payloads, predecessor, salt) == id; // Correlation
  42. scheduleBatch@withrevert(e, targets, values, payloads, predecessor, salt, delay);
  43. } else {
  44. calldataarg args;
  45. f@withrevert(e, args);
  46. }
  47. return !lastReverted;
  48. }
  49. // Uniformly handle execution of batched and non-batched operations.
  50. function helperExecuteWithRevert(env e, method f, bytes32 id, bytes32 predecessor) returns bool {
  51. if (f.selector == sig:execute(address, uint256, bytes, bytes32, bytes32).selector) {
  52. address target; uint256 value; bytes data; bytes32 salt;
  53. require hashOperation(target, value, data, predecessor, salt) == id; // Correlation
  54. execute@withrevert(e, target, value, data, predecessor, salt);
  55. } else if (f.selector == sig:executeBatch(address[], uint256[], bytes[], bytes32, bytes32).selector) {
  56. // NOTE: while the "single" correlation requirement works, the prover is not able to deal with the the "batch"
  57. // correlation requirement. This requirement is necessary to ensure that the call arguments correspond to the
  58. // operation ID that we are observing. This failure, from the prover, to "identify" a set of arguments that
  59. // correspond to the operation ID causes vacuity.
  60. //
  61. // Therefore, this path should not be used for now. Using it will cause the sanity check to fail.
  62. address[] targets; uint256[] values; bytes[] payloads; bytes32 salt;
  63. require hashOperationBatch(targets, values, payloads, predecessor, salt) == id; // Correlation
  64. executeBatch@withrevert(e, targets, values, payloads, predecessor, salt);
  65. } else {
  66. calldataarg args;
  67. f@withrevert(e, args);
  68. }
  69. return !lastReverted;
  70. }
  71. /*
  72. ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
  73. │ Definitions │
  74. └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
  75. */
  76. definition DONE_TIMESTAMP() returns uint256 = 1;
  77. definition UNSET() returns uint8 = 0x1;
  78. definition PENDING() returns uint8 = 0x2;
  79. definition DONE() returns uint8 = 0x4;
  80. definition isUnset(env e, bytes32 id) returns bool = !isOperation(e, id);
  81. definition isPending(env e, bytes32 id) returns bool = isOperationPending(e, id);
  82. definition isDone(env e, bytes32 id) returns bool = isOperationDone(e, id);
  83. definition state(env e, bytes32 id) returns uint8 = (isUnset(e, id) ? UNSET() : 0) | (isPending(e, id) ? PENDING() : 0) | (isDone(e, id) ? DONE() : 0);
  84. /*
  85. ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
  86. │ Invariants: consistency of accessors │
  87. └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
  88. */
  89. rule isOperationCheck(env e, bytes32 id) {
  90. assert isOperation(e, id) <=> getTimestamp(id) > 0;
  91. }
  92. rule isOperationPendingCheck(env e, bytes32 id) {
  93. assert isOperationPending(e, id) <=> getTimestamp(id) > DONE_TIMESTAMP();
  94. }
  95. rule isOperationDoneCheck(env e, bytes32 id) {
  96. assert isOperationDone(e, id) <=> getTimestamp(id) == DONE_TIMESTAMP();
  97. }
  98. rule isOperationReadyCheck(env e, bytes32 id) {
  99. assert isOperationReady(e, id) <=> (isOperationPending(e, id) && getTimestamp(id) <= e.block.timestamp);
  100. }
  101. /*
  102. ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
  103. │ Invariant: a proposal id is either unset, pending or done │
  104. └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
  105. */
  106. rule stateConsistency(env e, bytes32 id) {
  107. // Check states are mutually exclusive
  108. assert isUnset(e, id) <=> (!isPending(e, id) && !isDone(e, id) );
  109. assert isPending(e, id) <=> (!isUnset(e, id) && !isDone(e, id) );
  110. assert isDone(e, id) <=> (!isUnset(e, id) && !isPending(e, id));
  111. // Check that the state helper behaves as expected:
  112. assert isUnset(e, id) <=> state(e, id) == UNSET();
  113. assert isPending(e, id) <=> state(e, id) == PENDING();
  114. assert isDone(e, id) <=> state(e, id) == DONE();
  115. // Check substate
  116. assert isOperationReady(e, id) => isPending(e, id);
  117. }
  118. /*
  119. ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
  120. │ Rule: state transition rules │
  121. └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
  122. */
  123. rule stateTransition(bytes32 id, env e, method f, calldataarg args) filtered { f ->
  124. f.selector != sig:hashOperationBatch(address[], uint256[], bytes[], bytes32, bytes32).selector &&
  125. f.selector != sig:scheduleBatch(address[], uint256[], bytes[], bytes32, bytes32, uint256).selector &&
  126. f.selector != sig:executeBatch(address[], uint256[], bytes[], bytes32, bytes32).selector
  127. } {
  128. require e.block.timestamp > 1; // Sanity
  129. uint8 stateBefore = state(e, id);
  130. f(e, args);
  131. uint8 stateAfter = state(e, id);
  132. // Cannot jump from UNSET to DONE
  133. assert stateBefore == UNSET() => stateAfter != DONE();
  134. // UNSET → PENDING: schedule or scheduleBatch
  135. assert stateBefore == UNSET() && stateAfter == PENDING() => (
  136. f.selector == sig:schedule(address, uint256, bytes, bytes32, bytes32, uint256).selector ||
  137. f.selector == sig:scheduleBatch(address[], uint256[], bytes[], bytes32, bytes32, uint256).selector
  138. );
  139. // PENDING → UNSET: cancel
  140. assert stateBefore == PENDING() && stateAfter == UNSET() => (
  141. f.selector == sig:cancel(bytes32).selector
  142. );
  143. // PENDING → DONE: execute or executeBatch
  144. assert stateBefore == PENDING() && stateAfter == DONE() => (
  145. f.selector == sig:execute(address, uint256, bytes, bytes32, bytes32).selector ||
  146. f.selector == sig:executeBatch(address[], uint256[], bytes[], bytes32, bytes32).selector
  147. );
  148. // DONE is final
  149. assert stateBefore == DONE() => stateAfter == DONE();
  150. }
  151. /*
  152. ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
  153. │ Rule: minimum delay can only be updated through a timelock execution │
  154. └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
  155. */
  156. rule minDelayOnlyChange(env e, method f, calldataarg args) filtered { f ->
  157. f.selector != sig:hashOperationBatch(address[], uint256[], bytes[], bytes32, bytes32).selector &&
  158. f.selector != sig:scheduleBatch(address[], uint256[], bytes[], bytes32, bytes32, uint256).selector &&
  159. f.selector != sig:executeBatch(address[], uint256[], bytes[], bytes32, bytes32).selector
  160. } {
  161. uint256 delayBefore = getMinDelay();
  162. f(e, args);
  163. assert delayBefore != getMinDelay() => (e.msg.sender == currentContract && f.selector == sig:updateDelay(uint256).selector), "Unauthorized delay update";
  164. }
  165. /*
  166. ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
  167. │ Rule: schedule liveness and effects │
  168. └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
  169. */
  170. rule schedule(env e, method f, bytes32 id, uint256 delay) filtered { f ->
  171. f.selector == sig:schedule(address, uint256, bytes, bytes32, bytes32, uint256).selector
  172. // || f.selector == sig:scheduleBatch(address[], uint256[], bytes[], bytes32, bytes32, uint256).selector
  173. } {
  174. require nonpayable(e);
  175. // Basic timestamp assumptions
  176. require e.block.timestamp > 1;
  177. require e.block.timestamp + delay < max_uint256;
  178. require e.block.timestamp + getMinDelay() < max_uint256;
  179. bytes32 otherId; uint256 otherTimestamp = getTimestamp(otherId);
  180. uint8 stateBefore = state(e, id);
  181. bool isDelaySufficient = delay >= getMinDelay();
  182. bool isProposerBefore = hasRole(PROPOSER_ROLE(), e.msg.sender);
  183. bool success = helperScheduleWithRevert(e, f, id, delay);
  184. // liveness
  185. assert success <=> (
  186. stateBefore == UNSET() &&
  187. isDelaySufficient &&
  188. isProposerBefore
  189. );
  190. // effect
  191. assert success => state(e, id) == PENDING(), "State transition violation";
  192. assert success => getTimestamp(id) == require_uint256(e.block.timestamp + delay), "Proposal timestamp not correctly set";
  193. // no side effect
  194. assert otherTimestamp != getTimestamp(otherId) => id == otherId, "Other proposal affected";
  195. }
  196. /*
  197. ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
  198. │ Rule: execute liveness and effects │
  199. └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
  200. */
  201. rule execute(env e, method f, bytes32 id, bytes32 predecessor) filtered { f ->
  202. f.selector == sig:execute(address, uint256, bytes, bytes32, bytes32).selector
  203. // || f.selector == sig:executeBatch(address[], uint256[], bytes[], bytes32, bytes32).selector
  204. } {
  205. bytes32 otherId; uint256 otherTimestamp = getTimestamp(otherId);
  206. uint8 stateBefore = state(e, id);
  207. bool isOperationReadyBefore = isOperationReady(e, id);
  208. bool isExecutorOrOpen = hasRole(EXECUTOR_ROLE(), e.msg.sender) || hasRole(EXECUTOR_ROLE(), 0);
  209. bool predecessorDependency = predecessor == to_bytes32(0) || isDone(e, predecessor);
  210. bool success = helperExecuteWithRevert(e, f, id, predecessor);
  211. // The underlying transaction can revert, and that would cause the execution to revert. We can check that all non
  212. // reverting calls meet the requirements in terms of proposal readiness, access control and predecessor dependency.
  213. // We can't however guarantee that these requirements being meet ensure liveness of the proposal, because the
  214. // proposal can revert for reasons beyond our control.
  215. // liveness, should be `<=>` but can only check `=>` (see comment above)
  216. assert success => (
  217. stateBefore == PENDING() &&
  218. isOperationReadyBefore &&
  219. predecessorDependency &&
  220. isExecutorOrOpen
  221. );
  222. // effect
  223. assert success => state(e, id) == DONE(), "State transition violation";
  224. // no side effect
  225. assert otherTimestamp != getTimestamp(otherId) => id == otherId, "Other proposal affected";
  226. }
  227. /*
  228. ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
  229. │ Rule: cancel liveness and effects │
  230. └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
  231. */
  232. rule cancel(env e, bytes32 id) {
  233. require nonpayable(e);
  234. bytes32 otherId; uint256 otherTimestamp = getTimestamp(otherId);
  235. uint8 stateBefore = state(e, id);
  236. bool isCanceller = hasRole(CANCELLER_ROLE(), e.msg.sender);
  237. cancel@withrevert(e, id);
  238. bool success = !lastReverted;
  239. // liveness
  240. assert success <=> (
  241. stateBefore == PENDING() &&
  242. isCanceller
  243. );
  244. // effect
  245. assert success => state(e, id) == UNSET(), "State transition violation";
  246. // no side effect
  247. assert otherTimestamp != getTimestamp(otherId) => id == otherId, "Other proposal affected";
  248. }