UNPKG

lisk-framework

Version:

Lisk blockchain application platform

56 lines 2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Broadcaster = void 0; const lisk_codec_1 = require("@liskhq/lisk-codec"); const constants_1 = require("./constants"); const schemas_1 = require("./schemas"); class Broadcaster { constructor({ transactionPool, limit, interval, network }) { this._transactionPool = transactionPool; this._network = network; this._config = { limit, interval, }; this._transactionIdQueue = []; } init(args) { this._logger = args.logger; } start() { this._loopID = setInterval(() => { try { this._broadcast(); } catch (err) { this._logger.error({ err }, 'Failed to broadcast information'); } }, this._config.interval); } stop() { if (this._loopID) { clearInterval(this._loopID); } } enqueueTransactionId(transactionId) { if (this._transactionIdQueue.find(id => id.equals(transactionId)) !== undefined) { return false; } this._transactionIdQueue.push(transactionId); return true; } _broadcast() { this._transactionIdQueue = this._transactionIdQueue.filter(id => this._transactionPool.contains(id)); if (this._transactionIdQueue.length > 0) { const transactionIds = this._transactionIdQueue.slice(0, this._config.limit); const data = lisk_codec_1.codec.encode(schemas_1.postTransactionsAnnouncementSchema, { transactionIds }); this._network.broadcast({ event: constants_1.NETWORK_EVENT_POST_TRANSACTIONS_ANNOUNCEMENT, data, }); this._transactionIdQueue = this._transactionIdQueue.filter(id => transactionIds.find(sentID => sentID.equals(id)) === undefined); } } } exports.Broadcaster = Broadcaster; //# sourceMappingURL=broadcaster.js.map