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(store-sync): add syncToStash util #3192

Merged
merged 21 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 7 additions & 0 deletions packages/store-sync/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { StoreEventsAbiItem, StoreEventsAbi } from "@latticexyz/store";
import { Observable } from "rxjs";
import { UnionPick } from "@latticexyz/common/type-utils";
import {
ValueArgs,
getKeySchema,
getSchemaPrimitives,
getSchemaTypes,
Expand Down Expand Up @@ -140,3 +141,9 @@ export const schemasTable = {
keySchema: getSchemaTypes(getKeySchema(mudTables.Tables)),
valueSchema: getSchemaTypes(getValueSchema(mudTables.Tables)),
};

export const emptyValueArgs = {
staticData: "0x",
encodedLengths: "0x",
dynamicData: "0x",
} as const satisfies ValueArgs;
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe("createStorageAdapter", async () => {

it("sets component values from logs", async () => {
const stash = createStash(config);
const storageAdapter = createStorageAdapter({ config, stash });
const storageAdapter = createStorageAdapter({ stash });

console.log("fetching blocks");
// eslint-disable-next-line @typescript-eslint/no-unused-vars
Expand Down
31 changes: 10 additions & 21 deletions packages/store-sync/src/stash/createStorageAdapter.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,37 @@
import { World } from "@latticexyz/world";
import { StorageAdapterBlock, StorageAdapter } from "../common";
import { CreateStoreResult, getTable, StoreConfig } from "@latticexyz/stash/internal";
import {
decodeKey,
decodeValueArgs,
encodeValueArgs,
getKeySchema,
getSchemaTypes,
getValueSchema,
KeySchema,
} from "@latticexyz/protocol-parser/internal";
import { CreateStoreResult, getTable } from "@latticexyz/stash/internal";
import { spliceHex } from "@latticexyz/common";
import { size } from "viem";
import { Table } from "@latticexyz/config";
import { StorageAdapter, StorageAdapterBlock, emptyValueArgs } from "../common";

export type CreateStorageAdapterOptions<config extends World> = {
config: config;
export type CreateStorageAdapter<config extends StoreConfig> = {
stash: CreateStoreResult<config>;
};

const emptyValueArgs = {
staticData: "0x",
encodedLengths: "0x",
dynamicData: "0x",
} as const;

export function createStorageAdapter<config extends World>({
config,
export function createStorageAdapter<const config extends StoreConfig>({
stash,
}: CreateStorageAdapterOptions<config>): StorageAdapter {
const tablesById = Object.fromEntries(
Object.values(config.namespaces)
.flatMap((namespace) => Object.values(namespace.tables))
.map((table) => [table.tableId, table]),
);
}: CreateStorageAdapter<config>): StorageAdapter {
const tables = Object.values(stash.get().config).flatMap((namespace) => Object.values(namespace)) as readonly Table[];

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alvrs this is where types got wonky

return async function storageAdapter({ logs }: StorageAdapterBlock): Promise<void> {
for (const log of logs) {
const tableConfig = tablesById[log.args.tableId];
const tableConfig = tables.find((t) => t.tableId === log.args.tableId);
if (!tableConfig) continue;

// TODO: this should probably return `| undefined`?
const table = getTable({ stash, table: tableConfig });

const valueSchema = getSchemaTypes(getValueSchema(tableConfig));
const keySchema = getSchemaTypes(getKeySchema(tableConfig));
const keySchema = getSchemaTypes(getKeySchema(tableConfig)) as KeySchema;
const key = decodeKey(keySchema, log.args.keyTuple);

if (log.eventName === "Store_SetRecord") {
Expand Down
1 change: 1 addition & 0 deletions packages/store-sync/src/stash/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./createStorageAdapter";
export * from "./syncToStash";
74 changes: 46 additions & 28 deletions packages/store-sync/src/stash/syncToStash.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { World } from "@latticexyz/world";
import { CreateStoreResult, StoreConfig } from "@latticexyz/stash/internal";
import { Address, Client, publicActions } from "viem";
import { defineTable } from "@latticexyz/store/config/v2";
import { CreateStoreResult } from "@latticexyz/stash/internal";
import { createStorageAdapter } from "./createStorageAdapter";
import { defineTable } from "@latticexyz/store/config/v2";
import { SyncStep } from "../SyncStep";
import { SyncResult } from "../common";
import { createStoreSync } from "../createStoreSync";
import { SyncStep } from "../SyncStep";
import { getSchemaPrimitives, getValueSchema } from "@latticexyz/protocol-parser/internal";

export const SyncProgress = defineTable({
namespaceLabel: "syncToStash",
label: "SyncProgress",
schema: {
step: "string",
Expand All @@ -19,40 +20,57 @@ export const SyncProgress = defineTable({
key: [],
});

export type SyncToStashOptions<config extends World> = {
config: config;
stash: CreateStoreResult;
export const initialProgress = {
step: SyncStep.INITIALIZE,
percentage: 0,
latestBlockNumber: 0n,
lastBlockNumberProcessed: 0n,
message: "Connecting",
} satisfies getSchemaPrimitives<getValueSchema<typeof SyncProgress>>;

export type SyncToStashOptions<config extends StoreConfig> = {
stash: CreateStoreResult<config>;
client: Client;
address: Address;
startSync?: boolean;
};

export type SyncToStashResult = Omit<SyncResult, "waitForTransaction"> & {
waitForStateChange: SyncResult["waitForTransaction"];
stopSync: () => void;
};

export async function syncToStash<const config extends World>({
config,
export async function syncToStash<const config extends StoreConfig>({
stash,
client,
address,
}: SyncToStashOptions<config>): Promise<SyncResult> {
return createStoreSync({
storageAdapter: createStorageAdapter({ config, stash }),
startSync = true,
}: SyncToStashOptions<config>): Promise<SyncToStashResult> {
stash.registerTable({ table: SyncProgress });

const storageAdapter = createStorageAdapter({ stash });

const { waitForTransaction: waitForStateChange, ...sync } = await createStoreSync({
storageAdapter,
publicClient: client.extend(publicActions) as never,
address,
onProgress: ({ step, percentage, latestBlockNumber, lastBlockNumberProcessed, message }) => {
// already live, no need for more progress updates
if (stash.getRecord({ table: SyncProgress, key: {} })?.step === SyncStep.LIVE) {
return;
onProgress: (nextValue) => {
const currentValue = stash.getRecord({ table: SyncProgress, key: {} });
// update sync progress until we're caught up and live
if (currentValue?.step !== SyncStep.LIVE) {
stash.setRecord({ table: SyncProgress, key: {}, value: nextValue });
}

stash.setRecord({
table: SyncProgress,
key: {},
value: {
step,
percentage,
latestBlockNumber,
lastBlockNumberProcessed,
message,
},
});
},
});

const sub = startSync ? sync.storedBlockLogs$.subscribe() : null;
function stopSync(): void {
sub?.unsubscribe();
}

return {
...sync,
waitForStateChange,
stopSync,
};
}
Loading