From e0068f38a4730f18d9046583482d1c7bc76883e7 Mon Sep 17 00:00:00 2001 From: roleengineer Date: Tue, 12 Nov 2024 14:13:32 +0300 Subject: [PATCH 1/9] (DiscountedBalances): optimized _updateBalance function --- src/circles/DiscountedBalances.sol | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/circles/DiscountedBalances.sol b/src/circles/DiscountedBalances.sol index e698608..d7e985a 100644 --- a/src/circles/DiscountedBalances.sol +++ b/src/circles/DiscountedBalances.sol @@ -91,10 +91,7 @@ contract DiscountedBalances is Demurrage { // revert CirclesDemurrageAmountExceedsMaxUint192(_account, _id, _balance, 0); revert CirclesErrorAddressUintArgs(_account, _id, 0x81); } - DiscountedBalance memory discountedBalance = discountedBalances[_id][_account]; - discountedBalance.balance = uint192(_balance); - discountedBalance.lastUpdatedDay = _day; - discountedBalances[_id][_account] = discountedBalance; + discountedBalances[_id][_account] = DiscountedBalance({balance: uint192(_balance), lastUpdatedDay: _day}); } /** From 18d1a933c9dce985a1dfde76afea80d80b677beb Mon Sep 17 00:00:00 2001 From: roleengineer Date: Tue, 12 Nov 2024 14:35:44 +0300 Subject: [PATCH 2/9] (erc1155): optimized via _allowanceCheck function --- src/circles/ERC1155.sol | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/circles/ERC1155.sol b/src/circles/ERC1155.sol index b0a2dba..9d6d248 100644 --- a/src/circles/ERC1155.sol +++ b/src/circles/ERC1155.sol @@ -114,10 +114,7 @@ abstract contract ERC1155 is DiscountedBalances, Context, ERC165, IERC1155, IERC * @dev See {IERC1155-safeTransferFrom}. */ function safeTransferFrom(address _from, address _to, uint256 _id, uint256 _value, bytes memory _data) public { - address sender = _msgSender(); - if (_from != sender && !isApprovedForAll(_from, sender)) { - revert ERC1155MissingApprovalForAll(sender, _from); - } + _allowanceCheck(_from); _safeTransferFrom(_from, _to, _id, _value, _data); } @@ -131,10 +128,7 @@ abstract contract ERC1155 is DiscountedBalances, Context, ERC165, IERC1155, IERC uint256[] memory _values, bytes memory _data ) public virtual { - address sender = _msgSender(); - if (_from != sender && !isApprovedForAll(_from, sender)) { - revert ERC1155MissingApprovalForAll(sender, _from); - } + _allowanceCheck(_from); _safeBatchTransferFrom(_from, _to, _ids, _values, _data); } @@ -216,6 +210,16 @@ abstract contract ERC1155 is DiscountedBalances, Context, ERC165, IERC1155, IERC _acceptanceCheck(from, to, ids, values, data); } + /** + * @dev do the ERC1155 token the sender allowance check + */ + function _allowanceCheck(address _from) internal view { + address sender = _msgSender(); + if (_from != sender && !isApprovedForAll(_from, sender)) { + revert ERC1155MissingApprovalForAll(sender, _from); + } + } + /** * @dev do the ERC1155 token acceptance check to the receiver */ From 860d147706361099bdf9a64c1f653ce9caf43eed Mon Sep 17 00:00:00 2001 From: roleengineer Date: Tue, 12 Nov 2024 14:50:51 +0300 Subject: [PATCH 3/9] (hub): optimized via wrapping repeating nameRegistry calls --- src/hub/Hub.sol | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/src/hub/Hub.sol b/src/hub/Hub.sol index 14e6a6d..504076f 100644 --- a/src/hub/Hub.sol +++ b/src/hub/Hub.sol @@ -273,7 +273,7 @@ contract Hub is Circles, TypeDefinitions, IHubErrors { // store the metadata digest for the avatar metadata if (_metadataDigest != bytes32(0)) { - nameRegistry.setMetadataDigest(msg.sender, _metadataDigest); + _setMetadataDigest(_metadataDigest); } } @@ -290,11 +290,11 @@ contract Hub is Circles, TypeDefinitions, IHubErrors { _registerGroup(msg.sender, _mint, standardTreasury, _name, _symbol); // for groups register possible custom name and symbol - nameRegistry.registerCustomName(msg.sender, _name); + _registerCustomName(_name); nameRegistry.registerCustomSymbol(msg.sender, _symbol); // store the IPFS CIDv0 digest for the group metadata - nameRegistry.setMetadataDigest(msg.sender, _metadataDigest); + _setMetadataDigest(_metadataDigest); emit RegisterGroup(msg.sender, _mint, standardTreasury, _name, _symbol); } @@ -317,11 +317,11 @@ contract Hub is Circles, TypeDefinitions, IHubErrors { _registerGroup(msg.sender, _mint, _treasury, _name, _symbol); // for groups register possible custom name and symbol - nameRegistry.registerCustomName(msg.sender, _name); + _registerCustomName(_name); nameRegistry.registerCustomSymbol(msg.sender, _symbol); // store the metadata digest for the group metadata - nameRegistry.setMetadataDigest(msg.sender, _metadataDigest); + _setMetadataDigest(_metadataDigest); emit RegisterGroup(msg.sender, _mint, _treasury, _name, _symbol); } @@ -335,10 +335,10 @@ contract Hub is Circles, TypeDefinitions, IHubErrors { _insertAvatar(msg.sender); // for organizations, only register possible custom name - nameRegistry.registerCustomName(msg.sender, _name); + _registerCustomName(_name); // store the IPFS CIDv0 digest for the organization metadata - nameRegistry.setMetadataDigest(msg.sender, _metadataDigest); + _setMetadataDigest(_metadataDigest); emit RegisterOrganization(msg.sender, _name); } @@ -1228,4 +1228,20 @@ contract Hub is Circles, TypeDefinitions, IHubErrors { // update the expiry; checks must be done by caller trustMarker.expiry = _expiry; } + + /** + * @dev Calls nameRegistry to store the metadata digest. + * @param _metadataDigest sha256 metadata digest for the avatar metadata. + */ + function _setMetadataDigest(bytes32 _metadataDigest) private { + nameRegistry.setMetadataDigest(msg.sender, _metadataDigest); + } + + /** + * @dev Calls nameRegistry to register the custom name. + * @param _name immutable name of the group Circles or organization. + */ + function _registerCustomName(string calldata _name) private { + nameRegistry.registerCustomName(msg.sender, _name); + } } From aaa8a772296482b02b870393a3617678fc379c62 Mon Sep 17 00:00:00 2001 From: roleengineer Date: Tue, 12 Nov 2024 14:55:30 +0300 Subject: [PATCH 4/9] (hub): optimized via reusing registerCustomGroup --- src/hub/Hub.sol | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/src/hub/Hub.sol b/src/hub/Hub.sol index 504076f..33b4a75 100644 --- a/src/hub/Hub.sol +++ b/src/hub/Hub.sol @@ -287,16 +287,7 @@ contract Hub is Circles, TypeDefinitions, IHubErrors { function registerGroup(address _mint, string calldata _name, string calldata _symbol, bytes32 _metadataDigest) external { - _registerGroup(msg.sender, _mint, standardTreasury, _name, _symbol); - - // for groups register possible custom name and symbol - _registerCustomName(_name); - nameRegistry.registerCustomSymbol(msg.sender, _symbol); - - // store the IPFS CIDv0 digest for the group metadata - _setMetadataDigest(_metadataDigest); - - emit RegisterGroup(msg.sender, _mint, standardTreasury, _name, _symbol); + registerCustomGroup(_mint, standardTreasury, _name, _symbol, _metadataDigest); } /** @@ -313,7 +304,7 @@ contract Hub is Circles, TypeDefinitions, IHubErrors { string calldata _name, string calldata _symbol, bytes32 _metadataDigest - ) external { + ) public { _registerGroup(msg.sender, _mint, _treasury, _name, _symbol); // for groups register possible custom name and symbol From 6aa0d26621873b8eb61ece316347373d01591252 Mon Sep 17 00:00:00 2001 From: Benjamin Bollen Date: Wed, 13 Nov 2024 18:31:16 +0000 Subject: [PATCH 5/9] (workflow): update active CI branches --- .github/workflows/test.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 0bbac1d..69d1846 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -2,9 +2,9 @@ name: test on: push: - branches: [ master, develop ] + branches: [ master, develop, beta, candidate/stable ] pull_request: - branches: [ master, develop ] + branches: [ master, develop, beta, candidate/stable ] workflow_dispatch: env: From 3f4c8186000048d652e844d94ae9b22d6e311e10 Mon Sep 17 00:00:00 2001 From: roleengineer Date: Thu, 14 Nov 2024 12:44:48 +0100 Subject: [PATCH 6/9] (workflow): add command to print contract sizes --- .github/workflows/test.yml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 69d1846..ea5073a 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -42,3 +42,8 @@ jobs: run: | forge test -vvv id: test + + - name: Print compiled non-test contract sizes + run: | + forge build --skip test --sizes + id: build-sizes \ No newline at end of file From cd417188b41e34ccd8061f8fa4cb0064a2572492 Mon Sep 17 00:00:00 2001 From: roleengineer Date: Thu, 14 Nov 2024 12:50:05 +0100 Subject: [PATCH 7/9] (workflow): made .gas-snaphot available (removed from .gitignore) --- .gas-snapshot | 31 +++++++++++++++++++++++++++++++ .gitignore | 2 -- 2 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 .gas-snapshot diff --git a/.gas-snapshot b/.gas-snapshot new file mode 100644 index 0000000..d7d549e --- /dev/null +++ b/.gas-snapshot @@ -0,0 +1,31 @@ +CirclesTest:testCalculateIssuance() (gas: 3729002) +CirclesTest:testConsecutiveClaimablePeriods() (gas: 375082) +CirclesTest:testDemurragedTransfer() (gas: 229055) +CompositeMintGroupsTest:testCompositeGroupMint() (gas: 212459) +DemurrageTest:testDemurrageFactor() (gas: 79139) +DemurrageTest:testFuzzStablePointIssuance(int192) (runs: 256, μ: 63114, ~: 63250) +DemurrageTest:testInversionGammaBeta64x64_100years() (gas: 958541) +DemurrageTest:testInversionGammaBeta64x64_100years_withExtension() (gas: 1083080) +DemurrageTest:testInversionGammaBeta64x64_100years_withExtension_comparison() (gas: 1703713) +DemurrageTest:testInversionGammaBeta64x64_20years() (gas: 221562) +DemurrageTest:testRepeatedDemurrage() (gas: 15264702) +ERC20LiftTest:testERC20Demurrage() (gas: 133270) +ERC20LiftTest:testERC20Wrap() (gas: 954629) +ERC20LiftTest:testWrapAndUnwrapInflationaryERC20() (gas: 89881) +GroupMintTest:testRegisterGroup() (gas: 165153) +HubPathTransferTest:testOperateFlowMatrixConsentedFlow() (gas: 265369) +MigrationTest:testConversionMigrationV1ToTimeCircles() (gas: 18365) +MintGroupCirclesTest:testDirectSelfGroupMintFails() (gas: 341135) +MintGroupCirclesTest:testGroupMint() (gas: 340165) +MintGroupCirclesTest:testGroupMintFail() (gas: 23302) +MintGroupCirclesTest:testGroupMintMany() (gas: 858065) +MintGroupCirclesTest:testGroupMintMultiCollateral() (gas: 755126) +MintGroupCirclesTest:testSequentialGroupMint() (gas: 511760) +NamesTest:testBase58Conversion() (gas: 78904) +NamesTest:testCustomName() (gas: 40809) +NamesTest:testInvalidCustomNames() (gas: 20017) +NamesTest:testMetadataDigest() (gas: 35073) +NamesTest:testShortName() (gas: 102230) +NamesTest:testShortNameWithNonce() (gas: 73360) +NamesTest:testShortNameWithPadding() (gas: 71662) +V1MintStatusUpdateTest:testMigrationFromV1DuringBootstrap() (gas: 2542748) \ No newline at end of file diff --git a/.gitignore b/.gitignore index 52add64..38ce5f9 100644 --- a/.gitignore +++ b/.gitignore @@ -5,8 +5,6 @@ out/ # docs -# gas snapshot files -.gas-snapshot # Ignores development broadcast logs !/broadcast From f90e509e3ae28378bf6d1720e51e6aa8d29193e5 Mon Sep 17 00:00:00 2001 From: roleengineer Date: Thu, 14 Nov 2024 12:55:36 +0100 Subject: [PATCH 8/9] (workflow): added command to print gas difference against a pre-existing snapshot --- .github/workflows/test.yml | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index ea5073a..b4eb737 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -46,4 +46,9 @@ jobs: - name: Print compiled non-test contract sizes run: | forge build --skip test --sizes - id: build-sizes \ No newline at end of file + id: build-sizes + + - name: Print gas difference against a pre-existing snapshot + run: | + forge snapshot --diff + id: gas-diff \ No newline at end of file From 3033b2245784a2ed4bbe0c3f22c2dabaca3b0ffa Mon Sep 17 00:00:00 2001 From: roleengineer Date: Thu, 14 Nov 2024 13:59:57 +0100 Subject: [PATCH 9/9] mock contracts in test repo are extended with IS_TEST boolean --- test/hub/MockDeployment.sol | 1 + test/hub/MockHub.sol | 1 + test/hub/MockPathTransferHub.sol | 1 + 3 files changed, 3 insertions(+) diff --git a/test/hub/MockDeployment.sol b/test/hub/MockDeployment.sol index bd4e183..273e5af 100644 --- a/test/hub/MockDeployment.sol +++ b/test/hub/MockDeployment.sol @@ -14,6 +14,7 @@ import "../lift/MockERC20Lift.sol"; import "./MockHub.sol"; contract MockDeployment { + bool public IS_TEST = true; // State variables MockHub public hub; diff --git a/test/hub/MockHub.sol b/test/hub/MockHub.sol index 02a9b83..6f53ece 100644 --- a/test/hub/MockHub.sol +++ b/test/hub/MockHub.sol @@ -4,6 +4,7 @@ pragma solidity >=0.8.24; import "../../src/hub/Hub.sol"; contract MockHub is Hub { + bool public IS_TEST = true; // Constructor constructor(uint256 _inflationDayZero, uint256 _bootstrapTime) diff --git a/test/hub/MockPathTransferHub.sol b/test/hub/MockPathTransferHub.sol index 643e331..d6c2faa 100644 --- a/test/hub/MockPathTransferHub.sol +++ b/test/hub/MockPathTransferHub.sol @@ -7,6 +7,7 @@ import "../../src/migration/IHub.sol"; import "../../src/names/INameRegistry.sol"; contract MockPathTransferHub is Hub { + bool public IS_TEST = true; // Constructor constructor(uint256 _inflationDayZero, uint256 _bootstrapTime)