|
| 1 | +import * as assert from 'assert'; |
| 2 | +import { describe, it } from 'mocha'; |
| 3 | +import { HuffmanTapTreeNode, Taptree } from '../src/types'; |
| 4 | +import { tapTreeUsingHuffmanConstructor } from '../src/psbt/bip371'; |
| 5 | + |
| 6 | +describe('Taptree using Huffman Constructor', () => { |
| 7 | + const scriptBuff = Buffer.from(''); |
| 8 | + |
| 9 | + it('test empty array', () => { |
| 10 | + assert.throws( |
| 11 | + () => tapTreeUsingHuffmanConstructor([]), |
| 12 | + 'Cannot create taptree from empty list.', |
| 13 | + ); |
| 14 | + }); |
| 15 | + |
| 16 | + it( |
| 17 | + 'should return only one node for a single leaf', |
| 18 | + testLeafDistances([{ weight: 1, node: { output: scriptBuff } }], [0]), |
| 19 | + ); |
| 20 | + |
| 21 | + it( |
| 22 | + 'should return a balanced tree for a list of scripts with equal weights', |
| 23 | + testLeafDistances( |
| 24 | + [ |
| 25 | + { |
| 26 | + weight: 1, |
| 27 | + node: { |
| 28 | + output: scriptBuff, |
| 29 | + }, |
| 30 | + }, |
| 31 | + { |
| 32 | + weight: 1, |
| 33 | + node: { |
| 34 | + output: scriptBuff, |
| 35 | + }, |
| 36 | + }, |
| 37 | + { |
| 38 | + weight: 1, |
| 39 | + node: { |
| 40 | + output: scriptBuff, |
| 41 | + }, |
| 42 | + }, |
| 43 | + { |
| 44 | + weight: 1, |
| 45 | + node: { |
| 46 | + output: scriptBuff, |
| 47 | + }, |
| 48 | + }, |
| 49 | + ], |
| 50 | + [2, 2, 2, 2], |
| 51 | + ), |
| 52 | + ); |
| 53 | + |
| 54 | + it( |
| 55 | + 'should return an optimal binary tree for a list of scripts with weights [1, 2, 3, 4, 5]', |
| 56 | + testLeafDistances( |
| 57 | + [ |
| 58 | + { |
| 59 | + weight: 1, |
| 60 | + node: { |
| 61 | + output: scriptBuff, |
| 62 | + }, |
| 63 | + }, |
| 64 | + { |
| 65 | + weight: 2, |
| 66 | + node: { |
| 67 | + output: scriptBuff, |
| 68 | + }, |
| 69 | + }, |
| 70 | + { |
| 71 | + weight: 3, |
| 72 | + node: { |
| 73 | + output: scriptBuff, |
| 74 | + }, |
| 75 | + }, |
| 76 | + { |
| 77 | + weight: 4, |
| 78 | + node: { |
| 79 | + output: scriptBuff, |
| 80 | + }, |
| 81 | + }, |
| 82 | + { |
| 83 | + weight: 5, |
| 84 | + node: { |
| 85 | + output: scriptBuff, |
| 86 | + }, |
| 87 | + }, |
| 88 | + ], |
| 89 | + [3, 3, 2, 2, 2], |
| 90 | + ), |
| 91 | + ); |
| 92 | + |
| 93 | + it( |
| 94 | + 'should return an optimal binary tree for a list of scripts with weights [1, 2, 3, 3]', |
| 95 | + testLeafDistances( |
| 96 | + [ |
| 97 | + { |
| 98 | + weight: 1, |
| 99 | + node: { |
| 100 | + output: scriptBuff, |
| 101 | + }, |
| 102 | + }, |
| 103 | + { |
| 104 | + weight: 2, |
| 105 | + node: { |
| 106 | + output: scriptBuff, |
| 107 | + }, |
| 108 | + }, |
| 109 | + { |
| 110 | + weight: 3, |
| 111 | + node: { |
| 112 | + output: scriptBuff, |
| 113 | + }, |
| 114 | + }, |
| 115 | + { |
| 116 | + weight: 3, |
| 117 | + node: { |
| 118 | + output: scriptBuff, |
| 119 | + }, |
| 120 | + }, |
| 121 | + ], |
| 122 | + [3, 3, 2, 1], |
| 123 | + ), |
| 124 | + ); |
| 125 | + |
| 126 | + it( |
| 127 | + 'should return an optimal binary tree for a list of scripts with some negative weights: [1, 2, 3, -3]', |
| 128 | + testLeafDistances( |
| 129 | + [ |
| 130 | + { |
| 131 | + weight: 1, |
| 132 | + node: { |
| 133 | + output: scriptBuff, |
| 134 | + }, |
| 135 | + }, |
| 136 | + { |
| 137 | + weight: 2, |
| 138 | + node: { |
| 139 | + output: scriptBuff, |
| 140 | + }, |
| 141 | + }, |
| 142 | + { |
| 143 | + weight: 3, |
| 144 | + node: { |
| 145 | + output: scriptBuff, |
| 146 | + }, |
| 147 | + }, |
| 148 | + { |
| 149 | + weight: -3, |
| 150 | + node: { |
| 151 | + output: scriptBuff, |
| 152 | + }, |
| 153 | + }, |
| 154 | + ], |
| 155 | + [3, 2, 1, 3], |
| 156 | + ), |
| 157 | + ); |
| 158 | + |
| 159 | + it( |
| 160 | + 'should return an optimal binary tree for a list of scripts with some weights specified as infinity', |
| 161 | + testLeafDistances( |
| 162 | + [ |
| 163 | + { |
| 164 | + weight: 1, |
| 165 | + node: { |
| 166 | + output: scriptBuff, |
| 167 | + }, |
| 168 | + }, |
| 169 | + { |
| 170 | + weight: Number.POSITIVE_INFINITY, |
| 171 | + node: { |
| 172 | + output: scriptBuff, |
| 173 | + }, |
| 174 | + }, |
| 175 | + { |
| 176 | + weight: 3, |
| 177 | + node: { |
| 178 | + output: scriptBuff, |
| 179 | + }, |
| 180 | + }, |
| 181 | + { |
| 182 | + weight: Number.NEGATIVE_INFINITY, |
| 183 | + node: { |
| 184 | + output: scriptBuff, |
| 185 | + }, |
| 186 | + }, |
| 187 | + ], |
| 188 | + [3, 1, 2, 3], |
| 189 | + ), |
| 190 | + ); |
| 191 | +}); |
| 192 | + |
| 193 | +function testLeafDistances( |
| 194 | + input: HuffmanTapTreeNode[], |
| 195 | + expectedDistances: number[], |
| 196 | +) { |
| 197 | + return () => { |
| 198 | + const tree = tapTreeUsingHuffmanConstructor(input); |
| 199 | + |
| 200 | + if (!Array.isArray(tree)) { |
| 201 | + // tree is just one node |
| 202 | + assert.deepEqual([0], expectedDistances); |
| 203 | + return; |
| 204 | + } |
| 205 | + |
| 206 | + const leaves = input.map(value => value.node); |
| 207 | + |
| 208 | + const map = new Map<Taptree, number>(); // Map of leaf to actual distance |
| 209 | + let currentDistance = 1; |
| 210 | + let currentArray: Array<Taptree[] | Taptree> = tree as any; |
| 211 | + let nextArray: Array<Taptree[] | Taptree> = []; |
| 212 | + while (currentArray.length > 0) { |
| 213 | + currentArray.forEach(value => { |
| 214 | + if (Array.isArray(value)) { |
| 215 | + nextArray = nextArray.concat(value); |
| 216 | + return; |
| 217 | + } |
| 218 | + map.set(value, currentDistance); |
| 219 | + }); |
| 220 | + |
| 221 | + currentDistance += 1; // New level |
| 222 | + currentArray = nextArray; |
| 223 | + nextArray = []; |
| 224 | + } |
| 225 | + |
| 226 | + const actualDistances = leaves.map(value => map.get(value)); |
| 227 | + assert.deepEqual(actualDistances, expectedDistances); |
| 228 | + }; |
| 229 | +} |
0 commit comments