All files / src/utils redundancy.ts

73.33% Statements 33/45
46.15% Branches 12/26
100% Functions 4/4
72.72% Lines 32/44

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 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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 15033x   33x       33x       33x       33x       33x       33x       33x                   33x                     33x 5x 1x     4x 4x   4x 5x 4x               4x   1x   1x   1x   1x                       33x         33x         33x         33x           33x           1x               33x 1x 1x   1x                                                  
import { RedundancyLevel } from '..'
 
const mediumTable = [
  [94, 68, 46, 28, 14, 5, 1],
  [9, 8, 7, 6, 5, 4, 3],
]
const encMediumTable = [
  [47, 34, 23, 14, 7, 2],
  [9, 8, 7, 6, 5, 4],
]
const strongTable = [
  [104, 95, 86, 77, 69, 61, 53, 46, 39, 32, 26, 20, 15, 10, 6, 3, 1],
  [21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5],
]
const encStrongTable = [
  [52, 47, 43, 38, 34, 30, 26, 23, 19, 16, 13, 10, 7, 5, 3, 1],
  [21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6],
]
const insaneTable = [
  [92, 87, 82, 77, 73, 68, 63, 59, 54, 50, 45, 41, 37, 33, 29, 26, 22, 19, 16, 13, 10, 8, 5, 3, 2, 1],
  [31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6],
]
const encInsaneTable = [
  [46, 43, 41, 38, 36, 34, 31, 29, 27, 25, 22, 20, 18, 16, 14, 13, 11, 9, 8, 6, 5, 4, 2, 1],
  [31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 7],
]
const paranoidTable = [
  [
    37, 36, 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9,
    8, 7, 6, 5, 4, 3, 2, 1,
  ],
  [
    90, 88, 87, 85, 84, 82, 81, 79, 77, 76, 74, 72, 71, 69, 67, 66, 64, 62, 60, 59, 57, 55, 53, 51, 49, 48, 46, 44, 41,
    39, 37, 35, 32, 30, 27, 24, 20,
  ],
]
const encParanoidTable = [
  [18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1],
  [88, 85, 82, 79, 76, 72, 69, 66, 62, 59, 55, 51, 48, 44, 39, 35, 30, 24],
]
 
/**
 * Returns an approximate multiplier for the overhead of a given redundancy level.
 * Redundancy level is a tradeoff between storage overhead and fault tolerance.
 * Use this number to estimate the amount of chunks that will be stored for a given
 * redundancy level.
 */
export function approximateOverheadForRedundancyLevel(chunks: number, level: RedundancyLevel, encrypted: boolean) {
  if (level === RedundancyLevel.OFF) {
    return 0
  }
 
  const table = selectTable(level, encrypted)
  const [supportedChunks, parities] = table
 
  for (let i = 0; i < supportedChunks.length; i++) {
    if (chunks >= supportedChunks[i]) {
      return parities[i] / supportedChunks[i]
    }
  }
 
  return parities[parities.length - 1] / supportedChunks[supportedChunks.length - 1]
}
 
function selectTable(level: RedundancyLevel, encrypted: boolean) {
  switch (level) {
    case RedundancyLevel.MEDIUM:
      return encrypted ? encMediumTable : mediumTable
    case RedundancyLevel.STRONG:
      return encrypted ? encStrongTable : strongTable
    case RedundancyLevel.INSANE:
      return encrypted ? encInsaneTable : insaneTable
    case RedundancyLevel.PARANOID:
      return encrypted ? encParanoidTable : paranoidTable
    default:
      throw new Error(`Unknown redundancy level ${level}`)
  }
}
 
interface RedundancyStats {
  label: string
  value: RedundancyLevel
  errorTolerance: number
}
 
const medium = {
  label: 'medium',
  value: RedundancyLevel.MEDIUM,
  errorTolerance: 0.01,
}
const strong = {
  label: 'strong',
  value: RedundancyLevel.STRONG,
  errorTolerance: 0.05,
}
const insane = {
  label: 'insane',
  value: RedundancyLevel.INSANE,
  errorTolerance: 0.1,
}
const paranoid = {
  label: 'paranoid',
  value: RedundancyLevel.PARANOID,
  errorTolerance: 0.5,
}
 
export function getRedundancyStats(): {
  medium: RedundancyStats
  strong: RedundancyStats
  insane: RedundancyStats
  paranoid: RedundancyStats
} {
  return {
    medium,
    strong,
    insane,
    paranoid,
  }
}
 
export function getRedundancyStat(level?: string | RedundancyLevel): RedundancyStats {
  if (typeof level === 'string') {
    switch (level.toLowerCase()) {
      case 'medium':
        return medium
      case 'strong':
        return strong
      case 'insane':
        return insane
      case 'paranoid':
        return paranoid
      default:
        throw new Error(`Unknown redundancy level '${level}'`)
    }
  }
 
  switch (level) {
    case RedundancyLevel.MEDIUM:
      return medium
    case RedundancyLevel.STRONG:
      return strong
    case RedundancyLevel.INSANE:
      return insane
    case RedundancyLevel.PARANOID:
      return paranoid
    default:
      throw new Error(`Unknown redundancy level '${level}'`)
  }
}