All files / src/heartbeat index.js

91.67% Statements 11/12
66.67% Branches 2/3
66.67% Functions 2/3
91.67% Lines 11/12

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                                    1x   1x 1x     3x     1x 3x 3x     3x 2x 2x         1x      
// # Behavior of the heartbeat service
//
// - If the node **cannot find the heartbeat color in the configuration**,
// there is nothing it can do and the service is **not started**. A node
// operator will need to adjust their configuration and restart the node if
// they want it to start.
// - If the **color is defined in the configuration** but there is **no NFT
// available** for the current account, the heartbeat service **is started**
// - The service will check every `config.heartbeat.period` for an available
// NFT to push forward (default `60000 ms`), and if found it will create a new
// transaction to transfer the NFT to the account defined in `account.address`.
// - If there is an error in pushing forward the NFT transaction, the node will
// try after `config.heartbeat.periodOnError` to send the transaction again
// (default `5000 ms`)
// - If `config.heartbeat.filter` is `true` (this is also the default value),
// calling the RPC `plasma_getUnspent` will filter out the heartbeat NFT of any
// account different from `account.address` from the results.
 
const { logNode } = require('../utils/debug');
 
const loop = require('./loop');
const defaults = require('./defaults');
 
function updateConfig({ heartbeat = {} }) {
  return { ...defaults, ...heartbeat };
}
 
module.exports = async (bridgeState, sender) => {
  bridgeState.config.heartbeat = updateConfig(bridgeState.config);
  const heartbeatColor = await bridgeState.operatorContract.methods
    .heartbeatColor()
    .call();
  if (heartbeatColor) {
    bridgeState.config.heartbeat.color = heartbeatColor;
    setTimeout(
      () => loop(bridgeState, sender),
      bridgeState.config.heartbeat.period
    );
  } else {
    logNode('Cannot find Heartbeat color, liveliness service not started.');
  }
};