Skip to content
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

Use consistent type-guards in blinded block checks #4560

Merged
merged 1 commit into from
Sep 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 4 additions & 5 deletions packages/state-transition/src/block/processBlockHeader.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {toHexString, byteArrayEquals} from "@chainsafe/ssz";
import {allForks, ssz, bellatrix} from "@lodestar/types";
import {allForks, ssz, isBlindedBeaconBlock} from "@lodestar/types";
import {CachedBeaconStateAllForks} from "../types.js";
import {ZERO_HASH} from "../constants/index.js";

Expand Down Expand Up @@ -37,10 +37,9 @@ export function processBlockHeader(state: CachedBeaconStateAllForks, block: allF
);
}

const bodyRoot =
(block.body as bellatrix.BlindedBeaconBlockBody).executionPayloadHeader !== undefined
? ssz.bellatrix.BlindedBeaconBlockBody.hashTreeRoot(block.body as bellatrix.BlindedBeaconBlockBody)
: types.BeaconBlockBody.hashTreeRoot(block.body as bellatrix.BeaconBlockBody);
const bodyRoot = isBlindedBeaconBlock(block)
? ssz.bellatrix.BlindedBeaconBlockBody.hashTreeRoot(block.body)
: types.BeaconBlockBody.hashTreeRoot(block.body);

// cache current block as the new latest block
state.latestBlockHeader = ssz.phase0.BeaconBlockHeader.toViewDU({
Expand Down
4 changes: 2 additions & 2 deletions packages/state-transition/src/signatureSets/proposer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {DOMAIN_BEACON_PROPOSER} from "@lodestar/params";
import {allForks, ssz} from "@lodestar/types";
import {computeSigningRoot, isBlindedBeaconBlock} from "../util/index.js";
import {allForks, ssz, isBlindedBeaconBlock} from "@lodestar/types";
import {computeSigningRoot} from "../util/index.js";
import {ISignatureSet, SignatureSetType, verifySignatureSet} from "../util/signatureSets.js";
import {CachedBeaconStateAllForks} from "../types.js";

Expand Down
10 changes: 3 additions & 7 deletions packages/state-transition/src/util/bellatrix.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {allForks, bellatrix, ssz} from "@lodestar/types";
import {allForks, bellatrix, isBlindedBeaconBlock, ssz} from "@lodestar/types";
import {
BeaconStateBellatrix,
BeaconStateAllForks,
Expand Down Expand Up @@ -73,8 +73,8 @@ export function isBellatrixBlockBodyType(blockBody: allForks.BeaconBlockBody): b
export function getFullOrBlindedPayload(
block: allForks.FullOrBlindedBeaconBlock
): allForks.FullOrBlindedExecutionPayload {
if ((block as bellatrix.BlindedBeaconBlock).body.executionPayloadHeader !== undefined) {
return (block as bellatrix.BlindedBeaconBlock).body.executionPayloadHeader;
if (isBlindedBeaconBlock(block)) {
return block.body.executionPayloadHeader;
} else if ((block as bellatrix.BeaconBlock).body.executionPayload !== undefined) {
return (block as bellatrix.BeaconBlock).body.executionPayload;
} else {
Expand All @@ -87,7 +87,3 @@ export function isExecutionPayload(
): payload is bellatrix.ExecutionPayload {
return (payload as bellatrix.ExecutionPayload).transactions !== undefined;
}

export function isBlindedBeaconBlock(block: allForks.FullOrBlindedBeaconBlock): block is bellatrix.BlindedBeaconBlock {
return (block as bellatrix.BlindedBeaconBlock).body.executionPayloadHeader !== undefined;
}
2 changes: 2 additions & 0 deletions packages/types/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export * from "./types.js";
export * as ssz from "./sszTypes.js";
// Typeguards
export * from "./utils/typeguards.js";
// String type
export {StringType} from "./utils/StringType.js";
12 changes: 12 additions & 0 deletions packages/types/src/utils/typeguards.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {FullOrBlindedBeaconBlock, FullOrBlindedSignedBeaconBlock} from "../allForks/types.js";
import {ts as bellatrix} from "../bellatrix/index.js";

export function isBlindedBeaconBlock(block: FullOrBlindedBeaconBlock): block is bellatrix.BlindedBeaconBlock {
return (block as bellatrix.BlindedBeaconBlock).body.executionPayloadHeader !== undefined;
}

export function isBlindedSignedBeaconBlock(
signedBlock: FullOrBlindedSignedBeaconBlock
): signedBlock is bellatrix.SignedBlindedBeaconBlock {
return (signedBlock as bellatrix.SignedBlindedBeaconBlock).message.body.executionPayloadHeader !== undefined;
}
4 changes: 2 additions & 2 deletions packages/validator/src/services/block.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {BLSPubkey, Slot, BLSSignature, allForks, bellatrix} from "@lodestar/types";
import {BLSPubkey, Slot, BLSSignature, allForks, bellatrix, isBlindedBeaconBlock} from "@lodestar/types";
import {IChainForkConfig} from "@lodestar/config";
import {ForkName} from "@lodestar/params";
import {extendError, prettyBytes} from "@lodestar/utils";
Expand Down Expand Up @@ -104,7 +104,7 @@ export class BlockProposingService {
}

private publishBlockWrapper = async (signedBlock: allForks.FullOrBlindedSignedBeaconBlock): Promise<void> => {
return (signedBlock.message.body as bellatrix.BlindedBeaconBlockBody).executionPayloadHeader !== undefined
return isBlindedBeaconBlock(signedBlock.message)
? this.api.beacon.publishBlindedBlock(signedBlock as bellatrix.SignedBlindedBeaconBlock)
: this.api.beacon.publishBlock(signedBlock as allForks.SignedBeaconBlock);
};
Expand Down
13 changes: 3 additions & 10 deletions packages/validator/src/util/blindedBlock.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {IChainForkConfig} from "@lodestar/config";
import {allForks, bellatrix, phase0, Root, ssz} from "@lodestar/types";
import {allForks, phase0, Root, ssz, isBlindedBeaconBlock} from "@lodestar/types";

export function blindedOrFullBlockHashTreeRoot(
config: IChainForkConfig,
blindedOrFull: allForks.FullOrBlindedBeaconBlock
): Root {
return isBlindedBlock(blindedOrFull)
return isBlindedBeaconBlock(blindedOrFull)
? // Blinded
ssz.bellatrix.BlindedBeaconBlock.hashTreeRoot(blindedOrFull)
: // Full
Expand All @@ -16,7 +16,7 @@ export function blindedOrFullBlockToHeader(
config: IChainForkConfig,
blindedOrFull: allForks.FullOrBlindedBeaconBlock
): phase0.BeaconBlockHeader {
const bodyRoot = isBlindedBlock(blindedOrFull)
const bodyRoot = isBlindedBeaconBlock(blindedOrFull)
? // Blinded
ssz.bellatrix.BlindedBeaconBlockBody.hashTreeRoot(blindedOrFull.body)
: // Full
Expand All @@ -30,10 +30,3 @@ export function blindedOrFullBlockToHeader(
bodyRoot,
};
}

/** Typeguard for ternary operators */
function isBlindedBlock(
blindedOrFull: allForks.FullOrBlindedBeaconBlock
): blindedOrFull is bellatrix.BlindedBeaconBlock {
return (blindedOrFull.body as bellatrix.BlindedBeaconBlockBody).executionPayloadHeader !== undefined;
}