Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: sml serialisation #296

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/constants/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,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
Loading