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(store,world): replace ResourceSelector with ResourceId and WorldResourceId #1544

Merged
merged 48 commits into from Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from 47 commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
436f814
feat(store,world): add ResourceType, ResourceId
alvrs Sep 19, 2023
ead0f46
create ResourceId user type
alvrs Sep 19, 2023
6e478cd
refactor(store): use ResourceId instead of bytes32
alvrs Sep 19, 2023
b26630a
intermediate commit just in case
alvrs Sep 19, 2023
bbf982c
refactor world to use ResourceId instead of bytes32
alvrs Sep 19, 2023
c8421f0
rebuild artifacts
alvrs Sep 19, 2023
9d6e28a
gas report
alvrs Sep 19, 2023
2f80cd7
self-review
alvrs Sep 19, 2023
4bc95a2
self-review
alvrs Sep 19, 2023
896a275
Create stale-seahorses-pay.md
alvrs Sep 19, 2023
9eaf761
improve storeEventsAbi test for accuracy and clarity
holic Sep 20, 2023
9ad7956
fix missing indexed arg
holic Sep 20, 2023
2ce2905
table ID -> resource ID
holic Sep 20, 2023
8ed3a75
refactor resource ID utils
holic Sep 20, 2023
8b794df
add world resource types
holic Sep 20, 2023
b5efe9f
migrate most things
holic Sep 20, 2023
fd87f7b
fix logToTable test
holic Sep 20, 2023
010e08e
kick ci
holic Sep 20, 2023
d96262c
Merge branch 'main' into alvrs/resourcetype
alvrs Sep 20, 2023
b964236
replace hasOwn usage for now
holic Sep 20, 2023
4b92f09
prettier
alvrs Sep 20, 2023
437002f
rename config constants from SELECTOR to NAME
alvrs Sep 20, 2023
1c1d774
move tableId back to memory in tests to make gas reports comparable
alvrs Sep 20, 2023
711cb00
Update .changeset/stale-seahorses-pay.md
alvrs Sep 20, 2023
6fa3e89
Update .changeset/stale-seahorses-pay.md
alvrs Sep 20, 2023
f317806
Update .changeset/stale-seahorses-pay.md
alvrs Sep 20, 2023
64664af
Update packages/world/src/interfaces/IModule.sol
alvrs Sep 20, 2023
85590fa
rename errors
alvrs Sep 20, 2023
3509850
fix braces
alvrs Sep 20, 2023
43ad722
Update packages/world/src/modules/keyswithvalue/constants.sol
alvrs Sep 20, 2023
c0e39f8
Update packages/world/src/modules/keyswithvalue/getTargetTableId.sol
alvrs Sep 20, 2023
25ea104
Update packages/world/src/modules/keyswithvalue/getTargetTableId.sol
alvrs Sep 20, 2023
202c042
run prettier
alvrs Sep 20, 2023
01e6a64
update gas-report
alvrs Sep 20, 2023
c7030fa
feat(world): use `ResourceId namespaceId` for all methods acting on t…
alvrs Sep 20, 2023
4e2dcd5
remove isType
alvrs Sep 20, 2023
dccf072
feat: use named args for WorldResourceIdLib and ResourceIdLib
alvrs Sep 21, 2023
4cfadea
reorder arguments in WorldResourceIdLib and ResourceIdLib
alvrs Sep 21, 2023
cd90e05
change encoding order
alvrs Sep 21, 2023
33b4f89
update parser
alvrs Sep 21, 2023
53ad81b
rebuild artifacts
alvrs Sep 21, 2023
8db70ac
Apply suggestions from code review
alvrs Sep 21, 2023
41d3e12
Apply suggestions from code review
alvrs Sep 21, 2023
ee4f8c7
update test data
alvrs Sep 21, 2023
3b76172
Merge branch 'main' into alvrs/resourcetype
alvrs Sep 21, 2023
e4d97bc
Apply suggestions from code review
alvrs Sep 21, 2023
18aac90
update test data
alvrs Sep 21, 2023
de9124e
run prettier
alvrs Sep 21, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 25 additions & 0 deletions .changeset/mean-seals-nail.md
@@ -0,0 +1,25 @@
---
"@latticexyz/world": patch
"@latticexyz/store": patch
---

