Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions packages/ssz/src/type/vectorBasic.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {HashComputationLevel, Node, Tree} from "@chainsafe/persistent-merkle-tree";
import type {ArrayLike} from "../interface.ts";
import {maxChunksToDepth} from "../util/merkleize.ts";
import {namedClass} from "../util/named.ts";
import {Require} from "../util/types.ts";
Expand Down Expand Up @@ -76,6 +77,10 @@ export class VectorBasicType<ElementType extends BasicType<unknown>>
return new ArrayBasicTreeView(this, tree);
}

toView(value: ArrayLike<ValueOf<ElementType>>): ArrayBasicTreeView<ElementType> {
return super.toView(value as ValueOf<ElementType>[]);
}

getViewDU(node: Node, cache?: unknown): ArrayBasicTreeViewDU<ElementType> {
// cache type should be validated (if applicate) in the view

Expand All @@ -100,6 +105,10 @@ export class VectorBasicType<ElementType extends BasicType<unknown>>
return view.cache;
}

toViewDU(value: ArrayLike<ValueOf<ElementType>>): ArrayBasicTreeViewDU<ElementType> {
return super.toViewDU(value as ValueOf<ElementType>[]);
}

// Serialization + deserialization

value_serializedSize(): number {
Expand Down
14 changes: 14 additions & 0 deletions packages/ssz/test/unit/byType/vectorBasic/tree.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,17 @@ describe("VectorBasicType batchHashTreeRoot", () => {
expect(viewDU.batchHashTreeRoot()).toEqual(expectedRoot);
});
});

describe("VectorBasicType array-like values", () => {
const uint32VectorType = new VectorBasicType(new UintNumberType(4), 4);
const typedValue = new Uint32Array([11, 22, 33, 44]);
const arrayValue = [11, 22, 33, 44];

it("accepts Uint32Array in toView", () => {
expect(uint32VectorType.toView(typedValue).toValue()).toEqual(arrayValue);
});

it("accepts Uint32Array in toViewDU", () => {
expect(uint32VectorType.toViewDU(typedValue).toValue()).toEqual(arrayValue);
});
});
Loading