From 7090e77890c217da9560461f1d06d7ea9a9a0f27 Mon Sep 17 00:00:00 2001 From: Fraser Scott Date: Thu, 18 Jan 2024 13:58:09 +0000 Subject: [PATCH] feat(world): add renounceOwnership function --- packages/world/gas-report.json | 2 +- .../interfaces/IAccessManagementSystem.sol | 2 ++ .../world/src/modules/core/CoreModule.sol | 5 ++-- .../AccessManagementSystem.sol | 16 +++++++++++ packages/world/test/World.t.sol | 28 ++++++++++++++++++- 5 files changed, 49 insertions(+), 4 deletions(-) diff --git a/packages/world/gas-report.json b/packages/world/gas-report.json index a3e650f9d0..05918f0533 100644 --- a/packages/world/gas-report.json +++ b/packages/world/gas-report.json @@ -63,7 +63,7 @@ "file": "test/Factories.t.sol", "test": "testWorldFactory", "name": "deploy world via WorldFactory", - "gasUsed": 12514303 + "gasUsed": 12579230 }, { "file": "test/World.t.sol", diff --git a/packages/world/src/codegen/interfaces/IAccessManagementSystem.sol b/packages/world/src/codegen/interfaces/IAccessManagementSystem.sol index 0188232be4..187b16baa6 100644 --- a/packages/world/src/codegen/interfaces/IAccessManagementSystem.sol +++ b/packages/world/src/codegen/interfaces/IAccessManagementSystem.sol @@ -15,4 +15,6 @@ interface IAccessManagementSystem { function revokeAccess(ResourceId resourceId, address grantee) external; function transferOwnership(ResourceId namespaceId, address newOwner) external; + + function renounceOwnership(ResourceId namespaceId) external; } diff --git a/packages/world/src/modules/core/CoreModule.sol b/packages/world/src/modules/core/CoreModule.sol index 9a2d1979a5..ce370dfd8f 100644 --- a/packages/world/src/modules/core/CoreModule.sol +++ b/packages/world/src/modules/core/CoreModule.sol @@ -140,11 +140,12 @@ contract CoreModule is Module { * @dev Iterates through known function signatures and registers them. */ function _registerFunctionSelectors() internal { - string[3] memory functionSignaturesAccessManagement = [ + string[4] memory functionSignaturesAccessManagement = [ // --- AccessManagementSystem --- "grantAccess(bytes32,address)", "revokeAccess(bytes32,address)", - "transferOwnership(bytes32,address)" + "transferOwnership(bytes32,address)", + "renounceOwnership(bytes32)" ]; for (uint256 i = 0; i < functionSignaturesAccessManagement.length; i++) { _registerRootFunctionSelector(ACCESS_MANAGEMENT_SYSTEM_ID, functionSignaturesAccessManagement[i]); diff --git a/packages/world/src/modules/core/implementations/AccessManagementSystem.sol b/packages/world/src/modules/core/implementations/AccessManagementSystem.sol index 564a6a4e33..04a9c2cea8 100644 --- a/packages/world/src/modules/core/implementations/AccessManagementSystem.sol +++ b/packages/world/src/modules/core/implementations/AccessManagementSystem.sol @@ -59,4 +59,20 @@ contract AccessManagementSystem is System { // Grant access to new owner ResourceAccess._set(namespaceId, newOwner, true); } + + /** + * @notice Renounces ownership of the given namespace + * @dev Requires the caller to own the namespace. Revoke ResourceAccess for previous owner + * @param namespaceId The ID of the namespace to transfer ownership. + */ + function renounceOwnership(ResourceId namespaceId) public virtual { + // Require the caller to own the namespace + AccessControl.requireOwner(namespaceId, _msgSender()); + + // Delete namespace owner + NamespaceOwner._deleteRecord(namespaceId); + + // Revoke access from old owner + ResourceAccess._deleteRecord(namespaceId, _msgSender()); + } } diff --git a/packages/world/test/World.t.sol b/packages/world/test/World.t.sol index 3e9f6db4d3..c05d59f647 100644 --- a/packages/world/test/World.t.sol +++ b/packages/world/test/World.t.sol @@ -225,11 +225,12 @@ contract WorldTest is Test, GasReporter { CoreRegistrationSystem coreRegistrationSystem = CoreRegistrationSystem( Systems.getSystem(CORE_REGISTRATION_SYSTEM_ID) ); - bytes4[19] memory coreFunctionSignatures = [ + bytes4[20] memory coreFunctionSignatures = [ // --- AccessManagementSystem --- AccessManagementSystem.grantAccess.selector, AccessManagementSystem.revokeAccess.selector, AccessManagementSystem.transferOwnership.selector, + AccessManagementSystem.renounceOwnership.selector, // --- BalanceTransferSystem --- BalanceTransferSystem.transferBalanceToNamespace.selector, BalanceTransferSystem.transferBalanceToAddress.selector, @@ -426,6 +427,31 @@ contract WorldTest is Test, GasReporter { world.transferOwnership(namespaceId, address(1)); } + function testRenounceNamespace() public { + bytes14 namespace = "testRenounce"; + ResourceId namespaceId = WorldResourceIdLib.encodeNamespace(namespace); + + world.registerNamespace(namespaceId); + + // Expect the new owner to not be namespace owner before transfer + assertFalse( + (NamespaceOwner.get(namespaceId)) == address(0), + "new owner should not be namespace owner before transfer" + ); + + world.renounceOwnership(namespaceId); + + // Expect the new owner to be zero address + assertEq(NamespaceOwner.get(namespaceId), address(0), "zero address should be namespace owner"); + + // Expect previous owner to no longer have access + assertEq(ResourceAccess.get(namespaceId, address(this)), false, "caller should no longer have access"); + + // Expect revert if caller is not the owner + _expectAccessDenied(address(this), namespace, 0, RESOURCE_NAMESPACE); + world.renounceOwnership(namespaceId); + } + function testRegisterTable() public { FieldLayout fieldLayout = FieldLayoutEncodeHelper.encode(1, 32, 1); Schema valueSchema = SchemaEncodeHelper.encode(SchemaType.BOOL, SchemaType.UINT256, SchemaType.STRING);