-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(store-sync,store-indexer): sync from getLogs indexer endpoint (#…
- Loading branch information
Showing
15 changed files
with
289 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
--- | ||
"@latticexyz/store-sync": minor | ||
--- | ||
|
||
Refactored how we fetch snapshots from an indexer, preferring the new `getLogs` endpoint and falling back to the previous `findAll` if it isn't available. This refactor also prepares for an easier entry point for adding client caching of snapshots. | ||
|
||
The `initialState` option for various sync methods (`syncToPostgres`, `syncToRecs`, etc.) is now deprecated in favor of `initialBlockLogs`. For now, we'll automatically convert `initialState` into `initialBlockLogs`, but if you want to update your code, you can do: | ||
|
||
```ts | ||
import { tablesWithRecordsToLogs } from "@latticexyz/store-sync"; | ||
|
||
const initialBlockLogs = { | ||
blockNumber: initialState.blockNumber, | ||
logs: tablesWithRecordsToLogs(initialState.tables), | ||
}; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@latticexyz/common": minor | ||
--- | ||
|
||
Updated `chunk` types to use readonly arrays |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@latticexyz/store-indexer": minor | ||
--- | ||
|
||
Added `getLogs` query support to sqlite indexer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { asc, eq } from "drizzle-orm"; | ||
import { BaseSQLiteDatabase } from "drizzle-orm/sqlite-core"; | ||
import { buildTable, chainState, getTables } from "@latticexyz/store-sync/sqlite"; | ||
import { Hex, getAddress } from "viem"; | ||
import { decodeDynamicField } from "@latticexyz/protocol-parser"; | ||
import { SyncFilter, TableWithRecords } from "@latticexyz/store-sync"; | ||
|
||
// TODO: refactor sqlite and replace this with getLogs to match postgres (https://github.com/latticexyz/mud/issues/1970) | ||
|
||
/** | ||
* @deprecated | ||
* */ | ||
export function getTablesWithRecords( | ||
database: BaseSQLiteDatabase<"sync", any>, | ||
{ | ||
chainId, | ||
address, | ||
filters = [], | ||
}: { | ||
readonly chainId: number; | ||
readonly address?: Hex; | ||
readonly filters?: readonly SyncFilter[]; | ||
} | ||
): { blockNumber: bigint | null; tables: readonly TableWithRecords[] } { | ||
const metadata = database | ||
.select() | ||
.from(chainState) | ||
.where(eq(chainState.chainId, chainId)) | ||
.limit(1) | ||
.all() | ||
.find(() => true); | ||
|
||
// If _any_ filter has a table ID, this will filter down all data to just those tables. Which mean we can't yet mix table filters with key-only filters. | ||
// TODO: improve this so we can express this in the query (need to be able to query data across tables more easily) | ||
const tableIds = Array.from(new Set(filters.map((filter) => filter.tableId))); | ||
const tables = getTables(database) | ||
.filter((table) => address == null || getAddress(address) === getAddress(table.address)) | ||
.filter((table) => !tableIds.length || tableIds.includes(table.tableId)); | ||
|
||
const tablesWithRecords = tables.map((table) => { | ||
const sqliteTable = buildTable(table); | ||
const records = database | ||
.select() | ||
.from(sqliteTable) | ||
.where(eq(sqliteTable.__isDeleted, false)) | ||
.orderBy( | ||
asc(sqliteTable.__lastUpdatedBlockNumber) | ||
// TODO: add logIndex (https://github.com/latticexyz/mud/issues/1979) | ||
) | ||
.all(); | ||
const filteredRecords = !filters.length | ||
? records | ||
: records.filter((record) => { | ||
const keyTuple = decodeDynamicField("bytes32[]", record.__key); | ||
return filters.some( | ||
(filter) => | ||
filter.tableId === table.tableId && | ||
(filter.key0 == null || filter.key0 === keyTuple[0]) && | ||
(filter.key1 == null || filter.key1 === keyTuple[1]) | ||
); | ||
}); | ||
return { | ||
...table, | ||
records: filteredRecords.map((record) => ({ | ||
key: Object.fromEntries(Object.entries(table.keySchema).map(([name]) => [name, record[name]])), | ||
value: Object.fromEntries(Object.entries(table.valueSchema).map(([name]) => [name, record[name]])), | ||
})), | ||
}; | ||
}); | ||
|
||
return { | ||
blockNumber: metadata?.lastUpdatedBlockNumber ?? null, | ||
tables: tablesWithRecords, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.