upgradeable.patch 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md
  2. deleted file mode 100644
  3. index 2797a088..00000000
  4. --- a/.github/ISSUE_TEMPLATE/bug_report.md
  5. +++ /dev/null
  6. @@ -1,21 +0,0 @@
  7. ----
  8. -name: Bug report
  9. -about: Report a bug in OpenZeppelin Contracts
  10. -
  11. ----
  12. -
  13. -<!-- Briefly describe the issue you're experiencing. Tell us what you were trying to do and what happened instead. -->
  14. -
  15. -<!-- Remember, this is not a place to ask for help debugging code. For that, we welcome you in the OpenZeppelin Community Forum: https://forum.openzeppelin.com/. -->
  16. -
  17. -**💻 Environment**
  18. -
  19. -<!-- Tell us what version of OpenZeppelin Contracts you're using, and how you're using it: Truffle, Remix, etc. -->
  20. -
  21. -**📝 Details**
  22. -
  23. -<!-- Describe the problem you have been experiencing in more detail. Include as much information as you think is relevant. Keep in mind that transactions can fail for many reasons; context is key here. -->
  24. -
  25. -**🔢 Code to reproduce bug**
  26. -
  27. -<!-- We will be able to better help if you provide a minimal example that triggers the bug. -->
  28. diff --git a/.github/ISSUE_TEMPLATE/config.yml b/.github/ISSUE_TEMPLATE/config.yml
  29. index 4018cef2..d343a53d 100644
  30. --- a/.github/ISSUE_TEMPLATE/config.yml
  31. +++ b/.github/ISSUE_TEMPLATE/config.yml
  32. @@ -1,4 +1,8 @@
  33. +blank_issues_enabled: false
  34. contact_links:
  35. + - name: Bug Reports & Feature Requests
  36. + url: https://github.com/OpenZeppelin/openzeppelin-contracts/issues/new/choose
  37. + about: Visit the OpenZeppelin Contracts repository
  38. - name: Questions & Support Requests
  39. url: https://forum.openzeppelin.com/c/support/contracts/18
  40. about: Ask in the OpenZeppelin Forum
  41. diff --git a/.github/ISSUE_TEMPLATE/feature_request.md b/.github/ISSUE_TEMPLATE/feature_request.md
  42. deleted file mode 100644
  43. index ff596b0c..00000000
  44. --- a/.github/ISSUE_TEMPLATE/feature_request.md
  45. +++ /dev/null
  46. @@ -1,14 +0,0 @@
  47. ----
  48. -name: Feature request
  49. -about: Suggest an idea for OpenZeppelin Contracts
  50. -
  51. ----
  52. -
  53. -**🧐 Motivation**
  54. -<!-- Is your feature request related to a specific problem? Is it just a crazy idea? Tell us about it! -->
  55. -
  56. -**📝 Details**
  57. -<!-- Please describe your feature request in detail. -->
  58. -
  59. -<!-- Make sure that you have reviewed the OpenZeppelin Contracts Contributor Guidelines. -->
  60. -<!-- https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/CONTRIBUTING.md -->
  61. diff --git a/README.md b/README.md
  62. index 27627f43..e42a66e8 100644
  63. --- a/README.md
  64. +++ b/README.md
  65. @@ -19,6 +19,9 @@
  66. :building_construction: **Want to scale your decentralized application?** Check out [OpenZeppelin Defender](https://openzeppelin.com/defender) — a secure platform for automating and monitoring your operations.
  67. +> **Note**
  68. +> You are looking at the upgradeable variant of OpenZeppelin Contracts. Be sure to review the documentation on [Using OpenZeppelin Contracts with Upgrades](https://docs.openzeppelin.com/contracts/4.x/upgradeable).
  69. +
  70. ## Overview
  71. ### Installation
  72. @@ -26,7 +29,7 @@
  73. #### Hardhat, Truffle (npm)
  74. ```
  75. -$ npm install @openzeppelin/contracts
  76. +$ npm install @openzeppelin/contracts-upgradeable
  77. ```
  78. OpenZeppelin Contracts features a [stable API](https://docs.openzeppelin.com/contracts/releases-stability#api-stability), which means that your contracts won't break unexpectedly when upgrading to a newer minor version.
  79. @@ -38,7 +41,7 @@ OpenZeppelin Contracts features a [stable API](https://docs.openzeppelin.com/con
  80. > **Warning** Foundry installs the latest version initially, but subsequent `forge update` commands will use the `master` branch.
  81. ```
  82. -$ forge install OpenZeppelin/openzeppelin-contracts
  83. +$ forge install OpenZeppelin/openzeppelin-contracts-upgradeable
  84. ```
  85. ### Usage
  86. @@ -48,10 +51,11 @@ Once installed, you can use the contracts in the library by importing them:
  87. ```solidity
  88. pragma solidity ^0.8.19;
  89. -import {ERC721} from "@openzeppelin/contracts/token/ERC721/ERC721.sol";
  90. +import {ERC721Upgradeable} from "@openzeppelin/contracts-upgradeable/token/ERC721/ERC721Upgradeable.sol";
  91. -contract MyCollectible is ERC721 {
  92. - constructor() ERC721("MyCollectible", "MCO") {
  93. +contract MyCollectible is ERC721Upgradeable {
  94. + function initialize() initializer public {
  95. + __ERC721_init("MyCollectible", "MCO");
  96. }
  97. }
  98. ```
  99. diff --git a/contracts/finance/VestingWallet.sol b/contracts/finance/VestingWallet.sol
  100. index f776a7ca..d7a7e0b0 100644
  101. --- a/contracts/finance/VestingWallet.sol
  102. +++ b/contracts/finance/VestingWallet.sol
  103. @@ -19,6 +19,8 @@ import {Context} from "../utils/Context.sol";
  104. *
  105. * By setting the duration to 0, one can configure this contract to behave like an asset timelock that hold tokens for
  106. * a beneficiary until a specified time.
  107. + *
  108. + * @custom:storage-size 52
  109. */
  110. contract VestingWallet is Context {
  111. event EtherReleased(uint256 amount);
  112. diff --git a/contracts/governance/extensions/GovernorVotes.sol b/contracts/governance/extensions/GovernorVotes.sol
  113. index bb14d7fd..0785ebbd 100644
  114. --- a/contracts/governance/extensions/GovernorVotes.sol
  115. +++ b/contracts/governance/extensions/GovernorVotes.sol
  116. @@ -12,6 +12,8 @@ import {SafeCast} from "../../utils/math/SafeCast.sol";
  117. * @dev Extension of {Governor} for voting weight extraction from an {ERC20Votes} token, or since v4.5 an {ERC721Votes} token.
  118. *
  119. * _Available since v4.3._
  120. + *
  121. + * @custom:storage-size 51
  122. */
  123. abstract contract GovernorVotes is Governor {
  124. IERC5805 public immutable token;
  125. diff --git a/contracts/package.json b/contracts/package.json
  126. index df141192..1cf90ad1 100644
  127. --- a/contracts/package.json
  128. +++ b/contracts/package.json
  129. @@ -1,5 +1,5 @@
  130. {
  131. - "name": "@openzeppelin/contracts",
  132. + "name": "@openzeppelin/contracts-upgradeable",
  133. "description": "Secure Smart Contract library for Solidity",
  134. "version": "4.9.2",
  135. "files": [
  136. @@ -13,7 +13,7 @@
  137. },
  138. "repository": {
  139. "type": "git",
  140. - "url": "https://github.com/OpenZeppelin/openzeppelin-contracts.git"
  141. + "url": "https://github.com/OpenZeppelin/openzeppelin-contracts-upgradeable.git"
  142. },
  143. "keywords": [
  144. "solidity",
  145. diff --git a/contracts/token/ERC20/extensions/ERC20Capped.sol b/contracts/token/ERC20/extensions/ERC20Capped.sol
  146. index 98ec7144..4992115b 100644
  147. --- a/contracts/token/ERC20/extensions/ERC20Capped.sol
  148. +++ b/contracts/token/ERC20/extensions/ERC20Capped.sol
  149. @@ -7,6 +7,8 @@ import {ERC20} from "../ERC20.sol";
  150. /**
  151. * @dev Extension of {ERC20} that adds a cap to the supply of tokens.
  152. + *
  153. + * @custom:storage-size 51
  154. */
  155. abstract contract ERC20Capped is ERC20 {
  156. uint256 private immutable _cap;
  157. diff --git a/contracts/token/ERC20/extensions/ERC20Permit.sol b/contracts/token/ERC20/extensions/ERC20Permit.sol
  158. index 8778f4ba..825d4e16 100644
  159. --- a/contracts/token/ERC20/extensions/ERC20Permit.sol
  160. +++ b/contracts/token/ERC20/extensions/ERC20Permit.sol
  161. @@ -18,6 +18,8 @@ import {Nonces} from "../../../utils/Nonces.sol";
  162. * need to send a transaction, and thus is not required to hold Ether at all.
  163. *
  164. * _Available since v3.4._
  165. + *
  166. + * @custom:storage-size 51
  167. */
  168. abstract contract ERC20Permit is ERC20, IERC20Permit, EIP712, Nonces {
  169. // solhint-disable-next-line var-name-mixedcase
  170. diff --git a/contracts/token/ERC20/extensions/ERC20Wrapper.sol b/contracts/token/ERC20/extensions/ERC20Wrapper.sol
  171. index 2cbff622..97f43d7a 100644
  172. --- a/contracts/token/ERC20/extensions/ERC20Wrapper.sol
  173. +++ b/contracts/token/ERC20/extensions/ERC20Wrapper.sol
  174. @@ -14,6 +14,8 @@ import {SafeERC20} from "../utils/SafeERC20.sol";
  175. * wrapping of an existing "basic" ERC20 into a governance token.
  176. *
  177. * _Available since v4.2._
  178. + *
  179. + * @custom:storage-size 51
  180. */
  181. abstract contract ERC20Wrapper is ERC20 {
  182. IERC20 private immutable _underlying;
  183. diff --git a/contracts/utils/cryptography/EIP712.sol b/contracts/utils/cryptography/EIP712.sol
  184. index d94e956a..86bb5713 100644
  185. --- a/contracts/utils/cryptography/EIP712.sol
  186. +++ b/contracts/utils/cryptography/EIP712.sol
  187. @@ -4,7 +4,6 @@
  188. pragma solidity ^0.8.19;
  189. import {ECDSA} from "./ECDSA.sol";
  190. -import {ShortStrings, ShortString} from "../ShortStrings.sol";
  191. import {IERC5267} from "../../interfaces/IERC5267.sol";
  192. /**
  193. @@ -30,27 +29,19 @@ import {IERC5267} from "../../interfaces/IERC5267.sol";
  194. *
  195. * _Available since v3.4._
  196. *
  197. - * @custom:oz-upgrades-unsafe-allow state-variable-immutable state-variable-assignment
  198. + * @custom:storage-size 52
  199. */
  200. abstract contract EIP712 is IERC5267 {
  201. - using ShortStrings for *;
  202. -
  203. bytes32 private constant _TYPE_HASH =
  204. keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)");
  205. - // Cache the domain separator as an immutable value, but also store the chain id that it corresponds to, in order to
  206. - // invalidate the cached domain separator if the chain id changes.
  207. - bytes32 private immutable _cachedDomainSeparator;
  208. - uint256 private immutable _cachedChainId;
  209. - address private immutable _cachedThis;
  210. -
  211. + /// @custom:oz-renamed-from _HASHED_NAME
  212. bytes32 private immutable _hashedName;
  213. + /// @custom:oz-renamed-from _HASHED_VERSION
  214. bytes32 private immutable _hashedVersion;
  215. - ShortString private immutable _name;
  216. - ShortString private immutable _version;
  217. - string private _nameFallback;
  218. - string private _versionFallback;
  219. + string private _name;
  220. + string private _version;
  221. /**
  222. * @dev Initializes the domain separator and parameter caches.
  223. @@ -65,29 +56,23 @@ abstract contract EIP712 is IERC5267 {
  224. * contract upgrade].
  225. */
  226. constructor(string memory name, string memory version) {
  227. - _name = name.toShortStringWithFallback(_nameFallback);
  228. - _version = version.toShortStringWithFallback(_versionFallback);
  229. - _hashedName = keccak256(bytes(name));
  230. - _hashedVersion = keccak256(bytes(version));
  231. -
  232. - _cachedChainId = block.chainid;
  233. - _cachedDomainSeparator = _buildDomainSeparator();
  234. - _cachedThis = address(this);
  235. + _name = name;
  236. + _version = version;
  237. +
  238. + // Reset prior values in storage if upgrading
  239. + _hashedName = 0;
  240. + _hashedVersion = 0;
  241. }
  242. /**
  243. * @dev Returns the domain separator for the current chain.
  244. */
  245. function _domainSeparatorV4() internal view returns (bytes32) {
  246. - if (address(this) == _cachedThis && block.chainid == _cachedChainId) {
  247. - return _cachedDomainSeparator;
  248. - } else {
  249. - return _buildDomainSeparator();
  250. - }
  251. + return _buildDomainSeparator();
  252. }
  253. function _buildDomainSeparator() private view returns (bytes32) {
  254. - return keccak256(abi.encode(_TYPE_HASH, _hashedName, _hashedVersion, block.chainid, address(this)));
  255. + return keccak256(abi.encode(_TYPE_HASH, _EIP712NameHash(), _EIP712VersionHash(), block.chainid, address(this)));
  256. }
  257. /**
  258. @@ -128,6 +113,10 @@ abstract contract EIP712 is IERC5267 {
  259. uint256[] memory extensions
  260. )
  261. {
  262. + // If the hashed name and version in storage are non-zero, the contract hasn't been properly initialized
  263. + // and the EIP712 domain is not reliable, as it will be missing name and version.
  264. + require(_hashedName == 0 && _hashedVersion == 0, "EIP712: Uninitialized");
  265. +
  266. return (
  267. hex"0f", // 01111
  268. _EIP712Name(),
  269. @@ -142,26 +131,62 @@ abstract contract EIP712 is IERC5267 {
  270. /**
  271. * @dev The name parameter for the EIP712 domain.
  272. *
  273. - * NOTE: By default this function reads _name which is an immutable value.
  274. - * It only reads from storage if necessary (in case the value is too large to fit in a ShortString).
  275. - *
  276. - * _Available since v5.0._
  277. + * NOTE: This function reads from storage by default, but can be redefined to return a constant value if gas costs
  278. + * are a concern.
  279. */
  280. - // solhint-disable-next-line func-name-mixedcase
  281. - function _EIP712Name() internal view returns (string memory) {
  282. - return _name.toStringWithFallback(_nameFallback);
  283. + function _EIP712Name() internal view virtual returns (string memory) {
  284. + return _name;
  285. }
  286. /**
  287. * @dev The version parameter for the EIP712 domain.
  288. *
  289. - * NOTE: By default this function reads _version which is an immutable value.
  290. - * It only reads from storage if necessary (in case the value is too large to fit in a ShortString).
  291. + * NOTE: This function reads from storage by default, but can be redefined to return a constant value if gas costs
  292. + * are a concern.
  293. + */
  294. + function _EIP712Version() internal view virtual returns (string memory) {
  295. + return _version;
  296. + }
  297. +
  298. + /**
  299. + * @dev The hash of the name parameter for the EIP712 domain.
  300. + *
  301. + * NOTE: In previous versions this function was virtual. In this version you should override `_EIP712Name` instead.
  302. + */
  303. + function _EIP712NameHash() internal view returns (bytes32) {
  304. + string memory name = _EIP712Name();
  305. + if (bytes(name).length > 0) {
  306. + return keccak256(bytes(name));
  307. + } else {
  308. + // If the name is empty, the contract may have been upgraded without initializing the new storage.
  309. + // We return the name hash in storage if non-zero, otherwise we assume the name is empty by design.
  310. + bytes32 hashedName = _hashedName;
  311. + if (hashedName != 0) {
  312. + return hashedName;
  313. + } else {
  314. + return keccak256("");
  315. + }
  316. + }
  317. + }
  318. +
  319. + /**
  320. + * @dev The hash of the version parameter for the EIP712 domain.
  321. *
  322. - * _Available since v5.0._
  323. + * NOTE: In previous versions this function was virtual. In this version you should override `_EIP712Version` instead.
  324. */
  325. - // solhint-disable-next-line func-name-mixedcase
  326. - function _EIP712Version() internal view returns (string memory) {
  327. - return _version.toStringWithFallback(_versionFallback);
  328. + function _EIP712VersionHash() internal view returns (bytes32) {
  329. + string memory version = _EIP712Version();
  330. + if (bytes(version).length > 0) {
  331. + return keccak256(bytes(version));
  332. + } else {
  333. + // If the version is empty, the contract may have been upgraded without initializing the new storage.
  334. + // We return the version hash in storage if non-zero, otherwise we assume the version is empty by design.
  335. + bytes32 hashedVersion = _hashedVersion;
  336. + if (hashedVersion != 0) {
  337. + return hashedVersion;
  338. + } else {
  339. + return keccak256("");
  340. + }
  341. + }
  342. }
  343. }
  344. diff --git a/package.json b/package.json
  345. index 9eae6732..b3a56366 100644
  346. --- a/package.json
  347. +++ b/package.json
  348. @@ -33,7 +33,7 @@
  349. },
  350. "repository": {
  351. "type": "git",
  352. - "url": "https://github.com/OpenZeppelin/openzeppelin-contracts.git"
  353. + "url": "https://github.com/OpenZeppelin/openzeppelin-contracts-upgradeable.git"
  354. },
  355. "keywords": [
  356. "solidity",
  357. diff --git a/test/utils/cryptography/EIP712.test.js b/test/utils/cryptography/EIP712.test.js
  358. index 7ea535b7..32e3a370 100644
  359. --- a/test/utils/cryptography/EIP712.test.js
  360. +++ b/test/utils/cryptography/EIP712.test.js
  361. @@ -47,26 +47,6 @@ contract('EIP712', function (accounts) {
  362. const rebuildDomain = await getDomain(this.eip712);
  363. expect(mapValues(rebuildDomain, String)).to.be.deep.equal(mapValues(this.domain, String));
  364. });
  365. -
  366. - if (shortOrLong === 'short') {
  367. - // Long strings are in storage, and the proxy will not be properly initialized unless
  368. - // the upgradeable contract variant is used and the initializer is invoked.
  369. -
  370. - it('adjusts when behind proxy', async function () {
  371. - const factory = await Clones.new();
  372. - const cloneReceipt = await factory.$clone(this.eip712.address);
  373. - const cloneAddress = cloneReceipt.logs.find(({ event }) => event === 'return$clone').args.instance;
  374. - const clone = new EIP712Verifier(cloneAddress);
  375. -
  376. - const cloneDomain = { ...this.domain, verifyingContract: clone.address };
  377. -
  378. - const reportedDomain = await getDomain(clone);
  379. - expect(mapValues(reportedDomain, String)).to.be.deep.equal(mapValues(cloneDomain, String));
  380. -
  381. - const expectedSeparator = await domainSeparator(cloneDomain);
  382. - expect(await clone.$_domainSeparatorV4()).to.equal(expectedSeparator);
  383. - });
  384. - }
  385. });
  386. it('hash digest', async function () {