All files / src/validator/periods submitPeriodVote.js

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

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            4x   4x 4x 4x   4x 3x 3x   3x   3x 1x 1x     2x 1x     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 { logPeriod } = require('../../utils/debug');
const { getSlotsByAddr } = require('../../utils');
const isAlreadyVoted = require('./isAlreadyVoted');
 
module.exports = async (periodBlocksRoot, periodProposal, bridgeState) => {
  const { account, sender, currentState } = bridgeState;
  const mySlots = getSlotsByAddr(currentState.slots, account.address);
 
  const isValidator = mySlots.length > 0;
 
  if (!isValidator || process.env.NO_PERIOD_VOTE) {
    logPeriod(`[period vote] Not a validator`);
    return;
  }
 
  if (isAlreadyVoted(periodBlocksRoot, mySlots[0].id, periodProposal)) {
    logPeriod(
      `[period vote] Already voted. Slot: ${mySlots[0].id}. Root: ${periodBlocksRoot}`
    );
    return;
  }
 
  logPeriod(
    `[period vote] Submitting. Slot: ${mySlots[0].id}. Root: ${periodBlocksRoot}`
  );
  const input = new Input(new Outpoint(periodBlocksRoot, 0));
  const periodVoteTx = Tx.periodVote(mySlots[0].id, input).signAll(
    account.privateKey
  );
 
  await sender.send(periodVoteTx);
};