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 util to fetch snapshot from dozer #2996

Open
wants to merge 13 commits into
base: main
Choose a base branch
from

Conversation

alvrs
Copy link
Member

@alvrs alvrs commented Jul 31, 2024

No description provided.

Copy link

changeset-bot bot commented Jul 31, 2024

⚠️ No Changeset found

Latest commit: f22747a

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@alvrs alvrs marked this pull request as ready for review August 15, 2024 14:24
schemaAbiTypeToDefaultValue,
} from "@latticexyz/schema-type/internal";

export function decodeDozerField<abiType extends AbiType>(
Copy link
Member

Choose a reason for hiding this comment

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

what about decodeField or decodeColumnValue? (we're in the dozer export, so dozer in the name seems duplicative)

records: DecodeDozerRecordsResult;
}[];
}
| undefined;
Copy link
Member

Choose a reason for hiding this comment

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

not blocking but fwiw I found it quite nice to model this sort of thing with arktype, then can parse/validate/return strongly typed response from a JSON API request

Comment on lines +40 to +58
const response: DozerResponse = await (
await fetch(dozerUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(queries.map((query) => ({ address: storeAddress, query: query.sql }))),
})
).json();

if (isDozerResponseFail(response)) {
console.warn(`Dozer response: ${response.msg}\n\nTry reproducing via cURL:
curl ${dozerUrl} \\
--compressed \\
-H 'Accept-Encoding: gzip' \\
-H 'Content-Type: application/json' \\
-d '[${queries.map((query) => `{"address": "${storeAddress}", "query": "${query.sql.replaceAll('"', '\\"')}"}`).join(",")}]'`);
return;
}
Copy link
Member

@holic holic Aug 15, 2024

Choose a reason for hiding this comment

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

  • I find it a bit easier to read await fetch(...).then((res) => res.json()) than nested awaits
  • lifted stringified json up so we can reuse it in curl command output
Suggested change
const response: DozerResponse = await (
await fetch(dozerUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(queries.map((query) => ({ address: storeAddress, query: query.sql }))),
})
).json();
if (isDozerResponseFail(response)) {
console.warn(`Dozer response: ${response.msg}\n\nTry reproducing via cURL:
curl ${dozerUrl} \\
--compressed \\
-H 'Accept-Encoding: gzip' \\
-H 'Content-Type: application/json' \\
-d '[${queries.map((query) => `{"address": "${storeAddress}", "query": "${query.sql.replaceAll('"', '\\"')}"}`).join(",")}]'`);
return;
}
const json = JSON.stringify(queries.map((query) => ({ address: storeAddress, query: query.sql })));
const response: DozerResponse = fetch(dozerUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: json,
}).then((res) => res.json();
if (isDozerResponseFail(response)) {
console.warn(`Dozer response: ${response.msg}\n\nTry reproducing via cURL:
curl ${dozerUrl} \\
--compressed \\
-H 'Accept-Encoding: gzip' \\
-H 'Content-Type: application/json' \\
-d '${json.replaceAll("'", "\\'")}'`);
return;
}

}
| undefined;

export async function fetchRecordsSql({
Copy link
Member

Choose a reason for hiding this comment

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

what about just fetchRecords? or query? it's dozer so I think SQL can be implied

storeAddress,
}: FetchRecordsSqlArgs): Promise<FetchRecordsSqlResult> {
const response: DozerResponse = await (
await fetch(dozerUrl, {
Copy link
Member

@holic holic Aug 15, 2024

Choose a reason for hiding this comment

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

since this URL we're fetching is tied to the shape of the request/response, I wonder if this arg should be the endpoint/hostname and we append the path like /q?

-H 'Accept-Encoding: gzip' \\
-H 'Content-Type: application/json' \\
-d '[${queries.map((query) => `{"address": "${storeAddress}", "query": "${query.sql.replaceAll('"', '\\"')}"}`).join(",")}]'`);
return;
Copy link
Member

@holic holic Aug 15, 2024

Choose a reason for hiding this comment

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

I find it a little weird that a function to fetch a thing returns undefined on a failure rather than throwing or something.

I would usually reserve this behavior for the place where "failure is optional" e.g. getSnapshot or syncToStash

no changes expected, just curious to hear your reasoning

blockNumber: results.length > 0 ? bigIntMin(...results.map((result) => result.blockNumber)) : startBlock,
logs: results.flatMap((result) => result.logs),
};

Copy link
Member

Choose a reason for hiding this comment

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

ooc what happens if fetchLogs fails but fetchSql doesn't? or vice versa? or some of fetchSql does? do we end up in a weird state?


const fetchSql = (): Promise<StorageAdapterBlock | undefined>[] => {
return sqlFilters.map(async (filter) => {
const result = await fetchRecordsSql({ dozerUrl, storeAddress, queries: [filter] });
Copy link
Member

@holic holic Aug 15, 2024

Choose a reason for hiding this comment

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

more of a stylistic thing but I wonder if this would be more readable with

  • function for each query that we can .map with later
  • early return when no results
  const fetchRecords = async (query: TableQuery): Promise<StorageAdapterBlock | undefined> => {
    const result = await fetchRecordsSql({ dozerUrl, storeAddress, queries: [filter] });
    if (!result) return;
    return {
const results = (await Promise.all([fetchLogs(), ...sqlFilters.map(fetchRecords)])).filter(isDefined);

import { TableQuery } from "./common";

// For autocompletion but still allowing all SQL strings
export type Where<table extends Table> = `"${keyof table["schema"] & string}"` | (string & {});
Copy link
Member

@holic holic Aug 15, 2024

Choose a reason for hiding this comment

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

export type GetSnapshotArgs = {
dozerUrl: string;
storeAddress: Hex;
filters?: SyncFilter[];
Copy link
Member

Choose a reason for hiding this comment

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

would it better or worse to split this into two args: filters and queries?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants