TimelockController.test.js 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285
  1. const { BN, constants, expectEvent, expectRevert, time } = require('@openzeppelin/test-helpers');
  2. const { ZERO_ADDRESS, ZERO_BYTES32 } = constants;
  3. const { expect } = require('chai');
  4. const { shouldSupportInterfaces } = require('../utils/introspection/SupportsInterface.behavior');
  5. const { expectRevertCustomError } = require('../helpers/customError');
  6. const { OperationState } = require('../helpers/enums');
  7. const TimelockController = artifacts.require('TimelockController');
  8. const CallReceiverMock = artifacts.require('CallReceiverMock');
  9. const Implementation2 = artifacts.require('Implementation2');
  10. const ERC721 = artifacts.require('$ERC721');
  11. const ERC1155 = artifacts.require('$ERC1155');
  12. const TimelockReentrant = artifacts.require('$TimelockReentrant');
  13. const MINDELAY = time.duration.days(1);
  14. const salt = '0x025e7b0be353a74631ad648c667493c0e1cd31caa4cc2d3520fdc171ea0cc726'; // a random value
  15. function genOperation(target, value, data, predecessor, salt) {
  16. const id = web3.utils.keccak256(
  17. web3.eth.abi.encodeParameters(
  18. ['address', 'uint256', 'bytes', 'uint256', 'bytes32'],
  19. [target, value, data, predecessor, salt],
  20. ),
  21. );
  22. return { id, target, value, data, predecessor, salt };
  23. }
  24. function genOperationBatch(targets, values, payloads, predecessor, salt) {
  25. const id = web3.utils.keccak256(
  26. web3.eth.abi.encodeParameters(
  27. ['address[]', 'uint256[]', 'bytes[]', 'uint256', 'bytes32'],
  28. [targets, values, payloads, predecessor, salt],
  29. ),
  30. );
  31. return { id, targets, values, payloads, predecessor, salt };
  32. }
  33. contract('TimelockController', function (accounts) {
  34. const [, admin, proposer, canceller, executor, other] = accounts;
  35. const DEFAULT_ADMIN_ROLE = '0x0000000000000000000000000000000000000000000000000000000000000000';
  36. const PROPOSER_ROLE = web3.utils.soliditySha3('PROPOSER_ROLE');
  37. const EXECUTOR_ROLE = web3.utils.soliditySha3('EXECUTOR_ROLE');
  38. const CANCELLER_ROLE = web3.utils.soliditySha3('CANCELLER_ROLE');
  39. beforeEach(async function () {
  40. // Deploy new timelock
  41. this.mock = await TimelockController.new(MINDELAY, [proposer], [executor], admin);
  42. expect(await this.mock.hasRole(CANCELLER_ROLE, proposer)).to.be.equal(true);
  43. await this.mock.revokeRole(CANCELLER_ROLE, proposer, { from: admin });
  44. await this.mock.grantRole(CANCELLER_ROLE, canceller, { from: admin });
  45. // Mocks
  46. this.callreceivermock = await CallReceiverMock.new({ from: admin });
  47. this.implementation2 = await Implementation2.new({ from: admin });
  48. });
  49. shouldSupportInterfaces(['ERC1155Receiver']);
  50. it('initial state', async function () {
  51. expect(await this.mock.getMinDelay()).to.be.bignumber.equal(MINDELAY);
  52. expect(await this.mock.DEFAULT_ADMIN_ROLE()).to.be.equal(DEFAULT_ADMIN_ROLE);
  53. expect(await this.mock.PROPOSER_ROLE()).to.be.equal(PROPOSER_ROLE);
  54. expect(await this.mock.EXECUTOR_ROLE()).to.be.equal(EXECUTOR_ROLE);
  55. expect(await this.mock.CANCELLER_ROLE()).to.be.equal(CANCELLER_ROLE);
  56. expect(
  57. await Promise.all([PROPOSER_ROLE, CANCELLER_ROLE, EXECUTOR_ROLE].map(role => this.mock.hasRole(role, proposer))),
  58. ).to.be.deep.equal([true, false, false]);
  59. expect(
  60. await Promise.all([PROPOSER_ROLE, CANCELLER_ROLE, EXECUTOR_ROLE].map(role => this.mock.hasRole(role, canceller))),
  61. ).to.be.deep.equal([false, true, false]);
  62. expect(
  63. await Promise.all([PROPOSER_ROLE, CANCELLER_ROLE, EXECUTOR_ROLE].map(role => this.mock.hasRole(role, executor))),
  64. ).to.be.deep.equal([false, false, true]);
  65. });
  66. it('optional admin', async function () {
  67. const mock = await TimelockController.new(MINDELAY, [proposer], [executor], ZERO_ADDRESS, { from: other });
  68. expect(await mock.hasRole(DEFAULT_ADMIN_ROLE, admin)).to.be.equal(false);
  69. expect(await mock.hasRole(DEFAULT_ADMIN_ROLE, mock.address)).to.be.equal(true);
  70. });
  71. describe('methods', function () {
  72. describe('operation hashing', function () {
  73. it('hashOperation', async function () {
  74. this.operation = genOperation(
  75. '0x29cebefe301c6ce1bb36b58654fea275e1cacc83',
  76. '0xf94fdd6e21da21d2',
  77. '0xa3bc5104',
  78. '0xba41db3be0a9929145cfe480bd0f1f003689104d275ae912099f925df424ef94',
  79. '0x60d9109846ab510ed75c15f979ae366a8a2ace11d34ba9788c13ac296db50e6e',
  80. );
  81. expect(
  82. await this.mock.hashOperation(
  83. this.operation.target,
  84. this.operation.value,
  85. this.operation.data,
  86. this.operation.predecessor,
  87. this.operation.salt,
  88. ),
  89. ).to.be.equal(this.operation.id);
  90. });
  91. it('hashOperationBatch', async function () {
  92. this.operation = genOperationBatch(
  93. Array(8).fill('0x2d5f21620e56531c1d59c2df9b8e95d129571f71'),
  94. Array(8).fill('0x2b993cfce932ccee'),
  95. Array(8).fill('0xcf51966b'),
  96. '0xce8f45069cc71d25f71ba05062de1a3974f9849b004de64a70998bca9d29c2e7',
  97. '0x8952d74c110f72bfe5accdf828c74d53a7dfb71235dfa8a1e8c75d8576b372ff',
  98. );
  99. expect(
  100. await this.mock.hashOperationBatch(
  101. this.operation.targets,
  102. this.operation.values,
  103. this.operation.payloads,
  104. this.operation.predecessor,
  105. this.operation.salt,
  106. ),
  107. ).to.be.equal(this.operation.id);
  108. });
  109. });
  110. describe('simple', function () {
  111. describe('schedule', function () {
  112. beforeEach(async function () {
  113. this.operation = genOperation(
  114. '0x31754f590B97fD975Eb86938f18Cc304E264D2F2',
  115. 0,
  116. '0x3bf92ccc',
  117. ZERO_BYTES32,
  118. salt,
  119. );
  120. });
  121. it('proposer can schedule', async function () {
  122. const receipt = await this.mock.schedule(
  123. this.operation.target,
  124. this.operation.value,
  125. this.operation.data,
  126. this.operation.predecessor,
  127. this.operation.salt,
  128. MINDELAY,
  129. { from: proposer },
  130. );
  131. expectEvent(receipt, 'CallScheduled', {
  132. id: this.operation.id,
  133. index: web3.utils.toBN(0),
  134. target: this.operation.target,
  135. value: web3.utils.toBN(this.operation.value),
  136. data: this.operation.data,
  137. predecessor: this.operation.predecessor,
  138. delay: MINDELAY,
  139. });
  140. expectEvent(receipt, 'CallSalt', {
  141. id: this.operation.id,
  142. salt: this.operation.salt,
  143. });
  144. const block = await web3.eth.getBlock(receipt.receipt.blockHash);
  145. expect(await this.mock.getTimestamp(this.operation.id)).to.be.bignumber.equal(
  146. web3.utils.toBN(block.timestamp).add(MINDELAY),
  147. );
  148. });
  149. it('prevent overwriting active operation', async function () {
  150. await this.mock.schedule(
  151. this.operation.target,
  152. this.operation.value,
  153. this.operation.data,
  154. this.operation.predecessor,
  155. this.operation.salt,
  156. MINDELAY,
  157. { from: proposer },
  158. );
  159. await expectRevertCustomError(
  160. this.mock.schedule(
  161. this.operation.target,
  162. this.operation.value,
  163. this.operation.data,
  164. this.operation.predecessor,
  165. this.operation.salt,
  166. MINDELAY,
  167. { from: proposer },
  168. ),
  169. 'TimelockUnexpectedOperationState',
  170. [this.operation.id, OperationState.Unset],
  171. );
  172. });
  173. it('prevent non-proposer from committing', async function () {
  174. await expectRevertCustomError(
  175. this.mock.schedule(
  176. this.operation.target,
  177. this.operation.value,
  178. this.operation.data,
  179. this.operation.predecessor,
  180. this.operation.salt,
  181. MINDELAY,
  182. { from: other },
  183. ),
  184. `AccessControlUnauthorizedAccount`,
  185. [other, PROPOSER_ROLE],
  186. );
  187. });
  188. it('enforce minimum delay', async function () {
  189. await expectRevertCustomError(
  190. this.mock.schedule(
  191. this.operation.target,
  192. this.operation.value,
  193. this.operation.data,
  194. this.operation.predecessor,
  195. this.operation.salt,
  196. MINDELAY - 1,
  197. { from: proposer },
  198. ),
  199. 'TimelockInsufficientDelay',
  200. [MINDELAY, MINDELAY - 1],
  201. );
  202. });
  203. it('schedule operation with salt zero', async function () {
  204. const { receipt } = await this.mock.schedule(
  205. this.operation.target,
  206. this.operation.value,
  207. this.operation.data,
  208. this.operation.predecessor,
  209. ZERO_BYTES32,
  210. MINDELAY,
  211. { from: proposer },
  212. );
  213. expectEvent.notEmitted(receipt, 'CallSalt');
  214. });
  215. });
  216. describe('execute', function () {
  217. beforeEach(async function () {
  218. this.operation = genOperation(
  219. '0xAe22104DCD970750610E6FE15E623468A98b15f7',
  220. 0,
  221. '0x13e414de',
  222. ZERO_BYTES32,
  223. '0xc1059ed2dc130227aa1d1d539ac94c641306905c020436c636e19e3fab56fc7f',
  224. );
  225. });
  226. it('revert if operation is not scheduled', async function () {
  227. await expectRevertCustomError(
  228. this.mock.execute(
  229. this.operation.target,
  230. this.operation.value,
  231. this.operation.data,
  232. this.operation.predecessor,
  233. this.operation.salt,
  234. { from: executor },
  235. ),
  236. 'TimelockUnexpectedOperationState',
  237. [this.operation.id, OperationState.Ready],
  238. );
  239. });
  240. describe('with scheduled operation', function () {
  241. beforeEach(async function () {
  242. ({ receipt: this.receipt, logs: this.logs } = await this.mock.schedule(
  243. this.operation.target,
  244. this.operation.value,
  245. this.operation.data,
  246. this.operation.predecessor,
  247. this.operation.salt,
  248. MINDELAY,
  249. { from: proposer },
  250. ));
  251. });
  252. it('revert if execution comes too early 1/2', async function () {
  253. await expectRevertCustomError(
  254. this.mock.execute(
  255. this.operation.target,
  256. this.operation.value,
  257. this.operation.data,
  258. this.operation.predecessor,
  259. this.operation.salt,
  260. { from: executor },
  261. ),
  262. 'TimelockUnexpectedOperationState',
  263. [this.operation.id, OperationState.Ready],
  264. );
  265. });
  266. it('revert if execution comes too early 2/2', async function () {
  267. const timestamp = await this.mock.getTimestamp(this.operation.id);
  268. await time.increaseTo(timestamp - 5); // -1 is too tight, test sometime fails
  269. await expectRevertCustomError(
  270. this.mock.execute(
  271. this.operation.target,
  272. this.operation.value,
  273. this.operation.data,
  274. this.operation.predecessor,
  275. this.operation.salt,
  276. { from: executor },
  277. ),
  278. 'TimelockUnexpectedOperationState',
  279. [this.operation.id, OperationState.Ready],
  280. );
  281. });
  282. describe('on time', function () {
  283. beforeEach(async function () {
  284. const timestamp = await this.mock.getTimestamp(this.operation.id);
  285. await time.increaseTo(timestamp);
  286. });
  287. it('executor can reveal', async function () {
  288. const receipt = await this.mock.execute(
  289. this.operation.target,
  290. this.operation.value,
  291. this.operation.data,
  292. this.operation.predecessor,
  293. this.operation.salt,
  294. { from: executor },
  295. );
  296. expectEvent(receipt, 'CallExecuted', {
  297. id: this.operation.id,
  298. index: web3.utils.toBN(0),
  299. target: this.operation.target,
  300. value: web3.utils.toBN(this.operation.value),
  301. data: this.operation.data,
  302. });
  303. });
  304. it('prevent non-executor from revealing', async function () {
  305. await expectRevertCustomError(
  306. this.mock.execute(
  307. this.operation.target,
  308. this.operation.value,
  309. this.operation.data,
  310. this.operation.predecessor,
  311. this.operation.salt,
  312. { from: other },
  313. ),
  314. `AccessControlUnauthorizedAccount`,
  315. [other, EXECUTOR_ROLE],
  316. );
  317. });
  318. it('prevents reentrancy execution', async function () {
  319. // Create operation
  320. const reentrant = await TimelockReentrant.new();
  321. const reentrantOperation = genOperation(
  322. reentrant.address,
  323. 0,
  324. reentrant.contract.methods.reenter().encodeABI(),
  325. ZERO_BYTES32,
  326. salt,
  327. );
  328. // Schedule so it can be executed
  329. await this.mock.schedule(
  330. reentrantOperation.target,
  331. reentrantOperation.value,
  332. reentrantOperation.data,
  333. reentrantOperation.predecessor,
  334. reentrantOperation.salt,
  335. MINDELAY,
  336. { from: proposer },
  337. );
  338. // Advance on time to make the operation executable
  339. const timestamp = await this.mock.getTimestamp(reentrantOperation.id);
  340. await time.increaseTo(timestamp);
  341. // Grant executor role to the reentrant contract
  342. await this.mock.grantRole(EXECUTOR_ROLE, reentrant.address, { from: admin });
  343. // Prepare reenter
  344. const data = this.mock.contract.methods
  345. .execute(
  346. reentrantOperation.target,
  347. reentrantOperation.value,
  348. reentrantOperation.data,
  349. reentrantOperation.predecessor,
  350. reentrantOperation.salt,
  351. )
  352. .encodeABI();
  353. await reentrant.enableRentrancy(this.mock.address, data);
  354. // Expect to fail
  355. await expectRevertCustomError(
  356. this.mock.execute(
  357. reentrantOperation.target,
  358. reentrantOperation.value,
  359. reentrantOperation.data,
  360. reentrantOperation.predecessor,
  361. reentrantOperation.salt,
  362. { from: executor },
  363. ),
  364. 'TimelockUnexpectedOperationState',
  365. [reentrantOperation.id, OperationState.Ready],
  366. );
  367. // Disable reentrancy
  368. await reentrant.disableReentrancy();
  369. const nonReentrantOperation = reentrantOperation; // Not anymore
  370. // Try again successfully
  371. const receipt = await this.mock.execute(
  372. nonReentrantOperation.target,
  373. nonReentrantOperation.value,
  374. nonReentrantOperation.data,
  375. nonReentrantOperation.predecessor,
  376. nonReentrantOperation.salt,
  377. { from: executor },
  378. );
  379. expectEvent(receipt, 'CallExecuted', {
  380. id: nonReentrantOperation.id,
  381. index: web3.utils.toBN(0),
  382. target: nonReentrantOperation.target,
  383. value: web3.utils.toBN(nonReentrantOperation.value),
  384. data: nonReentrantOperation.data,
  385. });
  386. });
  387. });
  388. });
  389. });
  390. });
  391. describe('batch', function () {
  392. describe('schedule', function () {
  393. beforeEach(async function () {
  394. this.operation = genOperationBatch(
  395. Array(8).fill('0xEd912250835c812D4516BBD80BdaEA1bB63a293C'),
  396. Array(8).fill(0),
  397. Array(8).fill('0x2fcb7a88'),
  398. ZERO_BYTES32,
  399. '0x6cf9d042ade5de78bed9ffd075eb4b2a4f6b1736932c2dc8af517d6e066f51f5',
  400. );
  401. });
  402. it('proposer can schedule', async function () {
  403. const receipt = await this.mock.scheduleBatch(
  404. this.operation.targets,
  405. this.operation.values,
  406. this.operation.payloads,
  407. this.operation.predecessor,
  408. this.operation.salt,
  409. MINDELAY,
  410. { from: proposer },
  411. );
  412. for (const i in this.operation.targets) {
  413. expectEvent(receipt, 'CallScheduled', {
  414. id: this.operation.id,
  415. index: web3.utils.toBN(i),
  416. target: this.operation.targets[i],
  417. value: web3.utils.toBN(this.operation.values[i]),
  418. data: this.operation.payloads[i],
  419. predecessor: this.operation.predecessor,
  420. delay: MINDELAY,
  421. });
  422. expectEvent(receipt, 'CallSalt', {
  423. id: this.operation.id,
  424. salt: this.operation.salt,
  425. });
  426. }
  427. const block = await web3.eth.getBlock(receipt.receipt.blockHash);
  428. expect(await this.mock.getTimestamp(this.operation.id)).to.be.bignumber.equal(
  429. web3.utils.toBN(block.timestamp).add(MINDELAY),
  430. );
  431. });
  432. it('prevent overwriting active operation', async function () {
  433. await this.mock.scheduleBatch(
  434. this.operation.targets,
  435. this.operation.values,
  436. this.operation.payloads,
  437. this.operation.predecessor,
  438. this.operation.salt,
  439. MINDELAY,
  440. { from: proposer },
  441. );
  442. await expectRevertCustomError(
  443. this.mock.scheduleBatch(
  444. this.operation.targets,
  445. this.operation.values,
  446. this.operation.payloads,
  447. this.operation.predecessor,
  448. this.operation.salt,
  449. MINDELAY,
  450. { from: proposer },
  451. ),
  452. 'TimelockUnexpectedOperationState',
  453. [this.operation.id, OperationState.Unset],
  454. );
  455. });
  456. it('length of batch parameter must match #1', async function () {
  457. await expectRevertCustomError(
  458. this.mock.scheduleBatch(
  459. this.operation.targets,
  460. [],
  461. this.operation.payloads,
  462. this.operation.predecessor,
  463. this.operation.salt,
  464. MINDELAY,
  465. { from: proposer },
  466. ),
  467. 'TimelockInvalidOperationLength',
  468. [this.operation.targets.length, this.operation.payloads.length, 0],
  469. );
  470. });
  471. it('length of batch parameter must match #1', async function () {
  472. await expectRevertCustomError(
  473. this.mock.scheduleBatch(
  474. this.operation.targets,
  475. this.operation.values,
  476. [],
  477. this.operation.predecessor,
  478. this.operation.salt,
  479. MINDELAY,
  480. { from: proposer },
  481. ),
  482. 'TimelockInvalidOperationLength',
  483. [this.operation.targets.length, 0, this.operation.payloads.length],
  484. );
  485. });
  486. it('prevent non-proposer from committing', async function () {
  487. await expectRevertCustomError(
  488. this.mock.scheduleBatch(
  489. this.operation.targets,
  490. this.operation.values,
  491. this.operation.payloads,
  492. this.operation.predecessor,
  493. this.operation.salt,
  494. MINDELAY,
  495. { from: other },
  496. ),
  497. `AccessControlUnauthorizedAccount`,
  498. [other, PROPOSER_ROLE],
  499. );
  500. });
  501. it('enforce minimum delay', async function () {
  502. await expectRevertCustomError(
  503. this.mock.scheduleBatch(
  504. this.operation.targets,
  505. this.operation.values,
  506. this.operation.payloads,
  507. this.operation.predecessor,
  508. this.operation.salt,
  509. MINDELAY - 1,
  510. { from: proposer },
  511. ),
  512. 'TimelockInsufficientDelay',
  513. [MINDELAY, MINDELAY - 1],
  514. );
  515. });
  516. });
  517. describe('execute', function () {
  518. beforeEach(async function () {
  519. this.operation = genOperationBatch(
  520. Array(8).fill('0x76E53CcEb05131Ef5248553bEBDb8F70536830b1'),
  521. Array(8).fill(0),
  522. Array(8).fill('0x58a60f63'),
  523. ZERO_BYTES32,
  524. '0x9545eeabc7a7586689191f78a5532443698538e54211b5bd4d7dc0fc0102b5c7',
  525. );
  526. });
  527. it('revert if operation is not scheduled', async function () {
  528. await expectRevertCustomError(
  529. this.mock.executeBatch(
  530. this.operation.targets,
  531. this.operation.values,
  532. this.operation.payloads,
  533. this.operation.predecessor,
  534. this.operation.salt,
  535. { from: executor },
  536. ),
  537. 'TimelockUnexpectedOperationState',
  538. [this.operation.id, OperationState.Ready],
  539. );
  540. });
  541. describe('with scheduled operation', function () {
  542. beforeEach(async function () {
  543. ({ receipt: this.receipt, logs: this.logs } = await this.mock.scheduleBatch(
  544. this.operation.targets,
  545. this.operation.values,
  546. this.operation.payloads,
  547. this.operation.predecessor,
  548. this.operation.salt,
  549. MINDELAY,
  550. { from: proposer },
  551. ));
  552. });
  553. it('revert if execution comes too early 1/2', async function () {
  554. await expectRevertCustomError(
  555. this.mock.executeBatch(
  556. this.operation.targets,
  557. this.operation.values,
  558. this.operation.payloads,
  559. this.operation.predecessor,
  560. this.operation.salt,
  561. { from: executor },
  562. ),
  563. 'TimelockUnexpectedOperationState',
  564. [this.operation.id, OperationState.Ready],
  565. );
  566. });
  567. it('revert if execution comes too early 2/2', async function () {
  568. const timestamp = await this.mock.getTimestamp(this.operation.id);
  569. await time.increaseTo(timestamp - 5); // -1 is to tight, test sometime fails
  570. await expectRevertCustomError(
  571. this.mock.executeBatch(
  572. this.operation.targets,
  573. this.operation.values,
  574. this.operation.payloads,
  575. this.operation.predecessor,
  576. this.operation.salt,
  577. { from: executor },
  578. ),
  579. 'TimelockUnexpectedOperationState',
  580. [this.operation.id, OperationState.Ready],
  581. );
  582. });
  583. describe('on time', function () {
  584. beforeEach(async function () {
  585. const timestamp = await this.mock.getTimestamp(this.operation.id);
  586. await time.increaseTo(timestamp);
  587. });
  588. it('executor can reveal', async function () {
  589. const receipt = await this.mock.executeBatch(
  590. this.operation.targets,
  591. this.operation.values,
  592. this.operation.payloads,
  593. this.operation.predecessor,
  594. this.operation.salt,
  595. { from: executor },
  596. );
  597. for (const i in this.operation.targets) {
  598. expectEvent(receipt, 'CallExecuted', {
  599. id: this.operation.id,
  600. index: web3.utils.toBN(i),
  601. target: this.operation.targets[i],
  602. value: web3.utils.toBN(this.operation.values[i]),
  603. data: this.operation.payloads[i],
  604. });
  605. }
  606. });
  607. it('prevent non-executor from revealing', async function () {
  608. await expectRevertCustomError(
  609. this.mock.executeBatch(
  610. this.operation.targets,
  611. this.operation.values,
  612. this.operation.payloads,
  613. this.operation.predecessor,
  614. this.operation.salt,
  615. { from: other },
  616. ),
  617. `AccessControlUnauthorizedAccount`,
  618. [other, EXECUTOR_ROLE],
  619. );
  620. });
  621. it('length mismatch #1', async function () {
  622. await expectRevertCustomError(
  623. this.mock.executeBatch(
  624. [],
  625. this.operation.values,
  626. this.operation.payloads,
  627. this.operation.predecessor,
  628. this.operation.salt,
  629. { from: executor },
  630. ),
  631. 'TimelockInvalidOperationLength',
  632. [0, this.operation.payloads.length, this.operation.values.length],
  633. );
  634. });
  635. it('length mismatch #2', async function () {
  636. await expectRevertCustomError(
  637. this.mock.executeBatch(
  638. this.operation.targets,
  639. [],
  640. this.operation.payloads,
  641. this.operation.predecessor,
  642. this.operation.salt,
  643. { from: executor },
  644. ),
  645. 'TimelockInvalidOperationLength',
  646. [this.operation.targets.length, this.operation.payloads.length, 0],
  647. );
  648. });
  649. it('length mismatch #3', async function () {
  650. await expectRevertCustomError(
  651. this.mock.executeBatch(
  652. this.operation.targets,
  653. this.operation.values,
  654. [],
  655. this.operation.predecessor,
  656. this.operation.salt,
  657. { from: executor },
  658. ),
  659. 'TimelockInvalidOperationLength',
  660. [this.operation.targets.length, 0, this.operation.values.length],
  661. );
  662. });
  663. it('prevents reentrancy execution', async function () {
  664. // Create operation
  665. const reentrant = await TimelockReentrant.new();
  666. const reentrantBatchOperation = genOperationBatch(
  667. [reentrant.address],
  668. [0],
  669. [reentrant.contract.methods.reenter().encodeABI()],
  670. ZERO_BYTES32,
  671. salt,
  672. );
  673. // Schedule so it can be executed
  674. await this.mock.scheduleBatch(
  675. reentrantBatchOperation.targets,
  676. reentrantBatchOperation.values,
  677. reentrantBatchOperation.payloads,
  678. reentrantBatchOperation.predecessor,
  679. reentrantBatchOperation.salt,
  680. MINDELAY,
  681. { from: proposer },
  682. );
  683. // Advance on time to make the operation executable
  684. const timestamp = await this.mock.getTimestamp(reentrantBatchOperation.id);
  685. await time.increaseTo(timestamp);
  686. // Grant executor role to the reentrant contract
  687. await this.mock.grantRole(EXECUTOR_ROLE, reentrant.address, { from: admin });
  688. // Prepare reenter
  689. const data = this.mock.contract.methods
  690. .executeBatch(
  691. reentrantBatchOperation.targets,
  692. reentrantBatchOperation.values,
  693. reentrantBatchOperation.payloads,
  694. reentrantBatchOperation.predecessor,
  695. reentrantBatchOperation.salt,
  696. )
  697. .encodeABI();
  698. await reentrant.enableRentrancy(this.mock.address, data);
  699. // Expect to fail
  700. await expectRevertCustomError(
  701. this.mock.executeBatch(
  702. reentrantBatchOperation.targets,
  703. reentrantBatchOperation.values,
  704. reentrantBatchOperation.payloads,
  705. reentrantBatchOperation.predecessor,
  706. reentrantBatchOperation.salt,
  707. { from: executor },
  708. ),
  709. 'TimelockUnexpectedOperationState',
  710. [reentrantBatchOperation.id, OperationState.Ready],
  711. );
  712. // Disable reentrancy
  713. await reentrant.disableReentrancy();
  714. const nonReentrantBatchOperation = reentrantBatchOperation; // Not anymore
  715. // Try again successfully
  716. const receipt = await this.mock.executeBatch(
  717. nonReentrantBatchOperation.targets,
  718. nonReentrantBatchOperation.values,
  719. nonReentrantBatchOperation.payloads,
  720. nonReentrantBatchOperation.predecessor,
  721. nonReentrantBatchOperation.salt,
  722. { from: executor },
  723. );
  724. for (const i in nonReentrantBatchOperation.targets) {
  725. expectEvent(receipt, 'CallExecuted', {
  726. id: nonReentrantBatchOperation.id,
  727. index: web3.utils.toBN(i),
  728. target: nonReentrantBatchOperation.targets[i],
  729. value: web3.utils.toBN(nonReentrantBatchOperation.values[i]),
  730. data: nonReentrantBatchOperation.payloads[i],
  731. });
  732. }
  733. });
  734. });
  735. });
  736. it('partial execution', async function () {
  737. const operation = genOperationBatch(
  738. [this.callreceivermock.address, this.callreceivermock.address, this.callreceivermock.address],
  739. [0, 0, 0],
  740. [
  741. this.callreceivermock.contract.methods.mockFunction().encodeABI(),
  742. this.callreceivermock.contract.methods.mockFunctionRevertsNoReason().encodeABI(),
  743. this.callreceivermock.contract.methods.mockFunction().encodeABI(),
  744. ],
  745. ZERO_BYTES32,
  746. '0x8ac04aa0d6d66b8812fb41d39638d37af0a9ab11da507afd65c509f8ed079d3e',
  747. );
  748. await this.mock.scheduleBatch(
  749. operation.targets,
  750. operation.values,
  751. operation.payloads,
  752. operation.predecessor,
  753. operation.salt,
  754. MINDELAY,
  755. { from: proposer },
  756. );
  757. await time.increase(MINDELAY);
  758. await expectRevertCustomError(
  759. this.mock.executeBatch(
  760. operation.targets,
  761. operation.values,
  762. operation.payloads,
  763. operation.predecessor,
  764. operation.salt,
  765. { from: executor },
  766. ),
  767. 'FailedInnerCall',
  768. [],
  769. );
  770. });
  771. });
  772. });
  773. describe('cancel', function () {
  774. beforeEach(async function () {
  775. this.operation = genOperation(
  776. '0xC6837c44AA376dbe1d2709F13879E040CAb653ca',
  777. 0,
  778. '0x296e58dd',
  779. ZERO_BYTES32,
  780. '0xa2485763600634800df9fc9646fb2c112cf98649c55f63dd1d9c7d13a64399d9',
  781. );
  782. ({ receipt: this.receipt, logs: this.logs } = await this.mock.schedule(
  783. this.operation.target,
  784. this.operation.value,
  785. this.operation.data,
  786. this.operation.predecessor,
  787. this.operation.salt,
  788. MINDELAY,
  789. { from: proposer },
  790. ));
  791. });
  792. it('canceller can cancel', async function () {
  793. const receipt = await this.mock.cancel(this.operation.id, { from: canceller });
  794. expectEvent(receipt, 'Cancelled', { id: this.operation.id });
  795. });
  796. it('cannot cancel invalid operation', async function () {
  797. await expectRevertCustomError(
  798. this.mock.cancel(constants.ZERO_BYTES32, { from: canceller }),
  799. 'TimelockUnexpectedOperationState',
  800. [constants.ZERO_BYTES32, OperationState.Pending],
  801. );
  802. });
  803. it('prevent non-canceller from canceling', async function () {
  804. await expectRevertCustomError(
  805. this.mock.cancel(this.operation.id, { from: other }),
  806. `AccessControlUnauthorizedAccount`,
  807. [other, CANCELLER_ROLE],
  808. );
  809. });
  810. });
  811. });
  812. describe('maintenance', function () {
  813. it('prevent unauthorized maintenance', async function () {
  814. await expectRevertCustomError(this.mock.updateDelay(0, { from: other }), 'TimelockUnauthorizedCaller', [other]);
  815. });
  816. it('timelock scheduled maintenance', async function () {
  817. const newDelay = time.duration.hours(6);
  818. const operation = genOperation(
  819. this.mock.address,
  820. 0,
  821. this.mock.contract.methods.updateDelay(newDelay.toString()).encodeABI(),
  822. ZERO_BYTES32,
  823. '0xf8e775b2c5f4d66fb5c7fa800f35ef518c262b6014b3c0aee6ea21bff157f108',
  824. );
  825. await this.mock.schedule(
  826. operation.target,
  827. operation.value,
  828. operation.data,
  829. operation.predecessor,
  830. operation.salt,
  831. MINDELAY,
  832. { from: proposer },
  833. );
  834. await time.increase(MINDELAY);
  835. const receipt = await this.mock.execute(
  836. operation.target,
  837. operation.value,
  838. operation.data,
  839. operation.predecessor,
  840. operation.salt,
  841. { from: executor },
  842. );
  843. expectEvent(receipt, 'MinDelayChange', { newDuration: newDelay.toString(), oldDuration: MINDELAY });
  844. expect(await this.mock.getMinDelay()).to.be.bignumber.equal(newDelay);
  845. });
  846. });
  847. describe('dependency', function () {
  848. beforeEach(async function () {
  849. this.operation1 = genOperation(
  850. '0xdE66bD4c97304200A95aE0AadA32d6d01A867E39',
  851. 0,
  852. '0x01dc731a',
  853. ZERO_BYTES32,
  854. '0x64e932133c7677402ead2926f86205e2ca4686aebecf5a8077627092b9bb2feb',
  855. );
  856. this.operation2 = genOperation(
  857. '0x3c7944a3F1ee7fc8c5A5134ba7c79D11c3A1FCa3',
  858. 0,
  859. '0x8f531849',
  860. this.operation1.id,
  861. '0x036e1311cac523f9548e6461e29fb1f8f9196b91910a41711ea22f5de48df07d',
  862. );
  863. await this.mock.schedule(
  864. this.operation1.target,
  865. this.operation1.value,
  866. this.operation1.data,
  867. this.operation1.predecessor,
  868. this.operation1.salt,
  869. MINDELAY,
  870. { from: proposer },
  871. );
  872. await this.mock.schedule(
  873. this.operation2.target,
  874. this.operation2.value,
  875. this.operation2.data,
  876. this.operation2.predecessor,
  877. this.operation2.salt,
  878. MINDELAY,
  879. { from: proposer },
  880. );
  881. await time.increase(MINDELAY);
  882. });
  883. it('cannot execute before dependency', async function () {
  884. await expectRevertCustomError(
  885. this.mock.execute(
  886. this.operation2.target,
  887. this.operation2.value,
  888. this.operation2.data,
  889. this.operation2.predecessor,
  890. this.operation2.salt,
  891. { from: executor },
  892. ),
  893. 'TimelockUnexecutedPredecessor',
  894. [this.operation1.id],
  895. );
  896. });
  897. it('can execute after dependency', async function () {
  898. await this.mock.execute(
  899. this.operation1.target,
  900. this.operation1.value,
  901. this.operation1.data,
  902. this.operation1.predecessor,
  903. this.operation1.salt,
  904. { from: executor },
  905. );
  906. await this.mock.execute(
  907. this.operation2.target,
  908. this.operation2.value,
  909. this.operation2.data,
  910. this.operation2.predecessor,
  911. this.operation2.salt,
  912. { from: executor },
  913. );
  914. });
  915. });
  916. describe('usage scenario', function () {
  917. this.timeout(10000);
  918. it('call', async function () {
  919. const operation = genOperation(
  920. this.implementation2.address,
  921. 0,
  922. this.implementation2.contract.methods.setValue(42).encodeABI(),
  923. ZERO_BYTES32,
  924. '0x8043596363daefc89977b25f9d9b4d06c3910959ef0c4d213557a903e1b555e2',
  925. );
  926. await this.mock.schedule(
  927. operation.target,
  928. operation.value,
  929. operation.data,
  930. operation.predecessor,
  931. operation.salt,
  932. MINDELAY,
  933. { from: proposer },
  934. );
  935. await time.increase(MINDELAY);
  936. await this.mock.execute(
  937. operation.target,
  938. operation.value,
  939. operation.data,
  940. operation.predecessor,
  941. operation.salt,
  942. { from: executor },
  943. );
  944. expect(await this.implementation2.getValue()).to.be.bignumber.equal(web3.utils.toBN(42));
  945. });
  946. it('call reverting', async function () {
  947. const operation = genOperation(
  948. this.callreceivermock.address,
  949. 0,
  950. this.callreceivermock.contract.methods.mockFunctionRevertsNoReason().encodeABI(),
  951. ZERO_BYTES32,
  952. '0xb1b1b276fdf1a28d1e00537ea73b04d56639128b08063c1a2f70a52e38cba693',
  953. );
  954. await this.mock.schedule(
  955. operation.target,
  956. operation.value,
  957. operation.data,
  958. operation.predecessor,
  959. operation.salt,
  960. MINDELAY,
  961. { from: proposer },
  962. );
  963. await time.increase(MINDELAY);
  964. await expectRevertCustomError(
  965. this.mock.execute(operation.target, operation.value, operation.data, operation.predecessor, operation.salt, {
  966. from: executor,
  967. }),
  968. 'FailedInnerCall',
  969. [],
  970. );
  971. });
  972. it('call throw', async function () {
  973. const operation = genOperation(
  974. this.callreceivermock.address,
  975. 0,
  976. this.callreceivermock.contract.methods.mockFunctionThrows().encodeABI(),
  977. ZERO_BYTES32,
  978. '0xe5ca79f295fc8327ee8a765fe19afb58f4a0cbc5053642bfdd7e73bc68e0fc67',
  979. );
  980. await this.mock.schedule(
  981. operation.target,
  982. operation.value,
  983. operation.data,
  984. operation.predecessor,
  985. operation.salt,
  986. MINDELAY,
  987. { from: proposer },
  988. );
  989. await time.increase(MINDELAY);
  990. // Targeted function reverts with a panic code (0x1) + the timelock bubble the panic code
  991. await expectRevert.unspecified(
  992. this.mock.execute(operation.target, operation.value, operation.data, operation.predecessor, operation.salt, {
  993. from: executor,
  994. }),
  995. );
  996. });
  997. it('call out of gas', async function () {
  998. const operation = genOperation(
  999. this.callreceivermock.address,
  1000. 0,
  1001. this.callreceivermock.contract.methods.mockFunctionOutOfGas().encodeABI(),
  1002. ZERO_BYTES32,
  1003. '0xf3274ce7c394c5b629d5215723563a744b817e1730cca5587c567099a14578fd',
  1004. );
  1005. await this.mock.schedule(
  1006. operation.target,
  1007. operation.value,
  1008. operation.data,
  1009. operation.predecessor,
  1010. operation.salt,
  1011. MINDELAY,
  1012. { from: proposer },
  1013. );
  1014. await time.increase(MINDELAY);
  1015. await expectRevertCustomError(
  1016. this.mock.execute(operation.target, operation.value, operation.data, operation.predecessor, operation.salt, {
  1017. from: executor,
  1018. gas: '100000',
  1019. }),
  1020. 'FailedInnerCall',
  1021. [],
  1022. );
  1023. });
  1024. it('call payable with eth', async function () {
  1025. const operation = genOperation(
  1026. this.callreceivermock.address,
  1027. 1,
  1028. this.callreceivermock.contract.methods.mockFunction().encodeABI(),
  1029. ZERO_BYTES32,
  1030. '0x5ab73cd33477dcd36c1e05e28362719d0ed59a7b9ff14939de63a43073dc1f44',
  1031. );
  1032. await this.mock.schedule(
  1033. operation.target,
  1034. operation.value,
  1035. operation.data,
  1036. operation.predecessor,
  1037. operation.salt,
  1038. MINDELAY,
  1039. { from: proposer },
  1040. );
  1041. await time.increase(MINDELAY);
  1042. expect(await web3.eth.getBalance(this.mock.address)).to.be.bignumber.equal(web3.utils.toBN(0));
  1043. expect(await web3.eth.getBalance(this.callreceivermock.address)).to.be.bignumber.equal(web3.utils.toBN(0));
  1044. await this.mock.execute(
  1045. operation.target,
  1046. operation.value,
  1047. operation.data,
  1048. operation.predecessor,
  1049. operation.salt,
  1050. { from: executor, value: 1 },
  1051. );
  1052. expect(await web3.eth.getBalance(this.mock.address)).to.be.bignumber.equal(web3.utils.toBN(0));
  1053. expect(await web3.eth.getBalance(this.callreceivermock.address)).to.be.bignumber.equal(web3.utils.toBN(1));
  1054. });
  1055. it('call nonpayable with eth', async function () {
  1056. const operation = genOperation(
  1057. this.callreceivermock.address,
  1058. 1,
  1059. this.callreceivermock.contract.methods.mockFunctionNonPayable().encodeABI(),
  1060. ZERO_BYTES32,
  1061. '0xb78edbd920c7867f187e5aa6294ae5a656cfbf0dea1ccdca3751b740d0f2bdf8',
  1062. );
  1063. await this.mock.schedule(
  1064. operation.target,
  1065. operation.value,
  1066. operation.data,
  1067. operation.predecessor,
  1068. operation.salt,
  1069. MINDELAY,
  1070. { from: proposer },
  1071. );
  1072. await time.increase(MINDELAY);
  1073. expect(await web3.eth.getBalance(this.mock.address)).to.be.bignumber.equal(web3.utils.toBN(0));
  1074. expect(await web3.eth.getBalance(this.callreceivermock.address)).to.be.bignumber.equal(web3.utils.toBN(0));
  1075. await expectRevertCustomError(
  1076. this.mock.execute(operation.target, operation.value, operation.data, operation.predecessor, operation.salt, {
  1077. from: executor,
  1078. }),
  1079. 'FailedInnerCall',
  1080. [],
  1081. );
  1082. expect(await web3.eth.getBalance(this.mock.address)).to.be.bignumber.equal(web3.utils.toBN(0));
  1083. expect(await web3.eth.getBalance(this.callreceivermock.address)).to.be.bignumber.equal(web3.utils.toBN(0));
  1084. });
  1085. it('call reverting with eth', async function () {
  1086. const operation = genOperation(
  1087. this.callreceivermock.address,
  1088. 1,
  1089. this.callreceivermock.contract.methods.mockFunctionRevertsNoReason().encodeABI(),
  1090. ZERO_BYTES32,
  1091. '0xdedb4563ef0095db01d81d3f2decf57cf83e4a72aa792af14c43a792b56f4de6',
  1092. );
  1093. await this.mock.schedule(
  1094. operation.target,
  1095. operation.value,
  1096. operation.data,
  1097. operation.predecessor,
  1098. operation.salt,
  1099. MINDELAY,
  1100. { from: proposer },
  1101. );
  1102. await time.increase(MINDELAY);
  1103. expect(await web3.eth.getBalance(this.mock.address)).to.be.bignumber.equal(web3.utils.toBN(0));
  1104. expect(await web3.eth.getBalance(this.callreceivermock.address)).to.be.bignumber.equal(web3.utils.toBN(0));
  1105. await expectRevertCustomError(
  1106. this.mock.execute(operation.target, operation.value, operation.data, operation.predecessor, operation.salt, {
  1107. from: executor,
  1108. }),
  1109. 'FailedInnerCall',
  1110. [],
  1111. );
  1112. expect(await web3.eth.getBalance(this.mock.address)).to.be.bignumber.equal(web3.utils.toBN(0));
  1113. expect(await web3.eth.getBalance(this.callreceivermock.address)).to.be.bignumber.equal(web3.utils.toBN(0));
  1114. });
  1115. });
  1116. describe('safe receive', function () {
  1117. describe('ERC721', function () {
  1118. const name = 'Non Fungible Token';
  1119. const symbol = 'NFT';
  1120. const tokenId = new BN(1);
  1121. beforeEach(async function () {
  1122. this.token = await ERC721.new(name, symbol);
  1123. await this.token.$_mint(other, tokenId);
  1124. });
  1125. it('can receive an ERC721 safeTransfer', async function () {
  1126. await this.token.safeTransferFrom(other, this.mock.address, tokenId, { from: other });
  1127. });
  1128. });
  1129. describe('ERC1155', function () {
  1130. const uri = 'https://token-cdn-domain/{id}.json';
  1131. const tokenIds = {
  1132. 1: new BN(1000),
  1133. 2: new BN(2000),
  1134. 3: new BN(3000),
  1135. };
  1136. beforeEach(async function () {
  1137. this.token = await ERC1155.new(uri);
  1138. await this.token.$_mintBatch(other, Object.keys(tokenIds), Object.values(tokenIds), '0x');
  1139. });
  1140. it('can receive ERC1155 safeTransfer', async function () {
  1141. await this.token.safeTransferFrom(
  1142. other,
  1143. this.mock.address,
  1144. ...Object.entries(tokenIds)[0], // id + amount
  1145. '0x',
  1146. { from: other },
  1147. );
  1148. });
  1149. it('can receive ERC1155 safeBatchTransfer', async function () {
  1150. await this.token.safeBatchTransferFrom(
  1151. other,
  1152. this.mock.address,
  1153. Object.keys(tokenIds),
  1154. Object.values(tokenIds),
  1155. '0x',
  1156. { from: other },
  1157. );
  1158. });
  1159. });
  1160. });
  1161. });