All files / src/validator/periods checkEnoughVotes.js

100% Statements 6/6
100% Branches 4/4
100% Functions 1/1
100% Lines 6/6

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32              3x                   3x 8x   8x     8x   8x            
/**
 * Copyright (c) 2019-present, Leap DAO (leapdao.org)
 *
 * This source code is licensed under the Mozilla Public License Version 2.0
 * found in the LICENSE file in the root directory of this source tree.
 */
 
const getActiveSlots = require('../../utils/getActiveSlots');
 
/**
 * Checks if a given period root voted for by at least 2/3 of validator set
 * @returns {Object} Object with results like
 * { result:boolean, votes:number, needed:number }
 * where
 * - `votes` is a number of votes for a given period,
 * - `needed` is a minimum number of votes needed for consensus
 */
module.exports = (blocksRoot, periodProposal, slots) => {
  const activeSlots = getActiveSlots(slots);
  const votes =
    periodProposal && periodProposal.blocksRoot === blocksRoot
      ? periodProposal.votes.length
      : 0;
  const needed = Math.floor((activeSlots.length * 2) / 3) + 1;
 
  return {
    result: votes >= needed,
    votes,
    needed,
  };
};