-
-
Notifications
You must be signed in to change notification settings - Fork 436
feat: model BeaconStateView #8773
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
base: unstable
Are you sure you want to change the base?
Changes from all commits
adec313
2f750fd
26275bd
38c7c38
c9558a8
4d0f790
8911eb9
5937c14
782e1b1
018b541
7ea00cb
66eeaf2
ea1e79d
89e92a9
3aaf36a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| import {Tree} from "@chainsafe/persistent-merkle-tree"; | ||
| import { | ||
| BLOCK_BODY_EXECUTION_PAYLOAD_GINDEX, | ||
| FINALIZED_ROOT_GINDEX, | ||
| FINALIZED_ROOT_GINDEX_ELECTRA, | ||
| ForkName, | ||
| ForkPostBellatrix, | ||
| isForkPostElectra, | ||
| } from "@lodestar/params"; | ||
| import {BeaconBlockBody, SSZTypesFor, ssz} from "@lodestar/types"; | ||
| import {BeaconStateAllForks, CachedBeaconStateAllForks} from "../types.js"; | ||
| import {SyncCommitteeWitness} from "./types.js"; | ||
|
|
||
| export function getSyncCommitteesWitness(fork: ForkName, state: BeaconStateAllForks): SyncCommitteeWitness { | ||
| const n1 = state.node; | ||
| let witness: Uint8Array[]; | ||
| let currentSyncCommitteeRoot: Uint8Array; | ||
| let nextSyncCommitteeRoot: Uint8Array; | ||
|
|
||
| if (isForkPostElectra(fork)) { | ||
| const n2 = n1.left; | ||
| const n5 = n2.right; | ||
| const n10 = n5.left; | ||
| const n21 = n10.right; | ||
| const n43 = n21.right; | ||
|
Comment on lines
+21
to
+25
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The direct access to |
||
|
|
||
| currentSyncCommitteeRoot = n43.left.root; // n86 | ||
| nextSyncCommitteeRoot = n43.right.root; // n87 | ||
|
|
||
| // Witness branch is sorted by descending gindex | ||
| witness = [ | ||
| n21.left.root, // 42 | ||
| n10.left.root, // 20 | ||
| n5.right.root, // 11 | ||
| n2.left.root, // 4 | ||
| n1.right.root, // 3 | ||
| ]; | ||
| } else { | ||
| const n3 = n1.right; // [1]0110 | ||
| const n6 = n3.left; // 1[0]110 | ||
| const n13 = n6.right; // 10[1]10 | ||
| const n27 = n13.right; // 101[1]0 | ||
| currentSyncCommitteeRoot = n27.left.root; // n54 1011[0] | ||
| nextSyncCommitteeRoot = n27.right.root; // n55 1011[1] | ||
|
|
||
| // Witness branch is sorted by descending gindex | ||
| witness = [ | ||
| n13.left.root, // 26 | ||
| n6.left.root, // 12 | ||
| n3.right.root, // 7 | ||
| n1.left.root, // 2 | ||
| ]; | ||
| } | ||
|
|
||
| return { | ||
| witness, | ||
| currentSyncCommitteeRoot, | ||
| nextSyncCommitteeRoot, | ||
| }; | ||
| } | ||
|
|
||
| export function getNextSyncCommitteeBranch(syncCommitteesWitness: SyncCommitteeWitness): Uint8Array[] { | ||
| // Witness branch is sorted by descending gindex | ||
| return [syncCommitteesWitness.currentSyncCommitteeRoot, ...syncCommitteesWitness.witness]; | ||
| } | ||
|
|
||
| export function getCurrentSyncCommitteeBranch(syncCommitteesWitness: SyncCommitteeWitness): Uint8Array[] { | ||
| // Witness branch is sorted by descending gindex | ||
| return [syncCommitteesWitness.nextSyncCommitteeRoot, ...syncCommitteesWitness.witness]; | ||
| } | ||
|
|
||
| export function getFinalizedRootProof(state: CachedBeaconStateAllForks): Uint8Array[] { | ||
| const finalizedRootGindex = state.epochCtx.isPostElectra() ? FINALIZED_ROOT_GINDEX_ELECTRA : FINALIZED_ROOT_GINDEX; | ||
| return new Tree(state.node).getSingleProof(BigInt(finalizedRootGindex)); | ||
| } | ||
|
|
||
| export function getBlockBodyExecutionHeaderProof( | ||
| fork: ForkPostBellatrix, | ||
| body: BeaconBlockBody<ForkPostBellatrix> | ||
| ): Uint8Array[] { | ||
| const bodyView = (ssz[fork].BeaconBlockBody as SSZTypesFor<ForkPostBellatrix, "BeaconBlockBody">).toView(body); | ||
| return new Tree(bodyView.node).getSingleProof(BigInt(BLOCK_BODY_EXECUTION_PAYLOAD_GINDEX)); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| /** | ||
| * We aren't creating the sync committee proofs separately because our ssz library automatically adds leaves to composite types, | ||
| * so they're already included in the state proof, currently with no way to specify otherwise | ||
| * | ||
| * remove two offsets so the # of offsets in the state proof will be the # expected | ||
| * This is a hack, but properly setting the offsets in the state proof would require either removing witnesses needed for the committees | ||
| * or setting the roots of the committees in the state proof | ||
| * this will always be 1, syncProofLeavesLength | ||
| * | ||
| * | ||
| * With empty state (minimal) | ||
| * - `genesisTime = 0xffffffff` | ||
| * - `genesisValidatorsRoot = Buffer.alloc(32, 1)` | ||
| * | ||
| * Proof: | ||
| * ``` | ||
| * offsets: [ 5, 4, 3, 2, 1 ] | ||
| * leaves: [ | ||
| * '0xffffffff00000000000000000000000000000000000000000000000000000000', | ||
| * '0x0101010101010101010101010101010101010101010101010101010101010101', | ||
| * '0xb11b8bcf59425d6c99019cca1d2c2e47b51a2f74917a67ad132274f43e13ec43', | ||
| * '0x74bd1f2437cdf74b0904ee525d8da070a3fa27570942bf42cbab3dc5939600f0', | ||
| * '0x7f06739e5a42360c56e519a511675901c95402ea9877edc0d9a87471b1374a6a', | ||
| * '0x9f534204ba3c0b69fcb42a11987bfcbc5aea0463e5b0614312ded4b62cf3a380' | ||
| * ] | ||
| * ``` | ||
| */ | ||
| export type SyncCommitteeWitness = { | ||
| /** Vector[Bytes32, 4] or Vector[Bytes32, 5] depending on the fork */ | ||
| witness: Uint8Array[]; | ||
| currentSyncCommitteeRoot: Uint8Array; | ||
| nextSyncCommitteeRoot: Uint8Array; | ||
| }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this was moved from
lodestar/packages/beacon-node/src/chain/lightClient/proofs.ts
Line 14 in 0e7901d
although it does not look like it belongs to state-transition, it couples with
BeaconStateAllForksand this is the only place we can leave it