|
| 1 | +import { expect } from "chai"; |
| 2 | +import {describe, it} from "mocha"; |
| 3 | +import {fromSnapshot, indexToFinalizedGindices, toSnapshot} from "../../src/snapshot"; |
| 4 | +import {subtreeFillToContents} from "../../src/subtree"; |
| 5 | +import { LeafNode } from "../../src/node"; |
| 6 | +import { Tree, setNodesAtDepth } from "../../src/tree"; |
| 7 | +import { toGindex } from "../../src"; |
| 8 | + |
| 9 | +describe("toSnapshot and fromSnapshot", () => { |
| 10 | + const depth = 4; |
| 11 | + const maxItems = Math.pow(2, depth); |
| 12 | + |
| 13 | + for (let count = 0; count <= maxItems; count ++) { |
| 14 | + it(`toSnapshot and fromSnapshot with count ${count}`, () => { |
| 15 | + const nodes = Array.from({length: count}, (_, i) => LeafNode.fromRoot(Buffer.alloc(32, i))); |
| 16 | + const fullListRootNode = subtreeFillToContents(nodes, depth); |
| 17 | + const snapshot = toSnapshot(fullListRootNode, depth, count); |
| 18 | + const partialListRootNode = fromSnapshot(snapshot, depth); |
| 19 | + |
| 20 | + // 1st step - check if the restored root node is the same |
| 21 | + expect(partialListRootNode.root).to.deep.equal(fullListRootNode.root); |
| 22 | + |
| 23 | + // 2nd step - make sure we can add more nodes to the restored tree |
| 24 | + const fullTree = new Tree(fullListRootNode); |
| 25 | + const partialTree = new Tree(partialListRootNode); |
| 26 | + for (let i = count; i < maxItems; i++) { |
| 27 | + const gindex = toGindex(depth, BigInt(i)); |
| 28 | + fullTree.setNode(gindex, LeafNode.fromRoot(Buffer.alloc(32, i))); |
| 29 | + partialTree.setNode(gindex, LeafNode.fromRoot(Buffer.alloc(32, i))); |
| 30 | + expect(partialTree.root).to.deep.equal(fullTree.root); |
| 31 | + |
| 32 | + // and snapshot created from 2 trees are the same |
| 33 | + const snapshot1 = toSnapshot(fullTree.rootNode, depth, i + 1); |
| 34 | + const snapshot2 = toSnapshot(partialTree.rootNode, depth, i + 1); |
| 35 | + expect(snapshot2).to.deep.equal(snapshot1); |
| 36 | + } |
| 37 | + }); |
| 38 | + |
| 39 | + // setNodesAtDepth() api is what ssz uses to grow the tree in its commit() phase |
| 40 | + it(`toSnapshot and fromSnapshot with count ${count} then grow with setNodeAtDepth`, () => { |
| 41 | + const nodes = Array.from({length: count}, (_, i) => LeafNode.fromRoot(Buffer.alloc(32, i))); |
| 42 | + const fullListRootNode = subtreeFillToContents(nodes, depth); |
| 43 | + const snapshot = toSnapshot(fullListRootNode, depth, count); |
| 44 | + const partialListRootNode = fromSnapshot(snapshot, depth); |
| 45 | + |
| 46 | + // 1st step - check if the restored root node is the same |
| 47 | + expect(partialListRootNode.root).to.deep.equal(fullListRootNode.root); |
| 48 | + |
| 49 | + // 2nd step - grow the tree with setNodesAtDepth |
| 50 | + for (let i = count; i < maxItems; i++) { |
| 51 | + const addedNodes = Array.from({length: i - count + 1}, (_, j) => LeafNode.fromRoot(Buffer.alloc(32, j))); |
| 52 | + const indices = Array.from({length: i - count + 1}, (_, j) => j + count); |
| 53 | + const root1 = setNodesAtDepth(fullListRootNode, depth, indices, addedNodes); |
| 54 | + const root2 = setNodesAtDepth(partialListRootNode, depth, indices, addedNodes); |
| 55 | + expect(root2.root).to.deep.equal(root1.root); |
| 56 | + |
| 57 | + for (let j = count; j <= i; j++) { |
| 58 | + const snapshot1 = toSnapshot(root1, depth, j); |
| 59 | + const snapshot2 = toSnapshot(root2, depth, j); |
| 60 | + expect(snapshot2).to.deep.equal(snapshot1); |
| 61 | + } |
| 62 | + } |
| 63 | + }); |
| 64 | + |
| 65 | + it(`toSnapshot() multiple times with count ${count}`, () => { |
| 66 | + const nodes = Array.from({length: count}, (_, i) => LeafNode.fromRoot(Buffer.alloc(32, i))); |
| 67 | + const fullListRootNode = subtreeFillToContents(nodes, depth); |
| 68 | + const snapshot = toSnapshot(fullListRootNode, depth, count); |
| 69 | + const partialListRootNode = fromSnapshot(snapshot, depth); |
| 70 | + |
| 71 | + // 1st step - check if the restored root node is the same |
| 72 | + expect(partialListRootNode.root).to.deep.equal(fullListRootNode.root); |
| 73 | + |
| 74 | + const snapshot2 = toSnapshot(partialListRootNode, depth, count); |
| 75 | + const restoredRootNode2 = fromSnapshot(snapshot2, depth); |
| 76 | + |
| 77 | + // 2nd step - check if the restored root node is the same |
| 78 | + expect(restoredRootNode2.root).to.deep.equal(partialListRootNode.root); |
| 79 | + }); |
| 80 | + } |
| 81 | +}); |
| 82 | + |
| 83 | +describe("indexToFinalizedGindices", () => { |
| 84 | + // given a tree with depth = 4 |
| 85 | + // 1 |
| 86 | + // 2 3 |
| 87 | + // 4 5 6 7 |
| 88 | + // 8 9 10 11 12 13 14 15 |
| 89 | + // 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
| 90 | + const testCases: [number, number, bigint[]][] = [ |
| 91 | + [4, 0, [BigInt(16)]], |
| 92 | + [4, 1, [BigInt(8)]], |
| 93 | + [4, 2, [8, 18].map(BigInt)], |
| 94 | + [4, 3, [4].map(BigInt)], |
| 95 | + [4, 4, [4, 20].map(BigInt)], |
| 96 | + [4, 5, [4, 10].map(BigInt)], |
| 97 | + [4, 6, [4, 10, 22].map(BigInt)], |
| 98 | + [4, 7, [2].map(BigInt)], |
| 99 | + [4, 8, [2, 24].map(BigInt)], |
| 100 | + [4, 9, [2, 12].map(BigInt)], |
| 101 | + [4, 10, [2, 12, 26].map(BigInt)], |
| 102 | + [4, 11, [2, 6].map(BigInt)], |
| 103 | + [4, 12, [2, 6, 28].map(BigInt)], |
| 104 | + [4, 13, [2, 6, 14].map(BigInt)], |
| 105 | + [4, 14, [2, 6, 14, 30].map(BigInt)], |
| 106 | + [4, 15, [1].map(BigInt)], |
| 107 | + ]; |
| 108 | + |
| 109 | + for (const [depth, index, finalizeGindices] of testCases) { |
| 110 | + it(`should correctly get finalized gindexes for index ${index} and depth ${depth}`, () => { |
| 111 | + const actual = indexToFinalizedGindices(depth, index); |
| 112 | + expect(actual).to.deep.equal(finalizeGindices); |
| 113 | + }); |
| 114 | + } |
| 115 | +}); |
0 commit comments