From d5c0682fbd34fd7d5a96cd66a14b126c8f1afdb7 Mon Sep 17 00:00:00 2001 From: yonada Date: Tue, 27 Feb 2024 16:29:03 +0000 Subject: [PATCH] refactor: human-readable resource IDs use double underscore (#2310) Co-authored-by: Kevin Ingersoll --- .changeset/dirty-lizards-tell.md | 8 ++++++++ packages/cli/src/deploy/deploy.ts | 4 ++-- packages/cli/src/deploy/ensureSystems.ts | 13 ++++++------- packages/cli/src/deploy/ensureTables.ts | 9 ++++----- packages/cli/src/deploy/getSystems.ts | 5 ++--- packages/cli/src/deploy/resolveConfig.ts | 6 ++---- packages/cli/src/deploy/resourceLabel.ts | 3 --- packages/common/src/hexToResource.ts | 5 ++++- packages/common/src/index.ts | 1 + packages/common/src/resourceLabel.ts | 11 +++++++++++ packages/dev-tools/src/actions/WriteSummary.tsx | 6 ++---- packages/dev-tools/src/events/LogsTable.tsx | 9 ++++----- packages/dev-tools/src/summary/TablesSummary.tsx | 3 ++- packages/dev-tools/src/zustand/TablesPage.tsx | 7 +++---- .../src/postgres-decoded/createStorageAdapter.ts | 9 ++++++--- packages/store-sync/src/recs/recsStorage.ts | 11 ++++++----- packages/store-sync/src/recs/tableToComponent.ts | 5 +++-- packages/store-sync/src/sqlite/sqliteStorage.ts | 9 ++++++--- .../store-sync/src/zustand/createStorageAdapter.ts | 8 ++++++-- 19 files changed, 78 insertions(+), 54 deletions(-) create mode 100644 .changeset/dirty-lizards-tell.md delete mode 100644 packages/cli/src/deploy/resourceLabel.ts create mode 100644 packages/common/src/resourceLabel.ts diff --git a/.changeset/dirty-lizards-tell.md b/.changeset/dirty-lizards-tell.md new file mode 100644 index 0000000000..61413b2043 --- /dev/null +++ b/.changeset/dirty-lizards-tell.md @@ -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. diff --git a/packages/cli/src/deploy/deploy.ts b/packages/cli/src/deploy/deploy.ts index acedb22d98..3dc6f8e210 100644 --- a/packages/cli/src/deploy/deploy.ts +++ b/packages/cli/src/deploy/deploy.ts @@ -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"; @@ -49,7 +49,7 @@ export async function deploy({ ...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, diff --git a/packages/cli/src/deploy/ensureSystems.ts b/packages/cli/src/deploy/ensureSystems.ts index 4ebaf24e3b..3772843a5a 100644 --- a/packages/cli/src/deploy/ensureSystems.ts +++ b/packages/cli/src/deploy/ensureSystems.ts @@ -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"; @@ -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); @@ -117,14 +116,14 @@ 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({ @@ -132,7 +131,7 @@ export async function ensureSystems({ contracts: uniqueBy(missingSystems, (system) => getAddress(system.address)).map((system) => ({ bytecode: system.bytecode, deployedBytecodeSize: system.deployedBytecodeSize, - label: `${resourceLabel(system)} system`, + label: `${resourceToLabel(system)} system`, })), }); @@ -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); }, } diff --git a/packages/cli/src/deploy/ensureTables.ts b/packages/cli/src/deploy/ensureTables.ts index cd31975bda..31beddba46 100644 --- a/packages/cli/src/deploy/ensureTables.ts +++ b/packages/cli/src/deploy/ensureTables.ts @@ -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"; @@ -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( @@ -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); }, } diff --git a/packages/cli/src/deploy/getSystems.ts b/packages/cli/src/deploy/getSystems.ts index f3619fed63..8c291bc8c3 100644 --- a/packages/cli/src/deploy/getSystems.ts +++ b/packages/cli/src/deploy/getSystems.ts @@ -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"; @@ -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({ diff --git a/packages/cli/src/deploy/resolveConfig.ts b/packages/cli/src/deploy/resolveConfig.ts index ec18c69ef2..034af8bf41 100644 --- a/packages/cli/src/deploy/resolveConfig.ts +++ b/packages/cli/src/deploy/resolveConfig.ts @@ -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"; @@ -10,7 +10,6 @@ import { getCreate2Address, getAddress, hexToBytes, - Abi, bytesToHex, getFunctionSignature, } from "viem"; @@ -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 @@ -88,7 +86,7 @@ export function resolveConfig({ 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.` ); diff --git a/packages/cli/src/deploy/resourceLabel.ts b/packages/cli/src/deploy/resourceLabel.ts deleted file mode 100644 index e6e25f5d8b..0000000000 --- a/packages/cli/src/deploy/resourceLabel.ts +++ /dev/null @@ -1,3 +0,0 @@ -export function resourceLabel({ namespace, name }: { readonly namespace: string; readonly name: string }): string { - return `${namespace}:${name}`; -} diff --git a/packages/common/src/hexToResource.ts b/packages/common/src/hexToResource.ts index 5ac3f8a2de..1ccf25e506 100644 --- a/packages/common/src/hexToResource.ts +++ b/packages/common/src/hexToResource.ts @@ -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]) @@ -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 }; diff --git a/packages/common/src/index.ts b/packages/common/src/index.ts index 633ae85b37..e13d85d617 100644 --- a/packages/common/src/index.ts +++ b/packages/common/src/index.ts @@ -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"; diff --git a/packages/common/src/resourceLabel.ts b/packages/common/src/resourceLabel.ts new file mode 100644 index 0000000000..83ed9b6d1b --- /dev/null +++ b/packages/common/src/resourceLabel.ts @@ -0,0 +1,11 @@ +export type ResourceLabel = `${namespace}__${name}`; + +export function resourceToLabel({ + namespace, + name, +}: { + readonly namespace: namespace; + readonly name: name; +}): ResourceLabel { + return `${namespace}__${name}`; +} diff --git a/packages/dev-tools/src/actions/WriteSummary.tsx b/packages/dev-tools/src/actions/WriteSummary.tsx index fc4b011d3c..5ac4232835 100644 --- a/packages/dev-tools/src/actions/WriteSummary.tsx +++ b/packages/dev-tools/src/actions/WriteSummary.tsx @@ -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"; @@ -149,9 +149,7 @@ export function WriteSummary({ write }: Props) { // TODO: dedupe this with logs table so we can get both rendering the same return ( - - {table.namespace}:{table.name} - + {resourceToLabel(table)} {eventName === "Store_SetRecord" ? = : null} {eventName === "Store_SpliceStaticData" || eventName === "Store_SpliceDynamicData" ? ( diff --git a/packages/dev-tools/src/events/LogsTable.tsx b/packages/dev-tools/src/events/LogsTable.tsx index 5c969a6469..2c28c1a03b 100644 --- a/packages/dev-tools/src/events/LogsTable.tsx +++ b/packages/dev-tools/src/events/LogsTable.tsx @@ -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 @@ -22,7 +22,8 @@ export function LogsTable({ logs }: Props) { {logs.map((log) => { - const { namespace, name } = hexToResource(log.args.tableId); + const table = hexToResource(log.args.tableId); + const { namespace, name } = table; return ( {log.blockNumber?.toString()} - - {namespace}:{name} - + {resourceToLabel(table)} {log.args.keyTuple.join(",")} diff --git a/packages/dev-tools/src/summary/TablesSummary.tsx b/packages/dev-tools/src/summary/TablesSummary.tsx index 80db5dbb83..24badff906 100644 --- a/packages/dev-tools/src/summary/TablesSummary.tsx +++ b/packages/dev-tools/src/summary/TablesSummary.tsx @@ -1,3 +1,4 @@ +import { resourceToLabel } from "@latticexyz/common"; import { NavButton } from "../NavButton"; import { useTables } from "../zustand/useTables"; @@ -7,7 +8,7 @@ export function TablesSummary() {
{tables.map((table) => ( - {table.namespace}:{table.name} + {resourceToLabel(table)} ))}
diff --git a/packages/dev-tools/src/zustand/TablesPage.tsx b/packages/dev-tools/src/zustand/TablesPage.tsx index c1907d8c82..cd27b25dbc 100644 --- a/packages/dev-tools/src/zustand/TablesPage.tsx +++ b/packages/dev-tools/src/zustand/TablesPage.tsx @@ -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(); @@ -43,9 +44,7 @@ export function TablesPage() { } > {selectedTable ? ( - - {selectedTable.namespace}:{selectedTable.name} - + {resourceToLabel(selectedTable)} ) : ( Pick a tableā€¦ )} @@ -68,7 +67,7 @@ export function TablesPage() { } }} > - {table.namespace}:{table.name} + {resourceToLabel(table)} ))} diff --git a/packages/store-sync/src/postgres-decoded/createStorageAdapter.ts b/packages/store-sync/src/postgres-decoded/createStorageAdapter.ts index e8c3c5dd3d..227c0f39aa 100644 --- a/packages/store-sync/src/postgres-decoded/createStorageAdapter.ts +++ b/packages/store-sync/src/postgres-decoded/createStorageAdapter.ts @@ -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 @@ -60,7 +60,7 @@ export async function createStorageAdapter 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; } diff --git a/packages/store-sync/src/recs/recsStorage.ts b/packages/store-sync/src/recs/recsStorage.ts index e610787ab3..85a956c402 100644 --- a/packages/store-sync/src/recs/recsStorage.ts +++ b/packages/store-sync/src/recs/recsStorage.ts @@ -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"; @@ -72,16 +72,17 @@ export function recsStorage>({ 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; } diff --git a/packages/store-sync/src/recs/tableToComponent.ts b/packages/store-sync/src/recs/tableToComponent.ts index 8a3e89cc7c..cfd07a9107 100644 --- a/packages/store-sync/src/recs/tableToComponent.ts +++ b/packages/store-sync/src/recs/tableToComponent.ts @@ -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 = Component< { @@ -16,7 +17,7 @@ export type TableToComponent
= 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"] }; } @@ -40,7 +41,7 @@ export function tableToComponent
(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), }, diff --git a/packages/store-sync/src/sqlite/sqliteStorage.ts b/packages/store-sync/src/sqlite/sqliteStorage.ts index 7ea54ea3ec..6c369ab3b2 100644 --- a/packages/store-sync/src/sqlite/sqliteStorage.ts +++ b/packages/store-sync/src/sqlite/sqliteStorage.ts @@ -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, resourceToLabel, spliceHex } from "@latticexyz/common"; import { decodeKey, decodeValueArgs } from "@latticexyz/protocol-parser"; // TODO: upgrade drizzle and use async sqlite interface for consistency @@ -36,7 +36,7 @@ export async function sqliteStorage({ 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 ${resourceToLabel(table)} for world ${chainId}:${table.address}`); const sqliteTable = buildTable(table); @@ -88,7 +88,10 @@ export async function sqliteStorage({ ); if (!table) { const tableId = hexToResource(log.args.tableId); - debug(`table ${tableId.namespace}:${tableId.name} not found, skipping log`, log); + debug( + `table ${resourceToLabel({ namespace: tableId.namespace, name: tableId.name })} not found, skipping log`, + log + ); continue; } diff --git a/packages/store-sync/src/zustand/createStorageAdapter.ts b/packages/store-sync/src/zustand/createStorageAdapter.ts index 4427ef349f..7dcb55f20c 100644 --- a/packages/store-sync/src/zustand/createStorageAdapter.ts +++ b/packages/store-sync/src/zustand/createStorageAdapter.ts @@ -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, resourceToLabel, spliceHex } from "@latticexyz/common"; import { debug } from "./debug"; import { getId } from "./getId"; import { size } from "viem"; @@ -29,7 +29,11 @@ export function createStorageAdapter({ 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: ${resourceToLabel({ namespace, name })} (${log.args.tableId}) at ${ + log.address + }` + ); continue; }