BeaconProxy.test.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. const { expectRevert } = require('@openzeppelin/test-helpers');
  2. const { getSlot, BeaconSlot } = require('../../helpers/erc1967');
  3. const { expectRevertCustomError } = require('../../helpers/customError');
  4. const { expect } = require('chai');
  5. const UpgradeableBeacon = artifacts.require('UpgradeableBeacon');
  6. const BeaconProxy = artifacts.require('BeaconProxy');
  7. const DummyImplementation = artifacts.require('DummyImplementation');
  8. const DummyImplementationV2 = artifacts.require('DummyImplementationV2');
  9. const BadBeaconNoImpl = artifacts.require('BadBeaconNoImpl');
  10. const BadBeaconNotContract = artifacts.require('BadBeaconNotContract');
  11. contract('BeaconProxy', function (accounts) {
  12. const [upgradeableBeaconAdmin, anotherAccount] = accounts;
  13. describe('bad beacon is not accepted', async function () {
  14. it('non-contract beacon', async function () {
  15. await expectRevertCustomError(BeaconProxy.new(anotherAccount, '0x'), 'ERC1967InvalidBeacon', [anotherAccount]);
  16. });
  17. it('non-compliant beacon', async function () {
  18. const beacon = await BadBeaconNoImpl.new();
  19. await expectRevert.unspecified(BeaconProxy.new(beacon.address, '0x'));
  20. });
  21. it('non-contract implementation', async function () {
  22. const beacon = await BadBeaconNotContract.new();
  23. const implementation = await beacon.implementation();
  24. await expectRevertCustomError(BeaconProxy.new(beacon.address, '0x'), 'ERC1967InvalidImplementation', [
  25. implementation,
  26. ]);
  27. });
  28. });
  29. before('deploy implementation', async function () {
  30. this.implementationV0 = await DummyImplementation.new();
  31. this.implementationV1 = await DummyImplementationV2.new();
  32. });
  33. describe('initialization', function () {
  34. before(function () {
  35. this.assertInitialized = async ({ value, balance }) => {
  36. const beaconSlot = await getSlot(this.proxy, BeaconSlot);
  37. const beaconAddress = web3.utils.toChecksumAddress(beaconSlot.substr(-40));
  38. expect(beaconAddress).to.equal(this.beacon.address);
  39. const dummy = new DummyImplementation(this.proxy.address);
  40. expect(await dummy.value()).to.bignumber.eq(value);
  41. expect(await web3.eth.getBalance(this.proxy.address)).to.bignumber.eq(balance);
  42. };
  43. });
  44. beforeEach('deploy beacon', async function () {
  45. this.beacon = await UpgradeableBeacon.new(this.implementationV0.address, upgradeableBeaconAdmin);
  46. });
  47. it('no initialization', async function () {
  48. const data = Buffer.from('');
  49. const balance = '10';
  50. this.proxy = await BeaconProxy.new(this.beacon.address, data, { value: balance });
  51. await this.assertInitialized({ value: '0', balance });
  52. });
  53. it('non-payable initialization', async function () {
  54. const value = '55';
  55. const data = this.implementationV0.contract.methods.initializeNonPayableWithValue(value).encodeABI();
  56. this.proxy = await BeaconProxy.new(this.beacon.address, data);
  57. await this.assertInitialized({ value, balance: '0' });
  58. });
  59. it('payable initialization', async function () {
  60. const value = '55';
  61. const data = this.implementationV0.contract.methods.initializePayableWithValue(value).encodeABI();
  62. const balance = '100';
  63. this.proxy = await BeaconProxy.new(this.beacon.address, data, { value: balance });
  64. await this.assertInitialized({ value, balance });
  65. });
  66. it('reverting initialization', async function () {
  67. const data = this.implementationV0.contract.methods.reverts().encodeABI();
  68. await expectRevert(BeaconProxy.new(this.beacon.address, data), 'DummyImplementation reverted');
  69. });
  70. });
  71. it('upgrade a proxy by upgrading its beacon', async function () {
  72. const beacon = await UpgradeableBeacon.new(this.implementationV0.address, upgradeableBeaconAdmin);
  73. const value = '10';
  74. const data = this.implementationV0.contract.methods.initializeNonPayableWithValue(value).encodeABI();
  75. const proxy = await BeaconProxy.new(beacon.address, data);
  76. const dummy = new DummyImplementation(proxy.address);
  77. // test initial values
  78. expect(await dummy.value()).to.bignumber.eq(value);
  79. // test initial version
  80. expect(await dummy.version()).to.eq('V1');
  81. // upgrade beacon
  82. await beacon.upgradeTo(this.implementationV1.address, { from: upgradeableBeaconAdmin });
  83. // test upgraded version
  84. expect(await dummy.version()).to.eq('V2');
  85. });
  86. it('upgrade 2 proxies by upgrading shared beacon', async function () {
  87. const value1 = '10';
  88. const value2 = '42';
  89. const beacon = await UpgradeableBeacon.new(this.implementationV0.address, upgradeableBeaconAdmin);
  90. const proxy1InitializeData = this.implementationV0.contract.methods
  91. .initializeNonPayableWithValue(value1)
  92. .encodeABI();
  93. const proxy1 = await BeaconProxy.new(beacon.address, proxy1InitializeData);
  94. const proxy2InitializeData = this.implementationV0.contract.methods
  95. .initializeNonPayableWithValue(value2)
  96. .encodeABI();
  97. const proxy2 = await BeaconProxy.new(beacon.address, proxy2InitializeData);
  98. const dummy1 = new DummyImplementation(proxy1.address);
  99. const dummy2 = new DummyImplementation(proxy2.address);
  100. // test initial values
  101. expect(await dummy1.value()).to.bignumber.eq(value1);
  102. expect(await dummy2.value()).to.bignumber.eq(value2);
  103. // test initial version
  104. expect(await dummy1.version()).to.eq('V1');
  105. expect(await dummy2.version()).to.eq('V1');
  106. // upgrade beacon
  107. await beacon.upgradeTo(this.implementationV1.address, { from: upgradeableBeaconAdmin });
  108. // test upgraded version
  109. expect(await dummy1.version()).to.eq('V2');
  110. expect(await dummy2.version()).to.eq('V2');
  111. });
  112. });