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

feat(world): support callFrom(batchCall) #3506

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { IBaseWorld } from "../../../codegen/interfaces/IBaseWorld.sol";
import { revertWithBytes } from "../../../revertWithBytes.sol";
import { SystemCallData, SystemCallFromData } from "../types.sol";
import { LimitedCallContext } from "../LimitedCallContext.sol";
import { SystemCall } from "../../../SystemCall.sol";

/**
* @title Batch Call System
Expand All @@ -26,11 +27,12 @@ contract BatchCallSystem is System, LimitedCallContext {
returnDatas = new bytes[](systemCalls.length);

for (uint256 i; i < systemCalls.length; i++) {
(bool success, bytes memory returnData) = address(world).delegatecall(
abi.encodeCall(world.call, (systemCalls[i].systemId, systemCalls[i].callData))
bytes memory returnData = SystemCall.callWithHooksOrRevert(
address(world),
holic marked this conversation as resolved.
Show resolved Hide resolved
systemCalls[i].systemId,
systemCalls[i].callData,
0
);
if (!success) revertWithBytes(returnData);

returnDatas[i] = abi.decode(returnData, (bytes));
holic marked this conversation as resolved.
Show resolved Hide resolved
}
}
Expand Down
43 changes: 43 additions & 0 deletions packages/world/test/BatchCall.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import { RESOURCE_SYSTEM } from "../src/worldResourceTypes.sol";
import { IWorldErrors } from "../src/IWorldErrors.sol";
import { IBaseWorld } from "../src/codegen/interfaces/IBaseWorld.sol";
import { SystemCallData, SystemCallFromData } from "../src/modules/init/types.sol";
import { BATCH_CALL_SYSTEM_ID } from "../src/modules/init/constants.sol";
import { BatchCallSystem } from "../src/modules/init/implementations/BatchCallSystem.sol";

import { createWorld } from "./createWorld.sol";

Expand Down Expand Up @@ -175,4 +177,45 @@ contract BatchCallTest is Test, GasReporter {
assertEq(abi.decode(returnDatas[0], (address)), delegatee, "wrong delegatee returned");
assertEq(abi.decode(returnDatas[1], (address)), delegator, "wrong delegator returned");
}

/**
* If all calls come from the same delegation, it should be simpler and cheaper to compose
* calls via `callFrom(batchCall(...))` instead of `batchCallFrom(...)`.
*/
function testCallFromBatchCall() public {
// Register a new system
TestSystem system = new TestSystem();
world.registerSystem(systemId, system, true);

// Try to increment the counter without creating a delegation
SystemCallData[] memory systemCalls = new SystemCallData[](1);
systemCalls[0] = SystemCallData(systemId, abi.encodeCall(TestSystem.increment, ()));

vm.prank(delegatee);
vm.expectRevert(abi.encodeWithSelector(IWorldErrors.World_DelegationNotFound.selector, delegator, delegatee));
world.callFrom(delegator, BATCH_CALL_SYSTEM_ID, abi.encodeCall(BatchCallSystem.batchCall, (systemCalls)));

// Create an unlimited delegation
vm.prank(delegator);
world.registerDelegation(delegatee, UNLIMITED_DELEGATION, new bytes(0));

// Try to increment the counter without setting the admin
vm.prank(delegatee);
vm.expectRevert("sender is not admin");
world.callFrom(delegator, BATCH_CALL_SYSTEM_ID, abi.encodeCall(BatchCallSystem.batchCall, (systemCalls)));

assertEq(system.admin(), address(0));

// Set the admin and increment the counter twice
systemCalls = new SystemCallData[](3);
systemCalls[0] = SystemCallData(systemId, abi.encodeCall(TestSystem.setAdmin, (delegator)));
systemCalls[1] = SystemCallData(systemId, abi.encodeCall(TestSystem.increment, ()));
systemCalls[2] = SystemCallData(systemId, abi.encodeCall(TestSystem.increment, ()));

vm.prank(delegatee);
world.callFrom(delegator, BATCH_CALL_SYSTEM_ID, abi.encodeCall(BatchCallSystem.batchCall, (systemCalls)));

assertEq(system.admin(), delegator);
assertEq(system.counter(), 2, "wrong counter value");
}
}
Loading