TransparentUpgradeableProxy.behaviour.js 18 KB

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