implementing getTreeSlices

This commit is contained in:
Sergei SMART 2022-03-10 19:41:32 +10:00
parent 8e2b0a2dbc
commit ad634a6b3a

@ -195,17 +195,21 @@ export default class MerkleTree {
return { edgePath, edgeElement, edgeIndex } return { edgePath, edgeElement, edgeIndex }
} }
getTreeSlices(count: number): { edge: TreeEdge, elements: Element[] }[] { /**
* 🪓
* @param count
*/
getTreeSlices(count = 4): { edge: TreeEdge, elements: Element[] }[] {
const length = this._layers[0].length const length = this._layers[0].length
let size = Math.ceil(length / count) let size = Math.ceil(length / count)
size % 2 && size++ size % 2 && size++
const indexes = [] const slices = []
for (let i = length - size - 1; i > -size; i -= size) { for (let i = length - size - 1; i > -size; i -= size) {
const edgeLeft = i >= 0 ? i : 0 const edgeLeft = i >= 0 ? i : 0
const edgeRight = i + size const edgeRight = i + size
indexes.push({ edge: this.getTreeEdge(edgeLeft), elements: this.elements.slice(edgeLeft, edgeRight) }) slices.push({ edge: this.getTreeEdge(edgeLeft), elements: this.elements.slice(edgeLeft, edgeRight) })
} }
return indexes return slices.reverse()
} }
/** /**