Skip to content

Commit

Permalink
fix: sml serialisation
Browse files Browse the repository at this point in the history
  • Loading branch information
shumkov committed May 27, 2024
1 parent 1abe3dc commit 6b3b8fe
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 28 deletions.
1 change: 1 addition & 0 deletions lib/constants/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 10 additions & 9 deletions lib/deterministicmnlist/SimplifiedMNListDiff.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
35 changes: 16 additions & 19 deletions lib/deterministicmnlist/SimplifiedMNListEntry.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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]
Expand Down Expand Up @@ -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)
Expand All @@ -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');
}
Expand All @@ -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));
Expand All @@ -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();
Expand Down

0 comments on commit 6b3b8fe

Please sign in to comment.