Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion .github/actions/setup-prerequisites/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ runs:
- name: Setup foundry
uses: foundry-rs/foundry-toolchain@v1
with:
version: nightly
version: nightly-f47d7e0c29a36372908b917cd74aa589d5888f8e
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 @@ -22,16 +23,15 @@ contract BatchCallSystem is System, LimitedCallContext {
function batchCall(
SystemCallData[] calldata systemCalls
) public onlyDelegatecall returns (bytes[] memory returnDatas) {
IBaseWorld world = IBaseWorld(_world());
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))
returnDatas[i] = SystemCall.callWithHooksOrRevert(
_msgSender(),
systemCalls[i].systemId,
systemCalls[i].callData,
0
);
if (!success) revertWithBytes(returnData);

returnDatas[i] = abi.decode(returnData, (bytes));
}
}

Expand All @@ -48,11 +48,11 @@ contract BatchCallSystem is System, LimitedCallContext {
returnDatas = new bytes[](systemCalls.length);

for (uint256 i; i < systemCalls.length; i++) {
// TODO: swap this with SystemCall.callFromWithHooksOrRevert once available
(bool success, bytes memory returnData) = address(world).delegatecall(
abi.encodeCall(world.callFrom, (systemCalls[i].from, systemCalls[i].systemId, systemCalls[i].callData))
);
if (!success) revertWithBytes(returnData);

returnDatas[i] = abi.decode(returnData, (bytes));
}
}
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