envio
Version:
A latency and sync speed optimized, developer friendly blockchain data indexer.
160 lines (146 loc) • 5.68 kB
JavaScript
// Generated by ReScript, PLEASE EDIT WITH CARE
;
var Js_exn = require("rescript/lib/js/js_exn.js");
var Logging = require("./Logging.res.js");
var Prometheus = require("./Prometheus.res.js");
var EntityHistory = require("./db/EntityHistory.res.js");
var ErrorHandling = require("./ErrorHandling.res.js");
var InternalTable = require("./db/InternalTable.res.js");
var Caml_exceptions = require("rescript/lib/js/caml_exceptions.js");
var Caml_js_exceptions = require("rescript/lib/js/caml_js_exceptions.js");
var StorageError = /* @__PURE__ */Caml_exceptions.create("Persistence.StorageError");
var entityHistoryActionEnumConfig_name = EntityHistory.RowAction.name;
var entityHistoryActionEnumConfig_variants = EntityHistory.RowAction.variants;
var entityHistoryActionEnumConfig_schema = EntityHistory.RowAction.schema;
var entityHistoryActionEnumConfig = {
name: entityHistoryActionEnumConfig_name,
variants: entityHistoryActionEnumConfig_variants,
schema: entityHistoryActionEnumConfig_schema,
default: "SET"
};
function make(userEntities, allEnums, storage, sql) {
var allEntities = userEntities.concat([InternalTable.DynamicContractRegistry.config]);
var allEnums$1 = allEnums.concat([entityHistoryActionEnumConfig]);
return {
userEntities: userEntities,
allEntities: allEntities,
allEnums: allEnums$1,
storageStatus: "Unknown",
storage: storage,
sql: sql
};
}
async function init(persistence, chainConfigs, resetOpt) {
var reset = resetOpt !== undefined ? resetOpt : false;
try {
var promise = persistence.storageStatus;
var shouldRun;
if (typeof promise !== "object") {
shouldRun = true;
} else if (promise.TAG === "Initializing") {
await promise._0;
shouldRun = reset;
} else {
shouldRun = reset;
}
if (!shouldRun) {
return ;
}
var resolveRef = {
contents: null
};
var promise$1 = new Promise((function (resolve, param) {
resolveRef.contents = resolve;
}));
persistence.storageStatus = {
TAG: "Initializing",
_0: promise$1
};
if (reset || !await persistence.storage.isInitialized()) {
Logging.info("Initializing the indexer storage...");
var initialState = await persistence.storage.initialize(chainConfigs, persistence.allEntities, persistence.allEnums);
Logging.info("The indexer storage is ready. Starting indexing!");
persistence.storageStatus = {
TAG: "Ready",
_0: initialState
};
} else {
var match = persistence.storageStatus;
var tmp;
tmp = typeof match !== "object" || match.TAG !== "Initializing" ? false : true;
if (tmp) {
Logging.info("Found existing indexer storage. Resuming indexing state...");
var initialState$1 = await persistence.storage.resumeInitialState();
persistence.storageStatus = {
TAG: "Ready",
_0: initialState$1
};
var progress = {};
initialState$1.chains.forEach(function (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){
var exn = Caml_js_exceptions.internalToOCamlException(raw_exn);
return ErrorHandling.mkLogAndRaise(undefined, "EE800: Failed to initialize the indexer storage.", exn);
}
}
function getInitializedStorageOrThrow(persistence) {
var match = persistence.storageStatus;
if (typeof match !== "object" || match.TAG === "Initializing") {
return Js_exn.raiseError("Failed to access the indexer storage. The Persistence layer is not initialized.");
} else {
return persistence.storage;
}
}
function getInitializedState(persistence) {
var initialState = persistence.storageStatus;
if (typeof initialState !== "object" || initialState.TAG === "Initializing") {
return Js_exn.raiseError("Failed to access the initial state. The Persistence layer is not initialized.");
} else {
return initialState._0;
}
}
async function setEffectCacheOrThrow(persistence, effect, items, invalidationsCount) {
var match = persistence.storageStatus;
if (typeof match !== "object") {
return Js_exn.raiseError("Failed to access the indexer storage. The Persistence layer is not initialized.");
}
if (match.TAG === "Initializing") {
return Js_exn.raiseError("Failed to access the indexer storage. The Persistence layer is not initialized.");
}
var cache = match._0.cache;
var storage = persistence.storage;
var effectName = effect.name;
var c = cache[effectName];
var effectCacheRecord;
if (c !== undefined) {
effectCacheRecord = c;
} else {
var c$1 = {
effectName: effectName,
count: 0
};
cache[effectName] = c$1;
effectCacheRecord = c$1;
}
var initialize = effectCacheRecord.count === 0;
await storage.setEffectCacheOrThrow(effect, items, initialize);
effectCacheRecord.count = (effectCacheRecord.count + items.length | 0) - invalidationsCount | 0;
return Prometheus.EffectCacheCount.set(effectCacheRecord.count, effectName);
}
exports.StorageError = StorageError;
exports.entityHistoryActionEnumConfig = entityHistoryActionEnumConfig;
exports.make = make;
exports.init = init;
exports.getInitializedStorageOrThrow = getInitializedStorageOrThrow;
exports.getInitializedState = getInitializedState;
exports.setEffectCacheOrThrow = setEffectCacheOrThrow;
/* Logging Not a pure module */