# All files / src/txHelpers submitPeriod.js

77.78% Statements 28/36
61.54% Branches 8/13
80% Functions 4/5
78.79% Lines 26/33

    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

    1x
    1x

    1x
    2x
    2x
    2x
    2x

    1x
    2x

    2x

    1x

    3x
    3x

    3x
    1x

    1x
    1x

    2x

    2x

    2x
    2x
    1x
    1x
    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 {
      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 mySlotToSubmitFor = (slots, height, bridgeState) => {
      const mySlots = getSlotsByAddr(slots, bridgeState.account.address);
      const currentSlotId = getCurrentSlotId(slots, height);
      logPeriod('mySlots', currentSlotId, mySlots);
      return mySlots.find(slot => slot.id === currentSlotId);
    };

    const getPrevPeriodRoot = (period, bridgeState, height) => {
      const { lastBlocksRoot, lastPeriodRoot } = bridgeState;

      Eif (lastBlocksRoot === period.prevHash) return lastPeriodRoot; // found
      if (height === 32) return GENESIS; // not found on 32 block = first period to be submitted
      return null; // not found
    };

    module.exports = async (
      period,
      slots,
      height,
      bridgeState,
      nodeConfig = {}
    ) => {
      const { lastBlocksRoot, lastPeriodRoot } = bridgeState;
      let submittedPeriod = { timestamp: '0' };

      if (lastBlocksRoot === period.merkleRoot()) {
        submittedPeriod = await bridgeState.bridgeContract.methods
          .periods(lastPeriodRoot)
          .call();
        logPeriod('submittedPeriod', period.merkleRoot(), submittedPeriod);
        return submittedPeriod;
      }

      const prevPeriodRoot = getPrevPeriodRoot(period, bridgeState, height);

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

      const mySlotToSubmit = mySlotToSubmitFor(slots, height, bridgeState);
      if (mySlotToSubmit) {
        logPeriod('submitPeriod. Slot %d', mySlotToSubmit.id);
        Eif (!prevPeriodRoot) {
          logPeriod(
            'submitPeriod. Not previous period root found. Skipping submission'
          );
          return submittedPeriod;
        }
        const tx = sendTransaction(
          bridgeState.web3,
          bridgeState.operatorContract.methods.submitPeriod(
            mySlotToSubmit.id,
            prevPeriodRoot,
            period.merkleRoot()
          ),
          bridgeState.operatorContract.options.address,
          bridgeState.account
        ).catch(logError(height));

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

      return submittedPeriod;
    };