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

refactor: human-readable resource IDs use double underscore #2310

Merged
merged 10 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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
8 changes: 8 additions & 0 deletions .changeset/dirty-lizards-tell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@latticexyz/store-sync": patch
"@latticexyz/dev-tools": patch
"@latticexyz/common": patch
"@latticexyz/cli": patch
---

Updated all human-readable resource ID's to use `{namespace}__{name}` for consistency with world function signatures.
holic marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 1 addition & 1 deletion packages/cli/src/deploy/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { ensureModules } from "./ensureModules";
import { Table } from "./configToTables";
import { ensureNamespaceOwner } from "./ensureNamespaceOwner";
import { debug } from "./debug";
import { resourceLabel } from "./resourceLabel";
import { resourceLabel } from "@latticexyz/common";
import { uniqueBy } from "@latticexyz/common/utils";
import { ensureContractsDeployed } from "./ensureContractsDeployed";
import { worldFactoryContracts } from "./ensureWorldFactory";
Expand Down
3 changes: 1 addition & 2 deletions packages/cli/src/deploy/ensureSystems.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Client, Transport, Chain, Account, Hex, getAddress } from "viem";
import { writeContract } from "@latticexyz/common";
import { resourceLabel, writeContract } from "@latticexyz/common";
import { System, WorldDeploy, worldAbi } from "./common";
import { debug } from "./debug";
import { resourceLabel } from "./resourceLabel";
import { getSystems } from "./getSystems";
import { getResourceAccess } from "./getResourceAccess";
import { uniqueBy, wait } from "@latticexyz/common/utils";
Expand Down
3 changes: 1 addition & 2 deletions packages/cli/src/deploy/ensureTables.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { Client, Transport, Chain, Account, Hex } from "viem";
import { Table } from "./configToTables";
import { writeContract } from "@latticexyz/common";
import { resourceLabel, writeContract } from "@latticexyz/common";
import { WorldDeploy, worldAbi } from "./common";
import { valueSchemaToFieldLayoutHex, keySchemaToHex, valueSchemaToHex } from "@latticexyz/protocol-parser";
import { debug } from "./debug";
import { resourceLabel } from "./resourceLabel";
import { getTables } from "./getTables";
import pRetry from "p-retry";
import { wait } from "@latticexyz/common/utils";
Expand Down
3 changes: 1 addition & 2 deletions packages/cli/src/deploy/getSystems.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { System, WorldDeploy, worldTables } from "./common";
import { Client } from "viem";
import { getResourceIds } from "./getResourceIds";
import { hexToResource } from "@latticexyz/common";
import { hexToResource, resourceLabel } from "@latticexyz/common";
import { getTableValue } from "./getTableValue";
import { debug } from "./debug";
import { resourceLabel } from "./resourceLabel";
import { getFunctions } from "./getFunctions";
import { getResourceAccess } from "./getResourceAccess";

Expand Down
4 changes: 1 addition & 3 deletions packages/cli/src/deploy/resolveConfig.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { resolveWorldConfig } from "@latticexyz/world";
import { Config, ConfigInput, WorldFunction, salt } from "./common";
import { resourceToHex, hexToResource } from "@latticexyz/common";
import { resourceLabel, resourceToHex, hexToResource } from "@latticexyz/common";
import { resolveWithContext } from "@latticexyz/config";
import { encodeField } from "@latticexyz/protocol-parser";
import { SchemaAbiType, SchemaAbiTypeToPrimitiveType } from "@latticexyz/schema-type";
Expand All @@ -10,7 +10,6 @@ import {
getCreate2Address,
getAddress,
hexToBytes,
Abi,
bytesToHex,
getFunctionSignature,
} from "viem";
Expand All @@ -19,7 +18,6 @@ import { defaultModuleContracts } from "../utils/modules/constants";
import { getContractData } from "../utils/utils/getContractData";
import { configToTables } from "./configToTables";
import { deployer } from "./ensureDeployer";
import { resourceLabel } from "./resourceLabel";

// TODO: this should be replaced by https://github.com/latticexyz/mud/issues/1668

Expand Down
3 changes: 0 additions & 3 deletions packages/cli/src/deploy/resourceLabel.ts

This file was deleted.

5 changes: 4 additions & 1 deletion packages/common/src/hexToResource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Resource } from "./common";
import { ResourceType, resourceTypes } from "./resourceTypes";
import { resourceTypeIds } from "./resourceToHex";
import { ReverseMap } from "./type-utils/common";
import { resourceLabel } from "./resourceLabel";

