# 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

    1x
    1x

    1x
    8x
    1x

    7x

    7x
    1x

    6x

    2x

    4x
    1x

    3x

    3x
    1x

    3x
    1x

    3x
    1x

    2x

     /**
     * 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');

    module.exports = async (state, tx) => {
      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 periodRoot = bufferToHex(tx.inputs[0].prevout.hash);

      if (!state.periodVotes) {
        state.periodVotes = {};
      }

      if (!state.periodVotes[periodRoot]) {
        state.periodVotes[periodRoot] = [];
      }

      if (state.periodVotes[periodRoot].indexOf(slotId) >= 0) {
        throw new Error(
          `[period vote] Already submitted. Slot: ${slotId}. Root: ${periodRoot}`
        );
      }

      state.periodVotes[periodRoot].push(slotId);
    };