/**
 * BlockProcessor, Parses and persists Midnight blocks
 *
 * Processes a single block: parse header, classify transactions,
 * extract inputs/outputs/contract actions, and write to DB
 *
 */
import { MidnightNodeProvider, SignedBlock } from '../providers/MidnightNodeProvider';
/**
 * Mapping of pallet index to human-readable name and transaction type.
 * Used for classifying extrinsics into transaction types (e.g. transfer, contract_call).
 */
export interface PalletMapping {
    name: string;
    txType: string;
    isShielded?: boolean;
    isSystem?: boolean;
}
export interface ProcessResult {
    blockHeight: number;
    blockHash: string;
    transactionCount: number;
    contractActionCount: number;
    processingTimeMs: number;
}
/**
 * Per-block data fetched from the node, ready to be persisted. Produced by
 * `fetchBlockData`, consumed by `persistBlockData`. Decoupling fetch from
 * persist lets the crawler pipeline RPC fetches in parallel while writing
 * to SQLite serially.
 *
 * Modeled as a discriminated union on `alreadyIndexed`: when we short-circuit
 * because the block exists in the DB, the heavy RPC fields are absent; when
 * we fetched fully, they are all present. This replaces the earlier
 * `signedBlock: null as any` placeholder which bypassed type checking.
 */
export type PreparedBlock = PreparedBlockSkipped | PreparedBlockFetched;
export interface PreparedBlockSkipped {
    blockHash: string;
    height: number;
    fetchStartedAt: number;
    alreadyIndexed: true;
}
export interface PreparedBlockFetched {
    blockHash: string;
    height: number;
    signedBlock: SignedBlock;
    protocolVersion: number;
    timestamp: number;
    fetchStartedAt: number;
    /** Set when all RPCs for this block have resolved. Used for fetch-vs-persist timing diagnostics. */
    fetchCompletedAt?: number;
    alreadyIndexed: false;
}
export declare class BlockProcessor {
    private nodeProvider;
    private db;
    private palletMap;
    private cachedSpecVersion;
    private cachedSpecVersionValid;
    /** Well-known Substrate storage key for Timestamp::Now (twox128("Timestamp") + twox128("Now")) */
    private static readonly TIMESTAMP_STORAGE_KEY;
    constructor(nodeProvider: MidnightNodeProvider);
    init(): Promise<void>;
    /**
     * Process a single block by hash, fetch, parse, and persist atomically
     */
    processBlockByHash(blockHash: string): Promise<ProcessResult>;
    /**
     * Process a block by height
     */
    processBlockByHeight(height: number): Promise<ProcessResult>;
    /**
     * Fetch all data for a block in parallel without writing to the DB.
     * Used by the parallel catch-up pipeline so multiple block fetches can
     * be in flight while writes to SQLite happen serially in height order.
     */
    fetchBlockData(height: number): Promise<PreparedBlock>;
    /**
     * Fetch a contiguous range of blocks in two JSON-RPC batches.
     *
     * Round 1: `chain_getBlockHash(h)` for every height → 1 WSS round-trip
     * Round 2: for every NEW hash, `chain_getBlock(h)` + `state_getStorage(timestamp_key, h)`
     *           together in one batch frame → 1 WSS round-trip
     */
    fetchBlockBatch(heights: number[]): Promise<PreparedBlock[]>;
    private parseTimestampHex;
    /**
     * Persist a prefetched block. Mirror of the persist phase in
     * `processFromNode`, but skips all RPC calls.
     */
    persistPreparedBlock(prep: PreparedBlock): Promise<ProcessResult>;
    /**
     * Check if a block already exists in the local DB
     */
    blockExists(hash: string): Promise<boolean>;
    private processFromNode;
    private persistFromNode;
    private classifyExtrinsic;
    private extrinsicSize;
    private toContractActionType;
    private deriveContractAddress;
    private buildCircuitName;
    private mapPalletCall;
    private persistTransferProjections;
    private upsertNightBalance;
    private toBigInt;
    private toInt;
    /**
     * Compute blake2b-256 hash of the raw SCALE-encoded extrinsic bytes.
     * Produces the canonical extrinsic hash matching block explorers and other indexers.
     */
    private hashExtrinsic;
    /**
     * Query the Timestamp pallet storage at a specific block to get the on-chain timestamp.
     * Returns UNIX timestamp in seconds. Falls back to wall-clock time if query fails.
     */
    private getBlockTimestamp;
    /**
     * Get the runtime specVersion at a specific block
     */
    private getProtocolVersion;
    /**
     * Extract author/validator info from digest logs.
     * Looks for PreRuntime log (type 0x06) containing engine ID + authority data.
     * Falls back to the first digest log entry if no PreRuntime log found.
     */
    private extractAuthor;
}
