Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
25 changes: 23 additions & 2 deletions src/indexer/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import { RpcClient } from './rpc.js';
import type { RpcBlock, RpcReceipt, RpcTransaction } from './types.js';
import { hexToBigIntString, hexToBuffer } from './utils.js';

function isHistoricalStateError(error: unknown): boolean {
return error instanceof Error
&& /Storage error|state|archive|histor/i.test(error.message);
}

export function parseHeight(hexValue: string): bigint {
const parsed = hexToBigIntString(hexValue);
if (!parsed) {
Expand Down Expand Up @@ -110,8 +115,24 @@ async function upsertAccounts(client: PoolClient, addresses: string[], blockHeig
export async function refreshAccountState(
client: PoolClient, rpc: RpcClient, address: string, blockHex: string, blockHeight: bigint
): Promise<void> {
const balanceHex = await rpc.callWithRetry<string>('eth_getBalance', [address, blockHex]);
const nonceHex = await rpc.callWithRetry<string>('eth_getTransactionCount', [address, blockHex]);
let balanceHex: string;
let nonceHex: string;

try {
balanceHex = await rpc.callWithRetry<string>('eth_getBalance', [address, blockHex]);
nonceHex = await rpc.callWithRetry<string>('eth_getTransactionCount', [address, blockHex]);
} catch (error) {
if (!isHistoricalStateError(error)) {
throw error;
}

console.warn(
`Historical state unavailable for ${address} at block ${blockHeight}; falling back to latest state`
);
balanceHex = await rpc.callWithRetry<string>('eth_getBalance', [address, 'latest']);
nonceHex = await rpc.callWithRetry<string>('eth_getTransactionCount', [address, 'latest']);
}

const balance = hexToBigIntString(balanceHex) ?? '0';
const nonce = parseHeight(nonceHex).toString(10);

Expand Down
33 changes: 13 additions & 20 deletions src/routes/inference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,15 @@ export default async function inferenceRoutes(app: FastifyInstance) {
const submitter = q.submitter?.toLowerCase() || null;

const data = await cached(`inference:tasks:${page}:${limit}:${statusFilter}:${submitter}`, 10, async () => {
// Fetch all tasks from RPC
const allTasks = await rpcCallSafe<TaskItem[]>('qfc_getInferenceTasks', []) ?? [];

// Filter
let filtered = allTasks;
if (statusFilter) {
filtered = filtered.filter((t) => String(t.status).toLowerCase() === statusFilter.toLowerCase());
}
if (submitter) {
filtered = filtered.filter((t) => String(t.submitter).toLowerCase() === submitter);
}

// Sort by createdAt descending
filtered.sort((a, b) => Number(b.createdAt ?? 0) - Number(a.createdAt ?? 0));

// Stats
// Build RPC filter — qfc_listPublicTasks supports server-side filtering & pagination
const filter: Record<string, unknown> = {};
if (statusFilter) filter.status = statusFilter;
if (submitter) filter.submitter = submitter;

// Fetch all matching tasks (with generous limit for stats calculation)
const allTasks = await rpcCallSafe<TaskItem[]>('qfc_listPublicTasks', [{ ...filter, limit: 1000 }]) ?? [];

// Stats from full result set
const completed = allTasks.filter((t) => String(t.status).toLowerCase() === 'completed').length;
const pending = allTasks.filter((t) => String(t.status).toLowerCase() === 'pending').length;
const failed = allTasks.filter((t) => String(t.status).toLowerCase() === 'failed').length;
Expand All @@ -66,10 +59,10 @@ export default async function inferenceRoutes(app: FastifyInstance) {
? Math.round(completedTasks.reduce((sum, t) => sum + Number(t.executionTimeMs), 0) / completedTasks.length)
: 0;

// Paginate
const total = filtered.length;
// Paginate (RPC already returns newest-first)
const total = allTasks.length;
const start = (page - 1) * limit;
const items = filtered.slice(start, start + limit);
const items = allTasks.slice(start, start + limit);

return {
page,
Expand All @@ -90,7 +83,7 @@ export default async function inferenceRoutes(app: FastifyInstance) {
const [models, stats, allTasks, miners] = await Promise.all([
rpcCallSafe<ModelItem[]>('qfc_getSupportedModels', []) ?? [],
rpcCallSafe<Record<string, unknown>>('qfc_getInferenceStats', []),
rpcCallSafe<TaskItem[]>('qfc_getInferenceTasks', []) ?? [],
rpcCallSafe<TaskItem[]>('qfc_listPublicTasks', [{ limit: 1000 }]) ?? [],
rpcCallSafe<Array<Record<string, unknown>>>('qfc_getRegisteredMiners', []) ?? [],
]);

Expand Down
Loading