UNPKG

@activeledger/activeprotocol

Version:

Underlying protocol which handles consensus and the smart contract virtual machine of Activeledger

138 lines 4.77 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Shared = void 0; const activecrypto_1 = require("@activeledger/activecrypto"); const activelogger_1 = require("@activeledger/activelogger"); class Shared { constructor(_storeSingleError, entry, dbe, emitter) { this._storeSingleError = _storeSingleError; this.entry = entry; this.dbe = dbe; this.emitter = emitter; this.ioLabelMap = { i: {}, o: {} }; this._errorOut = { code: 0, reason: "", priority: 0, }; this._storedSingleErrorDoc = { id: "Default Error Id", }; this.filterMap = {}; this.assumedVirtualPrefix = ""; } set errorOut(errorOut) { this._errorOut = errorOut; } set storeSingleError(state) { this._storeSingleError = state; } getLabelIOMap(inputs, streamId) { let checkIOMap = inputs ? this.ioLabelMap.i : this.ioLabelMap.o; if (!Object.keys(checkIOMap).length) { return this.filterMap[streamId] ? this.filterMap[streamId] : streamId; } return checkIOMap[this.filterMap[streamId]] ? checkIOMap[this.filterMap[streamId]] : checkIOMap[streamId]; } filterPrefix(stream, skipMap = false) { if (!skipMap && this.filterMap[stream]) { return this.filterMap[stream]; } let [streamId, suffix] = stream.split(":"); if (streamId.length > 64) { if (!this.assumedVirtualPrefix) { this.assumedVirtualPrefix = streamId.slice(0, 2); } streamId = streamId.slice(-64); this.filterMap[streamId] = stream; this.filterMap[stream] = streamId; } if (suffix) { return streamId + ":" + suffix; } return streamId; } clearAllComms(virtualMachine, preserveComms) { if (virtualMachine.clearingInternodeCommsFromVM(this.entry.$umid)) { const nodes = Object.values(this.entry.$nodes); let i = nodes.length; while (i--) { if (nodes[i].incomms !== preserveComms) { nodes[i].incomms = null; } } } return this.entry; } async raiseLedgerError(code, reason, stop = false, priority = 0) { try { const dbDoc = (this._storedSingleErrorDoc = await this.storeError(code, reason, priority)); if (!stop) { let error = this._errorOut.reason; if (dbDoc.id) { error += " - Error " + dbDoc.id; } this.emitter.emitFailed({ status: this._errorOut.code, error, }); } } catch (error) { activelogger_1.ActiveLogger.fatal(error, "Database Error Log Issues"); if (!stop) { this.emitter.emit("failed", { status: code, error: error, }); } } } storeError(code, reason, priority = 0) { if (priority >= this._errorOut.priority) { this._errorOut.code = code; this._errorOut.reason = this.getGlobalReason(reason); this._errorOut.priority = priority; } if (!this._storeSingleError && this.entry) { const tmpEntry = JSON.parse(JSON.stringify(this.entry)); const nodeErrors = Object.keys(tmpEntry.$nodes); for (let i = nodeErrors.length; i--;) { tmpEntry.$nodes[nodeErrors[i]].incomms = null; } const doc = { code, processed: this._storeSingleError, umid: this.entry.$umid, transaction: tmpEntry, reason: this.getGlobalReason(reason), }; this._storeSingleError = true; return this.dbe.post(doc); } else { return Promise.resolve(this._storedSingleErrorDoc); } } getGlobalReason(reason) { return reason.message ? reason.message : reason.error ? reason.error : reason; } signatureCheck(publicKey, signature, type = "rsa") { try { let key = new activecrypto_1.ActiveCrypto.KeyPair(type, publicKey); return key.verify(this.entry.$tx, signature); } catch (error) { activelogger_1.ActiveLogger.error(error, "Signature Check Error"); return false; } } } exports.Shared = Shared; //# sourceMappingURL=shared.js.map