Skip to content

Commit 051cc9e

Browse files
Amxxfrangio
andauthored
Revert unwanted breaking change in the Clones library (OpenZeppelin#3456)
Co-authored-by: Francisco Giordano <frangio.1@gmail.com>
1 parent 54ce38c commit 051cc9e

File tree

5 files changed

+43
-26
lines changed

5 files changed

+43
-26
lines changed

CHANGELOG.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
## Unreleased
44

5-
* `Clones`: optimize clone creation ([#3329](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3329))
65
* `TimelockController`: Migrate `_call` to `_execute` and allow inheritance and overriding similar to `Governor`. ([#3317](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3317))
76
* `CrossChainEnabledPolygonChild`: replace the `require` statement with the custom error `NotCrossChainCall`. ([#3380](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3380))
87
* `ERC20FlashMint`: Add customizable flash fee receiver. ([#3327](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3327))

contracts/proxy/Clones.sol

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ library Clones {
2626
/// @solidity memory-safe-assembly
2727
assembly {
2828
let ptr := mload(0x40)
29-
mstore(ptr, 0x602d8060093d393df3363d3d373d3d3d363d7300000000000000000000000000)
30-
mstore(add(ptr, 0x13), shl(0x60, implementation))
31-
mstore(add(ptr, 0x27), 0x5af43d82803e903d91602b57fd5bf30000000000000000000000000000000000)
32-
instance := create(0, ptr, 0x36)
29+
mstore(ptr, 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000000000000000000000)
30+
mstore(add(ptr, 0x14), shl(0x60, implementation))
31+
mstore(add(ptr, 0x28), 0x5af43d82803e903d91602b57fd5bf30000000000000000000000000000000000)
32+
instance := create(0, ptr, 0x37)
3333
}
3434
require(instance != address(0), "ERC1167: create failed");
3535
}
@@ -45,10 +45,10 @@ library Clones {
4545
/// @solidity memory-safe-assembly
4646
assembly {
4747
let ptr := mload(0x40)
48-
mstore(ptr, 0x602d8060093d393df3363d3d373d3d3d363d7300000000000000000000000000)
49-
mstore(add(ptr, 0x13), shl(0x60, implementation))
50-
mstore(add(ptr, 0x27), 0x5af43d82803e903d91602b57fd5bf30000000000000000000000000000000000)
51-
instance := create2(0, ptr, 0x36, salt)
48+
mstore(ptr, 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000000000000000000000)
49+
mstore(add(ptr, 0x14), shl(0x60, implementation))
50+
mstore(add(ptr, 0x28), 0x5af43d82803e903d91602b57fd5bf30000000000000000000000000000000000)
51+
instance := create2(0, ptr, 0x37, salt)
5252
}
5353
require(instance != address(0), "ERC1167: create2 failed");
5454
}
@@ -64,13 +64,13 @@ library Clones {
6464
/// @solidity memory-safe-assembly
6565
assembly {
6666
let ptr := mload(0x40)
67-
mstore(ptr, 0x602d8060093d393df3363d3d373d3d3d363d7300000000000000000000000000)
68-
mstore(add(ptr, 0x13), shl(0x60, implementation))
69-
mstore(add(ptr, 0x27), 0x5af43d82803e903d91602b57fd5bf3ff00000000000000000000000000000000)
70-
mstore(add(ptr, 0x37), shl(0x60, deployer))
71-
mstore(add(ptr, 0x4b), salt)
72-
mstore(add(ptr, 0x6b), keccak256(ptr, 0x36))
73-
predicted := keccak256(add(ptr, 0x36), 0x55)
67+
mstore(ptr, 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000000000000000000000)
68+
mstore(add(ptr, 0x14), shl(0x60, implementation))
69+
mstore(add(ptr, 0x28), 0x5af43d82803e903d91602b57fd5bf3ff00000000000000000000000000000000)
70+
mstore(add(ptr, 0x38), shl(0x60, deployer))
71+
mstore(add(ptr, 0x4c), salt)
72+
mstore(add(ptr, 0x6c), keccak256(ptr, 0x37))
73+
predicted := keccak256(add(ptr, 0x37), 0x55)
7474
}
7575
}
7676

test/helpers/create2.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function computeCreate2Address (saltHex, bytecode, deployer) {
2+
return web3.utils.toChecksumAddress(`0x${web3.utils.sha3(`0x${[
3+
'ff',
4+
deployer,
5+
saltHex,
6+
web3.utils.soliditySha3(bytecode),
7+
].map(x => x.replace(/0x/, '')).join('')}`).slice(-40)}`);
8+
}
9+
10+
module.exports = {
11+
computeCreate2Address,
12+
};

test/proxy/Clones.test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
const { expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
2+
const { computeCreate2Address } = require('../helpers/create2');
3+
const { expect } = require('chai');
24

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

@@ -44,6 +46,19 @@ contract('Clones', function (accounts) {
4446
const salt = web3.utils.randomHex(32);
4547
const factory = await ClonesMock.new();
4648
const predicted = await factory.predictDeterministicAddress(implementation, salt);
49+
50+
const creationCode = [
51+
'0x3d602d80600a3d3981f3363d3d373d3d3d363d73',
52+
implementation.replace(/0x/, '').toLowerCase(),
53+
'5af43d82803e903d91602b57fd5bf3',
54+
].join('');
55+
56+
expect(computeCreate2Address(
57+
salt,
58+
creationCode,
59+
factory.address,
60+
)).to.be.equal(predicted);
61+
4762
expectEvent(
4863
await factory.cloneDeterministic(implementation, salt, '0x'),
4964
'NewInstance',

test/utils/Create2.test.js

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const { balance, BN, ether, expectRevert, send } = require('@openzeppelin/test-helpers');
2-
2+
const { computeCreate2Address } = require('../helpers/create2');
33
const { expect } = require('chai');
44

55
const Create2Impl = artifacts.require('Create2Impl');
@@ -90,12 +90,3 @@ contract('Create2', function (accounts) {
9090
});
9191
});
9292
});
93-
94-
function computeCreate2Address (saltHex, bytecode, deployer) {
95-
return web3.utils.toChecksumAddress(`0x${web3.utils.sha3(`0x${[
96-
'ff',
97-
deployer,
98-
saltHex,
99-
web3.utils.soliditySha3(bytecode),
100-
].map(x => x.replace(/0x/, '')).join('')}`).slice(-40)}`);
101-
}

0 commit comments

Comments
 (0)