Skip to content

Commit

Permalink
fix: use ssz in master branch
Browse files Browse the repository at this point in the history
  • Loading branch information
twoeths committed Jun 26, 2024
1 parent f9825bb commit 701a2d5
Show file tree
Hide file tree
Showing 31 changed files with 84 additions and 579 deletions.
7 changes: 1 addition & 6 deletions packages/ssz/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,8 @@ export {Type, ValueOf, JsonPath, ByteViews} from "./type/abstract";
export {BasicType, isBasicType} from "./type/basic";
export {CompositeType, CompositeTypeAny, CompositeView, CompositeViewDU, isCompositeType} from "./type/composite";
export {TreeView} from "./view/abstract";
export {ValueOfFields, ContainerTypeGeneric} from "./view/container";
export {ValueOfFields} from "./view/container";
export {TreeViewDU} from "./viewDU/abstract";
export {ListCompositeTreeViewDU} from "./viewDU/listComposite";
export {ArrayCompositeTreeViewDUCache} from "./viewDU/arrayComposite";

// Values
export {BitArray, getUint8ByteToBitBooleanArray} from "./value/bitArray";
Expand All @@ -38,6 +36,3 @@ export {BitArray, getUint8ByteToBitBooleanArray} from "./value/bitArray";
export {fromHexString, toHexString, byteArrayEquals} from "./util/byteArray";

export {hash64, symbolCachedPermanentRoot} from "./util/merkleize";

// others
export {BranchNodeStruct} from "./branchNodeStruct";
17 changes: 3 additions & 14 deletions packages/ssz/src/type/arrayBasic.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import {
BranchNode,
HashComputationGroup,
LeafNode,
Node,
getNodesAtDepth,
packedNodeRootsToBytes,
packedRootsBytesToNode,
arrayAtIndex,
} from "@chainsafe/persistent-merkle-tree";
import {Type, ValueOf, ByteViews} from "./abstract";
import {BasicType} from "./basic";
Expand Down Expand Up @@ -41,23 +39,14 @@ export function addLengthNode(chunksNode: Node, length: number): Node {
return new BranchNode(chunksNode, LeafNode.fromUint32(length));
}

export function setChunksNode(
rootNode: Node,
chunksNode: Node,
newLength: number | null,
hashComps: HashComputationGroup | null
): Node {
export function setChunksNode(rootNode: Node, chunksNode: Node, newLength?: number): Node {
const lengthNode =
newLength !== null
newLength !== undefined
? // If newLength is set, create a new node for length
LeafNode.fromUint32(newLength)
: // else re-use existing node
(rootNode.right as LeafNode);
const branchNode = new BranchNode(chunksNode, lengthNode);
if (hashComps !== null) {
arrayAtIndex(hashComps.byLevel, hashComps.offset).push({src0: chunksNode, src1: lengthNode, dest: branchNode});
}
return branchNode;
return new BranchNode(chunksNode, lengthNode);
}

export type ArrayProps = {isList: true; limit: number} | {isList: false; length: number};
Expand Down
6 changes: 3 additions & 3 deletions packages/ssz/src/type/bitArray.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {concatGindices, Gindex, HashComputationGroup, Node, toGindex, Tree} from "@chainsafe/persistent-merkle-tree";
import {concatGindices, Gindex, Node, toGindex, Tree} from "@chainsafe/persistent-merkle-tree";
import {fromHexString, toHexString, byteArrayEquals} from "../util/byteArray";
import {splitIntoRootChunks} from "../util/merkleize";
import {CompositeType, LENGTH_GINDEX} from "./composite";
Expand Down Expand Up @@ -29,8 +29,8 @@ export abstract class BitArrayType extends CompositeType<BitArray, BitArrayTreeV
return view.node;
}

commitViewDU(view: BitArrayTreeViewDU, hashComps: HashComputationGroup | null = null): Node {
view.commit(hashComps);
commitViewDU(view: BitArrayTreeViewDU): Node {
view.commit();
return view.node;
}

Expand Down
11 changes: 2 additions & 9 deletions packages/ssz/src/type/byteArray.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {concatGindices, Gindex, HashComputationGroup, Node, toGindex, Tree} from "@chainsafe/persistent-merkle-tree";
import {concatGindices, Gindex, Node, toGindex, Tree} from "@chainsafe/persistent-merkle-tree";
import {fromHexString, toHexString, byteArrayEquals} from "../util/byteArray";
import {splitIntoRootChunks} from "../util/merkleize";
import {ByteViews} from "./abstract";
Expand Down Expand Up @@ -37,8 +37,7 @@ export abstract class ByteArrayType extends CompositeType<ByteArray, ByteArray,
return this.commitViewDU(view);
}

