@chainsafe/ssz
Version:
Simple Serialize
38 lines • 1.3 kB
JavaScript
import { Node, hashObjectToUint8Array } from "@chainsafe/persistent-merkle-tree";
/**
* BranchNode whose children's data is represented as a struct, not a tree.
*
* This approach is usefull for memory efficiency of data that is not modified often, for example the validators
* registry in Ethereum consensus `state.validators`. The tradeoff is that getting the hash, are proofs is more
* expensive because the tree has to be recreated every time.
*/
export class BranchNodeStruct extends Node {
valueToNode;
value;
constructor(valueToNode, value) {
// First null value is to save an extra variable to check if a node has a root or not
super(null, 0, 0, 0, 0, 0, 0, 0);
this.valueToNode = valueToNode;
this.value = value;
}
get rootHashObject() {
if (this.h0 === null) {
const node = this.valueToNode(this.value);
super.applyHash(node.rootHashObject);
}
return this;
}
get root() {
return hashObjectToUint8Array(this.rootHashObject);
}
get left() {
return this.valueToNode(this.value).left;
}
get right() {
return this.valueToNode(this.value).right;
}
isLeaf() {
return false;
}
}
//# sourceMappingURL=branchNodeStruct.js.map