# All files / src/block updatePeriod.js

100% Statements 32/32
91.67% Branches 11/12
100% Functions 4/4
100% Lines 32/32

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
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87

    1x
    1x
    1x
    1x
    1x
    1x
    1x

    1x
    7x
    1x
    1x
    1x
    1x
    1x
    1x

    1x

    1x

    7x
    1x
    1x
    1x

    1x

    1x
    1x

    7x

    2x

    1x
    1x
    2x
    1x
    1x
    1x

     /**
     * 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 { Period } = require('leap-core');
    const submitPeriodVote = require('../period/submitPeriodVote');
    const submitPeriod = require('../txHelpers/submitPeriod');
    const activateSlot = require('../txHelpers/activateSlot');
    const { getAuctionedByAddr } = require('../utils');
    const { logPeriod } = require('../utils/debug');
    const checkEnoughVotes = require('../period/utils/checkEnoughVotes');

    module.exports = async (state, chainInfo, bridgeState, sender) => {
      if (bridgeState.pendingPeriod) {
        const pendingPeriodRoot = bridgeState.pendingPeriod.merkleRoot();
        const { result } = checkEnoughVotes(pendingPeriodRoot, state);
        Eif (result && !bridgeState.submittedPeriods[pendingPeriodRoot]) {
          logPeriod(`Enough votes to submit period: ${pendingPeriodRoot}`);
          try {
            await submitPeriod(
              bridgeState.pendingPeriod,
              state.slots,
              bridgeState.periodHeights[pendingPeriodRoot],
              bridgeState
            );
            bridgeState.submittedPeriods[pendingPeriodRoot] = true;
          } catch (err) {
            /* istanbul ignore next */
            logPeriod(`submit period: ${err}`);
          }
          bridgeState.previousPeriod = bridgeState.pendingPeriod;
        }
      }

      if (chainInfo.height % 32 === 0) {
        logPeriod('updatePeriod');
        try {
          bridgeState.periodHeights[bridgeState.currentPeriod.merkleRoot()] =
            chainInfo.height;
          // will be executed by all the nodes, but the actual period vote tx will be
          // submitted by validators only
          await submitPeriodVote(
            bridgeState.currentPeriod,
            state,
            bridgeState,
            sender
          );
        } catch (err) {
          /* istanbul ignore next */
          logPeriod(`period vote: ${err}`);
        }
        bridgeState.pendingPeriod = bridgeState.currentPeriod;
        bridgeState.currentPeriod = new Period(
          bridgeState.pendingPeriod.merkleRoot()
        );
      }
      if (chainInfo.height % 32 === 16) {
        // check if there is a validator slot that is "waiting for me"
        const myAuctionedSlots = getAuctionedByAddr(
          state.slots,
          bridgeState.account.address
        )
          .filter(({ activationEpoch }) => activationEpoch - state.epoch.epoch >= 2)
          .map(({ id }) => id);
        if (myAuctionedSlots.length > 0) {
          logPeriod('found some slots for activation', myAuctionedSlots);
          myAuctionedSlots.forEach(id => {
            const tx = activateSlot(id, bridgeState);
            /* istanbul ignore next */
            tx.catch(err => {
              logPeriod('activation error', err.message);
            });
            /* istanbul ignore next */
            if (typeof tx.on === 'function') {
              /* istanbul ignore next */
              tx.on('transactionHash', txHash => {
                logPeriod('activate', id, txHash);
              });
            }
          });
        }
      }
    };