2022-03-02 03:02:23 +01:00
|
|
|
import { Element, MerkleTree, MerkleTreeOptions, PartialMerkleTree } from '../src'
|
2022-02-28 08:00:28 +01:00
|
|
|
import { it } from 'mocha'
|
2022-03-02 03:02:23 +01:00
|
|
|
import { should } from 'chai'
|
|
|
|
import * as assert from 'assert'
|
|
|
|
import { createHash } from 'crypto'
|
|
|
|
|
|
|
|
const sha256Hash = (left, right) => createHash('sha256').update(`${left}${right}`).digest('hex')
|
2022-02-28 08:00:28 +01:00
|
|
|
|
|
|
|
describe('PartialMerkleTree', () => {
|
2022-03-09 07:25:49 +01:00
|
|
|
const getTestTrees = (levels: number, elements: Element[], edgeIndex: number, treeOptions: MerkleTreeOptions = {}) => {
|
2022-03-02 03:02:23 +01:00
|
|
|
const fullTree = new MerkleTree(levels, elements, treeOptions)
|
2022-03-09 07:25:49 +01:00
|
|
|
const edge = fullTree.getTreeEdge(edgeIndex)
|
2022-03-01 06:33:46 +01:00
|
|
|
const leavesAfterEdge = elements.slice(edge.edgeIndex)
|
2022-03-09 07:25:49 +01:00
|
|
|
const partialTree = new PartialMerkleTree(levels, edge, leavesAfterEdge, treeOptions)
|
2022-03-01 06:33:46 +01:00
|
|
|
return { fullTree, partialTree }
|
|
|
|
}
|
2022-02-28 08:00:28 +01:00
|
|
|
describe('#constructor', () => {
|
2022-03-09 07:25:49 +01:00
|
|
|
const { fullTree, partialTree } = getTestTrees(20, ['0', '1', '2', '3', '4', '5'], 2)
|
2022-03-01 06:33:46 +01:00
|
|
|
it('should initialize merkle tree with same root', () => {
|
|
|
|
should().equal(fullTree.root, partialTree.root)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should initialize merkle tree with same leaves count', () => {
|
|
|
|
should().equal(fullTree.elements.length, partialTree.elements.length)
|
|
|
|
})
|
2022-03-02 03:02:23 +01:00
|
|
|
|
|
|
|
it('should work with optional hash function and zero element', () => {
|
2022-03-09 07:25:49 +01:00
|
|
|
const { partialTree, fullTree } = getTestTrees(10, [1, 2, 3, 4, 5, 6], 3, {
|
2022-03-02 03:02:23 +01:00
|
|
|
hashFunction: sha256Hash,
|
|
|
|
zeroElement: 'zero',
|
|
|
|
})
|
|
|
|
should().equal(partialTree.root, fullTree.root)
|
|
|
|
})
|
2022-03-01 06:33:46 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('#insert', () => {
|
|
|
|
|
|
|
|
it('should have equal root to full tree after insertion ', () => {
|
2022-03-09 07:25:49 +01:00
|
|
|
const { fullTree, partialTree } = getTestTrees(10, ['0', '1', '2', '3', '4', '5', '6', '7'], 5)
|
2022-03-01 06:33:46 +01:00
|
|
|
fullTree.insert('9')
|
|
|
|
partialTree.insert('9')
|
|
|
|
should().equal(fullTree.root, partialTree.root)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should fail to insert when tree is full', () => {
|
2022-03-09 07:25:49 +01:00
|
|
|
const { partialTree } = getTestTrees(3, ['0', '1', '2', '3', '4', '5', '6', '7'], 5)
|
|
|
|
const call = () => partialTree.insert('8')
|
2022-03-01 06:33:46 +01:00
|
|
|
should().throw(call, 'Tree is full')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('#bulkInsert', () => {
|
|
|
|
|
|
|
|
it('should work like full tree', () => {
|
2022-03-09 07:25:49 +01:00
|
|
|
const { fullTree, partialTree } = getTestTrees(20, [1, 2, 3, 4, 5], 2)
|
2022-03-01 06:33:46 +01:00
|
|
|
partialTree.bulkInsert([6, 7, 8])
|
|
|
|
fullTree.bulkInsert([6, 7, 8])
|
|
|
|
should().equal(fullTree.root, partialTree.root)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should give the same result as sequential inserts', () => {
|
|
|
|
const initialArray = [
|
|
|
|
[1],
|
|
|
|
[1, 2],
|
|
|
|
[1, 2, 3],
|
|
|
|
[1, 2, 3, 4],
|
|
|
|
]
|
|
|
|
const insertedArray = [
|
|
|
|
[11],
|
|
|
|
[11, 12],
|
|
|
|
[11, 12, 13],
|
|
|
|
[11, 12, 13, 14],
|
|
|
|
]
|
|
|
|
for (const initial of initialArray) {
|
|
|
|
for (const inserted of insertedArray) {
|
2022-03-09 07:25:49 +01:00
|
|
|
const { partialTree: tree1 } = getTestTrees(10, initial, initial.length - 1)
|
|
|
|
const { partialTree: tree2 } = getTestTrees(10, initial, initial.length - 1)
|
2022-03-01 06:33:46 +01:00
|
|
|
tree1.bulkInsert(inserted)
|
|
|
|
for (const item of inserted) {
|
|
|
|
tree2.insert(item)
|
|
|
|
}
|
|
|
|
should().equal(tree1.root, tree2.root)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}).timeout(10000)
|
|
|
|
|
|
|
|
it('should fail to insert too many elements', () => {
|
2022-03-09 07:25:49 +01:00
|
|
|
const { partialTree } = getTestTrees(2, [1, 2, 3, 4], 2)
|
2022-03-01 06:33:46 +01:00
|
|
|
const call = () => partialTree.bulkInsert([5, 6, 7])
|
|
|
|
should().throw(call, 'Tree is full')
|
|
|
|
})
|
2022-03-02 03:02:23 +01:00
|
|
|
it('should bypass empty elements', () => {
|
|
|
|
const elements = [1, 2, 3, 4]
|
2022-03-09 07:25:49 +01:00
|
|
|
const { partialTree } = getTestTrees(2, elements, 2)
|
2022-03-02 03:02:23 +01:00
|
|
|
partialTree.bulkInsert([])
|
|
|
|
should().equal(partialTree.elements.length, elements.length, 'No elements inserted')
|
|
|
|
})
|
2022-03-01 06:33:46 +01:00
|
|
|
})
|
2022-03-02 03:02:23 +01:00
|
|
|
describe('#update', () => {
|
|
|
|
it('should update last element', () => {
|
2022-03-09 07:25:49 +01:00
|
|
|
const { fullTree, partialTree } = getTestTrees(10, [1, 2, 3, 4, 5], 2)
|
2022-03-02 03:02:23 +01:00
|
|
|
partialTree.update(4, 42)
|
|
|
|
fullTree.update(4, 42)
|
|
|
|
should().equal(partialTree.root, fullTree.root)
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
it('should update odd element', () => {
|
2022-03-09 07:25:49 +01:00
|
|
|
const { fullTree, partialTree } = getTestTrees(10, [1, 2, 3, 4, 5, 6, 7, 8], 2)
|
2022-03-02 03:02:23 +01:00
|
|
|
partialTree.update(4, 42)
|
|
|
|
fullTree.update(4, 42)
|
|
|
|
should().equal(partialTree.root, fullTree.root)
|
|
|
|
})
|
2022-03-01 06:33:46 +01:00
|
|
|
|
2022-03-02 03:02:23 +01:00
|
|
|
it('should update even element', () => {
|
2022-03-09 07:25:49 +01:00
|
|
|
const { fullTree, partialTree } = getTestTrees(10, [1, 2, 3, 4, 5, 6, 7, 8], 2)
|
2022-03-02 03:02:23 +01:00
|
|
|
partialTree.update(3, 42)
|
|
|
|
fullTree.update(3, 42)
|
|
|
|
should().equal(partialTree.root, fullTree.root)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should update extra element', () => {
|
2022-03-09 07:25:49 +01:00
|
|
|
const { fullTree, partialTree } = getTestTrees(10, [1, 2, 3, 4, 5], 2)
|
2022-03-02 03:02:23 +01:00
|
|
|
partialTree.update(5, 6)
|
|
|
|
fullTree.update(5, 6)
|
|
|
|
should().equal(fullTree.root, partialTree.root)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should fail to update incorrect index', () => {
|
2022-03-09 07:25:49 +01:00
|
|
|
const { partialTree } = getTestTrees(10, [1, 2, 3, 4, 5], 3)
|
2022-03-02 03:02:23 +01:00
|
|
|
should().throw((() => partialTree.update(-1, 42)), 'Insert index out of bounds: -1')
|
|
|
|
should().throw((() => partialTree.update(6, 42)), 'Insert index out of bounds: 6')
|
|
|
|
should().throw((() => partialTree.update(2, 42)), 'Index 2 is below the edge: 3')
|
|
|
|
// @ts-ignore
|
|
|
|
should().throw((() => partialTree.update('qwe', 42)), 'Insert index out of bounds: qwe')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should fail to update over capacity', () => {
|
2022-03-09 07:25:49 +01:00
|
|
|
const { partialTree } = getTestTrees(2, [1, 2, 3, 4], 1)
|
2022-03-02 03:02:23 +01:00
|
|
|
const call = () => partialTree.update(4, 42)
|
|
|
|
should().throw(call, 'Insert index out of bounds: 4')
|
|
|
|
})
|
|
|
|
})
|
2022-03-01 06:33:46 +01:00
|
|
|
describe('#indexOf', () => {
|
|
|
|
it('should return same result as full tree', () => {
|
2022-03-09 07:25:49 +01:00
|
|
|
const { fullTree, partialTree } = getTestTrees(10, [1, 2, 3, 4, 5, 6, 7, 8], 3)
|
2022-03-01 06:33:46 +01:00
|
|
|
should().equal(partialTree.indexOf(5), fullTree.indexOf(5))
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should find index', () => {
|
2022-03-09 07:25:49 +01:00
|
|
|
const { partialTree } = getTestTrees(10, [1, 2, 3, 4, 5], 2)
|
2022-03-01 06:33:46 +01:00
|
|
|
should().equal(partialTree.indexOf(3), 2)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should work with comparator', () => {
|
2022-03-09 07:25:49 +01:00
|
|
|
const { partialTree } = getTestTrees(10, [1, 2, 3, 4, 5], 2)
|
2022-03-01 06:33:46 +01:00
|
|
|
should().equal(partialTree.indexOf(4, (arg0, arg1) => arg0 === arg1), 3)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should return -1 for non existent element', () => {
|
2022-03-09 07:25:49 +01:00
|
|
|
const { partialTree } = getTestTrees(10, [1, 2, 3, 4, 5], 2)
|
2022-03-01 06:33:46 +01:00
|
|
|
should().equal(partialTree.indexOf(42), -1)
|
2022-02-28 08:00:28 +01:00
|
|
|
})
|
|
|
|
})
|
2022-03-01 07:02:45 +01:00
|
|
|
|
2022-03-02 04:22:54 +01:00
|
|
|
describe('#proof', () => {
|
|
|
|
it('should return proof for known leaf', () => {
|
2022-03-09 07:25:49 +01:00
|
|
|
const { partialTree } = getTestTrees(10, [1, 2, 3, 4, 5], 2)
|
2022-03-02 04:22:54 +01:00
|
|
|
assert.deepEqual(partialTree.proof(4), partialTree.path(3))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2022-03-01 07:02:45 +01:00
|
|
|
describe('#getters', () => {
|
|
|
|
it('should return capacity', () => {
|
|
|
|
const levels = 10
|
2022-03-09 07:25:49 +01:00
|
|
|
const capacity = 2 ** levels
|
|
|
|
const { fullTree, partialTree } = getTestTrees(levels, [1, 2, 3, 4, 5], 2)
|
2022-03-01 07:02:45 +01:00
|
|
|
should().equal(fullTree.capacity, capacity)
|
|
|
|
should().equal(partialTree.capacity, capacity)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should return same elements count as full tree', () => {
|
2022-03-02 03:02:23 +01:00
|
|
|
const levels = 20
|
|
|
|
const capacity = levels ** 2
|
|
|
|
const elements = Array.from({ length: capacity }, (_, i) => i)
|
|
|
|
const { fullTree, partialTree } = getTestTrees(levels, elements, 200)
|
2022-03-01 07:02:45 +01:00
|
|
|
should().equal(partialTree.elements.length, fullTree.elements.length)
|
|
|
|
})
|
|
|
|
|
2022-03-02 03:02:23 +01:00
|
|
|
it('should return copy of layers', () => {
|
2022-03-09 07:25:49 +01:00
|
|
|
const { partialTree } = getTestTrees(10, [1, 2, 3, 4, 5], 2)
|
2022-03-02 03:02:23 +01:00
|
|
|
const layers = partialTree.layers
|
|
|
|
should().not.equal(layers, partialTree.layers)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should return copy of zeros', () => {
|
2022-03-09 07:25:49 +01:00
|
|
|
const { partialTree } = getTestTrees(10, [1, 2, 3, 4, 5], 2)
|
2022-03-02 03:02:23 +01:00
|
|
|
const zeros = partialTree.zeros
|
|
|
|
should().not.equal(zeros, partialTree.zeros)
|
2022-03-01 07:02:45 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('#path', () => {
|
|
|
|
|
|
|
|
it('should return path for known nodes', () => {
|
2022-03-10 09:00:26 +01:00
|
|
|
const levels = 10
|
2022-03-09 07:25:49 +01:00
|
|
|
const capacity = 2 ** levels
|
|
|
|
const elements = Array.from({ length: capacity / 2 }, (_, i) => i)
|
2022-03-02 03:02:23 +01:00
|
|
|
const { fullTree, partialTree } = getTestTrees(levels, elements, 250)
|
2022-03-10 09:00:26 +01:00
|
|
|
assert.deepEqual(fullTree.path(251), partialTree.path(251))
|
|
|
|
}).timeout(1000)
|
2022-03-01 07:02:45 +01:00
|
|
|
|
|
|
|
it('should fail on incorrect index', () => {
|
2022-03-09 07:25:49 +01:00
|
|
|
const { partialTree } = getTestTrees(10, [1, 2, 3, 4, 5, 6, 7, 8, 9], 4)
|
2022-03-01 07:02:45 +01:00
|
|
|
should().throw((() => partialTree.path(-1)), 'Index out of bounds: -1')
|
|
|
|
should().throw((() => partialTree.path(10)), 'Index out of bounds: 10')
|
2022-03-02 04:22:54 +01:00
|
|
|
// @ts-ignore
|
2022-03-01 07:02:45 +01:00
|
|
|
should().throw((() => partialTree.path('qwe')), 'Index out of bounds: qwe')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should fail if index is below edge', () => {
|
2022-03-09 07:25:49 +01:00
|
|
|
const { partialTree } = getTestTrees(10, [1, 2, 3, 4, 5, 6, 7, 8, 9], 4)
|
2022-03-01 07:02:45 +01:00
|
|
|
const call = () => partialTree.path(2)
|
|
|
|
should().throw(call, 'Index 2 is below the edge: 4')
|
|
|
|
})
|
|
|
|
})
|
2022-03-02 05:33:04 +01:00
|
|
|
describe('#shiftEdge', () => {
|
|
|
|
it('should work', () => {
|
2022-03-09 07:25:49 +01:00
|
|
|
const levels = 10
|
|
|
|
const elements: Element[] = Array.from({ length: 20 ** 2 }, (_, i) => i)
|
2022-03-02 05:33:04 +01:00
|
|
|
const tree = new MerkleTree(levels, elements)
|
|
|
|
const edge1 = tree.getTreeEdge(200)
|
|
|
|
const edge2 = tree.getTreeEdge(100)
|
2022-03-09 07:25:49 +01:00
|
|
|
const partialTree = new PartialMerkleTree(levels, edge1, elements.slice(edge1.edgeIndex))
|
|
|
|
partialTree.shiftEdge(edge2, elements.slice(edge2.edgeIndex, partialTree.edgeIndex))
|
|
|
|
assert.deepEqual(partialTree.path(110), tree.path(110))
|
2022-03-02 05:33:04 +01:00
|
|
|
})
|
|
|
|
it('should fail if new edge index is over current edge', () => {
|
2022-03-09 07:25:49 +01:00
|
|
|
const { fullTree, partialTree } = getTestTrees(10, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 4)
|
|
|
|
const newEdge = fullTree.getTreeEdge(4)
|
2022-03-02 05:33:04 +01:00
|
|
|
const call = () => partialTree.shiftEdge(newEdge, [1, 2])
|
|
|
|
should().throw(call, 'New edgeIndex should be smaller then 4')
|
|
|
|
})
|
|
|
|
it('should fail if elements length are incorrect', () => {
|
2022-03-09 07:25:49 +01:00
|
|
|
const { fullTree, partialTree } = getTestTrees(10, [1, 2, 3, 4, 5, 6, 7, 8, 9], 4)
|
|
|
|
const newEdge = fullTree.getTreeEdge(3)
|
2022-03-02 05:33:04 +01:00
|
|
|
const call = () => partialTree.shiftEdge(newEdge, [1, 2])
|
2022-03-09 07:25:49 +01:00
|
|
|
should().throw(call, 'Elements length should be 1')
|
2022-03-02 05:33:04 +01:00
|
|
|
})
|
|
|
|
})
|
2022-03-02 03:02:23 +01:00
|
|
|
describe('#serialize', () => {
|
|
|
|
it('should work', () => {
|
2022-03-09 07:25:49 +01:00
|
|
|
const { partialTree } = getTestTrees(5, [1, 2, 3, 4, 5, 6, 7, 8, 9], 5)
|
2022-03-02 03:02:23 +01:00
|
|
|
const data = partialTree.serialize()
|
|
|
|
const dst = PartialMerkleTree.deserialize(data)
|
|
|
|
should().equal(partialTree.root, dst.root)
|
|
|
|
|
|
|
|
partialTree.insert(10)
|
|
|
|
dst.insert(10)
|
|
|
|
|
|
|
|
should().equal(partialTree.root, dst.root)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
describe('#toString', () => {
|
|
|
|
it('should return correct stringified representation', () => {
|
2022-03-09 07:25:49 +01:00
|
|
|
const { partialTree } = getTestTrees(5, [1, 2, 3, 4, 5, 6, 7, 8, 9], 5)
|
2022-03-02 03:02:23 +01:00
|
|
|
const str = partialTree.toString()
|
|
|
|
const dst = PartialMerkleTree.deserialize(JSON.parse(str))
|
|
|
|
should().equal(partialTree.root, dst.root)
|
|
|
|
|
|
|
|
partialTree.insert(10)
|
|
|
|
dst.insert(10)
|
|
|
|
|
|
|
|
should().equal(partialTree.root, dst.root)
|
|
|
|
})
|
|
|
|
})
|
2022-02-28 08:00:28 +01:00
|
|
|
})
|
2022-03-01 06:33:46 +01:00
|
|
|
|