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(dapi): can't parse masternode list diff #1988

Merged
merged 2 commits into from
Jul 24, 2024
Merged
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
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
Loading