TransparentUpgradeableProxy.behaviour.js 18 KB

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