@chainsafe/ssz
Version:
Simple Serialize
37 lines • 1.46 kB
JavaScript
import { LeafNode } from "@chainsafe/persistent-merkle-tree";
import { ArrayBasicTreeView } from "./arrayBasic.js";
export class ListBasicTreeView extends ArrayBasicTreeView {
type;
tree;
constructor(type, tree) {
super(type, tree);
this.type = type;
this.tree = tree;
}
/**
* Adds one value element at the end of the array and adds 1 to the current Tree length.
*/
push(value) {
const length = this.length;
if (length >= this.type.limit) {
throw Error("Error pushing over limit");
}
this.type.tree_setLength(this.tree, length + 1);
// If in new node..
if (length % this.type.itemsPerChunk === 0) {
// TODO: Optimize: This `inNewNode` could be ommitted but it would cause a full navigation in .set()
// Benchmark the cost of that navigation vs the extra math here
// TODO: Optimize: prevent double initialization
const leafNode = LeafNode.fromZero();
this.type.elementType.tree_setToPackedNode(leafNode, length, value);
// Commit immediately
const chunkIndex = Math.floor(length / this.type.itemsPerChunk);
this.tree.setNodeAtDepth(this.type.depth, chunkIndex, leafNode);
}
else {
// Re-use .set() since no new node is added
this.set(length, value);
}
}
}
//# sourceMappingURL=listBasic.js.map