Skip to content

Commit

Permalink
feat(world-modules): system bound delegation control (#1885)
Browse files Browse the repository at this point in the history
Co-authored-by: alvarius <[email protected]>
Co-authored-by: Kevin Ingersoll <[email protected]>
  • Loading branch information
3 people authored Nov 10, 2023
1 parent 1327ea8 commit ca226bb
Show file tree
Hide file tree
Showing 8 changed files with 423 additions and 5 deletions.
16 changes: 14 additions & 2 deletions packages/world-modules/gas-report.json
Original file line number Diff line number Diff line change
Expand Up @@ -225,19 +225,31 @@
"file": "test/StandardDelegationsModule.t.sol",
"test": "testCallFromCallboundDelegation",
"name": "register a callbound delegation",
"gasUsed": 117420
"gasUsed": 117426
},
{
"file": "test/StandardDelegationsModule.t.sol",
"test": "testCallFromCallboundDelegation",
"name": "call a system via a callbound delegation",
"gasUsed": 36688
},
{
"file": "test/StandardDelegationsModule.t.sol",
"test": "testCallFromSystemDelegation",
"name": "register a systembound delegation",
"gasUsed": 114982
},
{
"file": "test/StandardDelegationsModule.t.sol",
"test": "testCallFromSystemDelegation",
"name": "call a system via a systembound delegation",
"gasUsed": 33831
},
{
"file": "test/StandardDelegationsModule.t.sol",
"test": "testCallFromTimeboundDelegation",
"name": "register a timebound delegation",
"gasUsed": 111914
"gasUsed": 111920
},
{
"file": "test/StandardDelegationsModule.t.sol",
Expand Down
11 changes: 11 additions & 0 deletions packages/world-modules/mud.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,17 @@ export default mudConfig({
availableCalls: "uint256",
},
},
SystemboundDelegations: {
directory: "modules/std-delegations/tables",
keySchema: {
delegator: "address",
delegatee: "address",
systemId: "ResourceId",
},
valueSchema: {
availableCalls: "uint256",
},
},
TimeboundDelegations: {
directory: "modules/std-delegations/tables",
keySchema: {
Expand Down
1 change: 1 addition & 0 deletions packages/world-modules/src/index.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { KeysInTable, KeysInTableData, KeysInTableTableId } from "./modules/keys
import { UsedKeysIndex, UsedKeysIndexTableId } from "./modules/keysintable/tables/UsedKeysIndex.sol";
import { UniqueEntity } from "./modules/uniqueentity/tables/UniqueEntity.sol";
import { CallboundDelegations, CallboundDelegationsTableId } from "./modules/std-delegations/tables/CallboundDelegations.sol";
import { SystemboundDelegations, SystemboundDelegationsTableId } from "./modules/std-delegations/tables/SystemboundDelegations.sol";
import { TimeboundDelegations, TimeboundDelegationsTableId } from "./modules/std-delegations/tables/TimeboundDelegations.sol";
import { PuppetRegistry } from "./modules/puppet/tables/PuppetRegistry.sol";
import { Balances } from "./modules/tokens/tables/Balances.sol";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,20 @@ import { WorldContextConsumer } from "@latticexyz/world/src/WorldContext.sol";
import { revertWithBytes } from "@latticexyz/world/src/revertWithBytes.sol";

import { CallboundDelegationControl } from "./CallboundDelegationControl.sol";
import { SystemboundDelegationControl } from "./SystemboundDelegationControl.sol";
import { TimeboundDelegationControl } from "./TimeboundDelegationControl.sol";
import { MODULE_NAME, CALLBOUND_DELEGATION, TIMEBOUND_DELEGATION } from "./constants.sol";
import { MODULE_NAME, CALLBOUND_DELEGATION, SYSTEMBOUND_DELEGATION, TIMEBOUND_DELEGATION } from "./constants.sol";

import { CallboundDelegations } from "./tables/CallboundDelegations.sol";
import { SystemboundDelegations } from "./tables/SystemboundDelegations.sol";
import { TimeboundDelegations } from "./tables/TimeboundDelegations.sol";

/**
* This module registers tables and delegation control systems required for standard delegations
*/
contract StandardDelegationsModule is Module {
CallboundDelegationControl private immutable callboundDelegationControl = new CallboundDelegationControl();
SystemboundDelegationControl private immutable systemboundDelegationControl = new SystemboundDelegationControl();
TimeboundDelegationControl private immutable timeboundDelegationControl = new TimeboundDelegationControl();

function getName() public pure returns (bytes16) {
Expand All @@ -30,6 +33,7 @@ contract StandardDelegationsModule is Module {

// Register tables
CallboundDelegations.register();
SystemboundDelegations.register();
TimeboundDelegations.register();

// Register systems
Expand All @@ -38,6 +42,11 @@ contract StandardDelegationsModule is Module {
);
if (!success) revertWithBytes(returnData);

(success, returnData) = address(world).delegatecall(
abi.encodeCall(world.registerSystem, (SYSTEMBOUND_DELEGATION, systemboundDelegationControl, true))
);
if (!success) revertWithBytes(returnData);

(success, returnData) = address(world).delegatecall(
abi.encodeCall(world.registerSystem, (TIMEBOUND_DELEGATION, timeboundDelegationControl, true))
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.21;

import { DelegationControl } from "@latticexyz/world/src/DelegationControl.sol";
import { ResourceId } from "@latticexyz/world/src/WorldResourceId.sol";
import { SystemboundDelegations } from "./tables/SystemboundDelegations.sol";

contract SystemboundDelegationControl is DelegationControl {
/**
* Verify a delegation by checking if the caller (delegatee) has any available calls left for the given delegator in the SystemboundDelegations table and decrementing the available calls if so.
*/
function verify(address delegator, ResourceId systemId, bytes memory) public returns (bool) {
// Get the number of available calls for the given delegator, systemId and callData
uint256 availableCalls = SystemboundDelegations.get({
delegator: delegator,
delegatee: _msgSender(),
systemId: systemId
});

if (availableCalls == 1) {
// Remove the delegation from the SystemboundDelegations table
SystemboundDelegations.deleteRecord({ delegator: delegator, delegatee: _msgSender(), systemId: systemId });
return true;
}

if (availableCalls > 0) {
// Decrement the number of available calls
unchecked {
availableCalls--;
}
SystemboundDelegations.set({
delegator: delegator,
delegatee: _msgSender(),
systemId: systemId,
availableCalls: availableCalls
});
return true;
}

return false;
}

/**
* Initialize a delegation by setting the number of available calls in the SystemboundDelegations table
*/
function initDelegation(address delegatee, ResourceId systemId, uint256 numCalls) public {
SystemboundDelegations.set({
delegator: _msgSender(),
delegatee: delegatee,
systemId: systemId,
availableCalls: numCalls
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ ResourceId constant CALLBOUND_DELEGATION = ResourceId.wrap(
bytes32(abi.encodePacked(RESOURCE_SYSTEM, ROOT_NAMESPACE, bytes16("callbound")))
);

// Systembound delegation
ResourceId constant SYSTEMBOUND_DELEGATION = ResourceId.wrap(
bytes32(abi.encodePacked(RESOURCE_SYSTEM, ROOT_NAMESPACE, bytes16("systembound")))
);

// Timebound delegation
ResourceId constant TIMEBOUND_DELEGATION = ResourceId.wrap(
bytes32(abi.encodePacked(RESOURCE_SYSTEM, ROOT_NAMESPACE, bytes16("timebound")))
Expand Down
Loading

0 comments on commit ca226bb

Please sign in to comment.