Skip to content

Commit

Permalink
Merge branch 'master' of github.com:dashevo/dashcore-lib
Browse files Browse the repository at this point in the history
  • Loading branch information
shumkov committed Apr 29, 2022
2 parents a5947f4 + 399909b commit 04529ba
Show file tree
Hide file tree
Showing 6 changed files with 404 additions and 7 deletions.
4 changes: 3 additions & 1 deletion lib/constants/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ module.exports = {
// Params might differ when -llmqtestparams is used
LLMQ_TYPE_LLMQ_TEST: 100,

// 10 members, 6 (60%) threshold, one per hour (24 blocks)
// 12 members, 6 (60%) threshold, one per hour (24 blocks)
// Params might differ when -llmqdevnetparams is used
LLMQ_TYPE_LLMQ_DEVNET: 101,

Expand All @@ -83,4 +83,6 @@ module.exports = {

// keep diffs for 30 hours (720 blocks)
SMLSTORE_MAX_DIFFS: 720,

HASH_QUORUM_INDEX_REQUIRED_VERSION: 2,
};
53 changes: 48 additions & 5 deletions lib/deterministicmnlist/QuorumEntry.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,19 @@ const {
isHexaString: isHexString,
} = utils;

const { BLS_PUBLIC_KEY_SIZE, BLS_SIGNATURE_SIZE, SHA256_HASH_SIZE } = constants;
const {
BLS_PUBLIC_KEY_SIZE,
BLS_SIGNATURE_SIZE,
SHA256_HASH_SIZE,
HASH_QUORUM_INDEX_REQUIRED_VERSION,
} = constants;

/**
* @typedef {Object} SMLQuorumEntry
* @property {number} version
* @property {number} llmqType
* @property {string} quorumHash
* @property {number} [quorumIndex]
* @property {number} signersCount
* @property {string} signers
* @property {number} validMembersCount
Expand All @@ -41,6 +47,7 @@ const { BLS_PUBLIC_KEY_SIZE, BLS_SIGNATURE_SIZE, SHA256_HASH_SIZE } = constants;
* @property {number} version
* @property {number} llmqType
* @property {string} quorumHash
* @property {number} [quorumIndex]
* @property {number} signersCount
* @property {string} signers
* @property {number} validMembersCount
Expand Down Expand Up @@ -92,6 +99,11 @@ QuorumEntry.fromBuffer = function fromBuffer(buffer) {
.read(constants.SHA256_HASH_SIZE)
.reverse()
.toString('hex');

if (this.version >= HASH_QUORUM_INDEX_REQUIRED_VERSION) {
SMLQuorumEntry.quorumIndex = buffer.readInt16LE();
}

SMLQuorumEntry.signersCount = bufferReader.readVarintNum();
SMLQuorumEntry.validMembersCount = bufferReader.readVarintNum();
SMLQuorumEntry.quorumPublicKey = bufferReader
Expand All @@ -107,6 +119,11 @@ QuorumEntry.fromBuffer = function fromBuffer(buffer) {
.read(constants.SHA256_HASH_SIZE)
.reverse()
.toString('hex');

if (this.version >= HASH_QUORUM_INDEX_REQUIRED_VERSION) {
SMLQuorumEntry.quorumIndex = bufferReader.readInt16LE();
}

SMLQuorumEntry.signersCount = bufferReader.readVarintNum();
const signersBytesToRead =
Math.floor((SMLQuorumEntry.getParams().size + 7) / 8) || 1;
Expand Down Expand Up @@ -155,7 +172,13 @@ QuorumEntry.prototype.toBuffer = function toBuffer() {
bufferWriter.writeUInt16LE(this.version);
bufferWriter.writeUInt8(this.llmqType);
bufferWriter.write(Buffer.from(this.quorumHash, 'hex').reverse());

if (this.version >= HASH_QUORUM_INDEX_REQUIRED_VERSION) {
bufferWriter.writeInt16LE(this.quorumIndex);
}

bufferWriter.writeVarintNum(this.signersCount);

if (this.isOutdatedRPC) {
bufferWriter.writeVarintNum(this.validMembersCount);
bufferWriter.write(Buffer.from(this.quorumPublicKey, 'hex'));
Expand Down Expand Up @@ -185,6 +208,11 @@ QuorumEntry.prototype.toBufferForHashing = function toBufferForHashing() {
bufferWriter.writeUInt16LE(this.version);
bufferWriter.writeUInt8(this.llmqType);
bufferWriter.write(Buffer.from(this.quorumHash, 'hex').reverse());

if (this.version >= HASH_QUORUM_INDEX_REQUIRED_VERSION) {
bufferWriter.writeInt16LE(this.quorumIndex);
}

bufferWriter.writeVarintNum(fixedCounterLength);
bufferWriter.write(Buffer.from(this.signers, 'hex'));
bufferWriter.writeVarintNum(fixedCounterLength);
Expand All @@ -209,6 +237,7 @@ QuorumEntry.fromObject = function fromObject(obj) {
SMLQuorumEntry.version = obj.version;
SMLQuorumEntry.llmqType = obj.llmqType;
SMLQuorumEntry.quorumHash = obj.quorumHash;
SMLQuorumEntry.quorumIndex = obj.quorumIndex;
SMLQuorumEntry.signersCount = obj.signersCount;
SMLQuorumEntry.signers = obj.signers;
SMLQuorumEntry.validMembersCount = obj.validMembersCount;
Expand Down Expand Up @@ -237,6 +266,14 @@ QuorumEntry.prototype.validate = function validate() {
isSha256(this.quorumHash),
'Expected quorumHash to be a sha256 hex string'
);

if (this.version >= HASH_QUORUM_INDEX_REQUIRED_VERSION) {
$.checkArgument(
Number.isInteger(this.quorumIndex),
'Expected quorumIndex to be an integer'
);
}

$.checkArgument(
isUnsignedInteger(this.signersCount),
'Expect signersCount to be an unsigned integer'
Expand Down Expand Up @@ -274,7 +311,7 @@ QuorumEntry.prototype.validate = function validate() {
};

QuorumEntry.prototype.toObject = function toObject() {
return {
const result = {
version: this.version,
llmqType: this.llmqType,
quorumHash: this.quorumHash,
Expand All @@ -287,6 +324,12 @@ QuorumEntry.prototype.toObject = function toObject() {
quorumSig: this.quorumSig,
membersSig: this.membersSig,
};

if (this.version >= HASH_QUORUM_INDEX_REQUIRED_VERSION) {
result.quorumIndex = this.quorumIndex;
}

return result;
};

QuorumEntry.getParams = function getParams(llmqType) {
Expand Down Expand Up @@ -323,9 +366,9 @@ QuorumEntry.getParams = function getParams(llmqType) {
params.maximumActiveQuorumsCount = 2;
return params;
case constants.LLMQ_TYPES.LLMQ_TYPE_LLMQ_DEVNET:
params.size = 10;
params.threshold = 3;
params.maximumActiveQuorumsCount = 7;
params.size = 12;
params.threshold = 6;
params.maximumActiveQuorumsCount = 4;
return params;
case constants.LLMQ_TYPES.LLMQ_TYPE_TEST_V17:
params.size = 3;
Expand Down
11 changes: 11 additions & 0 deletions lib/encoding/bufferreader.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,17 @@ BufferReader.prototype.readUInt64BEBN = function () {
return bn;
};

/**
* Read the next Int16LE
*
* @return {number}
*/
BufferReader.prototype.readInt16LE = function () {
var val = this.buf.readInt16LE(this.pos);
this.pos = this.pos + 2;
return val;
}

/**
* Read the next UInt64LEBN
* @return {number}
Expand Down
12 changes: 12 additions & 0 deletions lib/encoding/bufferwriter.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,18 @@ BufferWriter.prototype.writeUInt32BE = function (n) {
return this;
};

/**
* Write a Int32LE to the local buffer array
* @param {number} n
* @return {BufferWriter}
*/
BufferWriter.prototype.writeInt16LE = function (n) {
var buf = Buffer.alloc(2);
buf.writeInt16LE(n, 0);
this.write(buf);
return this;
};

/**
* Write a Int32LE to the local buffer array
* @param {number} n
Expand Down
Loading

0 comments on commit 04529ba

Please sign in to comment.