Skip to content

Commit

Permalink
fix: crash is there's no signatory candidate when verifying chainlock (
Browse files Browse the repository at this point in the history
  • Loading branch information
antouhou committed Dec 24, 2020
1 parent bcaa7f4 commit 8bec51b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 7 deletions.
17 changes: 12 additions & 5 deletions lib/chainlock/chainlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,14 @@ class ChainLock {
*/
async verifySignatureWithQuorumOffset(smlStore, requestId, offset) {
const candidateSignatoryQuorum = this.selectSignatoryQuorum(smlStore, requestId, offset);
let result = false;

// Logic taken from dashsync-iOS
// https://github.com/dashevo/dashsync-iOS/blob/master/DashSync/Models/Chain/DSChainLock.m#L148-L185
// first try with default offset
let result = await this.verifySignatureAgainstQuorum(candidateSignatoryQuorum, requestId);
if (candidateSignatoryQuorum != null) {
// Logic taken from dashsync-iOS
// https://github.com/dashevo/dashsync-iOS/blob/master/DashSync/Models/Chain/DSChainLock.m#L148-L185
// first try with default offset
result = await this.verifySignatureAgainstQuorum(candidateSignatoryQuorum, requestId);
}

// second try with 0 offset, else with double offset
if (!result && offset === constants.LLMQ_SIGN_HEIGHT_OFFSET) {
Expand Down Expand Up @@ -242,14 +245,18 @@ class ChainLock {
* @param {SimplifiedMNListStore} smlStore - used to reconstruct quorum lists
* @param {Buffer} requestId
* @param {number} offset
* @returns {QuorumEntry} - signatoryQuorum
* @returns {QuorumEntry|null} - signatoryQuorum
*/
selectSignatoryQuorum(smlStore, requestId, offset) {
const chainlockSML = smlStore.getSMLbyHeight(this.height - offset);
const scoredQuorums = chainlockSML.calculateSignatoryQuorumScores(
chainlockSML.getChainlockLLMQType(), requestId,
);

if (scoredQuorums.length === 0) {
return null;
}

scoredQuorums.sort((a, b) => Buffer.compare(a.score, b.score));
return scoredQuorums[0].quorum;
}
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@dashevo/dashcore-lib",
"version": "0.19.14",
"version": "0.19.15",
"description": "A pure and powerful JavaScript Dash library.",
"author": "Dash Core Group, Inc. <[email protected]>",
"main": "index.js",
Expand Down
14 changes: 14 additions & 0 deletions test/chainlock/chainlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ const expect = chai.expect;

const bitcore = require('../../index');
const SimplifiedMNListStore = require('../../lib/deterministicmnlist/SimplifiedMNListStore');
const SimplifiedMNList = require('../../lib/deterministicmnlist/SimplifiedMNList');
const SMNListFixture = require('../fixtures/mnList');
const ChainLock = bitcore.ChainLock;
const QuorumEntry = bitcore.QuorumEntry;
const Networks = bitcore.Networks;

describe('ChainLock', function () {
let object;
Expand Down Expand Up @@ -167,6 +169,18 @@ describe('ChainLock', function () {
const isValid = await chainLock.verify(SMLStore);
expect(isValid).to.equal(true);
});
it("should return false if SML store does not have a signatory candidate", async function () {
const chainLock = new ChainLock(buf4);
const smlDiffArray = SMNListFixture.getChainlockDiffArray();
const SMLStore = new SimplifiedMNListStore(smlDiffArray);
SMLStore.getSMLbyHeight = function () {
var sml = new SimplifiedMNList();
sml.network = Networks.testnet;
return sml;
};
const isValid = await chainLock.verify(SMLStore);
expect(isValid).to.equal(false);
});
});
});

Expand Down

0 comments on commit 8bec51b

Please sign in to comment.