Remove code in preparation for v5.0 (#4258)

Co-authored-by: Ernesto García <ernestognw@gmail.com>
Co-authored-by: Francisco <fg@frang.io>
This commit is contained in:
Hadrien Croubois
2023-05-19 22:48:05 +02:00
committed by GitHub
parent 8de6eba8a3
commit 0f10efe232
125 changed files with 701 additions and 7390 deletions

View File

@ -1,5 +1,5 @@
const format = require('../format-lines');
const { OPTS, LEGACY_OPTS } = require('./Checkpoints.opts.js');
const { OPTS } = require('./Checkpoints.opts.js');
// TEMPLATE
const header = `\
@ -19,7 +19,7 @@ import "./math/SafeCast.sol";
*/
`;
const types = opts => `\
const template = opts => `\
struct ${opts.historyTypeName} {
${opts.checkpointTypeName}[] ${opts.checkpointFieldName};
}
@ -28,10 +28,7 @@ struct ${opts.checkpointTypeName} {
${opts.keyTypeName} ${opts.keyFieldName};
${opts.valueTypeName} ${opts.valueFieldName};
}
`;
/* eslint-disable max-len */
const operations = opts => `\
/**
* @dev Pushes a (\`key\`, \`value\`) pair into a ${opts.historyTypeName} so that it is stored as the checkpoint.
*
@ -87,77 +84,7 @@ function upperLookupRecent(${opts.historyTypeName} storage self, ${opts.keyTypeN
return pos == 0 ? 0 : _unsafeAccess(self.${opts.checkpointFieldName}, pos - 1).${opts.valueFieldName};
}
`;
const legacyOperations = opts => `\
/**
* @dev Returns the value at a given block number. If a checkpoint is not available at that block, the closest one
* before it is returned, or zero otherwise. Because the number returned corresponds to that at the end of the
* block, the requested block number must be in the past, excluding the current block.
*/
function getAtBlock(${opts.historyTypeName} storage self, uint256 blockNumber) internal view returns (uint256) {
require(blockNumber < block.number, "Checkpoints: block not yet mined");
uint32 key = SafeCast.toUint32(blockNumber);
uint256 len = self.${opts.checkpointFieldName}.length;
uint256 pos = _upperBinaryLookup(self.${opts.checkpointFieldName}, key, 0, len);
return pos == 0 ? 0 : _unsafeAccess(self.${opts.checkpointFieldName}, pos - 1).${opts.valueFieldName};
}
/**
* @dev Returns the value at a given block number. If a checkpoint is not available at that block, the closest one
* before it is returned, or zero otherwise. Similar to {upperLookup} but optimized for the case when the searched
* checkpoint is probably "recent", defined as being among the last sqrt(N) checkpoints where N is the number of
* checkpoints.
*/
function getAtProbablyRecentBlock(${opts.historyTypeName} storage self, uint256 blockNumber) internal view returns (uint256) {
require(blockNumber < block.number, "Checkpoints: block not yet mined");
uint32 key = SafeCast.toUint32(blockNumber);
uint256 len = self.${opts.checkpointFieldName}.length;
uint256 low = 0;
uint256 high = len;
if (len > 5) {
uint256 mid = len - Math.sqrt(len);
if (key < _unsafeAccess(self.${opts.checkpointFieldName}, mid)._blockNumber) {
high = mid;
} else {
low = mid + 1;
}
}
uint256 pos = _upperBinaryLookup(self.${opts.checkpointFieldName}, key, low, high);
return pos == 0 ? 0 : _unsafeAccess(self.${opts.checkpointFieldName}, pos - 1).${opts.valueFieldName};
}
/**
* @dev Pushes a value onto a History so that it is stored as the checkpoint for the current block.
*
* Returns previous value and new value.
*/
function push(${opts.historyTypeName} storage self, uint256 value) internal returns (uint256, uint256) {
return _insert(self.${opts.checkpointFieldName}, SafeCast.toUint32(block.number), SafeCast.toUint224(value));
}
/**
* @dev Pushes a value onto a History, by updating the latest value using binary operation \`op\`. The new value will
* be set to \`op(latest, delta)\`.
*
* Returns previous value and new value.
*/
function push(
${opts.historyTypeName} storage self,
function(uint256, uint256) view returns (uint256) op,
uint256 delta
) internal returns (uint256, uint256) {
return push(self, op(latest(self), delta));
}
`;
const common = opts => `\
/**
* @dev Returns the value in the most recent checkpoint, or zero if there are no checkpoints.
*/
@ -299,13 +226,6 @@ function _unsafeAccess(${opts.checkpointTypeName}[] storage self, uint256 pos)
module.exports = format(
header.trimEnd(),
'library Checkpoints {',
[
// Legacy types & functions
types(LEGACY_OPTS),
legacyOperations(LEGACY_OPTS),
common(LEGACY_OPTS),
// New flavors
...OPTS.flatMap(opts => [types(opts), operations(opts), common(opts)]),
],
OPTS.flatMap(opts => template(opts)),
'}',
);

View File

@ -12,11 +12,6 @@ const defaultOpts = size => ({
});
module.exports = {
VALUE_SIZES,
OPTS: VALUE_SIZES.map(size => defaultOpts(size)),
LEGACY_OPTS: {
...defaultOpts(224),
historyTypeName: 'History',
checkpointTypeName: 'Checkpoint',
keyFieldName: '_blockNumber',
},
};

View File

@ -1,6 +1,6 @@
const format = require('../format-lines');
const { capitalize } = require('../../helpers');
const { OPTS, LEGACY_OPTS } = require('./Checkpoints.opts.js');
const { OPTS } = require('./Checkpoints.opts.js');
// TEMPLATE
const header = `\
@ -12,7 +12,7 @@ import "../../contracts/utils/math/SafeCast.sol";
`;
/* eslint-disable max-len */
const common = opts => `\
const template = opts => `\
using Checkpoints for Checkpoints.${opts.historyTypeName};
// Maximum gap between keys used during the fuzzing tests: the \`_prepareKeys\` function with make sure that
@ -52,9 +52,7 @@ function _assertLatestCheckpoint(
assertEq(_key, key);
assertEq(_value, value);
}
`;
const testTrace = opts => `\
// tests
function testPush(
${opts.keyTypeName}[] memory keys,
@ -88,7 +86,7 @@ function testPush(
${opts.keyTypeName} lastKey = keys[keys.length - 1];
if (lastKey > 0) {
pastKey = _bound${capitalize(opts.keyTypeName)}(pastKey, 0, lastKey - 1);
vm.expectRevert();
this.push(pastKey, values[keys.length % values.length]);
}
@ -141,116 +139,8 @@ function testLookup(
}
`;
const testHistory = opts => `\
// tests
function testPush(
${opts.keyTypeName}[] memory keys,
${opts.valueTypeName}[] memory values,
${opts.keyTypeName} pastKey
) public {
vm.assume(values.length > 0 && values.length <= keys.length);
_prepareKeys(keys, _KEY_MAX_GAP);
// initial state
assertEq(_ckpts.length(), 0);
assertEq(_ckpts.latest(), 0);
_assertLatestCheckpoint(false, 0, 0);
uint256 duplicates = 0;
for (uint256 i = 0; i < keys.length; ++i) {
${opts.keyTypeName} key = keys[i];
${opts.valueTypeName} value = values[i % values.length];
if (i > 0 && key == keys[i - 1]) ++duplicates;
// push
vm.roll(key);
_ckpts.push(value);
// check length & latest
assertEq(_ckpts.length(), i + 1 - duplicates);
assertEq(_ckpts.latest(), value);
_assertLatestCheckpoint(true, key, value);
}
// Can't push any key in the past
if (keys.length > 0) {
${opts.keyTypeName} lastKey = keys[keys.length - 1];
if (lastKey > 0) {
pastKey = _bound${capitalize(opts.keyTypeName)}(pastKey, 0, lastKey - 1);
vm.roll(pastKey);
vm.expectRevert();
this.push(values[keys.length % values.length]);
}
}
}
// used to test reverts
function push(${opts.valueTypeName} value) external {
_ckpts.push(value);
}
function testLookup(
${opts.keyTypeName}[] memory keys,
${opts.valueTypeName}[] memory values,
${opts.keyTypeName} lookup
) public {
vm.assume(keys.length > 0);
vm.assume(values.length > 0 && values.length <= keys.length);
_prepareKeys(keys, _KEY_MAX_GAP);
${opts.keyTypeName} lastKey = keys[keys.length - 1];
vm.assume(lastKey > 0);
lookup = _bound${capitalize(opts.keyTypeName)}(lookup, 0, lastKey - 1);
${opts.valueTypeName} upper = 0;
for (uint256 i = 0; i < keys.length; ++i) {
${opts.keyTypeName} key = keys[i];
${opts.valueTypeName} value = values[i % values.length];
// push
vm.roll(key);
_ckpts.push(value);
// track expected result of lookups
if (key <= lookup) {
upper = value;
}
}
// check lookup
assertEq(_ckpts.getAtBlock(lookup), upper);
assertEq(_ckpts.getAtProbablyRecentBlock(lookup), upper);
vm.expectRevert(); this.getAtBlock(lastKey);
vm.expectRevert(); this.getAtBlock(lastKey + 1);
vm.expectRevert(); this.getAtProbablyRecentBlock(lastKey);
vm.expectRevert(); this.getAtProbablyRecentBlock(lastKey + 1);
}
// used to test reverts
function getAtBlock(${opts.keyTypeName} key) external view {
_ckpts.getAtBlock(key);
}
// used to test reverts
function getAtProbablyRecentBlock(${opts.keyTypeName} key) external view {
_ckpts.getAtProbablyRecentBlock(key);
}
`;
/* eslint-enable max-len */
// GENERATE
module.exports = format(
header,
// HISTORY
`contract Checkpoints${LEGACY_OPTS.historyTypeName}Test is Test {`,
[common(LEGACY_OPTS), testHistory(LEGACY_OPTS)],
'}',
// TRACEXXX
...OPTS.flatMap(opts => [
`contract Checkpoints${opts.historyTypeName}Test is Test {`,
[common(opts), testTrace(opts)],
'}',
]),
...OPTS.flatMap(opts => [`contract Checkpoints${opts.historyTypeName}Test is Test {`, [template(opts)], '}']),
);

View File

@ -153,22 +153,6 @@ function get(Bytes32ToBytes32Map storage map, bytes32 key) internal view returns
return value;
}
/**
* @dev Same as {get}, with a custom error message when \`key\` is not in the map.
*
* CAUTION: This function is deprecated because it requires allocating memory for the error
* message unnecessarily. For custom revert reasons use {tryGet}.
*/
function get(
Bytes32ToBytes32Map storage map,
bytes32 key,
string memory errorMessage
) internal view returns (bytes32) {
bytes32 value = map._values[key];
require(value != 0 || contains(map, key), errorMessage);
return value;
}
/**
* @dev Return the an array containing all the keys
*
@ -261,20 +245,6 @@ function get(${name} storage map, ${keyType} key) internal view returns (${value
return ${fromBytes32(valueType, `get(map._inner, ${toBytes32(keyType, 'key')})`)};
}
/**
* @dev Same as {get}, with a custom error message when \`key\` is not in the map.
*
* CAUTION: This function is deprecated because it requires allocating memory for the error
* message unnecessarily. For custom revert reasons use {tryGet}.
*/
function get(
${name} storage map,
${keyType} key,
string memory errorMessage
) internal view returns (${valueType}) {
return ${fromBytes32(valueType, `get(map._inner, ${toBytes32(keyType, 'key')}, errorMessage)`)};
}
/**
* @dev Return the an array containing all the keys
*

View File

@ -74,9 +74,6 @@ pragma solidity ^0.8.0;
*
* Using this library instead of the unchecked operations eliminates an entire
* class of bugs, so it's recommended to use it always.
*
* Can be combined with {SafeMath} and {SignedSafeMath} to extend it to smaller types, by performing
* all math on \`uint256\` and \`int256\` and then downcasting.
*/
`;

View File

@ -159,19 +159,6 @@ index 55e70b17..ceefb984 100644
},
"keywords": [
"solidity",
diff --git a/contracts/security/PullPayment.sol b/contracts/security/PullPayment.sol
index 65b4980f..f336592e 100644
--- a/contracts/security/PullPayment.sol
+++ b/contracts/security/PullPayment.sol
@@ -22,6 +22,8 @@ import "../utils/escrow/Escrow.sol";
* To use, derive from the `PullPayment` contract, and use {_asyncTransfer}
* instead of Solidity's `transfer` function. Payees can query their due
* payments with {payments}, and retrieve them with {withdrawPayments}.
+ *
+ * @custom:storage-size 51
*/
abstract contract PullPayment {
Escrow private immutable _escrow;
diff --git a/contracts/token/ERC20/extensions/ERC20Capped.sol b/contracts/token/ERC20/extensions/ERC20Capped.sol
index 16f830d1..9ef98148 100644
--- a/contracts/token/ERC20/extensions/ERC20Capped.sol