Skip to content

Commit

Permalink
world errors
Browse files Browse the repository at this point in the history
  • Loading branch information
alvrs committed Sep 21, 2023
1 parent e23e1e9 commit 841381e
Show file tree
Hide file tree
Showing 14 changed files with 101 additions and 83 deletions.
8 changes: 4 additions & 4 deletions packages/world/src/AccessControl.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,22 @@ library AccessControl {

/**
* Check for access at the given namespace or name.
* Reverts with AccessDenied if the caller has no access.
* Reverts with World_AccessDenied if the caller has no access.
*/
function requireAccess(ResourceId resourceId, address caller) internal view {
// Check if the given caller has access to the given namespace or name
if (!hasAccess(resourceId, caller)) {
revert IWorldErrors.AccessDenied(resourceId.toString(), caller);
revert IWorldErrors.World_AccessDenied(resourceId.toString(), caller);
}
}

/**
* Check for ownership of the namespace of the given resource ID.
* Reverts with AccessDenied if the check fails.
* Reverts with World_AccessDenied if the check fails.
*/
function requireOwner(ResourceId resourceId, address caller) internal view {
if (NamespaceOwner._get(ResourceId.unwrap(resourceId.getNamespaceId())) != caller) {
revert IWorldErrors.AccessDenied(resourceId.toString(), caller);
revert IWorldErrors.World_AccessDenied(resourceId.toString(), caller);
}
}
}
2 changes: 1 addition & 1 deletion packages/world/src/SystemCall.sol
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ library SystemCall {
(address systemAddress, bool publicAccess) = Systems._get(ResourceId.unwrap(systemId));

// Check if the system exists
if (systemAddress == address(0)) revert IWorldErrors.ResourceNotFound(systemId, systemId.toString());
if (systemAddress == address(0)) revert IWorldErrors.World_ResourceNotFound(systemId, systemId.toString());

// Allow access if the system is public or the caller has access to the namespace or name
if (!publicAccess) AccessControl.requireAccess(systemId, caller);
Expand Down
8 changes: 4 additions & 4 deletions packages/world/src/World.sol
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ contract World is StoreRead, IStoreData, IWorldKernel {
function initialize(IModule coreModule) public {
// Only the initial creator of the World can initialize it
if (msg.sender != creator) {
revert AccessDenied(ROOT_NAMESPACE_ID.toString(), msg.sender);
revert World_AccessDenied(ROOT_NAMESPACE_ID.toString(), msg.sender);
}

// The World can only be initialized once
if (InstalledModules._get(CORE_MODULE_NAME, keccak256("")) != address(0)) {
revert WorldAlreadyInitialized();
revert World_AlreadyInitialized();
}

// Initialize the World by installing the core module
Expand Down Expand Up @@ -274,7 +274,7 @@ contract World is StoreRead, IStoreData, IWorldKernel {
return SystemCall.callWithHooksOrRevert(delegator, systemId, callData, msg.value);
}

revert DelegationNotFound(delegator, msg.sender);
revert World_DelegationNotFound(delegator, msg.sender);
}

/************************************************************************
Expand All @@ -297,7 +297,7 @@ contract World is StoreRead, IStoreData, IWorldKernel {
fallback() external payable {
(bytes32 systemId, bytes4 systemFunctionSelector) = FunctionSelectors._get(msg.sig);

if (systemId == 0) revert FunctionSelectorNotFound(msg.sig);
if (systemId == 0) revert World_FunctionSelectorNotFound(msg.sig);

// Replace function selector in the calldata with the system function selector
bytes memory callData = Bytes.setBytes4(msg.data, 0, systemFunctionSelector);
Expand Down
24 changes: 12 additions & 12 deletions packages/world/src/interfaces/IWorldErrors.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ pragma solidity >=0.8.21;
import { ResourceId } from "@latticexyz/store/src/ResourceId.sol";

interface IWorldErrors {
error WorldAlreadyInitialized();
error ResourceExists(ResourceId resourceId, string resourceIdString);
error ResourceNotFound(ResourceId resourceId, string resourceIdString);
error AccessDenied(string resource, address caller);
error InvalidResourceId(ResourceId resourceId, string resourceIdString);
error SystemExists(address system);
error FunctionSelectorExists(bytes4 functionSelector);
error FunctionSelectorNotFound(bytes4 functionSelector);
error DelegationNotFound(address delegator, address delegatee);
error InsufficientBalance(uint256 balance, uint256 amount);
error InterfaceNotSupported(address contractAddress, bytes4 interfaceId);
error InvalidResourceType(bytes2 expected, ResourceId resourceId, string resourceIdString);
error World_AlreadyInitialized();
error World_ResourceAlreadyExists(ResourceId resourceId, string resourceIdString);
error World_ResourceNotFound(ResourceId resourceId, string resourceIdString);
error World_AccessDenied(string resource, address caller);
error World_InvalidResourceId(ResourceId resourceId, string resourceIdString);
error World_SystemAlreadyExists(address system);
error World_FunctionSelectorAlreadyExists(bytes4 functionSelector);
error World_FunctionSelectorNotFound(bytes4 functionSelector);
error World_DelegationNotFound(address delegator, address delegatee);
error World_InsufficientBalance(uint256 balance, uint256 amount);
error World_InterfaceNotSupported(address contractAddress, bytes4 interfaceId);
error World_InvalidResourceType(bytes2 expected, ResourceId resourceId, string resourceIdString);
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ contract BalanceTransferSystem is System, IWorldErrors {
) public virtual {
// Require the target ID to be a namespace ID
if (toNamespaceId.getType() != RESOURCE_NAMESPACE) {
revert InvalidResourceType(RESOURCE_NAMESPACE, toNamespaceId, toNamespaceId.toString());
revert World_InvalidResourceType(RESOURCE_NAMESPACE, toNamespaceId, toNamespaceId.toString());
}

// Require caller to have access to the namespace
Expand All @@ -36,7 +36,7 @@ contract BalanceTransferSystem is System, IWorldErrors {
uint256 balance = Balances._get(ResourceId.unwrap(fromNamespaceId));

// Require the balance balance to be greater or equal to the amount to transfer
if (amount > balance) revert InsufficientBalance(balance, amount);
if (amount > balance) revert World_InsufficientBalance(balance, amount);

// Update the balances
Balances._set(ResourceId.unwrap(fromNamespaceId), balance - amount);
Expand All @@ -54,7 +54,7 @@ contract BalanceTransferSystem is System, IWorldErrors {
uint256 balance = Balances._get(ResourceId.unwrap(fromNamespaceId));

// Require the balance balance to be greater or equal to the amount to transfer
if (amount > balance) revert InsufficientBalance(balance, amount);
if (amount > balance) revert World_InsufficientBalance(balance, amount);

// Update the balances
Balances._set(ResourceId.unwrap(fromNamespaceId), balance - amount);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ contract StoreRegistrationSystem is System, IWorldErrors {
string[] calldata fieldNames
) public virtual {
// Require the name to not be the namespace's root name
if (tableId.getName() == ROOT_NAME) revert InvalidResourceId(tableId, tableId.toString());
if (tableId.getName() == ROOT_NAME) revert World_InvalidResourceId(tableId, tableId.toString());

// If the namespace doesn't exist yet, register it
ResourceId namespaceId = tableId.getNamespaceId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ contract WorldRegistrationSystem is System, IWorldErrors {
function registerNamespace(ResourceId namespaceId) public virtual {
// Require the provided namespace ID to have type RESOURCE_NAMESPACE
if (namespaceId.getType() != RESOURCE_NAMESPACE) {
revert InvalidResourceType(RESOURCE_NAMESPACE, namespaceId, namespaceId.toString());
revert World_InvalidResourceType(RESOURCE_NAMESPACE, namespaceId, namespaceId.toString());
}

// Require namespace to not exist yet
if (ResourceIds._getExists(ResourceId.unwrap(namespaceId))) {
revert ResourceExists(namespaceId, namespaceId.toString());
revert World_ResourceAlreadyExists(namespaceId, namespaceId.toString());
}

// Register namespace resource ID
Expand Down Expand Up @@ -97,19 +97,19 @@ contract WorldRegistrationSystem is System, IWorldErrors {
function registerSystem(ResourceId systemId, WorldContextConsumer system, bool publicAccess) public virtual {
// Require the provided system ID to have type RESOURCE_SYSTEM
if (systemId.getType() != RESOURCE_SYSTEM) {
revert InvalidResourceType(RESOURCE_SYSTEM, systemId, systemId.toString());
revert World_InvalidResourceType(RESOURCE_SYSTEM, systemId, systemId.toString());
}

// Require the provided address to implement the WorldContextConsumer interface
requireInterface(address(system), WORLD_CONTEXT_CONSUMER_INTERFACE_ID);

// Require the name to not be the namespace's root name
if (systemId.getName() == ROOT_NAME) revert InvalidResourceId(systemId, systemId.toString());
if (systemId.getName() == ROOT_NAME) revert World_InvalidResourceId(systemId, systemId.toString());

// Require this system to not be registered at a different system ID yet
bytes32 existingSystemId = SystemRegistry._get(address(system));
if (existingSystemId != 0 && existingSystemId != ResourceId.unwrap(systemId)) {
revert SystemExists(address(system));
revert World_SystemAlreadyExists(address(system));
}

// If the namespace doesn't exist yet, register it
Expand Down Expand Up @@ -171,7 +171,7 @@ contract WorldRegistrationSystem is System, IWorldErrors {
// Require the function selector to be globally unique
bytes32 existingSystemId = FunctionSelectors._getSystemId(worldFunctionSelector);

if (existingSystemId != 0) revert FunctionSelectorExists(worldFunctionSelector);
if (existingSystemId != 0) revert World_FunctionSelectorAlreadyExists(worldFunctionSelector);

// Register the function selector
bytes memory systemFunctionSignature = abi.encodePacked(systemFunctionName, systemFunctionArguments);
Expand Down Expand Up @@ -199,7 +199,7 @@ contract WorldRegistrationSystem is System, IWorldErrors {
// Require the function selector to be globally unique
bytes32 existingSystemId = FunctionSelectors._getSystemId(worldFunctionSelector);

if (existingSystemId != 0) revert FunctionSelectorExists(worldFunctionSelector);
if (existingSystemId != 0) revert World_FunctionSelectorAlreadyExists(worldFunctionSelector);

// Register the function selector
FunctionSelectors._set(worldFunctionSelector, ResourceId.unwrap(systemId), systemFunctionSelector);
Expand Down
4 changes: 2 additions & 2 deletions packages/world/src/requireInterface.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import { IWorldErrors } from "./interfaces/IWorldErrors.sol";
function requireInterface(address contractAddress, bytes4 interfaceId) view {
try IERC165(contractAddress).supportsInterface(interfaceId) returns (bool supported) {
if (!supported) {
revert IWorldErrors.InterfaceNotSupported(contractAddress, interfaceId);
revert IWorldErrors.World_InterfaceNotSupported(contractAddress, interfaceId);
}
} catch {
revert IWorldErrors.InterfaceNotSupported(contractAddress, interfaceId);
revert IWorldErrors.World_InterfaceNotSupported(contractAddress, interfaceId);
}
}
2 changes: 1 addition & 1 deletion packages/world/test/AccessControl.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ contract AccessControlTest is Test, GasReporter, StoreMock {
function testRequireAccessRevert() public {
ResourceId tableId = _tableId;

vm.expectRevert(abi.encodeWithSelector(IWorldErrors.AccessDenied.selector, tableId.toString(), caller));
vm.expectRevert(abi.encodeWithSelector(IWorldErrors.World_AccessDenied.selector, tableId.toString(), caller));
AccessControl.requireAccess(tableId, caller);
}
}
8 changes: 4 additions & 4 deletions packages/world/test/StandardDelegationsModule.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ contract StandardDelegationsModuleTest is Test, GasReporter {

// Expect the delegation to have been used up
vm.prank(delegatee);
vm.expectRevert(abi.encodeWithSelector(IWorldErrors.DelegationNotFound.selector, delegator, delegatee));
vm.expectRevert(abi.encodeWithSelector(IWorldErrors.World_DelegationNotFound.selector, delegator, delegatee));
world.callFrom(delegator, systemId, abi.encodeCall(WorldTestSystem.msgSender, ()));
}

Expand Down Expand Up @@ -104,11 +104,11 @@ contract StandardDelegationsModuleTest is Test, GasReporter {
// Set the timestamp to maxTimestamp+1 and expect the delegation to be expired
vm.warp(maxTimestamp + 1);
vm.prank(delegatee);
vm.expectRevert(abi.encodeWithSelector(IWorldErrors.DelegationNotFound.selector, delegator, delegatee));
vm.expectRevert(abi.encodeWithSelector(IWorldErrors.World_DelegationNotFound.selector, delegator, delegatee));
world.callFrom(delegator, systemId, abi.encodeCall(WorldTestSystem.msgSender, ()));
}

function testRegisterDelegationRevertInterfaceNotSupported() public {
function testRegisterDelegationRevertWorld_InterfaceNotSupported() public {
// Register a system that is not a delegation control system
System noDelegationControlSystem = new System();
ResourceId noDelegationControlId = WorldResourceIdLib.encode({
Expand All @@ -122,7 +122,7 @@ contract StandardDelegationsModuleTest is Test, GasReporter {
vm.prank(delegator);
vm.expectRevert(
abi.encodeWithSelector(
IWorldErrors.InterfaceNotSupported.selector,
IWorldErrors.World_InterfaceNotSupported.selector,
address(noDelegationControlSystem),
DELEGATION_CONTROL_INTERFACE_ID
)
Expand Down
2 changes: 1 addition & 1 deletion packages/world/test/UniqueEntityModule.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ contract UniqueEntityModuleTest is Test, GasReporter {
// But changing the table directly isn't allowed
vm.expectRevert(
abi.encodeWithSelector(
IWorldErrors.AccessDenied.selector,
IWorldErrors.World_AccessDenied.selector,
WorldResourceIdLib.encode({ typeId: RESOURCE_TABLE, namespace: NAMESPACE, name: TABLE_NAME }).toString(),
alice
)
Expand Down
Loading

0 comments on commit 841381e

Please sign in to comment.