UNPKG

lisk-framework

Version:

Lisk blockchain application platform

60 lines 2.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CommitList = exports.COMMIT_SORT = void 0; var COMMIT_SORT; (function (COMMIT_SORT) { COMMIT_SORT[COMMIT_SORT["ASC"] = 1] = "ASC"; COMMIT_SORT[COMMIT_SORT["DSC"] = -1] = "DSC"; })(COMMIT_SORT = exports.COMMIT_SORT || (exports.COMMIT_SORT = {})); class CommitList { constructor() { this._commitMap = new Map(); } getByHeight(height) { var _a; return (_a = this._commitMap.get(height)) !== null && _a !== void 0 ? _a : []; } getHeights() { return [...this._commitMap.keys()]; } size() { let sum = 0; for (const [, value] of this._commitMap) { sum += value.length; } return sum; } add(commit) { var _a; const currentCommits = (_a = this._commitMap.get(commit.height)) !== null && _a !== void 0 ? _a : []; this._commitMap.set(commit.height, [...currentCommits, commit]); } exists(commit) { var _a; const currentCommits = (_a = this._commitMap.get(commit.height)) !== null && _a !== void 0 ? _a : []; return currentCommits.some(aCommit => aCommit.blockID.equals(commit.blockID) && aCommit.validatorAddress.equals(commit.validatorAddress)); } deleteByHeight(height) { this._commitMap.delete(height); } getAll(sortOrder = COMMIT_SORT.ASC) { return [...this._commitMap.values()].flat().sort((a, b) => sortOrder * (a.height - b.height)); } deleteSingle(commit) { const commitList = this._commitMap.get(commit.height); if (!commitList) { return; } const index = commitList.findIndex(c => c.blockID.equals(commit.blockID) && c.validatorAddress.equals(commit.validatorAddress)); if (index < 0) { return; } commitList.splice(index, 1); if (commitList.length === 0) { this._commitMap.delete(commit.height); } } } exports.CommitList = CommitList; //# sourceMappingURL=commit_list.js.map