# All files / src/period submitPeriodVote.js

100% Statements 19/19
100% Branches 10/10
100% Functions 2/2
100% Lines 19/19

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

    2x

    2x
    2x

    2x
    2x

    2x
    3x
    3x
    3x

    3x

    3x

    3x
    1x

    2x
    1x

    1x

    1x
    1x

    1x

     /**
     * 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 { Tx, Input, Outpoint } = require('leap-core');

    const { logNode } = require('../utils/debug');
    const { getSlotsByAddr } = require('../utils');

    const alreadyVoted = (periodRoot, slotId, periodVotes) =>
      (periodVotes[periodRoot] || []).indexOf(slotId) >= 0;

    module.exports = async (period, state, bridgeState, { send }) => {
      const { slots } = state;
      const periodVotes = state.periodVotes || {};
      const periodRoot = period.merkleRoot();

      const mySlots = getSlotsByAddr(slots, bridgeState.account.address);

      const isValidator = mySlots.length > 0;

      if (!isValidator || process.env.NO_PERIOD_VOTE) {
        return;
      }

      if (alreadyVoted(periodRoot, mySlots[0].id, periodVotes)) {
        logNode(
          `[period vote] Already submitted. Slot: ${mySlots[0].id}. Root: ${periodRoot}`
        );
        return;
      }

      const input = new Input(new Outpoint(periodRoot, 0));
      const periodVoteTx = Tx.periodVote(mySlots[0].id, input).signAll(
        bridgeState.account.privateKey
      );

      await send(periodVoteTx);
    };