From 4a7d282aa1f0ef23ff187971e3dc277857d849e0 Mon Sep 17 00:00:00 2001 From: alvrs Date: Tue, 15 Aug 2023 16:22:23 +0200 Subject: [PATCH] remove snap sync module --- docs/pages/world/modules.mdx | 34 +-- docs/pages/world/snap-sync.mdx | 144 ------------ .../minimal/packages/contracts/mud.config.ts | 9 - packages/cli/src/utils/deploy.ts | 7 - packages/network/src/index.ts | 1 - .../src/v2/snapSync/getSnapSyncRecords.ts | 48 ---- packages/network/src/v2/snapSync/index.ts | 1 - .../src/v2/snapSync/snapSyncSystemAbi.ts | 69 ------ packages/world/mud.config.ts | 2 - packages/world/package.json | 4 - .../src/modules/snapsync/SnapSyncModule.sol | 36 --- .../src/modules/snapsync/SnapSyncSystem.sol | 51 ---- .../world/src/modules/snapsync/SyncRecord.sol | 8 - .../world/src/modules/snapsync/constants.sol | 7 - packages/world/test/SnapSyncModule.t.sol | 222 ------------------ .../ts/plugins/snapsync/configExtensions.ts | 36 --- packages/world/ts/plugins/snapsync/index.ts | 7 - packages/world/ts/plugins/snapsync/plugin.ts | 7 - .../ts/plugins/snapsync/typeExtensions.ts | 12 - packages/world/tsup.config.ts | 8 +- 20 files changed, 2 insertions(+), 711 deletions(-) delete mode 100644 docs/pages/world/snap-sync.mdx delete mode 100644 packages/network/src/v2/snapSync/getSnapSyncRecords.ts delete mode 100644 packages/network/src/v2/snapSync/index.ts delete mode 100644 packages/network/src/v2/snapSync/snapSyncSystemAbi.ts delete mode 100644 packages/world/src/modules/snapsync/SnapSyncModule.sol delete mode 100644 packages/world/src/modules/snapsync/SnapSyncSystem.sol delete mode 100644 packages/world/src/modules/snapsync/SyncRecord.sol delete mode 100644 packages/world/src/modules/snapsync/constants.sol delete mode 100644 packages/world/test/SnapSyncModule.t.sol delete mode 100644 packages/world/ts/plugins/snapsync/configExtensions.ts delete mode 100644 packages/world/ts/plugins/snapsync/index.ts delete mode 100644 packages/world/ts/plugins/snapsync/plugin.ts delete mode 100644 packages/world/ts/plugins/snapsync/typeExtensions.ts diff --git a/docs/pages/world/modules.mdx b/docs/pages/world/modules.mdx index cb08ce3126..f587a5aaff 100644 --- a/docs/pages/world/modules.mdx +++ b/docs/pages/world/modules.mdx @@ -41,7 +41,7 @@ function spawnSoldier() public { ### Querying modules -The `KeysInTableModule` and `KeysWithValueModule` modules index information about tables on-chain. Their functionality is leveraged in `SnapSyncModule` and `query` to allow on-chain querying. +The `KeysInTableModule` and `KeysWithValueModule` modules index information about tables on-chain. Their functionality is leveraged in `query` to allow on-chain querying. #### **`KeysInTableModule`** @@ -111,38 +111,6 @@ bytes32[] memory keysWithValue = getKeysWithValue(world, OwnersId, Owners.encode Internally, it works by installing a [hook](/store/advanced-features#storage-hooks) that maintains an array of all keys in the table. -#### **`SnapSyncModule`** - -The [`SnapSyncModule`](https://github.com/latticexyz/mud/blob/main/packages/world/src/modules/snapsync/SnapSyncModule.sol) installs a [System](/world/world-101#systems) that returns all records in a given table, with offset-based pagination. It requires the `KeysInTable` module to be installed on each table, but the [`SnapSync` plugin](/world/snap-sync) does this automatically. - -`SnapSyncSystem` exposes two public `view` functions: - -- `getRecords(bytes32 tableId, uint256 limit, uint256 offset)` returns all keys in a table -- `getNumKeysInTable(bytes32 tableId)` returns the number of keys in a table - -Clients can use snap-sync to get all records on the World, then begin syncing regularly from the current block: - -```tsx -import { getSnapSyncRecords } from "@latticexyz/network"; -import { getTableIds } from "@latticexyz/common/deprecated"; - -... - -if (networkConfig.snapSync) { - const currentBlockNumber = await provider.getBlockNumber(); - const tableRecords = await getSnapSyncRecords( - networkConfig.worldAddress, - getTableIds(storeConfig), - currentBlockNumber, - signerOrProvider - ); - - result.startSync(tableRecords, currentBlockNumber); -} else { - result.startSync(); -} -``` - #### **`query`** `query` provides a simple API to get a list of keys matching certain specified criteria. It is not a standalone module, but requires `KeysInTable` and `KeysWithValue` to be installed. diff --git a/docs/pages/world/snap-sync.mdx b/docs/pages/world/snap-sync.mdx deleted file mode 100644 index f03b2e8382..0000000000 --- a/docs/pages/world/snap-sync.mdx +++ /dev/null @@ -1,144 +0,0 @@ -## Snap-sync - -The [`SnapSync`](https://github.com/latticexyz/mud/tree/main/packages/world/ts/plugins/snapsync) plugin allows clients to sync with the state of a World by fetching it directly from the blockchain. - -Without this plugin or [an Indexer](/indexer), clients can be slow to sync as they have to process all events since the World was deployed. - -Let's walk through an example of using snap-sync. - -#### Creating tables - -First, we define some tables. - -```tsx -import { mudConfig } from "@latticexyz/world/register"; - -export default mudConfig({ - tables: { - Karma: "int32", - Reputation: { - schema: { - fame: "uint8", - infamy: "uint8", - }, - }, - }, -}); -``` - -#### Adding the snap-sync plugin - -Next, add the `SnapSync` plugin. This will automatically install the necessary [smart contract modules](/world/modules#snapsyncmodule). - -```tsx -import { mudConfig } from "@latticexyz/world/register"; -import { resolveTableId } from "@latticexyz/config"; -/** - * Importing this enables "snap sync mode". - * It allows clients to sync the latest state of the world using view functions. - */ -import "@latticexyz/world/snapsync"; - -export default mudConfig({ - snapSync: true, - tables: { - Karma: "int32", - Reputation: { - schema: { - fame: "uint8", - infamy: "uint8", - }, - }, - }, -}); -``` - -#### Using snap-sync in the client - -Now that the modules are installed on-chain, we need to enable snap-sync in the client. This depends on your exact client setup, but it comes by default with the MUD templates from [Quick start](/quick-start#project-setup). - -##### With a template - -In the standard templates, you can enable snap-sync on a given client by appending `?snapSync=true` to the url. Alternatively you can hardcode it to be enabled by default in `getNetworkConfig`: - -```tsx -export async function getNetworkConfig(): Promise { - - ... - - return { - ... - snapSync: params.get("snapSync") === "true", - }; -} -``` - -##### Manually - -When using [`std-client`](https://github.com/latticexyz/mud/tree/main/packages/std-client), we usually begin syncing by calling the `startSync()` function from `setupMUDV2Network`: - -```tsx -// Start syncing from the initial block set in the MUD config -result.startSync(); -``` - -Without any arguments, `startSync()` uses the initial block number in the MUD config, which is usually the block that the World was deployed. The means the initial sync is slow as it processes every event since deployment. - -However, `startSync()` takes two arguments: - -- `initialRecords` is an initial set of records for each table. -- `initialBlockNumber` is the block number to start syncing from. - -Using snap-sync, we can fetch the current state directly from the World contract, "skipping" the initial sync phase. The initial records are fetched at the current block number, for every table, using the snap-sync `view`. - -In the client, we fetch all the records at the current block number then begin syncing: - -```tsx -import { getSnapSyncRecords } from "@latticexyz/network"; -import { getTableIds } from "@latticexyz/common/deprecated"; - -... - -// Fetch the ID's of all tables in our config -const tableIds = getTableIds(storeConfig); - -// Fetch the current block number -const currentBlockNumber = await provider.getBlockNumber(); - -// Fetch all records at the current block for all tables -const tableRecords = await getSnapSyncRecords( - networkConfig.worldAddress, - tableIds, - currentBlockNumber, - signerOrProvider -); - -// Start syncing with these records at the current block -result.startSync(tableRecords, currentBlockNumber); -``` - -If both the Indexer and snap-sync are enabled, the arguments that are passed into `startSync()` take precedence. In this case the client would perform the initial "catch up" with snap-sync then stream from the Indexer. - -To allow users to toggle snap-sync, add a flag to the `NetworkConfig` type: - -```tsx -type NetworkConfig = SetupContractConfig & { - privateKey: string; - faucetServiceUrl?: string; - snapSync?: boolean; -}; -``` - -To set a default value for the flag, edit `getNetworkConfig()`: - -```tsx -export async function getNetworkConfig(): Promise { - - ... - - return { - ... - snapSync: params.get("snapSync") === "true", - }; -} -``` diff --git a/examples/minimal/packages/contracts/mud.config.ts b/examples/minimal/packages/contracts/mud.config.ts index af345f9eb1..70d98bb5d6 100644 --- a/examples/minimal/packages/contracts/mud.config.ts +++ b/examples/minimal/packages/contracts/mud.config.ts @@ -1,17 +1,8 @@ import { mudConfig } from "@latticexyz/world/register"; -/** - * Importing this enables "snap sync mode". - * It allows clients to sync the latest state of the world using view functions. - * This is a simple way to quickly sync without the use of an external indexer. - * This could lead to expensive queries on live RPCs if the world is large, - * so we suggest using MODE for production deployments. - */ -import "@latticexyz/world/snapsync"; import { resolveTableId } from "@latticexyz/config"; export default mudConfig({ - snapSync: true, systems: { IncrementSystem: { name: "increment", diff --git a/packages/cli/src/utils/deploy.ts b/packages/cli/src/utils/deploy.ts index 345356bb67..f50a15dc30 100644 --- a/packages/cli/src/utils/deploy.ts +++ b/packages/cli/src/utils/deploy.ts @@ -18,7 +18,6 @@ import CoreModuleData from "@latticexyz/world/abi/CoreModule.sol/CoreModule.json import KeysWithValueModuleData from "@latticexyz/world/abi/KeysWithValueModule.sol/KeysWithValueModule.json" assert { type: "json" }; import KeysInTableModuleData from "@latticexyz/world/abi/KeysInTableModule.sol/KeysInTableModule.json" assert { type: "json" }; import UniqueEntityModuleData from "@latticexyz/world/abi/UniqueEntityModule.sol/UniqueEntityModule.json" assert { type: "json" }; -import SnapSyncModuleData from "@latticexyz/world/abi/SnapSyncModule.sol/SnapSyncModule.json" assert { type: "json" }; export interface DeployConfig { profile?: string; @@ -113,12 +112,6 @@ export async function deploy( disableTxWait, "UniqueEntityModule" ), - SnapSyncModule: deployContract( - SnapSyncModuleData.abi, - SnapSyncModuleData.bytecode, - disableTxWait, - "SnapSyncModule" - ), }; // Deploy user Modules diff --git a/packages/network/src/index.ts b/packages/network/src/index.ts index 7038444a38..cb1959abd8 100644 --- a/packages/network/src/index.ts +++ b/packages/network/src/index.ts @@ -17,5 +17,4 @@ export * from "./createBlockNumberStream"; export * from "./createFaucetService"; export * from "./utils"; export * from "./createFastTxExecutor"; -export * from "./v2/snapSync"; export { keyTupleToEntityID } from "./v2/keyTupleToEntityID"; diff --git a/packages/network/src/v2/snapSync/getSnapSyncRecords.ts b/packages/network/src/v2/snapSync/getSnapSyncRecords.ts deleted file mode 100644 index 165f6774f0..0000000000 --- a/packages/network/src/v2/snapSync/getSnapSyncRecords.ts +++ /dev/null @@ -1,48 +0,0 @@ -import { TableId } from "@latticexyz/common/deprecated"; -import { Hex } from "viem"; -import snapSyncSystemAbi from "./snapSyncSystemAbi"; -import { Contract, Signer, providers } from "ethers"; -import { RawTableRecord } from "../../types"; - -export async function getSnapSyncRecords( - worldAddress: string, - tables: TableId[], - currentBlockNumber: number, - signerOrProvider: Signer | providers.JsonRpcProvider -) { - const snapSyncContract = new Contract(worldAddress, snapSyncSystemAbi, signerOrProvider); - - const chunkSize = 100; - const tableIds = tables.map((table) => table.toHex()); - const tableRecords = [] as RawTableRecord[]; - for (const tableId of tableIds) { - const numKeys = ( - await snapSyncContract.callStatic["snapSync_system_getNumKeysInTable"](tableId, { - blockTag: currentBlockNumber, - }) - ).toNumber(); - if (numKeys === 0) continue; - - let remainingKeys = numKeys; - const numChunks = Math.ceil(numKeys / chunkSize); - for (let i = 0; i < numChunks; i++) { - const limit = Math.min(remainingKeys, chunkSize); - const offset = i * chunkSize; - remainingKeys -= limit; - - const records = await snapSyncContract.callStatic["snapSync_system_getRecords"](tableId, limit, offset, { - blockTag: currentBlockNumber, - }); - const transformedRecords = records.map((record: [string, string[], string]) => { - return { - tableId: TableId.fromHex(record[0] as Hex), - keyTuple: record[1], - value: record[2], - }; - }); - tableRecords.push(...transformedRecords); - } - } - - return tableRecords; -} diff --git a/packages/network/src/v2/snapSync/index.ts b/packages/network/src/v2/snapSync/index.ts deleted file mode 100644 index 02114ca54b..0000000000 --- a/packages/network/src/v2/snapSync/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from "./getSnapSyncRecords"; diff --git a/packages/network/src/v2/snapSync/snapSyncSystemAbi.ts b/packages/network/src/v2/snapSync/snapSyncSystemAbi.ts deleted file mode 100644 index 8dd4d712b0..0000000000 --- a/packages/network/src/v2/snapSync/snapSyncSystemAbi.ts +++ /dev/null @@ -1,69 +0,0 @@ -// TODO: autogenerate this and import from the SnapSync module -// (see https://github.com/latticexyz/mud/issues/836) -export default [ - { - inputs: [ - { - internalType: "bytes32", - name: "tableId", - type: "bytes32", - }, - ], - name: "snapSync_system_getNumKeysInTable", - outputs: [ - { - internalType: "uint256", - name: "", - type: "uint256", - }, - ], - stateMutability: "view", - type: "function", - }, - { - inputs: [ - { - internalType: "bytes32", - name: "tableId", - type: "bytes32", - }, - { - internalType: "uint256", - name: "limit", - type: "uint256", - }, - { - internalType: "uint256", - name: "offset", - type: "uint256", - }, - ], - name: "snapSync_system_getRecords", - outputs: [ - { - components: [ - { - internalType: "bytes32", - name: "tableId", - type: "bytes32", - }, - { - internalType: "bytes32[]", - name: "keyTuple", - type: "bytes32[]", - }, - { - internalType: "bytes", - name: "value", - type: "bytes", - }, - ], - internalType: "struct SyncRecord[]", - name: "", - type: "tuple[]", - }, - ], - stateMutability: "view", - type: "function", - }, -]; diff --git a/packages/world/mud.config.ts b/packages/world/mud.config.ts index 0402df962f..f000ace062 100644 --- a/packages/world/mud.config.ts +++ b/packages/world/mud.config.ts @@ -162,8 +162,6 @@ export default mudConfig({ // (see https://github.com/latticexyz/mud/pull/584) "UniqueEntitySystem", - "SnapSyncSystem", - // Worldgen currently does not support systems inheriting logic // from other contracts, so all parts of CoreSystem are named // System too to be included in the IBaseWorld interface. diff --git a/packages/world/package.json b/packages/world/package.json index c76e02063e..11611914e8 100644 --- a/packages/world/package.json +++ b/packages/world/package.json @@ -15,7 +15,6 @@ "./node": "./dist/ts/node/index.js", "./abi/*": "./abi/*", "./types/*": "./types/*", - "./snapsync": "./dist/ts/plugins/snapsync/index.js", "./*": "./dist/*" }, "typesVersions": { @@ -28,9 +27,6 @@ ], "node": [ "./ts/node/index.ts" - ], - "snapsync": [ - "./ts/plugins/snapsync/index.ts" ] } }, diff --git a/packages/world/src/modules/snapsync/SnapSyncModule.sol b/packages/world/src/modules/snapsync/SnapSyncModule.sol deleted file mode 100644 index b35ef982c8..0000000000 --- a/packages/world/src/modules/snapsync/SnapSyncModule.sol +++ /dev/null @@ -1,36 +0,0 @@ -// SPDX-License-Identifier: MIT -pragma solidity >=0.8.0; - -import { IBaseWorld } from "../../interfaces/IBaseWorld.sol"; -import { IModule } from "../../interfaces/IModule.sol"; - -import { WorldContext } from "../../WorldContext.sol"; - -import { SnapSyncSystem } from "./SnapSyncSystem.sol"; - -import { NAMESPACE, MODULE_NAME, SYSTEM_NAME, TABLE_NAME } from "./constants.sol"; - -/** - * NOTICE: Requires all tables in the world to use the KeysInTable module. - * This module registers a system that allows clients to load a snapshot of the World state - * by using view functions. - */ -contract SnapSyncModule is IModule, WorldContext { - // Since the SnapSyncSystem only exists once per World and writes to - // known tables, we can deploy it once and register it in multiple Worlds. - SnapSyncSystem private immutable snapSyncSystem = new SnapSyncSystem(); - - function getName() public pure returns (bytes16) { - return MODULE_NAME; - } - - function install(bytes memory) public { - IBaseWorld world = IBaseWorld(_world()); - - // Register system - world.registerSystem(NAMESPACE, SYSTEM_NAME, snapSyncSystem, true); - // Register system's functions - world.registerFunctionSelector(NAMESPACE, SYSTEM_NAME, "getRecords", "(bytes32,uint256,uint256)"); - world.registerFunctionSelector(NAMESPACE, SYSTEM_NAME, "getNumKeysInTable", "(bytes32)"); - } -} diff --git a/packages/world/src/modules/snapsync/SnapSyncSystem.sol b/packages/world/src/modules/snapsync/SnapSyncSystem.sol deleted file mode 100644 index 4657e4060e..0000000000 --- a/packages/world/src/modules/snapsync/SnapSyncSystem.sol +++ /dev/null @@ -1,51 +0,0 @@ -// SPDX-License-Identifier: MIT -pragma solidity >=0.8.0; -import { System } from "../../System.sol"; -import { StoreSwitch } from "@latticexyz/store/src/StoreSwitch.sol"; -import { Schema } from "@latticexyz/store/src/Schema.sol"; - -import { getKeysInTable } from "../keysintable/getKeysInTable.sol"; -import { KeysInTable, KeysInTableTableId } from "../keysintable/tables/KeysInTable.sol"; -import { SyncRecord } from "./SyncRecord.sol"; - -function keyToTuple(bytes32 key) pure returns (bytes32[] memory keyTuple) {} - -contract SnapSyncSystem is System { - function getRecords( - bytes32 tableId, - uint256 limit, - uint256 offset - ) public view virtual returns (SyncRecord[] memory records) { - records = new SyncRecord[](limit); - - Schema schema = StoreSwitch.getKeySchema(tableId); - uint256 numFields = schema.numFields(); - - for (uint256 i = offset; i < limit + offset; i++) { - bytes32[] memory keyTuple = new bytes32[](numFields); - - if (numFields > 0) { - keyTuple[0] = KeysInTable.getItemKeys0(tableId, i); - if (numFields > 1) { - keyTuple[1] = KeysInTable.getItemKeys1(tableId, i); - if (numFields > 2) { - keyTuple[2] = KeysInTable.getItemKeys2(tableId, i); - if (numFields > 3) { - keyTuple[3] = KeysInTable.getItemKeys3(tableId, i); - if (numFields > 4) { - keyTuple[4] = KeysInTable.getItemKeys4(tableId, i); - } - } - } - } - } - - bytes memory value = StoreSwitch.getRecord(tableId, keyTuple); - records[i] = SyncRecord({ tableId: tableId, keyTuple: keyTuple, value: value }); - } - } - - function getNumKeysInTable(bytes32 tableId) public view virtual returns (uint256) { - return KeysInTable.lengthKeys0(tableId); - } -} diff --git a/packages/world/src/modules/snapsync/SyncRecord.sol b/packages/world/src/modules/snapsync/SyncRecord.sol deleted file mode 100644 index 7af8963611..0000000000 --- a/packages/world/src/modules/snapsync/SyncRecord.sol +++ /dev/null @@ -1,8 +0,0 @@ -// SPDX-License-Identifier: MIT -pragma solidity >=0.8.0; - -struct SyncRecord { - bytes32 tableId; - bytes32[] keyTuple; - bytes value; -} diff --git a/packages/world/src/modules/snapsync/constants.sol b/packages/world/src/modules/snapsync/constants.sol deleted file mode 100644 index ef743f0152..0000000000 --- a/packages/world/src/modules/snapsync/constants.sol +++ /dev/null @@ -1,7 +0,0 @@ -// SPDX-License-Identifier: MIT -pragma solidity >=0.8.0; - -bytes16 constant NAMESPACE = bytes16("snapSync"); -bytes16 constant MODULE_NAME = bytes16("snapSync.m"); -bytes16 constant SYSTEM_NAME = bytes16("system"); -bytes16 constant TABLE_NAME = bytes16("table"); diff --git a/packages/world/test/SnapSyncModule.t.sol b/packages/world/test/SnapSyncModule.t.sol deleted file mode 100644 index 4af7739520..0000000000 --- a/packages/world/test/SnapSyncModule.t.sol +++ /dev/null @@ -1,222 +0,0 @@ -// SPDX-License-Identifier: MIT -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 { SchemaEncodeHelper } from "@latticexyz/store/test/SchemaEncodeHelper.sol"; -import { SchemaType } from "@latticexyz/schema-type/src/solidity/SchemaType.sol"; - -import { World } from "../src/World.sol"; -import { IBaseWorld } from "../src/interfaces/IBaseWorld.sol"; -import { ResourceSelector } from "../src/ResourceSelector.sol"; -import { ROOT_NAMESPACE } from "../src/constants.sol"; - -import { CoreModule } from "../src/modules/core/CoreModule.sol"; -import { KeysInTableModule } from "../src/modules/keysintable/KeysInTableModule.sol"; -import { SnapSyncModule } from "../src/modules/snapsync/SnapSyncModule.sol"; -import { SyncRecord } from "../src/modules/snapsync/SyncRecord.sol"; -import { getKeysInTable } from "../src/modules/keysintable/getKeysInTable.sol"; -import { hasKey } from "../src/modules/keysintable/hasKey.sol"; - -interface ISnapSyncSystem { - function snapSync_system_getRecords( - bytes32 tableId, - uint256 limit, - uint256 offset - ) external view returns (SyncRecord[] memory records); - - function snapSync_system_getNumKeysInTable(bytes32 tableId) external view returns (uint256); -} - -contract SnapSyncModuleTest is Test, GasReporter { - using ResourceSelector for bytes32; - IBaseWorld world; - KeysInTableModule keysInTableModule = new KeysInTableModule(); // Modules can be deployed once and installed multiple times - SnapSyncModule snapSyncModule = new SnapSyncModule(); // Modules can be deployed once and installed multiple times - - bytes16 namespace = ROOT_NAMESPACE; - bytes16 name = bytes16("source"); - bytes16 singletonName = bytes16("singleton"); - bytes16 compositeName = bytes16("composite"); - bytes32 key1 = keccak256("test"); - bytes32[] keyTuple1; - bytes32 key2 = keccak256("test2"); - bytes32[] keyTuple2; - - Schema tableSchema; - Schema tableKeySchema; - Schema singletonKeySchema; - Schema compositeKeySchema; - bytes32 tableId; - bytes32 singletonTableId; - bytes32 compositeTableId; - - uint256 val1 = 123; - uint256 val2 = 42; - - function setUp() public { - 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); - - world = IBaseWorld(address(new World())); - world.installRootModule(new CoreModule(), new bytes(0)); - keyTuple1 = new bytes32[](1); - keyTuple1[0] = key1; - keyTuple2 = new bytes32[](1); - keyTuple2[0] = key2; - } - - function _installModules() internal { - // Register source table - tableId = world.registerTable(namespace, name, tableSchema, tableKeySchema); - singletonTableId = world.registerTable(namespace, singletonName, tableSchema, singletonKeySchema); - compositeTableId = world.registerTable(namespace, compositeName, tableSchema, compositeKeySchema); - - world.installRootModule(keysInTableModule, abi.encode(tableId)); - world.installRootModule(keysInTableModule, abi.encode(singletonTableId)); - world.installRootModule(keysInTableModule, abi.encode(compositeTableId)); - world.installRootModule(snapSyncModule, ""); - } - - function testSnapSync(uint256 value1, uint256 value2) public { - _installModules(); - - // Set a value in the source table - world.setRecord(namespace, name, keyTuple1, abi.encodePacked(value1)); - - uint256 limit = ISnapSyncSystem(address(world)).snapSync_system_getNumKeysInTable(tableId); - - startGasReport("Call snap sync on a table with 1 record"); - SyncRecord[] memory records = ISnapSyncSystem(address(world)).snapSync_system_getRecords(tableId, limit, 0); - endGasReport(); - - // Assert that the list is correct - assertEq(records.length, 1); - assertEq(records[0].keyTuple[0], key1); - assertEq(records[0].value, abi.encodePacked(value1)); - - // Set another key with a different value - world.setRecord(namespace, name, keyTuple2, abi.encodePacked(value2)); - - limit = ISnapSyncSystem(address(world)).snapSync_system_getNumKeysInTable(tableId); - - startGasReport("Call snap sync on a table with 2 records"); - records = ISnapSyncSystem(address(world)).snapSync_system_getRecords(tableId, limit, 0); - endGasReport(); - - // Assert that the list is correct - assertEq(records.length, 2); - assertEq(records[0].keyTuple[0], key1); - assertEq(records[0].value, abi.encodePacked(value1)); - assertEq(records[1].keyTuple[0], key2); - assertEq(records[1].value, abi.encodePacked(value2)); - } - - function testSnapSyncGas() public { - testSnapSync(val1, val2); - } - - function testSnapSyncComposite(uint256 value1, uint256 value2) public { - _installModules(); - - bytes32[] memory keyTupleA = new bytes32[](3); - keyTupleA[0] = "A1"; - keyTupleA[1] = "A2"; - keyTupleA[2] = "A3"; - bytes32[] memory keyTupleB = new bytes32[](3); - keyTupleB[0] = "B1"; - keyTupleB[1] = "B2"; - keyTupleB[2] = "B3"; - - ISnapSyncSystem syncSystem = ISnapSyncSystem(address(world)); - - // Set a value in the source table - world.setRecord(namespace, compositeName, keyTupleA, abi.encodePacked(value1)); - - uint256 limit = syncSystem.snapSync_system_getNumKeysInTable(compositeTableId); - - startGasReport("Call snap sync on a table with 1 record"); - SyncRecord[] memory records = syncSystem.snapSync_system_getRecords(compositeTableId, limit, 0); - endGasReport(); - - // Assert that the list is correct - assertEq(records.length, 1); - for (uint256 i; i < 2; i++) { - assertEq(records[0].keyTuple[i], keyTupleA[i]); - } - assertEq(records[0].value, abi.encodePacked(value1)); - - // Set another key with a different value - world.setRecord(namespace, compositeName, keyTupleB, abi.encodePacked(value2)); - - limit = syncSystem.snapSync_system_getNumKeysInTable(compositeTableId); - - startGasReport("Call snap sync on a table with 2 records"); - records = syncSystem.snapSync_system_getRecords(compositeTableId, limit, 0); - endGasReport(); - - // Assert that the list is correct - assertEq(records.length, 2); - for (uint256 i; i < 3; i++) { - assertEq(records[0].keyTuple[i], keyTupleA[i]); - assertEq(records[1].keyTuple[i], keyTupleB[i]); - } - assertEq(records[0].value, abi.encodePacked(value1)); - assertEq(records[1].value, abi.encodePacked(value2)); - } - - function testSnapSyncCompositeSameKey(uint256 value1, uint256 value2) public { - _installModules(); - - bytes32[] memory keyTupleA = new bytes32[](3); - keyTupleA[0] = "KEY"; - keyTupleA[1] = "A2"; - keyTupleA[2] = "A3"; - bytes32[] memory keyTupleB = new bytes32[](3); - keyTupleB[0] = "KEY"; - keyTupleB[1] = "B2"; - keyTupleB[2] = "B3"; - - // Set a value in the source table - world.setRecord(namespace, compositeName, keyTupleA, abi.encodePacked(value1)); - - ISnapSyncSystem syncSystem = ISnapSyncSystem(address(world)); - - uint256 limit = syncSystem.snapSync_system_getNumKeysInTable(compositeTableId); - - startGasReport("Call snap sync on a table with 1 record"); - SyncRecord[] memory records = syncSystem.snapSync_system_getRecords(compositeTableId, limit, 0); - endGasReport(); - - // Assert that the list is correct - assertEq(records.length, 1); - for (uint256 i; i < 2; i++) { - assertEq(records[0].keyTuple[i], keyTupleA[i]); - } - assertEq(records[0].value, abi.encodePacked(value1)); - - // Set another key with a different value - world.setRecord(namespace, compositeName, keyTupleB, abi.encodePacked(value2)); - - limit = syncSystem.snapSync_system_getNumKeysInTable(compositeTableId); - - startGasReport("Call snap sync on a table with 2 records"); - records = syncSystem.snapSync_system_getRecords(compositeTableId, limit, 0); - endGasReport(); - - // Assert that the list is correct - assertEq(records.length, 2); - for (uint256 i; i < 3; i++) { - assertEq(records[0].keyTuple[i], keyTupleA[i]); - assertEq(records[1].keyTuple[i], keyTupleB[i]); - } - assertEq(records[0].value, abi.encodePacked(value1)); - assertEq(records[1].value, abi.encodePacked(value2)); - } -} diff --git a/packages/world/ts/plugins/snapsync/configExtensions.ts b/packages/world/ts/plugins/snapsync/configExtensions.ts deleted file mode 100644 index 58234eac3b..0000000000 --- a/packages/world/ts/plugins/snapsync/configExtensions.ts +++ /dev/null @@ -1,36 +0,0 @@ -import { extendMUDCoreConfig, resolveTableId } from "@latticexyz/config"; -import { zPluginStoreConfig } from "@latticexyz/store"; -import { zSnapSyncPluginConfig } from "./plugin"; -import { zPluginWorldConfig } from "../../library"; - -extendMUDCoreConfig((config) => { - const modifiedConfig = { ...config } as Record; - const snapSyncConfig = zSnapSyncPluginConfig.parse(config); - - if (snapSyncConfig.snapSync) { - const worldConfig = zPluginWorldConfig.parse(config); - const storeConfig = zPluginStoreConfig.parse(config); - const tableNames = Object.entries(storeConfig.tables) - .filter(([_, t]) => !t.ephemeral) - .map(([name, _]) => name); - const newModules = tableNames.map((tableName) => { - return { - name: "KeysInTableModule", - root: true, - args: [resolveTableId(tableName)], - }; - }); - - modifiedConfig.modules = [ - ...worldConfig.modules, - { - name: "SnapSyncModule", - root: true, - args: [], - }, - ...newModules, - ]; - } - - return modifiedConfig; -}); diff --git a/packages/world/ts/plugins/snapsync/index.ts b/packages/world/ts/plugins/snapsync/index.ts deleted file mode 100644 index 74d508cd4f..0000000000 --- a/packages/world/ts/plugins/snapsync/index.ts +++ /dev/null @@ -1,7 +0,0 @@ -/** - * Importing this file automatically adds the required MUD modules to make - * snap sync work. - */ - -import "./configExtensions"; -import "./typeExtensions"; diff --git a/packages/world/ts/plugins/snapsync/plugin.ts b/packages/world/ts/plugins/snapsync/plugin.ts deleted file mode 100644 index d0131d1517..0000000000 --- a/packages/world/ts/plugins/snapsync/plugin.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { z } from "zod"; - -export const zSnapSyncPluginConfig = z - .object({ - snapSync: z.boolean(), - }) - .catchall(z.any()); diff --git a/packages/world/ts/plugins/snapsync/typeExtensions.ts b/packages/world/ts/plugins/snapsync/typeExtensions.ts deleted file mode 100644 index 94dfc4d5ae..0000000000 --- a/packages/world/ts/plugins/snapsync/typeExtensions.ts +++ /dev/null @@ -1,12 +0,0 @@ -import "@latticexyz/store/register"; - -interface SnapSyncConfig { - snapSync: boolean; -} - -declare module "@latticexyz/config" { - // eslint-disable-next-line @typescript-eslint/no-empty-interface - export interface MUDCoreUserConfig extends SnapSyncConfig {} - // eslint-disable-next-line @typescript-eslint/no-empty-interface - export interface MUDCoreConfig extends SnapSyncConfig {} -} diff --git a/packages/world/tsup.config.ts b/packages/world/tsup.config.ts index ca5e11ac7f..8217d6fa02 100644 --- a/packages/world/tsup.config.ts +++ b/packages/world/tsup.config.ts @@ -1,13 +1,7 @@ import { defineConfig } from "tsup"; export default defineConfig({ - entry: [ - "mud.config.ts", - "ts/library/index.ts", - "ts/register/index.ts", - "ts/node/index.ts", - "ts/plugins/snapsync/index.ts", - ], + entry: ["mud.config.ts", "ts/library/index.ts", "ts/register/index.ts", "ts/node/index.ts"], target: "esnext", format: ["esm"], dts: false,