# All files / src/txHelpers submitPeriod.js

90% Statements 27/30
73.33% Branches 11/15
100% Functions 3/3
89.66% Lines 26/29

    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
    88
    89
    90
    91
    92
    93
    94

    1x
    1x

    1x

    1x

    3x
    3x

    3x

    3x
    3x

    3x
    3x

    3x
    3x

    3x

    3x

    3x
    2x
    2x
    2x
    2x
    2x
    1x
    1x

    1x
    1x

    3x

     /**
     * 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 {
      getSlotsByAddr,
      sendTransaction,
      getCurrentSlotId,
      GENESIS,
    } = require('../utils');
    const { logPeriod } = require('../utils/debug');

    /* istanbul ignore next */
    const logError = height => err => {
      logPeriod('submitPeriod error: %s (height: %d)', err.message, height);
    };
    const eventDistance = 4 * 60 * 12; // about 12 hours on main-net

    module.exports = async (
      period,
      slots,
      height,
      bridgeState,
      nodeConfig = {}
    ) => {
      // query the contracts for submissions
      // to find the period roots to the merkle roots
      const parentHeight = await bridgeState.web3.eth.getBlockNumber();
      const submissions = await bridgeState.operatorContract.getPastEvents(
        'Submission',
        {
          filter: {
            blocksRoot: [period.prevHash, period.merkleRoot()],
          },
          fromBlock: parentHeight - eventDistance,
        }
      );
      let submittedPeriod = { timestamp: '0' };

      // if last period not submitted, only period.prevHash would find an event
      // if current period submitted already, period.merkleRoot() would also match an event
      let prevPeriodRoot;
      let currentPeriodRoot;
      for (let i = 0; i < submissions.length; i += 1) {
        Iif (submissions[i].returnValues.blocksRoot === period.prevHash) {
          prevPeriodRoot = submissions[i].returnValues.periodRoot;
        }
        Eif (submissions[i].returnValues.blocksRoot === period.merkleRoot()) {
          currentPeriodRoot = submissions[i].returnValues.periodRoot;
        }
      }

      Eif (currentPeriodRoot) {
        submittedPeriod = await bridgeState.bridgeContract.methods
          .periods(currentPeriodRoot)
          .call();
        logPeriod('submittedPeriod', period.merkleRoot(), submittedPeriod);
      }

      Iif (nodeConfig.readonly) {
        logPeriod('Readonly node. Skipping the rest of submitPeriod');
        return submittedPeriod;
      }

      if (submittedPeriod.timestamp === '0') {
        const mySlots = getSlotsByAddr(slots, bridgeState.account.address);
        const currentSlotId = getCurrentSlotId(slots, height);
        const currentSlot = mySlots.find(slot => slot.id === currentSlotId);
        logPeriod('mySlots', currentSlotId, mySlots);
        if (currentSlot) {
          logPeriod('submitPeriod. Slot %d', currentSlot.id);
          const tx = sendTransaction(
            bridgeState.web3,
            bridgeState.operatorContract.methods.submitPeriod(
              currentSlot.id,
              prevPeriodRoot || GENESIS,
              period.merkleRoot()
            ),
            bridgeState.operatorContract.options.address,
            bridgeState.account
          ).catch(logError(height));

          tx.then(receipt => {
            logPeriod('submitPeriod tx', receipt);
          });
        }
      }

      return submittedPeriod;
    };