@pokt-network/pocket-js
Version:
Pocket-js core package with the main functionalities to interact with the Pocket Network.
126 lines • 4.94 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.RoutingTable = void 0;
var type_guard_1 = require("../utils/type-guard");
/**
*
*
* @class Routing
*/
var RoutingTable = /** @class */ (function () {
/**
* Creates an instance of routing.
* @param {URL[]} dispatchers - Array holding the initial dispatcher url(s).
* @param {Configuration} configuration - Configuration object.
* @param {IKVStore} store - KeyBase store object.
* @memberof Routing
*/
function RoutingTable(dispatchers, configuration, store) {
if (dispatchers === void 0) { dispatchers = []; }
this.localNodesFileName = "";
this.dispatchersKey = "DISPATCHER_KEY";
if (dispatchers.length > configuration.maxDispatchers && configuration.maxDispatchers > 0) {
throw new Error("Routing table cannot contain more than the specified maxDispatcher per blockchain.");
}
if (dispatchers.length < 1) {
throw new Error("Routing table must be initialized with at least one Dispatch node.");
}
this.configuration = configuration;
this.store = store;
this.store.add(this.dispatchersKey, dispatchers);
}
Object.defineProperty(RoutingTable.prototype, "dispatchersCount", {
/**
* Returns the stored dispatchers urls count
* @returns {number} Dispatcher nodes count.
* @memberof Routing
*/
get: function () {
var result = this.store.get(this.dispatchersKey);
if (result !== undefined && Array.isArray(result)) {
return result.length;
}
else {
return 0;
}
},
enumerable: false,
configurable: true
});
/**
* Returns an array of random dispatchers urls from the routing table
* @param {number} count - Desired number of dispatchers urls returned
* @returns {URL[]} Random dispatcher urls.
* @memberof Routing
*/
RoutingTable.prototype.getRandomDispatchers = function (count) {
var dispatchers = this.store.get(this.dispatchersKey);
// Shuffle array then return the slice
var shuffled = dispatchers.sort(function () { return 0.5 - Math.random(); });
if (dispatchers.length <= 0) {
return new Error("Unable to readRandomDispatchers(), No dispatcher's available.");
}
return shuffled.slice(0, count);
};
/**
* Returns a random dispatcher node from the routing table
* @returns {URL} Random dispatcher URL.
* @memberof Routing
*/
RoutingTable.prototype.getRandomDispatcher = function () {
var dispatchers = this.store.get(this.dispatchersKey);
if (dispatchers.length <= 0) {
return new Error("Unable to readRandomDispatcher(), No dispatcher's available.");
}
return dispatchers[Math.floor(Math.random() * dispatchers.length)];
};
/**
* Returns an specific node from the routing table based on public key
* @param {URL} url - Node's service url.
* @returns {Node} Node object.
* @memberof Routing
*/
RoutingTable.prototype.getDispatcher = function (url) {
var dispatchers = this.store.get(this.dispatchersKey.toUpperCase());
var requestedDispatcher;
var urlStr = url.toString();
dispatchers.forEach(function (storedURL) {
if (storedURL.toString() === urlStr) {
requestedDispatcher = url;
}
});
if (type_guard_1.typeGuard(requestedDispatcher, URL)) {
return requestedDispatcher;
}
else {
return new Error("Dispatcher not found in routing table.");
}
};
/**
* Add a dispatcher url to the routing table
* @param {URL} url - URL of the dispatcher node to be added.
* @memberof Routing
*/
RoutingTable.prototype.addDispatcher = function (url) {
var dispatchers = this.store.get(this.dispatchersKey);
var urlStr = url.toString();
// Checking if the dispatcher is already stored
var dispatcher = dispatchers.find(function (element) { return element.toString() === urlStr; });
// If the dispatcher exists then return
if (dispatcher !== undefined) {
return false;
}
// Add the new dispatcher to the array
dispatchers.push(url);
// If this pushes the count over the maxNodes, splice the first element off
if (dispatchers.length > this.configuration.maxDispatchers && this.configuration.maxDispatchers > 0) {
dispatchers.splice(0, 1);
}
// Update the store
this.store.add(this.dispatchersKey, dispatchers);
return true;
};
return RoutingTable;
}());
exports.RoutingTable = RoutingTable;
//# sourceMappingURL=routing-table.js.map