implementing tree slicing

This commit is contained in:
Sergei SMART 2022-03-10 18:29:31 +10:00
parent 6439775e37
commit 8e2b0a2dbc

View File

@ -188,13 +188,26 @@ export default class MerkleTree {
getTreeEdge(edgeIndex: number): TreeEdge {
const edgeElement = this._layers[0][edgeIndex]
if (!edgeElement) {
if (edgeElement === undefined) {
throw new Error('Element not found')
}
const edgePath = this.path(edgeIndex)
return { edgePath, edgeElement, edgeIndex }
}
getTreeSlices(count: number): { edge: TreeEdge, elements: Element[] }[] {
const length = this._layers[0].length
let size = Math.ceil(length / count)
size % 2 && size++
const indexes = []
for (let i = length - size - 1; i > -size; i -= size) {
const edgeLeft = i >= 0 ? i : 0
const edgeRight = i + size
indexes.push({ edge: this.getTreeEdge(edgeLeft), elements: this.elements.slice(edgeLeft, edgeRight) })
}
return indexes
}
/**
* Serialize entire tree state including intermediate layers into a plain object
* Deserializing it back will not require to recompute any hashes