Skip to content

Commit

Permalink
fix(store-sync): create table registration logs from indexer records (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
holic authored Nov 12, 2023
1 parent 369ef41 commit 712866f
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 41 deletions.
5 changes: 5 additions & 0 deletions .changeset/khaki-months-add.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@latticexyz/store-sync": patch
---

`createStoreSync` now correctly creates table registration logs from indexer records.
4 changes: 4 additions & 0 deletions packages/store-sync/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ export const storeTables = resolveConfig(storeConfig).tables;
/** @internal Temporary workaround until we redo our config parsing and can pull this directly from the config (https://github.com/latticexyz/mud/issues/1668) */
export const worldTables = resolveConfig(worldConfig).tables;

export const internalTableIds = [...Object.values(storeTables), ...Object.values(worldTables)].map(
(table) => table.tableId
);

export type ChainId = number;
export type WorldId = `${ChainId}:${Address}`;

Expand Down
35 changes: 20 additions & 15 deletions packages/store-sync/src/createStoreSync.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { StoreConfig, storeEventsAbi } from "@latticexyz/store";
import { Hex, TransactionReceiptNotFoundError } from "viem";
import { Hex, TransactionReceiptNotFoundError, encodeAbiParameters, parseAbiParameters } from "viem";
import {
StorageAdapter,
StorageAdapterBlock,
Expand All @@ -8,6 +8,8 @@ import {
SyncOptions,
SyncResult,
TableWithRecords,
internalTableIds,
storeTables,
} from "./common";
import { createBlockStream, blockRangeToLogs, groupLogsByBlockNumber } from "@latticexyz/block-logs-stream";
import {
Expand All @@ -33,7 +35,7 @@ import { createIndexerClient } from "./trpc-indexer";
import { SyncStep } from "./SyncStep";
import { chunk, isDefined } from "@latticexyz/common/utils";
import { encodeKey, encodeValueArgs } from "@latticexyz/protocol-parser";
import { internalTableIds } from "./internalTableIds";
import { tableToLog } from "./tableToLog";

const debug = parentDebug.extend("createStoreSync");

Expand Down Expand Up @@ -140,19 +142,22 @@ export async function createStoreSync<TConfig extends StoreConfig = StoreConfig>
message: "Hydrating from snapshot",
});

const logs: StorageAdapterLog[] = tables.flatMap((table) =>
table.records.map(
(record): StorageAdapterLog => ({
eventName: "Store_SetRecord",
address: table.address,
args: {
tableId: table.tableId,
keyTuple: encodeKey(table.keySchema, record.key),
...encodeValueArgs(table.valueSchema, record.value),
},
})
)
);
const logs: StorageAdapterLog[] = [
...tables.map(tableToLog),
...tables.flatMap((table) =>
table.records.map(
(record): StorageAdapterLog => ({
eventName: "Store_SetRecord",
address: table.address,
args: {
tableId: table.tableId,
keyTuple: encodeKey(table.keySchema, record.key),
...encodeValueArgs(table.valueSchema, record.value),
},
})
)
),
];

// Split snapshot operations into chunks so we can update the progress callback (and ultimately render visual progress for the user).
// This isn't ideal if we want to e.g. batch load these into a DB in a single DB tx, but we'll take it.
Expand Down
1 change: 0 additions & 1 deletion packages/store-sync/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from "./common";
export * from "./createStoreSync";
export * from "./internalTableIds";
export * from "./SyncStep";
23 changes: 0 additions & 23 deletions packages/store-sync/src/internalTableIds.ts

This file was deleted.

2 changes: 0 additions & 2 deletions packages/store-sync/src/logToTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import { Hex, concatHex, decodeAbiParameters, parseAbiParameters } from "viem";
import { StorageAdapterLog, Table, schemasTable } from "./common";
import { hexToResource } from "@latticexyz/common";

// TODO: add tableToLog

export function logToTable(log: StorageAdapterLog & { eventName: "Store_SetRecord" }): Table {
const [tableId, ...otherKeys] = log.args.keyTuple;
if (otherKeys.length) {
Expand Down
34 changes: 34 additions & 0 deletions packages/store-sync/src/tableToLog.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/* eslint-disable max-len */
import { describe, it, expect } from "vitest";
import { tableToLog } from "./tableToLog";
import { storeTables } from "./common";
import { flattenSchema } from "./flattenSchema";

describe("tableToLog", () => {
it("should convert a table object to table registration log", async () => {
expect(
tableToLog({
address: "0x3Aa5ebB10DC797CAC828524e59A333d0A371443c",
tableId: storeTables.Tables.tableId,
namespace: storeTables.Tables.namespace,
name: storeTables.Tables.name,
keySchema: flattenSchema(storeTables.Tables.keySchema),
valueSchema: flattenSchema(storeTables.Tables.valueSchema),
})
).toMatchInlineSnapshot(`
{
"address": "0x3Aa5ebB10DC797CAC828524e59A333d0A371443c",
"args": {
"dynamicData": "0x00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000077461626c654964000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000500000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000e00000000000000000000000000000000000000000000000000000000000000120000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a0000000000000000000000000000000000000000000000000000000000000000b6669656c644c61796f757400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000096b6579536368656d610000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000b76616c7565536368656d610000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000012616269456e636f6465644b65794e616d657300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000014616269456e636f6465644669656c644e616d6573000000000000000000000000",
"encodedLengths": "0x000000000000000000000000000000000000022000000000a0000000000002c0",
"keyTuple": [
"0x746273746f72650000000000000000005461626c657300000000000000000000",
],
"staticData": "0x0060030220202000000000000000000000000000000000000000000000000000002001005f000000000000000000000000000000000000000000000000000000006003025f5f5fc4c40000000000000000000000000000000000000000000000",
"tableId": "0x746273746f72650000000000000000005461626c657300000000000000000000",
},
"eventName": "Store_SetRecord",
}
`);
});
});
28 changes: 28 additions & 0 deletions packages/store-sync/src/tableToLog.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {
encodeKey,
encodeValueArgs,
keySchemaToHex,
valueSchemaToFieldLayoutHex,
valueSchemaToHex,
} from "@latticexyz/protocol-parser";
import { encodeAbiParameters, parseAbiParameters } from "viem";
import { StorageAdapterLog, Table, storeTables } from "./common";
import { flattenSchema } from "./flattenSchema";

export function tableToLog(table: Table): StorageAdapterLog & { eventName: "Store_SetRecord" } {
return {
eventName: "Store_SetRecord",
address: table.address,
args: {
tableId: storeTables.Tables.tableId,
keyTuple: encodeKey(flattenSchema(storeTables.Tables.keySchema), { tableId: table.tableId }),
...encodeValueArgs(flattenSchema(storeTables.Tables.valueSchema), {
fieldLayout: valueSchemaToFieldLayoutHex(table.valueSchema),
keySchema: keySchemaToHex(table.keySchema),
valueSchema: valueSchemaToHex(table.valueSchema),
abiEncodedKeyNames: encodeAbiParameters(parseAbiParameters("string[]"), [Object.keys(table.keySchema)]),
abiEncodedFieldNames: encodeAbiParameters(parseAbiParameters("string[]"), [Object.keys(table.valueSchema)]),
}),
},
};
}

0 comments on commit 712866f

Please sign in to comment.