-
Notifications
You must be signed in to change notification settings - Fork 173
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,store-indexer): sync from getLogs indexer endpoint #1973
Merged
Merged
Changes from 19 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
26635c0
hydrate from indexer logs
holic b975050
fix
holic c5b3fe1
note about old endpoint
holic 6c9ddd6
swap trpc transformer
holic fcbaf5d
refactor snapshot, fallback to old indexer endpoint
holic 39b32a9
Revert "swap trpc transformer"
holic eaa9068
warn about older indexers
holic 54173cf
implement getLogs in sqlite indexer frontend
holic 875e9b2
clean up
holic d9e9cb0
update test
holic 4ffd60b
sort records
holic 5795a3d
more predictable ordering
holic 723ed1a
Create wet-crabs-punch.md
holic 6822366
Create wicked-donuts-cheat.md
holic 0928e20
Create angry-peas-heal.md
holic 424a2f1
Update wicked-donuts-cheat.md
holic 9ec28ce
Update wet-crabs-punch.md
holic aa393d4
Update getTablesWithRecords.ts
holic 8726177
loosen e2e indexer test
holic f1886bc
remove extra sorting
holic 74921c7
Merge branch 'main' into holic/sync-indexer-logs
holic File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,72 @@ | ||
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)) | ||
.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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for context, why do we have to order the result here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it was an attempt to fix the e2e tests having inconsistent results between rpc and indexer as far how data is populated, but ended up not being enough
I fixed the e2e test here and need to bring this commit over: 74611df
ultimately we lose some fidelity/ordering when hydrating from the indexer because everything is done at the block level, so records pulled from the DB within the same block may not be exactly the same order as records created from a list of logs from the RPC (mostly due to a lack of
logIndex
)this isn't really an issue practically speaking, but felt like it would be nice to have some consistency between rpc and indexer in terms of data returned and its ordering
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we still need the sorting now? unclear if it affects performance at all
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should still prob order by block number, but maybe not others
I think these will still take advantage of the index, but will remove for now