fixed-merkle-tree/test/partialMerkleTree.spec.ts

298 lines
11 KiB
TypeScript
Raw Normal View History

import { Element, MerkleTree, MerkleTreeOptions, PartialMerkleTree } from '../src'
2022-02-28 08:00:28 +01:00
import { it } from 'mocha'
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 = {}) => {
const fullTree = new MerkleTree(levels, elements, treeOptions)
2022-03-09 07:25:49 +01:00
const edge = fullTree.getTreeEdge(edgeIndex)
const leavesAfterEdge = elements.slice(edge.edgeIndex)
2022-03-09 07:25:49 +01:00
const partialTree = new PartialMerkleTree(levels, edge, leavesAfterEdge, treeOptions)
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)
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)
})
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, {
hashFunction: sha256Hash,
zeroElement: 'zero',
})
should().equal(partialTree.root, fullTree.root)
})
})
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)
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')
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)
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)
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)
const call = () => partialTree.bulkInsert([5, 6, 7])
should().throw(call, 'Tree is full')
})
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)
partialTree.bulkInsert([])
should().equal(partialTree.elements.length, elements.length, 'No elements inserted')
})
})
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)
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)
partialTree.update(4, 42)
fullTree.update(4, 42)
should().equal(partialTree.root, fullTree.root)
})
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)
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)
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)
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)
const call = () => partialTree.update(4, 42)
should().throw(call, 'Insert index out of bounds: 4')
})
})
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)
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)
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)
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)
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-24 15:41:06 +01:00
const levels = 10
const capacity = 2 ** levels
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)
})
it('should return copy of layers', () => {
2022-03-09 07:25:49 +01:00
const { partialTree } = getTestTrees(10, [1, 2, 3, 4, 5], 2)
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)
const zeros = partialTree.zeros
should().not.equal(zeros, partialTree.zeros)
2022-03-01 07:02:45 +01:00
})
2022-03-13 05:45:13 +01:00
it('should return edge leaf', () => {
const { partialTree } = getTestTrees(10, [1, 2, 3, 4, 5], 2)
should().equal(partialTree.edgeElement, 3)
})
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)
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', () => {
2022-03-25 06:11:57 +01:00
const levels = 20
const elements: Element[] = Array.from({ length: 2 ** 18 }, (_, i) => i)
const tree = new MerkleTree(levels, elements)
2022-03-02 05:33:04 +01:00
it('should work', () => {
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))
2022-03-24 15:41:06 +01:00
tree.insert('1111')
partialTree.insert('1111')
2022-03-25 06:11:57 +01:00
assert.deepEqual(partialTree.path(150), tree.path(150))
2022-03-02 05:33:04 +01:00
})
2022-03-25 06:11:57 +01:00
it('should be able to build full tree from slices', () => {
const slices = tree.getTreeSlices(6)
const lastSlice = slices.pop()
const partialTree = new PartialMerkleTree(levels, lastSlice.edge, lastSlice.elements)
for (let i = slices.length - 1; i >= 0; i--) {
partialTree.shiftEdge(slices[i].edge, slices[i].elements)
}
partialTree.insert('1')
tree.insert('1')
assert.deepStrictEqual(partialTree.path(432), tree.path(432))
}).timeout(10000)
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
})
})
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)
const data = partialTree.serialize()
const dst = PartialMerkleTree.deserialize(data)
should().equal(partialTree.root, dst.root)
partialTree.insert(10)
dst.insert(10)
2022-03-28 09:44:50 +02:00
assert.deepStrictEqual(partialTree.path(6), dst.path(6))
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)
const str = partialTree.toString()
const dst = PartialMerkleTree.deserialize(JSON.parse(str))
2022-03-28 09:44:50 +02:00
assert.deepStrictEqual(partialTree.path(6), dst.path(6))
partialTree.insert(10)
dst.insert(10)
2022-03-28 09:44:50 +02:00
assert.deepStrictEqual(partialTree.path(6), dst.path(6))
assert.deepStrictEqual(partialTree.root, dst.root)
})
})
2022-02-28 08:00:28 +01:00
})