// TODO - batch
commitViewDU(view: ByteArray, hashComps: HashComputationGroup | null = null): Node {
commitViewDU(view: ByteArray): Node {
const uint8Array = new Uint8Array(this.value_serializedSize(view));
const dataView = new DataView(uint8Array.buffer, uint8Array.byteOffset, uint8Array.byteLength);
this.value_serializeToBytes({uint8Array, dataView}, 0, view);
Expand Down Expand Up @@ -70,12 +69,6 @@ export abstract class ByteArrayType extends CompositeType<ByteArray, ByteArray,
return Uint8Array.prototype.slice.call(data.uint8Array, start, end);
}

value_toTree(value: ByteArray): Node {
// this saves 1 allocation of Uint8Array
const dataView = new DataView(value.buffer, value.byteOffset, value.byteLength);
return this.tree_deserializeFromBytes({uint8Array: value, dataView}, 0, value.length);
}

// Merkleization

protected getRoots(value: ByteArray): Uint8Array[] {
Expand Down
3 changes: 1 addition & 2 deletions packages/ssz/src/type/composite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
createProof,
getNode,
Gindex,
HashComputationGroup,
Node,
Proof,
ProofType,
Expand Down Expand Up @@ -127,7 +126,7 @@ export abstract class CompositeType<V, TV, TVDU> extends Type<V> {
/** INTERNAL METHOD: Given a Tree View, returns a `Node` with all its updated data */
abstract commitView(view: TV): Node;
/** INTERNAL METHOD: Given a Deferred Update Tree View returns a `Node` with all its updated data */
abstract commitViewDU(view: TVDU, hashComps?: HashComputationGroup | null): Node;
abstract commitViewDU(view: TVDU): Node;
/** INTERNAL METHOD: Return the cache of a Deferred Update Tree View. May return `undefined` if this ViewDU has no cache */
abstract cacheOfViewDU(view: TVDU): unknown;

Expand Down
5 changes: 2 additions & 3 deletions packages/ssz/src/type/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
toGindex,
concatGindices,
getNode,
HashComputationGroup,
} from "@chainsafe/persistent-merkle-tree";
import {maxChunksToDepth} from "../util/merkleize";
import {Require} from "../util/types";
Expand Down Expand Up @@ -163,8 +162,8 @@ export class ContainerType<Fields extends Record<string, Type<unknown>>> extends
return view.node;
}

commitViewDU(view: ContainerTreeViewDUType<Fields>, hashComps: HashComputationGroup | null = null): Node {
view.commit(hashComps);
commitViewDU(view: ContainerTreeViewDUType<Fields>): Node {
view.commit();
return view.node;
}

Expand Down
7 changes: 3 additions & 4 deletions packages/ssz/src/type/containerNodeStruct.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {HashComputationGroup, Node, subtreeFillToContents} from "@chainsafe/persistent-merkle-tree";
import {Node} from "@chainsafe/persistent-merkle-tree";
import {Type, ByteViews} from "./abstract";
import {isCompositeType} from "./composite";
import {ContainerType, ContainerOptions, renderContainerTypeName} from "./container";
Expand Down Expand Up @@ -106,9 +106,8 @@ export class ContainerNodeStructType<Fields extends Record<string, Type<unknown>
return new BranchNodeStruct(this.valueToTree.bind(this), value);
}

private valueToTree(
value: ValueOfFields<Fields>,
): Node {
// TODO: Optimize conversion
private valueToTree(value: ValueOfFields<Fields>): Node {
const uint8Array = new Uint8Array(this.value_serializedSize(value));
const dataView = new DataView(uint8Array.buffer, uint8Array.byteOffset, uint8Array.byteLength);
this.value_serializeToBytes({uint8Array, dataView}, 0, value);
Expand Down
20 changes: 5 additions & 15 deletions packages/ssz/src/type/listBasic.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {HashComputationGroup, LeafNode, Node, Tree} from "@chainsafe/persistent-merkle-tree";
import {LeafNode, Node, Tree} from "@chainsafe/persistent-merkle-tree";
import {ValueOf} from "./abstract";
import {BasicType} from "./basic";
import {ByteViews} from "./composite";
Expand Down Expand Up @@ -93,8 +93,8 @@ export class ListBasicType<ElementType extends BasicType<unknown>>
return view.node;
}

commitViewDU(view: ListBasicTreeViewDU<ElementType>, hashComps: HashComputationGroup | null = null): Node {
view.commit(hashComps);
commitViewDU(view: ListBasicTreeViewDU<ElementType>): Node {
view.commit();
return view.node;
}

Expand Down Expand Up @@ -144,18 +144,8 @@ export class ListBasicType<ElementType extends BasicType<unknown>>
return node.left;
}

tree_chunksNodeOffset(): number {
// one more level for length, see setChunksNode below
return 1;
}

tree_setChunksNode(
rootNode: Node,
chunksNode: Node,
newLength: number | null,
hashComps: HashComputationGroup | null
): Node {
return setChunksNode(rootNode, chunksNode, newLength, hashComps);
tree_setChunksNode(rootNode: Node, chunksNode: Node, newLength?: number): Node {
return setChunksNode(rootNode, chunksNode, newLength);
}

// Merkleization
Expand Down
20 changes: 5 additions & 15 deletions packages/ssz/src/type/listComposite.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {HashComputationGroup, Node, Tree} from "@chainsafe/persistent-merkle-tree";
import {Node, Tree} from "@chainsafe/persistent-merkle-tree";
import {
mixInLength,
maxChunksToDepth,
Expand Down Expand Up @@ -97,8 +97,8 @@ export class ListCompositeType<
return view.node;
}

commitViewDU(view: ListCompositeTreeViewDU<ElementType>, hashComps: HashComputationGroup | null = null): Node {
view.commit(hashComps);
commitViewDU(view: ListCompositeTreeViewDU<ElementType>): Node {
view.commit();
return view.node;
}

Expand Down Expand Up @@ -150,18 +150,8 @@ export class ListCompositeType<
return node.left;
}

tree_chunksNodeOffset(): number {
// one more level for length, see setChunksNode below
return 1;
}

tree_setChunksNode(
rootNode: Node,
chunksNode: Node,
newLength: number | null,
hashComps: HashComputationGroup | null
): Node {
return setChunksNode(rootNode, chunksNode, newLength, hashComps);
tree_setChunksNode(rootNode: Node, chunksNode: Node, newLength?: number): Node {
return setChunksNode(rootNode, chunksNode, newLength);
}

// Merkleization
Expand Down
5 changes: 2 additions & 3 deletions packages/ssz/src/type/optional.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {concatGindices, Gindex, HashComputationGroup, Node, Tree, zeroNode} from "@chainsafe/persistent-merkle-tree";
import {concatGindices, Gindex, Node, Tree, zeroNode} from "@chainsafe/persistent-merkle-tree";
import {mixInLength} from "../util/merkleize";
import {Require} from "../util/types";
import {namedClass} from "../util/named";
Expand Down Expand Up @@ -75,8 +75,7 @@ export class OptionalType<ElementType extends Type<unknown>> extends CompositeTy
}

// TODO add an OptionalViewDU
// TODO - batch
commitViewDU(view: ValueOfType<ElementType>, hashComps: HashComputationGroup | null = null): Node {
commitViewDU(view: ValueOfType<ElementType>): Node {
return this.value_toTree(view);
}

Expand Down
6 changes: 0 additions & 6 deletions packages/ssz/src/type/uint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,6 @@ export class UintNumberType extends BasicType<number> {
}
}

value_toTree(value: number): Node {
const node = LeafNode.fromZero();
node.setUint(this.byteLength, 0, value, this.clipInfinity);
return node;
}

tree_serializeToBytes(output: ByteViews, offset: number, node: Node): number {
const value = (node as LeafNode).getUint(this.byteLength, 0, this.clipInfinity);
this.value_serializeToBytes(output, offset, value);
Expand Down
5 changes: 2 additions & 3 deletions packages/ssz/src/type/union.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {concatGindices, getNode, Gindex, HashComputationGroup, Node, Tree} from "@chainsafe/persistent-merkle-tree";
import {concatGindices, getNode, Gindex, Node, Tree} from "@chainsafe/persistent-merkle-tree";
import {mixInLength} from "../util/merkleize";
import {Require} from "../util/types";
import {namedClass} from "../util/named";
Expand Down Expand Up @@ -106,8 +106,7 @@ export class UnionType<Types extends Type<unknown>[]> extends CompositeType<
return this.value_toTree(view);
}

// TODO - batch
commitViewDU(view: ValueOfTypes<Types>, hashComps: HashComputationGroup | null = null): Node {
commitViewDU(view: ValueOfTypes<Types>): Node {
return this.value_toTree(view);
}

Expand Down
10 changes: 3 additions & 7 deletions packages/ssz/src/type/vectorBasic.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {HashComputationGroup, Node, Tree} from "@chainsafe/persistent-merkle-tree";
import {Node, Tree} from "@chainsafe/persistent-merkle-tree";
import {maxChunksToDepth, splitIntoRootChunks} from "../util/merkleize";
import {Require} from "../util/types";
import {namedClass} from "../util/named";
Expand Down Expand Up @@ -83,8 +83,8 @@ export class VectorBasicType<ElementType extends BasicType<unknown>>
return view.node;
}

commitViewDU(view: ArrayBasicTreeViewDU<ElementType>, hashComps: HashComputationGroup | null = null): Node {
view.commit(hashComps);
commitViewDU(view: ArrayBasicTreeViewDU<ElementType>): Node {
view.commit();
return view.node;
}

Expand Down Expand Up @@ -132,10 +132,6 @@ export class VectorBasicType<ElementType extends BasicType<unknown>>
return node;
}

tree_chunksNodeOffset(): number {
return 0;
}

tree_setChunksNode(rootNode: Node, chunksNode: Node): Node {
return chunksNode;
}
Expand Down
10 changes: 3 additions & 7 deletions packages/ssz/src/type/vectorComposite.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {HashComputationGroup, Node, Tree} from "@chainsafe/persistent-merkle-tree";
import {Node, Tree} from "@chainsafe/persistent-merkle-tree";
import {maxChunksToDepth} from "../util/merkleize";
import {Require} from "../util/types";
import {namedClass} from "../util/named";
Expand Down Expand Up @@ -90,8 +90,8 @@ export class VectorCompositeType<
return view.node;
}

commitViewDU(view: ArrayCompositeTreeViewDU<ElementType>, hashComps: HashComputationGroup | null = null): Node {
view.commit(hashComps);
commitViewDU(view: ArrayCompositeTreeViewDU<ElementType>): Node {
view.commit();
return view.node;
}

Expand Down Expand Up @@ -139,10 +139,6 @@ export class VectorCompositeType<
return node;
}

tree_chunksNodeOffset(): number {
return 0;
}

tree_setChunksNode(rootNode: Node, chunksNode: Node): Node {
return chunksNode;
}
Expand Down
11 changes: 2 additions & 9 deletions packages/ssz/src/view/arrayBasic.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {getNodesAtDepth, HashComputationGroup, LeafNode, Node, Tree} from "@chainsafe/persistent-merkle-tree";
import {getNodesAtDepth, LeafNode, Node, Tree} from "@chainsafe/persistent-merkle-tree";
import {ValueOf} from "../type/abstract";
import {BasicType} from "../type/basic";
import {CompositeType} from "../type/composite";
Expand All @@ -21,15 +21,8 @@ export type ArrayBasicType<ElementType extends BasicType<unknown>> = CompositeTy
tree_setLength(tree: Tree, length: number): void;
/** INTERNAL METHOD: Return the chunks node from a root node */
tree_getChunksNode(rootNode: Node): Node;
/** INTERNAL METHOD: Return the offset from root for HashComputation */
tree_chunksNodeOffset(): number;
/** INTERNAL METHOD: Return a new root node with changed chunks node and length */
tree_setChunksNode(
rootNode: Node,
chunksNode: Node,
newLength: number | null,
hashComps: HashComputationGroup | null
): Node;
tree_setChunksNode(rootNode: Node, chunksNode: Node, newLength?: number): Node;
};

export class ArrayBasicTreeView<ElementType extends BasicType<unknown>> extends TreeView<ArrayBasicType<ElementType>> {
Expand Down
11 changes: 2 additions & 9 deletions packages/ssz/src/view/arrayComposite.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {getNodesAtDepth, HashComputationGroup, Node, toGindexBitstring, Tree} from "@chainsafe/persistent-merkle-tree";
import {getNodesAtDepth, Node, toGindexBitstring, Tree} from "@chainsafe/persistent-merkle-tree";
import {ValueOf} from "../type/abstract";
import {CompositeType, CompositeView, CompositeViewDU} from "../type/composite";
import {TreeView} from "./abstract";
Expand All @@ -16,15 +16,8 @@ export type ArrayCompositeType<
tree_setLength(tree: Tree, length: number): void;
/** INTERNAL METHOD: Return the chunks node from a root node */
tree_getChunksNode(rootNode: Node): Node;
/** INTERNAL METHOD: Return the offset from root for HashComputation */
tree_chunksNodeOffset(): number;
/** INTERNAL METHOD: Return a new root node with changed chunks node and length */
tree_setChunksNode(
rootNode: Node,
chunksNode: Node,
newLength: number | null,
hashComps: HashComputationGroup | null
): Node;
tree_setChunksNode(rootNode: Node, chunksNode: Node, newLength?: number): Node;
};

export class ArrayCompositeTreeView<
Expand Down
Loading

0 comments on commit 701a2d5

Please sign in to comment.