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

refactor(world): separate call utils into WorldContextProvider and SystemCall #1370

Merged
merged 9 commits into from
Aug 31, 2023

Conversation

alvrs
Copy link
Member

@alvrs alvrs commented Aug 30, 2023

  • We previously had a Call.withSender util that managed appending the msg.sender to the calldata, and took a flag to use call or delegatecall. Since appending msg.sender to the calldata is tightly coupled with WorldContext, the utils for calling/delegatecalling with msg.sender appended to the calldata were moved to WorldContextProvider.callWithContext/delegatecallWithContext.
  • WorldContext is renamed to WorldContextConsumer (to clarify the interaction between WorldContextProvider (appending context) and WorldContextConsumer (extracting context))
  • The World previously had an internal _call method which handled access control checks and converting a resourceSelector into the system's address. Since this logic is useful for other libraries (most notably the delegation checks coming in feat(world): add callFrom entry point #1364) it is moved to library functions SystemCall.call/callWithHooks
  • System hooks' interface changed to take the resourceSelector instead of the system address. This seems more useful and simplifies the code. If needed a hook can still load the system's address from storage via Systems.get(resourceSelector) (which would be cheap since the storage slot is warm)

@changeset-bot
Copy link

changeset-bot bot commented Aug 30, 2023

🦋 Changeset detected

Latest commit: a2404a7

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 27 packages
Name Type
@latticexyz/world Major
@latticexyz/cli Major
@latticexyz/dev-tools Major
@latticexyz/store-sync Major
@latticexyz/store-indexer Major
@latticexyz/block-logs-stream Major
@latticexyz/common Major
@latticexyz/config Major
create-mud Major
@latticexyz/ecs-browser Major
@latticexyz/gas-report Major
@latticexyz/network Major
@latticexyz/noise Major
@latticexyz/phaserx Major
@latticexyz/protocol-parser Major
@latticexyz/react Major
@latticexyz/recs Major
@latticexyz/schema-type Major
@latticexyz/services Major
@latticexyz/solecs Major
solhint-config-mud Major
solhint-plugin-mud Major
@latticexyz/std-client Major
@latticexyz/std-contracts Major
@latticexyz/store-cache Major
@latticexyz/store Major
@latticexyz/utils Major

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

* Reverts with the error if the call was not successful.
* Else returns any return data.
*/
function callWithHooksOrRevert(
Copy link
Member Author

Choose a reason for hiding this comment

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

this is the previous behavior of _call. In addition we now have library functions that return a success flag instead of reverting, which is useful if we want to execute more logic if a call failed (eg. will be needed in #1364)

* If the system is not public, the caller must have access to the namespace or name.
* Returns the success status and any return data.
*/
function call(
Copy link
Member

Choose a reason for hiding this comment

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

Ohhh this is now a nice entry point for adding a system call event

@@ -5,7 +5,7 @@ import { StoreSwitch } from "@latticexyz/store/src/StoreSwitch.sol";
import { ResourceSelector } from "./ResourceSelector.sol";
import { SystemRegistry } from "./Tables.sol";

library Utils {
library SystemUtils {
Copy link
Member Author

Choose a reason for hiding this comment

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

renamed this to make the purpose a bit clearer, and to add more utility functions to the same file. (Unfortunately it's not possible to turn the SystemUtils.systemNamespace() function into a free function because it uses address(this))

Copy link
Member Author

Choose a reason for hiding this comment

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

Undid this change - felt unclean to have a mixed utils file, so i moved revertWithBytes to its own file. Still unhappy with this file being called just "Utils", but changing that should be its own PR.

@alvrs
Copy link
Member Author

alvrs commented Aug 30, 2023

dk1a
dk1a previously approved these changes Aug 30, 2023
Base automatically changed from alvrs/fix-ci-test-action to main August 31, 2023 13:34
@alvrs alvrs dismissed dk1a’s stale review August 31, 2023 13:34

The base branch was changed.

@alvrs
Copy link
Member Author

alvrs commented Aug 31, 2023

Merging since no change since last approval

@alvrs alvrs merged commit 9d0f492 into main Aug 31, 2023
18 checks passed
@alvrs alvrs deleted the alvrs/call-utils branch August 31, 2023 14:06
@holic
Copy link
Member

holic commented Sep 1, 2023

👏 this is great

* 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(
Copy link
Member

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?


/**
* 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.
Copy link
Member

Choose a reason for hiding this comment

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

I assume hooks are only called on success?

@github-actions github-actions bot mentioned this pull request Sep 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants