Skip to content

Commit

Permalink
fix(dapi): can't parse masternode list diff (#1988)
Browse files Browse the repository at this point in the history
  • Loading branch information
shumkov authored Jul 24, 2024
1 parent 5d4f8ca commit 4d7c856
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
27 changes: 11 additions & 16 deletions packages/dapi/lib/MasternodeListSync.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const EventEmitter = require('events');
const { SimplifiedMNListDiff } = require('@dashevo/dashcore-lib');
const cbor = require('cbor');
const logger = require('./logger');

const NULL_HASH = '0000000000000000000000000000000000000000000000000000000000000000';
Expand Down Expand Up @@ -69,17 +69,14 @@ class MasternodeListSync extends EventEmitter {
async sync(blockHash, blockHeight) {
const fullDiffObject = await this.coreRpc.getMnListDiff(NULL_HASH, blockHash);

// TODO: It's a dirty hack to fix serialisation issue, introduced by reverting version of the
// diff from 2 to 1. So now version 1 of diff contains entries of version 1 and 2 and
// we don't know how to parse it since version field is introduced in version 2.
fullDiffObject.nVersion = 2;

const previousBlockHash = this.blockHash;
const previousBlockHeight = this.blockHeight;

const fullDiff = new SimplifiedMNListDiff(fullDiffObject, this.network);

this.fullDiffBuffer = fullDiff.toBuffer();
// TODO: We can't use dashcore-lib SimplifiedMNListDiff toBuffer method, because due to SML
// design it's impossible to deserialize it back without knowing of the protocol version.
// In future, we want to switch to Rust implementation of SML so we don't want to spend
// time on fixing this issue in JS dashcore-lib
this.fullDiffBuffer = await cbor.encodeAsync(fullDiffObject);
this.blockHeight = blockHeight;
this.blockHash = blockHash;

Expand All @@ -95,13 +92,11 @@ class MasternodeListSync extends EventEmitter {
if (previousBlockHash) {
const diffObject = await this.coreRpc.getMnListDiff(previousBlockHash, blockHash);

// TODO: It's a dirty hack to fix serialisation issue, introduced by reverting version of the
// diff from 2 to 1. So now version 1 of diff contains entries of version 1 and 2 and we
// don't know how to parse it since version field is introduced in version 2.
diffObject.nVersion = 2;

const diff = new SimplifiedMNListDiff(diffObject, this.network);
const diffBuffer = diff.toBuffer();
// TODO: We can't use dashcore-lib SimplifiedMNListDiff toBuffer method, because due to SML
// design it's impossible to deserialize it back without knowing of the protocol version.
// In future, we want to switch to Rust implementation of SML so we don't want to spend
// time on fixing this issue in JS dashcore-lib
const diffBuffer = await cbor.encodeAsync(diffObject);

this.logger.debug({
previousBlockHash,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const SimplifiedMNList = require('@dashevo/dashcore-lib/lib/deterministicmnlist/SimplifiedMNList');
const SimplifiedMNListDiff = require('@dashevo/dashcore-lib/lib/deterministicmnlist/SimplifiedMNListDiff');
const cbor = require('cbor');

const logger = require('../logger');

Expand Down Expand Up @@ -98,11 +99,15 @@ class SimplifiedMasternodeListProvider {
}

let simplifiedMNListDiff;
let simplifiedMNListDiffObject;
let simplifiedMNListDiffBuffer;
try {
simplifiedMNListDiffBuffer = Buffer.from(response.getMasternodeListDiff_asU8());

simplifiedMNListDiffObject = cbor.decodeFirstSync(simplifiedMNListDiffBuffer);

simplifiedMNListDiff = new SimplifiedMNListDiff(
simplifiedMNListDiffBuffer,
simplifiedMNListDiffObject,
this.options.network,
);
} catch (e) {
Expand All @@ -112,7 +117,8 @@ class SimplifiedMasternodeListProvider {
diffCount,
network: this.options.network,
error: e,
simplifiedMNListDiff: simplifiedMNListDiffBuffer.toString('hex'),
simplifiedMNListDiffObject,
simplifiedMNListDiffBytes: simplifiedMNListDiffBuffer.toString('hex'),
},
);

Expand Down

0 comments on commit 4d7c856

Please sign in to comment.