implementing getTreeSlices types

This commit is contained in:
Sergei SMART 2022-03-13 14:45:13 +10:00
parent bc533ede2d
commit d11c5c168a
3 changed files with 19 additions and 3 deletions

@ -1,4 +1,13 @@
import { Element, HashFunction, MerkleTreeOptions, ProofPath, SerializedTreeState, simpleHash, TreeEdge } from './' import {
Element,
HashFunction,
MerkleTreeOptions,
ProofPath,
SerializedTreeState,
simpleHash,
TreeEdge,
TreeSlice,
} from './'
const defaultHash = (left: Element, right: Element): string => simpleHash([left, right]) const defaultHash = (left: Element, right: Element): string => simpleHash([left, right])
@ -199,11 +208,11 @@ export default class MerkleTree {
* 🪓 * 🪓
* @param count * @param count
*/ */
getTreeSlices(count = 4): { edge: TreeEdge, elements: Element[] }[] { getTreeSlices(count = 4): TreeSlice[] {
const length = this._layers[0].length const length = this._layers[0].length
let size = Math.ceil(length / count) let size = Math.ceil(length / count)
if (size % 2) size++ if (size % 2) size++
const slices = [] const slices: TreeSlice[] = []
for (let i = 0; i < length; i += size) { for (let i = 0; i < length; i += size) {
const edgeLeft = i const edgeLeft = i
const edgeRight = i + size const edgeRight = i + size

@ -40,5 +40,7 @@ export type TreeEdge = {
edgeIndex: number; edgeIndex: number;
edgeElementsCount: number; edgeElementsCount: number;
} }
export type TreeSlice = { edge: TreeEdge, elements: Element[] }
export type LeafWithIndex = { index: number, data: Element } export type LeafWithIndex = { index: number, data: Element }

@ -198,6 +198,11 @@ describe('PartialMerkleTree', () => {
const zeros = partialTree.zeros const zeros = partialTree.zeros
should().not.equal(zeros, partialTree.zeros) should().not.equal(zeros, partialTree.zeros)
}) })
it('should return edge leaf', () => {
const { partialTree } = getTestTrees(10, [1, 2, 3, 4, 5], 2)
should().equal(partialTree.edgeElement, 3)
})
}) })
describe('#path', () => { describe('#path', () => {