Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Use helper function
  • Loading branch information
JulissaDantes committed Nov 25, 2022
commit 6f773c76749dee49b277ca466adfe1ba7fa20f57
17 changes: 10 additions & 7 deletions test/proxy/transparent/ProxyAdmin.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { expectRevert } = require('@openzeppelin/test-helpers');

const { getSlot, ImplementationSlot, AdminSlot } = require('../../helpers/erc1967');
const { expect } = require('chai');
const { network } = require('hardhat');
const ImplV1 = artifacts.require('DummyImplementation');
Expand Down Expand Up @@ -40,9 +40,10 @@ contract('ProxyAdmin', function (accounts) {

it('changes proxy admin', async function () {
await this.proxyAdmin.changeProxyAdmin(this.proxy.address, newAdmin, { from: proxyAdminOwner });
const proxyAdminSlot = await getSlot(this.proxy, AdminSlot);
const proxyAdminAddress = web3.utils.toChecksumAddress(proxyAdminSlot.substr(-40));

const admin = '0x'+(await network.provider.send('eth_getStorageAt', [this.proxy.address,'0xb53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d6103','latest'])).substring(26);
expect(admin.toUpperCase()).to.be.eq(newAdmin.toUpperCase());
expect(proxyAdminAddress).to.be.eq(newAdmin);
});
});

Expand All @@ -60,8 +61,9 @@ contract('ProxyAdmin', function (accounts) {
it('upgrades implementation', async function () {
await this.proxyAdmin.upgrade(this.proxy.address, this.implementationV2.address, { from: proxyAdminOwner });

const implementation = '0x'+(await network.provider.send('eth_getStorageAt', [this.proxy.address,'0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc','latest'])).substring(26);
expect(implementation.toUpperCase()).to.be.eq(this.implementationV2.address.toUpperCase());
const implementationSlot = await getSlot(this.proxy, ImplementationSlot);
const implementationAddress = web3.utils.toChecksumAddress(implementationSlot.substr(-40));
expect(implementationAddress).to.be.eq(this.implementationV2.address);
});
});
});
Expand Down Expand Up @@ -97,8 +99,9 @@ contract('ProxyAdmin', function (accounts) {
await this.proxyAdmin.upgradeAndCall(this.proxy.address, this.implementationV2.address, callData,
{ from: proxyAdminOwner },
);
const implementation = '0x'+(await network.provider.send('eth_getStorageAt', [this.proxy.address,'0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc','latest'])).substring(26);
expect(implementation.toUpperCase()).to.be.eq(this.implementationV2.address.toUpperCase());
const implementationSlot = await getSlot(this.proxy, ImplementationSlot);
const implementationAddress = web3.utils.toChecksumAddress(implementationSlot.substr(-40));
expect(implementationAddress).to.be.eq(this.implementationV2.address);
});
});
});
Expand Down
61 changes: 47 additions & 14 deletions test/proxy/transparent/TransparentUpgradeableProxy.behaviour.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ module.exports = function shouldBehaveLikeTransparentUpgradeableProxy (createPro
});

