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(cli): use fetchLogs for resource tags #3350

Merged
merged 4 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
62 changes: 44 additions & 18 deletions packages/cli/src/deploy/ensureResourceTags.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import { Hex, Client, Transport, Chain, Account, stringToHex, BaseError } from "viem";
import { Hex, Client, Transport, Chain, Account, stringToHex, BaseError, concatHex } from "viem";
import { WorldDeploy } from "./common";
import { debug } from "./debug";
import { hexToResource, writeContract } from "@latticexyz/common";
import { identity, isDefined } from "@latticexyz/common/utils";
import metadataConfig from "@latticexyz/world-module-metadata/mud.config";
import metadataAbi from "@latticexyz/world-module-metadata/out/IMetadataSystem.sol/IMetadataSystem.abi.json" assert { type: "json" };
import { getRecord } from "./getRecord";
import { ensureModules } from "./ensureModules";
import metadataModule from "@latticexyz/world-module-metadata/out/MetadataModule.sol/MetadataModule.json" assert { type: "json" };
import { getContractArtifact } from "../utils/getContractArtifact";
import { createPrepareDeploy } from "./createPrepareDeploy";
import { waitForTransactions } from "./waitForTransactions";
import { LibraryMap } from "./getLibraryMap";
import { fetchBlockLogs } from "@latticexyz/block-logs-stream";
import { getStoreLogs, flattenStoreLogs, logToRecord } from "@latticexyz/store/internal";
import { getKeyTuple, getSchemaPrimitives } from "@latticexyz/protocol-parser/internal";

const metadataModuleArtifact = getContractArtifact(metadataModule);

