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(common): clarify resourceId (hex) from resource (object) #1706

Merged
merged 7 commits into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 3 additions & 1 deletion packages/common/src/common.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Hex } from "viem";
import { ResourceType } from "./resourceTypes";

export type ResourceId = {
export type Resource = {
namespace: string;
name: string;
type: ResourceType;
hex: Hex;
Copy link
Member Author

Choose a reason for hiding this comment

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

should arguably call this resourceId if we're calling the object "resource"

Copy link
Member

Choose a reason for hiding this comment

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

agree this could be resourceId

};
11 changes: 11 additions & 0 deletions packages/common/src/hexToResource.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { describe, it, expect } from "vitest";
import { hexToResource } from "./hexToResource";

describe("hexToResource", () => {
it("can convert from hex string", () => {
const resource = hexToResource("0x74626e616d65737061636500000000006e616d65000000000000000000000000");
expect(resource.type).toMatchInlineSnapshot('"table"');
expect(resource.namespace).toMatchInlineSnapshot('"namespace"');
expect(resource.name).toMatchInlineSnapshot('"name"');
});
});
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Hex, hexToString, sliceHex } from "viem";
import { ResourceId } from "./common";
import { Resource } from "./common";
import { ResourceType, resourceTypes } from "./resourceTypes";
import { resourceTypeIds } from "./resourceIdToHex";
import { resourceTypeIds } from "./resourceToHex";
import { ReverseMap } from "./type-utils/common";

const resourceTypeIdToType = Object.fromEntries(
Expand All @@ -16,7 +16,7 @@ function getResourceType(resourceTypeId: string): ResourceType | undefined {
}
}

