-
Notifications
You must be signed in to change notification settings - Fork 196
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
refactor(world): separate call utils into WorldContextProvider
and SystemCall
#1370
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ca21411
refactor(world): Call.withSenter -> WorldContextProvider, _call -> Sy…
alvrs 3bcf8b1
use callWithHooksOrRevert in fallback function
alvrs f7ce695
add changeset
alvrs 3643924
update gas report
alvrs 27a0303
rebuild artifacts
alvrs e9cc0aa
revert Utils change, move revertWithBytes to own file
alvrs 1d81365
rebuild example artifacts
alvrs a127072
prettier
alvrs a2404a7
build in e2e
alvrs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
--- | ||
"@latticexyz/world": major | ||
--- | ||
|
||
- The previous `Call.withSender` util is replaced with `WorldContextProvider`, since the usecase of appending the `msg.sender` to the calldata is tightly coupled with `WorldContextConsumer` (which extracts the appended context from the calldata). | ||
|
||
The previous `Call.withSender` utility reverted if the call failed and only returned the returndata on success. This is replaced with `callWithContextOrRevert`/`delegatecallWithContextOrRevert` | ||
|
||
```diff | ||
-import { Call } from "@latticexyz/world/src/Call.sol"; | ||
+import { WorldContextProvider } from "@latticexyz/world/src/WorldContext.sol"; | ||
|
||
-Call.withSender({ | ||
- delegate: false, | ||
- value: 0, | ||
- ... | ||
-}); | ||
+WorldContextProvider.callWithContextOrRevert({ | ||
+ value: 0, | ||
+ ... | ||
+}); | ||
|
||
-Call.withSender({ | ||
- delegate: true, | ||
- value: 0, | ||
- ... | ||
-}); | ||
+WorldContextProvider.delegatecallWithContextOrRevert({ | ||
+ ... | ||
+}); | ||
``` | ||
|
||
In addition there are utils that return a `bool success` flag instead of reverting on errors. This mirrors the behavior of Solidity's low level `call`/`delegatecall` functions and is useful in situations where additional logic should be executed in case of a reverting external call. | ||
|
||
```solidity | ||
library WorldContextProvider { | ||
function callWithContext( | ||
address target, // Address to call | ||
bytes memory funcSelectorAndArgs, // Abi encoded function selector and arguments to pass to pass to the contract | ||
address msgSender, // Address to append to the calldata as context for msgSender | ||
uint256 value // Value to pass with the call | ||
) internal returns (bool success, bytes memory data); | ||
|
||
function delegatecallWithContext( | ||
address target, // Address to call | ||
bytes memory funcSelectorAndArgs, // Abi encoded function selector and arguments to pass to pass to the contract | ||
address msgSender // Address to append to the calldata as context for msgSender | ||
) internal returns (bool success, bytes memory data); | ||
} | ||
``` | ||
|
||
- `WorldContext` is renamed to `WorldContextConsumer` to clarify the relationship between `WorldContextProvider` (appending context to the calldata) and `WorldContextConsumer` (extracting context from the calldata) | ||
|
||
```diff | ||
-import { WorldContext } from "@latticexyz/world/src/WorldContext.sol"; | ||
-import { WorldContextConsumer } from "@latticexyz/world/src/WorldContext.sol"; | ||
``` | ||
|
||
- The `World` contract previously had a `_call` method to handle calling systems via their resource selector, performing accesss control checks and call hooks registered for the system. | ||
|
||
```solidity | ||
library SystemCall { | ||
/** | ||
* Calls a system via its resource selector and perform access control checks. | ||
* Does not revert if the call fails, but returns a `success` flag along with the returndata. | ||
*/ | ||
function call( | ||
address caller, | ||
bytes32 resourceSelector, | ||
bytes memory funcSelectorAndArgs, | ||
uint256 value | ||
) internal returns (bool success, bytes memory data); | ||
|
||
/** | ||
* Calls a system via its resource selector, perform access control checks and trigger hooks registered for the system. | ||
* Does not revert if the call fails, but returns a `success` flag along with the returndata. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I assume hooks are only called on success? |
||
*/ | ||
function callWithHooks( | ||
address caller, | ||
bytes32 resourceSelector, | ||
bytes memory funcSelectorAndArgs, | ||
uint256 value | ||
) internal returns (bool success, bytes memory data); | ||
|
||
/** | ||
* Calls a system via its resource selector, perform access control checks and trigger hooks registered for the system. | ||
* Reverts if the call fails. | ||
*/ | ||
function callWithHooksOrRevert( | ||
address caller, | ||
bytes32 resourceSelector, | ||
bytes memory funcSelectorAndArgs, | ||
uint256 value | ||
) internal returns (bytes memory data); | ||
} | ||
``` | ||
|
||
- System hooks now are called with the system's resource selector instead of its address. The system's address can still easily obtained within the hook via `Systems.get(resourceSelector)` if necessary. | ||
|
||
```diff | ||
interface ISystemHook { | ||
function onBeforeCallSystem( | ||
address msgSender, | ||
- address systemAddress, | ||
+ bytes32 resourceSelector, | ||
bytes memory funcSelectorAndArgs | ||
) external; | ||
|
||
function onAfterCallSystem( | ||
address msgSender, | ||
- address systemAddress, | ||
+ bytes32 resourceSelector, | ||
bytes memory funcSelectorAndArgs | ||
) external; | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions
1
packages/world/abi/WorldContext.sol/WorldContextProvider.abi.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should this have a corresponding
callOrRevert
?