Skip to content

Commit

Permalink
fix: can't parse CommitmentTxPayload (#252)
Browse files Browse the repository at this point in the history
  • Loading branch information
shumkov authored May 2, 2022
1 parent a187c20 commit 2781060
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion lib/transaction/payload/commitmenttxpayload.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var CURRENT_PAYLOAD_VERSION = 1;
* @property {number} qfcVersion uint16_t 2 Version of the final commitment message
* @property {number} llmqtype uint8_t 1 type of the long living masternode quorum
* @property {string} quorumHash uint256 32 The quorum identifier
* @property {number} quorumIndex int16 2 The quorum index
* @property {number} signersSize compactSize uint 1-9 Bit size of the signers bitvector
* @property {string} signers byte[] (bitSize + 7) / 8 Bitset representing the aggregated signers of this final commitment
* @property {number} validMembersSize compactSize uint 1-9 Bit size of the validMembers bitvector
Expand All @@ -35,6 +36,7 @@ var CURRENT_PAYLOAD_VERSION = 1;
* @property {number} qfcVersion
* @property {number} llmqtype
* @property {string} quorumHash
* @property {number} quorumIndex
* @property {number} signersSize
* @property {string} signers
* @property {number} validMembersSize
Expand All @@ -54,6 +56,7 @@ function CommitmentTxPayload(options) {
this.qfcVersion = options.qfcVersion;
this.llmqtype = options.llmqtype;
this.quorumHash = options.quorumHash;
this.quorumIndex = options.quorumIndex;
this.signers = options.signers;
this.validMembers = options.validMembers;
this.quorumPublicKey = options.quorumPublicKey;
Expand Down Expand Up @@ -84,6 +87,10 @@ CommitmentTxPayload.fromBuffer = function fromBuffer(rawPayload) {
.read(constants.SHA256_HASH_SIZE)
.toString('hex');

if (payload.version >= constants.HASH_QUORUM_INDEX_REQUIRED_VERSION) {
payload.quorumIndex = payloadBufferReader.readInt16LE();
}

payload.signersSize = payloadBufferReader.readVarintNum();
var signersBytesToRead = Math.floor((payload.signersSize + 7) / 8) || 1;
payload.signers = payloadBufferReader
Expand Down Expand Up @@ -157,6 +164,14 @@ CommitmentTxPayload.prototype.validate = function () {
utils.isHexaString(this.quorumHash),
'Expect quorumHash to be a hex string'
);

if (this.version >= constants.HASH_QUORUM_INDEX_REQUIRED_VERSION) {
Preconditions.checkArgument(
Number.isInteger(this.quorumIndex),
'Expect quorumHash to be an integer'
);
}

Preconditions.checkArgument(
utils.isHexaString(this.signers),
'Expect signers to be a hex string'
Expand Down Expand Up @@ -206,6 +221,10 @@ CommitmentTxPayload.prototype.toJSON = function toJSON(options) {
sig: this.sig,
};

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

return payloadJSON;
};

Expand All @@ -226,7 +245,13 @@ CommitmentTxPayload.prototype.toBuffer = function toBuffer(options) {
.writeUInt32LE(this.height)
.writeUInt16LE(this.qfcVersion)
.writeUInt8(this.llmqtype)
.write(Buffer.from(this.quorumHash, 'hex'))
.write(Buffer.from(this.quorumHash, 'hex'));

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

payloadBufferWriter
.writeVarintNum(this.signersSize)
.write(Buffer.from(this.signers, 'hex'))
.writeVarintNum(this.validMembersSize)
Expand Down

0 comments on commit 2781060

Please sign in to comment.