Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 76 additions & 1 deletion lib/constants/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = {
const constants = {
// Public key id size in bytes
PUBKEY_ID_SIZE: 20,
// Standard compact size variable, size in bytes
Expand Down Expand Up @@ -106,3 +106,78 @@ module.exports = {
BASIC_BLS_SCHEME_VERSION: 3,
BASIC_BLS_SCHEME_HASH_QUORUM_INDEX_REQUIRED_VERSION: 4,
};

constants.LLMQ_TYPE_PARAMS= {
[constants.LLMQ_TYPES.LLMQ_TYPE_50_60]: {
size: 50,
threshold: 30,
maximumActiveQuorumsCount: 24,
},
[constants.LLMQ_TYPES.LLMQ_TYPE_60_75]: {
size: 60,
threshold: 45,
maximumActiveQuorumsCount: 24,
},
[constants.LLMQ_TYPES.LLMQ_TYPE_400_60]: {
size: 400,
threshold: 240,
maximumActiveQuorumsCount: 4,
},
[constants.LLMQ_TYPES.LLMQ_TYPE_400_85]: {
size: 400,
threshold: 340,
maximumActiveQuorumsCount: 4,
},
[constants.LLMQ_TYPES.LLMQ_TYPE_100_67]: {
size: 100,
threshold: 67,
maximumActiveQuorumsCount: 24,
},
[constants.LLMQ_TYPES.LLMQ_TYPE_25_67]: {
size: 25,
threshold: 67,
maximumActiveQuorumsCount: 24,
},
[constants.LLMQ_TYPES.LLMQ_TYPE_LLMQ_TEST]: {
size: 3,
threshold: 2,
maximumActiveQuorumsCount: 2,
},
[constants.LLMQ_TYPES.LLMQ_TYPE_LLMQ_DEVNET]: {
size: 12,
threshold: 6,
maximumActiveQuorumsCount: 4,
},
[constants.LLMQ_TYPES.LLMQ_TYPE_TEST_V17]: {
size: 3,
threshold: 2,
maximumActiveQuorumsCount: 2,
},
[constants.LLMQ_TYPES.LLMQ_TYPE_TEST_DIP0024]: {
size: 4,
threshold: 2,
maximumActiveQuorumsCount: 2,
},
[constants.LLMQ_TYPES.LLMQ_TYPE_TEST_INSTANTSEND]: {
size: 3,
threshold: 2,
maximumActiveQuorumsCount: 2,
},
[constants.LLMQ_TYPES.LLMQ_DEVNET_DIP0024]: {
size: 8,
threshold: 4,
maximumActiveQuorumsCount: 2,
},
[constants.LLMQ_TYPES.LLMQ_TEST_PLATFORM]: {
size: 3,
threshold: 2,
maximumActiveQuorumsCount: 2,
},
[constants.LLMQ_TYPES.LLMQ_DEVNET_PLATFORM]: {
size: 12,
threshold: 8,
maximumActiveQuorumsCount: 4,
},
};

module.exports = constants;
112 changes: 28 additions & 84 deletions lib/deterministicmnlist/QuorumEntry.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const {
* @class QuorumEntry
* @param {string|Object|Buffer} [arg] - A Buffer, JSON string,
* or Object representing a SMLQuorumEntry
* @param {Object} [llmqParamsOverride] - LLMQ type override
* @constructor
* @property {number} version
* @property {number} llmqType
Expand All @@ -57,26 +58,26 @@ const {
* @property {string} quorumSig
* @property {string} membersSig
*/
function QuorumEntry(arg) {
function QuorumEntry(arg, llmqParamsOverride = {}) {
if (arg) {
if (arg instanceof QuorumEntry) {
return arg.copy();
}

if (BufferUtil.isBuffer(arg)) {
return QuorumEntry.fromBuffer(arg);
return QuorumEntry.fromBuffer(arg, llmqParamsOverride);
}

if (_.isObject(arg)) {
return QuorumEntry.fromObject(arg);
return QuorumEntry.fromObject(arg, llmqParamsOverride);
}

if (arg instanceof QuorumEntry) {
return arg.copy();
}

if (isHexString(arg)) {
return QuorumEntry.fromHexString(arg);
return QuorumEntry.fromHexString(arg, llmqParamsOverride);
}
throw new TypeError('Unrecognized argument for QuorumEntry');
}
Expand All @@ -85,11 +86,13 @@ function QuorumEntry(arg) {
/**
* Parse buffer and returns QuorumEntry
* @param {Buffer} buffer
* @param {Object} [llmqParamsOverride]
* @return {QuorumEntry}
*/
QuorumEntry.fromBuffer = function fromBuffer(buffer) {
QuorumEntry.fromBuffer = function fromBuffer(buffer, llmqParamsOverride = {}) {
const bufferReader = new BufferReader(buffer);
const SMLQuorumEntry = new QuorumEntry();
SMLQuorumEntry.llmqParamsOverride = llmqParamsOverride;
SMLQuorumEntry.isVerified = false;
if (buffer.length < 100) {
SMLQuorumEntry.isOutdatedRPC = true;
Expand Down Expand Up @@ -159,10 +162,11 @@ QuorumEntry.fromBuffer = function fromBuffer(buffer) {

/**
* @param {string} string
* @param {Object} llmqParamsOverride
* @return {QuorumEntry}
*/
QuorumEntry.fromHexString = function fromString(string) {
return QuorumEntry.fromBuffer(Buffer.from(string, 'hex'));
QuorumEntry.fromHexString = function fromString(string, llmqParamsOverride = {}) {
return QuorumEntry.fromBuffer(Buffer.from(string, 'hex'), llmqParamsOverride);
};

/**
Expand Down Expand Up @@ -232,9 +236,10 @@ QuorumEntry.prototype.toBufferForHashing = function toBufferForHashing() {
/**
* Create SMLQuorumEntry from an object
* @param {SMLQuorumEntry} obj
* @param {Object} llmqParamsOverride
* @return {QuorumEntry}
*/
QuorumEntry.fromObject = function fromObject(obj) {
QuorumEntry.fromObject = function fromObject(obj, llmqParamsOverride = {}) {
const SMLQuorumEntry = new QuorumEntry();
SMLQuorumEntry.isVerified = false;
SMLQuorumEntry.isOutdatedRPC = false;
Expand All @@ -250,6 +255,9 @@ QuorumEntry.fromObject = function fromObject(obj) {
SMLQuorumEntry.quorumVvecHash = obj.quorumVvecHash;
SMLQuorumEntry.quorumSig = obj.quorumSig;
SMLQuorumEntry.membersSig = obj.membersSig;

SMLQuorumEntry.llmqParamsOverride = llmqParamsOverride;

if (SMLQuorumEntry.signers === undefined) {
SMLQuorumEntry.isOutdatedRPC = true;
}
Expand Down Expand Up @@ -334,88 +342,24 @@ QuorumEntry.prototype.toObject = function toObject() {
return result;
};

QuorumEntry.getParams = function getParams(llmqType) {
const params = {};
switch (llmqType) {
case constants.LLMQ_TYPES.LLMQ_TYPE_50_60:
params.size = 50;
params.threshold = 30;
params.maximumActiveQuorumsCount = 24;
return params;
case constants.LLMQ_TYPES.LLMQ_TYPE_60_75:
params.size = 60;
params.threshold = 45;
params.maximumActiveQuorumsCount = 32;
return params;
case constants.LLMQ_TYPES.LLMQ_TYPE_400_60:
params.size = 400;
params.threshold = 240;
params.maximumActiveQuorumsCount = 4;
return params;
case constants.LLMQ_TYPES.LLMQ_TYPE_400_85:
params.size = 400;
params.threshold = 340;
params.maximumActiveQuorumsCount = 4;
return params;
case constants.LLMQ_TYPES.LLMQ_TYPE_100_67:
params.size = 100;
params.threshold = 67;
params.maximumActiveQuorumsCount = 24;
return params;
case constants.LLMQ_TYPES.LLMQ_TYPE_25_67:
params.size = 25;
params.threshold = 67;
params.maximumActiveQuorumsCount = 24;
return params;
case constants.LLMQ_TYPES.LLMQ_TYPE_LLMQ_TEST:
params.size = 3;
params.threshold = 2;
params.maximumActiveQuorumsCount = 2;
return params;
case constants.LLMQ_TYPES.LLMQ_TYPE_LLMQ_DEVNET:
params.size = 12;
params.threshold = 6;
params.maximumActiveQuorumsCount = 4;
return params;
case constants.LLMQ_TYPES.LLMQ_TYPE_TEST_V17:
params.size = 3;
params.threshold = 2;
params.maximumActiveQuorumsCount = 2;
return params;
case constants.LLMQ_TYPES.LLMQ_TYPE_TEST_DIP0024:
params.size = 4;
params.threshold = 2;
params.maximumActiveQuorumsCount = 2;
return params;
case constants.LLMQ_TYPES.LLMQ_TYPE_TEST_INSTANTSEND:
params.size = 3;
params.threshold = 2;
params.maximumActiveQuorumsCount = 2;
return params;
case constants.LLMQ_TYPES.LLMQ_DEVNET_DIP0024:
params.size = 8;
params.threshold = 4;
params.maximumActiveQuorumsCount = 2;
return params;
case constants.LLMQ_TYPES.LLMQ_TEST_PLATFORM:
params.size = 3;
params.threshold = 2;
params.maximumActiveQuorumsCount = 2;
return params;
case constants.LLMQ_TYPES.LLMQ_DEVNET_PLATFORM:
params.size = 12;
params.threshold = 8;
params.maximumActiveQuorumsCount = 4;
return params;
default:
throw new Error(`Invalid llmq type ${llmqType}`);
QuorumEntry.getParams = function getParams(llmqType, llmqParamsOverride = {}) {
let llmqParams = constants.LLMQ_TYPE_PARAMS;
if (Object.keys(llmqParamsOverride).length > 0) {
llmqParams = _.merge({}, constants.LLMQ_TYPE_PARAMS, llmqParamsOverride);
}

if (!llmqParams[llmqType]) {
throw new Error(`Invalid llmq type ${llmqType}`);
}

return llmqParams[llmqType];
};

/**
* @return {number}
*/
QuorumEntry.prototype.getParams = function getParams() {
return QuorumEntry.getParams(this.llmqType);
return QuorumEntry.getParams(this.llmqType, this.llmqParamsOverride);
};

/**
Expand Down
25 changes: 19 additions & 6 deletions lib/deterministicmnlist/SimplifiedMNList.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ const Networks = require('../networks');
const Transaction = require('../transaction');
const { MASTERNODE_TYPE_HP } = require("../constants");

function SimplifiedMNList(simplifiedMNListDiff) {
/**
* @param {SimplifiedMNListDiff} simplifiedMNListDiff
* @param {Object} llmqParamsOverride
* @constructor
*/
function SimplifiedMNList(simplifiedMNListDiff = null, llmqParamsOverride = {}) {
this.baseBlockHash = constants.NULL_HASH;
this.blockHash = constants.NULL_HASH;
/**
Expand All @@ -38,6 +43,7 @@ function SimplifiedMNList(simplifiedMNListDiff) {
this.quorumsActive = false;
this.cbTx = null;
this.cbTxMerkleTree = null;
this.llmqParamsOverride = llmqParamsOverride;
if (simplifiedMNListDiff) {
this.applyDiff(simplifiedMNListDiff);
}
Expand All @@ -52,7 +58,11 @@ SimplifiedMNList.prototype.applyDiff = function applyDiff(
simplifiedMNListDiff
) {
// This will copy an instance of SimplifiedMNListDiff or create a new instance
const diff = new SimplifiedMNListDiff(simplifiedMNListDiff, this.network);
const diff = new SimplifiedMNListDiff(
simplifiedMNListDiff,
this.network,
this.llmqParamsOverride,
);

// only when we apply the first diff we set the network
if (!this.network) {
Expand Down Expand Up @@ -162,7 +172,8 @@ SimplifiedMNList.prototype.addAndMaybeRemoveQuorums =
? newGroupedQuorums[quorumType].length
: 0;
const maxAllowedQuorumsOfType = QuorumEntry.getParams(
Number(quorumType)
Number(quorumType),
this.llmqParamsOverride,
).maximumActiveQuorumsCount;

if (
Expand Down Expand Up @@ -566,15 +577,17 @@ SimplifiedMNList.prototype.toSimplifiedMNListDiff =
merkleRootQuorums: this.merkleRootQuorums,
nVersion: this.nVersion,
},
network
network,
this.llmqParamsOverride,
);
};

/**
* Recreates SML from json
* @param {Object} smlJson
* @param {Object} llmqParamsOverride
*/
SimplifiedMNList.fromJSON = function fromJSON(smlJson) {
SimplifiedMNList.fromJSON = function fromJSON(smlJson, llmqParamsOverride = {}) {
const sml = new SimplifiedMNList();
sml.baseBlockHash = smlJson.baseBlockHash;
sml.blockHash = smlJson.blockHash;
Expand All @@ -592,7 +605,7 @@ SimplifiedMNList.fromJSON = function fromJSON(smlJson) {
(mnRecord) => new SimplifiedMNListEntry(mnRecord)
);
sml.quorumList = smlJson.quorumList.map(
(quorumEntry) => new QuorumEntry(quorumEntry)
(quorumEntry) => new QuorumEntry(quorumEntry, llmqParamsOverride),
);
sml.validMNs = smlJson.validMNs.map(
(mnRecord) => new SimplifiedMNListEntry(mnRecord)
Expand Down
Loading