TransparentUpgradeableProxy.behaviour.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. const { BN, expectRevert, expectEvent, constants } = require('@openzeppelin/test-helpers');
  2. const { ZERO_ADDRESS } = constants;
  3. const { getSlot, ImplementationSlot, AdminSlot } = require('../../helpers/erc1967');
  4. const { expect } = require('chai');
  5. const Proxy = artifacts.require('Proxy');
  6. const Implementation1 = artifacts.require('Implementation1');
  7. const Implementation2 = artifacts.require('Implementation2');
  8. const Implementation3 = artifacts.require('Implementation3');
  9. const Implementation4 = artifacts.require('Implementation4');
  10. const MigratableMockV1 = artifacts.require('MigratableMockV1');
  11. const MigratableMockV2 = artifacts.require('MigratableMockV2');
  12. const MigratableMockV3 = artifacts.require('MigratableMockV3');
  13. const InitializableMock = artifacts.require('InitializableMock');
  14. const DummyImplementation = artifacts.require('DummyImplementation');
  15. const ClashingImplementation = artifacts.require('ClashingImplementation');
  16. module.exports = function shouldBehaveLikeTransparentUpgradeableProxy (createProxy, accounts) {
  17. const [proxyAdminAddress, proxyAdminOwner, anotherAccount] = accounts;
  18. before(async function () {
  19. this.implementationV0 = (await DummyImplementation.new()).address;
  20. this.implementationV1 = (await DummyImplementation.new()).address;
  21. });
  22. beforeEach(async function () {
  23. const initializeData = Buffer.from('');
  24. this.proxy = await createProxy(this.implementationV0, proxyAdminAddress, initializeData, {
  25. from: proxyAdminOwner,
  26. });
  27. this.proxyAddress = this.proxy.address;
  28. });
  29. describe('implementation', function () {
  30. it('returns the current implementation address', async function () {
  31. const implementation = await this.proxy.implementation({ from: proxyAdminAddress });
  32. expect(implementation).to.be.equal(this.implementationV0);
  33. });
  34. it('delegates to the implementation', async function () {
  35. const dummy = new DummyImplementation(this.proxyAddress);
  36. const value = await dummy.get();
  37. expect(value).to.equal(true);
  38. });
  39. });
  40. describe('upgradeTo', function () {
  41. describe('when the sender is the admin', function () {
  42. const from = proxyAdminAddress;
  43. describe('when the given implementation is different from the current one', function () {
  44. it('upgrades to the requested implementation', async function () {
  45. await this.proxy.upgradeTo(this.implementationV1, { from });
  46. const implementation = await this.proxy.implementation({ from: proxyAdminAddress });
  47. expect(implementation).to.be.equal(this.implementationV1);
  48. });
  49. it('emits an event', async function () {
  50. expectEvent(
  51. await this.proxy.upgradeTo(this.implementationV1, { from }),
  52. 'Upgraded', {
  53. implementation: this.implementationV1,
  54. },
  55. );
  56. });
  57. });
  58. describe('when the given implementation is the zero address', function () {
  59. it('reverts', async function () {
  60. await expectRevert(
  61. this.proxy.upgradeTo(ZERO_ADDRESS, { from }),
  62. 'ERC1967: new implementation is not a contract',
  63. );
  64. });
  65. });
  66. });
  67. describe('when the sender is not the admin', function () {
  68. const from = anotherAccount;
  69. it('reverts', async function () {
  70. await expectRevert.unspecified(
  71. this.proxy.upgradeTo(this.implementationV1, { from }),
  72. );
  73. });
  74. });
  75. });
  76. describe('upgradeToAndCall', function () {
  77. describe('without migrations', function () {
  78. beforeEach(async function () {
  79. this.behavior = await InitializableMock.new();
  80. });
  81. describe('when the call does not fail', function () {
  82. const initializeData = new InitializableMock('').contract.methods['initializeWithX(uint256)'](42).encodeABI();
  83. describe('when the sender is the admin', function () {
  84. const from = proxyAdminAddress;
  85. const value = 1e5;
  86. beforeEach(async function () {
  87. this.receipt = await this.proxy.upgradeToAndCall(this.behavior.address, initializeData, { from, value });
  88. });
  89. it('upgrades to the requested implementation', async function () {
  90. const implementation = await this.proxy.implementation({ from: proxyAdminAddress });
  91. expect(implementation).to.be.equal(this.behavior.address);
  92. });
  93. it('emits an event', function () {
  94. expectEvent(this.receipt, 'Upgraded', { implementation: this.behavior.address });
  95. });
  96. it('calls the initializer function', async function () {
  97. const migratable = new InitializableMock(this.proxyAddress);
  98. const x = await migratable.x();
  99. expect(x).to.be.bignumber.equal('42');
  100. });
  101. it('sends given value to the proxy', async function () {
  102. const balance = await web3.eth.getBalance(this.proxyAddress);
  103. expect(balance.toString()).to.be.bignumber.equal(value.toString());
  104. });
  105. it.skip('uses the storage of the proxy', async function () {
  106. // storage layout should look as follows:
  107. // - 0: Initializable storage
  108. // - 1-50: Initailizable reserved storage (50 slots)
  109. // - 51: initializerRan
  110. // - 52: x
  111. const storedValue = await Proxy.at(this.proxyAddress).getStorageAt(52);
  112. expect(parseInt(storedValue)).to.eq(42);
  113. });
  114. });
  115. describe('when the sender is not the admin', function () {
  116. it('reverts', async function () {
  117. await expectRevert.unspecified(
  118. this.proxy.upgradeToAndCall(this.behavior.address, initializeData, { from: anotherAccount }),
  119. );
  120. });
  121. });
  122. });
  123. describe('when the call does fail', function () {
  124. const initializeData = new InitializableMock('').contract.methods.fail().encodeABI();
  125. it('reverts', async function () {
  126. await expectRevert.unspecified(
  127. this.proxy.upgradeToAndCall(this.behavior.address, initializeData, { from: proxyAdminAddress }),
  128. );
  129. });
  130. });
  131. });
  132. describe('with migrations', function () {
  133. describe('when the sender is the admin', function () {
  134. const from = proxyAdminAddress;
  135. const value = 1e5;
  136. describe('when upgrading to V1', function () {
  137. const v1MigrationData = new MigratableMockV1('').contract.methods.initialize(42).encodeABI();
  138. beforeEach(async function () {
  139. this.behaviorV1 = await MigratableMockV1.new();
  140. this.balancePreviousV1 = new BN(await web3.eth.getBalance(this.proxyAddress));
  141. this.receipt = await this.proxy.upgradeToAndCall(this.behaviorV1.address, v1MigrationData, { from, value });
  142. });
  143. it('upgrades to the requested version and emits an event', async function () {
  144. const implementation = await this.proxy.implementation({ from: proxyAdminAddress });
  145. expect(implementation).to.be.equal(this.behaviorV1.address);
  146. expectEvent(this.receipt, 'Upgraded', { implementation: this.behaviorV1.address });
  147. });
  148. it('calls the \'initialize\' function and sends given value to the proxy', async function () {
  149. const migratable = new MigratableMockV1(this.proxyAddress);
  150. const x = await migratable.x();
  151. expect(x).to.be.bignumber.equal('42');
  152. const balance = await web3.eth.getBalance(this.proxyAddress);
  153. expect(new BN(balance)).to.be.bignumber.equal(this.balancePreviousV1.addn(value));
  154. });
  155. describe('when upgrading to V2', function () {
  156. const v2MigrationData = new MigratableMockV2('').contract.methods.migrate(10, 42).encodeABI();
  157. beforeEach(async function () {
  158. this.behaviorV2 = await MigratableMockV2.new();
  159. this.balancePreviousV2 = new BN(await web3.eth.getBalance(this.proxyAddress));
  160. this.receipt =
  161. await this.proxy.upgradeToAndCall(this.behaviorV2.address, v2MigrationData, { from, value });
  162. });
  163. it('upgrades to the requested version and emits an event', async function () {
  164. const implementation = await this.proxy.implementation({ from: proxyAdminAddress });
  165. expect(implementation).to.be.equal(this.behaviorV2.address);
  166. expectEvent(this.receipt, 'Upgraded', { implementation: this.behaviorV2.address });
  167. });
  168. it('calls the \'migrate\' function and sends given value to the proxy', async function () {
  169. const migratable = new MigratableMockV2(this.proxyAddress);
  170. const x = await migratable.x();
  171. expect(x).to.be.bignumber.equal('10');
  172. const y = await migratable.y();
  173. expect(y).to.be.bignumber.equal('42');
  174. const balance = new BN(await web3.eth.getBalance(this.proxyAddress));
  175. expect(balance).to.be.bignumber.equal(this.balancePreviousV2.addn(value));
  176. });
  177. describe('when upgrading to V3', function () {
  178. const v3MigrationData = new MigratableMockV3('').contract.methods['migrate()']().encodeABI();
  179. beforeEach(async function () {
  180. this.behaviorV3 = await MigratableMockV3.new();
  181. this.balancePreviousV3 = new BN(await web3.eth.getBalance(this.proxyAddress));
  182. this.receipt =
  183. await this.proxy.upgradeToAndCall(this.behaviorV3.address, v3MigrationData, { from, value });
  184. });
  185. it('upgrades to the requested version and emits an event', async function () {
  186. const implementation = await this.proxy.implementation({ from: proxyAdminAddress });
  187. expect(implementation).to.be.equal(this.behaviorV3.address);
  188. expectEvent(this.receipt, 'Upgraded', { implementation: this.behaviorV3.address });
  189. });
  190. it('calls the \'migrate\' function and sends given value to the proxy', async function () {
  191. const migratable = new MigratableMockV3(this.proxyAddress);
  192. const x = await migratable.x();
  193. expect(x).to.be.bignumber.equal('42');
  194. const y = await migratable.y();
  195. expect(y).to.be.bignumber.equal('10');
  196. const balance = new BN(await web3.eth.getBalance(this.proxyAddress));
  197. expect(balance).to.be.bignumber.equal(this.balancePreviousV3.addn(value));
  198. });
  199. });
  200. });
  201. });
  202. });
  203. describe('when the sender is not the admin', function () {
  204. const from = anotherAccount;
  205. it('reverts', async function () {
  206. const behaviorV1 = await MigratableMockV1.new();
  207. const v1MigrationData = new MigratableMockV1('').contract.methods.initialize(42).encodeABI();
  208. await expectRevert.unspecified(
  209. this.proxy.upgradeToAndCall(behaviorV1.address, v1MigrationData, { from }),
  210. );
  211. });
  212. });
  213. });
  214. });
  215. describe('changeAdmin', function () {
  216. describe('when the new proposed admin is not the zero address', function () {
  217. const newAdmin = anotherAccount;
  218. describe('when the sender is the admin', function () {
  219. beforeEach('transferring', async function () {
  220. this.receipt = await this.proxy.changeAdmin(newAdmin, { from: proxyAdminAddress });
  221. });
  222. it('assigns new proxy admin', async function () {
  223. const newProxyAdmin = await this.proxy.admin({ from: newAdmin });
  224. expect(newProxyAdmin).to.be.equal(anotherAccount);
  225. });
  226. it('emits an event', function () {
  227. expectEvent(this.receipt, 'AdminChanged', {
  228. previousAdmin: proxyAdminAddress,
  229. newAdmin: newAdmin,
  230. });
  231. });
  232. });
  233. describe('when the sender is not the admin', function () {
  234. it('reverts', async function () {
  235. await expectRevert.unspecified(this.proxy.changeAdmin(newAdmin, { from: anotherAccount }));
  236. });
  237. });
  238. });
  239. describe('when the new proposed admin is the zero address', function () {
  240. it('reverts', async function () {
  241. await expectRevert(
  242. this.proxy.changeAdmin(ZERO_ADDRESS, { from: proxyAdminAddress }),
  243. 'ERC1967: new admin is the zero address',
  244. );
  245. });
  246. });
  247. });
  248. describe('storage', function () {
  249. it('should store the implementation address in specified location', async function () {
  250. const implementationSlot = await getSlot(this.proxy, ImplementationSlot);
  251. const implementationAddress = web3.utils.toChecksumAddress(implementationSlot.substr(-40));
  252. expect(implementationAddress).to.be.equal(this.implementationV0);
  253. });
  254. it('should store the admin proxy in specified location', async function () {
  255. const proxyAdminSlot = await getSlot(this.proxy, AdminSlot);
  256. const proxyAdminAddress = web3.utils.toChecksumAddress(proxyAdminSlot.substr(-40));
  257. expect(proxyAdminAddress).to.be.equal(proxyAdminAddress);
  258. });
  259. });
  260. describe('transparent proxy', function () {
  261. beforeEach('creating proxy', async function () {
  262. const initializeData = Buffer.from('');
  263. this.impl = await ClashingImplementation.new();
  264. this.proxy = await createProxy(this.impl.address, proxyAdminAddress, initializeData, { from: proxyAdminOwner });
  265. this.clashing = new ClashingImplementation(this.proxy.address);
  266. });
  267. it('proxy admin cannot call delegated functions', async function () {
  268. await expectRevert(
  269. this.clashing.delegatedFunction({ from: proxyAdminAddress }),
  270. 'TransparentUpgradeableProxy: admin cannot fallback to proxy target',
  271. );
  272. });
  273. describe('when function names clash', function () {
  274. it('when sender is proxy admin should run the proxy function', async function () {
  275. const value = await this.proxy.admin({ from: proxyAdminAddress, value: 0 });
  276. expect(value).to.be.equal(proxyAdminAddress);
  277. });
  278. it('when sender is other should delegate to implementation', async function () {
  279. const value = await this.proxy.admin({ from: anotherAccount, value: 0 });
  280. expect(value).to.be.equal('0x0000000000000000000000000000000011111142');
  281. });
  282. it('when sender is proxy admin value should not be accepted', async function () {
  283. await expectRevert.unspecified(this.proxy.admin({ from: proxyAdminAddress, value: 1 }));
  284. });
  285. it('when sender is other value should be accepted', async function () {
  286. const value = await this.proxy.admin({ from: anotherAccount, value: 1 });
  287. expect(value).to.be.equal('0x0000000000000000000000000000000011111142');
  288. });
  289. });
  290. });
  291. describe('regression', () => {
  292. const initializeData = Buffer.from('');
  293. it('should add new function', async () => {
  294. const instance1 = await Implementation1.new();
  295. const proxy = await createProxy(instance1.address, proxyAdminAddress, initializeData, { from: proxyAdminOwner });
  296. const proxyInstance1 = new Implementation1(proxy.address);
  297. await proxyInstance1.setValue(42);
  298. const instance2 = await Implementation2.new();
  299. await proxy.upgradeTo(instance2.address, { from: proxyAdminAddress });
  300. const proxyInstance2 = new Implementation2(proxy.address);
  301. const res = await proxyInstance2.getValue();
  302. expect(res.toString()).to.eq('42');
  303. });
  304. it('should remove function', async () => {
  305. const instance2 = await Implementation2.new();
  306. const proxy = await createProxy(instance2.address, proxyAdminAddress, initializeData, { from: proxyAdminOwner });
  307. const proxyInstance2 = new Implementation2(proxy.address);
  308. await proxyInstance2.setValue(42);
  309. const res = await proxyInstance2.getValue();
  310. expect(res.toString()).to.eq('42');
  311. const instance1 = await Implementation1.new();
  312. await proxy.upgradeTo(instance1.address, { from: proxyAdminAddress });
  313. const proxyInstance1 = new Implementation2(proxy.address);
  314. await expectRevert.unspecified(proxyInstance1.getValue());
  315. });
  316. it('should change function signature', async () => {
  317. const instance1 = await Implementation1.new();
  318. const proxy = await createProxy(instance1.address, proxyAdminAddress, initializeData, { from: proxyAdminOwner });
  319. const proxyInstance1 = new Implementation1(proxy.address);
  320. await proxyInstance1.setValue(42);
  321. const instance3 = await Implementation3.new();
  322. await proxy.upgradeTo(instance3.address, { from: proxyAdminAddress });
  323. const proxyInstance3 = new Implementation3(proxy.address);
  324. const res = await proxyInstance3.getValue(8);
  325. expect(res.toString()).to.eq('50');
  326. });
  327. it('should add fallback function', async () => {
  328. const initializeData = Buffer.from('');
  329. const instance1 = await Implementation1.new();
  330. const proxy = await createProxy(instance1.address, proxyAdminAddress, initializeData, { from: proxyAdminOwner });
  331. const instance4 = await Implementation4.new();
  332. await proxy.upgradeTo(instance4.address, { from: proxyAdminAddress });
  333. const proxyInstance4 = new Implementation4(proxy.address);
  334. const data = '0x';
  335. await web3.eth.sendTransaction({ to: proxy.address, from: anotherAccount, data });
  336. const res = await proxyInstance4.getValue();
  337. expect(res.toString()).to.eq('1');
  338. });
  339. it('should remove fallback function', async () => {
  340. const instance4 = await Implementation4.new();
  341. const proxy = await createProxy(instance4.address, proxyAdminAddress, initializeData, { from: proxyAdminOwner });
  342. const instance2 = await Implementation2.new();
  343. await proxy.upgradeTo(instance2.address, { from: proxyAdminAddress });
  344. const data = '0x';
  345. await expectRevert.unspecified(
  346. web3.eth.sendTransaction({ to: proxy.address, from: anotherAccount, data }),
  347. );
  348. const proxyInstance2 = new Implementation2(proxy.address);
  349. const res = await proxyInstance2.getValue();
  350. expect(res.toString()).to.eq('0');
  351. });
  352. });
  353. };