Skip to content

Commit

Permalink
feat(world): rename funcSelectorAndArgs to callData (#1524)
Browse files Browse the repository at this point in the history
Co-authored-by: alvarius <alvarius@lattice.xyz>
  • Loading branch information
holic and alvrs committed Sep 17, 2023
1 parent 498d05e commit a0341da
Show file tree
Hide file tree
Showing 19 changed files with 98 additions and 124 deletions.
5 changes: 5 additions & 0 deletions .changeset/lovely-fireants-behave.md
@@ -0,0 +1,5 @@
---
"@latticexyz/world": patch
---

Renamed all `funcSelectorAndArgs` arguments to `callData` for clarity.
2 changes: 1 addition & 1 deletion packages/world/mud.config.ts
Expand Up @@ -150,7 +150,7 @@ export default mudConfig({
delegator: "address",
delegatee: "address",
resourceSelector: "bytes32",
funcSelectorAndArgsHash: "bytes32",
callDataHash: "bytes32",
},
valueSchema: {
availableCalls: "uint256",
Expand Down
9 changes: 2 additions & 7 deletions packages/world/src/Delegation.sol
Expand Up @@ -32,7 +32,7 @@ library DelegationInstance {
address delegator,
address delegatee,
bytes32 systemId,
bytes memory funcSelectorAndArgs
bytes memory callData
) internal returns (bool) {
// Early return if there is an unlimited delegation
if (isUnlimited(self)) return true;
Expand All @@ -44,12 +44,7 @@ library DelegationInstance {
(bool success, bytes memory data) = SystemCall.call({
caller: delegatee,
resourceSelector: Delegation.unwrap(self),
funcSelectorAndArgs: abi.encodeWithSelector(
IDelegationControl.verify.selector,
delegator,
systemId,
funcSelectorAndArgs
),
callData: abi.encodeWithSelector(IDelegationControl.verify.selector, delegator, systemId, callData),
value: 0
});

Expand Down
23 changes: 9 additions & 14 deletions packages/world/src/SystemCall.sol
Expand Up @@ -31,7 +31,7 @@ library SystemCall {
address caller,
uint256 value,
bytes32 resourceSelector,
bytes memory funcSelectorAndArgs
bytes memory callData
) internal returns (bool success, bytes memory data) {
// Load the system data
(address systemAddress, bool publicAccess) = Systems._get(resourceSelector);
Expand All @@ -55,13 +55,13 @@ library SystemCall {
msgSender: caller,
msgValue: value,
target: systemAddress,
funcSelectorAndArgs: funcSelectorAndArgs
callData: callData
})
: WorldContextProvider.callWithContext({
msgSender: caller,
msgValue: value,
target: systemAddress,
funcSelectorAndArgs: funcSelectorAndArgs
callData: callData
});
}

Expand All @@ -72,7 +72,7 @@ library SystemCall {
function callWithHooks(
address caller,
bytes32 resourceSelector,
bytes memory funcSelectorAndArgs,
bytes memory callData,
uint256 value
) internal returns (bool success, bytes memory data) {
// Get system hooks
Expand All @@ -82,23 +82,18 @@ library SystemCall {
for (uint256 i; i < hooks.length; i++) {
Hook hook = Hook.wrap(hooks[i]);
if (hook.isEnabled(BEFORE_CALL_SYSTEM)) {
ISystemHook(hook.getAddress()).onBeforeCallSystem(caller, resourceSelector, funcSelectorAndArgs);
ISystemHook(hook.getAddress()).onBeforeCallSystem(caller, resourceSelector, callData);
}
}

// Call the system and forward any return data
(success, data) = call({
caller: caller,
value: value,
resourceSelector: resourceSelector,
funcSelectorAndArgs: funcSelectorAndArgs
});
(success, data) = call({ caller: caller, value: value, resourceSelector: resourceSelector, callData: callData });

// Call onAfterCallSystem hooks (after calling the system)
for (uint256 i; i < hooks.length; i++) {
Hook hook = Hook.wrap(hooks[i]);
if (hook.isEnabled(AFTER_CALL_SYSTEM)) {
ISystemHook(hook.getAddress()).onAfterCallSystem(caller, resourceSelector, funcSelectorAndArgs);
ISystemHook(hook.getAddress()).onAfterCallSystem(caller, resourceSelector, callData);
}
}
}
Expand All @@ -110,14 +105,14 @@ library SystemCall {
function callWithHooksOrRevert(
address caller,
bytes32 resourceSelector,
bytes memory funcSelectorAndArgs,
bytes memory callData,
uint256 value
) internal returns (bytes memory data) {
(bool success, bytes memory returnData) = callWithHooks({
caller: caller,
value: value,
resourceSelector: resourceSelector,
funcSelectorAndArgs: funcSelectorAndArgs
callData: callData
});
if (!success) revertWithBytes(returnData);
return returnData;
Expand Down
21 changes: 9 additions & 12 deletions packages/world/src/World.sol
Expand Up @@ -85,7 +85,7 @@ contract World is StoreRead, IStoreData, IWorldKernel {
msgSender: msg.sender,
msgValue: 0,
target: address(module),
funcSelectorAndArgs: abi.encodeWithSelector(IModule.installRoot.selector, args)
callData: abi.encodeWithSelector(IModule.installRoot.selector, args)
});

// Register the module in the InstalledModules table
Expand Down Expand Up @@ -212,11 +212,8 @@ contract World is StoreRead, IStoreData, IWorldKernel {
* Call the system at the given resourceSelector.
* If the system is not public, the caller must have access to the namespace or name (encoded in the resourceSelector).
*/
function call(
bytes32 resourceSelector,
bytes memory funcSelectorAndArgs
) external payable virtual returns (bytes memory) {
return SystemCall.callWithHooksOrRevert(msg.sender, resourceSelector, funcSelectorAndArgs, msg.value);
function call(bytes32 resourceSelector, bytes memory callData) external payable virtual returns (bytes memory) {
return SystemCall.callWithHooksOrRevert(msg.sender, resourceSelector, callData, msg.value);
}

/**
Expand All @@ -226,26 +223,26 @@ contract World is StoreRead, IStoreData, IWorldKernel {
function callFrom(
address delegator,
bytes32 resourceSelector,
bytes memory funcSelectorAndArgs
bytes memory callData
) 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);
return SystemCall.callWithHooksOrRevert(msg.sender, resourceSelector, callData, 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 }));

if (explicitDelegation.verify(delegator, msg.sender, resourceSelector, funcSelectorAndArgs)) {
if (explicitDelegation.verify(delegator, msg.sender, resourceSelector, callData)) {
// forward the call as `delegator`
return SystemCall.callWithHooksOrRevert(delegator, resourceSelector, funcSelectorAndArgs, msg.value);
return SystemCall.callWithHooksOrRevert(delegator, resourceSelector, callData, msg.value);
}

// Check if the delegator has a fallback delegation control set
Delegation fallbackDelegation = Delegation.wrap(Delegations._get({ delegator: delegator, delegatee: address(0) }));
if (fallbackDelegation.verify(delegator, msg.sender, resourceSelector, funcSelectorAndArgs)) {
if (fallbackDelegation.verify(delegator, msg.sender, resourceSelector, callData)) {
// forward the call with `from` as `msgSender`
return SystemCall.callWithHooksOrRevert(delegator, resourceSelector, funcSelectorAndArgs, msg.value);
return SystemCall.callWithHooksOrRevert(delegator, resourceSelector, callData, msg.value);
}

revert DelegationNotFound(delegator, msg.sender);
Expand Down
20 changes: 10 additions & 10 deletions packages/world/src/WorldContext.sol
Expand Up @@ -46,46 +46,46 @@ abstract contract WorldContextConsumer is IWorldContextConsumer {
*/
library WorldContextProvider {
function appendContext(
bytes memory funcSelectorAndArgs,
bytes memory callData,
address msgSender,
uint256 msgValue
) internal pure returns (bytes memory) {
return abi.encodePacked(funcSelectorAndArgs, msgSender, msgValue);
return abi.encodePacked(callData, msgSender, msgValue);
}

function callWithContext(
address msgSender,
uint256 msgValue,
address target,
bytes memory funcSelectorAndArgs
bytes memory callData
) internal returns (bool success, bytes memory data) {
(success, data) = target.call{ value: 0 }(
appendContext({ funcSelectorAndArgs: funcSelectorAndArgs, msgSender: msgSender, msgValue: msgValue })
appendContext({ callData: callData, msgSender: msgSender, msgValue: msgValue })
);
}

function delegatecallWithContext(
address msgSender,
uint256 msgValue,
address target,
bytes memory funcSelectorAndArgs
bytes memory callData
) internal returns (bool success, bytes memory data) {
(success, data) = target.delegatecall(
appendContext({ funcSelectorAndArgs: funcSelectorAndArgs, msgSender: msgSender, msgValue: msgValue })
appendContext({ callData: callData, msgSender: msgSender, msgValue: msgValue })
);
}

function callWithContextOrRevert(
address msgSender,
uint256 msgValue,
address target,
bytes memory funcSelectorAndArgs
bytes memory callData
) internal returns (bytes memory data) {
(bool success, bytes memory _data) = callWithContext({
msgSender: msgSender,
msgValue: msgValue,
target: target,
funcSelectorAndArgs: funcSelectorAndArgs
callData: callData
});
if (!success) revertWithBytes(_data);
return _data;
Expand All @@ -95,13 +95,13 @@ library WorldContextProvider {
address msgSender,
uint256 msgValue,
address target,
bytes memory funcSelectorAndArgs
bytes memory callData
) internal returns (bytes memory data) {
(bool success, bytes memory _data) = delegatecallWithContext({
msgSender: msgSender,
msgValue: msgValue,
target: target,
funcSelectorAndArgs: funcSelectorAndArgs
callData: callData
});
if (!success) revertWithBytes(_data);
return _data;
Expand Down
2 changes: 1 addition & 1 deletion packages/world/src/interfaces/IDelegationControl.sol
Expand Up @@ -8,5 +8,5 @@ bytes4 constant DELEGATION_CONTROL_INTERFACE_ID = IDelegationControl.verify.sele
WORLD_CONTEXT_CONSUMER_INTERFACE_ID;

interface IDelegationControl is IWorldContextConsumer {
function verify(address delegator, bytes32 systemId, bytes calldata funcSelectorAndArgs) external returns (bool);
function verify(address delegator, bytes32 systemId, bytes calldata callData) external returns (bool);
}
4 changes: 2 additions & 2 deletions packages/world/src/interfaces/ISystemHook.sol
Expand Up @@ -9,7 +9,7 @@ bytes4 constant SYSTEM_HOOK_INTERFACE_ID = ISystemHook.onBeforeCallSystem.select
ERC165_INTERFACE_ID;

interface ISystemHook is IERC165 {
function onBeforeCallSystem(address msgSender, bytes32 resourceSelector, bytes memory funcSelectorAndArgs) external;
function onBeforeCallSystem(address msgSender, bytes32 resourceSelector, bytes memory callData) external;

function onAfterCallSystem(address msgSender, bytes32 resourceSelector, bytes memory funcSelectorAndArgs) external;
function onAfterCallSystem(address msgSender, bytes32 resourceSelector, bytes memory callData) external;
}
4 changes: 2 additions & 2 deletions packages/world/src/interfaces/IWorldKernel.sol
Expand Up @@ -18,7 +18,7 @@ interface IWorldCall {
* Call the system at the given resourceSelector.
* If the system is not public, the caller must have access to the namespace or name (encoded in the resourceSelector).
*/
function call(bytes32 resourceSelector, bytes memory funcSelectorAndArgs) external payable returns (bytes memory);
function call(bytes32 resourceSelector, bytes memory callData) external payable returns (bytes memory);

/**
* Call the system at the given resourceSelector on behalf of the given delegator.
Expand All @@ -27,7 +27,7 @@ interface IWorldCall {
function callFrom(
address delegator,
bytes32 resourceSelector,
bytes memory funcSelectorAndArgs
bytes memory callData
) external payable returns (bytes memory);
}

Expand Down
6 changes: 1 addition & 5 deletions packages/world/src/interfaces/IWorldRegistrationSystem.sol
Expand Up @@ -27,9 +27,5 @@ interface IWorldRegistrationSystem {
bytes4 systemFunctionSelector
) external returns (bytes4);

function registerDelegation(
address delegatee,
bytes32 delegationControlId,
bytes memory initFuncSelectorAndArgs
) external;
function registerDelegation(address delegatee, bytes32 delegationControlId, bytes memory initCallData) external;
}
4 changes: 2 additions & 2 deletions packages/world/src/modules/core/CoreModule.sol
Expand Up @@ -90,7 +90,7 @@ contract CoreModule is Module {
msgSender: _msgSender(),
msgValue: 0,
target: coreSystem,
funcSelectorAndArgs: abi.encodeCall(
callData: abi.encodeCall(
WorldRegistrationSystem.registerSystem,
(ResourceSelector.from(ROOT_NAMESPACE, CORE_SYSTEM_NAME), CoreSystem(coreSystem), true)
)
Expand Down Expand Up @@ -134,7 +134,7 @@ contract CoreModule is Module {
msgSender: _msgSender(),
msgValue: 0,
target: coreSystem,
funcSelectorAndArgs: abi.encodeCall(
callData: abi.encodeCall(
WorldRegistrationSystem.registerRootFunctionSelector,
(ResourceSelector.from(ROOT_NAMESPACE, CORE_SYSTEM_NAME), functionSelectors[i], functionSelectors[i])
)
Expand Down
Expand Up @@ -24,7 +24,7 @@ contract ModuleInstallationSystem is System {
msgSender: _msgSender(),
msgValue: 0,
target: address(module),
funcSelectorAndArgs: abi.encodeWithSelector(IModule.install.selector, args)
callData: abi.encodeWithSelector(IModule.install.selector, args)
});

// Register the module in the InstalledModules table
Expand Down
Expand Up @@ -57,7 +57,7 @@ contract StoreRegistrationSystem is System, IWorldErrors {
msgSender: _msgSender(),
msgValue: 0,
target: systemAddress,
funcSelectorAndArgs: abi.encodeWithSelector(WorldRegistrationSystem.registerNamespace.selector, namespace)
callData: abi.encodeWithSelector(WorldRegistrationSystem.registerNamespace.selector, namespace)
});
} else {
// otherwise require caller to own the namespace
Expand Down
Expand Up @@ -202,16 +202,12 @@ contract WorldRegistrationSystem is System, IWorldErrors {
/**
* Register a delegation from the caller to the given delegatee.
*/
function registerDelegation(
address delegatee,
bytes32 delegationControlId,
bytes memory initFuncSelectorAndArgs
) public {
function registerDelegation(address delegatee, bytes32 delegationControlId, bytes memory initCallData) public {
// Store the delegation control contract address
Delegations.set({ delegator: _msgSender(), delegatee: delegatee, delegationControlId: delegationControlId });

// If the delegation is not unlimited...
if (delegationControlId != UNLIMITED_DELEGATION && initFuncSelectorAndArgs.length > 0) {
if (delegationControlId != UNLIMITED_DELEGATION && initCallData.length > 0) {
// Require the delegationControl contract to implement the IDelegationControl interface
(address delegationControl, ) = Systems._get(delegationControlId);
requireInterface(delegationControl, DELEGATION_CONTROL_INTERFACE_ID);
Expand All @@ -220,7 +216,7 @@ contract WorldRegistrationSystem is System, IWorldErrors {
SystemCall.call({
caller: _msgSender(),
resourceSelector: delegationControlId,
funcSelectorAndArgs: initFuncSelectorAndArgs,
callData: initCallData,
value: 0
});
}
Expand Down

0 comments on commit a0341da

Please sign in to comment.