describe('implementation', function () {
it('returns the current implementation address', async function () {
const implementation = '0x'+(await network.provider.send('eth_getStorageAt', [this.proxy.address,'0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc','latest'])).substring(26);

expect(web3.utils.toChecksumAddress(implementation)).to.be.equal(this.implementationV0);
});

it('delegates to the implementation', async function () {
const dummy = new DummyImplementation(this.proxyAddress);
const value = await dummy.get();
Expand All @@ -46,6 +52,14 @@ module.exports = function shouldBehaveLikeTransparentUpgradeableProxy (createPro
const from = proxyAdminAddress;

describe('when the given implementation is different from the current one', function () {
it('upgrades to the requested implementation', async function () {
await this.proxy.upgradeTo(this.implementationV1, { from });

const implementationSlot = await getSlot(this.proxy, ImplementationSlot);
const implementationAddress = web3.utils.toChecksumAddress(implementationSlot.substr(-40));
expect(implementationAddress).to.be.equal(this.implementationV1);
});

it('emits an event', async function () {
expectEvent(
await this.proxy.upgradeTo(this.implementationV1, { from }),
Expand Down Expand Up @@ -94,6 +108,12 @@ module.exports = function shouldBehaveLikeTransparentUpgradeableProxy (createPro
this.receipt = await this.proxy.upgradeToAndCall(this.behavior.address, initializeData, { from, value });
});

it('upgrades to the requested implementation', async function () {
const implementationSlot = await getSlot(this.proxy, ImplementationSlot);
const implementationAddress = web3.utils.toChecksumAddress(implementationSlot.substr(-40));
expect(implementationAddress).to.be.equal(this.behavior.address);
});

it('emits an event', function () {
expectEvent(this.receipt, 'Upgraded', { implementation: this.behavior.address });
});
Expand Down Expand Up @@ -154,6 +174,13 @@ module.exports = function shouldBehaveLikeTransparentUpgradeableProxy (createPro
this.receipt = await this.proxy.upgradeToAndCall(this.behaviorV1.address, v1MigrationData, { from, value });
});

it('upgrades to the requested version and emits an event', async function () {
const implementationSlot = await getSlot(this.proxy, ImplementationSlot);
const implementation = web3.utils.toChecksumAddress(implementationSlot.substr(-40));
expect(implementation).to.be.equal(this.behaviorV1.address);
expectEvent(this.receipt, 'Upgraded', { implementation: this.behaviorV1.address });
});

it('calls the \'initialize\' function and sends given value to the proxy', async function () {
const migratable = new MigratableMockV1(this.proxyAddress);

Expand All @@ -174,6 +201,13 @@ module.exports = function shouldBehaveLikeTransparentUpgradeableProxy (createPro
await this.proxy.upgradeToAndCall(this.behaviorV2.address, v2MigrationData, { from, value });
});

it('upgrades to the requested version and emits an event', async function () {
const implementationSlot = await getSlot(this.proxy, ImplementationSlot);
const implementation = web3.utils.toChecksumAddress(implementationSlot.substr(-40));
expect(implementation).to.be.equal(this.behaviorV2.address);
expectEvent(this.receipt, 'Upgraded', { implementation: this.behaviorV2.address });
});

it('calls the \'migrate\' function and sends given value to the proxy', async function () {
const migratable = new MigratableMockV2(this.proxyAddress);

Expand All @@ -197,6 +231,13 @@ module.exports = function shouldBehaveLikeTransparentUpgradeableProxy (createPro
await this.proxy.upgradeToAndCall(this.behaviorV3.address, v3MigrationData, { from, value });
});

it('upgrades to the requested version and emits an event', async function () {
const implementationSlot = await getSlot(this.proxy, ImplementationSlot);
const implementation = web3.utils.toChecksumAddress(implementationSlot.substr(-40));
expect(implementation).to.be.equal(this.behaviorV3.address);
expectEvent(this.receipt, 'Upgraded', { implementation: this.behaviorV3.address });
});

it('calls the \'migrate\' function and sends given value to the proxy', async function () {
const migratable = new MigratableMockV3(this.proxyAddress);

Expand Down Expand Up @@ -237,6 +278,12 @@ module.exports = function shouldBehaveLikeTransparentUpgradeableProxy (createPro
this.receipt = await this.proxy.changeAdmin(newAdmin, { from: proxyAdminAddress });
});

it('assigns new proxy admin', async function () {
const proxyAdminSlot = await getSlot(this.proxy, AdminSlot);
const newProxyAdmin = web3.utils.toChecksumAddress(proxyAdminSlot.substr(-40));
expect(newProxyAdmin).to.be.equal(anotherAccount);
});

it('emits an event', function () {
expectEvent(this.receipt, 'AdminChanged', {
previousAdmin: proxyAdminAddress,
Expand All @@ -262,20 +309,6 @@ module.exports = function shouldBehaveLikeTransparentUpgradeableProxy (createPro
});
});

describe('storage', function () {
it('should store the implementation address in specified location', async function () {
const implementationSlot = await getSlot(this.proxy, ImplementationSlot);
const implementationAddress = web3.utils.toChecksumAddress(implementationSlot.substr(-40));
expect(implementationAddress).to.be.equal(this.implementationV0);
});

it('should store the admin proxy in specified location', async function () {
const proxyAdminSlot = await getSlot(this.proxy, AdminSlot);
const proxyAdminAddress = web3.utils.toChecksumAddress(proxyAdminSlot.substr(-40));
expect(proxyAdminAddress).to.be.equal(proxyAdminAddress);
});
});

describe('transparent proxy', function () {
beforeEach('creating proxy', async function () {
const initializeData = Buffer.from('');
Expand Down