TransparentUpgradeableProxy.behaviour.js 17 KB

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