The `ResourceType` table is removed.
It was previously used to store the resource type for each resource ID in a `World`. This is no longer necessary as the [resource type is now encoded in the resource ID](https://github.com/latticexyz/mud/pull/1544).

To still be able to determine whether a given resource ID exists, a `ResourceIds` table has been added.
The previous `ResourceType` table was part of `World` and missed tables that were registered directly via `StoreCore.registerTable` instead of via `World.registerTable` (e.g. when a table was registered as part of a root module).
This problem is solved by the new table `ResourceIds` being part of `Store`.

`StoreCore`'s `hasTable` function was removed in favor of using `ResourceIds.getExists(tableId)` directly.

```diff
- import { ResourceType } from "@latticexyz/world/src/tables/ResourceType.sol";
- import { StoreCore } from "@latticexyz/store/src/StoreCore.sol";
+ import { ResourceIds } from "@latticexyz/store/src/codegen/tables/ResourceIds.sol";

- bool tableExists = StoreCore.hasTable(tableId);
+ bool tableExists = ResourceIds.getExists(tableId);

- bool systemExists = ResourceType.get(systemId) != Resource.NONE;
+ bool systemExists = ResourceIds.getExists(systemId);
```
25 changes: 25 additions & 0 deletions .changeset/real-students-exercise.md
@@ -0,0 +1,25 @@
---
"@latticexyz/world": major
---

All `World` methods acting on namespaces as resources have been updated to use `ResourceId namespaceId` as parameter instead of `bytes14 namespace`.
The reason for this change is to make it clearer when a namespace is used as resource, as opposed to being part of another resource's ID.

```diff
+ import { ResourceId } from "@latticexyz/store/src/ResourceId.sol";

IBaseWorld {
- function registerNamespace(bytes14 namespace) external;
+ function registerNamespace(ResourceId namespaceId) external;

- function transferOwnership(bytes14 namespace, address newOwner) external;
+ function transferOwnership(ResourceId namespaceId, address newOwner) external;

- function transferBalanceToNamespace(bytes14 fromNamespace, bytes14 toNamespace, uint256 amount) external;
+ function transferBalanceToNamespace(ResourceId fromNamespaceId, ResourceId toNamespaceId, uint256 amount) external;

- function transferBalanceToAddress(bytes14 fromNamespace, address toAddress, uint256 amount) external;
+ function transferBalanceToAddress(ResourceId fromNamespaceId, address toAddress, uint256 amount) external;
}

```
70 changes: 70 additions & 0 deletions .changeset/stale-seahorses-pay.md
@@ -0,0 +1,70 @@
---
"@latticexyz/cli": major
"@latticexyz/common": major
"@latticexyz/config": major
"@latticexyz/store": major
---

- `ResourceSelector` is replaced with `ResourceId`, `ResourceIdLib`, `ResourceIdInstance`, `WorldResourceIdLib` and `WorldResourceIdInstance`.
alvrs marked this conversation as resolved.
Show resolved Hide resolved

Previously a "resource selector" was a `bytes32` value with the first 16 bytes reserved for the resource's namespace, and the last 16 bytes reserved for the resource's name.
Now a "resource ID" is a `bytes32` value with the first 2 bytes reserved for the resource type, the next 14 bytes reserved for the resource's namespace, and the last 16 bytes reserved for the resource's name.

Previously `ResouceSelector` was a library and the resource selector type was a plain `bytes32`.
Now `ResourceId` is a user type, and the functionality is implemented in the `ResourceIdInstance` (for type) and `WorldResourceIdInstance` (for namespace and name) libraries.
We split the logic into two libraries, because `Store` now also uses `ResourceId` and needs to be aware of resource types, but not of namespaces/names.

```diff
- import { ResourceSelector } from "@latticexyz/world/src/ResourceSelector.sol";
+ import { ResourceId, ResourceIdInstance } from "@latticexyz/store/src/ResourceId.sol";
+ import { WorldResourceIdLib, WorldResourceIdInstance } from "@latticexyz/world/src/WorldResourceId.sol";
+ import { RESOURCE_SYSTEM } from "@latticexyz/world/src/worldResourceTypes.sol";

- bytes32 systemId = ResourceSelector.from("namespace", "name");
+ ResourceId systemId = WorldResourceIdLib.encode(RESOURCE_SYSTEM, "namespace", "name");

- using ResourceSelector for bytes32;
+ using WorldResourceIdInstance for ResourceId;
+ using ResourceIdInstance for ResourceId;

systemId.getName();
systemId.getNamespace();
+ systemId.getType();

```

- All `Store` and `World` methods now use the `ResourceId` type for `tableId`, `systemId`, `moduleId` and `namespaceId`.
All mentions of `resourceSelector` were renamed to `resourceId` or the more specific type (e.g. `tableId`, `systemId`)

```diff
import { ResourceId } from "@latticexyz/store/src/ResourceId.sol";

IStore {
function setRecord(
- bytes32 tableId,
+ ResourceId tableId,
bytes32[] calldata keyTuple,
bytes calldata staticData,
PackedCounter encodedLengths,
bytes calldata dynamicData,
FieldLayout fieldLayout
) external;

// Same for all other methods
}
```

```diff
import { ResourceId } from "@latticexyz/store/src/ResourceId.sol";

IBaseWorld {
function callFrom(
address delegator,
- bytes32 resourceSelector,
+ ResourceId systemId,
bytes memory callData
) external payable returns (bytes memory);

// Same for all other methods
}
```
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the whitespace alignment on these two diffs looks weird

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ugh gihub messed up my attempt at suggesting a chance, but I think for diffs you want a "gutter" where the + and - live

so basically everything in the code block should indent one level then dedent the +- to be at the left edge
(see your diff above)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oof yes that's what i did, i think prettier keeps messing this up again

3 changes: 2 additions & 1 deletion .prettierignore
@@ -1,4 +1,5 @@
dist
**/.next
.next
templates/phaser/packages/art
CODEOWNERS
out
8 changes: 6 additions & 2 deletions e2e/packages/contracts/src/codegen/tables/Multi.sol

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions e2e/packages/contracts/src/codegen/tables/Number.sol

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions e2e/packages/contracts/src/codegen/tables/NumberList.sol

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions e2e/packages/contracts/src/codegen/tables/Vector.sol

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions e2e/packages/sync-test/data/setContractData.ts
Expand Up @@ -2,7 +2,7 @@ import { Page } from "@playwright/test";
import { Data } from "./types";
import { encodeTestData } from "./encodeTestData";
import { callWorld } from "./callWorld";
import { tableIdToHex } from "@latticexyz/common";
import { resourceIdToHex } from "@latticexyz/common";

/**
* Writes contract data by calling `world.setRecord` via the client
Expand All @@ -14,7 +14,7 @@ export async function setContractData(page: Page, data: Data) {
for (const record of records) {
const promise = await callWorld(page, "setRecord", [
// TODO: add support for multiple namespaces after https://github.com/latticexyz/mud/issues/994 is resolved
tableIdToHex("", table),
resourceIdToHex({ type: "table", namespace: "", name: table }),
record.key,
record.staticData,
record.encodedLengths,
Expand Down
20 changes: 9 additions & 11 deletions examples/minimal/packages/contracts/script/PostDeploy.s.sol
Expand Up @@ -3,7 +3,8 @@ pragma solidity >=0.8.0;

import { Script } from "forge-std/Script.sol";
import { console } from "forge-std/console.sol";
import { ResourceSelector } from "@latticexyz/world/src/ResourceSelector.sol";
import { ResourceId, WorldResourceIdLib } from "@latticexyz/world/src/WorldResourceId.sol";
import { RESOURCE_SYSTEM } from "@latticexyz/world/src/worldResourceTypes.sol";
import { IWorld } from "../src/codegen/world/IWorld.sol";

import { MessageTable, MessageTableTableId } from "../src/codegen/index.sol";
Expand All @@ -19,16 +20,13 @@ contract PostDeploy is Script {

// Manually deploy a system with another namespace
ChatNamespacedSystem chatNamespacedSystem = new ChatNamespacedSystem();
IWorld(worldAddress).registerSystem(
ResourceSelector.from("namespace", "ChatNamespaced"),
chatNamespacedSystem,
true
);
IWorld(worldAddress).registerFunctionSelector(
ResourceSelector.from("namespace", "ChatNamespaced"),
"sendMessage",
"(string)"
);
ResourceId systemId = WorldResourceIdLib.encode({
typeId: RESOURCE_SYSTEM,
namespace: "namespace",
name: "ChatNamespaced"
});
IWorld(worldAddress).registerSystem(systemId, chatNamespacedSystem, true);
IWorld(worldAddress).registerFunctionSelector(systemId, "sendMessage", "(string)");
// Grant this system access to MessageTable
IWorld(worldAddress).grantAccess(MessageTableTableId, address(chatNamespacedSystem));

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions packages/cli/contracts/src/codegen/tables/Dynamics1.sol

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions packages/cli/contracts/src/codegen/tables/Dynamics2.sol

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions packages/cli/contracts/src/codegen/tables/Ephemeral.sol

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions packages/cli/contracts/src/codegen/tables/Singleton.sol

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions packages/cli/contracts/src/codegen/tables/Statics.sol

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.