From aab9347eb9832925df0e54fcadcc1bc225b4e2cc Mon Sep 17 00:00:00 2001 From: Tuyen Nguyen Date: Mon, 19 Aug 2024 13:28:51 +0700 Subject: [PATCH 1/3] chore: reproduce the bug in sliceFrom() --- .../unit/byType/listComposite/tree.test.ts | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/packages/ssz/test/unit/byType/listComposite/tree.test.ts b/packages/ssz/test/unit/byType/listComposite/tree.test.ts index 21fab6f1d..a1df5bff3 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))); - } - }); + }); + } }); From bb4e2d11bc7222feb4f09bf59a16754485acd680 Mon Sep 17 00:00:00 2001 From: Tuyen Nguyen Date: Mon, 19 Aug 2024 13:41:47 +0700 Subject: [PATCH 2/3] fix: sliceFrom() --- packages/ssz/src/viewDU/listComposite.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/ssz/src/viewDU/listComposite.ts b/packages/ssz/src/viewDU/listComposite.ts index 1eff31d5f..1a6ae04f5 100644 --- a/packages/ssz/src/viewDU/listComposite.ts +++ b/packages/ssz/src/viewDU/listComposite.ts @@ -80,6 +80,7 @@ export class ListCompositeTreeViewDU< sliceFrom(index: number): this { // Commit before getting rootNode to ensure all pending data is in the rootNode this.commit(); + this.populateAllNodes(); // If negative index, try to make it positive long as |index| < length if (index < 0) { From 8eeb3cce4ee5e64a4f8d21f5af7e6f44ca15f332 Mon Sep 17 00:00:00 2001 From: Tuyen Nguyen Date: Mon, 19 Aug 2024 13:47:55 +0700 Subject: [PATCH 3/3] chore: more comments --- packages/ssz/src/viewDU/listComposite.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/ssz/src/viewDU/listComposite.ts b/packages/ssz/src/viewDU/listComposite.ts index 1a6ae04f5..5ac8418e4 100644 --- a/packages/ssz/src/viewDU/listComposite.ts +++ b/packages/ssz/src/viewDU/listComposite.ts @@ -80,6 +80,7 @@ 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