UNPKG

@chainsafe/ssz

Version:

Simple Serialize

57 lines 2.64 kB
import { fromSnapshot, zeroNode } from "@chainsafe/persistent-merkle-tree"; import { byteArrayEquals } from "../util/byteArray.js"; import { zeroSnapshot } from "../util/snapshot.js"; import { PartialListCompositeTreeViewDU } from "../viewDU/partialListComposite.js"; import { addLengthNode } from "./arrayBasic.js"; import { ListCompositeType } from "./listComposite.js"; /** * Similar to ListCompositeType, this is mainly used to create a PartialListCompositeTreeViewDU from a snapshot. * The ViewDU created is a partial tree created from a snapshot, not a full tree. * Note that this class only inherits minimal methods as defined in ArrayType of ../view/arrayBasic.ts * It'll throw errors for all other methods, most of the usage is in the ViewDU class. */ export class PartialListCompositeType extends ListCompositeType { elementType; limit; constructor(elementType, limit, opts) { super(elementType, limit, opts); this.elementType = elementType; this.limit = limit; // only inherit methods in ArrayType of ../view/arrayBasic.ts const inheritedMethods = [ "tree_getLength", "tree_setLength", "tree_getChunksNode", "tree_chunksNodeOffset", "tree_setChunksNode", ]; const methodNames = Object.getOwnPropertyNames(ListCompositeType.prototype).filter((prop) => prop !== "constructor" && typeof this[prop] === "function" && !inheritedMethods.includes(prop)); // throw errors for all remaining methods for (const methodName of methodNames) { this[methodName] = () => { throw new Error(`Method ${methodName} is not implemented for PartialListCompositeType`); }; } } /** * Create a PartialListCompositeTreeViewDU from a snapshot. */ toPartialViewDU(snapshot) { const chunksNode = fromSnapshot(snapshot, this.chunkDepth); const rootNode = addLengthNode(chunksNode, snapshot.count); if (!byteArrayEquals(rootNode.root, snapshot.root)) { throw new Error(`Snapshot root is incorrect, expected ${snapshot.root}, got ${rootNode.root}`); } return new PartialListCompositeTreeViewDU(this, rootNode, snapshot); } /** * Creates a PartialListCompositeTreeViewDU from a zero snapshot. */ defaultPartialViewDU() { const rootNode = addLengthNode(zeroNode(this.chunkDepth), 0); return new PartialListCompositeTreeViewDU(this, rootNode, zeroSnapshot(this.chunkDepth)); } } //# sourceMappingURL=partialListComposite.js.map