@chainsafe/ssz
Version:
Simple Serialize
107 lines • 4.28 kB
JavaScript
import { maxChunksToDepth } from "../util/merkleize.js";
import { namedClass } from "../util/named.js";
import { ArrayCompositeTreeView } from "../view/arrayComposite.js";
import { ArrayCompositeTreeViewDU } from "../viewDU/arrayComposite.js";
import { ArrayType } from "./array.js";
import { maxSizeArrayComposite, minSizeArrayComposite, tree_deserializeFromBytesArrayComposite, tree_serializeToBytesArrayComposite, tree_serializedSizeArrayComposite, value_deserializeFromBytesArrayComposite, value_getBlocksBytesArrayComposite, value_serializeToBytesArrayComposite, value_serializedSizeArrayComposite, } from "./arrayComposite.js";
/**
* Vector: Ordered fixed-length homogeneous collection, with N values
*
* Array of Composite type:
* - Composite types always take at least one chunk
* - Composite types are always returned as views
*/
export class VectorCompositeType extends ArrayType {
elementType;
length;
typeName;
itemsPerChunk = 1;
depth;
chunkDepth;
maxChunkCount;
fixedSize;
minSize;
maxSize;
isList = false;
isViewMutable = true;
defaultLen;
constructor(elementType, length, opts) {
super(elementType);
this.elementType = elementType;
this.length = length;
if (elementType.isBasic)
throw Error("elementType must not be basic");
if (length === 0)
throw Error("Vector length must be > 0");
this.typeName = opts?.typeName ?? `Vector[${elementType.typeName}, ${length}]`;
this.maxChunkCount = length;
this.chunkDepth = maxChunksToDepth(this.maxChunkCount);
this.depth = this.chunkDepth;
this.fixedSize = elementType.fixedSize === null ? null : length * elementType.fixedSize;
this.minSize = minSizeArrayComposite(elementType, length);
this.maxSize = maxSizeArrayComposite(elementType, length);
this.defaultLen = length;
this.blocksBuffer = new Uint8Array(Math.ceil(this.maxChunkCount / 2) * 64);
}
static named(elementType, limit, opts) {
return new (namedClass(VectorCompositeType, opts.typeName))(elementType, limit, opts);
}
getView(tree) {
return new ArrayCompositeTreeView(this, tree);
}
getViewDU(node, cache) {
// cache type should be validated (if applicate) in the view
// biome-ignore lint/suspicious/noExplicitAny: We need to use `any` here explicitly
return new ArrayCompositeTreeViewDU(this, node, cache);
}
commitView(view) {
return view.node;
}
commitViewDU(view, hcOffset = 0, hcByLevel = null) {
view.commit(hcOffset, hcByLevel);
return view.node;
}
cacheOfViewDU(view) {
return view.cache;
}
// Serialization + deserialization
value_serializedSize(value) {
return value_serializedSizeArrayComposite(this.elementType, this.length, value);
}
value_serializeToBytes(output, offset, value) {
return value_serializeToBytesArrayComposite(this.elementType, this.length, output, offset, value);
}
value_deserializeFromBytes(data, start, end) {
return value_deserializeFromBytesArrayComposite(this.elementType, data, start, end, this);
}
tree_serializedSize(node) {
return tree_serializedSizeArrayComposite(this.elementType, this.length, this.depth, node);
}
tree_serializeToBytes(output, offset, node) {
return tree_serializeToBytesArrayComposite(this.elementType, this.length, this.depth, node, output, offset);
}
tree_deserializeFromBytes(data, start, end) {
return tree_deserializeFromBytesArrayComposite(this.elementType, this.depth, data, start, end, this);
}
// Helpers for TreeView
tree_getLength() {
return this.length;
}
tree_setLength() {
// Vector's length is immutable, ignore this call
}
tree_getChunksNode(node) {
return node;
}
tree_chunksNodeOffset() {
return 0;
}
tree_setChunksNode(_rootNode, chunksNode) {
return chunksNode;
}
// Merkleization
getBlocksBytes(value) {
return value_getBlocksBytesArrayComposite(this.elementType, this.length, value, this.blocksBuffer);
}
}
//# sourceMappingURL=vectorComposite.js.map