Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(world): prevent the World from calling itself #1563

Merged
merged 19 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions .changeset/brown-garlics-lie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
"@latticexyz/world": major
---

All `World` methods now revert if the `World` calls itself.
The `World` should never need to externally call itself, since all internal table operations happen via library calls, and all root system operations happen via delegate call.

It should not be possible to make the `World` call itself as an external actor.
If it were possible to make the `World` call itself, it would be possible to write to internal tables that only the `World` should have access to.
As this is a very important invariance, we made it explicit in a requirement check in every `World` method, rather than just relying on making it impossible to trigger the `World` to call itself.

This is a breaking change for modules that previously used external calls to the `World` in the `installRoot` method.
In the `installRoot` method, the `World` can only be called via `delegatecall`, and table operations should be performed via the internal table methods (e.g. `_set` instead of `set`).

Example for how to replace external calls to `world` in root systems / root modules (`installRoot`) with `delegatecall`:

```diff
+ import { revertWithBytes } from "@latticexyz/world/src/revertWithBytes.sol";

- world.grantAccess(tableId, address(hook));
+ (bool success, bytes memory returnData) = address(world).delegatecall(
+ abi.encodeCall(world.grantAccess, (tableId, address(hook)))
+ );

+ if (!success) revertWithBytes(returnData);
```
108 changes: 51 additions & 57 deletions packages/world/gas-report.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,109 +3,103 @@
"file": "test/AccessControl.t.sol",
"test": "testAccessControl",
"name": "AccessControl: hasAccess (cold)",
"gasUsed": 7178
"gasUsed": 7134
},
{
"file": "test/AccessControl.t.sol",
"test": "testAccessControl",
"name": "AccessControl: hasAccess (warm, namespace only)",
"gasUsed": 1726
"gasUsed": 1682
},
{
"file": "test/AccessControl.t.sol",
"test": "testAccessControl",
"name": "AccessControl: hasAccess (warm)",
"gasUsed": 3186
"gasUsed": 3142
},
{
"file": "test/AccessControl.t.sol",
"test": "testRequireAccess",
"name": "AccessControl: requireAccess (cold)",
"gasUsed": 7221
"gasUsed": 7177
},
{
"file": "test/AccessControl.t.sol",
"test": "testRequireAccess",
"name": "AccessControl: requireAccess (warm)",
"gasUsed": 3224
},
{
"file": "test/AccessControl.t.sol",
"test": "testRequireAccess",
"name": "AccessControl: requireAccess (this address)",
"gasUsed": 153
"gasUsed": 3180
},
{
"file": "test/CallBatch.t.sol",
"test": "testCallBatchReturnData",
"name": "call systems with callBatch",
"gasUsed": 45546
"gasUsed": 45624
},
{
"file": "test/KeysInTableModule.t.sol",
"test": "testInstallComposite",
"name": "install keys in table module",
"gasUsed": 1415069
"gasUsed": 1415285
},
{
"file": "test/KeysInTableModule.t.sol",
"test": "testInstallGas",
"name": "install keys in table module",
"gasUsed": 1415069
"gasUsed": 1415285
},
{
"file": "test/KeysInTableModule.t.sol",
"test": "testInstallGas",
"name": "set a record on a table with keysInTableModule installed",
"gasUsed": 158293
"gasUsed": 158224
},
{
"file": "test/KeysInTableModule.t.sol",
"test": "testInstallSingleton",
"name": "install keys in table module",
"gasUsed": 1415069
"gasUsed": 1415285
},
{
"file": "test/KeysInTableModule.t.sol",
"test": "testSetAndDeleteRecordHookCompositeGas",
"name": "install keys in table module",
"gasUsed": 1415069
"gasUsed": 1415285
},
{
"file": "test/KeysInTableModule.t.sol",
"test": "testSetAndDeleteRecordHookCompositeGas",
"name": "change a composite record on a table with keysInTableModule installed",
"gasUsed": 22265
"gasUsed": 22242
},
{
"file": "test/KeysInTableModule.t.sol",
"test": "testSetAndDeleteRecordHookCompositeGas",
"name": "delete a composite record on a table with keysInTableModule installed",
"gasUsed": 160800
"gasUsed": 160593
},
{
"file": "test/KeysInTableModule.t.sol",
"test": "testSetAndDeleteRecordHookGas",
"name": "install keys in table module",
"gasUsed": 1415069
"gasUsed": 1415285
},
{
"file": "test/KeysInTableModule.t.sol",
"test": "testSetAndDeleteRecordHookGas",
"name": "change a record on a table with keysInTableModule installed",
"gasUsed": 20987
"gasUsed": 20964
},
{
"file": "test/KeysInTableModule.t.sol",
"test": "testSetAndDeleteRecordHookGas",
"name": "delete a record on a table with keysInTableModule installed",
"gasUsed": 85824
"gasUsed": 85709
},
{
"file": "test/KeysWithValueModule.t.sol",
"test": "testGetKeysWithValueGas",
"name": "install keys with value module",
"gasUsed": 665448
"gasUsed": 656381
},
{
"file": "test/KeysWithValueModule.t.sol",
Expand All @@ -123,49 +117,49 @@
"file": "test/KeysWithValueModule.t.sol",
"test": "testInstall",
"name": "install keys with value module",
"gasUsed": 665448
"gasUsed": 656381
},
{
"file": "test/KeysWithValueModule.t.sol",
"test": "testInstall",
"name": "set a record on a table with KeysWithValueModule installed",
"gasUsed": 135704
"gasUsed": 135635
},
{
"file": "test/KeysWithValueModule.t.sol",
"test": "testSetAndDeleteRecordHook",
"name": "install keys with value module",
"gasUsed": 665448
"gasUsed": 656381
},
{
"file": "test/KeysWithValueModule.t.sol",
"test": "testSetAndDeleteRecordHook",
"name": "change a record on a table with KeysWithValueModule installed",
"gasUsed": 106045
"gasUsed": 105976
},
{
"file": "test/KeysWithValueModule.t.sol",
"test": "testSetAndDeleteRecordHook",
"name": "delete a record on a table with KeysWithValueModule installed",
"gasUsed": 35455
"gasUsed": 35409
},
{
"file": "test/KeysWithValueModule.t.sol",
"test": "testSetField",
"name": "install keys with value module",
"gasUsed": 665448
"gasUsed": 656381
},
{
"file": "test/KeysWithValueModule.t.sol",
"test": "testSetField",
"name": "set a field on a table with KeysWithValueModule installed",
"gasUsed": 148453
"gasUsed": 148384
},
{
"file": "test/KeysWithValueModule.t.sol",
"test": "testSetField",
"name": "change a field on a table with KeysWithValueModule installed",
"gasUsed": 113212
"gasUsed": 113143
},
{
"file": "test/query.t.sol",
Expand Down Expand Up @@ -237,157 +231,157 @@
"file": "test/StandardDelegationsModule.t.sol",
"test": "testCallFromCallboundDelegation",
"name": "register a callbound delegation",
"gasUsed": 114729
"gasUsed": 114771
},
{
"file": "test/StandardDelegationsModule.t.sol",
"test": "testCallFromCallboundDelegation",
"name": "call a system via a callbound delegation",
"gasUsed": 33993
"gasUsed": 34014
},
{
"file": "test/StandardDelegationsModule.t.sol",
"test": "testCallFromTimeboundDelegation",
"name": "register a timebound delegation",
"gasUsed": 109224
"gasUsed": 109266
},
{
"file": "test/StandardDelegationsModule.t.sol",
"test": "testCallFromTimeboundDelegation",
"name": "call a system via a timebound delegation",
"gasUsed": 26998
"gasUsed": 27019
},
{
"file": "test/UniqueEntityModule.t.sol",
"test": "testInstall",
"name": "install unique entity module",
"gasUsed": 690539
"gasUsed": 680060
},
{
"file": "test/UniqueEntityModule.t.sol",
"test": "testInstall",
"name": "get a unique entity nonce (non-root module)",
"gasUsed": 52176
"gasUsed": 52189
},
{
"file": "test/UniqueEntityModule.t.sol",
"test": "testInstallRoot",
"name": "installRoot unique entity module",
"gasUsed": 680735
"gasUsed": 647141
},
{
"file": "test/UniqueEntityModule.t.sol",
"test": "testInstallRoot",
"name": "get a unique entity nonce (root module)",
"gasUsed": 52176
"gasUsed": 52189
},
{
"file": "test/World.t.sol",
"test": "testCall",
"name": "call a system via the World",
"gasUsed": 12702
"gasUsed": 12679
},
{
"file": "test/World.t.sol",
"test": "testCallFromUnlimitedDelegation",
"name": "register an unlimited delegation",
"gasUsed": 50617
"gasUsed": 50659
},
{
"file": "test/World.t.sol",
"test": "testCallFromUnlimitedDelegation",
"name": "call a system via an unlimited delegation",
"gasUsed": 12895
"gasUsed": 12916
},
{
"file": "test/World.t.sol",
"test": "testDeleteRecord",
"name": "Delete record",
"gasUsed": 9197
"gasUsed": 9174
},
{
"file": "test/World.t.sol",
"test": "testPushToField",
"name": "Push data to the table",
"gasUsed": 86871
"gasUsed": 86848
},
{
"file": "test/World.t.sol",
"test": "testRegisterFallbackSystem",
"name": "Register a fallback system",
"gasUsed": 59347
"gasUsed": 59383
},
{
"file": "test/World.t.sol",
"test": "testRegisterFallbackSystem",
"name": "Register a root fallback system",
"gasUsed": 52972
"gasUsed": 53008
},
{
"file": "test/World.t.sol",
"test": "testRegisterFunctionSelector",
"name": "Register a function selector",
"gasUsed": 79913
"gasUsed": 79949
},
{
"file": "test/World.t.sol",
"test": "testRegisterNamespace",
"name": "Register a new namespace",
"gasUsed": 123225
"gasUsed": 123261
},
{
"file": "test/World.t.sol",
"test": "testRegisterRootFunctionSelector",
"name": "Register a root function selector",
"gasUsed": 74885
"gasUsed": 74921
},
{
"file": "test/World.t.sol",
"test": "testRegisterSystem",
"name": "register a system",
"gasUsed": 165772
"gasUsed": 165814
},
{
"file": "test/World.t.sol",
"test": "testRegisterTable",
"name": "Register a new table in the namespace",
"gasUsed": 651898
"gasUsed": 641197
},
{
"file": "test/World.t.sol",
"test": "testSetField",
"name": "Write data to a table field",
"gasUsed": 37387
"gasUsed": 37364
},
{
"file": "test/World.t.sol",
"test": "testSetRecord",
"name": "Write data to the table",
"gasUsed": 35381
"gasUsed": 36490
},
{
"file": "test/WorldDynamicUpdate.t.sol",
"test": "testPopFromField",
"name": "pop 1 address (cold)",
"gasUsed": 24672
"gasUsed": 24649
},
{
"file": "test/WorldDynamicUpdate.t.sol",
"test": "testPopFromField",
"name": "pop 1 address (warm)",
"gasUsed": 13818
"gasUsed": 13795
},
{
"file": "test/WorldDynamicUpdate.t.sol",
"test": "testUpdateInField",
"name": "updateInField 1 item (cold)",
"gasUsed": 25236
"gasUsed": 25213
},
{
"file": "test/WorldDynamicUpdate.t.sol",
"test": "testUpdateInField",
"name": "updateInField 1 item (warm)",
"gasUsed": 14441
"gasUsed": 14418
},
{
"file": "test/WorldResourceId.t.sol",
Expand Down
Loading
Loading