Skip to content

Commit

Permalink
Review Dan PR
Browse files Browse the repository at this point in the history
  • Loading branch information
dapplion committed Nov 17, 2022
1 parent e969457 commit 4148785
Show file tree
Hide file tree
Showing 15 changed files with 67 additions and 59 deletions.
2 changes: 1 addition & 1 deletion packages/beacon-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@
"@multiformats/multiaddr": "^11.0.0",
"@types/datastore-level": "^3.0.0",
"buffer-xor": "^2.0.2",
"c-kzg": "^1.0.0",
"c-kzg": "^1.0.4",
"cross-fetch": "^3.1.4",
"datastore-core": "^8.0.1",
"datastore-level": "^9.0.1",
Expand Down
4 changes: 2 additions & 2 deletions packages/beacon-node/src/chain/archiver/archiveBlocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {fromHexString} from "@chainsafe/ssz";
import {Epoch, Slot} from "@lodestar/types";
import {IForkChoice} from "@lodestar/fork-choice";
import {ILogger, toHex} from "@lodestar/utils";
import {ForkSeq, MIN_EPOCHS_FOR_BLOBS_SIDECARS_REQUESTS, SLOTS_PER_EPOCH} from "@lodestar/params";
import {ForkSeq, SLOTS_PER_EPOCH} from "@lodestar/params";
import {computeEpochAtSlot, computeStartSlotAtEpoch} from "@lodestar/state-transition";
import {IKeyValue} from "@lodestar/db";
import {IChainForkConfig} from "@lodestar/config";
Expand Down Expand Up @@ -81,7 +81,7 @@ export async function archiveBlocks(
// Delete expired blobs
// Keep only `[max(GENESIS_EPOCH, current_epoch - MIN_EPOCHS_FOR_BLOBS_SIDECARS_REQUESTS), current_epoch]`
if (finalizedPostEIP4844) {
const blobsSidecarMinEpoch = currentEpoch - MIN_EPOCHS_FOR_BLOBS_SIDECARS_REQUESTS;
const blobsSidecarMinEpoch = currentEpoch - config.MIN_EPOCHS_FOR_BLOBS_SIDECARS_REQUESTS;
if (blobsSidecarMinEpoch > 0) {
const slotsToDelete = await db.blobsSidecarArchive.keys({lt: computeStartSlotAtEpoch(blobsSidecarMinEpoch)});
if (slotsToDelete.length > 0) {
Expand Down
22 changes: 3 additions & 19 deletions packages/beacon-node/src/chain/produceBlock/produceBlockBody.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {blobToKzgCommitment} from "c-kzg";
import {
Bytes32,
phase0,
Expand All @@ -23,7 +22,6 @@ import {
getRandaoMix,
getCurrentEpoch,
isMergeTransitionComplete,
verifyKzgCommitmentsAgainstTransactions,
} from "@lodestar/state-transition";
import {IChainForkConfig} from "@lodestar/config";
import {ForkName, ForkSeq} from "@lodestar/params";
Expand All @@ -34,7 +32,7 @@ import {PayloadId, IExecutionEngine, IExecutionBuilder} from "../../execution/in
import {ZERO_HASH, ZERO_HASH_HEX} from "../../constants/index.js";
import {IEth1ForBlockProduction} from "../../eth1/index.js";
import {numToQuantity} from "../../eth1/provider/utils.js";
import {byteArrayEquals} from "../../util/bytes.js";
import {validateBlobsAndKzgCommitments} from "./validateBlobsAndKzgCommitments.js";

// Time to provide the EL to generate a payload from new payload id
const PAYLOAD_GENERATION_TIME_MS = 500;
Expand Down Expand Up @@ -235,23 +233,9 @@ export async function produceBlockBody<T extends BlockType>(
throw Error(`blobsBundle incorrect blockHash ${blobsBundle.blockHash} != ${blockHash}`);
}

// Optionally sanity-check that the KZG commitments match the versioned hashes in the transactions
if (this.opts.sanityCheckExecutionEngineBlocks) {
// Optionally sanity-check that the KZG commitments match the versioned hashes in the transactions
verifyKzgCommitmentsAgainstTransactions(payload.transactions, blobsBundle.kzgs);

// Optionally sanity-check that the KZG commitments match the blobs (as produced by the execution engine)
if (blobsBundle.blobs.length !== blobsBundle.kzgs.length) {
throw Error(
`Blobs bundle blobs len ${blobsBundle.blobs.length} != kzgs len ${blobsBundle.kzgs.length}`
);
}

for (let i = 0; i < blobsBundle.blobs.length; i++) {
const kzg = blobToKzgCommitment(blobsBundle.blobs[i]) as eip4844.KZGCommitment;
if (!byteArrayEquals(kzg, blobsBundle.kzgs[i])) {
throw Error(`Wrong KZG[${i}] ${toHex(blobsBundle.kzgs[i])} expected ${toHex(kzg)}`);
}
}
validateBlobsAndKzgCommitments(payload, blobsBundle);
}

(blockBody as eip4844.BeaconBlockBody).blobKzgCommitments = blobsBundle.kzgs;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import {blobToKzgCommitment} from "c-kzg";
import {verifyKzgCommitmentsAgainstTransactions} from "@lodestar/state-transition";
import {allForks, eip4844} from "@lodestar/types";
import {toHex} from "@lodestar/utils";
import {BlobsBundle} from "../../execution/index.js";
import {byteArrayEquals} from "../../util/bytes.js";

/**
* Optionally sanity-check that the KZG commitments match the versioned hashes in the transactions
* https://github.com/ethereum/consensus-specs/blob/11a037fd9227e29ee809c9397b09f8cc3383a8c0/specs/eip4844/validator.md#blob-kzg-commitments
*/
export function validateBlobsAndKzgCommitments(payload: allForks.ExecutionPayload, blobsBundle: BlobsBundle): void {
verifyKzgCommitmentsAgainstTransactions(payload.transactions, blobsBundle.kzgs);

// Optionally sanity-check that the KZG commitments match the blobs (as produced by the execution engine)
if (blobsBundle.blobs.length !== blobsBundle.kzgs.length) {
throw Error(`Blobs bundle blobs len ${blobsBundle.blobs.length} != kzgs len ${blobsBundle.kzgs.length}`);
}

for (let i = 0; i < blobsBundle.blobs.length; i++) {
const kzg = blobToKzgCommitment(blobsBundle.blobs[i]) as eip4844.KZGCommitment;
if (!byteArrayEquals(kzg, blobsBundle.kzgs[i])) {
throw Error(`Wrong KZG[${i}] ${toHex(blobsBundle.kzgs[i])} expected ${toHex(kzg)}`);
}
}
}
11 changes: 7 additions & 4 deletions packages/beacon-node/src/execution/engine/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
DATA,
QUANTITY,
quantityToBigint,
dataToRootHex,
} from "../../eth1/provider/utils.js";
import {IJsonRpcHttpClient, ReqOpts} from "../../eth1/provider/jsonRpcHttpClient.js";
import {IMetrics} from "../../metrics/index.js";
Expand Down Expand Up @@ -491,17 +492,19 @@ export function parseExecutionPayload(data: ExecutionPayloadRpc): allForks.Execu

// EIP-4844 adds excessDataGas to the ExecutionPayload
if (data.excessDataGas) {
(payload as eip4844.ExecutionPayload).excessDataGas = BigInt(data.excessDataGas);
(payload as eip4844.ExecutionPayload).excessDataGas = quantityToBigint(data.excessDataGas);
}

return payload;
}

export function parseBlobsBundle(data: BlobsBundleRpc): BlobsBundle {
return {
blockHash: data.blockHash,
kzgs: data.kzgs.map((kzg) => dataToBytes(kzg)),
blobs: data.blobs.map((blob) => dataToBytes(blob)),
// NOTE: Keep as hex, since it's only used for equality downstream
blockHash: dataToRootHex(data.blockHash),
// As of Nov 17th 2022 according to Dan's tests Geth returns null if no blobs in block
kzgs: (data.kzgs ?? []).map((kzg) => dataToBytes(kzg)),
blobs: (data.blobs ?? []).map((blob) => dataToBytes(blob)),
};
}

Expand Down
1 change: 1 addition & 0 deletions packages/beacon-node/src/execution/engine/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ export class ExecutionEngineMock implements IExecutionEngine {

return {
blockHash: toHex(payload.blockHash),
// TODO EIP-4844: Assumes payload has no blob transactions
kzgs: [],
blobs: [],
};
Expand Down
20 changes: 5 additions & 15 deletions packages/beacon-node/src/network/gossip/encoding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,14 @@ export function msgIdFn(gossipTopicCache: GossipTopicCache, msg: Message): Uint8

let vec: Uint8Array[];

switch (topic.fork) {
if (topic.fork === ForkName.phase0) {
// message id for phase0.
// ```
// SHA256(MESSAGE_DOMAIN_VALID_SNAPPY + snappy_decompress(message.data))[:20]
// ```
case ForkName.phase0:
vec = [MESSAGE_DOMAIN_VALID_SNAPPY, msg.data];
break;

// message id for altair.
vec = [MESSAGE_DOMAIN_VALID_SNAPPY, msg.data];
} else {
// message id for altair and subsequent future forks.
// ```
// SHA256(
// MESSAGE_DOMAIN_VALID_SNAPPY +
Expand All @@ -58,15 +56,7 @@ export function msgIdFn(gossipTopicCache: GossipTopicCache, msg: Message): Uint8
// )[:20]
// ```
// https://github.com/ethereum/eth2.0-specs/blob/v1.1.0-alpha.7/specs/altair/p2p-interface.md#topics-and-messages
//
// TODO: check if the capella handling is same as the other forks
case ForkName.altair:
case ForkName.bellatrix:
case ForkName.capella:
case ForkName.eip4844: {
vec = [MESSAGE_DOMAIN_VALID_SNAPPY, intToBytes(msg.topic.length, 8), Buffer.from(msg.topic), msg.data];
break;
}
vec = [MESSAGE_DOMAIN_VALID_SNAPPY, intToBytes(msg.topic.length, 8), Buffer.from(msg.topic), msg.data];
}

return Buffer.from(digest(Buffer.concat(vec))).subarray(0, 20);
Expand Down
1 change: 1 addition & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
"@multiformats/multiaddr": "^11.0.0",
"@types/lockfile": "^1.0.1",
"bip39": "^3.0.2",
"c-kzg": "^1.0.4",
"deepmerge": "^4.2.2",
"ethers": "^5.0.2",
"expand-tilde": "^2.0.2",
Expand Down
14 changes: 14 additions & 0 deletions packages/cli/src/cmds/beacon/handler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import path from "node:path";
import {loadTrustedSetup, transformTrustedSetupJSON} from "c-kzg";
import {Registry} from "prom-client";
import {createSecp256k1PeerId} from "@libp2p/peer-id-factory";
import {createKeypairFromPeerId, ENR} from "@chainsafe/discv5";
Expand All @@ -25,6 +26,19 @@ import {initBeaconState} from "./initBeaconState.js";
export async function beaconHandler(args: IBeaconArgs & IGlobalArgs): Promise<void> {
const {config, options, beaconPaths, network, version, commit, peerId} = await beaconHandlerInit(args);

// TODO EIP-4844, where is the best place to do this?
if (config.EIP4844_FORK_EPOCH < Infinity) {
// Load our KZG trusted setup into C-KZG for later use
const SETUP_FILE_PATH = "testing_trusted_setups.json";
try {
const file = await transformTrustedSetupJSON(SETUP_FILE_PATH);
loadTrustedSetup(file);
} catch (e) {
(e as Error).message = `Error loading trusted setup ${SETUP_FILE_PATH}: ${(e as Error).message}`;
throw e;
}
}

// initialize directories
mkdir(beaconPaths.dataDir);
mkdir(beaconPaths.beaconDir);
Expand Down
5 changes: 3 additions & 2 deletions packages/config/src/chainConfig/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,10 @@ export type IChainConfig = {
DEPOSIT_NETWORK_ID: number;
DEPOSIT_CONTRACT_ADDRESS: Uint8Array;

// Blobs
// https://github.com/ethereum/consensus-specs/blob/dev/specs/eip4844/p2p-interface.md#configuration
// EIP-4844
// https://github.com/ethereum/consensus-specs/blob/11a037fd9227e29ee809c9397b09f8cc3383a8c0/specs/eip4844/p2p-interface.md#configuration
MAX_REQUEST_BLOBS_SIDECARS: number;
/** The minimum epoch range over which a node must serve blobs sidecars */
MIN_EPOCHS_FOR_BLOBS_SIDECARS_REQUESTS: number;
};

Expand Down
6 changes: 0 additions & 6 deletions packages/params/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,3 @@ export const MAX_REQUEST_LIGHT_CLIENT_COMMITTEE_HASHES = 128;
*/
export const SAFE_SLOTS_TO_IMPORT_OPTIMISTICALLY = 128;
export const INTERVALS_PER_SLOT = 3;

// EIP-4844
// https://github.com/ethereum/consensus-specs/blob/11a037fd9227e29ee809c9397b09f8cc3383a8c0/specs/eip4844/p2p-interface.md#configuration
/** The minimum epoch range over which a node must serve blobs sidecars */
export const MIN_EPOCHS_FOR_BLOBS_SIDECARS_REQUESTS = 4096;
export const MAX_REQUEST_BLOBS_SIDECARS = 128;
1 change: 0 additions & 1 deletion packages/params/src/setPreset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ export let userOverrides: Partial<BeaconPreset> | undefined = undefined;
* @param overrides - customized fields
*/
export function setActivePreset(presetName: PresetName, overrides?: Partial<BeaconPreset>): void {
console.log("setActivePreset called with:", {presetName, overrides});
if (presetStatus.frozen) {
throw Error(`Lodestar preset is already frozen. You must call setActivePreset() at the top of your
application entry point, before importing @lodestar/params, or any library that may import it.
Expand Down
1 change: 0 additions & 1 deletion packages/validator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@
"@lodestar/types": "^1.2.1",
"@lodestar/utils": "^1.2.1",
"bigint-buffer": "^1.1.5",
"c-kzg": "^1.0.0",
"cross-fetch": "^3.1.4",
"strict-event-emitter-types": "^2.0.0"
},
Expand Down
4 changes: 0 additions & 4 deletions packages/validator/src/validator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {loadTrustedSetup} from "c-kzg";
import {IDatabaseApiOptions} from "@lodestar/db";
import {BLSPubkey, ssz} from "@lodestar/types";
import {createIBeaconConfig, IBeaconConfig} from "@lodestar/config";
Expand Down Expand Up @@ -72,9 +71,6 @@ export class Validator {
const clock = new Clock(config, logger, {genesisTime: Number(genesis.genesisTime)});
const loggerVc = getLoggerVc(logger, clock);

// Load our KZG trusted setup into C-KZG for later use
loadTrustedSetup("trusted_setup.txt");

let api: Api;
if (typeof opts.api === "string" || Array.isArray(opts.api)) {
// This new api instance can make do with default timeout as a faster timeout is
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4791,10 +4791,10 @@ bytes@3.1.2:
resolved "https://registry.yarnpkg.com/bytes/-/bytes-3.1.2.tgz#8b0beeb98605adf1b128fa4386403c009e0221a5"
integrity sha512-/Nf7TyzTx6S3yRJObOAV7956r8cr2+Oj8AC5dt8wSP3BQAoeX58NoHyCU8P8zGkNXStjTSi6fzO6F0pBdcYbEg==

c-kzg@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/c-kzg/-/c-kzg-1.0.0.tgz#1ba32dbadd3dadf2bf461e25fa833663c3897db8"
integrity sha512-8Oisu9MXJ7X50idvdPMSwzgk9IN4uu6FMQJtQbaHxcYxoN/TAk2sLO72fGgUJDHg0CjV7rPRstbp8x6m+O3fvg==
c-kzg@^1.0.4:
version "1.0.4"
resolved "https://registry.yarnpkg.com/c-kzg/-/c-kzg-1.0.4.tgz#cd3b274cf1ead177d166eec481e0c4eade8b9e16"
integrity sha512-hzEyvdWODqDMkhuxsVatFeXEi2zHR8nXHamNXM3gE91hJ9VTR0G65XVGmxpQwP11Gn8dY54YOo0+q+7AoOZEuA==
dependencies:
node-addon-api "^5.0.0"

Expand Down

0 comments on commit 4148785

Please sign in to comment.