Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: (C4 #139) refactor _fallbackLSP17Extendable function to enable to run code after it is called + prevent potential solc bug "storage write removal" #674

Conversation

CJ42
Copy link
Member

@CJ42 CJ42 commented Aug 11, 2023

What does this PR introduce?

🐛 Bug

Currently, the function in LSP17 _fallbackLSP17Extendable does not allow to run any code logic after this function is called, because it uses the low level opcode return() in assembly.
This restrict some potential customization through inheritance, when one want to run some custom code logic after this function is being called.

This PR refactor this function:

  • in LSP17 to take a parameter.
  • in LSP0 + LSP9 to move from the following fallback syntax:
fallback() external payable {
    // ...
}

To the new following fallback syntax (available in all 0.8.x versions of Solidity).

fallback(bytes calldata data) external payable returns (bytes memory) {
   // ...
}

By removing assembly, , the function can be overriden to be called via super._fallbackLSP17Extendable(...) and run any code after.

Note: this refactoring also helps to prevent potential storage removal bug from the Solidity compiler, if projects inherit the LSP17 contract and compile their contract with a version from 0.8.13 to 0.8.16 (where the bug is present). See Solidity blog for more infos: https://soliditylang.org/blog/2022/09/08/storage-write-removal-before-conditional-termination/

🧪 Tests

Created new test suite LSP17Extendable.test.ts to test the basic functionalities of LSP17 and this new refactoring.
Tests include checking that storage write works as expected both before and after the _fallbackLSP17Extension() function is called.

Also added tests to ensure different type of revert reason strings are bubble up.

PR Checklist

  • Wrote Tests
  • Wrote & Generated Documentation (readme/natspec/dodoc)
  • Ran npm run lint && npm run lint:solidity (solhint)
  • Ran npm run format (prettier)
  • Ran npm run build
  • Ran npm run test

@github-actions
Copy link
Contributor

👋 Hello
⛽ I am the Gas Bot Reporter. I keep track of the gas costs of common interactions using Universal Profiles 🆙 !
📊 Here is a summary of the gas cost with the code introduced by this PR.

⛽📊 See Gas Benchmark report of Using UniversalProfile owned by an EOA

🔀 execute scenarios

execute scenarios - 👑 UP Owner ⛽ Gas Usage
Transfer 1 LYX to an EOA without data 37537
Transfer 1 LYX to a UP without data 36639
Transfer 1 LYX to an EOA with 256 bytes of data 42210
Transfer 1 LYX to a UP with 256 bytes of data 44831
Transfer 0.1 LYX to 3x EOA without data 70862
Transfer 0.1 LYX to 3x UP without data 75680
Transfer 0.1 LYX to 3x EOA with 256 bytes of data 84826
Transfer 0.1 LYX to 3x EOA with 256 bytes of data 100321

🗄️ setData scenarios

setData scenarios - 👑 UP Owner ⛽ Gas Usage
Set a 20 bytes long value 49971
Set a 60 bytes long value 95281
Set a 160 bytes long value 164453
Set a 300 bytes long value 279688
Set a 600 bytes long value 484136
Change the value of a data key already set 32847
Remove the value of a data key already set 27333
Set 2 data keys of 20 bytes long value 78428
Set 2 data keys of 100 bytes long value 260568
Set 3 data keys of 20 bytes long value 105145
Change the value of three data keys already set of 20 bytes long value 45445
Remove the value of three data keys already set 41339

🗄️ Tokens scenarios

Tokens scenarios - 👑 UP Owner ⛽ Gas Usage
Minting a LSP7Token to a UP (No Delegate) from an EOA 91241
Minting a LSP7Token to an EOA from an EOA 59206
Transferring an LSP7Token from a UP to another UP (No Delegate) 98689
Minting a LSP8Token to a UP (No Delegate) from an EOA 158192
Minting a LSP8Token to an EOA from an EOA 126157
Transferring an LSP8Token from a UP to another UP (No Delegate) 147236

📝 Notes

  • The execute and setData scenarios are executed on a fresh UniversalProfile smart contract, deployed as standard contracts (not as proxy behind a base contract implementation).
⛽📊 See Gas Benchmark report of Using UniversalProfile owned by an LSP6KeyManager

This document contains the gas usage for common interactions and scenarios when using UniversalProfile smart contracts.

🔀 execute scenarios

👑 unrestricted controller

execute scenarios - 👑 main controller ⛽ Gas Usage
transfer LYX to an EOA 60445
transfer LYX to a UP 62047
transfer tokens (LSP7) to an EOA (no data) 107168
transfer tokens (LSP7) to a UP (no data) 242740
transfer a NFT (LSP8) to a EOA (no data) 171015
transfer a NFT (LSP8) to a UP (no data) 289915

🛃 restricted controller

execute scenarios - 🛃 restricted controller ⛽ Gas Usage
transfer some LYXes to an EOA - restricted to 1 x allowed address only (TRANSFERVALUE + 1x AllowedCalls) 72654
transfers some tokens (LSP7) to an EOA - restricted to LSP7 + 2x allowed contracts only (CALL + 2x AllowedCalls) (no data) 122947
transfers some tokens (LSP7) to an other UP - restricted to LSP7 + 2x allowed contracts only (CALL + 2x AllowedCalls) (no data) 258519
transfers a NFT (LSP8) to an EOA - restricted to LSP8 + 2x allowed contracts only (CALL + 2x AllowedCalls) (no data) 186782
transfers a NFT (LSP8) to an other UP - restricted to LSP8 + 2x allowed contracts only (CALL + 2x AllowedCalls) (no data) 305682

🗄️ setData scenarios

👑 unrestricted controller

setData scenarios - 👑 main controller ⛽ Gas Usage
updates profile details (LSP3Profile metadata) 136875
give permissions to a controller (AddressPermissions[] + AddressPermissions[index] + AddressPermissions:Permissions:) 132906
restrict a controller to some specific ERC725Y Data Keys 139282
restrict a controller to interact only with 3x specific addresses 161986
remove a controller (its permissions + its address from the AddressPermissions[] array) 67871
write 5x LSP12 Issued Assets 233253

🛃 restricted controller

setData scenarios - 🛃 restricted controller ⛽ Gas Usage
setData(bytes32,bytes) -> updates 1x data key 102626
setData(bytes32[],bytes[]) -> updates 3x data keys (first x3) 161440
setData(bytes32[],bytes[]) -> updates 3x data keys (middle x3) 145519
setData(bytes32[],bytes[]) -> updates 3x data keys (last x3) 170752
setData(bytes32[],bytes[]) -> updates 2x data keys + add 3x new controllers (including setting the array length + indexes under AddressPermissions[index]) 249872

📝 Notes

  • The execute and setData scenarios are executed on a fresh UniversalProfile and LSP6KeyManager smart contracts, deployed as standard contracts (not as proxy behind a base contract implementation).

@github-actions
Copy link
Contributor

github-actions bot commented Aug 11, 2023

Changes to gas cost

Generated at commit: cc74597e680384c6873ef7e9f1627a1cfb25cfcb, compared to commit: 9db488aad9065f88cfc3c5f43f47b1fab35fc5f0

🧾 Summary (10% most significant diffs)

Contract Method Avg (+/-) %

Full diff report 👇
Contract Deployment Cost (+/-) Method Min (+/-) % Avg (+/-) % Median (+/-) % Max (+/-) % # Calls (+/-)
LSP6ExecuteRestrictedController 2,888,707 (+34,647)
LSP6ExecuteUnrestrictedController 2,888,707 (+34,647)
LSP6SetDataRestrictedController 2,873,483 (+34,638)
LSP6SetDataUnrestrictedController 2,873,483 (+34,638)

@CJ42 CJ42 force-pushed the DEV-5243_C4-139--related-to-58-Refactor-function-_fallbackLSP17Extendable-to-enable-to-run-code-after-this-function-is-called_Jean branch 2 times, most recently from ce2136f to 847428c Compare August 14, 2023 07:01
@CJ42 CJ42 force-pushed the DEV-5243_C4-139--related-to-58-Refactor-function-_fallbackLSP17Extendable-to-enable-to-run-code-after-this-function-is-called_Jean branch from 847428c to 2614da8 Compare August 14, 2023 08:08
@CJ42 CJ42 force-pushed the DEV-5243_C4-139--related-to-58-Refactor-function-_fallbackLSP17Extendable-to-enable-to-run-code-after-this-function-is-called_Jean branch from 2614da8 to bf16c54 Compare August 14, 2023 08:08
Copy link
Member

@skimaharvey skimaharvey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty cool

@CJ42 CJ42 merged commit 39ff1f4 into develop Aug 14, 2023
26 checks passed
@CJ42 CJ42 deleted the DEV-5243_C4-139--related-to-58-Refactor-function-_fallbackLSP17Extendable-to-enable-to-run-code-after-this-function-is-called_Jean branch August 14, 2023 13:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants