TimelockController.test.js 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286
  1. const { BN, constants, expectEvent, 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 Enums = 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. 'TimelockIncorrectState',
  170. [this.operation.id, Enums.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. 'TimelockIncorrectState',
  237. [this.operation.id, Enums.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. 'TimelockIncorrectState',
  263. [this.operation.id, Enums.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. 'TimelockIncorrectState',
  279. [this.operation.id, Enums.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. 'TimelockIncorrectState',
  365. [reentrantOperation.id, Enums.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. 'TimelockIncorrectState',
  453. [this.operation.id, Enums.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. 'TimelockIncorrectState',
  538. [this.operation.id, Enums.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. 'TimelockIncorrectState',
  564. [this.operation.id, Enums.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. 'TimelockIncorrectState',
  580. [this.operation.id, Enums.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. 'TimelockIncorrectState',
  710. [reentrantBatchOperation.id, Enums.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.mockFunctionThrows().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. 'TimelockFailedCall',
  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. 'TimelockIncorrectState',
  800. [constants.ZERO_BYTES32, Enums.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. 'TimelockMissingPredecessor',
  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. 'TimelockFailedCall',
  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. await expectRevertCustomError(
  991. this.mock.execute(operation.target, operation.value, operation.data, operation.predecessor, operation.salt, {
  992. from: executor,
  993. }),
  994. 'TimelockFailedCall',
  995. [],
  996. );
  997. });
  998. it('call out of gas', async function () {
  999. const operation = genOperation(
  1000. this.callreceivermock.address,
  1001. 0,
  1002. this.callreceivermock.contract.methods.mockFunctionOutOfGas().encodeABI(),
  1003. ZERO_BYTES32,
  1004. '0xf3274ce7c394c5b629d5215723563a744b817e1730cca5587c567099a14578fd',
  1005. );
  1006. await this.mock.schedule(
  1007. operation.target,
  1008. operation.value,
  1009. operation.data,
  1010. operation.predecessor,
  1011. operation.salt,
  1012. MINDELAY,
  1013. { from: proposer },
  1014. );
  1015. await time.increase(MINDELAY);
  1016. await expectRevertCustomError(
  1017. this.mock.execute(operation.target, operation.value, operation.data, operation.predecessor, operation.salt, {
  1018. from: executor,
  1019. gas: '70000',
  1020. }),
  1021. 'TimelockFailedCall',
  1022. [],
  1023. );
  1024. });
  1025. it('call payable with eth', async function () {
  1026. const operation = genOperation(
  1027. this.callreceivermock.address,
  1028. 1,
  1029. this.callreceivermock.contract.methods.mockFunction().encodeABI(),
  1030. ZERO_BYTES32,
  1031. '0x5ab73cd33477dcd36c1e05e28362719d0ed59a7b9ff14939de63a43073dc1f44',
  1032. );
  1033. await this.mock.schedule(
  1034. operation.target,
  1035. operation.value,
  1036. operation.data,
  1037. operation.predecessor,
  1038. operation.salt,
  1039. MINDELAY,
  1040. { from: proposer },
  1041. );
  1042. await time.increase(MINDELAY);
  1043. expect(await web3.eth.getBalance(this.mock.address)).to.be.bignumber.equal(web3.utils.toBN(0));
  1044. expect(await web3.eth.getBalance(this.callreceivermock.address)).to.be.bignumber.equal(web3.utils.toBN(0));
  1045. await this.mock.execute(
  1046. operation.target,
  1047. operation.value,
  1048. operation.data,
  1049. operation.predecessor,
  1050. operation.salt,
  1051. { from: executor, value: 1 },
  1052. );
  1053. expect(await web3.eth.getBalance(this.mock.address)).to.be.bignumber.equal(web3.utils.toBN(0));
  1054. expect(await web3.eth.getBalance(this.callreceivermock.address)).to.be.bignumber.equal(web3.utils.toBN(1));
  1055. });
  1056. it('call nonpayable with eth', async function () {
  1057. const operation = genOperation(
  1058. this.callreceivermock.address,
  1059. 1,
  1060. this.callreceivermock.contract.methods.mockFunctionNonPayable().encodeABI(),
  1061. ZERO_BYTES32,
  1062. '0xb78edbd920c7867f187e5aa6294ae5a656cfbf0dea1ccdca3751b740d0f2bdf8',
  1063. );
  1064. await this.mock.schedule(
  1065. operation.target,
  1066. operation.value,
  1067. operation.data,
  1068. operation.predecessor,
  1069. operation.salt,
  1070. MINDELAY,
  1071. { from: proposer },
  1072. );
  1073. await time.increase(MINDELAY);
  1074. expect(await web3.eth.getBalance(this.mock.address)).to.be.bignumber.equal(web3.utils.toBN(0));
  1075. expect(await web3.eth.getBalance(this.callreceivermock.address)).to.be.bignumber.equal(web3.utils.toBN(0));
  1076. await expectRevertCustomError(
  1077. this.mock.execute(operation.target, operation.value, operation.data, operation.predecessor, operation.salt, {
  1078. from: executor,
  1079. }),
  1080. 'TimelockFailedCall',
  1081. [],
  1082. );
  1083. expect(await web3.eth.getBalance(this.mock.address)).to.be.bignumber.equal(web3.utils.toBN(0));
  1084. expect(await web3.eth.getBalance(this.callreceivermock.address)).to.be.bignumber.equal(web3.utils.toBN(0));
  1085. });
  1086. it('call reverting with eth', async function () {
  1087. const operation = genOperation(
  1088. this.callreceivermock.address,
  1089. 1,
  1090. this.callreceivermock.contract.methods.mockFunctionRevertsNoReason().encodeABI(),
  1091. ZERO_BYTES32,
  1092. '0xdedb4563ef0095db01d81d3f2decf57cf83e4a72aa792af14c43a792b56f4de6',
  1093. );
  1094. await this.mock.schedule(
  1095. operation.target,
  1096. operation.value,
  1097. operation.data,
  1098. operation.predecessor,
  1099. operation.salt,
  1100. MINDELAY,
  1101. { from: proposer },
  1102. );
  1103. await time.increase(MINDELAY);
  1104. expect(await web3.eth.getBalance(this.mock.address)).to.be.bignumber.equal(web3.utils.toBN(0));
  1105. expect(await web3.eth.getBalance(this.callreceivermock.address)).to.be.bignumber.equal(web3.utils.toBN(0));
  1106. await expectRevertCustomError(
  1107. this.mock.execute(operation.target, operation.value, operation.data, operation.predecessor, operation.salt, {
  1108. from: executor,
  1109. }),
  1110. 'TimelockFailedCall',
  1111. [],
  1112. );
  1113. expect(await web3.eth.getBalance(this.mock.address)).to.be.bignumber.equal(web3.utils.toBN(0));
  1114. expect(await web3.eth.getBalance(this.callreceivermock.address)).to.be.bignumber.equal(web3.utils.toBN(0));
  1115. });
  1116. });
  1117. describe('safe receive', function () {
  1118. describe('ERC721', function () {
  1119. const name = 'Non Fungible Token';
  1120. const symbol = 'NFT';
  1121. const tokenId = new BN(1);
  1122. beforeEach(async function () {
  1123. this.token = await ERC721.new(name, symbol);
  1124. await this.token.$_mint(other, tokenId);
  1125. });
  1126. it('can receive an ERC721 safeTransfer', async function () {
  1127. await this.token.safeTransferFrom(other, this.mock.address, tokenId, { from: other });
  1128. });
  1129. });
  1130. describe('ERC1155', function () {
  1131. const uri = 'https://token-cdn-domain/{id}.json';
  1132. const tokenIds = {
  1133. 1: new BN(1000),
  1134. 2: new BN(2000),
  1135. 3: new BN(3000),
  1136. };
  1137. beforeEach(async function () {
  1138. this.token = await ERC1155.new(uri);
  1139. await this.token.$_mintBatch(other, Object.keys(tokenIds), Object.values(tokenIds), '0x');
  1140. });
  1141. it('can receive ERC1155 safeTransfer', async function () {
  1142. await this.token.safeTransferFrom(
  1143. other,
  1144. this.mock.address,
  1145. ...Object.entries(tokenIds)[0], // id + amount
  1146. '0x',
  1147. { from: other },
  1148. );
  1149. });
  1150. it('can receive ERC1155 safeBatchTransfer', async function () {
  1151. await this.token.safeBatchTransferFrom(
  1152. other,
  1153. this.mock.address,
  1154. Object.keys(tokenIds),
  1155. Object.values(tokenIds),
  1156. '0x',
  1157. { from: other },
  1158. );
  1159. });
  1160. });
  1161. });
  1162. });