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
Changes from 1 commit
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor resource ID utils
holic committed Sep 20, 2023
commit 8ed3a75c3a419a288c12c610e4c5f326efc82df6
7 changes: 1 addition & 6 deletions packages/common/src/common.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
export const resourceTypes = {
table: "tb",
offchainTable: "ot",
} as const;

export type ResourceType = (typeof resourceTypes)[keyof typeof resourceTypes];
import { ResourceType } from "./resourceTypes";

export type ResourceId = {
namespace: string;
4 changes: 2 additions & 2 deletions packages/common/src/hexToResourceId.test.ts
Original file line number Diff line number Diff line change
@@ -3,9 +3,9 @@ import { hexToResourceId } from "./hexToResourceId";

describe("hexToResourceId", () => {
it("can convert from hex string", () => {
const resourceId = hexToResourceId("0x6e616d657370616365000000000000006e616d65000000000000000000000000");
const resourceId = hexToResourceId("0x6e616d65737061636500000000006e616d650000000000000000000000007462");
expect(resourceId.namespace).toMatchInlineSnapshot('"namespace"');
expect(resourceId.name).toMatchInlineSnapshot('"name"');
expect(resourceId.type).toMatchInlineSnapshot('"name"');
expect(resourceId.type).toMatchInlineSnapshot('"table"');
});
});
21 changes: 17 additions & 4 deletions packages/common/src/hexToResourceId.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
import { Hex, hexToString, sliceHex } from "viem";
import { ResourceId } from "./common";
import { isResourceType } from "./isResourceType";
import { ResourceType } from "./resourceTypes";
import { resourceTypeIds } from "./resourceIdToHex";
import { ReverseMap } from "./type-utils/common";

const resourceTypeIdToType = Object.fromEntries(
Object.entries(resourceTypeIds).map(([key, value]) => [value, key])
) as ReverseMap<typeof resourceTypeIds>;

function getResourceType(resourceTypeId: string): ResourceType | undefined {
if (Object.hasOwn(resourceTypeIdToType, resourceTypeId)) {
return resourceTypeIdToType[resourceTypeId as keyof typeof resourceTypeIdToType];
}
}

export function hexToResourceId(hex: Hex): ResourceId {
const namespace = hexToString(sliceHex(hex, 0, 14)).replace(/\0+$/, "");
const name = hexToString(sliceHex(hex, 14, 30)).replace(/\0+$/, "");
const type = hexToString(sliceHex(hex, 30, 32)).replace(/\0+$/, "");
const resourceTypeId = hexToString(sliceHex(hex, 30, 32)).replace(/\0+$/, "");
const type = getResourceType(resourceTypeId);

if (!isResourceType(type)) {
throw new Error(`Unknown resource type: ${type}`);
if (!type) {
throw new Error(`Unknown resource type: ${resourceTypeId}`);
}

return { namespace, name, type };
alvrs marked this conversation as resolved.
Show resolved Hide resolved
alvrs marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 1 addition & 1 deletion packages/common/src/index.ts
Original file line number Diff line number Diff line change
@@ -4,8 +4,8 @@ export * from "./createContract";
export * from "./createNonceManager";
export * from "./getBurnerPrivateKey";
export * from "./hexToResourceId";
export * from "./isResourceType";
export * from "./readHex";
export * from "./resourceIdToHex";
export * from "./resourceTypes";
export * from "./spliceHex";
export * from "./transportObserver";
5 changes: 0 additions & 5 deletions packages/common/src/isResourceType.ts

This file was deleted.

17 changes: 8 additions & 9 deletions packages/common/src/resourceIdToHex.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { describe, it, expect } from "vitest";
import { resourceIdToHex } from "./resourceIdToHex";
import { hexToResourceId } from "./hexToResourceId";
import { resourceTypes } from "./common";

describe("resourceIdToHex", () => {
it("can convert table resource to hex string", () => {
const hex = resourceIdToHex({
type: resourceTypes.table,
type: "table",
namespace: "namespace",
name: "name",
});
@@ -15,14 +14,14 @@ describe("resourceIdToHex", () => {
{
"name": "name",
"namespace": "namespace",
"type": "tb",
"type": "table",
}
`);
});

it("can convert offchain table resource to hex string", () => {
const hex = resourceIdToHex({
type: resourceTypes.offchainTable,
type: "offchainTable",
namespace: "namespace",
name: "name",
});
@@ -31,14 +30,14 @@ describe("resourceIdToHex", () => {
{
"name": "name",
"namespace": "namespace",
"type": "ot",
"type": "offchainTable",
}
`);
});

it("truncates namespaces >14 bytes", () => {
const hex = resourceIdToHex({
type: resourceTypes.table,
type: "table",
namespace: "AVeryLongNamespace",
name: "name",
});
@@ -47,14 +46,14 @@ describe("resourceIdToHex", () => {
{
"name": "name",
"namespace": "AVeryLongNames",
"type": "tb",
"type": "table",
}
`);
});

it("truncates names >16 bytes", () => {
const hex = resourceIdToHex({
type: resourceTypes.table,
type: "table",
namespace: "namespace",
name: "AnUnnecessarilyLongName",
});
@@ -63,7 +62,7 @@ describe("resourceIdToHex", () => {
{
"name": "AnUnnecessarilyL",
"namespace": "namespace",
"type": "tb",
"type": "table",
}
`);
});
14 changes: 11 additions & 3 deletions packages/common/src/resourceIdToHex.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import { Hex, stringToHex, concatHex } from "viem";
import { ResourceId } from "./common";
import { ResourceType } from "./resourceTypes";

/** @internal */
holic marked this conversation as resolved.
Show resolved Hide resolved
export const resourceTypeIds = {
table: "tb",
offchainTable: "ot",
} as const satisfies Record<ResourceType, string>;

export function resourceIdToHex(resourceId: ResourceId): Hex {
const typeId = resourceTypeIds[resourceId.type];
return concatHex([
stringToHex(resourceId.namespace.substring(0, 14), { size: 14 }),
stringToHex(resourceId.name.substring(0, 16), { size: 16 }),
stringToHex(resourceId.type.substring(0, 2), { size: 2 }),
stringToHex(resourceId.namespace.slice(0, 14), { size: 14 }),
stringToHex(resourceId.name.slice(0, 16), { size: 16 }),
stringToHex(typeId, { size: 2 }),
]);
}
3 changes: 3 additions & 0 deletions packages/common/src/resourceTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const resourceTypes = ["table", "offchainTable"] as const;

export type ResourceType = (typeof resourceTypes)[number];
5 changes: 5 additions & 0 deletions packages/common/src/type-utils/common.ts
Original file line number Diff line number Diff line change
@@ -21,3 +21,8 @@ export type OrDefaults<T extends object, Defaults> = {
export type UnionOmit<T, K extends keyof any> = T extends any ? Omit<T, K> : never;
export type UnionKeys<T> = T extends any ? keyof T : never;
export type UnionPick<T, K extends UnionKeys<T>> = T extends any ? Pick<T, Extract<K, keyof T>> : never;

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type ReverseMap<T extends Record<any, any>> = {
[K in keyof T as T[K]]: K;
};
2 changes: 1 addition & 1 deletion packages/common/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"compilerOptions": {
/* Visit https://aka.ms/tsconfig.json to read more about this file */
"target": "es2021",
"target": "es2022",
"module": "esnext",
"moduleResolution": "node",
"declaration": true,