Skip to content

Commit

Permalink
feat(cli): hardcode table ID with codegen (#2229)
Browse files Browse the repository at this point in the history
  • Loading branch information
holic committed Feb 2, 2024
1 parent db7798b commit a35c05e
Show file tree
Hide file tree
Showing 70 changed files with 274 additions and 387 deletions.
10 changes: 10 additions & 0 deletions .changeset/blue-roses-listen.md
@@ -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.
6 changes: 2 additions & 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.

6 changes: 2 additions & 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.

6 changes: 2 additions & 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.

6 changes: 2 additions & 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.

6 changes: 2 additions & 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.

6 changes: 2 additions & 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.

6 changes: 2 additions & 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.

6 changes: 2 additions & 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.

6 changes: 2 additions & 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.

6 changes: 2 additions & 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.

6 changes: 2 additions & 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.

6 changes: 2 additions & 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.

28 changes: 7 additions & 21 deletions packages/common/src/codegen/render-solidity/common.test.ts
Expand Up @@ -62,34 +62,20 @@ 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;
"
`);
// Hex below is the result of \`WorldResourceIdLib.encode({ namespace: \\"somewhere\\", name: \\"Player\\", typeId: RESOURCE_TABLE });\`
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\\")
)
)
);
// Hex below is the result of \`WorldResourceIdLib.encode({ namespace: \\"somewhere\\", name: \\"Player\\", typeId: RESOURCE_OFFCHAIN_TABLE });\`
ResourceId constant _tableId = ResourceId.wrap(0x6f74736f6d6577686572650000000000506c6179657200000000000000000000);
ResourceId constant PlayerTableId = _tableId;
"
`);
Expand Down
18 changes: 9 additions & 9 deletions packages/common/src/codegen/render-solidity/common.ts
Expand Up @@ -8,6 +8,7 @@ import {
RenderType,
} from "./types";
import { posixPath } from "../utils";
import { resourceToHex } from "../../resourceToHex";

/**
* Common header for all codegenerated solidity files
Expand Down Expand Up @@ -213,15 +214,14 @@ 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}")
)
)
);
// Hex below is the result of \`WorldResourceIdLib.encode({ namespace: ${JSON.stringify(
namespace
)}, name: ${JSON.stringify(name)}, typeId: ${offchainOnly ? "RESOURCE_OFFCHAIN_TABLE" : "RESOURCE_TABLE"} });\`
ResourceId constant _tableId = ResourceId.wrap(${resourceToHex({
type: offchainOnly ? "offchainTable" : "table",
namespace,
name,
})});
ResourceId constant ${tableIdName} = _tableId;
`;
}
Expand Down

0 comments on commit a35c05e

Please sign in to comment.