From 89ef601d6bc9ba97cec4998c36bc98a3085c3f0b Mon Sep 17 00:00:00 2001 From: thephez Date: Wed, 11 Sep 2024 15:13:19 -0400 Subject: [PATCH] feat: test using an actual tx from mainnet --- lib/transaction/payload/mnhfsignalpayload.js | 21 ++++++++------- test/transaction/payload/mnhfsignalpayload.js | 27 +++++++++++++++++++ 2 files changed, 39 insertions(+), 9 deletions(-) diff --git a/lib/transaction/payload/mnhfsignalpayload.js b/lib/transaction/payload/mnhfsignalpayload.js index f83aee27f..07aa63e9b 100644 --- a/lib/transaction/payload/mnhfsignalpayload.js +++ b/lib/transaction/payload/mnhfsignalpayload.js @@ -55,13 +55,14 @@ MnHfSignalPayload.fromBuffer = function (rawPayload) { payload.version = payloadBufferReader.readUInt8(); payload.signal.versionBit = payloadBufferReader.readUInt8(); - payload.signal.quorumHash = payloadBufferReader.read(32).reverse().toString('hex'); + + // Reverse the quorumHash to correct the byte order (from little-endian to big-endian) + payload.signal.quorumHash = payloadBufferReader.read(32).toString('hex'); + payload.signal.sig = payloadBufferReader.read(96).toString('hex'); if (!payloadBufferReader.finished()) { - throw new Error( - 'Failed to parse payload: raw payload is bigger than expected.' - ); + throw new Error('Failed to parse payload: raw payload is bigger than expected.'); } payload.validate(); @@ -143,11 +144,13 @@ MnHfSignalPayload.prototype.toBuffer = function toBuffer() { this.validate(); var payloadBufferWriter = new BufferWriter(); - payloadBufferWriter - .writeUInt8(this.version) - .writeUInt8(this.signal.versionBit) - .write(Buffer.from(this.signal.quorumHash, 'hex').reverse()) - .write(Buffer.from(this.signal.sig, 'hex')); + payloadBufferWriter.writeUInt8(this.version); + payloadBufferWriter.writeUInt8(this.signal.versionBit); + + // Reverse the quorumHash to write in little-endian format + payloadBufferWriter.write(Buffer.from(this.signal.quorumHash, 'hex')); + + payloadBufferWriter.write(Buffer.from(this.signal.sig, 'hex')); return payloadBufferWriter.toBuffer(); }; diff --git a/test/transaction/payload/mnhfsignalpayload.js b/test/transaction/payload/mnhfsignalpayload.js index b97380007..5db4db10a 100644 --- a/test/transaction/payload/mnhfsignalpayload.js +++ b/test/transaction/payload/mnhfsignalpayload.js @@ -8,6 +8,7 @@ var DashcoreLib = require('../../../index'); var MnHfSignalPayload = DashcoreLib.Transaction.Payload.MnHfSignalPayload; +// Valid test data based on an actual MnHfSignal var validMnHfSignalPayloadJSON = { version: 1, signal: { @@ -22,7 +23,12 @@ var validMnHfSignalPayload = MnHfSignalPayload.fromJSON(validMnHfSignalPayloadJS var validMnHfSignalPayloadBuffer = validMnHfSignalPayload.toBuffer(); var validMnHfSignalPayloadHexString = validMnHfSignalPayloadBuffer.toString('hex'); +// An example of an actual MnHfSignal in hex format +var mainnetMnHfSignalHex = '010a00000000000000107c98e94bdb9ffb729fe9c190a6d1223fd9b6700ccb79b627a12bc6a3d43e76fd6ab5d48dff11998811747bf51ffe722d9fda93ae892e4b18a716f58045c86459d0dafd38ae1f7f520519002983fc307e92fa606c3eb5ac8cf6ca03a102889866d58c9207b483e0b975baee63c1202209293ff7393222f812'; +var mainnetMnHfSignalBuffer = Buffer.from(mainnetMnHfSignalHex, 'hex'); + describe('MnHfSignalPayload', function () { + describe('.fromBuffer', function () { beforeEach(function () { sinon.spy(MnHfSignalPayload.prototype, 'validate'); @@ -200,4 +206,25 @@ describe('MnHfSignalPayload', function () { expect(payload.validate.callCount).to.be.equal(1); }); }); + + describe('Actual MnHfSignalHex parsing', function () { + it('Should parse an actual MnHfSignal (hex) into MnHfSignalPayload', function () { + var payload = MnHfSignalPayload.fromBuffer(mainnetMnHfSignalBuffer); + + expect(payload).to.be.an.instanceOf(MnHfSignalPayload); + expect(payload.version).to.be.equal(1); + expect(payload.signal.versionBit).to.be.equal(10); + expect(payload.signal.quorumHash).to.be.equal('00000000000000107c98e94bdb9ffb729fe9c190a6d1223fd9b6700ccb79b627'); + expect(payload.signal.sig).to.be.equal(validMnHfSignalPayloadJSON.signal.sig); + }); + + it('Should serialize MnHfSignalPayload back to hex and match the original MnHfSignal hex', function () { + var payload = MnHfSignalPayload.fromBuffer(mainnetMnHfSignalBuffer); + + var serializedBuffer = payload.toBuffer(); + var serializedHex = serializedBuffer.toString('hex'); + + expect(serializedHex).to.be.equal(mainnetMnHfSignalHex); + }); + }); });