Skip to content

Commit

Permalink
feat(world): allow callFrom from own address without explicit delegat…
Browse files Browse the repository at this point in the history
…ion (#1407)
  • Loading branch information
alvrs authored Sep 7, 2023
1 parent 2459e15 commit 18d3aea
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/silver-mangos-thank.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@latticexyz/world": patch
---

Allow `callFrom` with the own address as `delegator` without requiring an explicit delegation
6 changes: 3 additions & 3 deletions packages/world/gas-report.json
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@
"file": "test/StandardDelegationsModule.t.sol",
"test": "testCallFromCallboundDelegation",
"name": "call a system via a callbound delegation",
"gasUsed": 44074
"gasUsed": 44114
},
{
"file": "test/StandardDelegationsModule.t.sol",
Expand All @@ -249,7 +249,7 @@
"file": "test/StandardDelegationsModule.t.sol",
"test": "testCallFromTimeboundDelegation",
"name": "call a system via a timebound delegation",
"gasUsed": 34779
"gasUsed": 34819
},
{
"file": "test/UniqueEntityModule.t.sol",
Expand Down Expand Up @@ -291,7 +291,7 @@
"file": "test/World.t.sol",
"test": "testCallFromUnlimitedDelegation",
"name": "call a system via an unlimited delegation",
"gasUsed": 17853
"gasUsed": 17893
},
{
"file": "test/World.t.sol",
Expand Down
5 changes: 5 additions & 0 deletions packages/world/src/World.sol
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,11 @@ contract World is StoreRead, IStoreData, IWorldKernel {
bytes32 resourceSelector,
bytes memory funcSelectorAndArgs
) external payable virtual returns (bytes memory) {
// If the delegator is the caller, call the system directly
if (delegator == msg.sender) {
return SystemCall.callWithHooksOrRevert(msg.sender, resourceSelector, funcSelectorAndArgs, msg.value);
}

// Check if there is an explicit authorization for this caller to perform actions on behalf of the delegator
Delegation explicitDelegation = Delegation.wrap(Delegations.get({ delegator: delegator, delegatee: msg.sender }));

Expand Down
21 changes: 21 additions & 0 deletions packages/world/test/World.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,27 @@ contract WorldTest is Test, GasReporter {
assertEq(returnedAddress, address(this), "subsystem returned wrong address");
}

function testCallFromSelf() public {
// Register a new system
WorldTestSystem system = new WorldTestSystem();
bytes32 resourceSelector = ResourceSelector.from("namespace", "testSystem");
world.registerSystem(resourceSelector, system, true);

address caller = address(1);

// Call a system via callFrom with the own address
vm.prank(caller);
bytes memory returnData = world.callFrom(
caller,
resourceSelector,
abi.encodeWithSelector(WorldTestSystem.msgSender.selector)
);
address returnedAddress = abi.decode(returnData, (address));

// Expect the system to have received the delegator's address
assertEq(returnedAddress, caller);
}

function testCallFromUnlimitedDelegation() public {
// Register a new system
WorldTestSystem system = new WorldTestSystem();
Expand Down

0 comments on commit 18d3aea

Please sign in to comment.