export function hexToResourceId(hex: Hex): ResourceId {
export function hexToResource(hex: Hex): Resource {
const resourceTypeId = hexToString(sliceHex(hex, 0, 2)).replace(/\0+$/, "");
const type = getResourceType(resourceTypeId);
const namespace = hexToString(sliceHex(hex, 2, 16)).replace(/\0+$/, "");
Expand All @@ -26,5 +26,5 @@ export function hexToResourceId(hex: Hex): ResourceId {
throw new Error(`Unknown resource type: ${resourceTypeId}`);
}

return { type, namespace, name };
return { type, namespace, name, hex };
}
11 changes: 0 additions & 11 deletions packages/common/src/hexToResourceId.test.ts

This file was deleted.

4 changes: 2 additions & 2 deletions packages/common/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ export * from "./getContract";
export * from "./getBurnerPrivateKey";
export * from "./getNonceManager";
export * from "./getNonceManagerId";
export * from "./hexToResourceId";
export * from "./hexToResource";
export * from "./readHex";
export * from "./resourceIdToHex";
export * from "./resourceToHex";
export * from "./resourceTypes";
export * from "./spliceHex";
export * from "./transportObserver";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { describe, it, expect } from "vitest";
import { resourceIdToHex } from "./resourceIdToHex";
import { hexToResourceId } from "./hexToResourceId";
import { resourceToHex } from "./resourceToHex";
import { hexToResource } from "./hexToResource";

describe("resourceIdToHex", () => {
describe("resourceToHex", () => {
it("can convert table resource to hex string", () => {
const hex = resourceIdToHex({
const hex = resourceToHex({
type: "table",
namespace: "namespace",
name: "name",
});
expect(hex).toMatchInlineSnapshot('"0x74626e616d65737061636500000000006e616d65000000000000000000000000"');
expect(hexToResourceId(hex)).toMatchInlineSnapshot(`
expect(hexToResource(hex)).toMatchInlineSnapshot(`
{
"hex": "0x74626e616d65737061636500000000006e616d65000000000000000000000000",
"name": "name",
"namespace": "namespace",
"type": "table",
Expand All @@ -20,14 +21,15 @@ describe("resourceIdToHex", () => {
});

it("can convert offchain table resource to hex string", () => {
const hex = resourceIdToHex({
const hex = resourceToHex({
type: "offchainTable",
namespace: "namespace",
name: "name",
});
expect(hex).toMatchInlineSnapshot('"0x6f746e616d65737061636500000000006e616d65000000000000000000000000"');
expect(hexToResourceId(hex)).toMatchInlineSnapshot(`
expect(hexToResource(hex)).toMatchInlineSnapshot(`
{
"hex": "0x6f746e616d65737061636500000000006e616d65000000000000000000000000",
"name": "name",
"namespace": "namespace",
"type": "offchainTable",
Expand All @@ -36,14 +38,15 @@ describe("resourceIdToHex", () => {
});

it("truncates namespaces >14 bytes", () => {
const hex = resourceIdToHex({
const hex = resourceToHex({
type: "table",
namespace: "AVeryLongNamespace",
name: "name",
});
expect(hex).toMatchInlineSnapshot('"0x746241566572794c6f6e674e616d65736e616d65000000000000000000000000"');
expect(hexToResourceId(hex)).toMatchInlineSnapshot(`
expect(hexToResource(hex)).toMatchInlineSnapshot(`
{
"hex": "0x746241566572794c6f6e674e616d65736e616d65000000000000000000000000",
"name": "name",
"namespace": "AVeryLongNames",
"type": "table",
Expand All @@ -52,14 +55,15 @@ describe("resourceIdToHex", () => {
});

it("truncates names >16 bytes", () => {
const hex = resourceIdToHex({
const hex = resourceToHex({
type: "table",
namespace: "namespace",
name: "AnUnnecessarilyLongName",
});
expect(hex).toMatchInlineSnapshot('"0x74626e616d6573706163650000000000416e556e6e65636573736172696c794c"');
expect(hexToResourceId(hex)).toMatchInlineSnapshot(`
expect(hexToResource(hex)).toMatchInlineSnapshot(`
{
"hex": "0x74626e616d6573706163650000000000416e556e6e65636573736172696c794c",
"name": "AnUnnecessarilyL",
"namespace": "namespace",
"type": "table",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Hex, stringToHex, concatHex } from "viem";
import { ResourceId } from "./common";
import { Resource } from "./common";
import { ResourceType } from "./resourceTypes";

/** @internal */
Expand All @@ -13,11 +13,11 @@ export const resourceTypeIds = {
system: "sy",
} as const satisfies Record<ResourceType, string>;

export function resourceIdToHex(resourceId: ResourceId): Hex {
const typeId = resourceTypeIds[resourceId.type];
export function resourceToHex(resource: Omit<Resource, "hex">): Hex {
const typeId = resourceTypeIds[resource.type];
return concatHex([
stringToHex(typeId, { size: 2 }),
stringToHex(resourceId.namespace.slice(0, 14), { size: 14 }),
stringToHex(resourceId.name.slice(0, 16), { size: 16 }),
stringToHex(resource.namespace.slice(0, 14), { size: 14 }),
stringToHex(resource.name.slice(0, 16), { size: 16 }),
]);
}
4 changes: 2 additions & 2 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, hexToResourceId } from "@latticexyz/common";
import { ContractWrite, hexToResource } from "@latticexyz/common";
import { useDevToolsContext } from "../DevToolsContext";
import { hexKeyTupleToEntity } from "@latticexyz/store-sync/recs";

Expand Down Expand Up @@ -145,7 +145,7 @@ export function WriteSummary({ write }: Props) {
</thead>
<tbody className="font-mono text-xs">
{events.map(({ eventName, args }, i) => {
const table = hexToResourceId((args as any).tableId);
const table = hexToResource((args as any).tableId);
// TODO: dedupe this with logs table so we can get both rendering the same
return (
<tr key={i}>
Expand Down
4 changes: 2 additions & 2 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 { hexToResourceId } from "@latticexyz/common";
import { hexToResource } from "@latticexyz/common";

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

Expand All @@ -22,7 +22,7 @@ export function LogsTable({ logs }: Props) {
</thead>
<tbody className="font-mono text-xs">
{logs.map((log) => {
const { namespace, name } = hexToResourceId(log.args.tableId);
const { namespace, name } = hexToResource(log.args.tableId);
return (
<tr
key={
Expand Down
4 changes: 2 additions & 2 deletions packages/store-sync/src/logToTable.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { hexToSchema, decodeValue, ValueSchema } from "@latticexyz/protocol-parser";
import { Hex, concatHex, decodeAbiParameters, parseAbiParameters } from "viem";
import { StorageAdapterLog, Table, schemasTable } from "./common";
import { hexToResourceId } from "@latticexyz/common";
import { hexToResource } from "@latticexyz/common";

// TODO: add tableToLog

Expand All @@ -11,7 +11,7 @@ export function logToTable(log: StorageAdapterLog & { eventName: "Store_SetRecor
console.warn("registerSchema event is expected to have only one key in key tuple, but got multiple", log);
}

const table = hexToResourceId(tableId);
const table = hexToResource(tableId);

const value = decodeValue(
// TODO: remove cast when we have strong types for user types
Expand Down
4 changes: 2 additions & 2 deletions packages/store-sync/src/postgres/getTableKey.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { getAddress } from "viem";
import { Table } from "../common";
import { hexToResourceId } from "@latticexyz/common";
import { hexToResource } from "@latticexyz/common";

export function getTableKey({ address, tableId }: Pick<Table, "address" | "tableId">): string {
const { namespace, name } = hexToResourceId(tableId);
const { namespace, name } = hexToResource(tableId);
return `${getAddress(address)}:${namespace}:${name}`;
}
4 changes: 2 additions & 2 deletions packages/store-sync/src/postgres/postgresStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { debug } from "./debug";
import { buildInternalTables } from "./buildInternalTables";
import { getTables } from "./getTables";
import { schemaVersion } from "./schemaVersion";
import { hexToResourceId, spliceHex } from "@latticexyz/common";
import { hexToResource, spliceHex } from "@latticexyz/common";
import { setupTables } from "./setupTables";
import { getTableKey } from "./getTableKey";
import { StorageAdapter, StorageAdapterBlock } from "../common";
Expand Down Expand Up @@ -85,7 +85,7 @@ export async function postgresStorage<TConfig extends StoreConfig = StoreConfig>
(table) => getTableKey(table) === getTableKey({ address: log.address, tableId: log.args.tableId })
);
if (!table) {
const { namespace, name } = hexToResourceId(log.args.tableId);
const { namespace, name } = hexToResource(log.args.tableId);
debug(`table ${namespace}:${name} not found, skipping log`, log);
continue;
}
Expand Down
4 changes: 2 additions & 2 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 { hexToResourceId, spliceHex } from "@latticexyz/common";
import { hexToResource, spliceHex } from "@latticexyz/common";
import { decodeValueArgs } from "@latticexyz/protocol-parser";
import { Hex, size } from "viem";
import { isTableRegistrationLog } from "../isTableRegistrationLog";
Expand Down Expand Up @@ -65,7 +65,7 @@ export function recsStorage<TConfig extends StoreConfig = StoreConfig>({
}

for (const log of logs) {
const { namespace, name } = hexToResourceId(log.args.tableId);
const { namespace, name } = hexToResource(log.args.tableId);
const table = getComponentValue(
components.RegisteredTables,
getTableEntity({ address: log.address, namespace, name })
Expand Down
6 changes: 3 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 { hexToResourceId, spliceHex } from "@latticexyz/common";
import { hexToResource, spliceHex } from "@latticexyz/common";
import { decodeKey, decodeValueArgs } from "@latticexyz/protocol-parser";

// TODO: upgrade drizzle and use async sqlite interface for consistency
Expand Down Expand Up @@ -61,7 +61,7 @@ export async function sqliteStorage<TConfig extends StoreConfig = StoreConfig>({
logs.map((log) =>
JSON.stringify({
address: getAddress(log.address),
...hexToResourceId(log.args.tableId),
...hexToResource(log.args.tableId),
})
)
)
Expand All @@ -87,7 +87,7 @@ export async function sqliteStorage<TConfig extends StoreConfig = StoreConfig>({
(table) => table.address === getAddress(log.address) && table.tableId === log.args.tableId
);
if (!table) {
const tableId = hexToResourceId(log.args.tableId);
const tableId = hexToResource(log.args.tableId);
debug(`table ${tableId.namespace}:${tableId.name} not found, skipping log`, log);
continue;
}
Expand Down