From c1d2249bb8dbddb4911e0d3503c55e06072f4fcd Mon Sep 17 00:00:00 2001 From: dk1a Date: Thu, 3 Aug 2023 20:10:03 +0300 Subject: [PATCH] refactor(store): separate SchemaLib into specialized libraries (#1216) --- .../abi/Schema.sol/SchemaInstance.abi.json | 1 + .../SchemaEncodeHelper.abi.json | 1 + packages/store/gas-report.json | 14 ++-- packages/store/src/Schema.sol | 84 ++++--------------- packages/store/src/StoreCore.sol | 9 +- packages/store/test/Schema.t.sol | 15 ++-- packages/store/test/SchemaEncodeHelper.sol | 68 +++++++++++++++ packages/store/test/StoreCore.t.sol | 53 +++++++----- packages/store/test/StoreCoreDynamic.t.sol | 7 +- packages/store/test/StoreCoreGas.t.sol | 42 ++++++---- .../abi/Schema.sol/SchemaInstance.abi.json | 1 + .../src/Schema.sol/SchemaInstance.abi.json | 1 + packages/world/test/KeysInTableModule.t.sol | 7 +- packages/world/test/KeysWithValueModule.t.sol | 7 +- packages/world/test/SnapSyncModule.t.sol | 7 +- packages/world/test/World.t.sol | 14 ++-- packages/world/test/WorldDynamicUpdate.t.sol | 5 +- packages/world/test/query.t.sol | 7 +- 18 files changed, 199 insertions(+), 144 deletions(-) create mode 100644 packages/store/abi/Schema.sol/SchemaInstance.abi.json create mode 100644 packages/store/abi/SchemaEncodeHelper.sol/SchemaEncodeHelper.abi.json create mode 100644 packages/store/test/SchemaEncodeHelper.sol create mode 100644 packages/world/abi/Schema.sol/SchemaInstance.abi.json create mode 100644 packages/world/abi/src/Schema.sol/SchemaInstance.abi.json diff --git a/packages/store/abi/Schema.sol/SchemaInstance.abi.json b/packages/store/abi/Schema.sol/SchemaInstance.abi.json new file mode 100644 index 0000000000..0637a088a0 --- /dev/null +++ b/packages/store/abi/Schema.sol/SchemaInstance.abi.json @@ -0,0 +1 @@ +[] \ No newline at end of file diff --git a/packages/store/abi/SchemaEncodeHelper.sol/SchemaEncodeHelper.abi.json b/packages/store/abi/SchemaEncodeHelper.sol/SchemaEncodeHelper.abi.json new file mode 100644 index 0000000000..0637a088a0 --- /dev/null +++ b/packages/store/abi/SchemaEncodeHelper.sol/SchemaEncodeHelper.abi.json @@ -0,0 +1 @@ +[] \ No newline at end of file diff --git a/packages/store/gas-report.json b/packages/store/gas-report.json index 4f8c9d2578..d6c2c2aab7 100644 --- a/packages/store/gas-report.json +++ b/packages/store/gas-report.json @@ -243,7 +243,7 @@ "file": "test/KeyEncoding.t.sol", "test": "testRegisterAndGetSchema", "name": "register KeyEncoding schema", - "gasUsed": 64679 + "gasUsed": 65010 }, { "file": "test/Mixed.t.sol", @@ -255,7 +255,7 @@ "file": "test/Mixed.t.sol", "test": "testRegisterAndGetSchema", "name": "register Mixed schema", - "gasUsed": 61687 + "gasUsed": 61992 }, { "file": "test/Mixed.t.sol", @@ -267,7 +267,7 @@ "file": "test/Mixed.t.sol", "test": "testSetAndGet", "name": "get record from Mixed", - "gasUsed": 12438 + "gasUsed": 12610 }, { "file": "test/PackedCounter.t.sol", @@ -296,7 +296,7 @@ { "file": "test/Schema.t.sol", "test": "testEncodeDecodeSchema", - "name": "encode schema with 6 entries [SchemaLib.encode]", + "name": "encode schema with 6 entries", "gasUsed": 5639 }, { @@ -771,7 +771,7 @@ "file": "test/StoreMetadata.t.sol", "test": "testSetAndGet", "name": "get record from StoreMetadataTable", - "gasUsed": 11430 + "gasUsed": 11576 }, { "file": "test/StoreSwitch.t.sol", @@ -879,7 +879,7 @@ "file": "test/Vector2.t.sol", "test": "testRegisterAndGetSchema", "name": "register Vector2 schema", - "gasUsed": 58890 + "gasUsed": 59169 }, { "file": "test/Vector2.t.sol", @@ -891,6 +891,6 @@ "file": "test/Vector2.t.sol", "test": "testSetAndGet", "name": "get Vector2 record", - "gasUsed": 4403 + "gasUsed": 4549 } ] diff --git a/packages/store/src/Schema.sol b/packages/store/src/Schema.sol index feba6f1d08..f58f344773 100644 --- a/packages/store/src/Schema.sol +++ b/packages/store/src/Schema.sol @@ -10,8 +10,11 @@ import { Bytes } from "./Bytes.sol"; // - 28 bytes for 28 schema types (MAX_DYNAMIC_FIELDS allows us to pack the lengths into 1 word) type Schema is bytes32; -using SchemaLib for Schema global; +using SchemaInstance for Schema global; +/** + * Static functions for Schema + */ library SchemaLib { error SchemaLib_InvalidLength(uint256 length); error SchemaLib_StaticTypeAfterDynamicType(); @@ -68,66 +71,12 @@ library SchemaLib { return Schema.wrap(schema); } +} - // Overrides for encode functions - function encode(SchemaType a) internal pure returns (Schema) { - SchemaType[] memory schema = new SchemaType[](1); - schema[0] = a; - return encode(schema); - } - - function encode(SchemaType a, SchemaType b) internal pure returns (Schema) { - SchemaType[] memory schema = new SchemaType[](2); - schema[0] = a; - schema[1] = b; - return encode(schema); - } - - function encode(SchemaType a, SchemaType b, SchemaType c) internal pure returns (Schema) { - SchemaType[] memory schema = new SchemaType[](3); - schema[0] = a; - schema[1] = b; - schema[2] = c; - return encode(schema); - } - - function encode(SchemaType a, SchemaType b, SchemaType c, SchemaType d) internal pure returns (Schema) { - SchemaType[] memory schema = new SchemaType[](4); - schema[0] = a; - schema[1] = b; - schema[2] = c; - schema[3] = d; - return encode(schema); - } - - function encode(SchemaType a, SchemaType b, SchemaType c, SchemaType d, SchemaType e) internal pure returns (Schema) { - SchemaType[] memory schema = new SchemaType[](5); - schema[0] = a; - schema[1] = b; - schema[2] = c; - schema[3] = d; - schema[4] = e; - return encode(schema); - } - - function encode( - SchemaType a, - SchemaType b, - SchemaType c, - SchemaType d, - SchemaType e, - SchemaType f - ) internal pure returns (Schema) { - SchemaType[] memory schema = new SchemaType[](6); - schema[0] = a; - schema[1] = b; - schema[2] = c; - schema[3] = d; - schema[4] = e; - schema[5] = f; - return encode(schema); - } - +/** + * Instance functions for Schema + */ +library SchemaInstance { /************************************************************************ * * INSTANCE FUNCTIONS @@ -178,15 +127,16 @@ library SchemaLib { function validate(Schema schema, bool allowEmpty) internal pure { // Schema must not be empty - if (!allowEmpty && schema.isEmpty()) revert SchemaLib_InvalidLength(0); + if (!allowEmpty && schema.isEmpty()) revert SchemaLib.SchemaLib_InvalidLength(0); // Schema must have no more than MAX_DYNAMIC_FIELDS uint256 _numDynamicFields = schema.numDynamicFields(); - if (_numDynamicFields > MAX_DYNAMIC_FIELDS) revert SchemaLib_InvalidLength(_numDynamicFields); + if (_numDynamicFields > SchemaLib.MAX_DYNAMIC_FIELDS) revert SchemaLib.SchemaLib_InvalidLength(_numDynamicFields); uint256 _numStaticFields = schema.numStaticFields(); // Schema must not have more than 28 fields in total - if (_numStaticFields + _numDynamicFields > 28) revert SchemaLib_InvalidLength(_numStaticFields + _numDynamicFields); + if (_numStaticFields + _numDynamicFields > 28) + revert SchemaLib.SchemaLib_InvalidLength(_numStaticFields + _numDynamicFields); // No static field can be after a dynamic field uint256 countStaticFields; @@ -194,11 +144,11 @@ library SchemaLib { for (uint256 i; i < _numStaticFields + _numDynamicFields; ) { if (schema.atIndex(i).getStaticByteLength() > 0) { // Static field in dynamic part - if (i >= _numStaticFields) revert SchemaLib_StaticTypeAfterDynamicType(); + if (i >= _numStaticFields) revert SchemaLib.SchemaLib_StaticTypeAfterDynamicType(); countStaticFields++; } else { // Dynamic field in static part - if (i < _numStaticFields) revert SchemaLib_StaticTypeAfterDynamicType(); + if (i < _numStaticFields) revert SchemaLib.SchemaLib_StaticTypeAfterDynamicType(); countDynamicFields++; } unchecked { @@ -207,10 +157,10 @@ library SchemaLib { } // Number of static fields must match - if (countStaticFields != _numStaticFields) revert SchemaLib_InvalidLength(countStaticFields); + if (countStaticFields != _numStaticFields) revert SchemaLib.SchemaLib_InvalidLength(countStaticFields); // Number of dynamic fields must match - if (countDynamicFields != _numDynamicFields) revert SchemaLib_InvalidLength(countDynamicFields); + if (countDynamicFields != _numDynamicFields) revert SchemaLib.SchemaLib_InvalidLength(countDynamicFields); } /** diff --git a/packages/store/src/StoreCore.sol b/packages/store/src/StoreCore.sol index 251d5c7785..af407d6009 100644 --- a/packages/store/src/StoreCore.sol +++ b/packages/store/src/StoreCore.sol @@ -36,10 +36,15 @@ library StoreCore { StoreSwitch.setStoreAddress(address(this)); // Register internal schema table + SchemaType[] memory _valueSchema = new SchemaType[](2); + _valueSchema[0] = SchemaType.BYTES32; + _valueSchema[1] = SchemaType.BYTES32; + SchemaType[] memory _keySchema = new SchemaType[](1); + _keySchema[0] = SchemaType.BYTES32; registerSchema( StoreCoreInternal.SCHEMA_TABLE, - SchemaLib.encode(SchemaType.BYTES32, SchemaType.BYTES32), // The Schema table's valueSchema is { valueSchema: BYTES32, keySchema: BYTES32 } - SchemaLib.encode(SchemaType.BYTES32) // The Schema table's keySchema is { tableId: BYTES32 } + SchemaLib.encode(_valueSchema), // The Schema table's valueSchema is { valueSchema: BYTES32, keySchema: BYTES32 } + SchemaLib.encode(_keySchema) // The Schema table's keySchema is { tableId: BYTES32 } ); // Register other internal tables diff --git a/packages/store/test/Schema.t.sol b/packages/store/test/Schema.t.sol index 11af647889..9a0aba97d3 100644 --- a/packages/store/test/Schema.t.sol +++ b/packages/store/test/Schema.t.sol @@ -5,12 +5,13 @@ import { Test, console } from "forge-std/Test.sol"; import { GasReporter } from "@latticexyz/gas-report/src/GasReporter.sol"; import { SchemaType } from "@latticexyz/schema-type/src/solidity/SchemaType.sol"; import { Schema, SchemaLib } from "../src/Schema.sol"; +import { SchemaEncodeHelper } from "./SchemaEncodeHelper.sol"; // TODO add tests for all schema types contract SchemaTest is Test, GasReporter { function testEncodeDecodeSchema() public { - startGasReport("encode schema with 6 entries [SchemaLib.encode]"); - Schema schema = SchemaLib.encode( + startGasReport("encode schema with 6 entries"); + Schema schema = SchemaEncodeHelper.encode( SchemaType.UINT8, // 1 byte SchemaType.UINT16, // 2 bytes SchemaType.UINT32, // 4 bytes @@ -33,7 +34,7 @@ contract SchemaTest is Test, GasReporter { } function testFailInvalidSchemaStaticAfterDynamic() public pure { - SchemaLib.encode(SchemaType.UINT8, SchemaType.UINT32_ARRAY, SchemaType.UINT16); + SchemaEncodeHelper.encode(SchemaType.UINT8, SchemaType.UINT32_ARRAY, SchemaType.UINT16); } function testEncodeMaxValidLength() public { @@ -129,7 +130,7 @@ contract SchemaTest is Test, GasReporter { } function testGetStaticSchemaLength() public { - Schema schema = SchemaLib.encode( + Schema schema = SchemaEncodeHelper.encode( SchemaType.UINT8, // 1 byte SchemaType.UINT16, // 2 bytes SchemaType.UINT32, // 4 bytes @@ -146,7 +147,7 @@ contract SchemaTest is Test, GasReporter { } function testGetNumStaticFields() public { - Schema schema = SchemaLib.encode( + Schema schema = SchemaEncodeHelper.encode( SchemaType.UINT8, // 1 byte SchemaType.UINT16, // 2 bytes SchemaType.UINT32, // 4 bytes @@ -163,7 +164,7 @@ contract SchemaTest is Test, GasReporter { } function testGetNumDynamicFields() public { - Schema schema = SchemaLib.encode( + Schema schema = SchemaEncodeHelper.encode( SchemaType.UINT8, // 1 byte SchemaType.UINT16, // 2 bytes SchemaType.UINT32, // 4 bytes @@ -232,7 +233,7 @@ contract SchemaTest is Test, GasReporter { } function testIsEmptyFalse() public { - Schema encodedSchema = SchemaLib.encode(SchemaType.UINT256); + Schema encodedSchema = SchemaEncodeHelper.encode(SchemaType.UINT256); startGasReport("check if schema is empty (non-empty schema)"); bool empty = encodedSchema.isEmpty(); diff --git a/packages/store/test/SchemaEncodeHelper.sol b/packages/store/test/SchemaEncodeHelper.sol new file mode 100644 index 0000000000..2fc21b70fa --- /dev/null +++ b/packages/store/test/SchemaEncodeHelper.sol @@ -0,0 +1,68 @@ +// SPDX-License-Identifier: MIT +pragma solidity >=0.8.0; + +import { SchemaType } from "@latticexyz/schema-type/src/solidity/SchemaType.sol"; +import { Schema, SchemaLib } from "../src/Schema.sol"; + +/** + * Overrides for encode function to simplify tests + */ +library SchemaEncodeHelper { + function encode(SchemaType a) internal pure returns (Schema) { + SchemaType[] memory schema = new SchemaType[](1); + schema[0] = a; + return SchemaLib.encode(schema); + } + + function encode(SchemaType a, SchemaType b) internal pure returns (Schema) { + SchemaType[] memory schema = new SchemaType[](2); + schema[0] = a; + schema[1] = b; + return SchemaLib.encode(schema); + } + + function encode(SchemaType a, SchemaType b, SchemaType c) internal pure returns (Schema) { + SchemaType[] memory schema = new SchemaType[](3); + schema[0] = a; + schema[1] = b; + schema[2] = c; + return SchemaLib.encode(schema); + } + + function encode(SchemaType a, SchemaType b, SchemaType c, SchemaType d) internal pure returns (Schema) { + SchemaType[] memory schema = new SchemaType[](4); + schema[0] = a; + schema[1] = b; + schema[2] = c; + schema[3] = d; + return SchemaLib.encode(schema); + } + + function encode(SchemaType a, SchemaType b, SchemaType c, SchemaType d, SchemaType e) internal pure returns (Schema) { + SchemaType[] memory schema = new SchemaType[](5); + schema[0] = a; + schema[1] = b; + schema[2] = c; + schema[3] = d; + schema[4] = e; + return SchemaLib.encode(schema); + } + + function encode( + SchemaType a, + SchemaType b, + SchemaType c, + SchemaType d, + SchemaType e, + SchemaType f + ) internal pure returns (Schema) { + SchemaType[] memory schema = new SchemaType[](6); + schema[0] = a; + schema[1] = b; + schema[2] = c; + schema[3] = d; + schema[4] = e; + schema[5] = f; + return SchemaLib.encode(schema); + } +} diff --git a/packages/store/test/StoreCore.t.sol b/packages/store/test/StoreCore.t.sol index 613c7914ff..a3a70a4acb 100644 --- a/packages/store/test/StoreCore.t.sol +++ b/packages/store/test/StoreCore.t.sol @@ -8,13 +8,14 @@ import { Bytes } from "../src/Bytes.sol"; import { TableId } from "../src/TableId.sol"; import { SliceLib } from "../src/Slice.sol"; import { EncodeArray } from "../src/tightcoder/EncodeArray.sol"; -import { Schema, SchemaLib } from "../src/Schema.sol"; +import { Schema } from "../src/Schema.sol"; import { PackedCounter, PackedCounterLib } from "../src/PackedCounter.sol"; import { StoreReadWithStubs } from "../src/StoreReadWithStubs.sol"; import { IStoreErrors } from "../src/IStoreErrors.sol"; import { IStore } from "../src/IStore.sol"; import { StoreSwitch } from "../src/StoreSwitch.sol"; import { StoreMetadataData, StoreMetadata } from "../src/codegen/Tables.sol"; +import { SchemaEncodeHelper } from "./SchemaEncodeHelper.sol"; import { StoreMock } from "./StoreMock.sol"; import { MirrorSubscriber, indexerTableId } from "./MirrorSubscriber.sol"; @@ -28,11 +29,11 @@ contract StoreCoreTest is Test, StoreMock { TestStruct private testStruct; mapping(uint256 => bytes) private testMapping; - Schema defaultKeySchema = SchemaLib.encode(SchemaType.BYTES32); + Schema defaultKeySchema = SchemaEncodeHelper.encode(SchemaType.BYTES32); function testRegisterAndGetSchema() public { - Schema schema = SchemaLib.encode(SchemaType.UINT8, SchemaType.UINT16, SchemaType.UINT8, SchemaType.UINT16); - Schema keySchema = SchemaLib.encode(SchemaType.UINT8, SchemaType.UINT16); + Schema schema = SchemaEncodeHelper.encode(SchemaType.UINT8, SchemaType.UINT16, SchemaType.UINT8, SchemaType.UINT16); + Schema keySchema = SchemaEncodeHelper.encode(SchemaType.UINT8, SchemaType.UINT16); bytes32 table = keccak256("some.table"); @@ -64,7 +65,7 @@ contract StoreCoreTest is Test, StoreMock { } function testHasSchema() public { - Schema schema = SchemaLib.encode(SchemaType.UINT8, SchemaType.UINT16, SchemaType.UINT8, SchemaType.UINT16); + Schema schema = SchemaEncodeHelper.encode(SchemaType.UINT8, SchemaType.UINT16, SchemaType.UINT8, SchemaType.UINT16); bytes32 table = keccak256("some.table"); bytes32 table2 = keccak256("other.table"); IStore(this).registerSchema(table, schema, defaultKeySchema); @@ -88,8 +89,13 @@ contract StoreCoreTest is Test, StoreMock { function testSetMetadata() public { bytes32 table = keccak256("some.table"); - Schema schema = SchemaLib.encode(SchemaType.UINT8, SchemaType.UINT16); - Schema keySchema = SchemaLib.encode(SchemaType.UINT8, SchemaType.UINT16, SchemaType.UINT8, SchemaType.UINT16); + Schema schema = SchemaEncodeHelper.encode(SchemaType.UINT8, SchemaType.UINT16); + Schema keySchema = SchemaEncodeHelper.encode( + SchemaType.UINT8, + SchemaType.UINT16, + SchemaType.UINT8, + SchemaType.UINT16 + ); string memory tableName = "someTable"; string[] memory fieldNames = new string[](2); fieldNames[0] = "field1"; @@ -109,8 +115,13 @@ contract StoreCoreTest is Test, StoreMock { function testlSetMetadataRevert() public { bytes32 table = keccak256("some.table"); - Schema schema = SchemaLib.encode(SchemaType.UINT8); - Schema keySchema = SchemaLib.encode(SchemaType.UINT8, SchemaType.UINT16, SchemaType.UINT8, SchemaType.UINT16); + Schema schema = SchemaEncodeHelper.encode(SchemaType.UINT8); + Schema keySchema = SchemaEncodeHelper.encode( + SchemaType.UINT8, + SchemaType.UINT16, + SchemaType.UINT8, + SchemaType.UINT16 + ); string memory tableName = "someTable"; string[] memory fieldNames = new string[](2); fieldNames[0] = "field1"; @@ -126,7 +137,7 @@ contract StoreCoreTest is Test, StoreMock { function testSetAndGetDynamicDataLength() public { bytes32 table = keccak256("some.table"); - Schema schema = SchemaLib.encode( + Schema schema = SchemaEncodeHelper.encode( SchemaType.UINT8, SchemaType.UINT16, SchemaType.UINT32, @@ -168,7 +179,7 @@ contract StoreCoreTest is Test, StoreMock { function testSetAndGetStaticData() public { // Register table's schema - Schema schema = SchemaLib.encode(SchemaType.UINT8, SchemaType.UINT16, SchemaType.UINT8, SchemaType.UINT16); + Schema schema = SchemaEncodeHelper.encode(SchemaType.UINT8, SchemaType.UINT16, SchemaType.UINT8, SchemaType.UINT16); bytes32 table = keccak256("some.table"); IStore(this).registerSchema(table, schema, defaultKeySchema); @@ -193,7 +204,7 @@ contract StoreCoreTest is Test, StoreMock { function testFailSetAndGetStaticData() public { // Register table's schema - Schema schema = SchemaLib.encode(SchemaType.UINT8, SchemaType.UINT16, SchemaType.UINT8, SchemaType.UINT16); + Schema schema = SchemaEncodeHelper.encode(SchemaType.UINT8, SchemaType.UINT16, SchemaType.UINT8, SchemaType.UINT16); bytes32 table = keccak256("some.table"); IStore(this).registerSchema(table, schema, defaultKeySchema); @@ -209,7 +220,7 @@ contract StoreCoreTest is Test, StoreMock { function testSetAndGetStaticDataSpanningWords() public { // Register table's schema - Schema schema = SchemaLib.encode(SchemaType.UINT128, SchemaType.UINT256); + Schema schema = SchemaEncodeHelper.encode(SchemaType.UINT128, SchemaType.UINT256); bytes32 table = keccak256("some.table"); IStore(this).registerSchema(table, schema, defaultKeySchema); @@ -239,7 +250,7 @@ contract StoreCoreTest is Test, StoreMock { { // Register table's schema - Schema schema = SchemaLib.encode(SchemaType.UINT128, SchemaType.UINT32_ARRAY, SchemaType.UINT32_ARRAY); + Schema schema = SchemaEncodeHelper.encode(SchemaType.UINT128, SchemaType.UINT32_ARRAY, SchemaType.UINT32_ARRAY); IStore(this).registerSchema(table, schema, defaultKeySchema); } @@ -314,7 +325,7 @@ contract StoreCoreTest is Test, StoreMock { { // Register table's schema - Schema schema = SchemaLib.encode( + Schema schema = SchemaEncodeHelper.encode( SchemaType.UINT128, SchemaType.UINT256, SchemaType.UINT32_ARRAY, @@ -453,7 +464,7 @@ contract StoreCoreTest is Test, StoreMock { bytes32 table = keccak256("some.table"); // Register table's schema - Schema schema = SchemaLib.encode(SchemaType.UINT128, SchemaType.UINT32_ARRAY, SchemaType.UINT32_ARRAY); + Schema schema = SchemaEncodeHelper.encode(SchemaType.UINT128, SchemaType.UINT32_ARRAY, SchemaType.UINT32_ARRAY); IStore(this).registerSchema(table, schema, defaultKeySchema); bytes16 firstDataBytes = bytes16(0x0102030405060708090a0b0c0d0e0f10); @@ -521,7 +532,7 @@ contract StoreCoreTest is Test, StoreMock { { // Register table's schema - Schema schema = SchemaLib.encode(SchemaType.UINT256, SchemaType.UINT32_ARRAY, SchemaType.UINT32_ARRAY); + Schema schema = SchemaEncodeHelper.encode(SchemaType.UINT256, SchemaType.UINT32_ARRAY, SchemaType.UINT32_ARRAY); IStore(this).registerSchema(table, schema, defaultKeySchema); } @@ -624,7 +635,7 @@ contract StoreCoreTest is Test, StoreMock { { // Register table's schema - Schema schema = SchemaLib.encode(SchemaType.UINT256, SchemaType.UINT32_ARRAY, SchemaType.UINT64_ARRAY); + Schema schema = SchemaEncodeHelper.encode(SchemaType.UINT256, SchemaType.UINT32_ARRAY, SchemaType.UINT64_ARRAY); IStore(this).registerSchema(table, schema, defaultKeySchema); } @@ -732,7 +743,7 @@ contract StoreCoreTest is Test, StoreMock { function testAccessEmptyData() public { bytes32 table = keccak256("some.table"); - Schema schema = SchemaLib.encode(SchemaType.UINT32, SchemaType.UINT32_ARRAY); + Schema schema = SchemaEncodeHelper.encode(SchemaType.UINT32, SchemaType.UINT32_ARRAY); IStore(this).registerSchema(table, schema, defaultKeySchema); @@ -762,7 +773,7 @@ contract StoreCoreTest is Test, StoreMock { key[0] = keccak256("some key"); // Register table's schema - Schema schema = SchemaLib.encode(SchemaType.UINT128); + Schema schema = SchemaEncodeHelper.encode(SchemaType.UINT128); IStore(this).registerSchema(table, schema, defaultKeySchema); // Create subscriber @@ -799,7 +810,7 @@ contract StoreCoreTest is Test, StoreMock { key[0] = keccak256("some key"); // Register table's schema - Schema schema = SchemaLib.encode(SchemaType.UINT128, SchemaType.UINT32_ARRAY); + Schema schema = SchemaEncodeHelper.encode(SchemaType.UINT128, SchemaType.UINT32_ARRAY); IStore(this).registerSchema(table, schema, defaultKeySchema); // Create subscriber diff --git a/packages/store/test/StoreCoreDynamic.t.sol b/packages/store/test/StoreCoreDynamic.t.sol index 10aac65a36..0db7e3f4b0 100644 --- a/packages/store/test/StoreCoreDynamic.t.sol +++ b/packages/store/test/StoreCoreDynamic.t.sol @@ -7,11 +7,12 @@ import { SchemaType } from "@latticexyz/schema-type/src/solidity/SchemaType.sol" import { StoreCore } from "../src/StoreCore.sol"; import { SliceLib } from "../src/Slice.sol"; import { EncodeArray } from "../src/tightcoder/EncodeArray.sol"; -import { Schema, SchemaLib } from "../src/Schema.sol"; +import { Schema } from "../src/Schema.sol"; import { StoreReadWithStubs } from "../src/StoreReadWithStubs.sol"; +import { SchemaEncodeHelper } from "./SchemaEncodeHelper.sol"; contract StoreCoreDynamicTest is Test, GasReporter, StoreReadWithStubs { - Schema internal defaultKeySchema = SchemaLib.encode(SchemaType.BYTES32); + Schema internal defaultKeySchema = SchemaEncodeHelper.encode(SchemaType.BYTES32); bytes32[] internal _key; bytes32 internal _table = keccak256("some.table"); @@ -34,7 +35,7 @@ contract StoreCoreDynamicTest is Test, GasReporter, StoreReadWithStubs { function setUp() public { // Register table's schema - Schema schema = SchemaLib.encode(SchemaType.UINT256, SchemaType.UINT32_ARRAY, SchemaType.UINT32_ARRAY); + Schema schema = SchemaEncodeHelper.encode(SchemaType.UINT256, SchemaType.UINT32_ARRAY, SchemaType.UINT32_ARRAY); StoreCore.registerSchema(_table, schema, defaultKeySchema); // Create key diff --git a/packages/store/test/StoreCoreGas.t.sol b/packages/store/test/StoreCoreGas.t.sol index 8bb60b01fb..3f800707b3 100644 --- a/packages/store/test/StoreCoreGas.t.sol +++ b/packages/store/test/StoreCoreGas.t.sol @@ -8,13 +8,14 @@ import { StoreCore, StoreCoreInternal } from "../src/StoreCore.sol"; import { Bytes } from "../src/Bytes.sol"; import { SliceLib } from "../src/Slice.sol"; import { EncodeArray } from "../src/tightcoder/EncodeArray.sol"; -import { Schema, SchemaLib } from "../src/Schema.sol"; +import { Schema } from "../src/Schema.sol"; import { PackedCounter, PackedCounterLib } from "../src/PackedCounter.sol"; import { StoreReadWithStubs } from "../src/StoreReadWithStubs.sol"; import { IStoreErrors } from "../src/IStoreErrors.sol"; import { IStore } from "../src/IStore.sol"; import { StoreSwitch } from "../src/StoreSwitch.sol"; import { StoreMetadataData, StoreMetadata } from "../src/codegen/Tables.sol"; +import { SchemaEncodeHelper } from "./SchemaEncodeHelper.sol"; import { StoreMock } from "./StoreMock.sol"; import { MirrorSubscriber } from "./MirrorSubscriber.sol"; @@ -28,11 +29,11 @@ contract StoreCoreGasTest is Test, GasReporter, StoreMock { TestStruct private testStruct; mapping(uint256 => bytes) private testMapping; - Schema defaultKeySchema = SchemaLib.encode(SchemaType.BYTES32); + Schema defaultKeySchema = SchemaEncodeHelper.encode(SchemaType.BYTES32); function testRegisterAndGetSchema() public { - Schema schema = SchemaLib.encode(SchemaType.UINT8, SchemaType.UINT16, SchemaType.UINT8, SchemaType.UINT16); - Schema keySchema = SchemaLib.encode(SchemaType.UINT8, SchemaType.UINT16); + Schema schema = SchemaEncodeHelper.encode(SchemaType.UINT8, SchemaType.UINT16, SchemaType.UINT8, SchemaType.UINT16); + Schema keySchema = SchemaEncodeHelper.encode(SchemaType.UINT8, SchemaType.UINT16); bytes32 table = keccak256("some.table"); startGasReport("StoreCore: register schema"); @@ -49,7 +50,7 @@ contract StoreCoreGasTest is Test, GasReporter, StoreMock { } function testHasSchema() public { - Schema schema = SchemaLib.encode(SchemaType.UINT8, SchemaType.UINT16, SchemaType.UINT8, SchemaType.UINT16); + Schema schema = SchemaEncodeHelper.encode(SchemaType.UINT8, SchemaType.UINT16, SchemaType.UINT8, SchemaType.UINT16); bytes32 table = keccak256("some.table"); bytes32 table2 = keccak256("other.table"); StoreCore.registerSchema(table, schema, defaultKeySchema); @@ -65,8 +66,13 @@ contract StoreCoreGasTest is Test, GasReporter, StoreMock { function testSetMetadata() public { bytes32 table = keccak256("some.table"); - Schema schema = SchemaLib.encode(SchemaType.UINT8, SchemaType.UINT16); - Schema keySchema = SchemaLib.encode(SchemaType.UINT8, SchemaType.UINT16, SchemaType.UINT8, SchemaType.UINT16); + Schema schema = SchemaEncodeHelper.encode(SchemaType.UINT8, SchemaType.UINT16); + Schema keySchema = SchemaEncodeHelper.encode( + SchemaType.UINT8, + SchemaType.UINT16, + SchemaType.UINT8, + SchemaType.UINT16 + ); string memory tableName = "someTable"; string[] memory fieldNames = new string[](2); fieldNames[0] = "field1"; @@ -83,7 +89,7 @@ contract StoreCoreGasTest is Test, GasReporter, StoreMock { function testSetAndGetDynamicDataLength() public { bytes32 table = keccak256("some.table"); - Schema schema = SchemaLib.encode( + Schema schema = SchemaEncodeHelper.encode( SchemaType.UINT8, SchemaType.UINT16, SchemaType.UINT32, @@ -116,7 +122,7 @@ contract StoreCoreGasTest is Test, GasReporter, StoreMock { function testSetAndGetStaticData() public { // Register table's schema - Schema schema = SchemaLib.encode(SchemaType.UINT8, SchemaType.UINT16, SchemaType.UINT8, SchemaType.UINT16); + Schema schema = SchemaEncodeHelper.encode(SchemaType.UINT8, SchemaType.UINT16, SchemaType.UINT8, SchemaType.UINT16); bytes32 table = keccak256("some.table"); StoreCore.registerSchema(table, schema, defaultKeySchema); @@ -137,7 +143,7 @@ contract StoreCoreGasTest is Test, GasReporter, StoreMock { function testSetAndGetStaticDataSpanningWords() public { // Register table's schema - Schema schema = SchemaLib.encode(SchemaType.UINT128, SchemaType.UINT256); + Schema schema = SchemaEncodeHelper.encode(SchemaType.UINT128, SchemaType.UINT256); bytes32 table = keccak256("some.table"); StoreCore.registerSchema(table, schema, defaultKeySchema); @@ -165,7 +171,7 @@ contract StoreCoreGasTest is Test, GasReporter, StoreMock { { // Register table's schema - Schema schema = SchemaLib.encode(SchemaType.UINT128, SchemaType.UINT32_ARRAY, SchemaType.UINT32_ARRAY); + Schema schema = SchemaEncodeHelper.encode(SchemaType.UINT128, SchemaType.UINT32_ARRAY, SchemaType.UINT32_ARRAY); StoreCore.registerSchema(table, schema, defaultKeySchema); } @@ -241,7 +247,7 @@ contract StoreCoreGasTest is Test, GasReporter, StoreMock { { // Register table's schema - Schema schema = SchemaLib.encode( + Schema schema = SchemaEncodeHelper.encode( SchemaType.UINT128, SchemaType.UINT256, SchemaType.UINT32_ARRAY, @@ -331,7 +337,7 @@ contract StoreCoreGasTest is Test, GasReporter, StoreMock { bytes32 table = keccak256("some.table"); // Register table's schema - Schema schema = SchemaLib.encode(SchemaType.UINT128, SchemaType.UINT32_ARRAY, SchemaType.UINT32_ARRAY); + Schema schema = SchemaEncodeHelper.encode(SchemaType.UINT128, SchemaType.UINT32_ARRAY, SchemaType.UINT32_ARRAY); StoreCore.registerSchema(table, schema, defaultKeySchema); bytes16 firstDataBytes = bytes16(0x0102030405060708090a0b0c0d0e0f10); @@ -387,7 +393,7 @@ contract StoreCoreGasTest is Test, GasReporter, StoreMock { { // Register table's schema - Schema schema = SchemaLib.encode(SchemaType.UINT256, SchemaType.UINT32_ARRAY, SchemaType.UINT32_ARRAY); + Schema schema = SchemaEncodeHelper.encode(SchemaType.UINT256, SchemaType.UINT32_ARRAY, SchemaType.UINT32_ARRAY); StoreCore.registerSchema(table, schema, defaultKeySchema); } @@ -461,7 +467,7 @@ contract StoreCoreGasTest is Test, GasReporter, StoreMock { { // Register table's schema - Schema schema = SchemaLib.encode(SchemaType.UINT256, SchemaType.UINT32_ARRAY, SchemaType.UINT64_ARRAY); + Schema schema = SchemaEncodeHelper.encode(SchemaType.UINT256, SchemaType.UINT32_ARRAY, SchemaType.UINT64_ARRAY); StoreCore.registerSchema(table, schema, defaultKeySchema); } @@ -535,7 +541,7 @@ contract StoreCoreGasTest is Test, GasReporter, StoreMock { function testAccessEmptyData() public { bytes32 table = keccak256("some.table"); - Schema schema = SchemaLib.encode(SchemaType.UINT32, SchemaType.UINT32_ARRAY); + Schema schema = SchemaEncodeHelper.encode(SchemaType.UINT32, SchemaType.UINT32_ARRAY); StoreCore.registerSchema(table, schema, defaultKeySchema); @@ -570,7 +576,7 @@ contract StoreCoreGasTest is Test, GasReporter, StoreMock { key[0] = keccak256("some key"); // Register table's schema - Schema schema = SchemaLib.encode(SchemaType.UINT128); + Schema schema = SchemaEncodeHelper.encode(SchemaType.UINT128); StoreCore.registerSchema(table, schema, defaultKeySchema); // Create subscriber @@ -603,7 +609,7 @@ contract StoreCoreGasTest is Test, GasReporter, StoreMock { key[0] = keccak256("some key"); // Register table's schema - Schema schema = SchemaLib.encode(SchemaType.UINT128, SchemaType.UINT32_ARRAY); + Schema schema = SchemaEncodeHelper.encode(SchemaType.UINT128, SchemaType.UINT32_ARRAY); StoreCore.registerSchema(table, schema, defaultKeySchema); // Create subscriber diff --git a/packages/world/abi/Schema.sol/SchemaInstance.abi.json b/packages/world/abi/Schema.sol/SchemaInstance.abi.json new file mode 100644 index 0000000000..0637a088a0 --- /dev/null +++ b/packages/world/abi/Schema.sol/SchemaInstance.abi.json @@ -0,0 +1 @@ +[] \ No newline at end of file diff --git a/packages/world/abi/src/Schema.sol/SchemaInstance.abi.json b/packages/world/abi/src/Schema.sol/SchemaInstance.abi.json new file mode 100644 index 0000000000..0637a088a0 --- /dev/null +++ b/packages/world/abi/src/Schema.sol/SchemaInstance.abi.json @@ -0,0 +1 @@ +[] \ No newline at end of file diff --git a/packages/world/test/KeysInTableModule.t.sol b/packages/world/test/KeysInTableModule.t.sol index 4656a03b6e..dd6b057bc5 100644 --- a/packages/world/test/KeysInTableModule.t.sol +++ b/packages/world/test/KeysInTableModule.t.sol @@ -5,6 +5,7 @@ import { Test } from "forge-std/Test.sol"; import { GasReporter } from "@latticexyz/gas-report/src/GasReporter.sol"; import { Schema, SchemaLib } from "@latticexyz/store/src/Schema.sol"; +import { SchemaEncodeHelper } from "@latticexyz/store/test/SchemaEncodeHelper.sol"; import { SchemaType } from "@latticexyz/schema-type/src/solidity/SchemaType.sol"; import { World } from "../src/World.sol"; @@ -45,9 +46,9 @@ contract KeysInTableModuleTest is Test, GasReporter { uint256 val2 = 42; function setUp() public { - tableSchema = SchemaLib.encode(SchemaType.UINT256); - tableKeySchema = SchemaLib.encode(SchemaType.BYTES32); - compositeKeySchema = SchemaLib.encode(SchemaType.BYTES32, SchemaType.BYTES32, SchemaType.BYTES32); + tableSchema = SchemaEncodeHelper.encode(SchemaType.UINT256); + tableKeySchema = SchemaEncodeHelper.encode(SchemaType.BYTES32); + compositeKeySchema = SchemaEncodeHelper.encode(SchemaType.BYTES32, SchemaType.BYTES32, SchemaType.BYTES32); SchemaType[] memory _schema = new SchemaType[](0); singletonKeySchema = SchemaLib.encode(_schema); diff --git a/packages/world/test/KeysWithValueModule.t.sol b/packages/world/test/KeysWithValueModule.t.sol index 4cca1745e2..0a063c41ac 100644 --- a/packages/world/test/KeysWithValueModule.t.sol +++ b/packages/world/test/KeysWithValueModule.t.sol @@ -4,7 +4,8 @@ pragma solidity >=0.8.0; import { Test } from "forge-std/Test.sol"; import { GasReporter } from "@latticexyz/gas-report/src/GasReporter.sol"; -import { Schema, SchemaLib } from "@latticexyz/store/src/Schema.sol"; +import { Schema } from "@latticexyz/store/src/Schema.sol"; +import { SchemaEncodeHelper } from "@latticexyz/store/test/SchemaEncodeHelper.sol"; import { SchemaType } from "@latticexyz/schema-type/src/solidity/SchemaType.sol"; import { World } from "../src/World.sol"; @@ -37,8 +38,8 @@ contract KeysWithValueModuleTest is Test, GasReporter { bytes32 targetTableId; function setUp() public { - sourceTableSchema = SchemaLib.encode(SchemaType.UINT256); - sourceTableKeySchema = SchemaLib.encode(SchemaType.BYTES32); + sourceTableSchema = SchemaEncodeHelper.encode(SchemaType.UINT256); + sourceTableKeySchema = SchemaEncodeHelper.encode(SchemaType.BYTES32); world = IBaseWorld(address(new World())); world.installRootModule(new CoreModule(), new bytes(0)); keyTuple1 = new bytes32[](1); diff --git a/packages/world/test/SnapSyncModule.t.sol b/packages/world/test/SnapSyncModule.t.sol index 0ad935c0ce..4af7739520 100644 --- a/packages/world/test/SnapSyncModule.t.sol +++ b/packages/world/test/SnapSyncModule.t.sol @@ -5,6 +5,7 @@ import { Test } from "forge-std/Test.sol"; import { GasReporter } from "@latticexyz/gas-report/src/GasReporter.sol"; import { Schema, SchemaLib } from "@latticexyz/store/src/Schema.sol"; +import { SchemaEncodeHelper } from "@latticexyz/store/test/SchemaEncodeHelper.sol"; import { SchemaType } from "@latticexyz/schema-type/src/solidity/SchemaType.sol"; import { World } from "../src/World.sol"; @@ -56,9 +57,9 @@ contract SnapSyncModuleTest is Test, GasReporter { uint256 val2 = 42; function setUp() public { - tableSchema = SchemaLib.encode(SchemaType.UINT256); - tableKeySchema = SchemaLib.encode(SchemaType.BYTES32); - compositeKeySchema = SchemaLib.encode(SchemaType.BYTES32, SchemaType.BYTES32, SchemaType.BYTES32); + tableSchema = SchemaEncodeHelper.encode(SchemaType.UINT256); + tableKeySchema = SchemaEncodeHelper.encode(SchemaType.BYTES32); + compositeKeySchema = SchemaEncodeHelper.encode(SchemaType.BYTES32, SchemaType.BYTES32, SchemaType.BYTES32); SchemaType[] memory _schema = new SchemaType[](0); singletonKeySchema = SchemaLib.encode(_schema); diff --git a/packages/world/test/World.t.sol b/packages/world/test/World.t.sol index d50f332e0a..c34aacb87f 100644 --- a/packages/world/test/World.t.sol +++ b/packages/world/test/World.t.sol @@ -9,7 +9,8 @@ import { SchemaType } from "@latticexyz/schema-type/src/solidity/SchemaType.sol" import { IStoreHook } from "@latticexyz/store/src/IStore.sol"; import { StoreCore, StoreCoreInternal } from "@latticexyz/store/src/StoreCore.sol"; import { StoreSwitch } from "@latticexyz/store/src/StoreSwitch.sol"; -import { Schema, SchemaLib } from "@latticexyz/store/src/Schema.sol"; +import { Schema } from "@latticexyz/store/src/Schema.sol"; +import { SchemaEncodeHelper } from "@latticexyz/store/test/SchemaEncodeHelper.sol"; import { StoreMetadataData, StoreMetadata } from "@latticexyz/store/src/codegen/Tables.sol"; import { EncodeArray } from "@latticexyz/store/src/tightcoder/EncodeArray.sol"; @@ -143,7 +144,7 @@ contract WorldTest is Test, GasReporter { event SystemHookCalled(bytes data); event WorldTestSystemLog(string log); - Schema defaultKeySchema = SchemaLib.encode(SchemaType.BYTES32); + Schema defaultKeySchema = SchemaEncodeHelper.encode(SchemaType.BYTES32); IBaseWorld world; bytes32 key; @@ -182,7 +183,10 @@ contract WorldTest is Test, GasReporter { bytes memory value = world.getRecord(StoreCoreInternal.SCHEMA_TABLE, schemaKey); assertEq( value, - abi.encodePacked(SchemaLib.encode(SchemaType.BYTES32, SchemaType.BYTES32), SchemaLib.encode(SchemaType.BYTES32)) + abi.encodePacked( + SchemaEncodeHelper.encode(SchemaType.BYTES32, SchemaType.BYTES32), + SchemaEncodeHelper.encode(SchemaType.BYTES32) + ) ); } @@ -227,7 +231,7 @@ contract WorldTest is Test, GasReporter { } function testRegisterTable() public { - Schema schema = SchemaLib.encode(SchemaType.BOOL, SchemaType.UINT256, SchemaType.STRING); + Schema schema = SchemaEncodeHelper.encode(SchemaType.BOOL, SchemaType.UINT256, SchemaType.STRING); bytes16 namespace = "testNamespace"; bytes16 table = "testTable"; @@ -261,7 +265,7 @@ contract WorldTest is Test, GasReporter { bytes16 name = "tableName"; bytes32 tableId = ResourceSelector.from(namespace, name); - Schema schema = SchemaLib.encode(SchemaType.UINT8, SchemaType.UINT8); + Schema schema = SchemaEncodeHelper.encode(SchemaType.UINT8, SchemaType.UINT8); string[] memory fieldNames = new string[](2); fieldNames[0] = "testField1"; fieldNames[1] = "testField2"; diff --git a/packages/world/test/WorldDynamicUpdate.t.sol b/packages/world/test/WorldDynamicUpdate.t.sol index 040ec307c1..dd1da325dc 100644 --- a/packages/world/test/WorldDynamicUpdate.t.sol +++ b/packages/world/test/WorldDynamicUpdate.t.sol @@ -9,7 +9,8 @@ import { SchemaType } from "@latticexyz/schema-type/src/solidity/SchemaType.sol" import { IStoreHook } from "@latticexyz/store/src/IStore.sol"; import { StoreCore } from "@latticexyz/store/src/StoreCore.sol"; import { StoreSwitch } from "@latticexyz/store/src/StoreSwitch.sol"; -import { Schema, SchemaLib } from "@latticexyz/store/src/Schema.sol"; +import { Schema } from "@latticexyz/store/src/Schema.sol"; +import { SchemaEncodeHelper } from "@latticexyz/store/test/SchemaEncodeHelper.sol"; import { EncodeArray } from "@latticexyz/store/src/tightcoder/EncodeArray.sol"; import { World } from "../src/World.sol"; @@ -30,7 +31,7 @@ contract UpdateInFieldTest is Test, GasReporter { event HookCalled(bytes data); event WorldTestSystemLog(string log); - Schema internal defaultKeySchema = SchemaLib.encode(SchemaType.BYTES32); + Schema internal defaultKeySchema = SchemaEncodeHelper.encode(SchemaType.BYTES32); IBaseWorld internal world; bytes32 internal key; diff --git a/packages/world/test/query.t.sol b/packages/world/test/query.t.sol index 27dd77c2b3..dd5a419fea 100644 --- a/packages/world/test/query.t.sol +++ b/packages/world/test/query.t.sol @@ -4,7 +4,8 @@ pragma solidity >=0.8.0; import { Test } from "forge-std/Test.sol"; import { GasReporter } from "@latticexyz/gas-report/src/GasReporter.sol"; -import { Schema, SchemaLib } from "@latticexyz/store/src/Schema.sol"; +import { Schema } from "@latticexyz/store/src/Schema.sol"; +import { SchemaEncodeHelper } from "@latticexyz/store/test/SchemaEncodeHelper.sol"; import { SchemaType } from "@latticexyz/schema-type/src/solidity/SchemaType.sol"; import { World } from "../src/World.sol"; @@ -43,8 +44,8 @@ contract QueryTest is Test, GasReporter { QueryFragment[] fragmentsHasNot; function setUp() public { - tableSchema = SchemaLib.encode(SchemaType.UINT256); - tableKeySchema = SchemaLib.encode(SchemaType.BYTES32); + tableSchema = SchemaEncodeHelper.encode(SchemaType.UINT256); + tableKeySchema = SchemaEncodeHelper.encode(SchemaType.BYTES32); world = IBaseWorld(address(new World())); world.installRootModule(new CoreModule(), new bytes(0));