mirror of
https://github.com/tornadocash/fixed-merkle-tree.git
synced 2024-11-21 17:27:08 +01:00
implementing tree slicing
This commit is contained in:
parent
6439775e37
commit
8e2b0a2dbc
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user