diff --git a/indexers/staking-squid/src/blocks/index.ts b/indexers/staking-squid/src/blocks/index.ts index a2f0e2fd8..6ca078d75 100644 --- a/indexers/staking-squid/src/blocks/index.ts +++ b/indexers/staking-squid/src/blocks/index.ts @@ -2,12 +2,12 @@ import type { ApiPromise } from "@autonomys/auto-utils"; import { Store } from "@subsquid/typeorm-store"; import { processExtrinsics } from "../extrinsics"; import type { Ctx, CtxBlock } from "../processor"; -import { getBlockNumber } from "../utils"; +import { logBlock } from "../utils"; import { Cache, load, save } from "../utils/cache"; export async function processBlocks(ctx: Ctx, api: ApiPromise) { let cache: Cache = await load(ctx); - console.log("Processing " + ctx.blocks.length + " blocks"); + logBlock(ctx.blocks); for (let block of ctx.blocks) { cache = await processBlock(cache, api, block); } diff --git a/indexers/staking-squid/src/main.ts b/indexers/staking-squid/src/main.ts index 5f86bd2d4..92c5f7e7f 100644 --- a/indexers/staking-squid/src/main.ts +++ b/indexers/staking-squid/src/main.ts @@ -5,10 +5,8 @@ import { processBlocks } from "./blocks"; import { processor } from "./processor"; processor.run(new TypeormDatabase({ supportHotBlocks: true }), async (ctx) => { - console.log("Starting processor"); - const api = await createConnection( - assertNotNull(process.env.RPC_CONSENSUS_HTTP, "No RPC endpoint supplied") + assertNotNull(process.env.RPC_CONSENSUS_HTTP, "No RPC_CONSENSUS_HTTP env") ); await processBlocks(ctx, api); diff --git a/indexers/staking-squid/src/utils/cache.ts b/indexers/staking-squid/src/utils/cache.ts index dd1e631bf..f4f6fae0f 100644 --- a/indexers/staking-squid/src/utils/cache.ts +++ b/indexers/staking-squid/src/utils/cache.ts @@ -61,10 +61,13 @@ export const load = async (ctx: Ctx): Promise => { ctx.store.find(Nominator), ]); - console.log("Loaded domains:", domains.length); - console.log("Loaded accounts:", accounts.length); - console.log("Loaded operators:", operators.length); - console.log("Loaded nominators:", nominators.length); + console.log( + "Loaded in cache:", + domains.length + " domains", + accounts.length + " accounts", + operators.length + " operators", + nominators.length + " nominators" + ); return { ...initCache, @@ -84,18 +87,35 @@ const saveEntry = async ( const entity = cache[name] as unknown as Map; if (entity.size === 0) return; - console.log(`Saving ${entity.size} ${name} entries to the database.`); - await ctx.store.save(Array.from(entity.values())); } catch (e) { console.error(`Failed to save ${name} with error:`, e); } }; +const logEntry = (name: string, entry: Map) => + entry.size > 0 ? entry.size + " " + name + ", " : ""; + export const save = async (ctx: Ctx, cache: Cache) => { // If the cache is not modified, skip saving if (!cache.isModified) return; + let log = logEntry("domains", cache.domains); + log += logEntry("accounts", cache.accounts); + log += logEntry("operators", cache.operators); + log += logEntry("nominators", cache.nominators); + + log += logEntry("deposits", cache.deposits); + log += logEntry("withdrawals", cache.withdrawals); + log += logEntry("bundles", cache.bundles); + log += logEntry("bundleAuthors", cache.bundleAuthors); + log += logEntry("operatorRewardedEvents", cache.operatorRewardedEvents); + log += logEntry("stats", cache.stats); + log += logEntry("statsPerDomain", cache.statsPerDomain); + log += logEntry("statsPerOperator", cache.statsPerOperator); + + console.log("Saving in database:", log); + await Promise.all( Object.keys(cache).map((k) => k !== "isModified" ? saveEntry(ctx, cache, k as keyof Cache) : null diff --git a/indexers/staking-squid/src/utils/index.ts b/indexers/staking-squid/src/utils/index.ts index bac9f4dc9..ff1a46224 100644 --- a/indexers/staking-squid/src/utils/index.ts +++ b/indexers/staking-squid/src/utils/index.ts @@ -1,13 +1,12 @@ +import { DEFAULT_SS58_FORMAT } from "@autonomys/auto-utils"; import { codec } from "@subsquid/ss58"; import type { Store } from "@subsquid/typeorm-store"; import { decodeHex } from "@subsquid/util-internal-hex"; import type { CtxBlock, ProcessorContext } from "../processor"; -const CODEC = 2254; - export const hexToAccount = (hex: string): string => { try { - return codec(CODEC).encode(decodeHex(hex)); + return codec(DEFAULT_SS58_FORMAT).encode(decodeHex(hex)); } catch (error) { console.error("Failed to convert hex to account:", error); return ""; @@ -44,3 +43,10 @@ export const bundleUID = ( domainId: number | string, domainBlockHash: string ): string => `${domainId}-${domainBlockHash}`; + +export const logBlock = (blocks: CtxBlock[]): void => + console.log( + "Processing " + blocks.length + " blocks", + "From " + getBlockNumber(blocks[0]), + "to " + getBlockNumber(blocks[blocks.length - 1]) + );