All files / src/tx/applyTx checkPeriodVote.js

100% Statements 20/20
100% Branches 17/17
100% Functions 1/1
100% Lines 20/20

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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58              1x 1x 1x   1x 10x 1x     9x   9x 1x     8x         2x         6x 1x         5x   5x       2x         2x     3x   3x 3x    
/**
 * Copyright (c) 2018-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 { Type } = require('leap-core');
const { bufferToHex } = require('ethereumjs-util');
const { logNode } = require('../../utils/debug');
 
module.exports = async (state, tx, bridgeState) => {
  if (tx.type !== Type.PERIOD_VOTE) {
    throw new Error('[period vote] periodVote tx expected');
  }
 
  const { slotId } = tx.options;
 
  if (!state.slots[slotId]) {
    throw new Error(`[period vote] Slot ${slotId} is empty`);
  }
 
  if (
    !tx.inputs ||
    !tx.inputs[0].signer ||
    tx.inputs[0].signer !== state.slots[slotId].signerAddr
  ) {
    throw new Error(
      `[period vote] Input should be signed by validator: ${state.slots[slotId].signerAddr}`
    );
  }
 
  if (tx.inputs[0].prevout.index !== 0) {
    throw new Error(
      `[period vote] Input should have prevout index of 0. Got: ${tx.inputs[0].prevout.index}`
    );
  }
 
  const blocksRoot = bufferToHex(tx.inputs[0].prevout.hash);
 
  if (
    !bridgeState.periodProposal ||
    bridgeState.periodProposal.blocksRoot !== blocksRoot
  ) {
    logNode(
      `[period vote] Vote for different period. Proposed root: ${
        (bridgeState.periodProposal || {}).blocksRoot
      }. Voted root: ${blocksRoot}`
    );
    return;
  }
 
  const votes = new Set(bridgeState.periodProposal.votes);
 
  votes.add(slotId);
  bridgeState.periodProposal.votes = [...votes];
};