# All files / src/block updatePeriod.js

95.24% Statements 20/21
100% Branches 9/9
100% Functions 4/4
95.24% Lines 20/21

    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

    1x
    1x
    1x
    1x
    1x

    1x
    4x
    1x
    1x
    1x

    1x
    1x

    4x

    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 submitPeriod = require('../txHelpers/submitPeriod');
    const activateSlot = require('../txHelpers/activateSlot');
    const { getAuctionedByAddr } = require('../utils');
    const { logPeriod } = require('../utils/debug');

    module.exports = async (state, chainInfo, bridgeState, nodeConfig = {}) => {
      if (chainInfo.height % 32 === 0) {
        logPeriod('updatePeriod');
        try {
          await submitPeriod(
            bridgeState.currentPeriod,
            state.slots,
            chainInfo.height,
            bridgeState,
            nodeConfig
          );
        } catch (err) {
          logPeriod(err);
        }
        bridgeState.previousPeriod = bridgeState.currentPeriod;
        bridgeState.currentPeriod = new Period(
          bridgeState.previousPeriod.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 && !nodeConfig.readonly) {
          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);
              });
            }
          });
        }
      }
    };