Skip to content

Commit

Permalink
test(store): add gas tests for AccessControl (#1323)
Browse files Browse the repository at this point in the history
  • Loading branch information
dk1a authored Aug 22, 2023
1 parent db28a6d commit c4b3cce
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 8 deletions.
36 changes: 36 additions & 0 deletions packages/world/gas-report.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,40 @@
[
{
"file": "test/AccessControl.t.sol",
"test": "testAccessControl",
"name": "AccessControl: hasAccess (cold)",
"gasUsed": 14088
},
{
"file": "test/AccessControl.t.sol",
"test": "testAccessControl",
"name": "AccessControl: hasAccess (warm, namespace only)",
"gasUsed": 4122
},
{
"file": "test/AccessControl.t.sol",
"test": "testAccessControl",
"name": "AccessControl: hasAccess (warm)",
"gasUsed": 8121
},
{
"file": "test/AccessControl.t.sol",
"test": "testRequireAccess",
"name": "AccessControl: requireAccess (cold)",
"gasUsed": 14131
},
{
"file": "test/AccessControl.t.sol",
"test": "testRequireAccess",
"name": "AccessControl: requireAccess (warm)",
"gasUsed": 8137
},
{
"file": "test/AccessControl.t.sol",
"test": "testRequireAccess",
"name": "AccessControl: requireAccess (this address)",
"gasUsed": 153
},
{
"file": "test/KeysInTableModule.t.sol",
"test": "testInstallComposite",
Expand Down
53 changes: 45 additions & 8 deletions packages/world/test/AccessControl.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,57 @@
pragma solidity >=0.8.0;

import "forge-std/Test.sol";
import { GasReporter } from "@latticexyz/gas-report/src/GasReporter.sol";
import { StoreReadWithStubs } from "@latticexyz/store/src/StoreReadWithStubs.sol";

import { IWorldErrors } from "../src/interfaces/IWorldErrors.sol";
import { World } from "../src/World.sol";
import { AccessControl } from "../src/AccessControl.sol";
import { ResourceSelector } from "../src/ResourceSelector.sol";

import { ResourceAccess } from "../src/tables/ResourceAccess.sol";
import { NamespaceOwner } from "../src/tables/NamespaceOwner.sol";

contract AccessControlTest is Test, StoreReadWithStubs {
bytes16 namespace = "namespace";
bytes16 name = "name";
address caller = address(0x01);
contract AccessControlTest is Test, GasReporter, StoreReadWithStubs {
bytes16 constant namespace = "namespace";
bytes16 constant name = "name";
address constant presetCaller = address(0x0123);
address constant caller = address(0x01);

function setUp() public {
ResourceAccess.register();
NamespaceOwner.register();

NamespaceOwner.set(namespace, address(this));
ResourceAccess.set(ResourceSelector.from(namespace), address(this), true);
ResourceAccess.set(ResourceSelector.from(namespace, name), presetCaller, true);
}

function testAccessControl() public {
bool hasAccess;

// Check that the caller has no access to the namespace or name
assertFalse(AccessControl.hasAccess(ResourceSelector.from(namespace, name), caller));
startGasReport("AccessControl: hasAccess (cold)");
hasAccess = AccessControl.hasAccess(ResourceSelector.from(namespace, name), caller);
endGasReport();
assertFalse(hasAccess);

// Grant access to the namespace
ResourceAccess.set(ResourceSelector.from(namespace, 0), caller, true);

// Check that the caller has access to the namespace or name
assertTrue(AccessControl.hasAccess(ResourceSelector.from(namespace, name), caller));
startGasReport("AccessControl: hasAccess (warm, namespace only)");
hasAccess = AccessControl.hasAccess(ResourceSelector.from(namespace, name), caller);
endGasReport();
assertTrue(hasAccess);

// Revoke access to the namespace
ResourceAccess.set(ResourceSelector.from(namespace, 0), caller, false);

// Check that the caller has no access to the namespace or name
assertFalse(AccessControl.hasAccess(ResourceSelector.from(namespace, name), caller));
startGasReport("AccessControl: hasAccess (warm)");
hasAccess = AccessControl.hasAccess(ResourceSelector.from(namespace, name), caller);
endGasReport();
assertFalse(hasAccess);

// Grant access to the name
ResourceAccess.set(ResourceSelector.from(namespace, name), caller, true);
Expand All @@ -52,4 +66,27 @@ contract AccessControlTest is Test, StoreReadWithStubs {
// Check that the caller has no access to the namespace or name
assertFalse(AccessControl.hasAccess(ResourceSelector.from(namespace, name), caller));
}

function testRequireAccess() public {
bytes32 resourceSelector = ResourceSelector.from(namespace, name);
startGasReport("AccessControl: requireAccess (cold)");
AccessControl.requireAccess(resourceSelector, presetCaller);
endGasReport();

startGasReport("AccessControl: requireAccess (warm)");
AccessControl.requireAccess(resourceSelector, presetCaller);
endGasReport();

startGasReport("AccessControl: requireAccess (this address)");
AccessControl.requireAccess(resourceSelector, address(this));
endGasReport();
}

function testRequireAccessRevert() public {
bytes32 resourceSelector = ResourceSelector.from(namespace, name);
vm.expectRevert(
abi.encodeWithSelector(IWorldErrors.AccessDenied.selector, ResourceSelector.toString(resourceSelector), caller)
);
AccessControl.requireAccess(resourceSelector, caller);
}
}

0 comments on commit c4b3cce

Please sign in to comment.