-
-
Notifications
You must be signed in to change notification settings - Fork 477
chore: review eip-7688 branch #9671
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| import {BranchNode, LeafNode, Node, zeroNode} from "@chainsafe/persistent-merkle-tree"; | ||
| import {progressiveSubtreeFillToContents} from "@chainsafe/ssz"; | ||
|
|
||
| // TODO: move these utils to @chainsafe/ssz (progressive.ts, next to progressiveSubtreeFillToContents) | ||
|
|
||
| /** Root node (chunks + length mix-in) of a zero-filled ProgressiveListBasicType of `length` items */ | ||
| export function zeroProgressiveListBasicRootNode(itemsPerChunk: number, length: number): Node { | ||
| const chunkCount = Math.ceil(length / itemsPerChunk); | ||
| // mirrors ssz's progressiveSubtreeCount (subtree capacities 1, 4, 16, ...) | ||
| let numSubtrees = 0; | ||
| for (let remaining = chunkCount, subtreeLength = 1; remaining > 0; subtreeLength *= 4) { | ||
| remaining -= Math.min(remaining, subtreeLength); | ||
| numSubtrees++; | ||
| } | ||
| return new BranchNode(zeroProgressiveNode(numSubtrees), LeafNode.fromUint32(length)); | ||
| } | ||
|
|
||
| /** | ||
| * Root node of a progressive list from its chunk/element nodes + length mix-in. | ||
| * `nodes` are packed 32-byte chunk leaves for basic lists, or element root nodes for composite lists. | ||
| */ | ||
| export function progressiveListRootNode(nodes: Node[], length: number): Node { | ||
| return new BranchNode(progressiveSubtreeFillToContents(nodes), LeafNode.fromUint32(length)); | ||
| } | ||
|
|
||
| /** | ||
| * Return the chunks node of a zero-filled progressive merkle list spanning `numSubtrees` | ||
| * balanced subtrees (chunk capacities 1, 4, 16, ... = 4^i; depths 0, 2, 4, ... = 2i). | ||
| * | ||
| * Not memoized: the zero data is already fully shared via the cached zeroNode(2i) subtrees; | ||
| * only the O(numSubtrees) spine BranchNodes (~10 at 2M validators) are allocated per call. | ||
| */ | ||
| function zeroProgressiveNode(numSubtrees: number): Node { | ||
| // chain(k) = B(zeroNode(0), B(zeroNode(2), ... B(zeroNode(2(k-1)), zeroNode(0)))) | ||
| let node: Node = zeroNode(0); // terminator | ||
| for (let i = numSubtrees - 1; i >= 0; i--) { | ||
| // BranchNode(left: balanced subtree i, right: rest of chain) — chain grows to the right, | ||
| // same as ssz progressiveSubtreeFillToContents: `root = new BranchNode(subtreeRoots[i], root)` | ||
| node = new BranchNode(zeroNode(2 * i), node); | ||
| } | ||
| return node; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| import {describe, expect, it} from "vitest"; | ||
| import {ssz} from "@lodestar/types"; | ||
| import {zeroProgressiveListBasicRootNode} from "../../../src/util/ssz.js"; | ||
|
|
||
| describe("zeroProgressiveListBasicRootNode", () => { | ||
| // EpochParticipation is ProgressiveList[ParticipationFlags] (uint8) → 32 items per chunk. | ||
| // Progressive subtree chunk capacities are 1, 4, 16, 64, ... so cumulative chunk counts are | ||
| // 1, 5, 21, 85, ... = 32, 160, 672, 2720 items. Test lengths around every boundary. | ||
| const lengths = [0, 1, 31, 32, 33, 160, 161, 671, 672, 673, 2720]; | ||
|
|
||
| for (const length of lengths) { | ||
| it(`equals naive zero-filled list length=${length}`, () => { | ||
| const fastView = ssz.gloas.EpochParticipation.getViewDU( | ||
| zeroProgressiveListBasicRootNode(ssz.gloas.EpochParticipation.itemsPerChunk, length) | ||
| ); | ||
| const naiveView = ssz.gloas.EpochParticipation.toViewDU(new Array<number>(length).fill(0)); | ||
|
|
||
| expect(fastView.length).toBe(length); | ||
| expect(fastView.hashTreeRoot()).toEqual(naiveView.hashTreeRoot()); | ||
| expect(fastView.serialize()).toEqual(naiveView.serialize()); | ||
| }); | ||
| } | ||
|
|
||
| it("produces a mutable view backed by shared zero nodes", () => { | ||
| const length = 673; | ||
| const fastView = ssz.gloas.EpochParticipation.getViewDU( | ||
| zeroProgressiveListBasicRootNode(ssz.gloas.EpochParticipation.itemsPerChunk, length) | ||
| ); | ||
| const naiveView = ssz.gloas.EpochParticipation.toViewDU(new Array<number>(length).fill(0)); | ||
|
|
||
| for (const view of [fastView, naiveView]) { | ||
| view.set(0, 7); | ||
| view.set(Math.floor(length / 2), 3); | ||
| view.set(length - 1, 1); | ||
| view.push(5); | ||
| view.commit(); | ||
| } | ||
|
|
||
| expect(fastView.getAll()).toEqual(naiveView.getAll()); | ||
| expect(fastView.hashTreeRoot()).toEqual(naiveView.hashTreeRoot()); | ||
| }); | ||
|
|
||
| it("does not mutate shared zero nodes across views", () => { | ||
| const length = 100; | ||
| const viewA = ssz.gloas.EpochParticipation.getViewDU( | ||
| zeroProgressiveListBasicRootNode(ssz.gloas.EpochParticipation.itemsPerChunk, length) | ||
| ); | ||
| const viewB = ssz.gloas.EpochParticipation.getViewDU( | ||
| zeroProgressiveListBasicRootNode(ssz.gloas.EpochParticipation.itemsPerChunk, length) | ||
| ); | ||
|
|
||
| viewA.set(50, 7); | ||
| viewA.commit(); | ||
|
|
||
| expect(viewA.get(50)).toBe(7); | ||
| expect(viewB.get(50)).toBe(0); | ||
| expect(viewB.getAll()).toEqual(new Array<number>(length).fill(0)); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.