Skip to content

Commit

Permalink
Revert unwanted breaking change in the Clones library (OpenZeppelin#3456
Browse files Browse the repository at this point in the history
)

Co-authored-by: Francisco Giordano <frangio.1@gmail.com>
  • Loading branch information
Amxx and frangio authored Jun 6, 2022
1 parent 54ce38c commit 051cc9e
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 26 deletions.
1 change: 0 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

## Unreleased

* `Clones`: optimize clone creation ([#3329](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3329))
* `TimelockController`: Migrate `_call` to `_execute` and allow inheritance and overriding similar to `Governor`. ([#3317](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3317))
* `CrossChainEnabledPolygonChild`: replace the `require` statement with the custom error `NotCrossChainCall`. ([#3380](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3380))
* `ERC20FlashMint`: Add customizable flash fee receiver. ([#3327](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3327))
Expand Down
30 changes: 15 additions & 15 deletions contracts/proxy/Clones.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ library Clones {
/// @solidity memory-safe-assembly
assembly {
let ptr := mload(0x40)
mstore(ptr, 0x602d8060093d393df3363d3d373d3d3d363d7300000000000000000000000000)
mstore(add(ptr, 0x13), shl(0x60, implementation))
mstore(add(ptr, 0x27), 0x5af43d82803e903d91602b57fd5bf30000000000000000000000000000000000)
instance := create(0, ptr, 0x36)
mstore(ptr, 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000000000000000000000)
mstore(add(ptr, 0x14), shl(0x60, implementation))
mstore(add(ptr, 0x28), 0x5af43d82803e903d91602b57fd5bf30000000000000000000000000000000000)
instance := create(0, ptr, 0x37)
}
require(instance != address(0), "ERC1167: create failed");
}
Expand All @@ -45,10 +45,10 @@ library Clones {
/// @solidity memory-safe-assembly
assembly {
let ptr := mload(0x40)
mstore(ptr, 0x602d8060093d393df3363d3d373d3d3d363d7300000000000000000000000000)
mstore(add(ptr, 0x13), shl(0x60, implementation))
mstore(add(ptr, 0x27), 0x5af43d82803e903d91602b57fd5bf30000000000000000000000000000000000)
instance := create2(0, ptr, 0x36, salt)
mstore(ptr, 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000000000000000000000)
mstore(add(ptr, 0x14), shl(0x60, implementation))
mstore(add(ptr, 0x28), 0x5af43d82803e903d91602b57fd5bf30000000000000000000000000000000000)
instance := create2(0, ptr, 0x37, salt)
}
require(instance != address(0), "ERC1167: create2 failed");
}
Expand All @@ -64,13 +64,13 @@ library Clones {
/// @solidity memory-safe-assembly
assembly {
let ptr := mload(0x40)
mstore(ptr, 0x602d8060093d393df3363d3d373d3d3d363d7300000000000000000000000000)
mstore(add(ptr, 0x13), shl(0x60, implementation))
mstore(add(ptr, 0x27), 0x5af43d82803e903d91602b57fd5bf3ff00000000000000000000000000000000)
mstore(add(ptr, 0x37), shl(0x60, deployer))
mstore(add(ptr, 0x4b), salt)
mstore(add(ptr, 0x6b), keccak256(ptr, 0x36))
predicted := keccak256(add(ptr, 0x36), 0x55)
mstore(ptr, 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000000000000000000000)
mstore(add(ptr, 0x14), shl(0x60, implementation))
mstore(add(ptr, 0x28), 0x5af43d82803e903d91602b57fd5bf3ff00000000000000000000000000000000)
mstore(add(ptr, 0x38), shl(0x60, deployer))
mstore(add(ptr, 0x4c), salt)
mstore(add(ptr, 0x6c), keccak256(ptr, 0x37))
predicted := keccak256(add(ptr, 0x37), 0x55)
}
}

Expand Down
12 changes: 12 additions & 0 deletions test/helpers/create2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function computeCreate2Address (saltHex, bytecode, deployer) {
return web3.utils.toChecksumAddress(`0x${web3.utils.sha3(`0x${[
'ff',
deployer,
saltHex,
web3.utils.soliditySha3(bytecode),
].map(x => x.replace(/0x/, '')).join('')}`).slice(-40)}`);
}

module.exports = {
computeCreate2Address,
};
15 changes: 15 additions & 0 deletions test/proxy/Clones.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const { expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
const { computeCreate2Address } = require('../helpers/create2');
const { expect } = require('chai');

const shouldBehaveLikeClone = require('./Clones.behaviour');

Expand Down Expand Up @@ -44,6 +46,19 @@ contract('Clones', function (accounts) {
const salt = web3.utils.randomHex(32);
const factory = await ClonesMock.new();
const predicted = await factory.predictDeterministicAddress(implementation, salt);

const creationCode = [
'0x3d602d80600a3d3981f3363d3d373d3d3d363d73',
implementation.replace(/0x/, '').toLowerCase(),
'5af43d82803e903d91602b57fd5bf3',
].join('');

expect(computeCreate2Address(
salt,
creationCode,
factory.address,
)).to.be.equal(predicted);

expectEvent(
await factory.cloneDeterministic(implementation, salt, '0x'),
'NewInstance',
Expand Down
11 changes: 1 addition & 10 deletions test/utils/Create2.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { balance, BN, ether, expectRevert, send } = require('@openzeppelin/test-helpers');

const { computeCreate2Address } = require('../helpers/create2');
const { expect } = require('chai');

const Create2Impl = artifacts.require('Create2Impl');
Expand Down Expand Up @@ -90,12 +90,3 @@ contract('Create2', function (accounts) {
});
});
});

function computeCreate2Address (saltHex, bytecode, deployer) {
return web3.utils.toChecksumAddress(`0x${web3.utils.sha3(`0x${[
'ff',
deployer,
saltHex,
web3.utils.soliditySha3(bytecode),
].map(x => x.replace(/0x/, '')).join('')}`).slice(-40)}`);
}

0 comments on commit 051cc9e

Please sign in to comment.