Skip to content

Commit

Permalink
feat: test using an actual tx from mainnet
Browse files Browse the repository at this point in the history
  • Loading branch information
thephez committed Sep 11, 2024
1 parent ed46e6d commit 89ef601
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 9 deletions.
21 changes: 12 additions & 9 deletions lib/transaction/payload/mnhfsignalpayload.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
};
Expand Down
27 changes: 27 additions & 0 deletions test/transaction/payload/mnhfsignalpayload.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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');
Expand Down Expand Up @@ -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);
});
});
});

0 comments on commit 89ef601

Please sign in to comment.