From 6b3b8fe950cef3ffb6fe0efc49a7766b0b9ca3cc Mon Sep 17 00:00:00 2001 From: Ivan Shumkov Date: Mon, 27 May 2024 23:02:02 +0700 Subject: [PATCH] fix: sml serialisation --- lib/constants/index.js | 1 + .../SimplifiedMNListDiff.js | 19 +++++----- .../SimplifiedMNListEntry.js | 35 +++++++++---------- 3 files changed, 27 insertions(+), 28 deletions(-) diff --git a/lib/constants/index.js b/lib/constants/index.js index 3e3c02778..d6f230d05 100644 --- a/lib/constants/index.js +++ b/lib/constants/index.js @@ -39,6 +39,7 @@ module.exports = { EMPTY_IPV4_ADDRESS: '0.0.0.0:0', CURRENT_PROTOCOL_VERSION: 70211, SML_ENTRY_VERSION_1_SIZE: 151, + SML_ENTRY_VERSION_2_MN_SIZE: 153, SML_ENTRY_TYPE_2_ADDITION_SIZE: 22, NULL_HASH: '0000000000000000000000000000000000000000000000000000000000000000', // In duffs diff --git a/lib/deterministicmnlist/SimplifiedMNListDiff.js b/lib/deterministicmnlist/SimplifiedMNListDiff.js index c01707fda..1ec0a90b4 100644 --- a/lib/deterministicmnlist/SimplifiedMNListDiff.js +++ b/lib/deterministicmnlist/SimplifiedMNListDiff.js @@ -84,21 +84,22 @@ SimplifiedMNListDiff.fromBuffer = function fromBuffer(buffer, network) { const mnListSize = bufferReader.readVarintNum(); data.mnList = []; for (let i = 0; i < mnListSize; i += 1) { + // We dropped support of version 1, because diff version was reverted + // from 2 to 1, so we don't have other choice const entryBufferParts = [ - bufferReader.read(constants.SML_ENTRY_VERSION_1_SIZE) + bufferReader.read(constants.SML_ENTRY_VERSION_2_MN_SIZE) ]; - if (data.nVersion >= 2) { - const type = bufferReader.readUInt16LE(); + const type = bufferReader.readUInt16LE(); - const typeBuffer = Buffer.alloc(2); - typeBuffer.writeUInt16LE(type); + const typeBuffer = Buffer.alloc(2); + typeBuffer.writeUInt16LE(type); - entryBufferParts.push(typeBuffer); + entryBufferParts.push(typeBuffer); - if (type === constants.MASTERNODE_TYPE_HP) { - entryBufferParts.push(bufferReader.read(constants.SML_ENTRY_TYPE_2_ADDITION_SIZE)); - } + // Read additional data for type 1 + if (type === constants.MASTERNODE_TYPE_HP) { + entryBufferParts.push(bufferReader.read(constants.SML_ENTRY_TYPE_2_ADDITION_SIZE)); } data.mnList.push( diff --git a/lib/deterministicmnlist/SimplifiedMNListEntry.js b/lib/deterministicmnlist/SimplifiedMNListEntry.js index d35e4279c..3440d5f30 100644 --- a/lib/deterministicmnlist/SimplifiedMNListEntry.js +++ b/lib/deterministicmnlist/SimplifiedMNListEntry.js @@ -32,8 +32,8 @@ const { * @property {string} pubKeyOperator - operator public key * @property {string} votingAddress * @property {boolean} isValid - * @property {number} [nVersion] - * @property {number} [nType] + * @property {number} nVersion + * @property {number} nType * @property {number} [platformHTTPPort] * @property {string} [platformNodeID] * @property {string} [payoutAddress] @@ -51,8 +51,8 @@ const { * @property {string} pubKeyOperator - operator public key * @property {string} votingAddress * @property {boolean} isValid - * @property {number} [nVersion] - * @property {number} [nType] + * @property {number} nVersion + * @property {number} nType * @property {string} [platformHTTPPort] * @property {number} [platformNodeID] * @property {string} [payoutAddress] @@ -92,6 +92,9 @@ SimplifiedMNListEntry.fromBuffer = function fromBuffer(buffer, network) { const object = { }; + // We do not support version 1 anymore that didn't have version field serialized + // Diff version was reverted from 2 to 1, so we don't have other choice + object.nVersion = bufferReader.readUInt16LE(); object.proRegTxHash = bufferReader.read(SHA256_HASH_SIZE).reverse().toString('hex'); object.confirmedHash = bufferReader .read(SHA256_HASH_SIZE) @@ -106,15 +109,10 @@ SimplifiedMNListEntry.fromBuffer = function fromBuffer(buffer, network) { object.isValid = Boolean(bufferReader.readUInt8()); - // In case of version 2, sml entry should contain more fields but - // we don't know version here - - if (!bufferReader.finished()) { - object.nType = bufferReader.readUInt16LE(); - } + object.nType = bufferReader.readUInt16LE(); + // If nType is 1 then we have HP stuff bellow if (object.nType === MASTERNODE_TYPE_HP) { - object.nVersion = 2; object.platformHTTPPort = bufferReader.readUInt16LE(); object.platformNodeID = bufferReader.read(PLATFORM_NODE_ID_SIZE).reverse().toString('hex'); } @@ -139,6 +137,9 @@ SimplifiedMNListEntry.prototype.toBuffer = function toBuffer() { this.validate(); const bufferWriter = new BufferWriter(); + // We dropped support of version 1, so we always serialise the version + // Diff version was reverted from 2 to 1, so we don't have other choice + bufferWriter.writeUInt16LE(this.nVersion); bufferWriter.write(Buffer.from(this.proRegTxHash, 'hex').reverse()); bufferWriter.write(Buffer.from(this.confirmedHash, 'hex').reverse()); bufferWriter.write(serializeIp(this.service)); @@ -148,15 +149,11 @@ SimplifiedMNListEntry.prototype.toBuffer = function toBuffer() { ); bufferWriter.writeUInt8(Number(this.isValid)); - if (this.nVersion === 2) { - if (typeof this.nType === 'number') { - bufferWriter.writeUInt16LE(this.nType); + bufferWriter.writeUInt16LE(this.nType); - if (this.nType === MASTERNODE_TYPE_HP) { - bufferWriter.writeUInt16LE(this.platformHTTPPort); - bufferWriter.write(Buffer.from(this.platformNodeID, 'hex').reverse()); - } - } + if (this.nType === MASTERNODE_TYPE_HP) { + bufferWriter.writeUInt16LE(this.platformHTTPPort); + bufferWriter.write(Buffer.from(this.platformNodeID, 'hex').reverse()); } return bufferWriter.toBuffer();