Skip to content

Commit

Permalink
clean-up log
Browse files Browse the repository at this point in the history
  • Loading branch information
marc-aurele-besner committed Aug 8, 2024
1 parent 4e1756f commit 4ee2ace
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 14 deletions.
4 changes: 2 additions & 2 deletions indexers/staking-squid/src/blocks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Store>, 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);
}
Expand Down
4 changes: 1 addition & 3 deletions indexers/staking-squid/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
32 changes: 26 additions & 6 deletions indexers/staking-squid/src/utils/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,13 @@ export const load = async (ctx: Ctx<Store>): Promise<Cache> => {
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,
Expand All @@ -84,18 +87,35 @@ const saveEntry = async <E extends Entity>(
const entity = cache[name] as unknown as Map<string, E>;
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 = <K>(name: string, entry: Map<string, K>) =>
entry.size > 0 ? entry.size + " " + name + ", " : "";

export const save = async (ctx: Ctx<Store>, 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
Expand Down
12 changes: 9 additions & 3 deletions indexers/staking-squid/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -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 "";
Expand Down Expand Up @@ -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])
);

0 comments on commit 4ee2ace

Please sign in to comment.