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(cli): hardcode table ID with codegen #2229

Merged
merged 7 commits into from
Feb 2, 2024
Merged
Changes from 5 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
10 changes: 10 additions & 0 deletions .changeset/blue-roses-listen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
"@latticexyz/cli": patch
"@latticexyz/common": patch
"@latticexyz/store": patch
"@latticexyz/world-modules": patch
"@latticexyz/world": patch
"create-mud": patch
---

Table libraries now hardcode the `bytes32` table ID value rather than computing it in Solidity. This saves a bit of gas across all storage operations.
5 changes: 1 addition & 4 deletions e2e/packages/contracts/src/codegen/tables/Multi.sol

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 1 addition & 4 deletions e2e/packages/contracts/src/codegen/tables/Number.sol

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 1 addition & 4 deletions e2e/packages/contracts/src/codegen/tables/NumberList.sol

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 1 addition & 4 deletions e2e/packages/contracts/src/codegen/tables/Position.sol

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 1 addition & 4 deletions e2e/packages/contracts/src/codegen/tables/StaticArray.sol

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 1 addition & 4 deletions e2e/packages/contracts/src/codegen/tables/Vector.sol

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 1 addition & 4 deletions packages/cli/contracts/src/codegen/tables/Dynamics1.sol

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 1 addition & 4 deletions packages/cli/contracts/src/codegen/tables/Dynamics2.sol

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 1 addition & 4 deletions packages/cli/contracts/src/codegen/tables/Offchain.sol

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 1 addition & 4 deletions packages/cli/contracts/src/codegen/tables/Singleton.sol

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 1 addition & 4 deletions packages/cli/contracts/src/codegen/tables/Statics.sol

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 1 addition & 4 deletions packages/cli/contracts/src/codegen/tables/UserTyped.sol

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 5 additions & 21 deletions packages/common/src/codegen/render-solidity/common.test.ts
Original file line number Diff line number Diff line change
@@ -62,34 +62,18 @@ describe("renderTableId", () => {
it("returns Solidity code to compute table ID", () => {
expect(renderTableId({ namespace: "somewhere", name: "Player", offchainOnly: false, tableIdName: "PlayerTableId" }))
.toMatchInlineSnapshot(`
"
ResourceId constant _tableId = ResourceId.wrap(
bytes32(
abi.encodePacked(
RESOURCE_TABLE,
bytes14(\\"somewhere\\"),
bytes16(\\"Player\\")
)
)
);
ResourceId constant PlayerTableId = _tableId;
"
`);
ResourceId constant _tableId = ResourceId.wrap(0x7462736f6d6577686572650000000000506c6179657200000000000000000000);
ResourceId constant PlayerTableId = _tableId;
"
`);
});

it("returns Solidity code to compute offchain table ID", () => {
expect(renderTableId({ namespace: "somewhere", name: "Player", offchainOnly: true, tableIdName: "PlayerTableId" }))
.toMatchInlineSnapshot(`
"
ResourceId constant _tableId = ResourceId.wrap(
bytes32(
abi.encodePacked(
RESOURCE_OFFCHAIN_TABLE,
bytes14(\\"somewhere\\"),
bytes16(\\"Player\\")
)
)
);
ResourceId constant _tableId = ResourceId.wrap(0x6f74736f6d6577686572650000000000506c6179657200000000000000000000);
ResourceId constant PlayerTableId = _tableId;
"
`);
15 changes: 6 additions & 9 deletions packages/common/src/codegen/render-solidity/common.ts
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@ import {
RenderType,
} from "./types";
import { posixPath } from "../utils";
import { resourceToHex } from "../../resourceToHex";

/**
* Common header for all codegenerated solidity files
@@ -213,15 +214,11 @@ export function renderTableId({
tableIdName,
}: Pick<StaticResourceData, "namespace" | "name" | "offchainOnly" | "tableIdName">): string {
return `
ResourceId constant _tableId = ResourceId.wrap(
bytes32(
abi.encodePacked(
${offchainOnly ? "RESOURCE_OFFCHAIN_TABLE" : "RESOURCE_TABLE"},
bytes14("${namespace}"),
bytes16("${name}")
)
)
);
ResourceId constant _tableId = ResourceId.wrap(${resourceToHex({
type: offchainOnly ? "offchainTable" : "table",
namespace,
name,
})});
ResourceId constant ${tableIdName} = _tableId;
`;
}
Loading

Unchanged files with check annotations Beta

network: { components, latestBlock$, worldContract, waitForTransaction },
} = await setup();
const _window = window as any;

Check warning on line 9 in e2e/packages/client-vanilla/src/index.ts

GitHub Actions / Run tests

Unexpected any. Specify a different type
_window.worldContract = worldContract;
_window.waitForTransaction = waitForTransaction;
export type SystemCalls = ReturnType<typeof createSystemCalls>;
export function createSystemCalls(network: SetupNetworkResult, components: ClientComponents) {

Check warning on line 6 in e2e/packages/client-vanilla/src/mud/createSystemCalls.ts

GitHub Actions / Run tests

'network' is defined but never used

Check warning on line 6 in e2e/packages/client-vanilla/src/mud/createSystemCalls.ts

GitHub Actions / Run tests

'components' is defined but never used
return {
// TODO
};
const context = [functionName, args, serialize.toString(), deserialize.toString()] as const;
const serializedValue = await page.evaluate(async ([functionName, args, serializeString, deserializeString]) => {
const _serialize = deserializeFunction(serializeString);
const _deserialize = deserializeFunction(deserializeString);

Check warning on line 17 in e2e/packages/sync-test/data/callPageFunction.ts

GitHub Actions / Run tests

'_deserialize' is assigned a value but never used
const value = await (window as any)[functionName](...args);

Check warning on line 18 in e2e/packages/sync-test/data/callPageFunction.ts

GitHub Actions / Run tests

Unexpected any. Specify a different type
const serializedValue = value ? _serialize(value) : undefined;
return serializedValue;
export function callWorld<TMethod extends WriteMethodName>(page: Page, method: TMethod, args?: WriteArgs<TMethod>) {
return page.evaluate(
([_method, _args]) => {
const worldContract = (window as any).worldContract as WorldContract;

Check warning on line 17 in e2e/packages/sync-test/data/callWorld.ts

GitHub Actions / Run tests

Unexpected any. Specify a different type
const writeMethod = worldContract.write[_method as any];

Check warning on line 18 in e2e/packages/sync-test/data/callWorld.ts

GitHub Actions / Run tests

Unexpected any. Specify a different type
return writeMethod(_args)
.then((tx) => window["waitForTransaction"](tx))
.catch((error) => {
import React from "react";

Check warning on line 1 in examples/minimal/packages/client-phaser/src/ui/Wrapper.tsx

GitHub Actions / Run tests

'React' is defined but never used
import styled from "styled-components";
export const Wrapper = styled.div`
import React, { useEffect, useMemo } from "react";

Check warning on line 1 in examples/minimal/packages/client-phaser/src/ui/hooks/useNetworkLayer.tsx

GitHub Actions / Run tests

'React' is defined but never used
import { createNetworkLayer } from "../../layers/network/createNetworkLayer";
import { usePromiseValue } from "./usePromiseValue";
import React, { useCallback, useEffect, useMemo, useRef, useState } from "react";

Check warning on line 1 in examples/minimal/packages/client-phaser/src/ui/hooks/usePhaserLayer.tsx

GitHub Actions / Run tests

'React' is defined but never used
import useResizeObserver, { ResizeHandler } from "use-resize-observer";
import { throttle } from "lodash";
import { createPhaserLayer } from "../../layers/phaser/createPhaserLayer";