UNPKG

envio

Version:

A latency and sync speed optimized, developer friendly blockchain data indexer.

127 lines (119 loc) 4.56 kB
// Generated by ReScript, PLEASE EDIT WITH CARE import * as Config from "./Config.res.mjs"; import * as Logging from "./Logging.res.mjs"; import * as EntityHistory from "./db/EntityHistory.res.mjs"; import * as ErrorHandling from "./ErrorHandling.res.mjs"; import * as Stdlib_JsError from "@rescript/runtime/lib/es6/Stdlib_JsError.js"; import * as Primitive_exceptions from "@rescript/runtime/lib/es6/Primitive_exceptions.js"; let StorageError = /* @__PURE__ */Primitive_exceptions.create("Persistence.StorageError"); function make(userEntities, allEnums, storage) { let allEntities = userEntities.concat([Config.EnvioAddresses.entityConfig]); let allEnums$1 = allEnums.concat([EntityHistory.RowAction.config]); return { userEntities: userEntities, allEntities: allEntities, allEnums: allEnums$1, storageStatus: "Unknown", storage: storage }; } async function init(persistence, chainConfigs, envioInfo, resetCommand, runCommand, resetOpt) { let reset = resetOpt !== undefined ? resetOpt : false; try { let promise = persistence.storageStatus; let shouldRun; if (typeof promise !== "object") { shouldRun = true; } else if (promise.TAG === "Initializing") { await promise._0; shouldRun = reset; } else { shouldRun = reset; } if (!shouldRun) { return; } let resolveRef = { contents: null }; let promise$1 = new Promise((resolve, param) => { resolveRef.contents = resolve; }); persistence.storageStatus = { TAG: "Initializing", _0: promise$1 }; if (reset || !await persistence.storage.isInitialized()) { Logging.info(`Initializing the indexer storage...`); let initialState = await persistence.storage.initialize(chainConfigs, persistence.allEntities, persistence.allEnums, envioInfo); Logging.info(`The indexer storage is ready. Starting indexing!`); persistence.storageStatus = { TAG: "Ready", _0: initialState }; } else { let match = persistence.storageStatus; let tmp; tmp = typeof match !== "object" ? false : match.TAG === "Initializing"; if (tmp) { Logging.info(`Found existing indexer storage. Resuming indexing state...`); let initialState$1 = await persistence.storage.resumeInitialState(); let stored = initialState$1.envioInfo; let changedPaths = stored !== undefined ? Config.diffPaths(stored, envioInfo) : ["envio info is missing — storage initialized by an older envio"]; let hasClickhouse; if (typeof envioInfo === "object" && envioInfo !== null && !Array.isArray(envioInfo)) { let match$1 = envioInfo["storage"]; if (typeof match$1 === "object" && match$1 !== null && !Array.isArray(match$1)) { let match$2 = match$1["clickhouse"]; hasClickhouse = match$2 === true; } else { hasClickhouse = false; } } else { hasClickhouse = false; } Config.throwIfIncompatible(changedPaths, resetCommand, runCommand, hasClickhouse); persistence.storageStatus = { TAG: "Ready", _0: initialState$1 }; let progress = {}; initialState$1.chains.forEach(c => { progress[c.id] = c.progressBlockNumber; }); Logging.info({ msg: `Successfully resumed indexing state! Continuing from the last checkpoint.`, progress: progress }); } } return resolveRef.contents(); } catch (raw_exn) { let exn = Primitive_exceptions.internalToException(raw_exn); return ErrorHandling.mkLogAndRaise(undefined, `Failed to initialize the indexer storage.`, exn); } } function getInitializedStorageOrThrow(persistence) { let match = persistence.storageStatus; if (typeof match !== "object" || match.TAG === "Initializing") { return Stdlib_JsError.throwWithMessage(`Failed to access the indexer storage. The Persistence layer is not initialized.`); } else { return persistence.storage; } } function getInitializedState(persistence) { let initialState = persistence.storageStatus; if (typeof initialState !== "object" || initialState.TAG === "Initializing") { return Stdlib_JsError.throwWithMessage(`Failed to access the initial state. The Persistence layer is not initialized.`); } else { return initialState._0; } } export { StorageError, make, init, getInitializedStorageOrThrow, getInitializedState, } /* Config Not a pure module */