UNPKG

@sangaman/xud

Version:
96 lines 3.91 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); /** Represents a list of nodes for managing network peers activity */ class NodeList { constructor(repository) { this.repository = repository; this.nodes = new Map(); /** * Return a [[NodeConnectionInfo]] array for nodes that haven't been banned and have known addresses. */ this.toConnectionInfoArray = () => { const nodeConnectionInfos = Array.from({ length: this.nodes.size }); let unbannedNodeCount = 0; this.nodes.forEach((node) => { if (!node.banned && node.addresses.length > 0) { nodeConnectionInfos[unbannedNodeCount] = { nodePubKey: node.nodePubKey, addresses: node.addresses, }; unbannedNodeCount += 1; } }); nodeConnectionInfos.length = unbannedNodeCount; return nodeConnectionInfos; }; /** * Check if a node with a given nodePubKey exists. */ this.has = (nodePubKey) => { return this.nodes.has(nodePubKey); }; this.forEach = (callback) => { this.nodes.forEach(callback); }; /** * Ban a node by nodePubKey. * @returns true if the node was banned, false otherwise */ this.ban = (nodePubKey) => __awaiter(this, void 0, void 0, function* () { const node = this.nodes.get(nodePubKey); if (node) { node.banned = true; yield this.repository.updateNode(node); return true; } return false; }); this.isBanned = (nodePubKey) => { const node = this.nodes.get(nodePubKey); return node ? node.banned : false; }; /** * Load this NodeList from the database. */ this.load = () => __awaiter(this, void 0, void 0, function* () { const nodes = yield this.repository.getNodes(); nodes.forEach((node) => { this.nodes.set(node.nodePubKey, node); }); }); /** * Create a Node in the database. */ this.createNode = (nodeFactory) => __awaiter(this, void 0, void 0, function* () { const node = yield this.repository.addNode(nodeFactory); this.nodes.set(node.nodePubKey, node); return node; }); /** * Update a node's addresses. * @return true if the specified node exists and was updated, false otherwise */ this.updateAddresses = (nodePubKey, addresses = []) => __awaiter(this, void 0, void 0, function* () { const node = this.nodes.get(nodePubKey); if (node) { node.addresses = addresses; yield this.repository.updateNode(node); return true; } return false; }); } get count() { return this.nodes.size; } } exports.default = NodeList; //# sourceMappingURL=NodeList.js.map