# All files / src/period index.js

100% Statements 20/20
100% Branches 6/6
100% Functions 4/4
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
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52

    1x
    1x
    1x

    3x

    3x

    3x

    3x

    1x
    1x

    2x

    2x
    2x
    2x

    2x

    2x
    1x
    1x

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

    module.exports = (bridgeState, sender) => async (
      rsp,
      state,
      chainInfo
    ) => {
      const height = chainInfo.height - 32;

      logPeriod('checkBridge');

      if (height <= 0 || !bridgeState.previousPeriod) {
        // genesis height doesn't need check
        rsp.status = 1;
        return;
      }

      logPeriod('checkBridge. Period: ', bridgeState.previousPeriod.merkleRoot());

      // use a timeout to relax race conditions (if period submission is fast)
      await new Promise(resolve => {
        setTimeout(async () => {
          await submitPeriodVote(bridgeState.previousPeriod, state, bridgeState, sender);

          const contractPeriod = await submitPeriod(
            bridgeState.previousPeriod,
            bridgeState.currentState.slots,
            height + bridgeState.checkCallsCount,
            bridgeState
          );

          if (contractPeriod.timestamp === '0') {
            rsp.status = 0;
            bridgeState.checkCallsCount += 1;
          } else {
            rsp.status = 1;
          }

          resolve();
        }, 300);
      });
    };