TransparentUpgradeableProxy.behaviour.js 17 KB

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