-
-
Notifications
You must be signed in to change notification settings - Fork 467
feat: update stateTransition interface to take raw block bytes instead #9632
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
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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -4,10 +4,10 @@ import {ACTIVE_PRESET, ForkName} from "@lodestar/params"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import {InputType} from "@lodestar/spec-test-util"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| BeaconStateAllForks, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| BeaconStateView, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| DataAvailabilityStatus, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ExecutionPayloadStatus, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| processSlots, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| stateTransition, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } from "@lodestar/state-transition"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import {SignedBeaconBlock, deneb, ssz} from "@lodestar/types"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import {bnToNum} from "@lodestar/utils"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -60,21 +60,29 @@ const sanityBlocks: TestRunnerFn<SanityBlocksTestCase, BeaconStateAllForks> = (f | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| testFunction: (testcase) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const stateTB = testcase.pre; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let wrappedState = createCachedBeaconStateTest(stateTB, getConfig(fork)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let wrappedState = new BeaconStateView(createCachedBeaconStateTest(stateTB, getConfig(fork))); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const verify = shouldVerify(testcase); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (let i = 0; i < testcase.meta.blocks_count; i++) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const signedBlock = testcase[`blocks_${i}`] as deneb.SignedBeaconBlock; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| wrappedState = stateTransition(wrappedState, signedBlock, { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Assume valid and available for this test | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| executionPayloadStatus: ExecutionPayloadStatus.valid, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| dataAvailabilityStatus: DataAvailabilityStatus.Available, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| verifyStateRoot: verify, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| verifyProposer: verify, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| verifySignatures: verify, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assertCorrectProgressiveBalances, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| wrappedState = wrappedState.stateTransition( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| wrappedState.cachedState.config | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .getForkTypes(signedBlock.message.slot) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .SignedBeaconBlock.serialize(signedBlock), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+63
to
+71
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. Avoid accessing the internal
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| false, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Assume valid and available for this test | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| executionPayloadStatus: ExecutionPayloadStatus.valid, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| dataAvailabilityStatus: DataAvailabilityStatus.Available, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| verifyStateRoot: verify, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| verifyProposer: verify, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| verifySignatures: verify, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assertCorrectProgressiveBalances, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| {} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) as BeaconStateView; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return wrappedState; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return wrappedState.cachedState; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| options: { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| inputTypes: inputTypeSszTreeViewDU, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,5 +1,5 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import {SLOTS_PER_EPOCH} from "@lodestar/params"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import {Epoch, SignedBeaconBlock, SignedBlindedBeaconBlock, Slot, ssz} from "@lodestar/types"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import {Epoch, Slot, ssz} from "@lodestar/types"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import {toRootHex} from "@lodestar/utils"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import {BlockExternalData, DataAvailabilityStatus, ExecutionPayloadStatus} from "./block/externalData.js"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import {processBlock} from "./block/index.js"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -83,7 +83,8 @@ export enum StateHashTreeRootSource { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export function stateTransition( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| state: CachedBeaconStateAllForks, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| signedBlock: SignedBeaconBlock | SignedBlindedBeaconBlock, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| signedBlockBytes: Uint8Array, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| isBlinded: boolean, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| options: StateTransitionOpts = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Assume default to be valid and available | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| executionPayloadStatus: ExecutionPayloadStatus.valid, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -93,8 +94,11 @@ export function stateTransition( | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| ): CachedBeaconStateAllForks { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const {verifyStateRoot = true, verifyProposer = true} = options; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const blockSlot = readSignedBlockSlot(signedBlockBytes); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const signedBlock = isBlinded | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ? state.config.getPostBellatrixForkTypes(blockSlot).SignedBlindedBeaconBlock.deserialize(signedBlockBytes) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| : state.config.getForkTypes(blockSlot).SignedBeaconBlock.deserialize(signedBlockBytes); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const block = signedBlock.message; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const blockSlot = block.slot; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // .clone() before mutating state in state transition | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let postState = state.clone(options.dontTransferCache); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -154,6 +158,20 @@ export function stateTransition( | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| return postState; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| function readSignedBlockSlot(signedBlockBytes: Uint8Array): Slot { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (signedBlockBytes.byteLength < 12) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| throw Error("Invalid signed block bytes: too short to read message offset and slot"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const view = new DataView(signedBlockBytes.buffer, signedBlockBytes.byteOffset, signedBlockBytes.byteLength); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const messageOffset = view.getUint32(0, true); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (messageOffset + 8 > signedBlockBytes.byteLength) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| throw Error("Invalid signed block bytes: message offset out of range"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return Number(view.getBigUint64(messageOffset, true)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+161
to
+173
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 minimum valid length of a serialized Checking Additionally, the
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Like `processSlots` from the spec but additionally handles fork upgrades | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
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.
Avoid accessing the internal
cachedStateproperty ofBeaconStateViewto retrieve the config. SincegetConfig(fork)is already called, we can store theconfigin a local variable and use it directly.