Expand All @@ -37,21 +39,45 @@ export async function ensureResourceTags<const value>({
} & (value extends Hex
? { readonly valueToHex?: (value: value) => Hex }
: { readonly valueToHex: (value: value) => Hex })): Promise<readonly Hex[]> {
const pendingTags = (
await Promise.all(
tags.map(async (tag) => {
const { value } = await getRecord({
client,
worldDeploy,
table: metadataConfig.tables.metadata__ResourceTag,
key: { resource: tag.resourceId, tag: stringToHex(tag.tag, { size: 32 }) },
});
if (value === valueToHex(tag.value)) return;
return tag;
}),
)
).filter(isDefined);
debug("ensuring", tags.length, "resource tags");

debug("looking up existing resource tags");
const blockLogs = await fetchBlockLogs({
fromBlock: worldDeploy.deployBlock,
toBlock: worldDeploy.stateBlock,
maxBlockRange: 100_000n,
async getLogs({ fromBlock, toBlock }) {
return getStoreLogs(client, {
address: worldDeploy.address,
fromBlock,
toBlock,
tableId: metadataConfig.tables.metadata__ResourceTag.tableId,
});
},
});
const logs = flattenStoreLogs(blockLogs.flatMap((block) => block.logs));
const records = logs.map((log) => logToRecord({ log, table: metadataConfig.tables.metadata__ResourceTag }));
debug("found", records.length, "resource tags");

const existingTags = new Map(
records.map((tag) => {
const key = concatHex(getKeyTuple(metadataConfig.tables.metadata__ResourceTag, tag));
return [key, tag.value];
}),
);

const desiredTags = tags.map(
(tag): getSchemaPrimitives<typeof metadataConfig.tables.metadata__ResourceTag.schema> => ({
resource: tag.resourceId,
tag: stringToHex(tag.tag, { size: 32 }),
value: valueToHex(tag.value),
}),
);

const pendingTags = desiredTags.filter((tag) => {
const key = concatHex(getKeyTuple(metadataConfig.tables.metadata__ResourceTag, tag));
return existingTags.get(key) !== tag.value;
});
if (pendingTags.length === 0) return [];

// TODO: check if metadata namespace exists, if we own it, and if so transfer ownership to the module before reinstalling
Expand Down Expand Up @@ -86,7 +112,7 @@ export async function ensureResourceTags<const value>({
return (
await Promise.all(
pendingTags.map(async (tag) => {
const resource = hexToResource(tag.resourceId);
const resource = hexToResource(tag.resource);
// TODO: move to resourceToDebugString
const resourceString = `${resource.type}:${resource.namespace}:${resource.name}`;
debug(`tagging ${resourceString} with ${tag.tag}: ${JSON.stringify(tag.value)}`);
Expand All @@ -97,7 +123,7 @@ export async function ensureResourceTags<const value>({
abi: metadataAbi,
// TODO: replace with batchCall (https://github.com/latticexyz/mud/issues/1645)
functionName: "metadata__setResourceTag",
args: [tag.resourceId, stringToHex(tag.tag, { size: 32 }), valueToHex(tag.value)],
args: [tag.resource, tag.tag, tag.value],
});
} catch (error) {
debug(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export async function getLogs(
readonly address?: Hex;
readonly filters?: readonly SyncFilter[];
},
): Promise<{ blockNumber: bigint; logs: (StorageAdapterLog & { eventName: "Store_SetRecord" })[] }> {
): Promise<{ blockNumber: bigint; logs: Extract<StorageAdapterLog, { eventName: "Store_SetRecord" }>[] }> {
const benchmark = createBenchmark("drizzleGetLogs");

const conditions = filters.length
Expand Down
2 changes: 1 addition & 1 deletion packages/store-indexer/src/postgres/recordToLog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { RecordData } from "./common";

export function recordToLog(
record: Omit<RecordData, "recordBlockNumber">,
): StorageAdapterLog & { eventName: "Store_SetRecord" } {
): Extract<StorageAdapterLog, { eventName: "Store_SetRecord" }> {
return {
address: record.address,
eventName: "Store_SetRecord",
Expand Down
3 changes: 2 additions & 1 deletion packages/store-sync/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ export * from "./logToTable";
export * from "./tablesWithRecordsToLogs";
export * from "./tableToLog";
export * from "./recordToLog";
export * from "./logToRecord";

export { logToRecord, type LogToRecordArgs } from "@latticexyz/store/internal";
Copy link
Member Author

@holic holic Oct 31, 2024

Choose a reason for hiding this comment

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

feels weird to export an internal thing but this let me move it to store without breaking

maybe we just remove this export since its intent is internal?

2 changes: 1 addition & 1 deletion packages/store-sync/src/isTableRegistrationLog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ import { StorageAdapterLog, schemasTable } from "./common";
*/
export function isTableRegistrationLog(
log: StorageAdapterLog,
): log is StorageAdapterLog & { eventName: "Store_SetRecord" } {
): log is Extract<StorageAdapterLog, { eventName: "Store_SetRecord" }> {
return log.eventName === "Store_SetRecord" && log.args.tableId === schemasTable.tableId;
}
2 changes: 1 addition & 1 deletion packages/store-sync/src/logToTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Schema } from "@latticexyz/config";
/**
* @internal
*/
export function logToTable(log: StorageAdapterLog & { eventName: "Store_SetRecord" }): Table {
export function logToTable(log: Extract<StorageAdapterLog, { eventName: "Store_SetRecord" }>): Table {
const [tableId, ...otherKeys] = log.args.keyTuple;
if (otherKeys.length) {
console.warn("registerSchema event is expected to have only one key in key tuple, but got multiple", log);
Expand Down
6 changes: 3 additions & 3 deletions packages/store-sync/src/recordToLog.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {
SchemaToPrimitives,
encodeKey,
encodeValueArgs,
getKeySchema,
getSchemaTypes,
getValueSchema,
getKey,
getValue,
getSchemaPrimitives,
} from "@latticexyz/protocol-parser/internal";
import { StorageAdapterLog } from "./common";
import { Table } from "@latticexyz/config";
Expand All @@ -17,14 +17,14 @@ type PartialTable = Pick<Table, "schema" | "key">;
type RecordToLogArgs<table extends PartialTable> = {
address: Hex;
table: table;
record: SchemaToPrimitives<getSchemaTypes<table["schema"]>>;
record: getSchemaPrimitives<table["schema"]>;
};

export function recordToLog<table extends Table>({
table,
record,
address,
}: RecordToLogArgs<table>): StorageAdapterLog & { eventName: "Store_SetRecord" } {
}: RecordToLogArgs<table>): Extract<StorageAdapterLog, { eventName: "Store_SetRecord" }> {
const keySchema = getSchemaTypes(getKeySchema(table));
const valueSchema = getSchemaTypes(getValueSchema(table));

Expand Down
2 changes: 1 addition & 1 deletion packages/store-sync/src/tableToLog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { StorageAdapterLog, Table, schemasTable } from "./common";
/**
* @internal
*/
export function tableToLog(table: Table): StorageAdapterLog & { eventName: "Store_SetRecord" } {
export function tableToLog(table: Table): Extract<StorageAdapterLog, { eventName: "Store_SetRecord" }> {
return {
eventName: "Store_SetRecord",
address: table.address,
Expand Down
1 change: 1 addition & 0 deletions packages/store/ts/exports/internal.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export * from "../common";
export * from "../getStoreLogs";
export * from "../flattenStoreLogs";
export * from "../logToRecord";

export * from "../config/v2/codegen";
export * from "../config/v2/defaults";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
import { Table } from "@latticexyz/config";
import {
SchemaToPrimitives,
decodeKey,
decodeValueArgs,
getKeySchema,
getSchemaPrimitives,
getSchemaTypes,
getValueSchema,
} from "@latticexyz/protocol-parser/internal";
import { StorageAdapterLog } from "./common";
import { StoreLog } from "./storeLog";

type PartialTable = Pick<Table, "schema" | "key">;
type PartialLog = Pick<Extract<StoreLog, { eventName: "Store_SetRecord" }>, "args">;

type LogToRecordArgs<table extends PartialTable> = {
export type LogToRecordArgs<table extends PartialTable> = {
table: table;
log: StorageAdapterLog & { eventName: "Store_SetRecord" };
log: PartialLog;
};

export function logToRecord<table extends PartialTable>({
table,
log,
}: LogToRecordArgs<table>): SchemaToPrimitives<getSchemaTypes<table["schema"]>> {
}: LogToRecordArgs<table>): getSchemaPrimitives<table["schema"]> {
const keySchema = getSchemaTypes(getKeySchema(table));
const valueSchema = getSchemaTypes(getValueSchema(table));
const key = decodeKey(keySchema, log.args.keyTuple);
Expand Down
Loading