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(world-modules): SystemSwitch properly calls systems from root #2205

Merged
merged 15 commits into from
Feb 7, 2024

Conversation

yonadaa
Copy link
Contributor

@yonadaa yonadaa commented Jan 29, 2024

Fixes #2089

Added a test case that fails without this code change

@yonadaa yonadaa requested review from alvrs and holic as code owners January 29, 2024 13:27
Copy link

changeset-bot bot commented Jan 29, 2024

🦋 Changeset detected

Latest commit: 1bc3139

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

This PR includes changesets to release 30 packages
Name Type
@latticexyz/world-modules Major
@latticexyz/cli Major
@latticexyz/abi-ts Major
@latticexyz/block-logs-stream Major
@latticexyz/common Major
@latticexyz/config Major
create-mud Major
@latticexyz/dev-tools Major
@latticexyz/ecs-browser Major
@latticexyz/faucet 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-indexer Major
@latticexyz/store-sync Major
@latticexyz/store Major
@latticexyz/utils Major
@latticexyz/world 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

@@ -39,19 +39,27 @@ library SystemSwitch {
function call(ResourceId systemId, bytes memory callData) internal returns (bytes memory returnData) {
address worldAddress = WorldContextConsumerLib._world();

// If we're in the World context, call the system directly via delegatecall
// If we're in the World context
if (address(this) == worldAddress) {
(address systemAddress, ) = Systems.get(systemId);
Copy link
Member

Choose a reason for hiding this comment

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

looking at this I wonder if we should just use the SystemCall.call library here

  function call(ResourceId systemId, bytes memory callData) internal returns (bytes memory returnData) {
    address worldAddress = WorldContextConsumerLib._world();

    // If we're in the World context, call via the internal library
    if (address(this) == worldAddress) {
      return SystemCall.call(systemId, callData);
    }

    // Otherwise, call the system via world.call
    returnData = IWorldKernel(worldAddress).call(systemId, callData);
  }

Copy link
Contributor Author

@yonadaa yonadaa Jan 30, 2024

Choose a reason for hiding this comment

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

I like this! It adds a small amount of gas for the publicAccess and value checks but it's great to reuse the code.

Copy link
Member

@holic holic Jan 30, 2024

Choose a reason for hiding this comment

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

(not blocking this PR) should we change all the other places to use the same lib?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Are there other places that do this? Searching for delegatecallWithContext or == ROOT_NAMESPACE doesn't bring up much for me

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wonder if this line could cause an error because it updates the balances expecting a root

if (value > 0) {

@yonadaa yonadaa changed the title fix(world-modules): root should call non-root systems fix(world-modules): SystemSwitch should properly call systems Jan 30, 2024
@yonadaa yonadaa changed the title fix(world-modules): SystemSwitch should properly call systems fix(world-modules): SystemSwitch properly calls systems from root Jan 30, 2024
@yonadaa yonadaa requested a review from alvrs January 30, 2024 17:42
@yonadaa
Copy link
Contributor Author

yonadaa commented Jan 31, 2024

We should merge #2216 into this

@yonadaa yonadaa merged commit c4fc850 into main Feb 7, 2024
11 checks passed
@yonadaa yonadaa deleted the yonadaaa/system-switch-non-root branch February 7, 2024 17:42
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.

SystemSwitch does not replicate World behavior when calling a non-root System from a root one
3 participants