@pokt-network/pocket-js
Version:
Pocket-js core package with the main functionalities to interact with the Pocket Network.
144 lines • 5.39 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConsensusRelayResponse = void 0;
var majority_response_1 = require("../input/majority-response");
var minority_response_1 = require("../input/minority-response");
/**
*
*
* @class ConsensusRelayResponse
*/
var ConsensusRelayResponse = /** @class */ (function () {
/**
* Consensus Relay Response.
* @constructor
* @param {string} signature - Signature.
* @param {string} payload - payload string.
* @param {ConsensusNode[]} consensusNodes - List of the nodes participating in the consensus.
*/
function ConsensusRelayResponse(signature, payload, consensusNodes) {
var _this = this;
this.consensusNodes = [];
this.consensusResult = false;
this.signature = signature;
this.payload = payload;
this.consensusNodes = consensusNodes;
this.setLocalConsensusResults();
// Populate the majority and minority response objects
var majorityResponse = [];
var minorityResponse = [];
this.consensusNodes.forEach(function (node) {
if (_this.consensusResult === true) {
if (node.status) {
majorityResponse.push(node.relayResponse);
}
else {
minorityResponse.push(node.relayResponse);
}
}
else {
if (!node.status) {
majorityResponse.push(node.relayResponse);
}
else {
minorityResponse.push(node.relayResponse);
}
}
});
// We only wanna handle the case of 1 node being in disagreement with the rest
if (minorityResponse.length === 1 && majorityResponse.length > 0) {
if (majorityResponse.length > 2) {
this.majorityResponse = new majority_response_1.MajorityResponse(majorityResponse.slice(0, 2));
}
else if (majorityResponse.length === 2) {
this.majorityResponse = new majority_response_1.MajorityResponse(majorityResponse);
}
else {
throw new Error("Majority response is equal to 1, it should be 2 responses.");
}
this.minorityResponse = new minority_response_1.MinorityResponse(minorityResponse[0]);
}
else {
this.majorityResponse = new majority_response_1.MajorityResponse(majorityResponse);
}
if (!this.isValid()) {
throw new TypeError("Invalid ConsensusRelayResponse properties.");
}
}
/**
*
* Creates a ConsensusRelayResponse object using a JSON string
* @param {String} json - JSON string.
* @returns {ConsensusRelayResponse} - ConsensusRelayResponse object.
* @memberof ConsensusRelayResponse
*/
ConsensusRelayResponse.fromJSON = function (json) {
try {
var jsonObject = JSON.parse(json);
return new ConsensusRelayResponse(jsonObject.signature, jsonObject.payload, jsonObject.nodes);
}
catch (error) {
throw error;
}
};
/**
*
* Creates a JSON object with the ConsensusRelayResponse properties
* @returns {JSON} - JSON Object.
* @memberof ConsensusRelayResponse
*/
ConsensusRelayResponse.prototype.toJSON = function () {
return {
payload: this.payload,
signature: this.signature
};
};
/**
*
* Check if the ConsensusRelayResponse object is valid
* @returns {boolean} - True or false.
* @memberof ConsensusRelayResponse
*/
ConsensusRelayResponse.prototype.isValid = function () {
return (this.signature.length !== 0 &&
this.payload.length !== 0 &&
this.consensusNodes.length !== 0);
};
/**
*
* Returns whether the local consensus result is positive or negative
* @memberof ConsensusRelayResponse
*/
ConsensusRelayResponse.prototype.setLocalConsensusResults = function () {
var results = [];
var firstResponseBuffer = Buffer.from(this.payload, 'utf8');
// Iterate each consensus nodes
this.consensusNodes.forEach(function (node) {
var responseBuffer = Buffer.from(node.relayResponse.payload, 'utf8');
// Compare each relay payload result
if (Buffer.compare(responseBuffer, firstResponseBuffer) === 0) {
// Set the node status for consensus to true
node.status = true;
results.push(true);
}
else {
// Set the node status for consensus to false
node.status = false;
results.push(false);
}
});
// Retrieve the true and false results
var positive = results.filter(Boolean).length;
var negative = results.length - positive;
// Updated the consensus result based on the output
if (positive > negative) {
this.consensusResult = true;
}
else {
this.consensusResult = false;
}
};
return ConsensusRelayResponse;
}());
exports.ConsensusRelayResponse = ConsensusRelayResponse;
//# sourceMappingURL=consensus-relay-response.js.map