TimelockController.test.js 44 KB

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