From 4ca1d6f789428c52d012e0e2d8fa5809849e581e Mon Sep 17 00:00:00 2001 From: twoeths Date: Mon, 19 Aug 2024 21:35:07 +0700 Subject: [PATCH] fix: sliceFrom api (#394) * chore: reproduce the bug in sliceFrom() * fix: sliceFrom() * chore: more comments --- packages/ssz/src/viewDU/listComposite.ts | 2 ++ .../unit/byType/listComposite/tree.test.ts | 20 +++++++++++-------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/packages/ssz/src/viewDU/listComposite.ts b/packages/ssz/src/viewDU/listComposite.ts index 1eff31d5..5ac8418e 100644 --- a/packages/ssz/src/viewDU/listComposite.ts +++ b/packages/ssz/src/viewDU/listComposite.ts @@ -80,6 +80,8 @@ export class ListCompositeTreeViewDU< sliceFrom(index: number): this { // Commit before getting rootNode to ensure all pending data is in the rootNode this.commit(); + // populate to `this.nodes` to ensure all nodes are loaded + this.populateAllNodes(); // If negative index, try to make it positive long as |index| < length if (index < 0) { diff --git a/packages/ssz/test/unit/byType/listComposite/tree.test.ts b/packages/ssz/test/unit/byType/listComposite/tree.test.ts index 21fab6f1..a1df5bff 100644 --- a/packages/ssz/test/unit/byType/listComposite/tree.test.ts +++ b/packages/ssz/test/unit/byType/listComposite/tree.test.ts @@ -3,6 +3,7 @@ import {CompositeView, ContainerType, ListCompositeType, toHexString, UintNumber import {ArrayCompositeTreeViewDU} from "../../../../src/viewDU/arrayComposite"; import {ssz} from "../../../lodestarTypes/primitive"; import {runViewTestMutation} from "../runViewTestMutation"; +import {ListCompositeTreeViewDU} from "../../../../src/viewDU/listComposite"; const uint64NumInfType = new UintNumberType(8, {clipInfinity: true}); const containerUintsType = new ContainerType( @@ -196,13 +197,16 @@ describe("ListCompositeType.sliceTo", () => { }); describe("ListCompositeType.sliceFrom", () => { - it("Slice List from multiple length", () => { - const listType = new ListCompositeType(ssz.Root, 1024); - const listLength = 16; - const list = Array.from({length: listLength}, (_, i) => Buffer.alloc(32, i)); - const listView = listType.toViewDU(list); + const listType = new ListCompositeType(ssz.Root, 1024); + const listLength = 16; + const list = Array.from({length: listLength}, (_, i) => Buffer.alloc(32, i)); + let listView: ListCompositeTreeViewDU; + beforeEach(() => { + listView = listType.toViewDU(list); + }); - for (let i = -(listLength + 1); i < listLength + 1; i++) { + for (let i = -(listLength + 1); i < listLength + 1; i++) { + it(`Slice List from list length ${listLength}`, () => { // compare list.slice(i) to listView.sliceFrom(i), they should be equivalent const slicedList = list.slice(i); const slicedListView = listView.sliceFrom(i); @@ -210,6 +214,6 @@ describe("ListCompositeType.sliceFrom", () => { expect(slicedListView.length).to.equal(slicedList.length); expect(toHexString(slicedListView.serialize())).to.equal(toHexString(listType.serialize(slicedList))); expect(toHexString(slicedListView.hashTreeRoot())).to.equal(toHexString(listType.hashTreeRoot(slicedList))); - } - }); + }); + } });