All files / src/flags flagsFactory.js

100% Statements 15/15
87.5% Branches 7/8
100% Functions 7/7
100% Lines 14/14

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      48x 48x     3x 13x   47x 2x 2x 4x 4x       45x 1x     44x     1x       13x    
/* eslint-disable no-prototype-builtins */
 
function flagValue(bridgeState, flagHeights, flag) {
  const targetHeight = flagHeights[flag] || 0;
  return bridgeState.blockHeight >= targetHeight;
}
 
module.exports = (flags = []) => (bridgeState, flagHeights = {}) => {
  const proxy = new Proxy(flagHeights, {
    get(target, key) {
      if (key === 'toJSON') {
        return () =>
          flags.reduce((flagValues, flag) => {
            flagValues[flag] = flagValue(bridgeState, target, flag);
            return flagValues;
          }, {});
      }
 
      if (!flags.includes(key)) {
        throw new Error(`Unknown feature flag ${key}`);
      }
 
      return flagValue(bridgeState, target, key);
    },
    set(_, key) {
      throw new Error(`Flags are read-only: ${key}`);
    },
  });
 
  return proxy;
};