Skip to content

Commit cad826f

Browse files
authored
Fix for signers size of quorum commitment transaction (#132)
* Fix commitment tx payload signers size serialization and adjusted tests * Bump the version
1 parent 491e3c6 commit cad826f

File tree

4 files changed

+56
-37
lines changed

4 files changed

+56
-37
lines changed

lib/transaction/payload/commitmenttxpayload.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ var Preconditions = require('../../util/preconditions');
77
var BufferWriter = require('../../encoding/bufferwriter');
88
var BufferReader = require('../../encoding/bufferreader');
99
var AbstractPayload = require('./abstractpayload');
10-
var Script = require('../../script');
1110

1211
var CURRENT_PAYLOAD_VERSION = 1;
1312

@@ -83,10 +82,12 @@ CommitmentTxPayload.fromBuffer = function fromBuffer(rawPayload) {
8382
payload.quorumHash = payloadBufferReader.read(constants.SHA256_HASH_SIZE).toString('hex');
8483

8584
payload.signersSize = payloadBufferReader.readVarintNum();
86-
payload.signers = payloadBufferReader.read(Math.floor((payload.signersSize + 7) / 8)).toString('hex');
85+
var signersBytesToRead = Math.floor((payload.signersSize + 7) / 8) || 1;
86+
payload.signers = payloadBufferReader.read(signersBytesToRead).toString('hex');
8787

8888
payload.validMembersSize = payloadBufferReader.readVarintNum();
89-
payload.validMembers = payloadBufferReader.read(Math.floor((payload.validMembersSize + 7) / 8)).toString('hex');
89+
var validMembersBytesToRead = Math.floor((payload.validMembersSize + 7) / 8) || 1;
90+
payload.validMembers = payloadBufferReader.read(validMembersBytesToRead).toString('hex');
9091

9192
payload.quorumPublicKey = payloadBufferReader.read(constants.BLS_PUBLIC_KEY_SIZE).toString('hex');
9293
payload.quorumVvecHash = payloadBufferReader.read(constants.SHA256_HASH_SIZE).toString('hex');
@@ -144,7 +145,7 @@ CommitmentTxPayload.prototype.toJSON = function toJSON(options) {
144145
qfcVersion: this.qfcVersion,
145146
llmqtype: this.llmqtype,
146147
quorumHash: this.quorumHash,
147-
signerSize: this.signerSize,
148+
signersSize: this.signersSize,
148149
signers: this.signers,
149150
validMembersSize: this.validMembersSize,
150151
validMembers: this.validMembers,
@@ -188,7 +189,7 @@ CommitmentTxPayload.prototype.toBuffer = function toBuffer(options) {
188189
validMemberSizeLength = 10;
189190
break;
190191
default:
191-
throw new Error(`Invalid llmq type ${this.llmqtype}`);
192+
throw new Error('Invalid llmq type ' + this.llmqtype);
192193
}
193194

194195
var payloadBufferWriter = new BufferWriter();
@@ -198,14 +199,14 @@ CommitmentTxPayload.prototype.toBuffer = function toBuffer(options) {
198199
.writeUInt16LE(this.qfcVersion)
199200
.writeUInt8(this.llmqtype)
200201
.write(Buffer.from(this.quorumHash, 'hex'))
201-
.writeVarintNum(signerSizeLength)
202+
.writeVarintNum(this.signersSize)
202203
.write(Buffer.from(this.signers, 'hex'))
203-
.writeVarintNum(validMemberSizeLength)
204+
.writeVarintNum(this.validMembersSize)
204205
.write(Buffer.from(this.validMembers, 'hex'))
205206
.write(Buffer.from(this.quorumPublicKey, 'hex'))
206207
.write(Buffer.from(this.quorumVvecHash, 'hex'))
207208
.write(Buffer.from(this.quorumSig, 'hex'))
208-
.write(Buffer.from(this.sig, 'hex'))
209+
.write(Buffer.from(this.sig, 'hex'));
209210

210211
return payloadBufferWriter.toBuffer();
211212
};

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@dashevo/dashcore-lib",
3-
"version": "0.17.6",
3+
"version": "0.17.7",
44
"description": "A pure and powerful JavaScript Dash library.",
55
"author": "Dash Core Group, Inc. <dev@dash.org>",
66
"main": "index.js",

test/transaction/payload/commitmenttxpayload.js

Lines changed: 45 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,32 @@ var sinon = require('sinon');
66

77
var DashcoreLib = require('../../../index');
88

9-
var Script = DashcoreLib.Script;
9+
var Transaction = DashcoreLib.Transaction;
1010
var CommitmentTxPayload = DashcoreLib.Transaction.Payload.CommitmentTxPayload;
1111

1212
var validCommitmentTxPayloadJSON = {
1313
version: 1,
14-
height: 279326,
14+
height: 137051,
1515
qfcVersion: 1,
1616
llmqtype: 1,
17-
quorumHash: '4cb9568141dded955ed90345b393fc62634be7d260ca328b89afdd0500000000',
17+
quorumHash: "f015624411254dd5a806e16310e7b9989e8224796f57e42bfb01420200000000",
1818
signersSize: 50,
19-
signers: '00000000000000',
19+
signers: "ffffffffffff03",
2020
validMembersSize: 50,
21-
validMembers: '00000000000000',
22-
quorumPublicKey: '000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
23-
quorumVvecHash: '0000000000000000000000000000000000000000000000000000000000000000',
24-
quorumSig: '000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
25-
sig: '000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
21+
validMembers: "ffffffffffff03",
22+
quorumPublicKey: "0ea4465cc4b2e6890e5bd37987822cabf1b79ec8dece016a3be75802482480af29f7961281405d2cb3d6e38157bda824",
23+
quorumVvecHash: "e3107d64e8d2bec7dff1f6d7fd1a9d31a181c82acaf052f8d27b71507fe2db12",
24+
quorumSig: "0f0a203004f025076c7c0d989464013b31a0104155946968317e6abcfafbe70a53c2b57811f0ebe888cef36eacf649401497c569a9f617c3eeaa7eacbc45969389fa229355097abe277f2452792a5f2e84e804bf2eac0843f01690fb6bd10df1",
25+
sig: "195f7a49aed846bcf475a4a3500b15a626c514c9223b46ce6fef505f19af8f8e1cec554cb63a04125c2d19bf0211ea4c15f289f1413b8618abdf7295e44fb196e326a803d9d7c7c69a12314cf88e463653fde5fecb1f8c3d6791180cdc382392"
2626
};
2727

28-
var validCommitmentTxPayloadHexString = '01001e430400010001f2a1f356b9e086220d38754b1de1e4dcbd8b080c3fa0a62c2bd0961400000000320000000000000032000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000';
29-
var payload = CommitmentTxPayload.fromBuffer(Buffer.from(validCommitmentTxPayloadHexString, 'hex'));
28+
var testNetPayloadHex = '01005b170200010001f015624411254dd5a806e16310e7b9989e8224796f57e42bfb0142020000000032ffffffffffff0332ffffffffffff030ea4465cc4b2e6890e5bd37987822cabf1b79ec8dece016a3be75802482480af29f7961281405d2cb3d6e38157bda824e3107d64e8d2bec7dff1f6d7fd1a9d31a181c82acaf052f8d27b71507fe2db120f0a203004f025076c7c0d989464013b31a0104155946968317e6abcfafbe70a53c2b57811f0ebe888cef36eacf649401497c569a9f617c3eeaa7eacbc45969389fa229355097abe277f2452792a5f2e84e804bf2eac0843f01690fb6bd10df1195f7a49aed846bcf475a4a3500b15a626c514c9223b46ce6fef505f19af8f8e1cec554cb63a04125c2d19bf0211ea4c15f289f1413b8618abdf7295e44fb196e326a803d9d7c7c69a12314cf88e463653fde5fecb1f8c3d6791180cdc382392';
29+
var testNetCommitmentTxHex = '03000600000000000000fd490101005b170200010001f015624411254dd5a806e16310e7b9989e8224796f57e42bfb0142020000000032ffffffffffff0332ffffffffffff030ea4465cc4b2e6890e5bd37987822cabf1b79ec8dece016a3be75802482480af29f7961281405d2cb3d6e38157bda824e3107d64e8d2bec7dff1f6d7fd1a9d31a181c82acaf052f8d27b71507fe2db120f0a203004f025076c7c0d989464013b31a0104155946968317e6abcfafbe70a53c2b57811f0ebe888cef36eacf649401497c569a9f617c3eeaa7eacbc45969389fa229355097abe277f2452792a5f2e84e804bf2eac0843f01690fb6bd10df1195f7a49aed846bcf475a4a3500b15a626c514c9223b46ce6fef505f19af8f8e1cec554cb63a04125c2d19bf0211ea4c15f289f1413b8618abdf7295e44fb196e326a803d9d7c7c69a12314cf88e463653fde5fecb1f8c3d6791180cdc382392';
30+
var testNetCommitmentTxHash = 'cf4b01ce796a4f37e6af6dd62671b608160d4a39acf3bfe95f9a59d1854b63f3';
31+
32+
function getPayloadBuffer() {
33+
return Buffer.from(testNetPayloadHex, 'hex');
34+
}
3035

3136
function checkValidJSON(payload) {
3237
expect(payload.version).to.be.equal(validCommitmentTxPayloadJSON.version);
@@ -45,9 +50,6 @@ function checkValidJSON(payload) {
4550

4651
describe('CommitmentTxPayload', function () {
4752

48-
var payload = null;
49-
var payloadBuffer = null;
50-
5153
describe('.fromJSON', function () {
5254

5355
beforeEach(function () {
@@ -59,7 +61,7 @@ describe('CommitmentTxPayload', function () {
5961
});
6062

6163
it('Should return instance of CommitmentTxPayload and call #validate on it', function () {
62-
payload = CommitmentTxPayload.fromJSON(validCommitmentTxPayloadJSON);
64+
var payload = CommitmentTxPayload.fromJSON(validCommitmentTxPayloadJSON);
6365
checkValidJSON(payload);
6466
});
6567
});
@@ -69,33 +71,33 @@ describe('CommitmentTxPayload', function () {
6971
sinon.spy(CommitmentTxPayload.prototype, 'validate');
7072
});
7173

72-
it('Should return payload buffer of specific length', function () {
74+
after(function () {
75+
CommitmentTxPayload.prototype.validate.restore();
76+
});
7377

78+
it('Should return payload buffer of specific length', function () {
79+
var payload = CommitmentTxPayload.fromBuffer(getPayloadBuffer());
7480
//Manually calculated from validCommitmentTxPayloadJSON
75-
var expectedBufferLength = 329;
81+
var expectedBufferLength = getPayloadBuffer().length;
7682

77-
payloadBuffer = payload.toBuffer();
83+
var payloadBuffer = payload.toBuffer();
7884
expect(payloadBuffer.length).to.be.equal(expectedBufferLength);
7985
});
80-
81-
after(function () {
82-
CommitmentTxPayload.prototype.validate.restore();
83-
})
8486
});
8587

8688
describe('.fromBuffer', function () {
8789
before(function () {
8890
sinon.spy(CommitmentTxPayload.prototype, 'validate');
8991
});
9092

93+
after(function () {
94+
CommitmentTxPayload.prototype.validate.restore();
95+
});
96+
9197
it('Should return payload from buffer', function () {
92-
var payloadFromBuffer = CommitmentTxPayload.fromBuffer(payloadBuffer);
98+
var payloadFromBuffer = CommitmentTxPayload.fromBuffer(getPayloadBuffer());
9399
checkValidJSON(payloadFromBuffer);
94100
});
95-
96-
after(function () {
97-
CommitmentTxPayload.prototype.validate.restore();
98-
})
99101
});
100102

101103
describe('#toJSON', function () {
@@ -108,8 +110,24 @@ describe('CommitmentTxPayload', function () {
108110
});
109111

110112
it('Should be able to serialize payload JSON', function () {
113+
var payload = CommitmentTxPayload.fromBuffer(getPayloadBuffer());
111114
var payloadJSON = payload.toJSON();
112115
checkValidJSON(payloadJSON);
113116
});
114117
});
118+
119+
describe('#toString', function () {
120+
it('Should serizlize to the same hex string', function () {
121+
var expectedCommitementHex = '03000600000000000000fd3d01010009020000010064b5fc275d292adda4e5c454e93f4bc47d2c77975fb0d06bb07bd6bada068bdf0c050005000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000';
122+
var parsed = new Transaction(expectedCommitementHex);
123+
console.log(parsed.hash);
124+
var actualCommitementHex = parsed.toString();
125+
expect(actualCommitementHex).to.be.equal(expectedCommitementHex);
126+
127+
var testnetParsedTransaction = new Transaction(testNetCommitmentTxHex);
128+
var ped = testnetParsedTransaction.extraPayload.toJSON();
129+
expect(testnetParsedTransaction.toString()).to.be.equal(testNetCommitmentTxHex);
130+
expect(testnetParsedTransaction.hash).to.be.equal(testNetCommitmentTxHash);
131+
});
132+
});
115133
});

0 commit comments

Comments
 (0)