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 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
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 IDs to use `{namespace}__{name}` for consistency with world function signatures.
4 changes: 2 additions & 2 deletions 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 { resourceToLabel } from "@latticexyz/common";
import { uniqueBy } from "@latticexyz/common/utils";
import { ensureContractsDeployed } from "./ensureContractsDeployed";
import { worldFactoryContracts } from "./ensureWorldFactory";
Expand Down Expand Up @@ -49,7 +49,7 @@ export async function deploy<configInput extends ConfigInput>({
...uniqueBy(systems, (system) => getAddress(system.address)).map((system) => ({
bytecode: system.bytecode,
deployedBytecodeSize: system.deployedBytecodeSize,
label: `${resourceLabel(system)} system`,
label: `${resourceToLabel(system)} system`,
})),
...uniqueBy(config.modules, (mod) => getAddress(mod.address)).map((mod) => ({
bytecode: mod.bytecode,
Expand Down
13 changes: 6 additions & 7 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 { resourceToLabel, 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 Expand Up @@ -103,7 +102,7 @@ export async function ensureSystems({
)
);
if (existingSystems.length) {
debug("existing systems", existingSystems.map(resourceLabel).join(", "));
debug("existing systems", existingSystems.map(resourceToLabel).join(", "));
}
const existingSystemIds = existingSystems.map((system) => system.systemId);

Expand All @@ -117,22 +116,22 @@ export async function ensureSystems({
)
);
if (systemsToUpgrade.length) {
debug("upgrading systems", systemsToUpgrade.map(resourceLabel).join(", "));
debug("upgrading systems", systemsToUpgrade.map(resourceToLabel).join(", "));
}

const systemsToAdd = missingSystems.filter(
(system) => !worldSystems.some((worldSystem) => worldSystem.systemId === system.systemId)
);
if (systemsToAdd.length) {
debug("registering new systems", systemsToAdd.map(resourceLabel).join(", "));
debug("registering new systems", systemsToAdd.map(resourceToLabel).join(", "));
}

await ensureContractsDeployed({
client,
contracts: uniqueBy(missingSystems, (system) => getAddress(system.address)).map((system) => ({
bytecode: system.bytecode,
deployedBytecodeSize: system.deployedBytecodeSize,
label: `${resourceLabel(system)} system`,
label: `${resourceToLabel(system)} system`,
})),
});

Expand All @@ -151,7 +150,7 @@ export async function ensureSystems({
retries: 3,
onFailedAttempt: async (error) => {
const delay = error.attemptNumber * 500;
debug(`failed to register system ${resourceLabel(system)}, retrying in ${delay}ms...`);
debug(`failed to register system ${resourceToLabel(system)}, retrying in ${delay}ms...`);
await wait(delay);
},
}
Expand Down
9 changes: 4 additions & 5 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 { resourceToLabel, 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 All @@ -23,12 +22,12 @@ export async function ensureTables({

const existingTables = tables.filter((table) => worldTableIds.includes(table.tableId));
if (existingTables.length) {
debug("existing tables", existingTables.map(resourceLabel).join(", "));
debug("existing tables", existingTables.map(resourceToLabel).join(", "));
}

const missingTables = tables.filter((table) => !worldTableIds.includes(table.tableId));
if (missingTables.length) {
debug("registering tables", missingTables.map(resourceLabel).join(", "));
debug("registering tables", missingTables.map(resourceToLabel).join(", "));
return await Promise.all(
missingTables.map((table) =>
pRetry(
Expand All @@ -52,7 +51,7 @@ export async function ensureTables({
retries: 3,
onFailedAttempt: async (error) => {
const delay = error.attemptNumber * 500;
debug(`failed to register table ${resourceLabel(table)}, retrying in ${delay}ms...`);
debug(`failed to register table ${resourceToLabel(table)}, retrying in ${delay}ms...`);
await wait(delay);
},
}
Expand Down
5 changes: 2 additions & 3 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, resourceToLabel } from "@latticexyz/common";
import { getTableValue } from "./getTableValue";
import { debug } from "./debug";
import { resourceLabel } from "./resourceLabel";
import { getFunctions } from "./getFunctions";
import { getResourceAccess } from "./getResourceAccess";

Expand All @@ -22,7 +21,7 @@ export async function getSystems({
]);
const systems = resourceIds.map(hexToResource).filter((resource) => resource.type === "system");

debug("looking up systems", systems.map(resourceLabel).join(", "));
debug("looking up systems", systems.map(resourceToLabel).join(", "));
return await Promise.all(
systems.map(async (system) => {
const { system: address, publicAccess } = await getTableValue({
Expand Down
6 changes: 2 additions & 4 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 { resourceToLabel, 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 Expand Up @@ -88,7 +86,7 @@ export function resolveConfig<config extends ConfigInput>({
const targetSystem = systems.find((s) => s.systemId === systemId);
if (!targetSystem) {
throw new Error(
`System ${resourceLabel(system)} wanted access to ${resourceLabel(
`System ${resourceToLabel(system)} wanted access to ${resourceToLabel(
hexToResource(systemId)
)}, but it wasn't found in the config.`
);
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 { resourceToLabel } 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}:${resourceToLabel({ 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<namespace extends string = string, name extends string = string> = `${namespace}__${name}`;

export function resourceToLabel<namespace extends string, name extends string>({
namespace,
name,
}: {
readonly namespace: namespace;
readonly name: name;
}): ResourceLabel<namespace, name> {
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, resourceToLabel } 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">{resourceToLabel(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, resourceToLabel } 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">{resourceToLabel(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 { resourceToLabel } 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}
{resourceToLabel(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 { resourceToLabel } 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">{resourceToLabel(selectedTable)}</span>
) : (
<span>Pick a table…</span>
)}
Expand All @@ -68,7 +67,7 @@ export function TablesPage() {
}
}}
>
{table.namespace}:{table.name}
{resourceToLabel(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, resourceToLabel } 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 ${resourceToLabel({ 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 ${resourceToLabel({ 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, resourceToLabel, 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: ${resourceToLabel({ 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} (${resourceToLabel({
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, resourceToLabel } 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: resourceToLabel(table),
keySchema: mapObject(table.keySchema, ({ type }) => type),
valueSchema: mapObject(table.valueSchema, ({ type }) => type),
},
Expand Down
Loading
Loading