const resourceTypeIdToType = Object.fromEntries(
Object.entries(resourceTypeIds).map(([key, value]) => [value, key])
Expand All @@ -23,7 +24,9 @@ export function hexToResource(hex: Hex): Resource {
const name = hexToString(sliceHex(hex, 16, 32)).replace(/\0+$/, "");

if (!type) {
throw new Error(`Unknown type (${resourceTypeId}) for resource (${resourceTypeId}:${namespace}:${name})`);
throw new Error(
`Unknown type (${resourceTypeId}) for resource (${resourceTypeId}:${resourceLabel({ namespace, name })})`
);
}

return { resourceId: hex, type, namespace, name };
Expand Down
1 change: 1 addition & 0 deletions packages/common/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export * from "./getNonceManager";
export * from "./getNonceManagerId";
export * from "./hexToResource";
export * from "./readHex";
export * from "./resourceLabel";
export * from "./resourceToHex";
export * from "./resourceTypes";
export * from "./result";
Expand Down
11 changes: 11 additions & 0 deletions packages/common/src/resourceLabel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export type ResourceLabel = `${string}__${string}`;
Copy link
Member

@holic holic Feb 27, 2024

Choose a reason for hiding this comment

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

if you wanted to get real fancy, could make this generic, e.g.

export type ResourceLabel<namespace extends string = string, name extends string = string> = `${namespace}__${name}`;

only case I can think in which this might be useful is the RECS component metadata for a given table, where tableName would show up as e.g. store__Tables

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ohhh I like that! I was thinking namespace and name should kinda be types


export function resourceLabel({
Copy link
Member

Choose a reason for hiding this comment

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

thoughts on getResourceLabel or resourceToLabel?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Definitely prefer one of them

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Going with resourceToLabel for consistency with resourceToHex

namespace,
name,
}: {
readonly namespace: string;
readonly name: string;
Copy link
Member

@holic holic Feb 27, 2024

Choose a reason for hiding this comment

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

not blocking but this could also be Pick<Resource, 'namespace' | 'name'>

(we should also make Resource type use readonly props)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Kicking this one until we fix Resource: #2321

}): ResourceLabel {
return `${namespace}__${name}`;
}
6 changes: 2 additions & 4 deletions packages/dev-tools/src/actions/WriteSummary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { getTransaction } from "./getTransaction";
import { getTransactionReceipt } from "./getTransactionReceipt";
import { getTransactionResult } from "./getTransactionResult";
import { ErrorTrace } from "../ErrorTrace";
import { ContractWrite, hexToResource } from "@latticexyz/common";
import { ContractWrite, hexToResource, resourceLabel } from "@latticexyz/common";
import { useDevToolsContext } from "../DevToolsContext";
import { hexKeyTupleToEntity } from "@latticexyz/store-sync/recs";

Expand Down Expand Up @@ -149,9 +149,7 @@ export function WriteSummary({ write }: Props) {
// TODO: dedupe this with logs table so we can get both rendering the same
return (
<tr key={i}>
<td className="whitespace-nowrap overflow-hidden text-ellipsis">
{table.namespace}:{table.name}
</td>
<td className="whitespace-nowrap overflow-hidden text-ellipsis">{resourceLabel(table)}</td>
<td className="whitespace-nowrap">
{eventName === "Store_SetRecord" ? <span className="text-green-500 font-bold">=</span> : null}
{eventName === "Store_SpliceStaticData" || eventName === "Store_SpliceDynamicData" ? (
Expand Down
9 changes: 4 additions & 5 deletions packages/dev-tools/src/events/LogsTable.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { StorageAdapterLog } from "@latticexyz/store-sync";
import { EventIcon } from "./EventIcon";
import { hexToResource } from "@latticexyz/common";
import { hexToResource, resourceLabel } from "@latticexyz/common";

// TODO: use react-table or similar for better perf with lots of logs

Expand All @@ -22,7 +22,8 @@ export function LogsTable({ logs }: Props) {
</thead>
<tbody className="font-mono text-xs">
{logs.map((log) => {
const { namespace, name } = hexToResource(log.args.tableId);
const table = hexToResource(log.args.tableId);
const { namespace, name } = table;
return (
<tr
key={
Expand All @@ -35,9 +36,7 @@ export function LogsTable({ logs }: Props) {
<td className="px-1 whitespace-nowrap overflow-hidden text-ellipsis text-white/40">
{log.blockNumber?.toString()}
</td>
<td className="px-1 whitespace-nowrap overflow-hidden text-ellipsis">
{namespace}:{name}
</td>
<td className="px-1 whitespace-nowrap overflow-hidden text-ellipsis">{resourceLabel(table)}</td>
<td className="px-1 whitespace-nowrap overflow-hidden text-ellipsis">{log.args.keyTuple.join(",")}</td>
<td className="px-1 whitespace-nowrap">
<EventIcon type={log.eventName} />
Expand Down
3 changes: 2 additions & 1 deletion packages/dev-tools/src/summary/TablesSummary.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { resourceLabel } from "@latticexyz/common";
import { NavButton } from "../NavButton";
import { useTables } from "../zustand/useTables";

Expand All @@ -7,7 +8,7 @@ export function TablesSummary() {
<div className="flex flex-col gap-1 items-start">
{tables.map((table) => (
<NavButton key={table.tableId} to={`/tables/${table.tableId}`} className="font-mono text-xs hover:text-white">
{table.namespace}:{table.name}
{resourceLabel(table)}
</NavButton>
))}
</div>
Expand Down
7 changes: 3 additions & 4 deletions packages/dev-tools/src/zustand/TablesPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { NavButton } from "../NavButton";
import { useEffect, useRef } from "react";
import { twMerge } from "tailwind-merge";
import { useTables } from "./useTables";
import { resourceLabel } from "@latticexyz/common";

export function TablesPage() {
const tables = useTables();
Expand Down Expand Up @@ -43,9 +44,7 @@ export function TablesPage() {
}
>
{selectedTable ? (
<span className="font-mono">
{selectedTable.namespace}:{selectedTable.name}
</span>
<span className="font-mono">{resourceLabel(selectedTable)}</span>
) : (
<span>Pick a table…</span>
)}
Expand All @@ -68,7 +67,7 @@ export function TablesPage() {
}
}}
>
{table.namespace}:{table.name}
{resourceLabel(table)}
</NavButton>
))}
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { tables as internalTables } from "../postgres/tables";
import { createStorageAdapter as createBytesStorageAdapter } from "../postgres/createStorageAdapter";
import { setupTables } from "../postgres/setupTables";
import { getTables } from "./getTables";
import { hexToResource } from "@latticexyz/common";
import { hexToResource, resourceLabel } from "@latticexyz/common";

// Currently assumes one DB per chain ID

Expand Down Expand Up @@ -60,7 +60,7 @@ export async function createStorageAdapter<TConfig extends StoreConfig = StoreCo
);
if (!table) {
const { namespace, name } = hexToResource(log.args.tableId);
debug(`table registration record for ${namespace}:${name} not found, skipping log`, log);
debug(`table registration record for ${resourceLabel({ namespace, name })} not found, skipping log`, log);
continue;
}

Expand Down Expand Up @@ -89,7 +89,10 @@ export async function createStorageAdapter<TConfig extends StoreConfig = StoreCo
.then((rows) => rows.find(() => true));
if (!record) {
const { namespace, name } = hexToResource(log.args.tableId);
debug(`no record found for ${log.args.keyTuple} in table ${namespace}:${name}, skipping log`, log);
debug(
`no record found for ${log.args.keyTuple} in table ${resourceLabel({ namespace, name })}, skipping log`,
log
);
continue;
}

Expand Down
11 changes: 6 additions & 5 deletions packages/store-sync/src/recs/recsStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { debug } from "./debug";
import { World as RecsWorld, getComponentValue, hasComponent, removeComponent, setComponent } from "@latticexyz/recs";
import { defineInternalComponents } from "./defineInternalComponents";
import { getTableEntity } from "./getTableEntity";
import { hexToResource, spliceHex } from "@latticexyz/common";
import { hexToResource, resourceLabel, spliceHex } from "@latticexyz/common";
import { decodeValueArgs } from "@latticexyz/protocol-parser";
import { Hex, size } from "viem";
import { isTableRegistrationLog } from "../isTableRegistrationLog";
Expand Down Expand Up @@ -72,16 +72,17 @@ export function recsStorage<tables extends Record<string, Table>>({
getTableEntity({ address: log.address, namespace, name })
)?.table;
if (!table) {
debug(`skipping update for unknown table: ${namespace}:${name} at ${log.address}`);
debug(`skipping update for unknown table: ${resourceLabel({ namespace, name })} at ${log.address}`);
continue;
}

const component = world.components.find((c) => c.id === table.tableId);
if (!component) {
debug(
`skipping update for unknown component: ${table.tableId} (${table.namespace}:${
table.name
}). Available components: ${Object.keys(components)}`
`skipping update for unknown component: ${table.tableId} (${resourceLabel({
namespace,
name,
})}). Available components: ${Object.keys(components)}`
);
continue;
}
Expand Down
5 changes: 3 additions & 2 deletions packages/store-sync/src/recs/tableToComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { SchemaAbiTypeToRecsType, schemaAbiTypeToRecsType } from "./schemaAbiTyp
import { SchemaAbiType } from "@latticexyz/schema-type";
import { Table } from "@latticexyz/store";
import { mapObject } from "@latticexyz/common/utils";
import { ResourceLabel, resourceLabel } from "@latticexyz/common";

export type TableToComponent<table extends Table> = Component<
{
Expand All @@ -16,7 +17,7 @@ export type TableToComponent<table extends Table> = Component<
},
StoreComponentMetadata & {
componentName: table["name"];
tableName: `${table["namespace"]}:${table["name"]}`;
tableName: ResourceLabel;
keySchema: { [name in keyof table["keySchema"] & string]: table["keySchema"][name]["type"] };
valueSchema: { [name in keyof table["valueSchema"] & string]: table["valueSchema"][name]["type"] };
}
Expand All @@ -40,7 +41,7 @@ export function tableToComponent<table extends Table>(world: World, table: table
id: table.tableId,
metadata: {
componentName: table.name,
tableName: `${table.namespace}:${table.name}`,
tableName: resourceLabel(table),
keySchema: mapObject(table.keySchema, ({ type }) => type),
valueSchema: mapObject(table.valueSchema, ({ type }) => type),
},
Expand Down
9 changes: 6 additions & 3 deletions packages/store-sync/src/sqlite/sqliteStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { schemaVersion } from "./schemaVersion";
import { StorageAdapter } from "../common";
import { isTableRegistrationLog } from "../isTableRegistrationLog";
import { logToTable } from "../logToTable";
import { hexToResource, spliceHex } from "@latticexyz/common";
import { hexToResource, resourceLabel, spliceHex } from "@latticexyz/common";
import { decodeKey, decodeValueArgs } from "@latticexyz/protocol-parser";

// TODO: upgrade drizzle and use async sqlite interface for consistency
Expand All @@ -36,7 +36,7 @@ export async function sqliteStorage<TConfig extends StoreConfig = StoreConfig>({
const newTables = logs.filter(isTableRegistrationLog).map(logToTable);
await database.transaction(async (tx) => {
for (const table of newTables) {
debug(`creating table ${table.namespace}:${table.name} for world ${chainId}:${table.address}`);
debug(`creating table ${resourceLabel(table)} for world ${chainId}:${table.address}`);

const sqliteTable = buildTable(table);

Expand Down Expand Up @@ -88,7 +88,10 @@ export async function sqliteStorage<TConfig extends StoreConfig = StoreConfig>({
);
if (!table) {
const tableId = hexToResource(log.args.tableId);
debug(`table ${tableId.namespace}:${tableId.name} not found, skipping log`, log);
debug(
`table ${resourceLabel({ namespace: tableId.namespace, name: tableId.name })} not found, skipping log`,
log
);
continue;
}

Expand Down
8 changes: 6 additions & 2 deletions packages/store-sync/src/zustand/createStorageAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Tables } from "@latticexyz/store";
import { StorageAdapter } from "../common";
import { RawRecord } from "./common";
import { ZustandStore } from "./createStore";
import { hexToResource, spliceHex } from "@latticexyz/common";
import { hexToResource, resourceLabel, spliceHex } from "@latticexyz/common";
import { debug } from "./debug";
import { getId } from "./getId";
import { size } from "viem";
Expand All @@ -29,7 +29,11 @@ export function createStorageAdapter<tables extends Tables>({
const table = store.getState().tables[log.args.tableId];
if (!table) {
const { namespace, name } = hexToResource(log.args.tableId);
debug(`skipping update for unknown table: ${namespace}:${name} (${log.args.tableId}) at ${log.address}`);
debug(
`skipping update for unknown table: ${resourceLabel({ namespace, name })} (${log.args.tableId}) at ${
log.address
}`
);
continue;
}

Expand Down
Loading