Skip to content

Commit

Permalink
Merge branch 'gasreport' into testing/gasreport/erc721
Browse files Browse the repository at this point in the history
  • Loading branch information
Amxx committed Jul 6, 2022
2 parents ffae7c2 + 9776c32 commit e53643b
Show file tree
Hide file tree
Showing 11 changed files with 2,098 additions and 1,213 deletions.
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"indent": ["error", 2],
"linebreak-style": ["error", "unix"],
"max-len": ["error", 120, 2],
"no-control-regex": "off",
"no-debugger": "off",
"no-dupe-args": "error",
"no-dupe-keys": "error",
Expand Down
37 changes: 37 additions & 0 deletions .github/workflows/checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,40 @@ jobs:
- name: Set up environment
uses: ./.github/actions/setup
- uses: crytic/slither-action@v0.1.1

gas:
if: github.event_name == 'pull_request'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up environment
uses: ./.github/actions/setup
- name: 'Download reference report'
run: |
RUN_ID=`gh run list --repo ${{ github.repository }} --branch ${{ github.base_ref }} --workflow ${{ github.workflow }} --json databaseId --jq '.[0].databaseId'`
gh run download ${RUN_ID} --repo ${{ github.repository }} -n gasreport
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: 'Generate new report'
run: npm run test
env:
GAS: true
GAS_REPORT: HEAD.gasreport.log~
- name: 'Compare reports'
run: node scripts/checks/compareGasReports.js HEAD.gasreport.log~ ${{ github.base_ref }}.gasreport.log

gasCache:
if: github.event_name != 'pull_request'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up environment
uses: ./.github/actions/setup
- run: npm run test
env:
GAS: true
GAS_REPORT: ${{ github.ref_name }}.gasreport.log
- uses: actions/upload-artifact@v3
with:
name: gasreport
path: ${{ github.ref_name }}.gasreport.log
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
* `Address`: optimize `functionCall` by calling `functionCallWithValue` directly. ([#3468](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3468))
* `Address`: optimize `functionCall` functions by checking contract size only if there is no returned data. ([#3469](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3469))
* `GovernorCompatibilityBravo`: remove unused `using` statements ([#3506](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3506))
* `ERC20`: optimize `_transfer`, `_mint` and `_burn` by using `unchecked` arithmetic when possible. ([#3513](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3513))
* `ERC721`: optimize transfers by making approval clearing implicit instead of emitting an event. ([#3481](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3481))

### Compatibility Note

ERC-721 integrators that interpret contract state from events should make sure that they implement the clearing of approval that is implicit in every transfer according to the EIP. Previous versions of OpenZeppellin Contracts emitted an explicit `Approval` event even though it was not required by the specification, and this is no longer the case.

## 4.7.0 (2022-06-29)

Expand Down
12 changes: 9 additions & 3 deletions contracts/token/ERC20/ERC20.sol
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,10 @@ contract ERC20 is Context, IERC20, IERC20Metadata {
require(fromBalance >= amount, "ERC20: transfer amount exceeds balance");
unchecked {
_balances[from] = fromBalance - amount;
// Overflow not possible: the sum of all balances is capped by totalSupply, and the sum is preserved by
// decrementing then incrementing.
_balances[to] += amount;
}
_balances[to] += amount;

emit Transfer(from, to, amount);

Expand All @@ -260,7 +262,10 @@ contract ERC20 is Context, IERC20, IERC20Metadata {
_beforeTokenTransfer(address(0), account, amount);

_totalSupply += amount;
_balances[account] += amount;
unchecked {
// Overflow not possible: balance + amount is at most totalSupply + amount, which is checked above.
_balances[account] += amount;
}
emit Transfer(address(0), account, amount);

_afterTokenTransfer(address(0), account, amount);
Expand All @@ -286,8 +291,9 @@ contract ERC20 is Context, IERC20, IERC20Metadata {
require(accountBalance >= amount, "ERC20: burn amount exceeds balance");
unchecked {
_balances[account] = accountBalance - amount;
// Overflow not possible: amount <= accountBalance <= totalSupply.
_totalSupply -= amount;
}
_totalSupply -= amount;

emit Transfer(account, address(0), amount);

Expand Down
2 changes: 1 addition & 1 deletion contracts/token/ERC721/ERC721.sol
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ contract ERC721 is Context, ERC165, IERC721, IERC721Metadata {
_beforeTokenTransfer(from, to, tokenId);

// Clear approvals from the previous owner
_approve(address(0), tokenId);
delete _tokenApprovals[tokenId];

unchecked {
// `_balances[from]` cannot overflow for the same reason as described in `_burn`:
Expand Down
6 changes: 3 additions & 3 deletions contracts/utils/math/Math.sol
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ library Math {
}

/**
* @dev Returns the square root of a number. It the number is not a perfect square, the value is rounded down.
* @dev Returns the square root of a number. If the number is not a perfect square, the value is rounded down.
*
* Inspired by Henry S. Warren, Jr.'s "Hacker's Delight" (Chapter 11).
*/
Expand All @@ -165,8 +165,8 @@ library Math {
// `msb(a) <= a < 2*msb(a)`.
// We also know that `k`, the position of the most significant bit, is such that `msb(a) = 2**k`.
// This gives `2**k < a <= 2**(k+1)` → `2**(k/2) <= sqrt(a) < 2 ** (k/2+1)`.
// Using an algorithm similar to the msb conmputation, we are able to compute `result = 2**(k/2)` which is a
// good first aproximation of `sqrt(a)` with at least 1 correct bit.
// Using an algorithm similar to the msb computation, we are able to compute `result = 2**(k/2)` which is a
// good first approximation of `sqrt(a)` with at least 1 correct bit.
uint256 result = 1;
uint256 x = a;
if (x >> 128 > 0) {
Expand Down
16 changes: 9 additions & 7 deletions hardhat.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ const path = require('path');
const argv = require('yargs/yargs')()
.env('')
.options({
ci: {
type: 'boolean',
default: false,
},
coverage: {
type: 'boolean',
default: false,
Expand All @@ -24,6 +20,12 @@ const argv = require('yargs/yargs')()
type: 'boolean',
default: false,
},
gasReport: {
alias: 'enableGasReportPath',
type: 'string',
implies: 'gas',
default: undefined,
},
mode: {
alias: 'compileMode',
type: 'string',
Expand All @@ -49,15 +51,15 @@ const argv = require('yargs/yargs')()

require('@nomiclabs/hardhat-truffle5');

if (argv.enableGasReport) {
if (argv.gas) {
require('hardhat-gas-reporter');
}

for (const f of fs.readdirSync(path.join(__dirname, 'hardhat'))) {
require(path.join(__dirname, 'hardhat', f));
}

const withOptimizations = argv.enableGasReport || argv.compileMode === 'production';
const withOptimizations = argv.gas || argv.compileMode === 'production';

/**
* @type import('hardhat/config').HardhatUserConfig
Expand All @@ -81,7 +83,7 @@ module.exports = {
},
gasReporter: {
currency: 'USD',
outputFile: argv.ci ? 'gas-report.txt' : undefined,
outputFile: argv.gasReport,
coinmarketcap: argv.coinmarketcap,
},
};
Expand Down

0 comments on commit e53643b

Please sign in to comment.