mirror of
https://github.com/tornadocash/fixed-merkle-tree.git
synced 2024-12-27 15:17:56 +01:00
100% coverage of fixed tree
This commit is contained in:
parent
21e673a659
commit
6173676074
@ -131,6 +131,9 @@ export class PartialMerkleTree {
|
||||
if (isNaN(Number(index)) || index < 0 || index > this._layers[0].length || index >= this.capacity) {
|
||||
throw new Error('Insert index out of bounds: ' + index)
|
||||
}
|
||||
if (index < this._edgeLeaf.index) {
|
||||
throw new Error(`Index ${index} is below the edge: ${this._edgeLeaf.index}`)
|
||||
}
|
||||
this._layers[0][index] = element
|
||||
for (let level = 1; level <= this.levels; level++) {
|
||||
index >>= 1
|
||||
@ -154,6 +157,9 @@ export class PartialMerkleTree {
|
||||
if (isNaN(Number(index)) || index < 0 || index >= this._layers[0].length) {
|
||||
throw new Error('Index out of bounds: ' + index)
|
||||
}
|
||||
if (index < this._edgeLeaf.index) {
|
||||
throw new Error(`Index ${index} is below the edge: ${this._edgeLeaf.index}`)
|
||||
}
|
||||
let elIndex = +index
|
||||
const pathElements: Element[] = []
|
||||
const pathIndices: number[] = []
|
||||
|
@ -247,6 +247,7 @@ describe('MerkleTree', () => {
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe('#getTreeEdge', () => {
|
||||
it('should return correct treeEdge', () => {
|
||||
const expectedEdge: TreeEdge = {
|
||||
@ -255,18 +256,24 @@ describe('MerkleTree', () => {
|
||||
5,
|
||||
'1390935134112885103361924701261056180224',
|
||||
'1952916572242076545231119328171167580160',
|
||||
'938972308169430750202858820582946897920'
|
||||
'938972308169430750202858820582946897920',
|
||||
],
|
||||
pathIndices: [ 0, 0, 1, 0 ],
|
||||
pathPositions: [ 5, 0, 0, 0 ]
|
||||
pathIndices: [0, 0, 1, 0],
|
||||
pathPositions: [5, 0, 0, 0],
|
||||
},
|
||||
edgeElement: 4,
|
||||
edgeIndex: 4
|
||||
edgeIndex: 4,
|
||||
}
|
||||
const tree = new MerkleTree(4, [0, 1, 2, 3, 4, 5])
|
||||
assert.deepEqual(tree.getTreeEdge(4), expectedEdge)
|
||||
})
|
||||
it('should fail if element not found', () => {
|
||||
const tree = new MerkleTree(4, [0, 1, 2, 3, 4, 5])
|
||||
const call = () => tree.getTreeEdge(6)
|
||||
should().throw(call, 'Element not found')
|
||||
})
|
||||
})
|
||||
|
||||
describe('#getters', () => {
|
||||
const elements = [1, 2, 3, 4, 5]
|
||||
const layers = [
|
||||
|
@ -1,6 +1,7 @@
|
||||
import { Element, MerkleTree, PartialMerkleTree } from '../src'
|
||||
import { it } from 'mocha'
|
||||
import { should } from 'chai'
|
||||
import * as assert from 'assert'
|
||||
|
||||
describe('PartialMerkleTree', () => {
|
||||
const getTestTrees = (levels: number, elements: Element[], edgeElement: Element) => {
|
||||
@ -101,5 +102,46 @@ describe('PartialMerkleTree', () => {
|
||||
should().equal(partialTree.indexOf(42), -1)
|
||||
})
|
||||
})
|
||||
|
||||
describe('#getters', () => {
|
||||
it('should return capacity', () => {
|
||||
const levels = 10
|
||||
const capacity = levels ** 2
|
||||
const { fullTree, partialTree } = getTestTrees(levels, [1, 2, 3, 4, 5], 3)
|
||||
should().equal(fullTree.capacity, capacity)
|
||||
should().equal(partialTree.capacity, capacity)
|
||||
})
|
||||
|
||||
it('should return same elements count as full tree', () => {
|
||||
const { fullTree, partialTree } = getTestTrees(10, [1, 2, 3, 4, 5], 3)
|
||||
should().equal(partialTree.elements.length, fullTree.elements.length)
|
||||
})
|
||||
|
||||
it('should return same layers count as full tree', () => {
|
||||
const { fullTree, partialTree } = getTestTrees(10, [1, 2, 3, 4, 5], 3)
|
||||
should().equal(partialTree.layers.length, fullTree.layers.length)
|
||||
})
|
||||
})
|
||||
|
||||
describe('#path', () => {
|
||||
|
||||
it('should return path for known nodes', () => {
|
||||
const { fullTree, partialTree } = getTestTrees(10, [1, 2, 3, 4, 5, 6, 7, 8, 9], 5)
|
||||
assert.deepEqual(fullTree.path(4), partialTree.path(4))
|
||||
})
|
||||
|
||||
it('should fail on incorrect index', () => {
|
||||
const { partialTree } = getTestTrees(10, [1, 2, 3, 4, 5, 6, 7, 8, 9], 5)
|
||||
should().throw((() => partialTree.path(-1)), 'Index out of bounds: -1')
|
||||
should().throw((() => partialTree.path(10)), 'Index out of bounds: 10')
|
||||
should().throw((() => partialTree.path('qwe')), 'Index out of bounds: qwe')
|
||||
})
|
||||
|
||||
it('should fail if index is below edge', () => {
|
||||
const { partialTree } = getTestTrees(10, [1, 2, 3, 4, 5, 6, 7, 8, 9], 5)
|
||||
const call = () => partialTree.path(2)
|
||||
should().throw(call, 'Index 2 is below the edge: 4')
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user